Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove C typedefs (mc_foobar_t) and mc_forward.h
[simgrid.git] / src / mc / mc_snapshot.h
1 /* Copyright (c) 2007-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef SIMGRID_MC_SNAPSHOT_H
8 #define SIMGRID_MC_SNAPSHOT_H
9
10 #include <cstdint>
11 #include <cstddef>
12
13 #include <vector>
14 #include <set>
15 #include <string>
16 #include <memory>
17
18 #include <sys/types.h> // off_t
19
20 #include <simgrid_config.h>
21 #include "src/xbt/mmalloc/mmprivate.h"
22 #include <xbt/asserts.h>
23 #include <xbt/base.h>
24
25 #include "src/mc/mc_forward.hpp"
26 #include "src/mc/ModelChecker.hpp"
27 #include "src/mc/PageStore.hpp"
28 #include "src/mc/AddressSpace.hpp"
29 #include "src/mc/mc_unw.h"
30 #include "src/mc/RegionSnapshot.hpp"
31
32 SG_BEGIN_DECL()
33
34 // ***** Snapshot region
35
36 XBT_PRIVATE void mc_region_restore_sparse(simgrid::mc::Process* process, mc_mem_region_t reg);
37
38 static inline __attribute__((always_inline))
39 void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
40 {
41   auto split = simgrid::mc::mmu::split(addr - region->start().address());
42   auto pageno = split.first;
43   auto offset = split.second;
44   const void* snapshot_page = region->page_data().page(pageno);
45   return (char*) snapshot_page + offset;
46 }
47
48 static inline __attribute__((always_inline))
49 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
50 {
51   switch (region->storage_type()) {
52   case simgrid::mc::StorageType::NoData:
53   default:
54     xbt_die("Storage type not supported");
55
56   case simgrid::mc::StorageType::Flat:
57     {
58       uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
59       return (void *) ((uintptr_t) region->flat_data().get() + offset);
60     }
61
62   case simgrid::mc::StorageType::Chunked:
63     return mc_translate_address_region_chunked(addr, region);
64
65   case simgrid::mc::StorageType::Privatized:
66     {
67       xbt_assert(process_index >=0,
68         "Missing process index for privatized region");
69       xbt_assert((size_t) process_index < region->privatized_data().size(),
70         "Out of range process index");
71       simgrid::mc::RegionSnapshot& subregion= region->privatized_data()[process_index];
72       return mc_translate_address_region(addr, &subregion, process_index);
73     }
74   }
75 }
76
77 XBT_PRIVATE mc_mem_region_t mc_get_snapshot_region(
78   const void* addr, const simgrid::mc::Snapshot *snapshot, int process_index);
79
80 }
81
82 // ***** MC Snapshot
83
84 /** Ignored data
85  *
86  *  Some parts of the snapshot are ignored by zeroing them out: the real
87  *  values is stored here.
88  * */
89 typedef struct s_mc_snapshot_ignored_data {
90   void* start;
91   std::vector<char> data;
92 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
93
94 typedef struct s_fd_infos{
95   std::string filename;
96   int number;
97   off_t current_position;
98   int flags;
99 }s_fd_infos_t, *fd_infos_t;
100
101 /** Information about a given stack frame
102  *
103  */
104 typedef struct s_mc_stack_frame {
105   /** Instruction pointer */
106   unw_word_t ip;
107   /** Stack pointer */
108   unw_word_t sp;
109   unw_word_t frame_base;
110   simgrid::mc::Frame* frame;
111   std::string frame_name;
112   unw_cursor_t unw_cursor;
113 } s_mc_stack_frame_t, *mc_stack_frame_t;
114
115 typedef struct s_local_variable{
116   simgrid::mc::Frame* subprogram;
117   unsigned long ip;
118   std::string name;
119   simgrid::mc::Type* type;
120   void *address;
121   int region;
122 } s_local_variable_t, *local_variable_t;
123
124 typedef struct XBT_PRIVATE s_mc_snapshot_stack {
125   std::vector<s_local_variable> local_variables;
126   simgrid::mc::UnwindContext context;
127   std::vector<s_mc_stack_frame_t> stack_frames;
128   int process_index;
129 } s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
130
131 typedef struct s_mc_global_t {
132   simgrid::mc::Snapshot* snapshot;
133   int prev_pair;
134   char *prev_req;
135   int initial_communications_pattern_done;
136   int recv_deterministic;
137   int send_deterministic;
138   char *send_diff;
139   char *recv_diff;
140 }s_mc_global_t, *mc_global_t;
141
142 namespace simgrid {
143 namespace mc {
144
145 class XBT_PRIVATE Snapshot final : public AddressSpace {
146 public:
147   Snapshot(Process* process);
148   ~Snapshot();
149   const void* read_bytes(void* buffer, std::size_t size,
150     RemotePtr<void> address, int process_index = ProcessIndexAny,
151     ReadOptions options = ReadOptions::none()) const override;
152 public: // To be private
153   int num_state;
154   std::size_t heap_bytes_used;
155   std::vector<std::unique_ptr<s_mc_mem_region_t>> snapshot_regions;
156   std::set<pid_t> enabled_processes;
157   int privatization_index;
158   std::vector<std::size_t> stack_sizes;
159   std::vector<s_mc_snapshot_stack_t> stacks;
160   std::vector<simgrid::mc::IgnoredHeapRegion> to_ignore;
161   std::uint64_t hash;
162   std::vector<s_mc_snapshot_ignored_data> ignored_data;
163   std::vector<s_fd_infos_t> current_fds;
164 };
165
166 }
167 }
168
169 extern "C" {
170
171 static inline __attribute__ ((always_inline))
172 mc_mem_region_t mc_get_region_hinted(void* addr, simgrid::mc::Snapshot* snapshot, int process_index, mc_mem_region_t region)
173 {
174   if (region->contain(simgrid::mc::remote(addr)))
175     return region;
176   else
177     return mc_get_snapshot_region(addr, snapshot, process_index);
178 }
179
180 static const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot);
181
182 }
183
184 #ifdef __cplusplus
185
186 namespace simgrid {
187 namespace mc {
188
189 XBT_PRIVATE simgrid::mc::Snapshot* take_snapshot(int num_state);
190 XBT_PRIVATE void restore_snapshot(simgrid::mc::Snapshot*);
191
192 }
193 }
194
195 #endif
196
197 extern "C" {
198
199 XBT_PRIVATE void mc_restore_page_snapshot_region(
200   simgrid::mc::Process* process,
201   void* start_addr, simgrid::mc::ChunkedData const& pagenos);
202
203 const void* MC_region_read_fragmented(
204   mc_mem_region_t region, void* target, const void* addr, std::size_t size);
205
206 int MC_snapshot_region_memcmp(
207   const void* addr1, mc_mem_region_t region1,
208   const void* addr2, mc_mem_region_t region2, std::size_t size);
209 XBT_PRIVATE int MC_snapshot_memcmp(
210   const void* addr1, simgrid::mc::Snapshot* snapshot1,
211   const void* addr2, simgrid::mc::Snapshot* snapshot2, int process_index, std::size_t size);
212
213 static inline __attribute__ ((always_inline))
214 const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot)
215 {
216   if(snapshot==NULL)
217       xbt_die("snapshot is NULL");
218   return mc_model_checker->process().get_heap()->breakval;
219 }
220
221 /** @brief Read memory from a snapshot region
222  *
223  *  @param addr    Process (non-snapshot) address of the data
224  *  @param region  Snapshot memory region where the data is located
225  *  @param target  Buffer to store the value
226  *  @param size    Size of the data to read in bytes
227  *  @return Pointer where the data is located (target buffer of original location)
228  */
229 static inline __attribute__((always_inline))
230 const void* MC_region_read(
231   mc_mem_region_t region, void* target, const void* addr, std::size_t size)
232 {
233   xbt_assert(region);
234
235   std::uintptr_t offset =
236     (std::uintptr_t) addr - (std::uintptr_t) region->start().address();
237
238   xbt_assert(region->contain(simgrid::mc::remote(addr)),
239     "Trying to read out of the region boundary.");
240
241   switch (region->storage_type()) {
242   case simgrid::mc::StorageType::NoData:
243   default:
244     xbt_die("Storage type not supported");
245
246   case simgrid::mc::StorageType::Flat:
247     return (char*) region->flat_data().get() + offset;
248
249   case simgrid::mc::StorageType::Chunked:
250     {
251       // Last byte of the region:
252       void* end = (char*) addr + size - 1;
253       if (simgrid::mc::mmu::sameChunk((std::uintptr_t) addr, (std::uintptr_t) end) ) {
254         // The memory is contained in a single page:
255         return mc_translate_address_region_chunked((uintptr_t) addr, region);
256       } else {
257         // The memory spans several pages:
258         return MC_region_read_fragmented(region, target, addr, size);
259       }
260     }
261
262   // We currently do not pass the process_index to this function so we assume
263   // that the privatized region has been resolved in the callers:
264   case simgrid::mc::StorageType::Privatized:
265     xbt_die("Storage type not supported");
266   }
267 }
268
269 static inline __attribute__ ((always_inline))
270 void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
271 {
272   void* res;
273   return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
274 }
275
276 SG_END_DECL()
277
278 #endif