Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid costly exceptions when looking into a map.
[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 #ifndef SIMGRID_MC_PAGESTORE_HPP
8 #define SIMGRID_MC_PAGESTORE_HPP
9
10 #include <cstdint>
11 #include <vector>
12
13 #include <unordered_map>
14 #include <unordered_set>
15
16 #include "xbt/base.h"
17
18 #include "src/mc/mc_mmu.h"
19 #include "src/mc/mc_forward.hpp"
20
21 namespace simgrid {
22 namespace mc {
23
24 /** @brief Storage for snapshot memory pages
25  *
26  * The first (lower) layer of the per-page snapshot mechanism is a page store:
27  * its responsibility is to store immutable sharable reference-counted memory
28  * pages independently of the snapshotting logic. Snapshot management and
29  * representation is 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 class PageStore {
76 public: // Types
77   typedef std::uint64_t hash_type;
78 private: // Types
79   // We are using a cheap hash to index a page.
80   // We should expect collision and we need to associate multiple page indices
81   // to the same hash.
82   typedef std::unordered_set<std::size_t> page_set_type;
83   typedef std::unordered_map<hash_type, page_set_type> pages_map_type;
84
85 private: // Fields:
86   /** First page */
87   void* memory_;
88   /** Number of available pages in virtual memory */
89   std::size_t capacity_;
90   /** Top of the used pages (index of the next available page) */
91   std::size_t top_index_;
92   /** Page reference count */
93   std::vector<std::uint64_t> page_counts_;
94   /** Index of available pages before the top */
95   std::vector<std::size_t> free_pages_;
96   /** Index from page hash to page index */
97   pages_map_type hash_index_;
98
99 private: // Methods
100   void resize(std::size_t size);
101   std::size_t alloc_page();
102   void remove_page(std::size_t pageno);
103
104 public: // Constructors
105   PageStore(PageStore const&) = delete;
106   PageStore& operator=(PageStore const&) = delete;
107   explicit PageStore(std::size_t size);
108   ~PageStore();
109
110 public: // Methods
111
112   /** @brief Decrement the reference count for a given page
113    *
114    * Decrement the reference count of this page. Used when a snapshot is destroyed.
115    *
116    * If the reference count reaches zero, the page is recycled:
117    * it is added to the `free_pages_` list and removed from the `hash_index_`.
118    *
119    * */
120   void unref_page(std::size_t pageno);
121
122   /** @brief Increment the refcount for a given page
123    *
124    * This method used to increase a reference count of a page if we know
125    * that the content of a page is the same as a page already in the page
126    * store.
127    *
128    * This will be the case if a page if soft clean: we know that is has not
129    * changed since the previous cnapshot/restoration and we can avoid
130    * hashing the page, comparing byte-per-byte to candidates.
131    * */
132   void ref_page(size_t pageno);
133
134   /** @brief Store a page in the page store */
135   std::size_t store_page(void* page);
136
137   /** @brief Get a page from its page number
138    *
139    *  @param pageno Number of the memory page in the store
140    *  @return Start of the page
141    */
142   const void* get_page(std::size_t pageno) const;
143
144 public: // Debug/test methods
145
146   /** @brief Get the number of references for a page */
147   std::size_t get_ref(std::size_t pageno);
148
149   /** @brief Get the number of used pages */
150   std::size_t size();
151
152   /** @brief Get the capacity of the page store
153    *
154    *  The capacity is expanded by a system call (mremap).
155    * */
156   std::size_t capacity();
157
158 };
159
160 XBT_ALWAYS_INLINE void PageStore::unref_page(std::size_t pageno)
161 {
162   if ((--this->page_counts_[pageno]) == 0)
163     this->remove_page(pageno);
164 }
165
166 XBT_ALWAYS_INLINE void PageStore::ref_page(size_t pageno)
167 {
168   ++this->page_counts_[pageno];
169 }
170
171 XBT_ALWAYS_INLINE const void* PageStore::get_page(std::size_t pageno) const
172 {
173   return (void*) simgrid::mc::mmu::join(pageno, (std::uintptr_t) this->memory_);
174 }
175
176 XBT_ALWAYS_INLINE std::size_t PageStore::get_ref(std::size_t pageno)
177 {
178   return this->page_counts_[pageno];
179 }
180
181 XBT_ALWAYS_INLINE std::size_t PageStore::size()
182 {
183   return this->top_index_ - this->free_pages_.size();
184 }
185
186 XBT_ALWAYS_INLINE std::size_t PageStore::capacity()
187 {
188   return this->capacity_;
189 }
190
191 }
192 }
193
194 #endif