1 /* Copyright (c) 2007-2019. 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. */
10 #define MAP_POPULATE MAP_PREFAULT_READ
13 #include "src/mc/ModelChecker.hpp"
14 #include "src/mc/mc_config.hpp"
15 #include "src/mc/mc_forward.hpp"
17 #include "src/mc/mc_smx.hpp"
18 #include "src/mc/sosp/RegionSnapshot.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc, "Logging specific to region snapshots");
25 RegionDense::RegionDense(RegionType region_type, void* start_addr, void* permanent_addr, size_t size)
26 : RegionSnapshot(region_type, start_addr, permanent_addr, size)
28 flat_data_ = Buffer::malloc(size);
30 mc_model_checker->process().read_bytes(flat_data_.get(), size, remote(permanent_addr));
32 storage_type_ = StorageType::Flat;
33 page_numbers_.clear();
35 XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu",
36 (region_type == RegionType::Heap ? "Heap" : (region_type == RegionType::Data ? "Data" : "?")),
37 flat_data().get(), permanent_addr, size);
40 /** @brief Take a snapshot of a given region
43 * @param start_addr Address of the region in the simulated process
44 * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the
46 * @param size Size of the data*
48 RegionSnapshot* region(RegionType type, void* start_addr, void* permanent_addr, size_t size)
50 if (_sg_mc_sparse_checkpoint)
51 return new RegionSparse(type, start_addr, permanent_addr, size);
53 return new RegionDense(type, start_addr, permanent_addr, size);
56 RegionSparse::RegionSparse(RegionType region_type, void* start_addr, void* permanent_addr, size_t size)
57 : RegionSnapshot(region_type, start_addr, permanent_addr, size)
59 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
60 assert(process != nullptr);
62 xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize - 1)) == 0, "Start address not at the beginning of a page");
63 xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize - 1)) == 0,
64 "Permanent address not at the beginning of a page");
66 storage_type_ = StorageType::Chunked;
70 ChunkedData(mc_model_checker->page_store(), *process, RemotePtr<void>(permanent_addr), mmu::chunk_count(size));
73 /** @brief Restore a region from a snapshot
75 * @param region Target region
77 void RegionSnapshot::restore()
79 switch (storage_type()) {
80 case simgrid::mc::StorageType::Flat:
81 mc_model_checker->process().write_bytes(flat_data().get(), size(), permanent_address());
84 case simgrid::mc::StorageType::Chunked:
85 xbt_assert(((permanent_address().address()) & (xbt_pagesize - 1)) == 0, "Not at the beginning of a page");
86 xbt_assert(simgrid::mc::mmu::chunk_count(size()) == page_data().page_count());
88 for (size_t i = 0; i != page_data().page_count(); ++i) {
89 void* target_page = (void*)simgrid::mc::mmu::join(i, (std::uintptr_t)(void*)permanent_address().address());
90 const void* source_page = page_data().page(i);
91 mc_model_checker->process().write_bytes(source_page, xbt_pagesize, remote(target_page));
96 default: // includes StorageType::NoData
97 xbt_die("Storage type not supported");
103 } // namespace simgrid