Logo AND Algorithmique Numérique Distribuée

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