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