Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Support for mmap-able snapthots
[simgrid.git] / src / mc / RegionSnapshot.hpp
1 /* Copyright (c) 2007-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_REGION_SNAPSHOT_HPP
8 #define SIMGRID_MC_REGION_SNAPSHOT_HPP
9
10 #include <cstddef>
11 #include <utility>
12
13 #include <xbt/base.h>
14
15 #include "PageStore.hpp"
16 #include "AddressSpace.hpp"
17
18 namespace simgrid {
19 namespace mc {
20
21 class PerPageCopy {
22   PageStore* store_;
23   std::vector<std::size_t> pagenos_;
24 public:
25   PerPageCopy() : store_(nullptr) {}
26   PerPageCopy(PerPageCopy const& that)
27   {
28     store_ = that.store_;
29     pagenos_ = that.pagenos_;
30     for (std::size_t pageno : pagenos_)
31       store_->ref_page(pageno);
32   }
33   void clear()
34   {
35     for (std::size_t pageno : pagenos_)
36       store_->unref_page(pageno);
37     pagenos_.clear();
38   }
39   ~PerPageCopy() {
40     clear();
41   }
42
43   PerPageCopy(PerPageCopy&& that)
44   {
45     store_ = that.store_;
46     that.store_ = nullptr;
47     pagenos_ = std::move(that.pagenos_);
48     that.pagenos_.clear();
49   }
50   PerPageCopy& operator=(PerPageCopy const& that)
51   {
52     this->clear();
53     store_ = that.store_;
54     pagenos_ = that.pagenos_;
55     for (std::size_t pageno : pagenos_)
56       store_->ref_page(pageno);
57     return *this;
58   }
59   PerPageCopy& operator=(PerPageCopy && that)
60   {
61     this->clear();
62     store_ = that.store_;
63     that.store_ = nullptr;
64     pagenos_ = std::move(that.pagenos_);
65     that.pagenos_.clear();
66     return *this;
67   }
68
69   std::size_t page_count() const
70   {
71     return pagenos_.size();
72   }
73
74   std::size_t pageno(std::size_t i) const
75   {
76     return pagenos_[i];
77   }
78
79   const void* page(std::size_t i) const
80   {
81     return store_->get_page(pagenos_[i]);
82   }
83
84   PerPageCopy(PageStore& store, AddressSpace& as,
85     remote_ptr<void> addr, std::size_t page_count);
86 };
87
88 enum class RegionType {
89   Unknown = 0,
90   Heap = 1,
91   Data = 2
92 };
93
94 // TODO, use Boost.Variant instead of this
95 enum class StorageType {
96   NoData = 0,
97   Flat = 1,
98   Chunked = 2,
99   Privatized = 3
100 };
101
102 class data_deleter {
103 public:
104   enum Type {
105     Free,
106     Munmap
107   };
108 private:
109   Type type_;
110   std::size_t size_;
111 public:
112   data_deleter() : type_(Free) {}
113   data_deleter(Type type, std::size_t size) : type_(type), size_(size) {}
114   void operator()(void* p) const;
115 };
116
117 /** @brief Copy/snapshot of a given memory region
118  *
119  *  Different types of region snapshot storage types exist:
120  *  <ul>
121  *    <li>flat/dense snapshots are a simple copy of the region;</li>
122  *    <li>sparse/per-page snapshots are snaapshots which shared
123  *    identical pages.</li>
124  *    <li>privatized (SMPI global variable privatisation).
125  *  </ul>
126  *
127  *  This is handled with a variant based approch:
128  *
129  *    * `storage_type` identified the type of storage;
130  *    * an anonymous enum is used to distinguish the relevant types for
131  *      each type.
132  */
133 class RegionSnapshot {
134   static const RegionType UnknownRegion = RegionType::Unknown;
135   static const RegionType HeapRegion = RegionType::Heap;
136   static const RegionType DataRegion = RegionType::Data;
137   static const StorageType NoData = StorageType::NoData;
138   static const StorageType FlatData = StorageType::Flat;
139   static const StorageType ChunkedData = StorageType::Chunked;
140   static const StorageType PrivatizedData = StorageType::Privatized;
141 public:
142   typedef std::unique_ptr<char[], data_deleter> flat_data_ptr;
143 private:
144   RegionType region_type_;
145   StorageType storage_type_;
146   simgrid::mc::ObjectInformation* object_info_;
147
148   /** @brief  Virtual address of the region in the simulated process */
149   void *start_addr_;
150
151   /** @brief Size of the data region in bytes */
152   size_t size_;
153
154   /** @brief Permanent virtual address of the region
155    *
156    * This is usually the same address as the simuilated process address.
157    * However, when using SMPI privatization of global variables,
158    * each SMPI process has its own set of global variables stored
159    * at a different virtual address. The scheduler maps those region
160    * on the region of the global variables.
161    *
162    * */
163   void *permanent_addr_;
164
165   flat_data_ptr flat_data_;
166   PerPageCopy page_numbers_;
167   std::vector<RegionSnapshot> privatized_regions_;
168 public:
169   RegionSnapshot() :
170     region_type_(UnknownRegion),
171     storage_type_(NoData),
172     object_info_(nullptr),
173     start_addr_(nullptr),
174     size_(0),
175     permanent_addr_(nullptr)
176   {}
177   RegionSnapshot(RegionType type, void *start_addr, void* permanent_addr, size_t size) :
178     region_type_(type),
179     storage_type_(NoData),
180     object_info_(nullptr),
181     start_addr_(start_addr),
182     size_(size),
183     permanent_addr_(permanent_addr)
184   {}
185   ~RegionSnapshot() {}
186   RegionSnapshot(RegionSnapshot const&) = default;
187   RegionSnapshot& operator=(RegionSnapshot const&) = default;
188   RegionSnapshot(RegionSnapshot&& that)
189   {
190     region_type_ = that.region_type_;
191     storage_type_ = that.storage_type_;
192     object_info_ = that.object_info_;
193     start_addr_ = that.start_addr_;
194     size_ = that.size_;
195     permanent_addr_ = that.permanent_addr_;
196     flat_data_ = std::move(that.flat_data_);
197     page_numbers_ = std::move(that.page_numbers_);
198     privatized_regions_ = std::move(that.privatized_regions_);
199     that.clear();
200   }
201   RegionSnapshot& operator=(RegionSnapshot&& that)
202   {
203     region_type_ = that.region_type_;
204     storage_type_ = that.storage_type_;
205     object_info_ = that.object_info_;
206     start_addr_ = that.start_addr_;
207     size_ = that.size_;
208     permanent_addr_ = that.permanent_addr_;
209     flat_data_ = std::move(that.flat_data_);
210     page_numbers_ = std::move(that.page_numbers_);
211     privatized_regions_ = std::move(that.privatized_regions_);
212     that.clear();
213     return *this;
214   }
215
216   // Data
217
218   void clear()
219   {
220     region_type_ = UnknownRegion;
221     storage_type_ = NoData;
222     privatized_regions_.clear();
223     page_numbers_.clear();
224     flat_data_.reset();
225     object_info_ = nullptr;
226     start_addr_ = nullptr;
227     size_ = 0;
228     permanent_addr_ = nullptr;
229   }
230
231   void clear_data()
232   {
233     storage_type_ = NoData;
234     flat_data_.reset();
235     page_numbers_.clear();
236     privatized_regions_.clear();
237   }
238   
239   void flat_data(flat_data_ptr data)
240   {
241     storage_type_ = FlatData;
242     flat_data_ = std::move(data);
243     page_numbers_.clear();
244     privatized_regions_.clear();
245   }
246   const char* flat_data() const { return flat_data_.get(); }
247
248   void page_data(PerPageCopy page_data)
249   {
250     storage_type_ = ChunkedData;
251     flat_data_.reset();
252     page_numbers_ = std::move(page_data);
253     privatized_regions_.clear();
254   }
255   PerPageCopy const& page_data() const { return page_numbers_; }
256
257   void privatized_data(std::vector<RegionSnapshot> data)
258   {
259     storage_type_ = PrivatizedData;
260     flat_data_.reset();
261     page_numbers_.clear();
262     privatized_regions_ = std::move(data);
263   }
264   std::vector<RegionSnapshot> const& privatized_data() const
265   {
266     return privatized_regions_;
267   }
268   std::vector<RegionSnapshot>& privatized_data()
269   {
270     return privatized_regions_;
271   }
272
273   simgrid::mc::ObjectInformation* object_info() const { return object_info_; }
274   void object_info(simgrid::mc::ObjectInformation* info) { object_info_ = info; }
275
276   // Other getters
277
278   remote_ptr<void> start() const { return remote(start_addr_); }
279   remote_ptr<void> end() const { return remote((char*)start_addr_ + size_); }
280   remote_ptr<void> permanent_address() const { return remote(permanent_addr_); }
281   std::size_t size() const { return size_; }
282   StorageType storage_type() const { return storage_type_; }
283   RegionType region_type() const { return region_type_; }
284
285   bool contain(remote_ptr<void> p) const
286   {
287     return p >= start() && p < end();
288   }
289 };
290
291 RegionSnapshot privatized_region(
292   RegionType type, void *start_addr, void* data_addr, size_t size);
293 RegionSnapshot dense_region(
294   RegionType type, void *start_addr, void* data_addr, size_t size);
295 simgrid::mc::RegionSnapshot sparse_region(
296   RegionType type, void *start_addr, void* data_addr, size_t size);
297 simgrid::mc::RegionSnapshot region(
298   RegionType type, void *start_addr, void* data_addr, size_t size);
299   
300 }
301 }
302
303 typedef class simgrid::mc::RegionSnapshot s_mc_mem_region_t, *mc_mem_region_t;
304
305 #endif