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"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc,
20 "Logging specific to region snapshots");
28 const char* to_cstr(RegionType region)
31 case RegionType::Unknown:
33 case RegionType::Heap:
35 case RegionType::Data:
42 void data_deleter::operator()(void* p) const
54 RegionSnapshot dense_region(
55 RegionType region_type,
56 void *start_addr, void* permanent_addr, size_t size)
58 simgrid::mc::RegionSnapshot::flat_data_ptr data;
60 data = simgrid::mc::RegionSnapshot::flat_data_ptr((char*) std::malloc(size));
62 char* ptr = (char*) mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
63 if (ptr == MAP_FAILED)
64 throw std::bad_alloc();
65 simgrid::mc::data_deleter deleter(
66 simgrid::mc::data_deleter::Munmap, size);
67 data = simgrid::mc::RegionSnapshot::flat_data_ptr(ptr, deleter);
69 mc_model_checker->process().read_bytes(data.get(), size,
70 remote(permanent_addr),
71 simgrid::mc::ProcessIndexDisabled);
73 // Mark the region as mergeable *after* we have written into it.
74 // There no point to let KSM do the hard work before that.
75 madvise(data.get(), size, MADV_MERGEABLE);
77 simgrid::mc::RegionSnapshot region(
78 region_type, start_addr, permanent_addr, size);
79 region.flat_data(std::move(data));
81 XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu",
82 to_cstr(region_type), region.flat_data(), permanent_addr, size);
83 return std::move(region);
86 /** @brief Take a snapshot of a given region
89 * @param start_addr Address of the region in the simulated process
90 * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
91 * @param size Size of the data*
93 RegionSnapshot region(
94 RegionType type, void *start_addr, void* permanent_addr, size_t size,
95 RegionSnapshot const* ref_region)
97 if (_sg_mc_sparse_checkpoint) {
98 return sparse_region(type, start_addr, permanent_addr, size, ref_region);
100 return dense_region(type, start_addr, permanent_addr, size);
104 RegionSnapshot sparse_region(RegionType region_type,
105 void *start_addr, void* permanent_addr, size_t size,
106 RegionSnapshot const* ref_region)
108 simgrid::mc::Process* process = &mc_model_checker->process();
109 assert(process != nullptr);
111 bool use_soft_dirty = _sg_mc_sparse_checkpoint && _sg_mc_soft_dirty
112 && ref_region != nullptr
113 && ref_region->storage_type() == simgrid::mc::StorageType::Chunked;
115 xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize-1)) == 0,
116 "Not at the beginning of a page");
117 xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize-1)) == 0,
118 "Not at the beginning of a page");
119 size_t page_count = mc_page_count(size);
121 std::vector<std::uint64_t> pagemap;
122 const size_t* ref_page_numbers = nullptr;
123 if (use_soft_dirty) {
124 pagemap.resize(page_count);
125 process->read_pagemap(pagemap.data(),
126 mc_page_number(nullptr, permanent_addr), page_count);
127 ref_page_numbers = ref_region->page_data().pagenos();
130 simgrid::mc::ChunkedData page_data(
131 mc_model_checker->page_store(), *process, permanent_addr, page_count,
133 use_soft_dirty ? pagemap.data() : nullptr);
135 simgrid::mc::RegionSnapshot region(
136 region_type, start_addr, permanent_addr, size);
137 region.page_data(std::move(page_data));
138 return std::move(region);