Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improving the performance of the NS-3 bindings by:
[simgrid.git] / src / mc / sosp / PageStore.cpp
1 /* Copyright (c) 2015-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <sys/mman.h>
7 #ifdef __FreeBSD__
8 #define MAP_POPULATE MAP_PREFAULT_READ
9 #endif
10
11 #include "src/internal_config.h"
12 #include "xbt/log.h"
13 #include "xbt/sysdep.h"
14
15 #include "src/mc/sosp/PageStore.hpp"
16 #include "src/mc/mc_mmu.hpp"
17
18 #include <cstring> // memcpy, memcmp
19 #include <unistd.h>
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_page_snapshot, mc, "Logging specific to mc_page_snapshot");
22
23 namespace simgrid {
24 namespace mc {
25
26 /** @brief Compute a hash for the given memory page
27  *
28  *  The page is used before inserting the page in the page store
29  *  in order to find duplicate of this page in the page store.
30  *
31  *  @param data Memory page
32  *  @return hash off the page
33  */
34 static XBT_ALWAYS_INLINE PageStore::hash_type mc_hash_page(const void* data)
35 {
36   const std::uint64_t* values = (const uint64_t*)data;
37   std::size_t n               = xbt_pagesize / sizeof(uint64_t);
38
39   // This djb2:
40   std::uint64_t hash = 5381;
41   for (std::size_t i = 0; i != n; ++i)
42     hash = ((hash << 5) + hash) + values[i];
43   return hash;
44 }
45
46 // ***** snapshot_page_manager
47
48 PageStore::PageStore(std::size_t size) : memory_(nullptr), capacity_(size), top_index_(0)
49 {
50   // Using mmap in order to be able to expand the region by relocating it somewhere else in the virtual memory space:
51   void* memory =
52       ::mmap(nullptr, size << xbt_pagebits, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
53   if (memory == MAP_FAILED)
54     xbt_die("Could not mmap initial snapshot pages.");
55
56   this->top_index_ = 0;
57   this->memory_    = memory;
58   this->page_counts_.resize(size);
59 }
60
61 PageStore::~PageStore()
62 {
63   ::munmap(this->memory_, this->capacity_ << xbt_pagebits);
64 }
65
66 void PageStore::resize(std::size_t size)
67 {
68   size_t old_bytesize = this->capacity_ << xbt_pagebits;
69   size_t new_bytesize = size << xbt_pagebits;
70   void* new_memory;
71
72   // Expand the memory region by moving it into another
73   // virtual memory address if necessary:
74 #if HAVE_MREMAP
75   new_memory = mremap(this->memory_, old_bytesize, new_bytesize, MREMAP_MAYMOVE);
76   if (new_memory == MAP_FAILED)
77     xbt_die("Could not mremap snapshot pages.");
78 #else
79   if (new_bytesize > old_bytesize) {
80     // Grow: first try to add new space after current map
81     new_memory = mmap((char*)this->memory_ + old_bytesize, new_bytesize - old_bytesize, PROT_READ | PROT_WRITE,
82                       MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
83     if (new_memory == MAP_FAILED)
84       xbt_die("Could not mremap snapshot pages.");
85     // Check if expanding worked
86     if (new_memory != (char*)this->memory_ + old_bytesize) {
87       // New memory segment could not be put at the end of this->memory_,
88       // so cancel this one and try to rellocate everything and copy data
89       munmap(new_memory, new_bytesize - old_bytesize);
90       new_memory =
91           mmap(nullptr, new_bytesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
92       if (new_memory == MAP_FAILED)
93         xbt_die("Could not mremap snapshot pages.");
94       memcpy(new_memory, this->memory_, old_bytesize);
95       munmap(this->memory_, old_bytesize);
96     }
97   } else {
98     // We don't have functions to shrink a mapping, so leave memory as
99     // it is for now
100     new_memory = this->memory_;
101   }
102 #endif
103
104   this->capacity_ = size;
105   this->memory_   = new_memory;
106   this->page_counts_.resize(size, 0);
107 }
108
109 /** Allocate a free page
110  *
111  *  @return index of the free page
112  */
113 std::size_t PageStore::alloc_page()
114 {
115   if (this->free_pages_.empty()) {
116
117     // Expand the region:
118     if (this->top_index_ == this->capacity_)
119       // All the pages are allocated, we need add more pages:
120       this->resize(2 * this->capacity_);
121
122     // Use a page from the top:
123     return this->top_index_++;
124
125   } else {
126
127     // Use a page from free_pages_ (inside of the region):
128     size_t res = this->free_pages_[this->free_pages_.size() - 1];
129     this->free_pages_.pop_back();
130     return res;
131   }
132 }
133
134 void PageStore::remove_page(std::size_t pageno)
135 {
136   this->free_pages_.push_back(pageno);
137   const void* page = this->get_page(pageno);
138   hash_type hash   = mc_hash_page(page);
139   this->hash_index_[hash].erase(pageno);
140 }
141
142 /** Store a page in memory */
143 std::size_t PageStore::store_page(void* page)
144 {
145   xbt_assert(top_index_ <= this->capacity_, "top_index is not consistent");
146
147   // First, we check if a page with the same content is already in the page store:
148   //  1. compute the hash of the page
149   //  2. find pages with the same hash using `hash_index_`
150   //  3. find a page with the same content
151   hash_type hash = mc_hash_page(page);
152
153   // Try to find a duplicate in set of pages with the same hash:
154   page_set_type& page_set = this->hash_index_[hash];
155   for (size_t const& pageno : page_set) {
156     const void* snapshot_page = this->get_page(pageno);
157     if (memcmp(page, snapshot_page, xbt_pagesize) == 0) {
158
159       // If a page with the same content is already in the page store it's reused and its refcount is incremented.
160       page_counts_[pageno]++;
161       return pageno;
162     }
163   }
164
165   // Otherwise, a new page is allocated in the page store and the content of the page is `memcpy()`-ed to this new page.
166   std::size_t pageno = alloc_page();
167   xbt_assert(this->page_counts_[pageno] == 0, "Allocated page is already used");
168   void* snapshot_page = (void*)this->get_page(pageno);
169   memcpy(snapshot_page, page, xbt_pagesize);
170   page_set.insert(pageno);
171   page_counts_[pageno]++;
172   return pageno;
173 }
174
175 } // namespace mc
176 } // namespace simgrid