Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Avoid loosing meaningful bits ot pair.p2 when hashing pointer pair
[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_private.h"
20 #include "mc_mmu.h"
21
22 #ifndef MC_PAGE_SNAPSHOT_H
23 #define MC_PAGE_SNAPSHOT_H
24
25 struct s_mc_pages_store;
26
27 #ifdef __cplusplus
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 struct s_mc_pages_store {
82 private: // Types
83   typedef uint64_t hash_type;
84   typedef boost ::unordered_set<size_t> page_set_type;
85   typedef boost::unordered_map<hash_type, page_set_type> pages_map_type;
86
87 private: // Fields:
88   /** First page
89    *
90    *  mc_page_store_get_page expects that this is the first field.
91    * */
92   void* memory_;
93   /** Number of available pages in virtual memory */
94   size_t capacity_;
95   /** Top of the used pages (index of the next available page) */
96   size_t top_index_;
97   /** Page reference count */
98   std::vector<uint64_t> page_counts_;
99   /** Index of available pages before the top */
100   std::vector<size_t> free_pages_;
101   /** Index from page hash to page index */
102   pages_map_type hash_index_;
103
104 private: // Methods
105   void resize(size_t size);
106   size_t alloc_page();
107   void remove_page(size_t pageno);
108
109 public: // Constructors
110   explicit s_mc_pages_store(size_t size);
111   ~s_mc_pages_store();
112
113 public: // 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
118    * destroyed.
119    *
120    * If the reference count reaches zero, the page is recycled:
121    * it is added to the `free_pages_` list and removed from the `hash_index_`.
122    *
123    * */
124   void unref_page(size_t pageno);
125
126   /** @brief Increment the refcount for a given page
127    *
128    * This method used to increase a reference count of a page if we know
129    * that the content of a page is the same as a page already in the page
130    * store.
131    *
132    * This will be the case if a page if soft clean: we know that is has not
133    * changed since the previous cnapshot/restoration and we can avoid
134    * hashing the page, comparing byte-per-byte to candidates.
135    * */
136   void ref_page(size_t pageno);
137
138   /** @brief Store a page in the page store */
139   size_t store_page(void* page);
140
141   /** @brief Get a page from its page number
142    *
143    *  @param Number of the memory page in the store
144    *  @return Start of the page
145    */
146   const void* get_page(size_t pageno) const;
147
148 public: // Debug/test methods
149
150   /** @brief Get the number of references for a page */
151   size_t get_ref(size_t pageno);
152
153   /** @brief Get the number of used pages */
154   size_t size();
155
156   /** @brief Get the capacity of the page store
157    *
158    *  The capacity is expanded by a system call (mremap).
159    * */
160   size_t capacity();
161
162 };
163
164 inline __attribute__((always_inline))
165 void s_mc_pages_store::unref_page(size_t pageno) {
166   if ((--this->page_counts_[pageno]) == 0) {
167     this->remove_page(pageno);
168   }
169 }
170
171 inline __attribute__((always_inline))
172 void s_mc_pages_store::ref_page(size_t pageno) {
173   ++this->page_counts_[pageno];
174 }
175
176 inline __attribute__((always_inline))
177 const void* s_mc_pages_store::get_page(size_t pageno) const {
178   return mc_page_from_number(this->memory_, pageno);
179 }
180
181 inline __attribute__((always_inline))
182 size_t s_mc_pages_store::get_ref(size_t pageno)  {
183   return this->page_counts_[pageno];
184 }
185
186 inline __attribute__((always_inline))
187 size_t s_mc_pages_store::size() {
188   return this->top_index_ - this->free_pages_.size();
189 }
190
191 inline __attribute__((always_inline))
192 size_t s_mc_pages_store::capacity() {
193   return this->capacity_;
194 }
195
196 #endif
197
198 /**
199  */
200 static inline __attribute__((always_inline))
201 const void* mc_page_store_get_page(mc_pages_store_t page_store, size_t pageno)
202 {
203   // This is page_store->memory_:
204   void* memory = *(void**)page_store;
205   return mc_page_from_number(memory, pageno);
206 }
207
208 #endif