Logo AND Algorithmique Numérique Distribuée

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