1 /* Copyright (c) 2014. The SimGrid Team.
2 * All rights reserved. */
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. */
8 #include <string.h> // memcpy, memcp
12 #include <boost/foreach.hpp>
16 #include "mc_page_store.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc,
21 "Logging specific to mc_page_snapshot");
25 /** @brief Compte a hash for the given memory page
27 * The page is used before inserting the page in the page store
28 * in order to find duplicate of this pae in the page store.
30 * @param data Memory page
31 * @return hash off the page
33 static inline __attribute__ ((always_inline))
34 uint64_t mc_hash_page(const void* data)
36 const uint64_t* values = (const uint64_t*) data;
37 size_t n = xbt_pagesize / sizeof(uint64_t);
41 for (size_t i=0; i!=n; ++i) {
42 hash = ((hash << 5) + hash) + values[i];
47 // ***** snapshot_page_manager
49 s_mc_pages_store::s_mc_pages_store(size_t size) :
50 memory_(NULL), capacity_(0), top_index_(0)
52 // Using mmap in order to be able to expand the region
53 // by relocating it somewhere else in the virtual memory
55 void * memory = ::mmap(NULL, size << xbt_pagebits, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
56 if (memory==MAP_FAILED) {
57 xbt_die("Could not mmap initial snapshot pages.");
61 this->capacity_ = size;
62 this->memory_ = memory;
63 this->page_counts_.resize(size);
66 s_mc_pages_store::~s_mc_pages_store()
68 ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
71 void s_mc_pages_store::resize(size_t size)
73 size_t old_bytesize = this->capacity_ << xbt_pagebits;
74 size_t new_bytesize = size << xbt_pagebits;
76 // Expand the memory region by moving it into another
77 // virtual memory address if necessary:
78 void* new_memory = mremap(this->memory_, old_bytesize, new_bytesize, MREMAP_MAYMOVE);
79 if (new_memory == MAP_FAILED) {
80 xbt_die("Could not mremap snapshot pages.");
83 this->capacity_ = size;
84 this->memory_ = new_memory;
85 this->page_counts_.resize(size, 0);
88 /** Allocate a free page
90 * @return index of the free page
92 size_t s_mc_pages_store::alloc_page()
94 if (this->free_pages_.empty()) {
97 if (this->top_index_ == this->capacity_) {
98 // All the pages are allocated, we need add more pages:
99 this->resize(2 * this->capacity_);
102 // Use a page from the top:
103 return this->top_index_++;
107 // Use a page from free_pages_ (inside of the region):
108 size_t res = this->free_pages_[this->free_pages_.size() - 1];
109 this->free_pages_.pop_back();
115 void s_mc_pages_store::remove_page(size_t pageno)
117 this->free_pages_.push_back(pageno);
118 const void* page = this->get_page(pageno);
119 uint64_t hash = mc_hash_page(page);
120 this->hash_index_[hash].erase(pageno);
123 /** Store a page in memory */
124 size_t s_mc_pages_store::store_page(void* page)
126 xbt_assert(mc_page_offset(page)==0, "Not at the beginning of a page");
127 xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
129 // First, we check if a page with the same content is already in the page
131 // 1. compute the hash of the page;
132 // 2. find pages with the same hash using `hash_index_`;
133 // 3. find a page with the same content.
134 uint64_t hash = mc_hash_page(page);
135 page_set_type& page_set = this->hash_index_[hash];
136 BOOST_FOREACH (size_t pageno, page_set) {
137 const void* snapshot_page = this->get_page(pageno);
138 if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
140 // If a page with the same content is already in the page store it is
141 // reused and its reference count is incremented.
142 page_counts_[pageno]++;
148 // Otherwise, a new page is allocated in the page store and the content
149 // of the page is `memcpy()`-ed to this new page.
150 size_t pageno = alloc_page();
151 xbt_assert(this->page_counts_[pageno]==0, "Allocated page is already used");
152 void* snapshot_page = (void*) this->get_page(pageno);
153 memcpy(snapshot_page, page, xbt_pagesize);
154 page_set.insert(pageno);
155 page_counts_[pageno]++;
163 mc_pages_store_t mc_pages_store_new()
165 return new s_mc_pages_store_t(500);