Logo AND Algorithmique Numérique Distribuée

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