1 /* Copyright (c) 2007-2017. 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. */
11 # define MAP_POPULATE MAP_PREFAULT_READ
15 #include "src/mc/mc_snapshot.hpp"
17 #include "src/mc/ChunkedData.hpp"
18 #include "src/mc/RegionSnapshot.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc,
21 "Logging specific to region snapshots");
27 const char* to_cstr(RegionType region)
30 case RegionType::Unknown:
32 case RegionType::Heap:
34 case RegionType::Data:
41 Buffer::Buffer(std::size_t size, Type type) : size_(size), type_(type)
45 data_ = ::operator new(size_);
48 data_ = ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
49 if (data_ == MAP_FAILED) {
53 throw std::bad_alloc();
61 void Buffer::clear() noexcept
65 ::operator delete(data_);
68 if (munmap(data_, size_) != 0)
79 RegionSnapshot dense_region(
80 RegionType region_type,
81 void *start_addr, void* permanent_addr, size_t size)
83 // When KSM support is enables, we allocate memory using mmap:
84 // * we don't want to advise bits of the heap as mergable
85 // * mmap gives data aligned on page boundaries which is merge friendly
86 simgrid::mc::Buffer data;
88 data = Buffer::mmap(size);
90 data = Buffer::malloc(size);
92 mc_model_checker->process().read_bytes(data.get(), size,
93 remote(permanent_addr),
94 simgrid::mc::ProcessIndexDisabled);
98 // Mark the region as mergeable *after* we have written into it.
99 // Trying to merge them before is useless/counterproductive.
100 madvise(data.get(), size, MADV_MERGEABLE);
103 simgrid::mc::RegionSnapshot region(
104 region_type, start_addr, permanent_addr, size);
105 region.flat_data(std::move(data));
107 XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu",
108 to_cstr(region_type), region.flat_data().get(), permanent_addr, size);
112 /** @brief Take a snapshot of a given region
115 * @param start_addr Address of the region in the simulated process
116 * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
117 * @param size Size of the data*
119 RegionSnapshot region(
120 RegionType type, void *start_addr, void* permanent_addr, size_t size)
122 if (_sg_mc_sparse_checkpoint)
123 return sparse_region(type, start_addr, permanent_addr, size);
125 return dense_region(type, start_addr, permanent_addr, size);
128 RegionSnapshot sparse_region(RegionType region_type,
129 void *start_addr, void* permanent_addr, size_t size)
131 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
132 assert(process != nullptr);
134 xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize-1)) == 0,
135 "Not at the beginning of a page");
136 xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize-1)) == 0,
137 "Not at the beginning of a page");
138 size_t page_count = simgrid::mc::mmu::chunkCount(size);
140 simgrid::mc::ChunkedData page_data(mc_model_checker->page_store(), *process, RemotePtr<void>(permanent_addr),
143 simgrid::mc::RegionSnapshot region(
144 region_type, start_addr, permanent_addr, size);
145 region.page_data(std::move(page_data));