Logo AND Algorithmique Numérique Distribuée

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