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 enum class StorageType {
45 void* data_ = nullptr;
47 Type type_ = Type::Malloc;
49 Buffer(std::size_t size, Type type = Type::Malloc);
50 Buffer(void* data, std::size_t size, Type type = Type::Malloc) :
51 data_(data), size_(size), type_(type) {}
54 void clear() noexcept;
55 ~Buffer() noexcept { clear(); }
57 static Buffer malloc(std::size_t size)
59 return Buffer(size, Type::Malloc);
61 static Buffer mmap(std::size_t size)
63 return Buffer(size, Type::Mmap);
67 Buffer(Buffer const& buffer) = delete;
68 Buffer& operator=(Buffer const& buffer) = delete;
71 Buffer(Buffer&& that) noexcept
72 : data_(that.data_), size_(that.size_), type_(that.type_)
76 that.type_ = Type::Malloc;
78 Buffer& operator=(Buffer&& that) noexcept
86 that.type_ = Type::Malloc;
90 void* get() { return data_; }
91 const void* get() const { return data_; }
92 std::size_t size() const { return size_; }
95 /** A copy/snapshot of a given memory region
97 * Different types of region snapshot storage types exist:
99 * * flat/dense snapshots are a simple copy of the region;
101 * * sparse/per-page snapshots are snaapshots which shared
104 * * privatized (SMPI global variable privatisation).
106 * This is handled with a variant based approach:
108 * * `storage_type` identified the type of storage;
110 * * an anonymous enum is used to distinguish the relevant types for
113 class RegionSnapshot {
115 static const RegionType UnknownRegion = RegionType::Unknown;
116 static const RegionType HeapRegion = RegionType::Heap;
117 static const RegionType DataRegion = RegionType::Data;
119 RegionType region_type_;
120 StorageType storage_type_;
121 simgrid::mc::ObjectInformation* object_info_;
123 /** @brief Virtual address of the region in the simulated process */
126 /** @brief Size of the data region in bytes */
129 /** @brief Permanent virtual address of the region
131 * This is usually the same address as the simuilated process address.
132 * However, when using SMPI privatization of global variables,
133 * each SMPI process has its own set of global variables stored
134 * at a different virtual address. The scheduler maps those region
135 * on the region of the global variables.
138 void *permanent_addr_;
141 ChunkedData page_numbers_;
142 std::vector<RegionSnapshot> privatized_regions_;
145 region_type_(UnknownRegion),
146 storage_type_(StorageType::NoData),
147 object_info_(nullptr),
148 start_addr_(nullptr),
150 permanent_addr_(nullptr)
152 RegionSnapshot(RegionType type, void *start_addr, void* permanent_addr, size_t size) :
154 storage_type_(StorageType::NoData),
155 object_info_(nullptr),
156 start_addr_(start_addr),
158 permanent_addr_(permanent_addr)
161 RegionSnapshot(RegionSnapshot const&) = default;
162 RegionSnapshot& operator=(RegionSnapshot const&) = default;
163 RegionSnapshot(RegionSnapshot&& that)
165 region_type_ = that.region_type_;
166 storage_type_ = that.storage_type_;
167 object_info_ = that.object_info_;
168 start_addr_ = that.start_addr_;
170 permanent_addr_ = that.permanent_addr_;
171 flat_data_ = std::move(that.flat_data_);
172 page_numbers_ = std::move(that.page_numbers_);
173 privatized_regions_ = std::move(that.privatized_regions_);
176 RegionSnapshot& operator=(RegionSnapshot&& that)
178 region_type_ = that.region_type_;
179 storage_type_ = that.storage_type_;
180 object_info_ = that.object_info_;
181 start_addr_ = that.start_addr_;
183 permanent_addr_ = that.permanent_addr_;
184 flat_data_ = std::move(that.flat_data_);
185 page_numbers_ = std::move(that.page_numbers_);
186 privatized_regions_ = std::move(that.privatized_regions_);
195 region_type_ = UnknownRegion;
196 storage_type_ = StorageType::NoData;
197 privatized_regions_.clear();
198 page_numbers_.clear();
200 object_info_ = nullptr;
201 start_addr_ = nullptr;
203 permanent_addr_ = nullptr;
208 storage_type_ = StorageType::NoData;
210 page_numbers_.clear();
211 privatized_regions_.clear();
214 void flat_data(Buffer data)
216 storage_type_ = StorageType::Flat;
217 flat_data_ = std::move(data);
218 page_numbers_.clear();
219 privatized_regions_.clear();
221 const Buffer& flat_data() const { return flat_data_; }
222 Buffer& flat_data() { return flat_data_; }
224 void page_data(ChunkedData page_data)
226 storage_type_ = StorageType::Chunked;
228 page_numbers_ = std::move(page_data);
229 privatized_regions_.clear();
231 ChunkedData const& page_data() const { return page_numbers_; }
233 void privatized_data(std::vector<RegionSnapshot> data)
235 storage_type_ = StorageType::Privatized;
237 page_numbers_.clear();
238 privatized_regions_ = std::move(data);
240 std::vector<RegionSnapshot> const& privatized_data() const
242 return privatized_regions_;
244 std::vector<RegionSnapshot>& privatized_data()
246 return privatized_regions_;
249 simgrid::mc::ObjectInformation* object_info() const { return object_info_; }
250 void object_info(simgrid::mc::ObjectInformation* info) { object_info_ = info; }
254 RemotePtr<void> start() const { return remote(start_addr_); }
255 RemotePtr<void> end() const { return remote((char*)start_addr_ + size_); }
256 RemotePtr<void> permanent_address() const { return remote(permanent_addr_); }
257 std::size_t size() const { return size_; }
258 StorageType storage_type() const { return storage_type_; }
259 RegionType region_type() const { return region_type_; }
261 bool contain(RemotePtr<void> p) const
263 return p >= start() && p < end();
267 RegionSnapshot privatized_region(
268 RegionType region_type, void *start_addr, void* permanent_addr,
270 RegionSnapshot dense_region(
271 RegionType type, void *start_addr, void* data_addr, std::size_t size);
272 simgrid::mc::RegionSnapshot sparse_region(
273 RegionType type, void *start_addr, void* data_addr, std::size_t size);
274 simgrid::mc::RegionSnapshot region(
275 RegionType type, void *start_addr, void* data_addr, std::size_t size);
280 typedef class simgrid::mc::RegionSnapshot s_mc_mem_region_t, *mc_mem_region_t;