Logo AND Algorithmique Numérique Distribuée

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