1 /* Copyright (c) 2007-2015. 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. */
12 #include "src/mc/mc_snapshot.h"
14 #include "src/mc/ChunkedData.hpp"
15 #include "src/mc/RegionSnapshot.hpp"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc,
18 "Logging specific to region snapshots");
24 const char* to_cstr(RegionType region)
27 case RegionType::Unknown:
29 case RegionType::Heap:
31 case RegionType::Data:
38 Buffer::Buffer(std::size_t size, Type type) : size_(size), type_(type)
42 data_ = ::malloc(size_);
45 data_ = ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
46 if (data_ == MAP_FAILED) {
50 throw std::bad_alloc();
58 void Buffer::clear() noexcept
65 if (munmap(data_, size_) != 0)
76 RegionSnapshot dense_region(
77 RegionType region_type,
78 void *start_addr, void* permanent_addr, size_t size)
80 // When KSM support is enables, we allocate memory using mmap:
81 // * we don't want to madvise bits of the heap;
82 // * mmap gives data aligned on page boundaries which is merge friendly.
83 simgrid::mc::Buffer data;
85 data = Buffer::mmap(size);
87 data = Buffer::malloc(size);
89 mc_model_checker->process().read_bytes(data.get(), size,
90 remote(permanent_addr),
91 simgrid::mc::ProcessIndexDisabled);
94 // Mark the region as mergeable *after* we have written into it.
95 // Trying to merge them before is useless/counterproductive.
96 madvise(data.get(), size, MADV_MERGEABLE);
98 simgrid::mc::RegionSnapshot region(
99 region_type, start_addr, permanent_addr, size);
100 region.flat_data(std::move(data));
102 XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu",
103 to_cstr(region_type), region.flat_data().get(), permanent_addr, size);
107 /** @brief Take a snapshot of a given region
110 * @param start_addr Address of the region in the simulated process
111 * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
112 * @param size Size of the data*
114 RegionSnapshot region(
115 RegionType type, void *start_addr, void* permanent_addr, size_t size,
116 RegionSnapshot const* ref_region)
118 if (_sg_mc_sparse_checkpoint)
119 return sparse_region(type, start_addr, permanent_addr, size, ref_region);
121 return dense_region(type, start_addr, permanent_addr, size);
124 RegionSnapshot sparse_region(RegionType region_type,
125 void *start_addr, void* permanent_addr, size_t size,
126 RegionSnapshot const* ref_region)
128 simgrid::mc::Process* process = &mc_model_checker->process();
129 assert(process != nullptr);
131 bool use_soft_dirty = _sg_mc_sparse_checkpoint && _sg_mc_soft_dirty
132 && ref_region != nullptr
133 && ref_region->storage_type() == simgrid::mc::StorageType::Chunked;
135 xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize-1)) == 0,
136 "Not at the beginning of a page");
137 xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize-1)) == 0,
138 "Not at the beginning of a page");
139 size_t page_count = simgrid::mc::mmu::chunkCount(size);
141 std::vector<std::uint64_t> pagemap;
142 const size_t* ref_page_numbers = nullptr;
143 if (use_soft_dirty) {
144 pagemap.resize(page_count);
145 process->read_pagemap(pagemap.data(),
146 simgrid::mc::mmu::split((std::size_t) permanent_addr).first, page_count);
147 ref_page_numbers = ref_region->page_data().pagenos();
150 simgrid::mc::ChunkedData page_data(
151 mc_model_checker->page_store(), *process, permanent_addr, page_count,
153 use_soft_dirty ? pagemap.data() : nullptr);
155 simgrid::mc::RegionSnapshot region(
156 region_type, start_addr, permanent_addr, size);
157 region.page_data(std::move(page_data));