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
14 #include "src/mc/mc_config.hpp"
15 #include "src/mc/sosp/mc_snapshot.hpp"
17 #include "src/mc/sosp/ChunkedData.hpp"
18 #include "src/mc/sosp/RegionSnapshot.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc, "Logging specific to region snapshots");
25 static inline const char* to_cstr(RegionType region)
28 case RegionType::Unknown:
30 case RegionType::Heap:
32 case RegionType::Data:
39 Buffer::Buffer(std::size_t size, Type type) : size_(size), type_(type)
43 data_ = ::operator new(size_);
46 data_ = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
47 if (data_ == MAP_FAILED) {
51 throw std::bad_alloc();
59 void Buffer::clear() noexcept
63 ::operator delete(data_);
66 if (munmap(data_, size_) != 0)
77 RegionSnapshot dense_region(RegionType region_type, void* start_addr, void* permanent_addr, size_t size)
79 simgrid::mc::Buffer data = Buffer::malloc(size);
81 mc_model_checker->process().read_bytes(data.get(), size, remote(permanent_addr), simgrid::mc::ProcessIndexDisabled);
83 simgrid::mc::RegionSnapshot region(region_type, start_addr, permanent_addr, size);
84 region.flat_data(std::move(data));
86 XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu", to_cstr(region_type),
87 region.flat_data().get(), permanent_addr, size);
91 /** @brief Take a snapshot of a given region
94 * @param start_addr Address of the region in the simulated process
95 * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the
97 * @param size Size of the data*
99 RegionSnapshot region(RegionType type, void* start_addr, void* permanent_addr, size_t size)
101 if (_sg_mc_sparse_checkpoint)
102 return sparse_region(type, start_addr, permanent_addr, size);
104 return dense_region(type, start_addr, permanent_addr, size);
107 RegionSnapshot sparse_region(RegionType region_type, void* start_addr, void* permanent_addr, size_t size)
109 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
110 assert(process != nullptr);
112 xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize - 1)) == 0, "Not at the beginning of a page");
113 xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize - 1)) == 0, "Not at the beginning of a page");
114 size_t page_count = simgrid::mc::mmu::chunkCount(size);
116 simgrid::mc::ChunkedData page_data(mc_model_checker->page_store(), *process, RemotePtr<void>(permanent_addr),
119 simgrid::mc::RegionSnapshot region(region_type, start_addr, permanent_addr, size);
120 region.page_data(std::move(page_data));
125 } // namespace simgrid