Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Plug a memleak
[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 "mc/mc.h"
14 #include "src/mc/mc_config.hpp"
15 #include "src/mc/sosp/mc_snapshot.hpp"
16
17 #include "src/mc/mc_smx.hpp"
18 #include "src/mc/sosp/ChunkedData.hpp"
19 #include "src/mc/sosp/RegionSnapshot.hpp"
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc, "Logging specific to region snapshots");
22
23 namespace simgrid {
24 namespace mc {
25
26 RegionDense::RegionDense(RegionType region_type, void* start_addr, void* permanent_addr, size_t size)
27     : RegionSnapshot(region_type, start_addr, permanent_addr, size)
28 {
29   flat_data_ = Buffer::malloc(size);
30
31   mc_model_checker->process().read_bytes(flat_data_.get(), size, remote(permanent_addr),
32                                          simgrid::mc::ProcessIndexDisabled);
33
34   storage_type_ = StorageType::Flat;
35   page_numbers_.clear();
36   privatized_regions_.clear();
37
38   XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu",
39             (region_type == RegionType::Heap ? "Heap" : (region_type == RegionType::Data ? "Data" : "?")),
40             flat_data().get(), permanent_addr, size);
41 }
42
43 /** @brief Take a snapshot of a given region
44  *
45  * @param type
46  * @param start_addr   Address of the region in the simulated process
47  * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the
48  * privatized mapping)
49  * @param size         Size of the data*
50  */
51 RegionSnapshot* region(RegionType type, void* start_addr, void* permanent_addr, size_t size)
52 {
53   if (_sg_mc_sparse_checkpoint)
54     return new RegionSparse(type, start_addr, permanent_addr, size);
55   else
56     return new RegionDense(type, start_addr, permanent_addr, size);
57 }
58
59 RegionSparse::RegionSparse(RegionType region_type, void* start_addr, void* permanent_addr, size_t size)
60     : RegionSnapshot(region_type, start_addr, permanent_addr, size)
61 {
62   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
63   assert(process != nullptr);
64
65   xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize - 1)) == 0, "Start address not at the beginning of a page");
66   xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize - 1)) == 0,
67              "Permanent address not at the beginning of a page");
68
69   storage_type_ = StorageType::Chunked;
70   flat_data_.clear();
71   privatized_regions_.clear();
72
73   page_numbers_ =
74       ChunkedData(mc_model_checker->page_store(), *process, RemotePtr<void>(permanent_addr), mmu::chunk_count(size));
75 }
76
77 #if HAVE_SMPI
78 RegionPrivatized::RegionPrivatized(RegionType region_type, void* start_addr, void* permanent_addr, std::size_t size)
79     : RegionSnapshot(region_type, start_addr, permanent_addr, size)
80 {
81   storage_type_ = StorageType::Privatized;
82
83   size_t process_count = MC_smpi_process_count();
84
85   // Read smpi_privatization_regions from MCed:
86   smpi_privatization_region_t remote_smpi_privatization_regions;
87   mc_model_checker->process().read_variable("smpi_privatization_regions", &remote_smpi_privatization_regions,
88                                             sizeof(remote_smpi_privatization_regions));
89   s_smpi_privatization_region_t privatization_regions[process_count];
90   mc_model_checker->process().read_bytes(&privatization_regions, sizeof(privatization_regions),
91                                          remote(remote_smpi_privatization_regions));
92
93   privatized_regions_.reserve(process_count);
94   for (size_t i = 0; i < process_count; i++)
95     privatized_regions_.push_back(std::unique_ptr<simgrid::mc::RegionSnapshot>(
96         region(region_type, start_addr, privatization_regions[i].address, size)));
97
98   flat_data_.clear();
99   page_numbers_.clear();
100 }
101 #endif
102
103 } // namespace mc
104 } // namespace simgrid