Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use C++11 <random> instead of rand().
[simgrid.git] / src / mc / sosp / PageStore.hpp
1 /* Copyright (c) 2015-2019. 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 <cstdint>
10 #include <vector>
11
12 #include <unordered_map>
13 #include <unordered_set>
14
15 #include "src/mc/mc_forward.hpp"
16 #include "src/mc/mc_mmu.hpp"
17
18 #ifndef XBT_ALWAYS_INLINE
19 #define XBT_ALWAYS_INLINE inline __attribute__((always_inline))
20 #endif
21
22 namespace simgrid {
23 namespace mc {
24
25 /** @brief Storage for snapshot memory pages
26  *
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
31  *
32  * Data structure:
33  *
34  *  * A pointer (`memory_`) to a (currently anonymous) `mmap()`ed memory
35  *    region holding the memory pages (the address of the first page).
36  *
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.
43  *
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`:
47  *
48  *         void* page = (char*) page_store->memory + page_index << pagebits;
49  *
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
52  *    `mremap()`.
53  *
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_`).
59  *
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.
62  *
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_`
66  *    list.
67  *
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.
74  *
75  */
76 class PageStore {
77 public: // Types
78   typedef std::uint64_t hash_type;
79
80 private:
81   // Types
82   // We are using a cheap hash to index a page.
83   // We should expect collision and we need to associate multiple page indices
84   // to the same hash.
85   typedef std::unordered_set<std::size_t> page_set_type;
86   typedef std::unordered_map<hash_type, page_set_type> pages_map_type;
87
88   // Fields:
89   /** First page */
90   void* memory_;
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_;
101
102   // Methods
103   void resize(std::size_t size);
104   std::size_t alloc_page();
105   void remove_page(std::size_t pageno);
106
107 public:
108   // Constructors
109   PageStore(PageStore const&) = delete;
110   PageStore& operator=(PageStore const&) = delete;
111   explicit PageStore(std::size_t size);
112   ~PageStore();
113
114   // Methods
115
116   /** @brief Decrement the reference count for a given page
117    *
118    * Decrement the reference count of this page. Used when a snapshot is destroyed.
119    *
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_`.
122    *
123    * */
124   void unref_page(std::size_t pageno);
125
126   /** @brief Increment the refcount for a given page
127    *
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
130    * store.
131    *
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.
135    * */
136   void ref_page(size_t pageno);
137
138   /** @brief Store a page in the page store */
139   std::size_t store_page(void* page);
140
141   /** @brief Get a page from its page number
142    *
143    *  @param pageno Number of the memory page in the store
144    *  @return Start of the page
145    */
146   const void* get_page(std::size_t pageno) const;
147
148   // Debug/test methods
149
150   /** @brief Get the number of references for a page */
151   std::size_t get_ref(std::size_t pageno);
152
153   /** @brief Get the number of used pages */
154   std::size_t size();
155
156   /** @brief Get the capacity of the page store
157    *
158    *  The capacity is expanded by a system call (mremap).
159    * */
160   std::size_t capacity();
161 };
162
163 XBT_ALWAYS_INLINE void PageStore::unref_page(std::size_t pageno)
164 {
165   if ((--this->page_counts_[pageno]) == 0)
166     this->remove_page(pageno);
167 }
168
169 XBT_ALWAYS_INLINE void PageStore::ref_page(size_t pageno)
170 {
171   ++this->page_counts_[pageno];
172 }
173
174 XBT_ALWAYS_INLINE const void* PageStore::get_page(std::size_t pageno) const
175 {
176   return (void*)simgrid::mc::mmu::join(pageno, (std::uintptr_t)this->memory_);
177 }
178
179 XBT_ALWAYS_INLINE std::size_t PageStore::get_ref(std::size_t pageno)
180 {
181   return this->page_counts_[pageno];
182 }
183
184 XBT_ALWAYS_INLINE std::size_t PageStore::size()
185 {
186   return this->top_index_ - this->free_pages_.size();
187 }
188
189 XBT_ALWAYS_INLINE std::size_t PageStore::capacity()
190 {
191   return this->capacity_;
192 }
193
194 } // namespace mc
195 } // namespace simgrid
196
197 #endif