Logo AND Algorithmique Numérique Distribuée

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