1 /* Copyright (c) 2015-2018. 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
12 #include <unordered_map>
13 #include <unordered_set>
15 #include "src/mc/mc_forward.hpp"
16 #include "src/mc/mc_mmu.hpp"
18 #ifndef XBT_ALWAYS_INLINE
19 #define XBT_ALWAYS_INLINE inline __attribute__((always_inline))
25 /** @brief Storage for snapshot memory pages
27 * The first (lower) layer of the per-page snapshot mechanism is a page store:
28 * its responsibility is to store immutable sharable reference-counted memory
29 * pages independently of the snapshotting logic. Snapshot management and
30 * representation is handled to an higher layer. READMORE
34 * * A pointer (`memory_`) to a (currently anonymous) `mmap()`ed memory
35 * region holding the memory pages (the address of the first page).
37 * We want to keep this memory region aligned on the memory pages (so
38 * that we might be able to create non-linear memory mappings on those
39 * pages in the future) and be able to expand it without coyping the
40 * data (there will be a lot of pages here): we will be able to
41 * efficiently expand the memory mapping using `mremap()`, moving it
42 * to another virtual address if necessary.
44 * Because we will move this memory mapping on the virtual address
45 * space, only the index of the page will be stored in the snapshots
46 * and the page will always be looked up by going through `memory`:
48 * void* page = (char*) page_store->memory + page_index << pagebits;
50 * * The number of pages mapped in virtual memory (`capacity_`). Once all
51 * those pages are used, we need to expand the page store with
54 * * A reference count for each memory page `page_counts_`. Each time a
55 * snapshot references a page, the counter is incremented. If a
56 * snapshot is freed, the reference count is decremented. When the
57 * reference count, of a page reaches 0 it is added to a list of available
58 * pages (`free_pages_`).
60 * * A list of free pages `free_pages_` which can be reused. This avoids having
61 * to scan the reference count list to find a free page.
63 * * When we are expanding the memory map we do not want to add thousand of page
64 * to the `free_pages_` list and remove them just afterwards. The `top_index_`
65 * field is an index after which all pages are free and are not in the `free_pages_`
68 * * When we are adding a page, we need to check if a page with the same
69 * content is already in the page store in order to reuse it. For this
70 * reason, we maintain an index (`hash_index_`) mapping the hash of a
71 * page to the list of page indices with this hash.
72 * We use a fast (non cryptographic) hash so there may be conflicts:
73 * we must be able to store multiple indices for the same hash.
78 typedef std::uint64_t hash_type;
82 // We are using a cheap hash to index a page.
83 // We should expect collision and we need to associate multiple page indices
85 typedef std::unordered_set<std::size_t> page_set_type;
86 typedef std::unordered_map<hash_type, page_set_type> pages_map_type;
91 /** Number of available pages in virtual memory */
92 std::size_t capacity_;
93 /** Top of the used pages (index of the next available page) */
94 std::size_t top_index_;
95 /** Page reference count */
96 std::vector<std::uint64_t> page_counts_;
97 /** Index of available pages before the top */
98 std::vector<std::size_t> free_pages_;
99 /** Index from page hash to page index */
100 pages_map_type hash_index_;
103 void resize(std::size_t size);
104 std::size_t alloc_page();
105 void remove_page(std::size_t pageno);
109 PageStore(PageStore const&) = delete;
110 PageStore& operator=(PageStore const&) = delete;
111 explicit PageStore(std::size_t size);
116 /** @brief Decrement the reference count for a given page
118 * Decrement the reference count of this page. Used when a snapshot is destroyed.
120 * If the reference count reaches zero, the page is recycled:
121 * it is added to the `free_pages_` list and removed from the `hash_index_`.
124 void unref_page(std::size_t pageno);
126 /** @brief Increment the refcount for a given page
128 * This method used to increase a reference count of a page if we know
129 * that the content of a page is the same as a page already in the page
132 * This will be the case if a page if soft clean: we know that is has not
133 * changed since the previous cnapshot/restoration and we can avoid
134 * hashing the page, comparing byte-per-byte to candidates.
136 void ref_page(size_t pageno);
138 /** @brief Store a page in the page store */
139 std::size_t store_page(void* page);
141 /** @brief Get a page from its page number
143 * @param pageno Number of the memory page in the store
144 * @return Start of the page
146 const void* get_page(std::size_t pageno) const;
148 // Debug/test methods
150 /** @brief Get the number of references for a page */
151 std::size_t get_ref(std::size_t pageno);
153 /** @brief Get the number of used pages */
156 /** @brief Get the capacity of the page store
158 * The capacity is expanded by a system call (mremap).
160 std::size_t capacity();
163 XBT_ALWAYS_INLINE void PageStore::unref_page(std::size_t pageno)
165 if ((--this->page_counts_[pageno]) == 0)
166 this->remove_page(pageno);
169 XBT_ALWAYS_INLINE void PageStore::ref_page(size_t pageno)
171 ++this->page_counts_[pageno];
174 XBT_ALWAYS_INLINE const void* PageStore::get_page(std::size_t pageno) const
176 return (void*)simgrid::mc::mmu::join(pageno, (std::uintptr_t)this->memory_);
179 XBT_ALWAYS_INLINE std::size_t PageStore::get_ref(std::size_t pageno)
181 return this->page_counts_[pageno];
184 XBT_ALWAYS_INLINE std::size_t PageStore::size()
186 return this->top_index_ - this->free_pages_.size();
189 XBT_ALWAYS_INLINE std::size_t PageStore::capacity()
191 return this->capacity_;
195 } // namespace simgrid