Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Add support of privatized global variables in per-page snapshot
[simgrid.git] / src / mc / mc_page_store.cpp
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 <unistd.h>
8 #include <string.h> // memcpy, memcp
9
10 #include <boost/foreach.hpp>
11
12 #include <xbt.h>
13
14 #include "mc_page_store.h"
15
16 #include "mc_mmu.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc,
19                                 "Logging specific to mc_page_snapshot");
20
21 // ***** Utility:
22
23 /** @brief Compte a hash for the given memory page
24  *
25  *  The page is used before inserting the page in the page store
26  *  in order to find duplicate of this pae in the page store.
27  *
28  *  @param data Memory page
29  *  @return hash off the page
30  */
31 static inline  __attribute__ ((always_inline))
32 uint64_t mc_hash_page(const void* data)
33 {
34   const uint64_t* values = (const uint64_t*) data;
35   size_t n = xbt_pagesize / sizeof(uint64_t);
36
37   // This djb2:
38   uint64_t hash = 5381;
39   for (size_t i=0; i!=n; ++i) {
40     hash = ((hash << 5) + hash) + values[i];
41   }
42   return hash;
43 }
44
45 // ***** snapshot_page_manager
46
47 s_mc_pages_store::s_mc_pages_store(size_t size) :
48   memory_(NULL), capacity_(0), top_index_(0)
49 {
50   // Using mmap in order to be able to expand the region
51   // by relocating it somewhere else in the virtual memory
52   // space:
53   void * memory = ::mmap(NULL, size << xbt_pagebits, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
54   if (memory==MAP_FAILED) {
55     xbt_die("Could not mmap initial snapshot pages.");
56   }
57
58   this->top_index_ = 0;
59   this->capacity_ = size;
60   this->memory_ = memory;
61   this->page_counts_.resize(size);
62 }
63
64 s_mc_pages_store::~s_mc_pages_store()
65 {
66   ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
67 }
68
69 void s_mc_pages_store::resize(size_t size)
70 {
71   size_t old_bytesize = this->capacity_ << xbt_pagebits;
72   size_t new_bytesize = size << xbt_pagebits;
73
74   // Expand the memory region by moving it into another
75   // virtual memory address if necessary:
76   void* new_memory = mremap(this->memory_, old_bytesize, new_bytesize, MREMAP_MAYMOVE);
77   if (new_memory == MAP_FAILED) {
78     xbt_die("Could not mremap snapshot pages.");
79   }
80
81   this->capacity_ = size;
82   this->memory_ = new_memory;
83   this->page_counts_.resize(size, 0);
84 }
85
86 /** Allocate a free page
87  *
88  *  @return index of the free page
89  */
90 size_t s_mc_pages_store::alloc_page()
91 {
92   if (this->free_pages_.empty()) {
93
94     // Expand the region:
95     if (this->top_index_ == this->capacity_) {
96       // All the pages are allocated, we need add more pages:
97       this->resize(2 * this->capacity_);
98     }
99
100     // Use a page from the top:
101     return this->top_index_++;
102
103   } else {
104
105     // Use a page from free_pages_ (inside of the region):
106     size_t res = this->free_pages_[this->free_pages_.size() - 1];
107     this->free_pages_.pop_back();
108     return res;
109
110   }
111 }
112
113 void s_mc_pages_store::remove_page(size_t pageno)
114 {
115   this->free_pages_.push_back(pageno);
116   void* page = mc_page_from_number(this->memory_, pageno);
117   uint64_t hash = mc_hash_page(page);
118   this->hash_index_[hash].erase(pageno);
119 }
120
121 /** Store a page in memory */
122 size_t s_mc_pages_store::store_page(void* page)
123 {
124   xbt_assert(mc_page_offset(page)==0, "Not at the beginning of a page");
125   xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
126
127   // First, we check if a page with the same content is already in the page
128   // store:
129   //  1. compute the hash of the page;
130   //  2. find pages with the same hash using `hash_index_`;
131   //  3. find a page with the same content.
132   uint64_t hash = mc_hash_page(page);
133   page_set_type& page_set = this->hash_index_[hash];
134   BOOST_FOREACH (size_t pageno, page_set) {
135     const void* snapshot_page = this->get_page(pageno);
136     if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
137
138       // If a page with the same content is already in the page store it is
139       // reused and its reference count is incremented.
140       page_counts_[pageno]++;
141       return pageno;
142
143     }
144   }
145
146   // Otherwise, a new page is allocated in the page store and the content
147   // of the page is `memcpy()`-ed to this new page.
148   size_t pageno = alloc_page();
149   void* snapshot_page = (void*) this->get_page(pageno);
150   memcpy(snapshot_page, page, xbt_pagesize);
151   page_set.insert(pageno);
152   page_counts_[pageno]++;
153   return pageno;
154 }
155
156 // ***** Main C API
157
158 extern "C" {
159
160 mc_pages_store_t mc_pages_store_new()
161 {
162   return new s_mc_pages_store_t(500);
163 }
164
165 }