Logo AND Algorithmique Numérique Distribuée

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