Logo AND Algorithmique Numérique Distribuée

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