Logo AND Algorithmique Numérique Distribuée

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