Logo AND Algorithmique Numérique Distribuée

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