Logo AND Algorithmique Numérique Distribuée

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