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
13 #include <boost/unordered_map.hpp>
14 #include <boost/unordered_set.hpp>
17 #include "mc_forward.hpp"
22 /** @brief Storage for snapshot memory pages
24 * The first (lower) layer of the per-page snapshot mechanism is a page
25 * store: it's responsibility is to store immutable shareable
26 * reference-counted memory pages independently of the snapshoting
27 * logic. Snapshot management and representation, soft-dirty tracking is
28 * handled to an higher layer. READMORE
32 * * A pointer (`memory_`) to a (currently anonymous) `mmap()`ed memory
33 * region holding the memory pages (the address of the first page).
35 * We want to keep this memory region aligned on the memory pages (so
36 * that we might be able to create non-linear memory mappings on those
37 * pages in the future) and be able to expand it without coyping the
38 * data (there will be a lot of pages here): we will be able to
39 * efficiently expand the memory mapping using `mremap()`, moving it
40 * to another virtual address if necessary.
42 * Because we will move this memory mapping on the virtual address
43 * space, only the index of the page will be stored in the snapshots
44 * and the page will always be looked up by going through `memory`:
46 * void* page = (char*) page_store->memory + page_index << pagebits;
48 * * The number of pages mapped in virtual memory (`capacity_`). Once all
49 * those pages are used, we need to expand the page store with
52 * * A reference count for each memory page `page_counts_`. Each time a
53 * snapshot references a page, the counter is incremented. If a
54 * snapshot is freed, the reference count is decremented. When the
55 * reference count, of a page reaches 0 it is added to a list of available
56 * pages (`free_pages_`).
58 * * A list of free pages `free_pages_` which can be reused. This avoids having
59 * to scan the reference count list to find a free page.
61 * * When we are expanding the memory map we do not want to add thousand of page
62 * to the `free_pages_` list and remove them just afterwards. The `top_index_`
63 * field is an index after which all pages are free and are not in the `free_pages_`
66 * * When we are adding a page, we need to check if a page with the same
67 * content is already in the page store in order to reuse it. For this
68 * reason, we maintain an index (`hash_index_`) mapping the hash of a
69 * page to the list of page indices with this hash.
70 * We use a fast (non cryptographic) hash so there may be conflicts:
71 * we must be able to store multiple indices for the same hash.
76 typedef std::uint64_t hash_type;
78 // We are using a cheap hash to index a page.
79 // We should expect collision and we need to associate multiple page indices
81 typedef boost::unordered_set<std::size_t> page_set_type;
82 typedef boost::unordered_map<hash_type, page_set_type> pages_map_type;
87 /** Number of available pages in virtual memory */
88 std::size_t capacity_;
89 /** Top of the used pages (index of the next available page) */
90 std::size_t top_index_;
91 /** Page reference count */
92 std::vector<std::uint64_t> page_counts_;
93 /** Index of available pages before the top */
94 std::vector<std::size_t> free_pages_;
95 /** Index from page hash to page index */
96 pages_map_type hash_index_;
99 void resize(std::size_t size);
100 std::size_t alloc_page();
101 void remove_page(std::size_t pageno);
103 public: // Constructors
104 PageStore(PageStore const&) = delete;
105 PageStore& operator=(PageStore const&) = delete;
106 explicit PageStore(std::size_t size);
111 /** @brief Decrement the reference count for a given page
113 * Decrement the reference count of this page. Used when a snapshot is
116 * If the reference count reaches zero, the page is recycled:
117 * it is added to the `free_pages_` list and removed from the `hash_index_`.
120 void unref_page(std::size_t pageno);
122 /** @brief Increment the refcount for a given page
124 * This method used to increase a reference count of a page if we know
125 * that the content of a page is the same as a page already in the page
128 * This will be the case if a page if soft clean: we know that is has not
129 * changed since the previous cnapshot/restoration and we can avoid
130 * hashing the page, comparing byte-per-byte to candidates.
132 void ref_page(size_t pageno);
134 /** @brief Store a page in the page store */
135 size_t store_page(void* page);
137 /** @brief Get a page from its page number
139 * @param Number of the memory page in the store
140 * @return Start of the page
142 const void* get_page(std::size_t pageno) const;
144 public: // Debug/test methods
146 /** @brief Get the number of references for a page */
147 std::size_t get_ref(std::size_t pageno);
149 /** @brief Get the number of used pages */
152 /** @brief Get the capacity of the page store
154 * The capacity is expanded by a system call (mremap).
156 std::size_t capacity();
160 inline __attribute__((always_inline))
161 void PageStore::unref_page(std::size_t pageno) {
162 if ((--this->page_counts_[pageno]) == 0)
163 this->remove_page(pageno);
166 inline __attribute__((always_inline))
167 void PageStore::ref_page(size_t pageno)
169 ++this->page_counts_[pageno];
172 inline __attribute__((always_inline))
173 const void* PageStore::get_page(std::size_t pageno) const
175 return mc_page_from_number(this->memory_, pageno);
178 inline __attribute__((always_inline))
179 std::size_t PageStore::get_ref(std::size_t pageno)
181 return this->page_counts_[pageno];
184 inline __attribute__((always_inline))
185 size_t PageStore::size() {
186 return this->top_index_ - this->free_pages_.size();
189 inline __attribute__((always_inline))
190 std::size_t PageStore::capacity()
192 return this->capacity_;