Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Documentation
[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 <mc/PageStore.hpp>
16 #include <mc/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 std::size_t* pagenos() const { return pagenos_.data(); }
80   std::size_t*       pagenos()       { return pagenos_.data(); }
81
82   const void* page(std::size_t i) const
83   {
84     return store_->get_page(pagenos_[i]);
85   }
86
87   PerPageCopy(PageStore& store, AddressSpace& as,
88     remote_ptr<void> addr, std::size_t page_count,
89     const size_t* ref_page_numbers, const std::uint64_t* pagemap);
90 };
91
92 enum class RegionType {
93   Unknown = 0,
94   Heap = 1,
95   Data = 2
96 };
97
98 // TODO, use Boost.Variant instead of this
99 enum class StorageType {
100   NoData = 0,
101   Flat = 1,
102   Chunked = 2,
103   Privatized = 3
104 };
105
106 class data_deleter {
107 public:
108   enum Type {
109     Free,
110     Munmap
111   };
112 private:
113   Type type_;
114   std::size_t size_;
115 public:
116   data_deleter() : type_(Free) {}
117   data_deleter(Type type, std::size_t size) : type_(type), size_(size) {}
118   void operator()(void* p) const;
119 };
120
121 typedef std::unique_ptr<char[], data_deleter> unique_data_ptr;
122
123 /** A copy/snapshot of a given memory region
124  *
125  *  Different types of region snapshot storage types exist:
126  *
127  *  * flat/dense snapshots are a simple copy of the region;
128  *
129  *  * sparse/per-page snapshots are snaapshots which shared
130  *    identical pages.
131  *
132  *  * privatized (SMPI global variable privatisation).
133  *
134  *  This is handled with a variant based approch:
135  *
136  *  * `storage_type` identified the type of storage;
137  *
138  *  * an anonymous enum is used to distinguish the relevant types for
139  *    each type.
140  */
141 class RegionSnapshot {
142 public:
143   static const RegionType UnknownRegion = RegionType::Unknown;
144   static const RegionType HeapRegion = RegionType::Heap;
145   static const RegionType DataRegion = RegionType::Data;
146   static const StorageType NoData = StorageType::NoData;
147   static const StorageType FlatData = StorageType::Flat;
148   static const StorageType ChunkedData = StorageType::Chunked;
149   static const StorageType PrivatizedData = StorageType::Privatized;
150 public:
151   typedef unique_data_ptr flat_data_ptr;
152 private:
153   RegionType region_type_;
154   StorageType storage_type_;
155   simgrid::mc::ObjectInformation* object_info_;
156
157   /** @brief  Virtual address of the region in the simulated process */
158   void *start_addr_;
159
160   /** @brief Size of the data region in bytes */
161   size_t size_;
162
163   /** @brief Permanent virtual address of the region
164    *
165    * This is usually the same address as the simuilated process address.
166    * However, when using SMPI privatization of global variables,
167    * each SMPI process has its own set of global variables stored
168    * at a different virtual address. The scheduler maps those region
169    * on the region of the global variables.
170    *
171    * */
172   void *permanent_addr_;
173
174   flat_data_ptr flat_data_;
175   PerPageCopy page_numbers_;
176   std::vector<RegionSnapshot> privatized_regions_;
177 public:
178   RegionSnapshot() :
179     region_type_(UnknownRegion),
180     storage_type_(NoData),
181     object_info_(nullptr),
182     start_addr_(nullptr),
183     size_(0),
184     permanent_addr_(nullptr)
185   {}
186   RegionSnapshot(RegionType type, void *start_addr, void* permanent_addr, size_t size) :
187     region_type_(type),
188     storage_type_(NoData),
189     object_info_(nullptr),
190     start_addr_(start_addr),
191     size_(size),
192     permanent_addr_(permanent_addr)
193   {}
194   ~RegionSnapshot() {}
195   RegionSnapshot(RegionSnapshot const&) = default;
196   RegionSnapshot& operator=(RegionSnapshot const&) = default;
197   RegionSnapshot(RegionSnapshot&& that)
198   {
199     region_type_ = that.region_type_;
200     storage_type_ = that.storage_type_;
201     object_info_ = that.object_info_;
202     start_addr_ = that.start_addr_;
203     size_ = that.size_;
204     permanent_addr_ = that.permanent_addr_;
205     flat_data_ = std::move(that.flat_data_);
206     page_numbers_ = std::move(that.page_numbers_);
207     privatized_regions_ = std::move(that.privatized_regions_);
208     that.clear();
209   }
210   RegionSnapshot& operator=(RegionSnapshot&& that)
211   {
212     region_type_ = that.region_type_;
213     storage_type_ = that.storage_type_;
214     object_info_ = that.object_info_;
215     start_addr_ = that.start_addr_;
216     size_ = that.size_;
217     permanent_addr_ = that.permanent_addr_;
218     flat_data_ = std::move(that.flat_data_);
219     page_numbers_ = std::move(that.page_numbers_);
220     privatized_regions_ = std::move(that.privatized_regions_);
221     that.clear();
222     return *this;
223   }
224
225   // Data
226
227   void clear()
228   {
229     region_type_ = UnknownRegion;
230     storage_type_ = NoData;
231     privatized_regions_.clear();
232     page_numbers_.clear();
233     flat_data_.reset();
234     object_info_ = nullptr;
235     start_addr_ = nullptr;
236     size_ = 0;
237     permanent_addr_ = nullptr;
238   }
239
240   void clear_data()
241   {
242     storage_type_ = NoData;
243     flat_data_.reset();
244     page_numbers_.clear();
245     privatized_regions_.clear();
246   }
247   
248   void flat_data(flat_data_ptr data)
249   {
250     storage_type_ = FlatData;
251     flat_data_ = std::move(data);
252     page_numbers_.clear();
253     privatized_regions_.clear();
254   }
255   const char* flat_data() const { return flat_data_.get(); }
256
257   void page_data(PerPageCopy page_data)
258   {
259     storage_type_ = ChunkedData;
260     flat_data_.reset();
261     page_numbers_ = std::move(page_data);
262     privatized_regions_.clear();
263   }
264   PerPageCopy const& page_data() const { return page_numbers_; }
265
266   void privatized_data(std::vector<RegionSnapshot> data)
267   {
268     storage_type_ = PrivatizedData;
269     flat_data_.reset();
270     page_numbers_.clear();
271     privatized_regions_ = std::move(data);
272   }
273   std::vector<RegionSnapshot> const& privatized_data() const
274   {
275     return privatized_regions_;
276   }
277   std::vector<RegionSnapshot>& privatized_data()
278   {
279     return privatized_regions_;
280   }
281
282   simgrid::mc::ObjectInformation* object_info() const { return object_info_; }
283   void object_info(simgrid::mc::ObjectInformation* info) { object_info_ = info; }
284
285   // Other getters
286
287   remote_ptr<void> start() const { return remote(start_addr_); }
288   remote_ptr<void> end() const { return remote((char*)start_addr_ + size_); }
289   remote_ptr<void> permanent_address() const { return remote(permanent_addr_); }
290   std::size_t size() const { return size_; }
291   StorageType storage_type() const { return storage_type_; }
292   RegionType region_type() const { return region_type_; }
293
294   bool contain(remote_ptr<void> p) const
295   {
296     return p >= start() && p < end();
297   }
298 };
299
300 RegionSnapshot privatized_region(
301   RegionType type, void *start_addr, void* data_addr, size_t size);
302 RegionSnapshot dense_region(
303   RegionType type, void *start_addr, void* data_addr, size_t size);
304 simgrid::mc::RegionSnapshot sparse_region(
305   RegionType type, void *start_addr, void* data_addr, size_t size,
306   RegionSnapshot const* ref_region);
307 simgrid::mc::RegionSnapshot region(
308   RegionType type, void *start_addr, void* data_addr, size_t size,
309   RegionSnapshot const* ref_region);
310
311 }
312 }
313
314 typedef class simgrid::mc::RegionSnapshot s_mc_mem_region_t, *mc_mem_region_t;
315
316 #endif