Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
87e007f4021c84c323865cf264044cb66e31f300
[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 <simgrid_config.h>
14 #include "../xbt/mmalloc/mmprivate.h"
15 #include <xbt/asserts.h>
16 #include <xbt/dynar.h>
17
18 #include "mc_forward.h"
19 #include "ModelChecker.hpp"
20 #include "PageStore.hpp"
21 #include "mc_mmalloc.h"
22 #include "mc/AddressSpace.hpp"
23 #include "mc_unw.h"
24 #include "RegionSnapshot.hpp"
25
26 SG_BEGIN_DECL()
27
28 // ***** Snapshot region
29
30 XBT_INTERNAL void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t reg);
31
32 static inline  __attribute__ ((always_inline))
33 bool mc_region_contain(mc_mem_region_t region, const void* p)
34 {
35   return region->contain(simgrid::mc::remote(p));
36 }
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 MC_REGION_STORAGE_TYPE_NONE:
52   default:
53     xbt_die("Storage type not supported");
54
55   case MC_REGION_STORAGE_TYPE_FLAT:
56     {
57       uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
58       return (void *) ((uintptr_t) region->flat_data().data() + offset);
59     }
60
61   case MC_REGION_STORAGE_TYPE_CHUNKED:
62     return mc_translate_address_region_chunked(addr, region);
63
64   case MC_REGION_STORAGE_TYPE_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_INTERNAL mc_mem_region_t mc_get_snapshot_region(
77   const void* addr, const s_mc_snapshot_t *snapshot, int process_index);
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   size_t size;
89   void* data;
90 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
91
92 typedef struct s_fd_infos{
93   char *filename;
94   int number;
95   off_t current_position;
96   int flags;
97 }s_fd_infos_t, *fd_infos_t;
98
99 }
100
101 namespace simgrid {
102 namespace mc {
103
104 class Snapshot : public AddressSpace {
105 public:
106   Snapshot();
107   ~Snapshot();
108   const void* read_bytes(void* buffer, std::size_t size,
109     remote_ptr<void> address, int process_index = ProcessIndexAny,
110     ReadMode mode = Normal) const MC_OVERRIDE;
111 public: // To be private
112   mc_process_t process;
113   int num_state;
114   size_t heap_bytes_used;
115   mc_mem_region_t* snapshot_regions;
116   size_t snapshot_regions_count;
117   xbt_dynar_t enabled_processes;
118   int privatization_index;
119   size_t *stack_sizes;
120   xbt_dynar_t stacks;
121   xbt_dynar_t to_ignore;
122   uint64_t hash;
123   xbt_dynar_t ignored_data;
124   int total_fd;
125   fd_infos_t *current_fd;
126 };
127
128 }
129 }
130
131 extern "C" {
132
133 static inline __attribute__ ((always_inline))
134 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
135 {
136   if (mc_region_contain(region, addr))
137     return region;
138   else
139     return mc_get_snapshot_region(addr, snapshot, process_index);
140 }
141
142 /** Information about a given stack frame
143  *
144  */
145 typedef struct s_mc_stack_frame {
146   /** Instruction pointer */
147   unw_word_t ip;
148   /** Stack pointer */
149   unw_word_t sp;
150   unw_word_t frame_base;
151   dw_frame_t frame;
152   char* frame_name;
153   unw_cursor_t unw_cursor;
154 } s_mc_stack_frame_t, *mc_stack_frame_t;
155
156 typedef struct s_mc_snapshot_stack{
157   xbt_dynar_t local_variables;
158   mc_unw_context_t context;
159   xbt_dynar_t stack_frames; // mc_stack_frame_t
160   int process_index;
161 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
162
163 typedef struct s_mc_global_t {
164   mc_snapshot_t snapshot;
165   int prev_pair;
166   char *prev_req;
167   int initial_communications_pattern_done;
168   int recv_deterministic;
169   int send_deterministic;
170   char *send_diff;
171   char *recv_diff;
172 }s_mc_global_t, *mc_global_t;
173
174 static const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
175
176 XBT_INTERNAL mc_snapshot_t MC_take_snapshot(int num_state);
177 XBT_INTERNAL void MC_restore_snapshot(mc_snapshot_t);
178
179 XBT_INTERNAL void mc_restore_page_snapshot_region(
180   mc_process_t process,
181   void* start_addr, simgrid::mc::PerPageCopy const& pagenos);
182
183 MC_SHOULD_BE_INTERNAL const void* MC_region_read_fragmented(
184   mc_mem_region_t region, void* target, const void* addr, size_t size);
185
186 MC_SHOULD_BE_INTERNAL int MC_snapshot_region_memcmp(
187   const void* addr1, mc_mem_region_t region1,
188   const void* addr2, mc_mem_region_t region2, size_t size);
189 XBT_INTERNAL int MC_snapshot_memcmp(
190   const void* addr1, mc_snapshot_t snapshot1,
191   const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
192
193 static inline __attribute__ ((always_inline))
194 const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot)
195 {
196   if(snapshot==NULL)
197       xbt_die("snapshot is NULL");
198   return mc_model_checker->process().get_heap()->breakval;
199 }
200
201 /** @brief Read memory from a snapshot region
202  *
203  *  @param addr    Process (non-snapshot) address of the data
204  *  @param region  Snapshot memory region where the data is located
205  *  @param target  Buffer to store the value
206  *  @param size    Size of the data to read in bytes
207  *  @return Pointer where the data is located (target buffer of original location)
208  */
209 static inline __attribute__((always_inline))
210 const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr, size_t size)
211 {
212   xbt_assert(region);
213
214   uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
215
216   xbt_assert(mc_region_contain(region, addr),
217     "Trying to read out of the region boundary.");
218
219   switch (region->storage_type()) {
220   case MC_REGION_STORAGE_TYPE_NONE:
221   default:
222     xbt_die("Storage type not supported");
223
224   case MC_REGION_STORAGE_TYPE_FLAT:
225     return (char*) region->flat_data().data() + offset;
226
227   case MC_REGION_STORAGE_TYPE_CHUNKED:
228     {
229       // Last byte of the region:
230       void* end = (char*) addr + size - 1;
231       if (mc_same_page(addr, end) ) {
232         // The memory is contained in a single page:
233         return mc_translate_address_region_chunked((uintptr_t) addr, region);
234       } else {
235         // The memory spans several pages:
236         return MC_region_read_fragmented(region, target, addr, size);
237       }
238     }
239
240   // We currently do not pass the process_index to this function so we assume
241   // that the privatized region has been resolved in the callers:
242   case MC_REGION_STORAGE_TYPE_PRIVATIZED:
243     xbt_die("Storage type not supported");
244   }
245 }
246
247 static inline __attribute__ ((always_inline))
248 void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
249 {
250   void* res;
251   return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
252 }
253
254 SG_END_DECL()
255
256 #endif