Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Constify pointer and reference parameters in src/mc/.
[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 namespace simgrid {
25 namespace mc {
26
27 /** @brief Compute a hash for the given memory page
28  *
29  *  The page is used before inserting the page in the page store in order to find duplicate of this page in the page
30  *  store.
31  *
32  *  @param data Memory page
33  *  @return hash off the page
34  */
35 static XBT_ALWAYS_INLINE PageStore::hash_type mc_hash_page(const void* data)
36 {
37 #ifdef SG_HAVE_CPP14
38   return xxh::xxhash<64>(data, xbt_pagesize);
39 #else
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 #endif
49 }
50
51 // ***** snapshot_page_manager
52
53 PageStore::PageStore(std::size_t size) : capacity_(size)
54 {
55   // Using mmap in order to be able to expand the region by relocating it somewhere else in the virtual memory space:
56   void* memory =
57       ::mmap(nullptr, size << xbt_pagebits, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
58   if (memory == MAP_FAILED)
59     xbt_die("Could not mmap initial snapshot pages.");
60
61   this->top_index_ = 0;
62   this->memory_    = memory;
63   this->page_counts_.resize(size);
64 }
65
66 PageStore::~PageStore()
67 {
68   ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
69 }
70
71 void PageStore::resize(std::size_t size)
72 {
73   size_t old_bytesize = this->capacity_ << xbt_pagebits;
74   size_t new_bytesize = size << xbt_pagebits;
75   void* new_memory;
76
77   // Expand the memory region by moving it into another
78   // virtual memory address if necessary:
79 #if HAVE_MREMAP
80   new_memory = mremap(this->memory_, old_bytesize, new_bytesize, MREMAP_MAYMOVE);
81   if (new_memory == MAP_FAILED)
82     xbt_die("Could not mremap snapshot pages.");
83 #else
84   if (new_bytesize > old_bytesize) {
85     // Grow: first try to add new space after current map
86     new_memory = mmap((char*)this->memory_ + old_bytesize, new_bytesize - old_bytesize, PROT_READ | PROT_WRITE,
87                       MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
88     if (new_memory == MAP_FAILED)
89       xbt_die("Could not mremap snapshot pages.");
90     // Check if expanding worked
91     if (new_memory != (char*)this->memory_ + old_bytesize) {
92       // New memory segment could not be put at the end of this->memory_,
93       // so cancel this one and try to relocate everything and copy data
94       munmap(new_memory, new_bytesize - old_bytesize);
95       new_memory =
96           mmap(nullptr, new_bytesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
97       if (new_memory == MAP_FAILED)
98         xbt_die("Could not mremap snapshot pages.");
99       memcpy(new_memory, this->memory_, old_bytesize);
100       munmap(this->memory_, old_bytesize);
101     }
102   } else {
103     // We don't have functions to shrink a mapping, so leave memory as
104     // it is for now
105     new_memory = this->memory_;
106   }
107 #endif
108
109   this->capacity_ = size;
110   this->memory_   = new_memory;
111   this->page_counts_.resize(size, 0);
112 }
113
114 /** Allocate a free page
115  *
116  *  @return index of the free page
117  */
118 std::size_t PageStore::alloc_page()
119 {
120   if (this->free_pages_.empty()) {
121     // Expand the region:
122     if (this->top_index_ == this->capacity_)
123       // All the pages are allocated, we need add more pages:
124       this->resize(2 * this->capacity_);
125
126     // Use a page from the top:
127     return this->top_index_++;
128   } else {
129     // Use a page from free_pages_ (inside of the region):
130     size_t res = this->free_pages_[this->free_pages_.size() - 1];
131     this->free_pages_.pop_back();
132     return res;
133   }
134 }
135
136 void PageStore::remove_page(std::size_t pageno)
137 {
138   this->free_pages_.push_back(pageno);
139   const void* page = this->get_page(pageno);
140   hash_type hash   = mc_hash_page(page);
141   this->hash_index_[hash].erase(pageno);
142 }
143
144 /** Store a page in memory */
145 std::size_t PageStore::store_page(const void* page)
146 {
147   xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
148
149   // First, we check if a page with the same content is already in the page 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
155   // Try to find a duplicate in set of pages with the same hash:
156   page_set_type& page_set = this->hash_index_[hash];
157   for (size_t const& pageno : page_set) {
158     const void* snapshot_page = this->get_page(pageno);
159     if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
160       // If a page with the same content is already in the page store it's reused and its refcount is incremented.
161       page_counts_[pageno]++;
162       return pageno;
163     }
164   }
165
166   // Otherwise, a new page is allocated in the page store and the content of the page is `memcpy()`-ed to this new page.
167   std::size_t pageno = alloc_page();
168   xbt_assert(this->page_counts_[pageno] == 0, "Allocated page is already used");
169   void* snapshot_page = this->get_page(pageno);
170   memcpy(snapshot_page, page, xbt_pagesize);
171   page_set.insert(pageno);
172   page_counts_[pageno]++;
173   return pageno;
174 }
175
176 } // namespace mc
177 } // namespace simgrid