Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics: make more fields private
[simgrid.git] / src / mc / sosp / mc_snapshot.hpp
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 #ifndef SIMGRID_MC_SNAPSHOT_HPP
7 #define SIMGRID_MC_SNAPSHOT_HPP
8
9 #include "src/mc/ModelChecker.hpp"
10 #include "src/mc/mc_unw.hpp"
11 #include "src/mc/remote/RemoteClient.hpp"
12 #include "src/mc/sosp/RegionSnapshot.hpp"
13
14 // ***** Snapshot region
15
16 XBT_PRIVATE void mc_region_restore_sparse(simgrid::mc::RemoteClient* process, mc_mem_region_t reg);
17
18 static XBT_ALWAYS_INLINE void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
19 {
20   auto split                = simgrid::mc::mmu::split(addr - region->start().address());
21   auto pageno               = split.first;
22   auto offset               = split.second;
23   const void* snapshot_page = region->page_data().page(pageno);
24   return (char*)snapshot_page + offset;
25 }
26
27 static XBT_ALWAYS_INLINE void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
28 {
29   switch (region->storage_type()) {
30     case simgrid::mc::StorageType::Flat: {
31       uintptr_t offset = (uintptr_t)addr - (uintptr_t)region->start().address();
32       return (void*)((uintptr_t)region->flat_data().get() + offset);
33     }
34     case simgrid::mc::StorageType::Chunked:
35       return mc_translate_address_region_chunked(addr, region);
36     case simgrid::mc::StorageType::Privatized: {
37       xbt_assert(process_index >= 0, "Missing process index for privatized region");
38       xbt_assert((size_t)process_index < region->privatized_data().size(), "Out of range process index");
39       simgrid::mc::RegionSnapshot& subregion = region->privatized_data()[process_index];
40       return mc_translate_address_region(addr, &subregion, process_index);
41     }
42     default: // includes StorageType::NoData
43       xbt_die("Storage type not supported");
44   }
45 }
46
47 XBT_PRIVATE mc_mem_region_t mc_get_snapshot_region(const void* addr, const simgrid::mc::Snapshot* snapshot,
48                                                    int process_index);
49
50 // ***** MC Snapshot
51
52 /** Ignored data
53  *
54  *  Some parts of the snapshot are ignored by zeroing them out: the real
55  *  values is stored here.
56  * */
57 struct s_mc_snapshot_ignored_data_t {
58   void* start;
59   std::vector<char> data;
60 };
61
62 /** Information about a given stack frame */
63 struct s_mc_stack_frame_t {
64   /** Instruction pointer */
65   unw_word_t ip;
66   /** Stack pointer */
67   unw_word_t sp;
68   unw_word_t frame_base;
69   simgrid::mc::Frame* frame;
70   std::string frame_name;
71   unw_cursor_t unw_cursor;
72 };
73 typedef s_mc_stack_frame_t* mc_stack_frame_t;
74
75 struct s_local_variable_t {
76   simgrid::mc::Frame* subprogram;
77   unsigned long ip;
78   std::string name;
79   simgrid::mc::Type* type;
80   void* address;
81   int region;
82 };
83 typedef s_local_variable_t* local_variable_t;
84
85 struct XBT_PRIVATE s_mc_snapshot_stack_t {
86   std::vector<s_local_variable_t> local_variables;
87   simgrid::mc::UnwindContext context;
88   std::vector<s_mc_stack_frame_t> stack_frames;
89   int process_index;
90 };
91 typedef s_mc_snapshot_stack_t* mc_snapshot_stack_t;
92
93 namespace simgrid {
94 namespace mc {
95
96 class XBT_PRIVATE Snapshot final : public AddressSpace {
97 public:
98   Snapshot(RemoteClient* process, int num_state);
99   ~Snapshot() = default;
100   const void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, int process_index = ProcessIndexAny,
101                          ReadOptions options = ReadOptions::none()) const override;
102
103   // To be private
104   int num_state;
105   std::size_t heap_bytes_used;
106   std::vector<std::unique_ptr<s_mc_mem_region_t>> snapshot_regions;
107   std::set<pid_t> enabled_processes;
108   int privatization_index;
109   std::vector<std::size_t> stack_sizes;
110   std::vector<s_mc_snapshot_stack_t> stacks;
111   std::vector<simgrid::mc::IgnoredHeapRegion> to_ignore;
112   std::uint64_t hash;
113   std::vector<s_mc_snapshot_ignored_data_t> ignored_data;
114 };
115 } // namespace mc
116 } // namespace simgrid
117
118 static XBT_ALWAYS_INLINE mc_mem_region_t mc_get_region_hinted(void* addr, simgrid::mc::Snapshot* snapshot,
119                                                               int process_index, mc_mem_region_t region)
120 {
121   if (region->contain(simgrid::mc::remote(addr)))
122     return region;
123   else
124     return mc_get_snapshot_region(addr, snapshot, process_index);
125 }
126
127 static const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot);
128
129 namespace simgrid {
130 namespace mc {
131
132 XBT_PRIVATE std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state);
133 XBT_PRIVATE void restore_snapshot(std::shared_ptr<simgrid::mc::Snapshot> snapshot);
134 } // namespace mc
135 } // namespace simgrid
136
137 XBT_PRIVATE void mc_restore_page_snapshot_region(simgrid::mc::RemoteClient* process, void* start_addr,
138                                                  simgrid::mc::ChunkedData const& pagenos);
139
140 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, std::size_t size);
141
142 int MC_snapshot_region_memcmp(const void* addr1, mc_mem_region_t region1, const void* addr2, mc_mem_region_t region2,
143                               std::size_t size);
144
145 static XBT_ALWAYS_INLINE const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot)
146 {
147   if (snapshot == nullptr)
148     xbt_die("snapshot is nullptr");
149   return mc_model_checker->process().get_heap()->breakval;
150 }
151
152 /** @brief Read memory from a snapshot region
153  *
154  *  @param addr    Process (non-snapshot) address of the data
155  *  @param region  Snapshot memory region where the data is located
156  *  @param target  Buffer to store the value
157  *  @param size    Size of the data to read in bytes
158  *  @return Pointer where the data is located (target buffer of original location)
159  */
160 static XBT_ALWAYS_INLINE const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr,
161                                                     std::size_t size)
162 {
163   xbt_assert(region);
164
165   std::uintptr_t offset = (std::uintptr_t)addr - (std::uintptr_t)region->start().address();
166
167   xbt_assert(region->contain(simgrid::mc::remote(addr)), "Trying to read out of the region boundary.");
168
169   switch (region->storage_type()) {
170     case simgrid::mc::StorageType::Flat:
171       return (char*)region->flat_data().get() + offset;
172
173     case simgrid::mc::StorageType::Chunked: {
174       // Last byte of the region:
175       void* end = (char*)addr + size - 1;
176       if (simgrid::mc::mmu::sameChunk((std::uintptr_t)addr, (std::uintptr_t)end)) {
177         // The memory is contained in a single page:
178         return mc_translate_address_region_chunked((uintptr_t)addr, region);
179       }
180       // Otherwise, the memory spans several pages:
181       return MC_region_read_fragmented(region, target, addr, size);
182     }
183
184     default:
185       // includes StorageType::NoData and StorageType::Privatized (we currently do not pass the process_index to this
186       // function so we assume that the privatized region has been resolved in the callers)
187       xbt_die("Storage type not supported");
188   }
189 }
190
191 static XBT_ALWAYS_INLINE void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
192 {
193   void* res;
194   return *(void**)MC_region_read(region, &res, addr, sizeof(void*));
195 }
196
197 #endif