Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b91af866ed1b42b8df4c5a3b03d48e1464332342
[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 extern "C" {
22
23 static void mc_read_pagemap(uint64_t* pagemap, size_t page_start, size_t page_count);
24
25 }
26
27 // ***** Utility:
28
29 /** @brief Compte a hash for the given memory page
30  *
31  *  The page is used before inserting the page in the page store
32  *  in order to find duplicate of this pae in the page store.
33  *
34  *  @param data Memory page
35  *  @return hash off the page
36  */
37 static inline uint64_t mc_hash_page(const void* data)
38 {
39   const uint64_t* values = (const uint64_t*) data;
40   size_t n = xbt_pagesize / sizeof(uint64_t);
41
42   // This djb2:
43   uint64_t hash = 5381;
44   for (size_t i=0; i!=n; ++i) {
45     hash = ((hash << 5) + hash) + values[i];
46   }
47   return hash;
48 }
49
50 // ***** snapshot_page_manager
51
52 s_mc_pages_store::s_mc_pages_store(size_t size) :
53   memory_(NULL), capacity_(0), top_index_(0)
54 {
55   // Using mmap in order to be able to expand the region
56   // by relocating it somewhere else in the virtual memory
57   // space:
58   void * memory = ::mmap(NULL, size << xbt_pagebits, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
59   if (memory==MAP_FAILED) {
60     xbt_die("Could not mmap initial snapshot pages.");
61   }
62
63   this->top_index_ = 0;
64   this->capacity_ = size;
65   this->memory_ = memory;
66   this->page_counts_.resize(size);
67 }
68
69 s_mc_pages_store::~s_mc_pages_store()
70 {
71   ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
72 }
73
74 void s_mc_pages_store::resize(size_t size)
75 {
76   size_t new_bytesize = size << xbt_pagebits;
77
78   // Expand the memory region by moving it into another
79   // virtual memory address if necessary:
80   void* new_memory = mremap(this->memory_, this->capacity_ << xbt_pagesize, new_bytesize, MREMAP_MAYMOVE);
81   if (new_memory == MAP_FAILED) {
82     xbt_die("Could not mremap snapshot pages.");
83   }
84
85   this->capacity_ = size;
86   this->memory_ = new_memory;
87   this->page_counts_.resize(size);
88 }
89
90 /** Allocate a free page
91  *
92  *  @return index of the free page
93  */
94 size_t s_mc_pages_store::alloc_page()
95 {
96   if (this->free_pages_.empty()) {
97
98     // Expand the region:
99     if (top_index_ == this->capacity_) {
100       // All the pages are allocated, we need add more pages:
101       this->resize(2 * this->capacity_);
102     }
103
104     // Use a page from the top:
105     return this->top_index_++;
106
107   } else {
108
109     // Use a page from free_pages_ (inside of the region):
110     size_t res = this->free_pages_[this->free_pages_.size() - 1];
111     this->free_pages_.pop_back();
112     return res;
113
114   }
115 }
116
117 void s_mc_pages_store::remove_page(size_t pageno)
118 {
119   this->free_pages_.push_back(pageno);
120   void* page = mc_page_from_number(this->memory_, pageno);
121   uint64_t hash = mc_hash_page(page);
122   this->hash_index_[hash].erase(pageno);
123 }
124
125 /** Store a page in memory */
126 size_t s_mc_pages_store::store_page(void* page)
127 {
128   xbt_assert(mc_page_offset(page)==0, "Not at the beginning of a page");
129   xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
130
131   // Search the page in the snapshot pages:
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       // Page found, reuse it:
138       page_counts_[pageno]++;
139       return pageno;
140     }
141   }
142
143   // Allocate a new page for this page:
144   size_t pageno = alloc_page();
145   void* snapshot_page = (void*) this->get_page(pageno);
146   memcpy(snapshot_page, page, xbt_pagesize);
147   page_set.insert(pageno);
148   page_counts_[pageno]++;
149   return pageno;
150 }
151
152 // ***** Main C API
153
154 extern "C" {
155
156 mc_pages_store_t mc_pages_store_new()
157 {
158   return new s_mc_pages_store_t(500);
159 }
160
161 }