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, memcmp
12 # define MAP_POPULATE MAP_PREFAULT_READ
17 #include <xbt/sysdep.h>
19 #include "src/mc/PageStore.hpp"
21 #include "src/mc/mc_mmu.h"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc,
24 "Logging specific to mc_page_snapshot");
29 /** @brief Compute a hash for the given memory page
31 * The page is used before inserting the page in the page store
32 * in order to find duplicate of this page in the page store.
34 * @param data Memory page
35 * @return hash off the page
37 static inline __attribute__ ((always_inline))
38 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) :
53 memory_(nullptr), capacity_(0), top_index_(0)
55 // Using mmap in order to be able to expand the region
56 // by relocating it somewhere else in the virtual memory
58 void* memory = ::mmap(nullptr, size << xbt_pagebits, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
59 if (memory == MAP_FAILED)
60 xbt_die("Could not mmap initial snapshot pages.");
63 this->capacity_ = size;
64 this->memory_ = memory;
65 this->page_counts_.resize(size);
68 PageStore::~PageStore()
70 ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
73 void PageStore::resize(std::size_t size)
75 size_t old_bytesize = this->capacity_ << xbt_pagebits;
76 size_t new_bytesize = size << xbt_pagebits;
79 // Expand the memory region by moving it into another
80 // virtual memory address if necessary:
82 new_memory = mremap(this->memory_, old_bytesize, new_bytesize, MREMAP_MAYMOVE);
83 if (new_memory == MAP_FAILED)
84 xbt_die("Could not mremap snapshot pages.");
86 if (new_bytesize > old_bytesize) {
87 // Grow: first try to add new space after current map
88 new_memory = mmap((char *)this->memory_ + old_bytesize,
89 new_bytesize-old_bytesize,
91 MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE,
93 if (new_memory == MAP_FAILED)
94 xbt_die("Could not mremap snapshot pages.");
95 // Check if expanding worked
96 if (new_memory != (char *)this->memory_ + old_bytesize) {
97 // New memory segment could not be put at the end of this->memory_,
98 // so cancel this one and try to rellocate everything and copy data
99 munmap(new_memory, new_bytesize-old_bytesize);
100 new_memory = mmap(nullptr,
102 PROT_READ|PROT_WRITE,
103 MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE,
105 if (new_memory == MAP_FAILED)
106 xbt_die("Could not mremap snapshot pages.");
107 memcpy(new_memory, this->memory_, old_bytesize);
108 munmap(this->memory_, old_bytesize);
112 // We don't have functions to shrink a mapping, so leave memory as
114 new_memory = this->memory_;
118 this->capacity_ = size;
119 this->memory_ = new_memory;
120 this->page_counts_.resize(size, 0);
123 /** Allocate a free page
125 * @return index of the free page
127 std::size_t PageStore::alloc_page()
129 if (this->free_pages_.empty()) {
131 // Expand the region:
132 if (this->top_index_ == this->capacity_)
133 // All the pages are allocated, we need add more pages:
134 this->resize(2 * this->capacity_);
136 // Use a page from the top:
137 return this->top_index_++;
141 // Use a page from free_pages_ (inside of the region):
142 size_t res = this->free_pages_[this->free_pages_.size() - 1];
143 this->free_pages_.pop_back();
149 void PageStore::remove_page(std::size_t pageno)
151 this->free_pages_.push_back(pageno);
152 const void* page = this->get_page(pageno);
153 hash_type hash = mc_hash_page(page);
154 this->hash_index_[hash].erase(pageno);
157 /** Store a page in memory */
158 std::size_t PageStore::store_page(void* page)
160 xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
162 // First, we check if a page with the same content is already in the page
164 // 1. compute the hash of the page;
165 // 2. find pages with the same hash using `hash_index_`;
166 // 3. find a page with the same content.
167 hash_type hash = mc_hash_page(page);
169 // Try to find a duplicate in set of pages with the same hash:
170 page_set_type& page_set = this->hash_index_[hash];
171 for (size_t pageno : page_set) {
172 const void* snapshot_page = this->get_page(pageno);
173 if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
175 // If a page with the same content is already in the page store it is
176 // reused and its reference count is incremented.
177 page_counts_[pageno]++;
183 // Otherwise, a new page is allocated in the page store and the content
184 // of the page is `memcpy()`-ed to this new page.
185 std::size_t pageno = alloc_page();
186 xbt_assert(this->page_counts_[pageno]==0, "Allocated page is already used");
187 void* snapshot_page = (void*) this->get_page(pageno);
188 memcpy(snapshot_page, page, xbt_pagesize);
189 page_set.insert(pageno);
190 page_counts_[pageno]++;
203 #include <sys/mman.h>
207 #include "src/mc/PageStore.hpp"
209 static int value = 0;
211 static void new_content(void* data, std::size_t size)
213 ::memset(data, ++value, size);
216 static void* getpage()
218 return mmap(nullptr, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
221 XBT_TEST_SUITE("mc_page_store", "Page store");
223 XBT_TEST_UNIT("base", test_mc_page_store, "Test adding/removing pages in the store")
225 using simgrid::mc::PageStore;
227 xbt_test_add("Init");
228 std::size_t pagesize = (size_t) getpagesize();
229 std::unique_ptr<PageStore> store
230 = std::unique_ptr<PageStore>(new simgrid::mc::PageStore(500));
231 void* data = getpage();
232 xbt_test_assert(store->size()==0, "Bad size");
234 xbt_test_add("Store the page once");
235 new_content(data, pagesize);
236 size_t pageno1 = store->store_page(data);
237 xbt_test_assert(store->get_ref(pageno1)==1, "Bad refcount");
238 const void* copy = store->get_page(pageno1);
239 xbt_test_assert(::memcmp(data, copy, pagesize)==0, "Page data should be the same");
240 xbt_test_assert(store->size()==1, "Bad size");
242 xbt_test_add("Store the same page again");
243 size_t pageno2 = store->store_page(data);
244 xbt_test_assert(pageno1==pageno2, "Page should be the same");
245 xbt_test_assert(store->get_ref(pageno1)==2, "Bad refcount");
246 xbt_test_assert(store->size()==1, "Bad size");
248 xbt_test_add("Store a new page");
249 new_content(data, pagesize);
250 size_t pageno3 = store->store_page(data);
251 xbt_test_assert(pageno1 != pageno3, "New page should be different");
252 xbt_test_assert(store->size()==2, "Bad size");
254 xbt_test_add("Unref pages");
255 store->unref_page(pageno1);
256 xbt_assert(store->get_ref(pageno1)==1, "Bad refcount");
257 xbt_assert(store->size()==2, "Bad size");
258 store->unref_page(pageno2);
259 xbt_test_assert(store->size()==1, "Bad size");
261 xbt_test_add("Reallocate page");
262 new_content(data, pagesize);
263 size_t pageno4 = store->store_page(data);
264 xbt_test_assert(pageno1 == pageno4, "Page was not reused");
265 xbt_test_assert(store->get_ref(pageno4)==1, "Bad refcount");
266 xbt_test_assert(store->size()==2, "Bad size");
269 #endif /* SIMGRID_TEST */