Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mc: move code around in the hope that it will sort itself while doing so
[simgrid.git] / src / mc / sosp / RegionSnapshot.cpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <cstdlib>
7
8 #include <sys/mman.h>
9 #ifdef __FreeBSD__
10 #define MAP_POPULATE MAP_PREFAULT_READ
11 #endif
12
13 #include "src/mc/ModelChecker.hpp"
14 #include "src/mc/mc_config.hpp"
15 #include "src/mc/mc_forward.hpp"
16
17 #include "src/mc/mc_smx.hpp"
18 #include "src/mc/sosp/RegionSnapshot.hpp"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc, "Logging specific to region snapshots");
21
22 namespace simgrid {
23 namespace mc {
24
25 RegionDense::RegionDense(RegionType region_type, void* start_addr, void* permanent_addr, size_t size)
26     : RegionSnapshot(region_type, start_addr, permanent_addr, size)
27 {
28   flat_data_ = Buffer::malloc(size);
29
30   mc_model_checker->process().read_bytes(flat_data_.get(), size, remote(permanent_addr),
31                                          simgrid::mc::ProcessIndexDisabled);
32
33   storage_type_ = StorageType::Flat;
34   page_numbers_.clear();
35   privatized_regions_.clear();
36
37   XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu",
38             (region_type == RegionType::Heap ? "Heap" : (region_type == RegionType::Data ? "Data" : "?")),
39             flat_data().get(), permanent_addr, size);
40 }
41
42 /** @brief Take a snapshot of a given region
43  *
44  * @param type
45  * @param start_addr   Address of the region in the simulated process
46  * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the
47  * privatized mapping)
48  * @param size         Size of the data*
49  */
50 RegionSnapshot* region(RegionType type, void* start_addr, void* permanent_addr, size_t size)
51 {
52   if (_sg_mc_sparse_checkpoint)
53     return new RegionSparse(type, start_addr, permanent_addr, size);
54   else
55     return new RegionDense(type, start_addr, permanent_addr, size);
56 }
57
58 RegionSparse::RegionSparse(RegionType region_type, void* start_addr, void* permanent_addr, size_t size)
59     : RegionSnapshot(region_type, start_addr, permanent_addr, size)
60 {
61   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
62   assert(process != nullptr);
63
64   xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize - 1)) == 0, "Start address not at the beginning of a page");
65   xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize - 1)) == 0,
66              "Permanent address not at the beginning of a page");
67
68   storage_type_ = StorageType::Chunked;
69   flat_data_.clear();
70   privatized_regions_.clear();
71
72   page_numbers_ =
73       ChunkedData(mc_model_checker->page_store(), *process, RemotePtr<void>(permanent_addr), mmu::chunk_count(size));
74 }
75
76 #if HAVE_SMPI
77 RegionPrivatized::RegionPrivatized(RegionType region_type, void* start_addr, void* permanent_addr, std::size_t size)
78     : RegionSnapshot(region_type, start_addr, permanent_addr, size)
79 {
80   storage_type_ = StorageType::Privatized;
81
82   size_t process_count = MC_smpi_process_count();
83
84   // Read smpi_privatization_regions from MCed:
85   smpi_privatization_region_t remote_smpi_privatization_regions;
86   mc_model_checker->process().read_variable("smpi_privatization_regions", &remote_smpi_privatization_regions,
87                                             sizeof(remote_smpi_privatization_regions));
88   s_smpi_privatization_region_t privatization_regions[process_count];
89   mc_model_checker->process().read_bytes(&privatization_regions, sizeof(privatization_regions),
90                                          remote(remote_smpi_privatization_regions));
91
92   privatized_regions_.reserve(process_count);
93   for (size_t i = 0; i < process_count; i++)
94     privatized_regions_.push_back(std::unique_ptr<simgrid::mc::RegionSnapshot>(
95         region(region_type, start_addr, privatization_regions[i].address, size)));
96
97   flat_data_.clear();
98   page_numbers_.clear();
99 }
100 #endif
101
102 /** @brief Restore a region from a snapshot
103  *
104  *  @param region     Target region
105  */
106 void RegionSnapshot::restore()
107 {
108   switch (storage_type()) {
109     case simgrid::mc::StorageType::Flat:
110       mc_model_checker->process().write_bytes(flat_data().get(), size(), permanent_address());
111       break;
112
113     case simgrid::mc::StorageType::Chunked:
114       xbt_assert(((permanent_address().address()) & (xbt_pagesize - 1)) == 0, "Not at the beginning of a page");
115       xbt_assert(simgrid::mc::mmu::chunk_count(size()) == page_data().page_count());
116
117       for (size_t i = 0; i != page_data().page_count(); ++i) {
118         void* target_page = (void*)simgrid::mc::mmu::join(i, (std::uintptr_t)(void*)permanent_address().address());
119         const void* source_page = page_data().page(i);
120         mc_model_checker->process().write_bytes(source_page, xbt_pagesize, remote(target_page));
121       }
122
123       break;
124
125     case simgrid::mc::StorageType::Privatized:
126       for (auto& p : privatized_data())
127         p.get()->restore();
128       break;
129
130     default: // includes StorageType::NoData
131       xbt_die("Storage type not supported");
132       break;
133   }
134 }
135
136 } // namespace mc
137 } // namespace simgrid