Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into mc
[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, memcp
9
10 #include <sys/mman.h>
11
12 #include <boost/foreach.hpp>
13
14 #include <xbt.h>
15
16 #include "PageStore.hpp"
17
18 #ifdef MC_PAGE_STORE_MD4
19 #include <nettle/md4.h>
20 #endif
21
22 #include "mc_mmu.h"
23
24 extern "C" {
25
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc,
27                                 "Logging specific to mc_page_snapshot");
28
29 namespace simgrid {
30 namespace mc {
31
32 /** @brief Compte a hash for the given memory page
33  *
34  *  The page is used before inserting the page in the page store
35  *  in order to find duplicate of this page in the page store.
36  *
37  *  @param data Memory page
38  *  @return hash off the page
39  */
40 static inline  __attribute__ ((always_inline))
41 PageStore::hash_type mc_hash_page(const void* data)
42 {
43 #ifdef MC_PAGE_STORE_MD4
44    boost::array<uint64_t,2> result;
45    md4_ctx context;
46    md4_init(&context);
47    md4_update(&context, xbt_pagesize, (const uint8_t*) data);
48    md4_digest(&context, MD4_DIGEST_SIZE, (uint8_t*) &(result[0]));
49    return result;
50 #else
51   const uint64_t* values = (const uint64_t*) data;
52   size_t n = xbt_pagesize / sizeof(uint64_t);
53
54   // This djb2:
55   uint64_t hash = 5381;
56   for (size_t i=0; i!=n; ++i) {
57     hash = ((hash << 5) + hash) + values[i];
58   }
59   return hash;
60 #endif
61 }
62
63 // ***** snapshot_page_manager
64
65 PageStore::PageStore(size_t size) :
66   memory_(NULL), capacity_(0), top_index_(0)
67 {
68   // Using mmap in order to be able to expand the region
69   // by relocating it somewhere else in the virtual memory
70   // space:
71   void * memory = ::mmap(NULL, size << xbt_pagebits, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
72   if (memory==MAP_FAILED) {
73     xbt_die("Could not mmap initial snapshot pages.");
74   }
75
76   this->top_index_ = 0;
77   this->capacity_ = size;
78   this->memory_ = memory;
79   this->page_counts_.resize(size);
80 }
81
82 PageStore::~PageStore()
83 {
84   ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
85 }
86
87 void PageStore::resize(size_t size)
88 {
89   size_t old_bytesize = this->capacity_ << xbt_pagebits;
90   size_t new_bytesize = size << xbt_pagebits;
91
92   // Expand the memory region by moving it into another
93   // virtual memory address if necessary:
94   void* new_memory = mremap(this->memory_, old_bytesize, new_bytesize, MREMAP_MAYMOVE);
95   if (new_memory == MAP_FAILED) {
96     xbt_die("Could not mremap snapshot pages.");
97   }
98
99   this->capacity_ = size;
100   this->memory_ = new_memory;
101   this->page_counts_.resize(size, 0);
102 }
103
104 /** Allocate a free page
105  *
106  *  @return index of the free page
107  */
108 size_t PageStore::alloc_page()
109 {
110   if (this->free_pages_.empty()) {
111
112     // Expand the region:
113     if (this->top_index_ == this->capacity_) {
114       // All the pages are allocated, we need add more pages:
115       this->resize(2 * this->capacity_);
116     }
117
118     // Use a page from the top:
119     return this->top_index_++;
120
121   } else {
122
123     // Use a page from free_pages_ (inside of the region):
124     size_t res = this->free_pages_[this->free_pages_.size() - 1];
125     this->free_pages_.pop_back();
126     return res;
127
128   }
129 }
130
131 void PageStore::remove_page(size_t pageno)
132 {
133   this->free_pages_.push_back(pageno);
134   const void* page = this->get_page(pageno);
135   hash_type hash = mc_hash_page(page);
136 #ifdef MC_PAGE_STORE_MD4
137   this->hash_index_.erase(hash);
138 #else
139   this->hash_index_[hash].erase(pageno);
140 #endif
141 }
142
143 /** Store a page in memory */
144 size_t PageStore::store_page(void* page)
145 {
146   xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
147
148   // First, we check if a page with the same content is already in the page
149   // store:
150   //  1. compute the hash of the page;
151   //  2. find pages with the same hash using `hash_index_`;
152   //  3. find a page with the same content.
153   hash_type hash = mc_hash_page(page);
154 #ifdef MC_PAGE_STORE_MD4
155   s_mc_pages_store::pages_map_type::const_iterator i =
156     this->hash_index_.find(hash);
157   if (i!=this->hash_index_.cend()) {
158     // If a page with the same content is already in the page store it is
159     // reused and its reference count is incremented.
160     size_t pageno = i->second;
161     page_counts_[pageno]++;
162     return pageno;
163   }
164 #else
165
166   // Try to find a duplicate in set of pages with the same hash:
167   page_set_type& page_set = this->hash_index_[hash];
168   BOOST_FOREACH (size_t pageno, page_set) {
169     const void* snapshot_page = this->get_page(pageno);
170     if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
171
172       // If a page with the same content is already in the page store it is
173       // reused and its reference count is incremented.
174       page_counts_[pageno]++;
175       return pageno;
176
177     }
178   }
179 #endif
180
181   // Otherwise, a new page is allocated in the page store and the content
182   // of the page is `memcpy()`-ed to this new page.
183   size_t pageno = alloc_page();
184   xbt_assert(this->page_counts_[pageno]==0, "Allocated page is already used");
185   void* snapshot_page = (void*) this->get_page(pageno);
186   memcpy(snapshot_page, page, xbt_pagesize);
187 #ifdef MC_PAGE_STORE_MD4
188   this->hash_index_[hash] = pageno;
189 #else
190   page_set.insert(pageno);
191 #endif
192   page_counts_[pageno]++;
193   return pageno;
194 }
195
196 }
197 }
198
199 #ifdef SIMGRID_TEST
200
201 #include <string.h>
202 #include <stdlib.h>
203 #include <stdint.h>
204 #include <unistd.h>
205 #include <sys/mman.h>
206
207 #include <memory>
208
209 #include "mc/PageStore.hpp"
210
211 static int value = 0;
212
213 static void new_content(void* data, size_t size)
214 {
215   memset(data, ++value, size);
216 }
217
218 static void* getpage()
219 {
220   return mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
221 }
222
223 extern "C" {
224
225 XBT_TEST_SUITE("mc_page_store", "Page store");
226
227 XBT_TEST_UNIT("base", test_mc_page_store, "Test adding/removing pages in the store")
228 {
229   xbt_test_add("Init");
230   size_t pagesize = (size_t) getpagesize();
231   std::unique_ptr<simgrid::mc::PageStore> store
232     = std::unique_ptr<simgrid::mc::PageStore>(new simgrid::mc::PageStore(500));
233   void* data = getpage();
234   xbt_test_assert(store->size()==0, "Bad size");
235
236   xbt_test_add("Store the page once");
237   new_content(data, pagesize);
238   size_t pageno1 = store->store_page(data);
239   xbt_test_assert(store->get_ref(pageno1)==1, "Bad refcount");
240   const void* copy = store->get_page(pageno1);
241   xbt_test_assert(memcmp(data, copy, pagesize)==0, "Page data should be the same");
242   xbt_test_assert(store->size()==1, "Bad size");
243
244   xbt_test_add("Store the same page again");
245   size_t pageno2 = store->store_page(data);
246   xbt_test_assert(pageno1==pageno2, "Page should be the same");
247   xbt_test_assert(store->get_ref(pageno1)==2, "Bad refcount");
248   xbt_test_assert(store->size()==1, "Bad size");
249
250   xbt_test_add("Store a new page");
251   new_content(data, pagesize);
252   size_t pageno3 = store->store_page(data);
253   xbt_test_assert(pageno1 != pageno3, "New page should be different");
254   xbt_test_assert(store->size()==2, "Bad size");
255
256   xbt_test_add("Unref pages");
257   store->unref_page(pageno1);
258   xbt_assert(store->get_ref(pageno1)==1, "Bad refcount");
259   xbt_assert(store->size()==2, "Bad size");
260   store->unref_page(pageno2);
261   xbt_test_assert(store->size()==1, "Bad size");
262
263   xbt_test_add("Reallocate page");
264   new_content(data, pagesize);
265   size_t pageno4 = store->store_page(data);
266   xbt_test_assert(pageno1 == pageno4, "Page was not reused");
267   xbt_test_assert(store->get_ref(pageno4)==1, "Bad refcount");
268   xbt_test_assert(store->size()==2, "Bad size");
269 }
270
271 }
272
273 #endif /* SIMGRID_TEST */
274
275 }