1 /* Copyright (c) 2015-2019. 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
12 #include "xbt/sysdep.h"
14 #include "src/mc/sosp/PageStore.hpp"
15 #include "src/mc/mc_mmu.hpp"
17 #include <cstring> // memcpy, memcmp
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc, "Logging specific to mc_page_snapshot");
25 /** @brief Compute a hash for the given memory page
27 * The page is used before inserting the page in the page store
28 * in order to find duplicate of this page in the page store.
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 const std::uint64_t* values = (const uint64_t*)data;
36 std::size_t n = xbt_pagesize / sizeof(uint64_t);
39 std::uint64_t hash = 5381;
40 for (std::size_t i = 0; i != n; ++i)
41 hash = ((hash << 5) + hash) + values[i];
45 // ***** snapshot_page_manager
47 PageStore::PageStore(std::size_t size) : memory_(nullptr), capacity_(size), top_index_(0)
49 // Using mmap in order to be able to expand the region by relocating it somewhere else in the virtual memory space:
51 ::mmap(nullptr, size << xbt_pagebits, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
52 if (memory == MAP_FAILED)
53 xbt_die("Could not mmap initial snapshot pages.");
56 this->memory_ = memory;
57 this->page_counts_.resize(size);
60 PageStore::~PageStore()
62 ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
65 void PageStore::resize(std::size_t size)
67 size_t old_bytesize = this->capacity_ << xbt_pagebits;
68 size_t new_bytesize = size << xbt_pagebits;
71 // Expand the memory region by moving it into another
72 // virtual memory address if necessary:
74 new_memory = mremap(this->memory_, old_bytesize, new_bytesize, MREMAP_MAYMOVE);
75 if (new_memory == MAP_FAILED)
76 xbt_die("Could not mremap snapshot pages.");
78 if (new_bytesize > old_bytesize) {
79 // Grow: first try to add new space after current map
80 new_memory = mmap((char*)this->memory_ + old_bytesize, new_bytesize - old_bytesize, PROT_READ | PROT_WRITE,
81 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
82 if (new_memory == MAP_FAILED)
83 xbt_die("Could not mremap snapshot pages.");
84 // Check if expanding worked
85 if (new_memory != (char*)this->memory_ + old_bytesize) {
86 // New memory segment could not be put at the end of this->memory_,
87 // so cancel this one and try to rellocate everything and copy data
88 munmap(new_memory, new_bytesize - old_bytesize);
90 mmap(nullptr, new_bytesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
91 if (new_memory == MAP_FAILED)
92 xbt_die("Could not mremap snapshot pages.");
93 memcpy(new_memory, this->memory_, old_bytesize);
94 munmap(this->memory_, old_bytesize);
97 // We don't have functions to shrink a mapping, so leave memory as
99 new_memory = this->memory_;
103 this->capacity_ = size;
104 this->memory_ = new_memory;
105 this->page_counts_.resize(size, 0);
108 /** Allocate a free page
110 * @return index of the free page
112 std::size_t PageStore::alloc_page()
114 if (this->free_pages_.empty()) {
116 // Expand the region:
117 if (this->top_index_ == this->capacity_)
118 // All the pages are allocated, we need add more pages:
119 this->resize(2 * this->capacity_);
121 // Use a page from the top:
122 return this->top_index_++;
126 // Use a page from free_pages_ (inside of the region):
127 size_t res = this->free_pages_[this->free_pages_.size() - 1];
128 this->free_pages_.pop_back();
133 void PageStore::remove_page(std::size_t pageno)
135 this->free_pages_.push_back(pageno);
136 const void* page = this->get_page(pageno);
137 hash_type hash = mc_hash_page(page);
138 this->hash_index_[hash].erase(pageno);
141 /** Store a page in memory */
142 std::size_t PageStore::store_page(void* page)
144 xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
146 // First, we check if a page with the same content is already in the page store:
147 // 1. compute the hash of the page
148 // 2. find pages with the same hash using `hash_index_`
149 // 3. find a page with the same content
150 hash_type hash = mc_hash_page(page);
152 // Try to find a duplicate in set of pages with the same hash:
153 page_set_type& page_set = this->hash_index_[hash];
154 for (size_t const& pageno : page_set) {
155 const void* snapshot_page = this->get_page(pageno);
156 if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
158 // If a page with the same content is already in the page store it's reused and its refcount is incremented.
159 page_counts_[pageno]++;
164 // Otherwise, a new page is allocated in the page store and the content of the page is `memcpy()`-ed to this new page.
165 std::size_t pageno = alloc_page();
166 xbt_assert(this->page_counts_[pageno] == 0, "Allocated page is already used");
167 void* snapshot_page = (void*)this->get_page(pageno);
168 memcpy(snapshot_page, page, xbt_pagesize);
169 page_set.insert(pageno);
170 page_counts_[pageno]++;
175 } // namespace simgrid