Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove mc_translate_address()
[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 "ModelChecker.hpp"
20 #include "PageStore.hpp"
21 #include "mc_mmalloc.h"
22 #include "mc/AddressSpace.hpp"
23 #include "mc_unw.h"
24
25 SG_BEGIN_DECL()
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_SHOULD_BE_INTERNAL mc_mem_region_t mc_region_new_sparse(
101   mc_region_type_t type, void *start_addr, void* data_addr, size_t size);
102 MC_SHOULD_BE_INTERNAL void MC_region_destroy(mc_mem_region_t reg);
103 XBT_INTERNAL void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t 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_chunked(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 =
118     mc_model_checker->page_store().get_page(snapshot_pageno);
119   return (char*) snapshot_page + mc_page_offset((void*) addr);
120 }
121
122 static inline __attribute__((always_inline))
123 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
124 {
125   switch (region->storage_type) {
126   case MC_REGION_STORAGE_TYPE_NONE:
127   default:
128     xbt_die("Storage type not supported");
129
130   case MC_REGION_STORAGE_TYPE_FLAT:
131     {
132       uintptr_t offset = addr - (uintptr_t) region->start_addr;
133       return (void *) ((uintptr_t) region->flat.data + offset);
134     }
135
136   case MC_REGION_STORAGE_TYPE_CHUNKED:
137     return mc_translate_address_region_chunked(addr, region);
138
139   case MC_REGION_STORAGE_TYPE_PRIVATIZED:
140     {
141       xbt_assert(process_index >=0,
142         "Missing process index for privatized region");
143       xbt_assert((size_t) process_index < region->privatized.regions_count,
144         "Out of range process index");
145       mc_mem_region_t subregion = region->privatized.regions[process_index];
146       xbt_assert(subregion, "Missing memory region for process %i", process_index);
147       return mc_translate_address_region(addr, subregion, process_index);
148     }
149   }
150 }
151
152 XBT_INTERNAL mc_mem_region_t mc_get_snapshot_region(
153   const void* addr, const s_mc_snapshot_t *snapshot, int process_index);
154
155 // ***** MC Snapshot
156
157 /** Ignored data
158  *
159  *  Some parts of the snapshot are ignored by zeroing them out: the real
160  *  values is stored here.
161  * */
162 typedef struct s_mc_snapshot_ignored_data {
163   void* start;
164   size_t size;
165   void* data;
166 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
167
168 typedef struct s_fd_infos{
169   char *filename;
170   int number;
171   off_t current_position;
172   int flags;
173 }s_fd_infos_t, *fd_infos_t;
174
175 }
176
177 namespace simgrid {
178 namespace mc {
179
180 class Snapshot : public AddressSpace {
181 public:
182   Snapshot();
183   ~Snapshot();
184   const void* read_bytes(void* buffer, std::size_t size,
185     remote_ptr<void> address, int process_index = ProcessIndexAny,
186     ReadMode mode = Normal) const MC_OVERRIDE;
187 public: // To be private
188   mc_process_t process;
189   int num_state;
190   size_t heap_bytes_used;
191   mc_mem_region_t* snapshot_regions;
192   size_t snapshot_regions_count;
193   xbt_dynar_t enabled_processes;
194   int privatization_index;
195   size_t *stack_sizes;
196   xbt_dynar_t stacks;
197   xbt_dynar_t to_ignore;
198   uint64_t hash;
199   xbt_dynar_t ignored_data;
200   int total_fd;
201   fd_infos_t *current_fd;
202 };
203
204 }
205 }
206
207 extern "C" {
208
209 static inline __attribute__ ((always_inline))
210 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
211 {
212   if (mc_region_contain(region, addr))
213     return region;
214   else
215     return mc_get_snapshot_region(addr, snapshot, process_index);
216 }
217
218 /** Information about a given stack frame
219  *
220  */
221 typedef struct s_mc_stack_frame {
222   /** Instruction pointer */
223   unw_word_t ip;
224   /** Stack pointer */
225   unw_word_t sp;
226   unw_word_t frame_base;
227   dw_frame_t frame;
228   char* frame_name;
229   unw_cursor_t unw_cursor;
230 } s_mc_stack_frame_t, *mc_stack_frame_t;
231
232 typedef struct s_mc_snapshot_stack{
233   xbt_dynar_t local_variables;
234   mc_unw_context_t context;
235   xbt_dynar_t stack_frames; // mc_stack_frame_t
236   int process_index;
237 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
238
239 typedef struct s_mc_global_t {
240   mc_snapshot_t snapshot;
241   int prev_pair;
242   char *prev_req;
243   int initial_communications_pattern_done;
244   int recv_deterministic;
245   int send_deterministic;
246   char *send_diff;
247   char *recv_diff;
248 }s_mc_global_t, *mc_global_t;
249
250 static const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
251
252 XBT_INTERNAL mc_snapshot_t MC_take_snapshot(int num_state);
253 XBT_INTERNAL void MC_restore_snapshot(mc_snapshot_t);
254
255 XBT_INTERNAL size_t* mc_take_page_snapshot_region(mc_process_t process,
256   void* data, size_t page_count);
257 XBT_INTERNAL void mc_free_page_snapshot_region(size_t* pagenos, size_t page_count);
258 XBT_INTERNAL void mc_restore_page_snapshot_region(
259   mc_process_t process,
260   void* start_addr, size_t page_count, size_t* pagenos);
261
262 MC_SHOULD_BE_INTERNAL const void* MC_region_read_fragmented(
263   mc_mem_region_t region, void* target, const void* addr, size_t size);
264
265 // Deprecated compatibility wrapper
266 static inline
267 const void* MC_snapshot_read(mc_snapshot_t snapshot,
268   simgrid::mc::AddressSpace::ReadMode mode,
269   void* target, const void* addr, size_t size, int process_index)
270 {
271   return snapshot->read_bytes(target, size, simgrid::mc::remote(addr),
272     process_index, mode);
273 }
274
275 MC_SHOULD_BE_INTERNAL int MC_snapshot_region_memcmp(
276   const void* addr1, mc_mem_region_t region1,
277   const void* addr2, mc_mem_region_t region2, size_t size);
278 XBT_INTERNAL int MC_snapshot_memcmp(
279   const void* addr1, mc_snapshot_t snapshot1,
280   const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
281
282 static inline __attribute__ ((always_inline))
283 const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot)
284 {
285   if(snapshot==NULL)
286       xbt_die("snapshot is NULL");
287   return mc_model_checker->process().get_heap()->breakval;
288 }
289
290 /** @brief Read memory from a snapshot region
291  *
292  *  @param addr    Process (non-snapshot) address of the data
293  *  @param region  Snapshot memory region where the data is located
294  *  @param target  Buffer to store the value
295  *  @param size    Size of the data to read in bytes
296  *  @return Pointer where the data is located (target buffer of original location)
297  */
298 static inline __attribute__((always_inline))
299 const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr, size_t size)
300 {
301   xbt_assert(region);
302
303   uintptr_t offset = (char*) addr - (char*) region->start_addr;
304
305   xbt_assert(mc_region_contain(region, addr),
306     "Trying to read out of the region boundary.");
307
308   switch (region->storage_type) {
309   case MC_REGION_STORAGE_TYPE_NONE:
310   default:
311     xbt_die("Storage type not supported");
312
313   case MC_REGION_STORAGE_TYPE_FLAT:
314     return (char*) region->flat.data + offset;
315
316   case MC_REGION_STORAGE_TYPE_CHUNKED:
317     {
318       // Last byte of the region:
319       void* end = (char*) addr + size - 1;
320       if (mc_same_page(addr, end) ) {
321         // The memory is contained in a single page:
322         return mc_translate_address_region_chunked((uintptr_t) addr, region);
323       } else {
324         // The memory spans several pages:
325         return MC_region_read_fragmented(region, target, addr, size);
326       }
327     }
328
329   // We currently do not pass the process_index to this function so we assume
330   // that the privatized region has been resolved in the callers:
331   case MC_REGION_STORAGE_TYPE_PRIVATIZED:
332     xbt_die("Storage type not supported");
333   }
334 }
335
336 static inline __attribute__ ((always_inline))
337 void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
338 {
339   void* res;
340   return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
341 }
342
343 SG_END_DECL()
344
345 #endif