Logo AND Algorithmique Numérique Distribuée

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