Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ok, I stop trying to please sonar.
[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/internal_config.h"
20
21 #include "src/mc/PageStore.hpp"
22
23 #include "src/mc/mc_mmu.h"
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc,
26                                 "Logging specific to mc_page_snapshot");
27
28 namespace simgrid {
29 namespace mc {
30
31 /** @brief Compute a hash for the given memory page
32  *
33  *  The page is used before inserting the page in the page store
34  *  in order to find duplicate of this page in the page store.
35  *
36  *  @param data Memory page
37  *  @return hash off the page
38  */
39 static inline  __attribute__ ((always_inline))
40 PageStore::hash_type mc_hash_page(const void* data)
41 {
42   const std::uint64_t* values = (const uint64_t*) data;
43   std::size_t n = xbt_pagesize / sizeof(uint64_t);
44
45   // This djb2:
46   std::uint64_t hash = 5381;
47   for (std::size_t i = 0; i != n; ++i)
48     hash = ((hash << 5) + hash) + values[i];
49   return hash;
50 }
51
52 // ***** snapshot_page_manager
53
54 PageStore::PageStore(size_t size) :
55   memory_(nullptr), capacity_(0), top_index_(0)
56 {
57   // Using mmap in order to be able to expand the region
58   // by relocating it somewhere else in the virtual memory
59   // space:
60   void* memory = ::mmap(nullptr, size << xbt_pagebits, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
61   if (memory == MAP_FAILED)
62     xbt_die("Could not mmap initial snapshot pages.");
63
64   this->top_index_ = 0;
65   this->capacity_ = size;
66   this->memory_ = memory;
67   this->page_counts_.resize(size);
68 }
69
70 PageStore::~PageStore()
71 {
72   ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
73 }
74
75 void PageStore::resize(std::size_t size)
76 {
77   size_t old_bytesize = this->capacity_ << xbt_pagebits;
78   size_t new_bytesize = size << xbt_pagebits;
79   void *new_memory;
80
81   // Expand the memory region by moving it into another
82   // virtual memory address if necessary:
83 #if HAVE_MREMAP
84   new_memory = mremap(this->memory_, old_bytesize, new_bytesize, MREMAP_MAYMOVE);
85   if (new_memory == MAP_FAILED)
86     xbt_die("Could not mremap snapshot pages.");
87 #else
88   if (new_bytesize > old_bytesize) {
89     // Grow: first try to add new space after current map
90     new_memory = mmap((char *)this->memory_ + old_bytesize,
91                       new_bytesize-old_bytesize,
92                       PROT_READ|PROT_WRITE,
93                       MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE,
94                       -1, 0);
95     if (new_memory == MAP_FAILED)
96       xbt_die("Could not mremap snapshot pages.");
97     // Check if expanding worked
98     if (new_memory != (char *)this->memory_ + old_bytesize) {
99       // New memory segment could not be put at the end of this->memory_,
100       // so cancel this one and try to rellocate everything and copy data
101       munmap(new_memory, new_bytesize-old_bytesize);
102       new_memory = mmap(nullptr,
103                         new_bytesize,
104                         PROT_READ|PROT_WRITE,
105                         MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE,
106                         -1, 0);
107       if (new_memory == MAP_FAILED)
108         xbt_die("Could not mremap snapshot pages.");
109       memcpy(new_memory, this->memory_, old_bytesize);
110       munmap(this->memory_, old_bytesize);
111     }
112   }
113   else {
114     // We don't have functions to shrink a mapping, so leave memory as
115     // it is for now
116     new_memory = this->memory_;
117   }
118 #endif
119
120   this->capacity_ = size;
121   this->memory_ = new_memory;
122   this->page_counts_.resize(size, 0);
123 }
124
125 /** Allocate a free page
126  *
127  *  @return index of the free page
128  */
129 std::size_t PageStore::alloc_page()
130 {
131   if (this->free_pages_.empty()) {
132
133     // Expand the region:
134     if (this->top_index_ == this->capacity_)
135       // All the pages are allocated, we need add more pages:
136       this->resize(2 * this->capacity_);
137
138     // Use a page from the top:
139     return this->top_index_++;
140
141   } else {
142
143     // Use a page from free_pages_ (inside of the region):
144     size_t res = this->free_pages_[this->free_pages_.size() - 1];
145     this->free_pages_.pop_back();
146     return res;
147
148   }
149 }
150
151 void PageStore::remove_page(std::size_t pageno)
152 {
153   this->free_pages_.push_back(pageno);
154   const void* page = this->get_page(pageno);
155   hash_type hash = mc_hash_page(page);
156   this->hash_index_[hash].erase(pageno);
157 }
158
159 /** Store a page in memory */
160 std::size_t PageStore::store_page(void* page)
161 {
162   xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
163
164   // First, we check if a page with the same content is already in the page
165   // store:
166   //  1. compute the hash of the page;
167   //  2. find pages with the same hash using `hash_index_`;
168   //  3. find a page with the same content.
169   hash_type hash = mc_hash_page(page);
170
171   // Try to find a duplicate in set of pages with the same hash:
172   page_set_type& page_set = this->hash_index_[hash];
173   for (size_t pageno : page_set) {
174     const void* snapshot_page = this->get_page(pageno);
175     if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
176
177       // If a page with the same content is already in the page store it is
178       // reused and its reference count is incremented.
179       page_counts_[pageno]++;
180       return pageno;
181
182     }
183   }
184
185   // Otherwise, a new page is allocated in the page store and the content
186   // of the page is `memcpy()`-ed to this new page.
187   std::size_t pageno = alloc_page();
188   xbt_assert(this->page_counts_[pageno]==0, "Allocated page is already used");
189   void* snapshot_page = (void*) this->get_page(pageno);
190   memcpy(snapshot_page, page, xbt_pagesize);
191   page_set.insert(pageno);
192   page_counts_[pageno]++;
193   return pageno;
194 }
195
196 }
197 }
198
199 #ifdef SIMGRID_TEST
200
201 #include <cstring>
202 #include <cstdint>
203
204 #include <unistd.h>
205 #include <sys/mman.h>
206
207 #include <memory>
208
209 #include "src/mc/PageStore.hpp"
210
211 static int value = 0;
212
213 static void new_content(void* data, std::size_t size)
214 {
215   ::memset(data, ++value, size);
216 }
217
218 static void* getpage()
219 {
220   return mmap(nullptr, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
221 }
222
223 XBT_TEST_SUITE("mc_page_store", "Page store");
224
225 XBT_TEST_UNIT("base", test_mc_page_store, "Test adding/removing pages in the store")
226 {
227   using simgrid::mc::PageStore;
228   
229   xbt_test_add("Init");
230   std::size_t pagesize = (size_t) getpagesize();
231   std::unique_ptr<PageStore> store
232     = std::unique_ptr<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 #endif /* SIMGRID_TEST */