Logo AND Algorithmique Numérique Distribuée

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