Logo AND Algorithmique Numérique Distribuée

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