Logo AND Algorithmique Numérique Distribuée

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