Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add alternative to remap() in PageStore when not available.
[simgrid.git] / src / mc / PageStore.cpp
1 /* Copyright (c) 2015. 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 <unistd.h>
8 #include <string.h> // memcpy, memcmp
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/mc/PageStore.hpp"
20
21 #include "src/mc/mc_mmu.h"
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc,
24                                 "Logging specific to mc_page_snapshot");
25
26 namespace simgrid {
27 namespace mc {
28
29 /** @brief Compute a hash for the given memory page
30  *
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.
33  *
34  *  @param data Memory page
35  *  @return hash off the page
36  */
37 static inline  __attribute__ ((always_inline))
38 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) :
53   memory_(nullptr), capacity_(0), top_index_(0)
54 {
55   // Using mmap in order to be able to expand the region
56   // by relocating it somewhere else in the virtual memory
57   // space:
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.");
61
62   this->top_index_ = 0;
63   this->capacity_ = size;
64   this->memory_ = memory;
65   this->page_counts_.resize(size);
66 }
67
68 PageStore::~PageStore()
69 {
70   ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
71 }
72
73 void PageStore::resize(std::size_t size)
74 {
75   size_t old_bytesize = this->capacity_ << xbt_pagebits;
76   size_t new_bytesize = size << xbt_pagebits;
77   void *new_memory;
78
79   // Expand the memory region by moving it into another
80   // virtual memory address if necessary:
81 #if HAVE_MREMAP
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.");
85 #else
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,
90                       PROT_READ|PROT_WRITE,
91                       MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE,
92                       -1, 0);
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,
101                         new_bytesize,
102                         PROT_READ|PROT_WRITE,
103                         MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE,
104                         -1, 0);
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);
109     }
110   }
111   else {
112     // We don't have functions to shrink a mapping, so leave memory as
113     // it is for now
114     new_memory = this->memory_;
115   }
116 #endif
117
118   this->capacity_ = size;
119   this->memory_ = new_memory;
120   this->page_counts_.resize(size, 0);
121 }
122
123 /** Allocate a free page
124  *
125  *  @return index of the free page
126  */
127 std::size_t PageStore::alloc_page()
128 {
129   if (this->free_pages_.empty()) {
130
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_);
135
136     // Use a page from the top:
137     return this->top_index_++;
138
139   } else {
140
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();
144     return res;
145
146   }
147 }
148
149 void PageStore::remove_page(std::size_t pageno)
150 {
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);
155 }
156
157 /** Store a page in memory */
158 std::size_t PageStore::store_page(void* page)
159 {
160   xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
161
162   // First, we check if a page with the same content is already in the page
163   // store:
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);
168
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) {
174
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]++;
178       return pageno;
179
180     }
181   }
182
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]++;
191   return pageno;
192 }
193
194 }
195 }
196
197 #ifdef SIMGRID_TEST
198
199 #include <cstring>
200 #include <cstdint>
201
202 #include <unistd.h>
203 #include <sys/mman.h>
204
205 #include <memory>
206
207 #include "src/mc/PageStore.hpp"
208
209 static int value = 0;
210
211 static void new_content(void* data, std::size_t size)
212 {
213   ::memset(data, ++value, size);
214 }
215
216 static void* getpage()
217 {
218   return mmap(nullptr, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
219 }
220
221 XBT_TEST_SUITE("mc_page_store", "Page store");
222
223 XBT_TEST_UNIT("base", test_mc_page_store, "Test adding/removing pages in the store")
224 {
225   using simgrid::mc::PageStore;
226   
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");
233
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");
241
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");
247
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");
253
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");
260
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");
267 }
268
269 #endif /* SIMGRID_TEST */