Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into xbt_random
[simgrid.git] / src / mc / sosp / PageStore.cpp
1 /* Copyright (c) 2015-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <sys/mman.h>
7 #ifdef __FreeBSD__
8 #define MAP_POPULATE MAP_PREFAULT_READ
9 #endif
10
11 #include "src/internal_config.h"
12 #include "xbt/log.h"
13 #include "xbt/sysdep.h"
14
15 #ifdef SG_HAVE_CPP14
16 #include "src/include/xxhash.hpp"
17 #endif
18 #include "src/mc/mc_mmu.hpp"
19 #include "src/mc/sosp/PageStore.hpp"
20
21 #include <cstring> // memcpy, memcmp
22 #include <unistd.h>
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc, "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 XBT_ALWAYS_INLINE PageStore::hash_type mc_hash_page(const void* data)
38 {
39 #ifdef SG_HAVE_CPP14
40   return xxh::xxhash<64>(data, xbt_pagesize);
41 #else
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 #endif
51 }
52
53 // ***** snapshot_page_manager
54
55 PageStore::PageStore(std::size_t size) : memory_(nullptr), capacity_(size), top_index_(0)
56 {
57   // Using mmap in order to be able to expand the region by relocating it somewhere else in the virtual memory space:
58   void* memory =
59       ::mmap(nullptr, size << xbt_pagebits, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
60   if (memory == MAP_FAILED)
61     xbt_die("Could not mmap initial snapshot pages.");
62
63   this->top_index_ = 0;
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, new_bytesize - old_bytesize, PROT_READ | PROT_WRITE,
89                       MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
90     if (new_memory == MAP_FAILED)
91       xbt_die("Could not mremap snapshot pages.");
92     // Check if expanding worked
93     if (new_memory != (char*)this->memory_ + old_bytesize) {
94       // New memory segment could not be put at the end of this->memory_,
95       // so cancel this one and try to relocate everything and copy data
96       munmap(new_memory, new_bytesize - old_bytesize);
97       new_memory =
98           mmap(nullptr, new_bytesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
99       if (new_memory == MAP_FAILED)
100         xbt_die("Could not mremap snapshot pages.");
101       memcpy(new_memory, this->memory_, old_bytesize);
102       munmap(this->memory_, old_bytesize);
103     }
104   } else {
105     // We don't have functions to shrink a mapping, so leave memory as
106     // it is for now
107     new_memory = this->memory_;
108   }
109 #endif
110
111   this->capacity_ = size;
112   this->memory_   = new_memory;
113   this->page_counts_.resize(size, 0);
114 }
115
116 /** Allocate a free page
117  *
118  *  @return index of the free page
119  */
120 std::size_t PageStore::alloc_page()
121 {
122   if (this->free_pages_.empty()) {
123
124     // Expand the region:
125     if (this->top_index_ == this->capacity_)
126       // All the pages are allocated, we need add more pages:
127       this->resize(2 * this->capacity_);
128
129     // Use a page from the top:
130     return this->top_index_++;
131
132   } else {
133
134     // Use a page from free_pages_ (inside of the region):
135     size_t res = this->free_pages_[this->free_pages_.size() - 1];
136     this->free_pages_.pop_back();
137     return res;
138   }
139 }
140
141 void PageStore::remove_page(std::size_t pageno)
142 {
143   this->free_pages_.push_back(pageno);
144   const void* page = this->get_page(pageno);
145   hash_type hash   = mc_hash_page(page);
146   this->hash_index_[hash].erase(pageno);
147 }
148
149 /** Store a page in memory */
150 std::size_t PageStore::store_page(void* page)
151 {
152   xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
153
154   // First, we check if a page with the same content is already in the page store:
155   //  1. compute the hash of the page
156   //  2. find pages with the same hash using `hash_index_`
157   //  3. find a page with the same content
158   hash_type hash = mc_hash_page(page);
159
160   // Try to find a duplicate in set of pages with the same hash:
161   page_set_type& page_set = this->hash_index_[hash];
162   for (size_t const& pageno : page_set) {
163     const void* snapshot_page = this->get_page(pageno);
164     if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
165
166       // If a page with the same content is already in the page store it's reused and its refcount is incremented.
167       page_counts_[pageno]++;
168       return pageno;
169     }
170   }
171
172   // Otherwise, a new page is allocated in the page store and the content of the page is `memcpy()`-ed to this new page.
173   std::size_t pageno = alloc_page();
174   xbt_assert(this->page_counts_[pageno] == 0, "Allocated page is already used");
175   void* snapshot_page = (void*)this->get_page(pageno);
176   memcpy(snapshot_page, page, xbt_pagesize);
177   page_set.insert(pageno);
178   page_counts_[pageno]++;
179   return pageno;
180 }
181
182 } // namespace mc
183 } // namespace simgrid