1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_MC_REGION_SNAPSHOT_HPP
7 #define SIMGRID_MC_REGION_SNAPSHOT_HPP
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"
25 enum class RegionType { Unknown = 0, Heap = 1, Data = 2 };
27 enum class StorageType { NoData = 0, Flat = 1, Chunked = 2, Privatized = 3 };
31 enum class Type { Malloc, Mmap };
32 void* data_ = nullptr;
34 Type type_ = Type::Malloc;
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) {}
41 void clear() noexcept;
42 ~Buffer() noexcept { clear(); }
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); }
48 Buffer(Buffer const& buffer) = delete;
49 Buffer& operator=(Buffer const& buffer) = delete;
52 Buffer(Buffer&& that) noexcept : data_(that.data_), size_(that.size_), type_(that.type_)
56 that.type_ = Type::Malloc;
58 Buffer& operator=(Buffer&& that) noexcept
66 that.type_ = Type::Malloc;
70 void* get() { return data_; }
71 const void* get() const { return data_; }
72 std::size_t size() const { return size_; }
75 /** A copy/snapshot of a given memory region
77 * Different types of region snapshot storage types exist:
79 * * flat/dense snapshots are a simple copy of the region;
81 * * sparse/per-page snapshots are snaapshots which shared
84 * * privatized (SMPI global variable privatization).
86 * This is handled with a variant based approach:
88 * * `storage_type` identified the type of storage;
90 * * an anonymous enum is used to distinguish the relevant types for
93 class RegionSnapshot {
95 static const RegionType UnknownRegion = RegionType::Unknown;
96 static const RegionType HeapRegion = RegionType::Heap;
97 static const RegionType DataRegion = RegionType::Data;
100 RegionType region_type_;
101 StorageType storage_type_;
102 simgrid::mc::ObjectInformation* object_info_;
104 /** @brief Virtual address of the region in the simulated process */
107 /** @brief Size of the data region in bytes */
110 /** @brief Permanent virtual address of the region
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.
119 void* permanent_addr_;
122 ChunkedData page_numbers_;
123 std::vector<RegionSnapshot> privatized_regions_;
127 : region_type_(UnknownRegion)
128 , storage_type_(StorageType::NoData)
129 , object_info_(nullptr)
130 , start_addr_(nullptr)
132 , permanent_addr_(nullptr)
135 RegionSnapshot(RegionType type, void* start_addr, void* permanent_addr, size_t size)
137 , storage_type_(StorageType::NoData)
138 , object_info_(nullptr)
139 , start_addr_(start_addr)
141 , permanent_addr_(permanent_addr)
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_)
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_))
160 RegionSnapshot& operator=(RegionSnapshot&& that)
162 region_type_ = that.region_type_;
163 storage_type_ = that.storage_type_;
164 object_info_ = that.object_info_;
165 start_addr_ = that.start_addr_;
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_);
179 region_type_ = UnknownRegion;
180 storage_type_ = StorageType::NoData;
181 privatized_regions_.clear();
182 page_numbers_.clear();
184 object_info_ = nullptr;
185 start_addr_ = nullptr;
187 permanent_addr_ = nullptr;
192 storage_type_ = StorageType::NoData;
194 page_numbers_.clear();
195 privatized_regions_.clear();
198 void flat_data(Buffer data)
200 storage_type_ = StorageType::Flat;
201 flat_data_ = std::move(data);
202 page_numbers_.clear();
203 privatized_regions_.clear();
205 const Buffer& flat_data() const { return flat_data_; }
206 Buffer& flat_data() { return flat_data_; }
208 void page_data(ChunkedData page_data)
210 storage_type_ = StorageType::Chunked;
212 page_numbers_ = std::move(page_data);
213 privatized_regions_.clear();
215 ChunkedData const& page_data() const { return page_numbers_; }
217 void privatized_data(std::vector<RegionSnapshot> data)
219 storage_type_ = StorageType::Privatized;
221 page_numbers_.clear();
222 privatized_regions_ = std::move(data);
224 std::vector<RegionSnapshot> const& privatized_data() const { return privatized_regions_; }
225 std::vector<RegionSnapshot>& privatized_data() { return privatized_regions_; }
227 simgrid::mc::ObjectInformation* object_info() const { return object_info_; }
228 void object_info(simgrid::mc::ObjectInformation* info) { object_info_ = info; }
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_; }
239 bool contain(RemotePtr<void> p) const { return p >= start() && p < end(); }
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);
248 } // namespace simgrid
250 typedef simgrid::mc::RegionSnapshot s_mc_mem_region_t;
251 typedef s_mc_mem_region_t* mc_mem_region_t;