Logo AND Algorithmique Numérique Distribuée

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