1 /* Copyright (c) 2015-2023. 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. */
6 #ifndef SIMGRID_MC_PAGESTORE_HPP
7 #define SIMGRID_MC_PAGESTORE_HPP
9 #include "src/mc/mc_forward.hpp"
10 #include "src/mc/mc_mmu.hpp"
12 #include <unordered_map>
13 #include <unordered_set>
16 #ifndef XBT_ALWAYS_INLINE
17 #define XBT_ALWAYS_INLINE inline __attribute__((always_inline))
20 namespace simgrid::mc {
22 /** @brief Storage for snapshot memory pages
24 * The first (lower) layer of the per-page snapshot mechanism is a page store:
25 * its responsibility is to store immutable shareable reference-counted memory
26 * pages independently of the snapshotting logic. Snapshot management and
27 * representation is handled to an higher layer. READMORE
31 * * A pointer (`memory_`) to a (currently anonymous) `mmap()`ed memory
32 * region holding the memory pages (the address of the first page).
34 * We want to keep this memory region aligned on the memory pages (so
35 * that we might be able to create non-linear memory mappings on those
36 * pages in the future) and be able to expand it without copying the
37 * data (there will be a lot of pages here): we will be able to
38 * efficiently expand the memory mapping using `mremap()`, moving it
39 * to another virtual address if necessary.
41 * Because we will move this memory mapping on the virtual address
42 * space, only the index of the page will be stored in the snapshots
43 * and the page will always be looked up by going through `memory`:
45 * void* page = (char*) page_store->memory + page_index << pagebits;
47 * * The number of pages mapped in virtual memory (`capacity_`). Once all
48 * those pages are used, we need to expand the page store with
51 * * A reference count for each memory page `page_counts_`. Each time a
52 * snapshot references a page, the counter is incremented. If a
53 * snapshot is freed, the reference count is decremented. When the
54 * reference count, of a page reaches 0 it is added to a list of available
55 * pages (`free_pages_`).
57 * * A list of free pages `free_pages_` which can be reused. This avoids having
58 * to scan the reference count list to find a free page.
60 * * When we are expanding the memory map we do not want to add thousand of page
61 * to the `free_pages_` list and remove them just afterwards. The `top_index_`
62 * field is an index after which all pages are free and are not in the `free_pages_`
65 * * When we are adding a page, we need to check if a page with the same
66 * content is already in the page store in order to reuse it. For this
67 * reason, we maintain an index (`hash_index_`) mapping the hash of a
68 * page to the list of page indices with this hash.
69 * We use a fast (non cryptographic) hash so there may be conflicts:
70 * we must be able to store multiple indices for the same hash.
75 using hash_type = std::uint64_t;
79 // We are using a cheap hash to index a page.
80 // We should expect collision and we need to associate multiple page indices
82 using page_set_type = std::unordered_set<std::size_t>;
83 using pages_map_type = std::unordered_map<hash_type, page_set_type>;
88 /** Number of available pages in virtual memory */
89 std::size_t capacity_;
90 /** Top of the used pages (index of the next available page) */
91 std::size_t top_index_;
92 /** Page reference count */
93 std::vector<std::uint64_t> page_counts_;
94 /** Index of available pages before the top */
95 std::vector<std::size_t> free_pages_;
96 /** Index from page hash to page index */
97 pages_map_type hash_index_;
100 void resize(std::size_t size);
101 std::size_t alloc_page();
102 void remove_page(std::size_t pageno);
106 PageStore(PageStore const&) = delete;
107 PageStore& operator=(PageStore const&) = delete;
108 explicit PageStore(std::size_t size);
113 /** @brief Decrement the reference count for a given page
115 * Decrement the reference count of this page. Used when a snapshot is destroyed.
117 * If the reference count reaches zero, the page is recycled:
118 * it is added to the `free_pages_` list and removed from the `hash_index_`.
121 void unref_page(std::size_t pageno);
123 /** @brief Increment the refcount for a given page
125 * This method used to increase a reference count of a page if we know
126 * that the content of a page is the same as a page already in the page
129 * This will be the case if a page if soft clean: we know that is has not
130 * changed since the previous snapshot/restoration and we can avoid
131 * hashing the page, comparing byte-per-byte to candidates.
133 void ref_page(size_t pageno);
135 /** @brief Store a page in the page store */
136 std::size_t store_page(const void* page);
138 /** @brief Get a page from its page number
140 * @param pageno Number of the memory page in the store
141 * @return Start of the page
143 void* get_page(std::size_t pageno) const;
145 // Debug/test methods
147 /** @brief Get the number of references for a page */
148 std::size_t get_ref(std::size_t pageno) const;
150 /** @brief Get the number of used pages */
151 std::size_t size() const;
153 /** @brief Get the capacity of the page store
155 * The capacity is expanded by a system call (mremap).
157 std::size_t capacity() const;
160 XBT_ALWAYS_INLINE void PageStore::unref_page(std::size_t pageno)
162 if ((--this->page_counts_[pageno]) == 0)
163 this->remove_page(pageno);
166 XBT_ALWAYS_INLINE void PageStore::ref_page(size_t pageno)
168 ++this->page_counts_[pageno];
171 XBT_ALWAYS_INLINE void* PageStore::get_page(std::size_t pageno) const
173 return (void*)simgrid::mc::mmu::join(pageno, (std::uintptr_t)this->memory_);
176 XBT_ALWAYS_INLINE std::size_t PageStore::get_ref(std::size_t pageno) const
178 return this->page_counts_[pageno];
181 XBT_ALWAYS_INLINE std::size_t PageStore::size() const
183 return this->top_index_ - this->free_pages_.size();
186 XBT_ALWAYS_INLINE std::size_t PageStore::capacity() const
188 return this->capacity_;
191 } // namespace simgrid::mc