1 /* Copyright (c) 2015-2021. The SimGrid Team. All rights reserved. */
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. */
8 #define MAP_POPULATE MAP_PREFAULT_READ
11 #include "src/internal_config.h"
13 #include "xbt/sysdep.h"
15 #include "src/include/xxhash.hpp"
16 #include "src/mc/mc_mmu.hpp"
17 #include "src/mc/sosp/PageStore.hpp"
19 #include <cstring> // memcpy, memcmp
25 /** @brief Compute a hash for the given memory page
27 * The page is used before inserting the page in the page store in order to find duplicate of this page in the page
30 * @param data Memory page
31 * @return hash off the page
33 static XBT_ALWAYS_INLINE PageStore::hash_type mc_hash_page(const void* data)
35 return xxh::xxhash<64>(data, xbt_pagesize);
38 // ***** snapshot_page_manager
40 PageStore::PageStore(std::size_t size) : capacity_(size)
42 // Using mmap in order to be able to expand the region by relocating it somewhere else in the virtual memory space:
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.");
49 this->memory_ = memory;
50 this->page_counts_.resize(size);
53 PageStore::~PageStore()
55 ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
58 void PageStore::resize(std::size_t size)
60 size_t old_bytesize = this->capacity_ << xbt_pagebits;
61 size_t new_bytesize = size << xbt_pagebits;
64 // Expand the memory region by moving it into another
65 // virtual memory address if necessary:
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.");
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);
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);
90 // We don't have functions to shrink a mapping, so leave memory as
92 new_memory = this->memory_;
96 this->capacity_ = size;
97 this->memory_ = new_memory;
98 this->page_counts_.resize(size, 0);
101 /** Allocate a free page
103 * @return index of the free page
105 std::size_t PageStore::alloc_page()
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_);
113 // Use a page from the top:
114 return this->top_index_++;
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();
123 void PageStore::remove_page(std::size_t pageno)
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);
131 /** Store a page in memory */
132 std::size_t PageStore::store_page(const void* page)
134 xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
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);
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]++;
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]++;
164 } // namespace simgrid