1 /* Copyright (c) 2007-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #ifndef SIMGRID_MC_REGION_SNAPSHOT_HPP
8 #define SIMGRID_MC_REGION_SNAPSHOT_HPP
18 #include "src/mc/RemotePtr.hpp"
19 #include "src/mc/PageStore.hpp"
20 #include "src/mc/AddressSpace.hpp"
21 #include "src/mc/ChunkedData.hpp"
26 enum class RegionType {
32 // TODO, use Boost.Variant instead of this
33 enum class StorageType {
46 void* data_ = nullptr;
48 Type type_ = Type::Malloc;
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) {}
55 void clear() noexcept;
56 ~Buffer() noexcept { clear(); }
58 static Buffer malloc(std::size_t size)
60 return Buffer(size, Type::Malloc);
62 static Buffer mmap(std::size_t size)
64 return Buffer(size, Type::Mmap);
68 Buffer(Buffer const& buffer) = delete;
69 Buffer& operator=(Buffer const& buffer) = delete;
72 Buffer(Buffer&& that) noexcept
73 : data_(that.data_), size_(that.size_), type_(that.type_)
77 that.type_ = Type::Malloc;
79 Buffer& operator=(Buffer&& that) noexcept
87 that.type_ = Type::Malloc;
91 void* get() { return data_; }
92 const void* get() const { return data_; }
93 std::size_t size() const { return size_; }
96 /** A copy/snapshot of a given memory region
98 * Different types of region snapshot storage types exist:
100 * * flat/dense snapshots are a simple copy of the region;
102 * * sparse/per-page snapshots are snaapshots which shared
105 * * privatized (SMPI global variable privatisation).
107 * This is handled with a variant based approch:
109 * * `storage_type` identified the type of storage;
111 * * an anonymous enum is used to distinguish the relevant types for
114 class RegionSnapshot {
116 static const RegionType UnknownRegion = RegionType::Unknown;
117 static const RegionType HeapRegion = RegionType::Heap;
118 static const RegionType DataRegion = RegionType::Data;
120 RegionType region_type_;
121 StorageType storage_type_;
122 simgrid::mc::ObjectInformation* object_info_;
124 /** @brief Virtual address of the region in the simulated process */
127 /** @brief Size of the data region in bytes */
130 /** @brief Permanent virtual address of the region
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.
139 void *permanent_addr_;
142 ChunkedData page_numbers_;
143 std::vector<RegionSnapshot> privatized_regions_;
146 region_type_(UnknownRegion),
147 storage_type_(StorageType::NoData),
148 object_info_(nullptr),
149 start_addr_(nullptr),
151 permanent_addr_(nullptr)
153 RegionSnapshot(RegionType type, void *start_addr, void* permanent_addr, size_t size) :
155 storage_type_(StorageType::NoData),
156 object_info_(nullptr),
157 start_addr_(start_addr),
159 permanent_addr_(permanent_addr)
162 RegionSnapshot(RegionSnapshot const&) = default;
163 RegionSnapshot& operator=(RegionSnapshot const&) = default;
164 RegionSnapshot(RegionSnapshot&& that)
166 region_type_ = that.region_type_;
167 storage_type_ = that.storage_type_;
168 object_info_ = that.object_info_;
169 start_addr_ = that.start_addr_;
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_);
177 RegionSnapshot& operator=(RegionSnapshot&& that)
179 region_type_ = that.region_type_;
180 storage_type_ = that.storage_type_;
181 object_info_ = that.object_info_;
182 start_addr_ = that.start_addr_;
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_);
196 region_type_ = UnknownRegion;
197 storage_type_ = StorageType::NoData;
198 privatized_regions_.clear();
199 page_numbers_.clear();
201 object_info_ = nullptr;
202 start_addr_ = nullptr;
204 permanent_addr_ = nullptr;
209 storage_type_ = StorageType::NoData;
211 page_numbers_.clear();
212 privatized_regions_.clear();
215 void flat_data(Buffer data)
217 storage_type_ = StorageType::Flat;
218 flat_data_ = std::move(data);
219 page_numbers_.clear();
220 privatized_regions_.clear();
222 const Buffer& flat_data() const { return flat_data_; }
223 Buffer& flat_data() { return flat_data_; }
225 void page_data(ChunkedData page_data)
227 storage_type_ = StorageType::Chunked;
229 page_numbers_ = std::move(page_data);
230 privatized_regions_.clear();
232 ChunkedData const& page_data() const { return page_numbers_; }
234 void privatized_data(std::vector<RegionSnapshot> data)
236 storage_type_ = StorageType::Privatized;
238 page_numbers_.clear();
239 privatized_regions_ = std::move(data);
241 std::vector<RegionSnapshot> const& privatized_data() const
243 return privatized_regions_;
245 std::vector<RegionSnapshot>& privatized_data()
247 return privatized_regions_;
250 simgrid::mc::ObjectInformation* object_info() const { return object_info_; }
251 void object_info(simgrid::mc::ObjectInformation* info) { object_info_ = info; }
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_; }
262 bool contain(RemotePtr<void> p) const
264 return p >= start() && p < end();
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);
283 typedef class simgrid::mc::RegionSnapshot s_mc_mem_region_t, *mc_mem_region_t;