Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4467b9fff5ea77e2ffd0f24de3386942fb35d8b0
[simgrid.git] / src / mc / mc_snapshot.h
1 /* Copyright (c) 2007-2014. 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 MC_SNAPSHOT_H
8 #define MC_SNAPSHOT_H
9
10 #include <sys/types.h> // off_t
11 #include <stdint.h> // size_t
12
13 #include <vector>
14 #include <set>
15 #include <memory>
16
17 #include <simgrid_config.h>
18 #include "../xbt/mmalloc/mmprivate.h"
19 #include <xbt/asserts.h>
20 #include <xbt/dynar.h>
21
22 #include "mc_forward.h"
23 #include "ModelChecker.hpp"
24 #include "PageStore.hpp"
25 #include "mc_mmalloc.h"
26 #include "mc/AddressSpace.hpp"
27 #include "mc_unw.h"
28 #include "RegionSnapshot.hpp"
29
30 SG_BEGIN_DECL()
31
32 // ***** Snapshot region
33
34 XBT_INTERNAL void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t reg);
35
36 static inline __attribute__((always_inline))
37 void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
38 {
39   size_t pageno = mc_page_number((void*)region->start().address(), (void*) addr);
40   const void* snapshot_page =
41     region->page_data().page(pageno);
42   return (char*) snapshot_page + mc_page_offset((void*) addr);
43 }
44
45 static inline __attribute__((always_inline))
46 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
47 {
48   switch (region->storage_type()) {
49   case simgrid::mc::StorageType::NoData:
50   default:
51     xbt_die("Storage type not supported");
52
53   case simgrid::mc::StorageType::Flat:
54     {
55       uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
56       return (void *) ((uintptr_t) region->flat_data().data() + offset);
57     }
58
59   case simgrid::mc::StorageType::Chunked:
60     return mc_translate_address_region_chunked(addr, region);
61
62   case simgrid::mc::StorageType::Privatized:
63     {
64       xbt_assert(process_index >=0,
65         "Missing process index for privatized region");
66       xbt_assert((size_t) process_index < region->privatized_data().size(),
67         "Out of range process index");
68       simgrid::mc::RegionSnapshot& subregion= region->privatized_data()[process_index];
69       return mc_translate_address_region(addr, &subregion, process_index);
70     }
71   }
72 }
73
74 XBT_INTERNAL mc_mem_region_t mc_get_snapshot_region(
75   const void* addr, const s_mc_snapshot_t *snapshot, int process_index);
76
77 }
78
79 // ***** MC Snapshot
80
81 /** Ignored data
82  *
83  *  Some parts of the snapshot are ignored by zeroing them out: the real
84  *  values is stored here.
85  * */
86 typedef struct s_mc_snapshot_ignored_data {
87   void* start;
88   std::vector<char> data;
89 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
90
91 typedef struct s_fd_infos{
92   std::string filename;
93   int number;
94   off_t current_position;
95   int flags;
96 }s_fd_infos_t, *fd_infos_t;
97
98 /** Information about a given stack frame
99  *
100  */
101 typedef struct s_mc_stack_frame {
102   /** Instruction pointer */
103   unw_word_t ip;
104   /** Stack pointer */
105   unw_word_t sp;
106   unw_word_t frame_base;
107   dw_frame_t frame;
108   std::string frame_name;
109   unw_cursor_t unw_cursor;
110 } s_mc_stack_frame_t, *mc_stack_frame_t;
111
112 typedef struct s_local_variable{
113   dw_frame_t subprogram;
114   unsigned long ip;
115   std::string name;
116   dw_type_t type;
117   void *address;
118   int region;
119 } s_local_variable_t, *local_variable_t;
120
121 typedef struct s_mc_snapshot_stack{
122   std::vector<s_local_variable> local_variables;
123   mc_unw_context_t context;
124   xbt_dynar_t stack_frames; // mc_stack_frame_t
125   int process_index;
126
127   ~s_mc_snapshot_stack();
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 Snapshot : public AddressSpace {
145 public:
146   Snapshot();
147   ~Snapshot();
148   const void* read_bytes(void* buffer, std::size_t size,
149     remote_ptr<void> address, int process_index = ProcessIndexAny,
150     ReadMode mode = Normal) const MC_OVERRIDE;
151 public: // To be private
152   mc_process_t process;
153   int num_state;
154   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<size_t> stack_sizes;
159   xbt_dynar_t stacks;
160   std::vector<s_mc_heap_ignore_region_t> to_ignore;
161   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, mc_snapshot_t 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(mc_snapshot_t snapshot);
181
182 XBT_INTERNAL mc_snapshot_t MC_take_snapshot(int num_state);
183 XBT_INTERNAL void MC_restore_snapshot(mc_snapshot_t);
184
185 XBT_INTERNAL void mc_restore_page_snapshot_region(
186   mc_process_t process,
187   void* start_addr, simgrid::mc::PerPageCopy const& pagenos);
188
189 MC_SHOULD_BE_INTERNAL const void* MC_region_read_fragmented(
190   mc_mem_region_t region, void* target, const void* addr, size_t size);
191
192 MC_SHOULD_BE_INTERNAL int MC_snapshot_region_memcmp(
193   const void* addr1, mc_mem_region_t region1,
194   const void* addr2, mc_mem_region_t region2, size_t size);
195 XBT_INTERNAL int MC_snapshot_memcmp(
196   const void* addr1, mc_snapshot_t snapshot1,
197   const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
198
199 static inline __attribute__ ((always_inline))
200 const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot)
201 {
202   if(snapshot==NULL)
203       xbt_die("snapshot is NULL");
204   return mc_model_checker->process().get_heap()->breakval;
205 }
206
207 /** @brief Read memory from a snapshot region
208  *
209  *  @param addr    Process (non-snapshot) address of the data
210  *  @param region  Snapshot memory region where the data is located
211  *  @param target  Buffer to store the value
212  *  @param size    Size of the data to read in bytes
213  *  @return Pointer where the data is located (target buffer of original location)
214  */
215 static inline __attribute__((always_inline))
216 const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr, size_t size)
217 {
218   xbt_assert(region);
219
220   uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
221
222   xbt_assert(region->contain(simgrid::mc::remote(addr)),
223     "Trying to read out of the region boundary.");
224
225   switch (region->storage_type()) {
226   case simgrid::mc::StorageType::NoData:
227   default:
228     xbt_die("Storage type not supported");
229
230   case simgrid::mc::StorageType::Flat:
231     return (char*) region->flat_data().data() + offset;
232
233   case simgrid::mc::StorageType::Chunked:
234     {
235       // Last byte of the region:
236       void* end = (char*) addr + size - 1;
237       if (mc_same_page(addr, end) ) {
238         // The memory is contained in a single page:
239         return mc_translate_address_region_chunked((uintptr_t) addr, region);
240       } else {
241         // The memory spans several pages:
242         return MC_region_read_fragmented(region, target, addr, size);
243       }
244     }
245
246   // We currently do not pass the process_index to this function so we assume
247   // that the privatized region has been resolved in the callers:
248   case simgrid::mc::StorageType::Privatized:
249     xbt_die("Storage type not supported");
250   }
251 }
252
253 static inline __attribute__ ((always_inline))
254 void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
255 {
256   void* res;
257   return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
258 }
259
260 SG_END_DECL()
261
262 XBT_INTERNAL int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2,
263                           std::vector<s_mc_heap_ignore_region_t>* i1,
264                           std::vector<s_mc_heap_ignore_region_t>* i2);
265
266 #endif