Logo AND Algorithmique Numérique Distribuée

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