1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef SIMGRID_MC_PAGESTORE_HPP
8 #define SIMGRID_MC_PAGESTORE_HPP
14 #include <boost/array.hpp>
15 #include <boost/utility.hpp>
16 #include <boost/unordered_map.hpp>
17 #include <boost/unordered_set.hpp>
22 #include "mc_forward.h"
27 /** @brief Storage for snapshot memory pages
29 * The first (lower) layer of the per-page snapshot mechanism is a page
30 * store: it's responsibility is to store immutable shareable
31 * reference-counted memory pages independently of the snapshoting
32 * logic. Snapshot management and representation, soft-dirty tracking is
33 * handled to an higher layer. READMORE
37 * * A pointer (`memory_`) to a (currently anonymous) `mmap()`ed memory
38 * region holding the memory pages (the address of the first page).
40 * We want to keep this memory region aligned on the memory pages (so
41 * that we might be able to create non-linear memory mappings on those
42 * pages in the future) and be able to expand it without coyping the
43 * data (there will be a lot of pages here): we will be able to
44 * efficiently expand the memory mapping using `mremap()`, moving it
45 * to another virtual address if necessary.
47 * Because we will move this memory mapping on the virtual address
48 * space, only the index of the page will be stored in the snapshots
49 * and the page will always be looked up by going through `memory`:
51 * void* page = (char*) page_store->memory + page_index << pagebits;
53 * * The number of pages mapped in virtual memory (`capacity_`). Once all
54 * those pages are used, we need to expand the page store with
57 * * A reference count for each memory page `page_counts_`. Each time a
58 * snapshot references a page, the counter is incremented. If a
59 * snapshot is freed, the reference count is decremented. When the
60 * reference count, of a page reaches 0 it is added to a list of available
61 * pages (`free_pages_`).
63 * * A list of free pages `free_pages_` which can be reused. This avoids having
64 * to scan the reference count list to find a free page.
66 * * When we are expanding the memory map we do not want to add thousand of page
67 * to the `free_pages_` list and remove them just afterwards. The `top_index_`
68 * field is an index after which all pages are free and are not in the `free_pages_`
71 * * When we are adding a page, we need to check if a page with the same
72 * content is already in the page store in order to reuse it. For this
73 * reason, we maintain an index (`hash_index_`) mapping the hash of a
74 * page to the list of page indices with this hash.
75 * We use a fast (non cryptographic) hash so there may be conflicts:
76 * we must be able to store multiple indices for the same hash.
81 #ifdef MC_PAGE_STORE_MD4
82 typedef boost::array<uint64_t,2> hash_type;
84 typedef uint64_t hash_type;
87 #ifdef MC_PAGE_STORE_MD4
88 // We are using a secure hash to identify a page.
89 // We assume there will not be any collision: we need to map a hash
90 // to a single page index.
91 typedef boost::unordered_map<hash_type, size_t> pages_map_type;
93 // We are using a cheap hash to index a page.
94 // We should expect collision and we need to associate multiple page indices
96 typedef boost::unordered_set<size_t> page_set_type;
97 typedef boost::unordered_map<hash_type, page_set_type> pages_map_type;
103 * mc_page_store_get_page expects that this is the first field.
106 /** Number of available pages in virtual memory */
108 /** Top of the used pages (index of the next available page) */
110 /** Page reference count */
111 std::vector<uint64_t> page_counts_;
112 /** Index of available pages before the top */
113 std::vector<size_t> free_pages_;
114 /** Index from page hash to page index */
115 pages_map_type hash_index_;
118 void resize(size_t size);
120 void remove_page(size_t pageno);
122 public: // Constructors
123 PageStore(PageStore const&) = delete;
124 PageStore& operator=(PageStore const&) = delete;
125 explicit PageStore(size_t size);
130 /** @brief Decrement the reference count for a given page
132 * Decrement the reference count of this page. Used when a snapshot is
135 * If the reference count reaches zero, the page is recycled:
136 * it is added to the `free_pages_` list and removed from the `hash_index_`.
139 void unref_page(size_t pageno);
141 /** @brief Increment the refcount for a given page
143 * This method used to increase a reference count of a page if we know
144 * that the content of a page is the same as a page already in the page
147 * This will be the case if a page if soft clean: we know that is has not
148 * changed since the previous cnapshot/restoration and we can avoid
149 * hashing the page, comparing byte-per-byte to candidates.
151 void ref_page(size_t pageno);
153 /** @brief Store a page in the page store */
154 size_t store_page(void* page);
156 /** @brief Get a page from its page number
158 * @param Number of the memory page in the store
159 * @return Start of the page
161 const void* get_page(size_t pageno) const;
163 public: // Debug/test methods
165 /** @brief Get the number of references for a page */
166 size_t get_ref(size_t pageno);
168 /** @brief Get the number of used pages */
171 /** @brief Get the capacity of the page store
173 * The capacity is expanded by a system call (mremap).
179 inline __attribute__((always_inline))
180 void PageStore::unref_page(size_t pageno) {
181 if ((--this->page_counts_[pageno]) == 0) {
182 this->remove_page(pageno);
186 inline __attribute__((always_inline))
187 void PageStore::ref_page(size_t pageno) {
188 ++this->page_counts_[pageno];
191 inline __attribute__((always_inline))
192 const void* PageStore::get_page(size_t pageno) const {
193 return mc_page_from_number(this->memory_, pageno);
196 inline __attribute__((always_inline))
197 size_t PageStore::get_ref(size_t pageno) {
198 return this->page_counts_[pageno];
201 inline __attribute__((always_inline))
202 size_t PageStore::size() {
203 return this->top_index_ - this->free_pages_.size();
206 inline __attribute__((always_inline))
207 size_t PageStore::capacity() {
208 return this->capacity_;