Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Slightly reorganize log categories; remove unused ones.
[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
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 #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) : memory_(nullptr), capacity_(size), top_index_(0)
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
122     // Expand the region:
123     if (this->top_index_ == this->capacity_)
124       // All the pages are allocated, we need add more pages:
125       this->resize(2 * this->capacity_);
126
127     // Use a page from the top:
128     return this->top_index_++;
129
130   } else {
131
132     // Use a page from free_pages_ (inside of the region):
133     size_t res = this->free_pages_[this->free_pages_.size() - 1];
134     this->free_pages_.pop_back();
135     return res;
136   }
137 }
138
139 void PageStore::remove_page(std::size_t pageno)
140 {
141   this->free_pages_.push_back(pageno);
142   const void* page = this->get_page(pageno);
143   hash_type hash   = mc_hash_page(page);
144   this->hash_index_[hash].erase(pageno);
145 }
146
147 /** Store a page in memory */
148 std::size_t PageStore::store_page(void* page)
149 {
150   xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
151
152   // First, we check if a page with the same content is already in the page store:
153   //  1. compute the hash of the page
154   //  2. find pages with the same hash using `hash_index_`
155   //  3. find a page with the same content
156   hash_type hash = mc_hash_page(page);
157
158   // Try to find a duplicate in set of pages with the same hash:
159   page_set_type& page_set = this->hash_index_[hash];
160   for (size_t const& pageno : page_set) {
161     const void* snapshot_page = this->get_page(pageno);
162     if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
163
164       // If a page with the same content is already in the page store it's reused and its refcount is incremented.
165       page_counts_[pageno]++;
166       return pageno;
167     }
168   }
169
170   // Otherwise, a new page is allocated in the page store and the content of the page is `memcpy()`-ed to this new page.
171   std::size_t pageno = alloc_page();
172   xbt_assert(this->page_counts_[pageno] == 0, "Allocated page is already used");
173   void* snapshot_page = (void*)this->get_page(pageno);
174   memcpy(snapshot_page, page, xbt_pagesize);
175   page_set.insert(pageno);
176   page_counts_[pageno]++;
177   return pageno;
178 }
179
180 } // namespace mc
181 } // namespace simgrid