Logo AND Algorithmique Numérique Distribuée

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