Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[project-description] Fix extraction of the ns-3 version.
[simgrid.git] / src / mc / sosp / PageStore.hpp
1 /* Copyright (c) 2015-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_MC_PAGESTORE_HPP
7 #define SIMGRID_MC_PAGESTORE_HPP
8
9 #include "src/mc/mc_forward.hpp"
10 #include "src/mc/mc_mmu.hpp"
11
12 #include <unordered_map>
13 #include <unordered_set>
14 #include <vector>
15
16 #ifndef XBT_ALWAYS_INLINE
17 #define XBT_ALWAYS_INLINE inline __attribute__((always_inline))
18 #endif
19
20 namespace simgrid::mc {
21
22 /** @brief Storage for snapshot memory pages
23  *
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
28  *
29  * Data structure:
30  *
31  *  * A pointer (`memory_`) to a (currently anonymous) `mmap()`ed memory
32  *    region holding the memory pages (the address of the first page).
33  *
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.
40  *
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`:
44  *
45  *         void* page = (char*) page_store->memory + page_index << pagebits;
46  *
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
49  *    `mremap()`.
50  *
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_`).
56  *
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.
59  *
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_`
63  *    list.
64  *
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.
71  *
72  */
73 class PageStore {
74 public: // Types
75   using hash_type = std::uint64_t;
76
77 private:
78   // Types
79   // We are using a cheap hash to index a page.
80   // We should expect collision and we need to associate multiple page indices
81   // to the same hash.
82   using page_set_type  = std::unordered_set<std::size_t>;
83   using pages_map_type = std::unordered_map<hash_type, page_set_type>;
84
85   // Fields:
86   /** First page */
87   void* memory_;
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_;
98
99   // Methods
100   void resize(std::size_t size);
101   std::size_t alloc_page();
102   void remove_page(std::size_t pageno);
103
104 public:
105   // Constructors
106   PageStore(PageStore const&) = delete;
107   PageStore& operator=(PageStore const&) = delete;
108   explicit PageStore(std::size_t size);
109   ~PageStore();
110
111   // Methods
112
113   /** @brief Decrement the reference count for a given page
114    *
115    * Decrement the reference count of this page. Used when a snapshot is destroyed.
116    *
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_`.
119    *
120    * */
121   void unref_page(std::size_t pageno);
122
123   /** @brief Increment the refcount for a given page
124    *
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
127    * store.
128    *
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.
132    * */
133   void ref_page(size_t pageno);
134
135   /** @brief Store a page in the page store */
136   std::size_t store_page(const void* page);
137
138   /** @brief Get a page from its page number
139    *
140    *  @param pageno Number of the memory page in the store
141    *  @return Start of the page
142    */
143   void* get_page(std::size_t pageno) const;
144
145   // Debug/test methods
146
147   /** @brief Get the number of references for a page */
148   std::size_t get_ref(std::size_t pageno) const;
149
150   /** @brief Get the number of used pages */
151   std::size_t size() const;
152
153   /** @brief Get the capacity of the page store
154    *
155    *  The capacity is expanded by a system call (mremap).
156    * */
157   std::size_t capacity() const;
158 };
159
160 XBT_ALWAYS_INLINE void PageStore::unref_page(std::size_t pageno)
161 {
162   if ((--this->page_counts_[pageno]) == 0)
163     this->remove_page(pageno);
164 }
165
166 XBT_ALWAYS_INLINE void PageStore::ref_page(size_t pageno)
167 {
168   ++this->page_counts_[pageno];
169 }
170
171 XBT_ALWAYS_INLINE void* PageStore::get_page(std::size_t pageno) const
172 {
173   return (void*)simgrid::mc::mmu::join(pageno, (std::uintptr_t)this->memory_);
174 }
175
176 XBT_ALWAYS_INLINE std::size_t PageStore::get_ref(std::size_t pageno) const
177 {
178   return this->page_counts_[pageno];
179 }
180
181 XBT_ALWAYS_INLINE std::size_t PageStore::size() const
182 {
183   return this->top_index_ - this->free_pages_.size();
184 }
185
186 XBT_ALWAYS_INLINE std::size_t PageStore::capacity() const
187 {
188   return this->capacity_;
189 }
190
191 } // namespace simgrid::mc
192
193 #endif