Logo AND Algorithmique Numérique Distribuée

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