1 /* Copyright (c) 2015-2020. 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"
16 #include "src/include/xxhash.hpp"
18 #include "src/mc/mc_mmu.hpp"
19 #include "src/mc/sosp/PageStore.hpp"
21 #include <cstring> // memcpy, memcmp
27 /** @brief Compute a hash for the given memory page
29 * The page is used before inserting the page in the page store in order to find duplicate of this page in the page
32 * @param data Memory page
33 * @return hash off the page
35 static XBT_ALWAYS_INLINE PageStore::hash_type mc_hash_page(const void* data)
38 return xxh::xxhash<64>(data, xbt_pagesize);
40 const std::uint64_t* values = (const uint64_t*)data;
41 std::size_t n = xbt_pagesize / sizeof(uint64_t);
44 std::uint64_t hash = 5381;
45 for (std::size_t i = 0; i != n; ++i)
46 hash = ((hash << 5) + hash) + values[i];
51 // ***** snapshot_page_manager
53 PageStore::PageStore(std::size_t size) : capacity_(size)
55 // Using mmap in order to be able to expand the region by relocating it somewhere else in the virtual memory space:
57 ::mmap(nullptr, size << xbt_pagebits, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
58 if (memory == MAP_FAILED)
59 xbt_die("Could not mmap initial snapshot pages.");
62 this->memory_ = memory;
63 this->page_counts_.resize(size);
66 PageStore::~PageStore()
68 ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
71 void PageStore::resize(std::size_t size)
73 size_t old_bytesize = this->capacity_ << xbt_pagebits;
74 size_t new_bytesize = size << xbt_pagebits;
77 // Expand the memory region by moving it into another
78 // virtual memory address if necessary:
80 new_memory = mremap(this->memory_, old_bytesize, new_bytesize, MREMAP_MAYMOVE);
81 if (new_memory == MAP_FAILED)
82 xbt_die("Could not mremap snapshot pages.");
84 if (new_bytesize > old_bytesize) {
85 // Grow: first try to add new space after current map
86 new_memory = mmap((char*)this->memory_ + old_bytesize, new_bytesize - old_bytesize, PROT_READ | PROT_WRITE,
87 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
88 if (new_memory == MAP_FAILED)
89 xbt_die("Could not mremap snapshot pages.");
90 // Check if expanding worked
91 if (new_memory != (char*)this->memory_ + old_bytesize) {
92 // New memory segment could not be put at the end of this->memory_,
93 // so cancel this one and try to relocate everything and copy data
94 munmap(new_memory, new_bytesize - old_bytesize);
96 mmap(nullptr, new_bytesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
97 if (new_memory == MAP_FAILED)
98 xbt_die("Could not mremap snapshot pages.");
99 memcpy(new_memory, this->memory_, old_bytesize);
100 munmap(this->memory_, old_bytesize);
103 // We don't have functions to shrink a mapping, so leave memory as
105 new_memory = this->memory_;
109 this->capacity_ = size;
110 this->memory_ = new_memory;
111 this->page_counts_.resize(size, 0);
114 /** Allocate a free page
116 * @return index of the free page
118 std::size_t PageStore::alloc_page()
120 if (this->free_pages_.empty()) {
121 // Expand the region:
122 if (this->top_index_ == this->capacity_)
123 // All the pages are allocated, we need add more pages:
124 this->resize(2 * this->capacity_);
126 // Use a page from the top:
127 return this->top_index_++;
129 // Use a page from free_pages_ (inside of the region):
130 size_t res = this->free_pages_[this->free_pages_.size() - 1];
131 this->free_pages_.pop_back();
136 void PageStore::remove_page(std::size_t pageno)
138 this->free_pages_.push_back(pageno);
139 const void* page = this->get_page(pageno);
140 hash_type hash = mc_hash_page(page);
141 this->hash_index_[hash].erase(pageno);
144 /** Store a page in memory */
145 std::size_t PageStore::store_page(const void* page)
147 xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
149 // First, we check if a page with the same content is already in the page store:
150 // 1. compute the hash of the page
151 // 2. find pages with the same hash using `hash_index_`
152 // 3. find a page with the same content
153 hash_type hash = mc_hash_page(page);
155 // Try to find a duplicate in set of pages with the same hash:
156 page_set_type& page_set = this->hash_index_[hash];
157 for (size_t const& pageno : page_set) {
158 const void* snapshot_page = this->get_page(pageno);
159 if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
160 // If a page with the same content is already in the page store it's reused and its refcount is incremented.
161 page_counts_[pageno]++;
166 // Otherwise, a new page is allocated in the page store and the content of the page is `memcpy()`-ed to this new page.
167 std::size_t pageno = alloc_page();
168 xbt_assert(this->page_counts_[pageno] == 0, "Allocated page is already used");
169 void* snapshot_page = this->get_page(pageno);
170 memcpy(snapshot_page, page, xbt_pagesize);
171 page_set.insert(pageno);
172 page_counts_[pageno]++;
177 } // namespace simgrid