Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename the plugins from the command line, and document it
[simgrid.git] / src / mc / PageStore.hpp
1 /* Copyright (c) 2015-2018. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef SIMGRID_MC_PAGESTORE_HPP
8 #define SIMGRID_MC_PAGESTORE_HPP
9
10 #include <cstdint>
11 #include <vector>
12
13 #include <unordered_map>
14 #include <unordered_set>
15
16 #include "xbt/base.h"
17
18 #include "src/mc/mc_forward.hpp"
19 #include "src/mc/mc_mmu.hpp"
20
21 namespace simgrid {
22 namespace mc {
23
24 /** @brief Storage for snapshot memory pages
25  *
26  * The first (lower) layer of the per-page snapshot mechanism is a page store:
27  * its responsibility is to store immutable sharable reference-counted memory
28  * pages independently of the snapshotting logic. Snapshot management and
29  * representation is handled to an higher layer. READMORE
30  *
31  * Data structure:
32  *
33  *  * A pointer (`memory_`) to a (currently anonymous) `mmap()`ed memory
34  *    region holding the memory pages (the address of the first page).
35  *
36  *    We want to keep this memory region aligned on the memory pages (so
37  *    that we might be able to create non-linear memory mappings on those
38  *    pages in the future) and be able to expand it without coyping the
39  *    data (there will be a lot of pages here): we will be able to
40  *    efficiently expand the memory mapping using `mremap()`, moving it
41  *    to another virtual address if necessary.
42  *
43  *    Because we will move this memory mapping on the virtual address
44  *    space, only the index of the page will be stored in the snapshots
45  *    and the page will always be looked up by going through `memory`:
46  *
47  *         void* page = (char*) page_store->memory + page_index << pagebits;
48  *
49  *  * The number of pages mapped in virtual memory (`capacity_`). Once all
50  *    those pages are used, we need to expand the page store with
51  *    `mremap()`.
52  *
53  *  * A reference count for each memory page `page_counts_`. Each time a
54  *    snapshot references a page, the counter is incremented. If a
55  *    snapshot is freed, the reference count is decremented. When the
56  *    reference count, of a page reaches 0 it is added to a list of available
57  *    pages (`free_pages_`).
58  *
59  *  * A list of free pages `free_pages_` which can be reused. This avoids having
60  *    to scan the reference count list to find a free page.
61  *
62  *  * When we are expanding the memory map we do not want to add thousand of page
63  *    to the `free_pages_` list and remove them just afterwards. The `top_index_`
64  *    field is an index after which all pages are free and are not in the `free_pages_`
65  *    list.
66  *
67  *  * When we are adding a page, we need to check if a page with the same
68  *    content is already in the page store in order to reuse it. For this
69  *    reason, we maintain an index (`hash_index_`) mapping the hash of a
70  *    page to the list of page indices with this hash.
71  *    We use a fast (non cryptographic) hash so there may be conflicts:
72  *    we must be able to store multiple indices for the same hash.
73  *
74  */
75 class PageStore {
76 public: // Types
77   typedef std::uint64_t hash_type;
78
79 private:
80   // Types
81   // We are using a cheap hash to index a page.
82   // We should expect collision and we need to associate multiple page indices
83   // to the same hash.
84   typedef std::unordered_set<std::size_t> page_set_type;
85   typedef std::unordered_map<hash_type, page_set_type> pages_map_type;
86
87   // Fields:
88   /** First page */
89   void* memory_;
90   /** Number of available pages in virtual memory */
91   std::size_t capacity_;
92   /** Top of the used pages (index of the next available page) */
93   std::size_t top_index_;
94   /** Page reference count */
95   std::vector<std::uint64_t> page_counts_;
96   /** Index of available pages before the top */
97   std::vector<std::size_t> free_pages_;
98   /** Index from page hash to page index */
99   pages_map_type hash_index_;
100
101   // Methods
102   void resize(std::size_t size);
103   std::size_t alloc_page();
104   void remove_page(std::size_t pageno);
105
106 public:
107   // Constructors
108   PageStore(PageStore const&) = delete;
109   PageStore& operator=(PageStore const&) = delete;
110   explicit PageStore(std::size_t size);
111   ~PageStore();
112
113   // Methods
114
115   /** @brief Decrement the reference count for a given page
116    *
117    * Decrement the reference count of this page. Used when a snapshot is destroyed.
118    *
119    * If the reference count reaches zero, the page is recycled:
120    * it is added to the `free_pages_` list and removed from the `hash_index_`.
121    *
122    * */
123   void unref_page(std::size_t pageno);
124
125   /** @brief Increment the refcount for a given page
126    *
127    * This method used to increase a reference count of a page if we know
128    * that the content of a page is the same as a page already in the page
129    * store.
130    *
131    * This will be the case if a page if soft clean: we know that is has not
132    * changed since the previous cnapshot/restoration and we can avoid
133    * hashing the page, comparing byte-per-byte to candidates.
134    * */
135   void ref_page(size_t pageno);
136
137   /** @brief Store a page in the page store */
138   std::size_t store_page(void* page);
139
140   /** @brief Get a page from its page number
141    *
142    *  @param pageno Number of the memory page in the store
143    *  @return Start of the page
144    */
145   const void* get_page(std::size_t pageno) const;
146
147   // Debug/test methods
148
149   /** @brief Get the number of references for a page */
150   std::size_t get_ref(std::size_t pageno);
151
152   /** @brief Get the number of used pages */
153   std::size_t size();
154
155   /** @brief Get the capacity of the page store
156    *
157    *  The capacity is expanded by a system call (mremap).
158    * */
159   std::size_t capacity();
160
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 }
195 }
196
197 #endif