Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge master into mc-process
[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 "mc_model_checker.h"
20 #include "mc_page_store.h"
21 #include "mc_mmalloc.h"
22 #include "mc_address_space.h"
23 #include "mc_unw.h"
24
25 SG_BEGIN_DECL()
26
27 void mc_softdirty_reset(void);
28
29 // ***** Snapshot region
30
31 typedef enum e_mc_region_type_t {
32   MC_REGION_TYPE_UNKNOWN = 0,
33   MC_REGION_TYPE_HEAP = 1,
34   MC_REGION_TYPE_DATA = 2
35 } mc_region_type_t;
36
37 // TODO, use OO instead of this
38 typedef enum e_mc_region_storeage_type_t {
39   MC_REGION_STORAGE_TYPE_NONE = 0,
40   MC_REGION_STORAGE_TYPE_FLAT = 1,
41   MC_REGION_STORAGE_TYPE_CHUNKED = 2,
42   MC_REGION_STORAGE_TYPE_PRIVATIZED = 3
43 } mc_region_storage_type_t;
44
45 /** @brief Copy/snapshot of a given memory region
46  *
47  *  Different types of region snapshot storage types exist:
48  *  <ul>
49  *    <li>flat/dense snapshots are a simple copy of the region;</li>
50  *    <li>sparse/per-page snapshots are snaapshots which shared
51  *    identical pages.</li>
52  *    <li>privatized (SMPI global variable privatisation).
53  *  </ul>
54  *
55  *  This is handled with a variant based approch:
56  *
57  *    * `storage_type` identified the type of storage;
58  *    * an anonymous enum is used to distinguish the relevant types for
59  *      each type.
60  */
61 typedef struct s_mc_mem_region s_mc_mem_region_t, *mc_mem_region_t;
62
63 struct s_mc_mem_region {
64   mc_region_type_t region_type;
65   mc_region_storage_type_t storage_type;
66   mc_object_info_t object_info;
67
68   /** @brief  Virtual address of the region in the simulated process */
69   void *start_addr;
70
71   /** @brief Size of the data region in bytes */
72   size_t size;
73
74   /** @brief Permanent virtual address of the region
75    *
76    * This is usually the same address as the simuilated process address.
77    * However, when using SMPI privatization of global variables,
78    * each SMPI process has its own set of global variables stored
79    * at a different virtual address. The scheduler maps those region
80    * on the region of the global variables.
81    *
82    * */
83   void *permanent_addr;
84
85   union {
86     struct {
87       /** @brief Copy of the snapshot for flat snapshots regions (NULL otherwise) */
88       void *data;
89     } flat;
90     struct {
91       /** @brief Pages indices in the page store for per-page snapshots (NULL otherwise) */
92       size_t* page_numbers;
93     } chunked;
94     struct {
95       size_t regions_count;
96       mc_mem_region_t* regions;
97     } privatized;
98   };
99
100 };
101
102 mc_mem_region_t mc_region_new_sparse(mc_region_type_t type, void *start_addr, void* data_addr, size_t size, mc_mem_region_t ref_reg);
103 void MC_region_destroy(mc_mem_region_t reg);
104 void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t reg, mc_mem_region_t ref_reg);
105
106 static inline  __attribute__ ((always_inline))
107 bool mc_region_contain(mc_mem_region_t region, const void* p)
108 {
109   return p >= region->start_addr &&
110     p < (void*)((char*) region->start_addr + region->size);
111 }
112
113 static inline __attribute__((always_inline))
114 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region)
115 {
116     size_t pageno = mc_page_number(region->start_addr, (void*) addr);
117     size_t snapshot_pageno = region->chunked.page_numbers[pageno];
118     const void* snapshot_page = mc_page_store_get_page(mc_model_checker->pages, snapshot_pageno);
119     return (char*) snapshot_page + mc_page_offset((void*) addr);
120 }
121
122 mc_mem_region_t mc_get_snapshot_region(const void* addr, mc_snapshot_t snapshot, int process_index);
123
124 /** \brief Translate a pointer from process address space to snapshot address space
125  *
126  *  The address space contains snapshot of the main/application memory:
127  *  this function finds the address in a given snaphot for a given
128  *  real/application address.
129  *
130  *  For read only memory regions and other regions which are not int the
131  *  snapshot, the address is not changed.
132  *
133  *  \param addr     Application address
134  *  \param snapshot The snapshot of interest (if NULL no translation is done)
135  *  \return         Translated address in the snapshot address space
136  * */
137 static inline __attribute__((always_inline))
138 void* mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot, int process_index)
139 {
140
141   // If not in a process state/clone:
142   if (!snapshot) {
143     return (uintptr_t *) addr;
144   }
145
146   mc_mem_region_t region = mc_get_snapshot_region((void*) addr, snapshot, process_index);
147
148   xbt_assert(mc_region_contain(region, (void*) addr), "Trying to read out of the region boundary.");
149
150   if (!region) {
151     return (void *) addr;
152   }
153
154   switch (region->storage_type) {
155   case MC_REGION_STORAGE_TYPE_NONE:
156   default:
157     xbt_die("Storage type not supported");
158
159   case MC_REGION_STORAGE_TYPE_FLAT:
160     {
161       uintptr_t offset = addr - (uintptr_t) region->start_addr;
162       return (void *) ((uintptr_t) region->flat.data + offset);
163     }
164
165   case MC_REGION_STORAGE_TYPE_CHUNKED:
166     return mc_translate_address_region(addr, region);
167
168   case MC_REGION_STORAGE_TYPE_PRIVATIZED:
169     {
170       xbt_assert(process_index >=0,
171         "Missing process index for privatized region");
172       xbt_assert((size_t) process_index < region->privatized.regions_count,
173         "Out of range process index");
174       mc_mem_region_t subregion = region->privatized.regions[process_index];
175       xbt_assert(subregion, "Missing memory region for process %i", process_index);
176       return mc_translate_address(addr, snapshot, process_index);
177     }
178   }
179 }
180
181 // ***** MC Snapshot
182
183 /** Ignored data
184  *
185  *  Some parts of the snapshot are ignored by zeroing them out: the real
186  *  values is stored here.
187  * */
188 typedef struct s_mc_snapshot_ignored_data {
189   void* start;
190   size_t size;
191   void* data;
192 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
193
194 typedef struct s_fd_infos{
195   char *filename;
196   int number;
197   off_t current_position;
198   int flags;
199 }s_fd_infos_t, *fd_infos_t;
200
201 struct s_mc_snapshot {
202   mc_process_t process;
203   s_mc_address_space_t address_space;
204   size_t heap_bytes_used;
205   mc_mem_region_t* snapshot_regions;
206   size_t snapshot_regions_count;
207   xbt_dynar_t enabled_processes;
208   int privatization_index;
209   size_t *stack_sizes;
210   xbt_dynar_t stacks;
211   xbt_dynar_t to_ignore;
212   uint64_t hash;
213   xbt_dynar_t ignored_data;
214   int total_fd;
215   fd_infos_t *current_fd;
216 };
217
218 static inline __attribute__ ((always_inline))
219 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
220 {
221   if (mc_region_contain(region, addr))
222     return region;
223   else
224     return mc_get_snapshot_region(addr, snapshot, process_index);
225 }
226
227 /** Information about a given stack frame
228  *
229  */
230 typedef struct s_mc_stack_frame {
231   /** Instruction pointer */
232   unw_word_t ip;
233   /** Stack pointer */
234   unw_word_t sp;
235   unw_word_t frame_base;
236   dw_frame_t frame;
237   char* frame_name;
238   unw_cursor_t unw_cursor;
239 } s_mc_stack_frame_t, *mc_stack_frame_t;
240
241 typedef struct s_mc_snapshot_stack{
242   xbt_dynar_t local_variables;
243   mc_unw_context_t context;
244   xbt_dynar_t stack_frames; // mc_stack_frame_t
245   int process_index;
246 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
247
248 typedef struct s_mc_global_t {
249   mc_snapshot_t snapshot;
250   int raw_mem_set;
251   int prev_pair;
252   char *prev_req;
253   int initial_communications_pattern_done;
254   int comm_deterministic;
255   int send_deterministic;
256 }s_mc_global_t, *mc_global_t;
257
258 typedef struct s_mc_checkpoint_ignore_region{
259   void *addr;
260   size_t size;
261 }s_mc_checkpoint_ignore_region_t, *mc_checkpoint_ignore_region_t;
262
263 static const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
264
265 mc_snapshot_t MC_take_snapshot(int num_state);
266 void MC_restore_snapshot(mc_snapshot_t);
267 void MC_free_snapshot(mc_snapshot_t);
268
269 int mc_important_snapshot(mc_snapshot_t snapshot);
270
271 size_t* mc_take_page_snapshot_region(mc_process_t process,
272   void* data, size_t page_count, uint64_t* pagemap, size_t* reference_pages);
273 void mc_free_page_snapshot_region(size_t* pagenos, size_t page_count);
274 void mc_restore_page_snapshot_region(
275   mc_process_t process,
276   void* start_addr, size_t page_count, size_t* pagenos,
277   uint64_t* pagemap, size_t* reference_pagenos);
278
279 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, size_t size);
280
281 const void* MC_snapshot_read(mc_snapshot_t snapshot, e_adress_space_read_flags_t flags,
282   void* target, const void* addr, size_t size, int process_index);
283 int MC_snapshot_region_memcmp(
284   const void* addr1, mc_mem_region_t region1,
285   const void* addr2, mc_mem_region_t region2, size_t size);
286 int MC_snapshot_memcmp(
287   const void* addr1, mc_snapshot_t snapshot1,
288   const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
289
290 static inline __attribute__ ((always_inline))
291 const void* MC_snapshot_read_pointer(mc_snapshot_t snapshot, const void* addr, int process_index)
292 {
293   void* res;
294   return *(const void**) MC_snapshot_read(snapshot, MC_ADDRESS_SPACE_READ_FLAGS_LAZY,
295     &res, addr, sizeof(void*), process_index);
296 }
297
298 static inline __attribute__ ((always_inline))
299 const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot)
300 {
301   if(snapshot==NULL)
302       xbt_die("snapshot is NULL");
303   // This is &std_heap->breakval in the target process:
304   void** addr = &MC_process_get_heap(&mc_model_checker->process)->breakval;
305   // Read (std_heap->breakval) in the target process (*addr i.e. std_heap->breakval):
306   return MC_snapshot_read_pointer(snapshot, addr, MC_PROCESS_INDEX_ANY);
307 }
308
309 /** @brief Read memory from a snapshot region
310  *
311  *  @param addr    Process (non-snapshot) address of the data
312  *  @param region  Snapshot memory region where the data is located
313  *  @param target  Buffer to store the value
314  *  @param size    Size of the data to read in bytes
315  *  @return Pointer where the data is located (target buffer of original location)
316  */
317 static inline __attribute__((always_inline))
318 const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr, size_t size)
319 {
320   if (region==NULL)
321     // Should be deprecated:
322     return addr;
323
324   uintptr_t offset = (char*) addr - (char*) region->start_addr;
325
326   xbt_assert(mc_region_contain(region, addr),
327     "Trying to read out of the region boundary.");
328
329   switch (region->storage_type) {
330   case MC_REGION_STORAGE_TYPE_NONE:
331   default:
332     xbt_die("Storage type not supported");
333
334   case MC_REGION_STORAGE_TYPE_FLAT:
335     return (char*) region->flat.data + offset;
336
337   case MC_REGION_STORAGE_TYPE_CHUNKED:
338     {
339       // Last byte of the region:
340       void* end = (char*) addr + size - 1;
341       if (mc_same_page(addr, end) ) {
342         // The memory is contained in a single page:
343         return mc_translate_address_region((uintptr_t) addr, region);
344       } else {
345         // The memory spans several pages:
346         return MC_region_read_fragmented(region, target, addr, size);
347       }
348     }
349
350   // We currently do not pass the process_index to this function so we assume
351   // that the privatized region has been resolved in the callers:
352   case MC_REGION_STORAGE_TYPE_PRIVATIZED:
353     xbt_die("Storage type not supported");
354   }
355 }
356
357 static inline __attribute__ ((always_inline))
358 void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
359 {
360   void* res;
361   return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
362 }
363
364 SG_END_DECL()
365
366 #endif