X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/a24865789e18ecc8625a9518db00c95607d541af..037ed0177fab7a21eee2439624cbb4c4db8ccc31:/src/mc/mc_page_store.cpp diff --git a/src/mc/mc_page_store.cpp b/src/mc/mc_page_store.cpp index b6ba6f74cb..f2c01ee92e 100644 --- a/src/mc/mc_page_store.cpp +++ b/src/mc/mc_page_store.cpp @@ -7,12 +7,18 @@ #include #include // memcpy, memcp +#include + #include #include #include "mc_page_store.h" +#ifdef MC_PAGE_STORE_MD4 +#include +#endif + #include "mc_mmu.h" XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc, @@ -23,14 +29,22 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc, /** @brief Compte a hash for the given memory page * * The page is used before inserting the page in the page store - * in order to find duplicate of this pae in the page store. + * in order to find duplicate of this page in the page store. * * @param data Memory page * @return hash off the page */ static inline __attribute__ ((always_inline)) -uint64_t mc_hash_page(const void* data) +s_mc_pages_store::hash_type mc_hash_page(const void* data) { +#ifdef MC_PAGE_STORE_MD4 + boost::array result; + md4_ctx context; + md4_init(&context); + md4_update(&context, xbt_pagesize, (const uint8_t*) data); + md4_digest(&context, MD4_DIGEST_SIZE, (uint8_t*) &(result[0])); + return result; +#else const uint64_t* values = (const uint64_t*) data; size_t n = xbt_pagesize / sizeof(uint64_t); @@ -40,6 +54,7 @@ uint64_t mc_hash_page(const void* data) hash = ((hash << 5) + hash) + values[i]; } return hash; +#endif } // ***** snapshot_page_manager @@ -114,14 +129,17 @@ void s_mc_pages_store::remove_page(size_t pageno) { this->free_pages_.push_back(pageno); const void* page = this->get_page(pageno); - uint64_t hash = mc_hash_page(page); + hash_type hash = mc_hash_page(page); +#ifdef MC_PAGE_STORE_MD4 + this->hash_index_.erase(hash); +#else this->hash_index_[hash].erase(pageno); +#endif } /** Store a page in memory */ size_t s_mc_pages_store::store_page(void* page) { - xbt_assert(mc_page_offset(page)==0, "Not at the beginning of a page"); xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent"); // First, we check if a page with the same content is already in the page @@ -129,7 +147,20 @@ size_t s_mc_pages_store::store_page(void* page) // 1. compute the hash of the page; // 2. find pages with the same hash using `hash_index_`; // 3. find a page with the same content. - uint64_t hash = mc_hash_page(page); + hash_type hash = mc_hash_page(page); +#ifdef MC_PAGE_STORE_MD4 + s_mc_pages_store::pages_map_type::const_iterator i = + this->hash_index_.find(hash); + if (i!=this->hash_index_.cend()) { + // If a page with the same content is already in the page store it is + // reused and its reference count is incremented. + size_t pageno = i->second; + page_counts_[pageno]++; + return pageno; + } +#else + + // Try to find a duplicate in set of pages with the same hash: page_set_type& page_set = this->hash_index_[hash]; BOOST_FOREACH (size_t pageno, page_set) { const void* snapshot_page = this->get_page(pageno); @@ -142,6 +173,7 @@ size_t s_mc_pages_store::store_page(void* page) } } +#endif // Otherwise, a new page is allocated in the page store and the content // of the page is `memcpy()`-ed to this new page. @@ -149,7 +181,11 @@ size_t s_mc_pages_store::store_page(void* page) xbt_assert(this->page_counts_[pageno]==0, "Allocated page is already used"); void* snapshot_page = (void*) this->get_page(pageno); memcpy(snapshot_page, page, xbt_pagesize); +#ifdef MC_PAGE_STORE_MD4 + this->hash_index_[hash] = pageno; +#else page_set.insert(pageno); +#endif page_counts_[pageno]++; return pageno; } @@ -163,4 +199,84 @@ mc_pages_store_t mc_pages_store_new() return new s_mc_pages_store_t(500); } +void mc_pages_store_delete(mc_pages_store_t store) +{ + delete store; +} + +} + +#ifdef SIMGRID_TEST + +#include +#include +#include +#include +#include + +#include + +#include "mc/mc_page_store.h" + +static int value = 0; + +static void new_content(void* data, size_t size) +{ + memset(data, ++value, size); +} + +static void* getpage() +{ + return mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); +} + +extern "C" { + +XBT_TEST_SUITE("mc_page_store", "Page store"); + +XBT_TEST_UNIT("base", test_mc_page_store, "Test adding/removing pages in the store") +{ + xbt_test_add("Init"); + size_t pagesize = (size_t) getpagesize(); + std::auto_ptr store = std::auto_ptr(new s_mc_pages_store(500)); + void* data = getpage(); + xbt_test_assert(store->size()==0, "Bad size"); + + xbt_test_add("Store the page once"); + new_content(data, pagesize); + size_t pageno1 = store->store_page(data); + xbt_test_assert(store->get_ref(pageno1)==1, "Bad refcount"); + const void* copy = store->get_page(pageno1); + xbt_test_assert(memcmp(data, copy, pagesize)==0, "Page data should be the same"); + xbt_test_assert(store->size()==1, "Bad size"); + + xbt_test_add("Store the same page again"); + size_t pageno2 = store->store_page(data); + xbt_test_assert(pageno1==pageno2, "Page should be the same"); + xbt_test_assert(store->get_ref(pageno1)==2, "Bad refcount"); + xbt_test_assert(store->size()==1, "Bad size"); + + xbt_test_add("Store a new page"); + new_content(data, pagesize); + size_t pageno3 = store->store_page(data); + xbt_test_assert(pageno1 != pageno3, "New page should be different"); + xbt_test_assert(store->size()==2, "Bad size"); + + xbt_test_add("Unref pages"); + store->unref_page(pageno1); + xbt_assert(store->get_ref(pageno1)==1, "Bad refcount"); + xbt_assert(store->size()==2, "Bad size"); + store->unref_page(pageno2); + xbt_test_assert(store->size()==1, "Bad size"); + + xbt_test_add("Reallocate page"); + new_content(data, pagesize); + size_t pageno4 = store->store_page(data); + xbt_test_assert(pageno1 == pageno4, "Page was not reused"); + xbt_test_assert(store->get_ref(pageno4)==1, "Bad refcount"); + xbt_test_assert(store->size()==2, "Bad size"); } + +} + +#endif /* SIMGRID_TEST */