Logo AND Algorithmique Numérique Distribuée

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