1 /* Copyright (c) 2015-2017. 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. */
7 #include <cstring> // memcpy, memcmp
12 # define MAP_POPULATE MAP_PREFAULT_READ
17 #include "xbt/sysdep.h"
19 #include "src/internal_config.h"
21 #include "src/mc/PageStore.hpp"
23 #include "src/mc/mc_mmu.hpp"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc, "Logging specific to mc_page_snapshot");
30 /** @brief Compute a hash for the given memory page
32 * The page is used before inserting the page in the page store
33 * in order to find duplicate of this page in the page store.
35 * @param data Memory page
36 * @return hash off the page
38 static XBT_ALWAYS_INLINE PageStore::hash_type mc_hash_page(const void* data)
40 const std::uint64_t* values = (const uint64_t*) data;
41 std::size_t n = xbt_pagesize / sizeof(uint64_t);
44 std::uint64_t hash = 5381;
45 for (std::size_t i = 0; i != n; ++i)
46 hash = ((hash << 5) + hash) + values[i];
50 // ***** snapshot_page_manager
52 PageStore::PageStore(size_t size) : memory_(nullptr), capacity_(size), top_index_(0)
54 // Using mmap in order to be able to expand the region by relocating it somewhere else in the virtual memory space:
55 void* memory = ::mmap(nullptr, size << xbt_pagebits, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
56 if (memory == MAP_FAILED)
57 xbt_die("Could not mmap initial snapshot pages.");
60 this->memory_ = memory;
61 this->page_counts_.resize(size);
64 PageStore::~PageStore()
66 ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
69 void PageStore::resize(std::size_t size)
71 size_t old_bytesize = this->capacity_ << xbt_pagebits;
72 size_t new_bytesize = size << xbt_pagebits;
75 // Expand the memory region by moving it into another
76 // virtual memory address if necessary:
78 new_memory = mremap(this->memory_, old_bytesize, new_bytesize, MREMAP_MAYMOVE);
79 if (new_memory == MAP_FAILED)
80 xbt_die("Could not mremap snapshot pages.");
82 if (new_bytesize > old_bytesize) {
83 // Grow: first try to add new space after current map
84 new_memory = mmap((char *)this->memory_ + old_bytesize,
85 new_bytesize-old_bytesize,
87 MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE,
89 if (new_memory == MAP_FAILED)
90 xbt_die("Could not mremap snapshot pages.");
91 // Check if expanding worked
92 if (new_memory != (char *)this->memory_ + old_bytesize) {
93 // New memory segment could not be put at the end of this->memory_,
94 // so cancel this one and try to rellocate everything and copy data
95 munmap(new_memory, new_bytesize-old_bytesize);
96 new_memory = mmap(nullptr,
99 MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE,
101 if (new_memory == MAP_FAILED)
102 xbt_die("Could not mremap snapshot pages.");
103 memcpy(new_memory, this->memory_, old_bytesize);
104 munmap(this->memory_, old_bytesize);
108 // We don't have functions to shrink a mapping, so leave memory as
110 new_memory = this->memory_;
114 this->capacity_ = size;
115 this->memory_ = new_memory;
116 this->page_counts_.resize(size, 0);
119 /** Allocate a free page
121 * @return index of the free page
123 std::size_t PageStore::alloc_page()
125 if (this->free_pages_.empty()) {
127 // Expand the region:
128 if (this->top_index_ == this->capacity_)
129 // All the pages are allocated, we need add more pages:
130 this->resize(2 * this->capacity_);
132 // Use a page from the top:
133 return this->top_index_++;
137 // Use a page from free_pages_ (inside of the region):
138 size_t res = this->free_pages_[this->free_pages_.size() - 1];
139 this->free_pages_.pop_back();
144 void PageStore::remove_page(std::size_t pageno)
146 this->free_pages_.push_back(pageno);
147 const void* page = this->get_page(pageno);
148 hash_type hash = mc_hash_page(page);
149 this->hash_index_[hash].erase(pageno);
152 /** Store a page in memory */
153 std::size_t PageStore::store_page(void* page)
155 xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
157 // First, we check if a page with the same content is already in the page store:
158 // 1. compute the hash of the page
159 // 2. find pages with the same hash using `hash_index_`
160 // 3. find a page with the same content
161 hash_type hash = mc_hash_page(page);
163 // Try to find a duplicate in set of pages with the same hash:
164 page_set_type& page_set = this->hash_index_[hash];
165 for (size_t const& pageno : page_set) {
166 const void* snapshot_page = this->get_page(pageno);
167 if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
169 // If a page with the same content is already in the page store it's reused and its refcount is incremented.
170 page_counts_[pageno]++;
176 // Otherwise, a new page is allocated in the page store and the content of the page is `memcpy()`-ed to this new page.
177 std::size_t pageno = alloc_page();
178 xbt_assert(this->page_counts_[pageno]==0, "Allocated page is already used");
179 void* snapshot_page = (void*) this->get_page(pageno);
180 memcpy(snapshot_page, page, xbt_pagesize);
181 page_set.insert(pageno);
182 page_counts_[pageno]++;
195 #include <sys/mman.h>
199 #include "src/mc/PageStore.hpp"
201 static int value = 0;
203 static void new_content(void* data, std::size_t size)
205 ::memset(data, ++value, size);
208 static void* getpage()
210 return mmap(nullptr, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
213 XBT_TEST_SUITE("mc_page_store", "Page store");
215 XBT_TEST_UNIT("base", test_mc_page_store, "Test adding/removing pages in the store")
217 using simgrid::mc::PageStore;
219 xbt_test_add("Init");
220 std::size_t pagesize = (size_t) getpagesize();
221 std::unique_ptr<PageStore> store = std::unique_ptr<PageStore>(new simgrid::mc::PageStore(500));
222 void* data = getpage();
223 xbt_test_assert(store->size()==0, "Bad size");
225 xbt_test_add("Store the page once");
226 new_content(data, pagesize);
227 size_t pageno1 = store->store_page(data);
228 xbt_test_assert(store->get_ref(pageno1)==1, "Bad refcount");
229 const void* copy = store->get_page(pageno1);
230 xbt_test_assert(::memcmp(data, copy, pagesize)==0, "Page data should be the same");
231 xbt_test_assert(store->size()==1, "Bad size");
233 xbt_test_add("Store the same page again");
234 size_t pageno2 = store->store_page(data);
235 xbt_test_assert(pageno1==pageno2, "Page should be the same");
236 xbt_test_assert(store->get_ref(pageno1)==2, "Bad refcount");
237 xbt_test_assert(store->size()==1, "Bad size");
239 xbt_test_add("Store a new page");
240 new_content(data, pagesize);
241 size_t pageno3 = store->store_page(data);
242 xbt_test_assert(pageno1 != pageno3, "New page should be different");
243 xbt_test_assert(store->size()==2, "Bad size");
245 xbt_test_add("Unref pages");
246 store->unref_page(pageno1);
247 xbt_assert(store->get_ref(pageno1)==1, "Bad refcount");
248 xbt_assert(store->size()==2, "Bad size");
249 store->unref_page(pageno2);
250 xbt_test_assert(store->size()==1, "Bad size");
252 xbt_test_add("Reallocate page");
253 new_content(data, pagesize);
254 size_t pageno4 = store->store_page(data);
255 xbt_test_assert(pageno1 == pageno4, "Page was not reused");
256 xbt_test_assert(store->get_ref(pageno4)==1, "Bad refcount");
257 xbt_test_assert(store->size()==2, "Bad size");
260 #endif /* SIMGRID_TEST */