1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include <string.h> // memcpy, memcp
14 #include "src/mc/PageStore.hpp"
16 #include "src/mc/mc_mmu.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc,
19 "Logging specific to mc_page_snapshot");
24 /** @brief Compte a hash for the given memory page
26 * The page is used before inserting the page in the page store
27 * in order to find duplicate of this page in the page store.
29 * @param data Memory page
30 * @return hash off the page
32 static inline __attribute__ ((always_inline))
33 PageStore::hash_type mc_hash_page(const void* data)
35 const std::uint64_t* values = (const uint64_t*) data;
36 std::size_t n = xbt_pagesize / sizeof(uint64_t);
39 std::uint64_t hash = 5381;
40 for (std::size_t i = 0; i != n; ++i)
41 hash = ((hash << 5) + hash) + values[i];
45 // ***** snapshot_page_manager
47 PageStore::PageStore(size_t size) :
48 memory_(nullptr), capacity_(0), top_index_(0)
50 // Using mmap in order to be able to expand the region
51 // by relocating it somewhere else in the virtual memory
53 void* memory = ::mmap(nullptr, size << xbt_pagebits, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
54 if (memory == MAP_FAILED)
55 xbt_die("Could not mmap initial snapshot pages.");
58 this->capacity_ = size;
59 this->memory_ = memory;
60 this->page_counts_.resize(size);
63 PageStore::~PageStore()
65 ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
68 void PageStore::resize(std::size_t size)
70 size_t old_bytesize = this->capacity_ << xbt_pagebits;
71 size_t new_bytesize = size << xbt_pagebits;
73 // Expand the memory region by moving it into another
74 // virtual memory address if necessary:
75 void* new_memory = mremap(this->memory_, old_bytesize, new_bytesize, MREMAP_MAYMOVE);
76 if (new_memory == MAP_FAILED)
77 xbt_die("Could not mremap snapshot pages.");
79 this->capacity_ = size;
80 this->memory_ = new_memory;
81 this->page_counts_.resize(size, 0);
84 /** Allocate a free page
86 * @return index of the free page
88 std::size_t PageStore::alloc_page()
90 if (this->free_pages_.empty()) {
93 if (this->top_index_ == this->capacity_)
94 // All the pages are allocated, we need add more pages:
95 this->resize(2 * this->capacity_);
97 // Use a page from the top:
98 return this->top_index_++;
102 // Use a page from free_pages_ (inside of the region):
103 size_t res = this->free_pages_[this->free_pages_.size() - 1];
104 this->free_pages_.pop_back();
110 void PageStore::remove_page(std::size_t pageno)
112 this->free_pages_.push_back(pageno);
113 const void* page = this->get_page(pageno);
114 hash_type hash = mc_hash_page(page);
115 this->hash_index_[hash].erase(pageno);
118 /** Store a page in memory */
119 std::size_t PageStore::store_page(void* page)
121 xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
123 // First, we check if a page with the same content is already in the page
125 // 1. compute the hash of the page;
126 // 2. find pages with the same hash using `hash_index_`;
127 // 3. find a page with the same content.
128 hash_type hash = mc_hash_page(page);
130 // Try to find a duplicate in set of pages with the same hash:
131 page_set_type& page_set = this->hash_index_[hash];
132 for (size_t pageno : page_set) {
133 const void* snapshot_page = this->get_page(pageno);
134 if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
136 // If a page with the same content is already in the page store it is
137 // reused and its reference count is incremented.
138 page_counts_[pageno]++;
144 // Otherwise, a new page is allocated in the page store and the content
145 // of the page is `memcpy()`-ed to this new page.
146 std::size_t pageno = alloc_page();
147 xbt_assert(this->page_counts_[pageno]==0, "Allocated page is already used");
148 void* snapshot_page = (void*) this->get_page(pageno);
149 memcpy(snapshot_page, page, xbt_pagesize);
150 page_set.insert(pageno);
151 page_counts_[pageno]++;
164 #include <sys/mman.h>
168 #include "src/mc/PageStore.hpp"
170 static int value = 0;
172 static void new_content(void* data, std::size_t size)
174 ::memset(data, ++value, size);
177 static void* getpage()
179 return mmap(nullptr, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
182 XBT_TEST_SUITE("mc_page_store", "Page store");
184 XBT_TEST_UNIT("base", test_mc_page_store, "Test adding/removing pages in the store")
186 using simgrid::mc::PageStore;
188 xbt_test_add("Init");
189 std::size_t pagesize = (size_t) getpagesize();
190 std::unique_ptr<PageStore> store
191 = std::unique_ptr<PageStore>(new simgrid::mc::PageStore(500));
192 void* data = getpage();
193 xbt_test_assert(store->size()==0, "Bad size");
195 xbt_test_add("Store the page once");
196 new_content(data, pagesize);
197 size_t pageno1 = store->store_page(data);
198 xbt_test_assert(store->get_ref(pageno1)==1, "Bad refcount");
199 const void* copy = store->get_page(pageno1);
200 xbt_test_assert(::memcmp(data, copy, pagesize)==0, "Page data should be the same");
201 xbt_test_assert(store->size()==1, "Bad size");
203 xbt_test_add("Store the same page again");
204 size_t pageno2 = store->store_page(data);
205 xbt_test_assert(pageno1==pageno2, "Page should be the same");
206 xbt_test_assert(store->get_ref(pageno1)==2, "Bad refcount");
207 xbt_test_assert(store->size()==1, "Bad size");
209 xbt_test_add("Store a new page");
210 new_content(data, pagesize);
211 size_t pageno3 = store->store_page(data);
212 xbt_test_assert(pageno1 != pageno3, "New page should be different");
213 xbt_test_assert(store->size()==2, "Bad size");
215 xbt_test_add("Unref pages");
216 store->unref_page(pageno1);
217 xbt_assert(store->get_ref(pageno1)==1, "Bad refcount");
218 xbt_assert(store->size()==2, "Bad size");
219 store->unref_page(pageno2);
220 xbt_test_assert(store->size()==1, "Bad size");
222 xbt_test_add("Reallocate page");
223 new_content(data, pagesize);
224 size_t pageno4 = store->store_page(data);
225 xbt_test_assert(pageno1 == pageno4, "Page was not reused");
226 xbt_test_assert(store->get_ref(pageno4)==1, "Bad refcount");
227 xbt_test_assert(store->size()==2, "Bad size");
230 #endif /* SIMGRID_TEST */