Logo AND Algorithmique Numérique Distribuée

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