Logo AND Algorithmique Numérique Distribuée

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