Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modified snapshot and pagestore tests.
[simgrid.git] / src / mc / snapshot / PageStore.cpp
1 /* Copyright (c) 2015-2018. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <cstring> // memcpy, memcmp
8 #include <unistd.h>
9
10 #include <sys/mman.h>
11 #ifdef __FreeBSD__
12 # define MAP_POPULATE MAP_PREFAULT_READ
13 #endif
14
15 #include "xbt/base.h"
16 #include "xbt/log.h"
17 #include "xbt/sysdep.h"
18
19 #include "src/internal_config.h"
20
21 #include "src/mc/PageStore.hpp"
22
23 #include "src/mc/mc_mmu.hpp"
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc, "Logging specific to mc_page_snapshot");
26
27 namespace simgrid {
28 namespace mc {
29
30 /** @brief Compute a hash for the given memory page
31  *
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.
34  *
35  *  @param data Memory page
36  *  @return hash off the page
37  */
38 static XBT_ALWAYS_INLINE PageStore::hash_type mc_hash_page(const void* data)
39 {
40   const std::uint64_t* values = (const uint64_t*) data;
41   std::size_t n = xbt_pagesize / sizeof(uint64_t);
42
43   // This djb2:
44   std::uint64_t hash = 5381;
45   for (std::size_t i = 0; i != n; ++i)
46     hash = ((hash << 5) + hash) + values[i];
47   return hash;
48 }
49
50 // ***** snapshot_page_manager
51
52 PageStore::PageStore(size_t size) : memory_(nullptr), capacity_(size), top_index_(0)
53 {
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.");
58
59   this->top_index_ = 0;
60   this->memory_ = memory;
61   this->page_counts_.resize(size);
62 }
63
64 PageStore::~PageStore()
65 {
66   ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
67 }
68
69 void PageStore::resize(std::size_t size)
70 {
71   size_t old_bytesize = this->capacity_ << xbt_pagebits;
72   size_t new_bytesize = size << xbt_pagebits;
73   void *new_memory;
74
75   // Expand the memory region by moving it into another
76   // virtual memory address if necessary:
77 #if HAVE_MREMAP
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.");
81 #else
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,
86                       PROT_READ|PROT_WRITE,
87                       MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE,
88                       -1, 0);
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,
97                         new_bytesize,
98                         PROT_READ|PROT_WRITE,
99                         MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE,
100                         -1, 0);
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);
105     }
106   }
107   else {
108     // We don't have functions to shrink a mapping, so leave memory as
109     // it is for now
110     new_memory = this->memory_;
111   }
112 #endif
113
114   this->capacity_ = size;
115   this->memory_ = new_memory;
116   this->page_counts_.resize(size, 0);
117 }
118
119 /** Allocate a free page
120  *
121  *  @return index of the free page
122  */
123 std::size_t PageStore::alloc_page()
124 {
125   if (this->free_pages_.empty()) {
126
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_);
131
132     // Use a page from the top:
133     return this->top_index_++;
134
135   } else {
136
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();
140     return res;
141   }
142 }
143
144 void PageStore::remove_page(std::size_t pageno)
145 {
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);
150 }
151
152 /** Store a page in memory */
153 std::size_t PageStore::store_page(void* page)
154 {
155   xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
156
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);
162
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) {
168
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]++;
171       return pageno;
172
173     }
174   }
175
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]++;
183   return pageno;
184 }
185
186 }
187 }
188
189 #ifdef SIMGRID_TEST
190
191 #include <cstring>
192 #include <cstdint>
193
194 #include <unistd.h>
195 #include <sys/mman.h>
196
197 #include <memory>
198
199 #include "src/mc/PageStore.hpp"
200
201 static int value = 0;
202
203 static void new_content(void* data, std::size_t size)
204 {
205   ::memset(data, ++value, size);
206 }
207
208 static void* getpage()
209 {
210   return mmap(nullptr, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
211 }
212
213 XBT_TEST_SUITE("mc_page_store", "Page store");
214
215 XBT_TEST_UNIT("base", test_mc_page_store, "Test adding/removing pages in the store")
216 {
217   using simgrid::mc::PageStore;
218
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");
224
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");
232
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");
238
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");
244
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");
251
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");
258 }
259
260 #endif /* SIMGRID_TEST */