Logo AND Algorithmique Numérique Distribuée

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