Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] RegionSnapshot class
[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 namespace simgrid {
44 namespace mc {
45
46 class PerPageCopy {
47   PageStore* store_;
48   std::vector<std::size_t> pagenos_;
49 public:
50   PerPageCopy() : store_(nullptr) {}
51   PerPageCopy(PerPageCopy const& that)
52   {
53     store_ = that.store_;
54     pagenos_ = that.pagenos_;
55     for (std::size_t pageno : pagenos_)
56       store_->ref_page(pageno);
57   }
58   void clear()
59   {
60     for (std::size_t pageno : pagenos_)
61       store_->unref_page(pageno);
62     pagenos_.clear();
63   }
64   ~PerPageCopy() {
65     clear();
66   }
67
68   PerPageCopy(PerPageCopy&& that)
69   {
70     store_ = that.store_;
71     that.store_ = nullptr;
72     pagenos_ = std::move(that.pagenos_);
73     that.pagenos_.clear();
74   }
75   PerPageCopy& operator=(PerPageCopy const& that)
76   {
77     this->clear();
78     store_ = that.store_;
79     pagenos_ = that.pagenos_;
80     for (std::size_t pageno : pagenos_)
81       store_->ref_page(pageno);
82     return *this;
83   }
84   PerPageCopy& operator=(PerPageCopy && that)
85   {
86     this->clear();
87     store_ = that.store_;
88     that.store_ = nullptr;
89     pagenos_ = std::move(that.pagenos_);
90     that.pagenos_.clear();
91     return *this;
92   }
93
94   std::size_t page_count() const
95   {
96     return pagenos_.size();
97   }
98
99   std::size_t pageno(std::size_t i) const
100   {
101     return pagenos_[i];
102   }
103
104   const void* page(std::size_t i) const
105   {
106     return store_->get_page(pagenos_[i]);
107   }
108
109   PerPageCopy(PageStore& store, AddressSpace& as,
110     remote_ptr<void> addr, std::size_t page_count);
111 };
112
113 /** @brief Copy/snapshot of a given memory region
114  *
115  *  Different types of region snapshot storage types exist:
116  *  <ul>
117  *    <li>flat/dense snapshots are a simple copy of the region;</li>
118  *    <li>sparse/per-page snapshots are snaapshots which shared
119  *    identical pages.</li>
120  *    <li>privatized (SMPI global variable privatisation).
121  *  </ul>
122  *
123  *  This is handled with a variant based approch:
124  *
125  *    * `storage_type` identified the type of storage;
126  *    * an anonymous enum is used to distinguish the relevant types for
127  *      each type.
128  */
129 class RegionSnapshot {
130 public:
131   mc_region_type_t region_type;
132   mc_region_storage_type_t storage_type;
133   mc_object_info_t object_info;
134
135   /** @brief  Virtual address of the region in the simulated process */
136   void *start_addr;
137
138   /** @brief Size of the data region in bytes */
139   size_t size;
140
141   /** @brief Permanent virtual address of the region
142    *
143    * This is usually the same address as the simuilated process address.
144    * However, when using SMPI privatization of global variables,
145    * each SMPI process has its own set of global variables stored
146    * at a different virtual address. The scheduler maps those region
147    * on the region of the global variables.
148    *
149    * */
150   void *permanent_addr;
151
152   std::vector<char> flat_data_;
153   PerPageCopy page_numbers_;
154   std::vector<std::unique_ptr<RegionSnapshot>> privatized_regions_;
155 public:
156   RegionSnapshot() :
157     region_type(MC_REGION_TYPE_UNKNOWN),
158     storage_type(MC_REGION_STORAGE_TYPE_NONE),
159     object_info(nullptr),
160     start_addr(nullptr),
161     size(0),
162     permanent_addr(nullptr)
163   {}
164   ~RegionSnapshot();
165   RegionSnapshot(RegionSnapshot const&) = delete;
166   RegionSnapshot& operator=(RegionSnapshot const&) = delete;
167 };
168
169 }
170 }
171
172 typedef class simgrid::mc::RegionSnapshot s_mc_mem_region_t, *mc_mem_region_t;
173
174 MC_SHOULD_BE_INTERNAL mc_mem_region_t mc_region_new_sparse(
175   mc_region_type_t type, void *start_addr, void* data_addr, size_t size);
176 XBT_INTERNAL void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t reg);
177
178 static inline  __attribute__ ((always_inline))
179 bool mc_region_contain(mc_mem_region_t region, const void* p)
180 {
181   return p >= region->start_addr &&
182     p < (void*)((char*) region->start_addr + region->size);
183 }
184
185 static inline __attribute__((always_inline))
186 void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
187 {
188   size_t pageno = mc_page_number(region->start_addr, (void*) addr);
189   const void* snapshot_page =
190     region->page_numbers_.page(pageno);
191   return (char*) snapshot_page + mc_page_offset((void*) addr);
192 }
193
194 static inline __attribute__((always_inline))
195 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
196 {
197   switch (region->storage_type) {
198   case MC_REGION_STORAGE_TYPE_NONE:
199   default:
200     xbt_die("Storage type not supported");
201
202   case MC_REGION_STORAGE_TYPE_FLAT:
203     {
204       uintptr_t offset = addr - (uintptr_t) region->start_addr;
205       return (void *) ((uintptr_t) region->flat_data_.data() + offset);
206     }
207
208   case MC_REGION_STORAGE_TYPE_CHUNKED:
209     return mc_translate_address_region_chunked(addr, region);
210
211   case MC_REGION_STORAGE_TYPE_PRIVATIZED:
212     {
213       xbt_assert(process_index >=0,
214         "Missing process index for privatized region");
215       xbt_assert((size_t) process_index < region->privatized_regions_.size(),
216         "Out of range process index");
217       mc_mem_region_t subregion = region->privatized_regions_[process_index].get();
218       xbt_assert(subregion, "Missing memory region for process %i", process_index);
219       return mc_translate_address_region(addr, subregion, process_index);
220     }
221   }
222 }
223
224 XBT_INTERNAL mc_mem_region_t mc_get_snapshot_region(
225   const void* addr, const s_mc_snapshot_t *snapshot, int process_index);
226
227 // ***** MC Snapshot
228
229 /** Ignored data
230  *
231  *  Some parts of the snapshot are ignored by zeroing them out: the real
232  *  values is stored here.
233  * */
234 typedef struct s_mc_snapshot_ignored_data {
235   void* start;
236   size_t size;
237   void* data;
238 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
239
240 typedef struct s_fd_infos{
241   char *filename;
242   int number;
243   off_t current_position;
244   int flags;
245 }s_fd_infos_t, *fd_infos_t;
246
247 }
248
249 namespace simgrid {
250 namespace mc {
251
252 class Snapshot : public AddressSpace {
253 public:
254   Snapshot();
255   ~Snapshot();
256   const void* read_bytes(void* buffer, std::size_t size,
257     remote_ptr<void> address, int process_index = ProcessIndexAny,
258     ReadMode mode = Normal) const MC_OVERRIDE;
259 public: // To be private
260   mc_process_t process;
261   int num_state;
262   size_t heap_bytes_used;
263   mc_mem_region_t* snapshot_regions;
264   size_t snapshot_regions_count;
265   xbt_dynar_t enabled_processes;
266   int privatization_index;
267   size_t *stack_sizes;
268   xbt_dynar_t stacks;
269   xbt_dynar_t to_ignore;
270   uint64_t hash;
271   xbt_dynar_t ignored_data;
272   int total_fd;
273   fd_infos_t *current_fd;
274 };
275
276 }
277 }
278
279 extern "C" {
280
281 static inline __attribute__ ((always_inline))
282 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
283 {
284   if (mc_region_contain(region, addr))
285     return region;
286   else
287     return mc_get_snapshot_region(addr, snapshot, process_index);
288 }
289
290 /** Information about a given stack frame
291  *
292  */
293 typedef struct s_mc_stack_frame {
294   /** Instruction pointer */
295   unw_word_t ip;
296   /** Stack pointer */
297   unw_word_t sp;
298   unw_word_t frame_base;
299   dw_frame_t frame;
300   char* frame_name;
301   unw_cursor_t unw_cursor;
302 } s_mc_stack_frame_t, *mc_stack_frame_t;
303
304 typedef struct s_mc_snapshot_stack{
305   xbt_dynar_t local_variables;
306   mc_unw_context_t context;
307   xbt_dynar_t stack_frames; // mc_stack_frame_t
308   int process_index;
309 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
310
311 typedef struct s_mc_global_t {
312   mc_snapshot_t snapshot;
313   int prev_pair;
314   char *prev_req;
315   int initial_communications_pattern_done;
316   int recv_deterministic;
317   int send_deterministic;
318   char *send_diff;
319   char *recv_diff;
320 }s_mc_global_t, *mc_global_t;
321
322 static const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
323
324 XBT_INTERNAL mc_snapshot_t MC_take_snapshot(int num_state);
325 XBT_INTERNAL void MC_restore_snapshot(mc_snapshot_t);
326
327 XBT_INTERNAL void mc_restore_page_snapshot_region(
328   mc_process_t process,
329   void* start_addr, simgrid::mc::PerPageCopy const& pagenos);
330
331 MC_SHOULD_BE_INTERNAL const void* MC_region_read_fragmented(
332   mc_mem_region_t region, void* target, const void* addr, size_t size);
333
334 // Deprecated compatibility wrapper
335 static inline
336 const void* MC_snapshot_read(mc_snapshot_t snapshot,
337   simgrid::mc::AddressSpace::ReadMode mode,
338   void* target, const void* addr, size_t size, int process_index)
339 {
340   return snapshot->read_bytes(target, size, simgrid::mc::remote(addr),
341     process_index, mode);
342 }
343
344 MC_SHOULD_BE_INTERNAL int MC_snapshot_region_memcmp(
345   const void* addr1, mc_mem_region_t region1,
346   const void* addr2, mc_mem_region_t region2, size_t size);
347 XBT_INTERNAL int MC_snapshot_memcmp(
348   const void* addr1, mc_snapshot_t snapshot1,
349   const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
350
351 static inline __attribute__ ((always_inline))
352 const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot)
353 {
354   if(snapshot==NULL)
355       xbt_die("snapshot is NULL");
356   return mc_model_checker->process().get_heap()->breakval;
357 }
358
359 /** @brief Read memory from a snapshot region
360  *
361  *  @param addr    Process (non-snapshot) address of the data
362  *  @param region  Snapshot memory region where the data is located
363  *  @param target  Buffer to store the value
364  *  @param size    Size of the data to read in bytes
365  *  @return Pointer where the data is located (target buffer of original location)
366  */
367 static inline __attribute__((always_inline))
368 const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr, size_t size)
369 {
370   xbt_assert(region);
371
372   uintptr_t offset = (char*) addr - (char*) region->start_addr;
373
374   xbt_assert(mc_region_contain(region, addr),
375     "Trying to read out of the region boundary.");
376
377   switch (region->storage_type) {
378   case MC_REGION_STORAGE_TYPE_NONE:
379   default:
380     xbt_die("Storage type not supported");
381
382   case MC_REGION_STORAGE_TYPE_FLAT:
383     return (char*) region->flat_data_.data() + offset;
384
385   case MC_REGION_STORAGE_TYPE_CHUNKED:
386     {
387       // Last byte of the region:
388       void* end = (char*) addr + size - 1;
389       if (mc_same_page(addr, end) ) {
390         // The memory is contained in a single page:
391         return mc_translate_address_region_chunked((uintptr_t) addr, region);
392       } else {
393         // The memory spans several pages:
394         return MC_region_read_fragmented(region, target, addr, size);
395       }
396     }
397
398   // We currently do not pass the process_index to this function so we assume
399   // that the privatized region has been resolved in the callers:
400   case MC_REGION_STORAGE_TYPE_PRIVATIZED:
401     xbt_die("Storage type not supported");
402   }
403 }
404
405 static inline __attribute__ ((always_inline))
406 void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
407 {
408   void* res;
409   return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
410 }
411
412 SG_END_DECL()
413
414 #endif