1 /* Copyright (c) 2007-2014. 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. */
10 #include <sys/types.h> // off_t
11 #include <stdint.h> // size_t
13 #include <simgrid_config.h>
14 #include "../xbt/mmalloc/mmprivate.h"
15 #include <xbt/asserts.h>
16 #include <xbt/dynar.h>
18 #include "mc_forward.h"
19 #include "ModelChecker.hpp"
20 #include "PageStore.hpp"
21 #include "mc_mmalloc.h"
22 #include "mc/AddressSpace.hpp"
27 // ***** Snapshot region
29 typedef enum e_mc_region_type_t {
30 MC_REGION_TYPE_UNKNOWN = 0,
31 MC_REGION_TYPE_HEAP = 1,
32 MC_REGION_TYPE_DATA = 2
35 // TODO, use OO instead of this
36 typedef enum e_mc_region_storage_type_t {
37 MC_REGION_STORAGE_TYPE_NONE = 0,
38 MC_REGION_STORAGE_TYPE_FLAT = 1,
39 MC_REGION_STORAGE_TYPE_CHUNKED = 2,
40 MC_REGION_STORAGE_TYPE_PRIVATIZED = 3
41 } mc_region_storage_type_t;
48 std::vector<std::size_t> pagenos_;
50 PerPageCopy() : store_(nullptr) {}
51 PerPageCopy(PerPageCopy const& that)
54 pagenos_ = that.pagenos_;
55 for (std::size_t pageno : pagenos_)
56 store_->ref_page(pageno);
60 for (std::size_t pageno : pagenos_)
61 store_->unref_page(pageno);
68 PerPageCopy(PerPageCopy&& that)
71 that.store_ = nullptr;
72 pagenos_ = std::move(that.pagenos_);
73 that.pagenos_.clear();
75 PerPageCopy& operator=(PerPageCopy const& that)
79 pagenos_ = that.pagenos_;
80 for (std::size_t pageno : pagenos_)
81 store_->ref_page(pageno);
84 PerPageCopy& operator=(PerPageCopy && that)
88 that.store_ = nullptr;
89 pagenos_ = std::move(that.pagenos_);
90 that.pagenos_.clear();
94 std::size_t page_count() const
96 return pagenos_.size();
99 std::size_t pageno(std::size_t i) const
104 const void* page(std::size_t i) const
106 return store_->get_page(pagenos_[i]);
109 PerPageCopy(PageStore& store, AddressSpace& as,
110 remote_ptr<void> addr, std::size_t page_count);
113 /** @brief Copy/snapshot of a given memory region
115 * Different types of region snapshot storage types exist:
117 * <li>flat/dense snapshots are a simple copy of the region;</li>
118 * <li>sparse/per-page snapshots are snaapshots which shared
119 * identical pages.</li>
120 * <li>privatized (SMPI global variable privatisation).
123 * This is handled with a variant based approch:
125 * * `storage_type` identified the type of storage;
126 * * an anonymous enum is used to distinguish the relevant types for
129 class RegionSnapshot {
131 mc_region_type_t region_type_;
132 mc_region_storage_type_t storage_type_;
133 mc_object_info_t object_info_;
135 /** @brief Virtual address of the region in the simulated process */
138 /** @brief Size of the data region in bytes */
141 /** @brief Permanent virtual address of the region
143 * This is usually the same address as the simuilated process address.
144 * However, when using SMPI privatization of global variables,
145 * each SMPI process has its own set of global variables stored
146 * at a different virtual address. The scheduler maps those region
147 * on the region of the global variables.
150 void *permanent_addr_;
152 std::vector<char> flat_data_;
153 PerPageCopy page_numbers_;
154 std::vector<std::unique_ptr<RegionSnapshot>> privatized_regions_;
157 region_type_(MC_REGION_TYPE_UNKNOWN),
158 storage_type_(MC_REGION_STORAGE_TYPE_NONE),
159 object_info_(nullptr),
160 start_addr_(nullptr),
162 permanent_addr_(nullptr)
164 RegionSnapshot(mc_region_type_t type, void *start_addr, void* permanent_addr, size_t size) :
166 storage_type_(MC_REGION_STORAGE_TYPE_NONE),
167 object_info_(nullptr),
168 start_addr_(start_addr),
170 permanent_addr_(permanent_addr)
173 RegionSnapshot(RegionSnapshot const&) = delete;
174 RegionSnapshot& operator=(RegionSnapshot const&) = delete;
180 storage_type_ = MC_REGION_STORAGE_TYPE_NONE;
182 page_numbers_.clear();
183 privatized_regions_.clear();
186 void flat_data(std::vector<char> data)
188 storage_type_ = MC_REGION_STORAGE_TYPE_FLAT;
189 flat_data_ = std::move(data);
190 page_numbers_.clear();
191 privatized_regions_.clear();
193 std::vector<char> const& flat_data() const { return flat_data_; }
195 void page_data(PerPageCopy page_data)
197 storage_type_ = MC_REGION_STORAGE_TYPE_CHUNKED;
199 page_numbers_ = std::move(page_data);
200 privatized_regions_.clear();
202 PerPageCopy const& page_data() const { return page_numbers_; }
204 void privatized_data(std::vector<std::unique_ptr<RegionSnapshot>> data)
206 storage_type_ = MC_REGION_STORAGE_TYPE_PRIVATIZED;
208 page_numbers_.clear();
209 privatized_regions_ = std::move(data);
211 std::vector<std::unique_ptr<RegionSnapshot>> const& privatized_data() const
213 return privatized_regions_;
216 mc_object_info_t object_info() const { return object_info_; }
217 void object_info(mc_object_info_t info) { object_info_ = info; }
221 remote_ptr<void> start() const { return remote(start_addr_); }
222 remote_ptr<void> end() const { return remote((char*)start_addr_ + size_); }
223 remote_ptr<void> permanent_address() const { return remote(permanent_addr_); }
224 std::size_t size() const { return size_; }
225 mc_region_storage_type_t storage_type() const { return storage_type_; }
226 mc_region_type_t region_type() const { return region_type_; }
228 bool contain(remote_ptr<void> p) const
230 return p >= start() && p < end();
237 typedef class simgrid::mc::RegionSnapshot s_mc_mem_region_t, *mc_mem_region_t;
239 MC_SHOULD_BE_INTERNAL mc_mem_region_t mc_region_new_sparse(
240 mc_region_type_t type, void *start_addr, void* data_addr, size_t size);
241 XBT_INTERNAL void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t reg);
243 static inline __attribute__ ((always_inline))
244 bool mc_region_contain(mc_mem_region_t region, const void* p)
246 return region->contain(simgrid::mc::remote(p));
249 static inline __attribute__((always_inline))
250 void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
252 size_t pageno = mc_page_number((void*)region->start().address(), (void*) addr);
253 const void* snapshot_page =
254 region->page_data().page(pageno);
255 return (char*) snapshot_page + mc_page_offset((void*) addr);
258 static inline __attribute__((always_inline))
259 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
261 switch (region->storage_type()) {
262 case MC_REGION_STORAGE_TYPE_NONE:
264 xbt_die("Storage type not supported");
266 case MC_REGION_STORAGE_TYPE_FLAT:
268 uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
269 return (void *) ((uintptr_t) region->flat_data().data() + offset);
272 case MC_REGION_STORAGE_TYPE_CHUNKED:
273 return mc_translate_address_region_chunked(addr, region);
275 case MC_REGION_STORAGE_TYPE_PRIVATIZED:
277 xbt_assert(process_index >=0,
278 "Missing process index for privatized region");
279 xbt_assert((size_t) process_index < region->privatized_data().size(),
280 "Out of range process index");
281 mc_mem_region_t subregion = region->privatized_data()[process_index].get();
282 xbt_assert(subregion, "Missing memory region for process %i", process_index);
283 return mc_translate_address_region(addr, subregion, process_index);
288 XBT_INTERNAL mc_mem_region_t mc_get_snapshot_region(
289 const void* addr, const s_mc_snapshot_t *snapshot, int process_index);
295 * Some parts of the snapshot are ignored by zeroing them out: the real
296 * values is stored here.
298 typedef struct s_mc_snapshot_ignored_data {
302 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
304 typedef struct s_fd_infos{
307 off_t current_position;
309 }s_fd_infos_t, *fd_infos_t;
316 class Snapshot : public AddressSpace {
320 const void* read_bytes(void* buffer, std::size_t size,
321 remote_ptr<void> address, int process_index = ProcessIndexAny,
322 ReadMode mode = Normal) const MC_OVERRIDE;
323 public: // To be private
324 mc_process_t process;
326 size_t heap_bytes_used;
327 mc_mem_region_t* snapshot_regions;
328 size_t snapshot_regions_count;
329 xbt_dynar_t enabled_processes;
330 int privatization_index;
333 xbt_dynar_t to_ignore;
335 xbt_dynar_t ignored_data;
337 fd_infos_t *current_fd;
345 static inline __attribute__ ((always_inline))
346 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
348 if (mc_region_contain(region, addr))
351 return mc_get_snapshot_region(addr, snapshot, process_index);
354 /** Information about a given stack frame
357 typedef struct s_mc_stack_frame {
358 /** Instruction pointer */
362 unw_word_t frame_base;
365 unw_cursor_t unw_cursor;
366 } s_mc_stack_frame_t, *mc_stack_frame_t;
368 typedef struct s_mc_snapshot_stack{
369 xbt_dynar_t local_variables;
370 mc_unw_context_t context;
371 xbt_dynar_t stack_frames; // mc_stack_frame_t
373 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
375 typedef struct s_mc_global_t {
376 mc_snapshot_t snapshot;
379 int initial_communications_pattern_done;
380 int recv_deterministic;
381 int send_deterministic;
384 }s_mc_global_t, *mc_global_t;
386 static const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
388 XBT_INTERNAL mc_snapshot_t MC_take_snapshot(int num_state);
389 XBT_INTERNAL void MC_restore_snapshot(mc_snapshot_t);
391 XBT_INTERNAL void mc_restore_page_snapshot_region(
392 mc_process_t process,
393 void* start_addr, simgrid::mc::PerPageCopy const& pagenos);
395 MC_SHOULD_BE_INTERNAL const void* MC_region_read_fragmented(
396 mc_mem_region_t region, void* target, const void* addr, size_t size);
398 MC_SHOULD_BE_INTERNAL int MC_snapshot_region_memcmp(
399 const void* addr1, mc_mem_region_t region1,
400 const void* addr2, mc_mem_region_t region2, size_t size);
401 XBT_INTERNAL int MC_snapshot_memcmp(
402 const void* addr1, mc_snapshot_t snapshot1,
403 const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
405 static inline __attribute__ ((always_inline))
406 const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot)
409 xbt_die("snapshot is NULL");
410 return mc_model_checker->process().get_heap()->breakval;
413 /** @brief Read memory from a snapshot region
415 * @param addr Process (non-snapshot) address of the data
416 * @param region Snapshot memory region where the data is located
417 * @param target Buffer to store the value
418 * @param size Size of the data to read in bytes
419 * @return Pointer where the data is located (target buffer of original location)
421 static inline __attribute__((always_inline))
422 const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr, size_t size)
426 uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
428 xbt_assert(mc_region_contain(region, addr),
429 "Trying to read out of the region boundary.");
431 switch (region->storage_type()) {
432 case MC_REGION_STORAGE_TYPE_NONE:
434 xbt_die("Storage type not supported");
436 case MC_REGION_STORAGE_TYPE_FLAT:
437 return (char*) region->flat_data().data() + offset;
439 case MC_REGION_STORAGE_TYPE_CHUNKED:
441 // Last byte of the region:
442 void* end = (char*) addr + size - 1;
443 if (mc_same_page(addr, end) ) {
444 // The memory is contained in a single page:
445 return mc_translate_address_region_chunked((uintptr_t) addr, region);
447 // The memory spans several pages:
448 return MC_region_read_fragmented(region, target, addr, size);
452 // We currently do not pass the process_index to this function so we assume
453 // that the privatized region has been resolved in the callers:
454 case MC_REGION_STORAGE_TYPE_PRIVATIZED:
455 xbt_die("Storage type not supported");
459 static inline __attribute__ ((always_inline))
460 void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
463 return *(void**) MC_region_read(region, &res, addr, sizeof(void*));