Logo AND Algorithmique Numérique Distribuée

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