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. */
10 #include "mc_snapshot.h"
11 #include "RegionSnapshot.hpp"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc,
16 "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 void data_deleter::operator()(void* p) const
50 RegionSnapshot dense_region(
51 RegionType region_type,
52 void *start_addr, void* permanent_addr, size_t size)
54 simgrid::mc::RegionSnapshot::flat_data_ptr data;
56 data = simgrid::mc::RegionSnapshot::flat_data_ptr((char*) malloc(size));
58 char* ptr = (char*) mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
59 if (ptr == MAP_FAILED)
60 throw std::bad_alloc();
61 simgrid::mc::data_deleter deleter(
62 simgrid::mc::data_deleter::Munmap, size);
63 data = simgrid::mc::RegionSnapshot::flat_data_ptr(ptr, deleter);
65 mc_model_checker->process().read_bytes(data.get(), size,
66 remote(permanent_addr),
67 simgrid::mc::ProcessIndexDisabled);
69 // Mark the region as mergeable *after* we have written into it.
70 // There no point to let KSM do the hard work before that.
71 madvise(data.get(), size, MADV_MERGEABLE);
73 simgrid::mc::RegionSnapshot region(
74 region_type, start_addr, permanent_addr, size);
75 region.flat_data(std::move(data));
77 XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu",
78 to_cstr(region_type), region.flat_data(), permanent_addr, size);
79 return std::move(region);
82 /** @brief Take a snapshot of a given region
85 * @param start_addr Address of the region in the simulated process
86 * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
87 * @param size Size of the data*
89 RegionSnapshot region(
90 RegionType type, void *start_addr, void* permanent_addr, size_t size)
92 if (_sg_mc_sparse_checkpoint) {
93 return sparse_region(type, start_addr, permanent_addr, size);
95 return dense_region(type, start_addr, permanent_addr, size);
99 RegionSnapshot sparse_region(RegionType region_type,
100 void *start_addr, void* permanent_addr, size_t size)
102 simgrid::mc::Process* process = &mc_model_checker->process();
104 xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize-1)) == 0,
105 "Not at the beginning of a page");
106 xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize-1)) == 0,
107 "Not at the beginning of a page");
108 size_t page_count = mc_page_count(size);
110 simgrid::mc::PerPageCopy page_data(mc_model_checker->page_store(), *process,
111 permanent_addr, page_count);
113 simgrid::mc::RegionSnapshot region(
114 region_type, start_addr, permanent_addr, size);
115 region.page_data(std::move(page_data));
116 return std::move(region);