Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a6a4cde12bb2dfc14f785355aa97d6f8b83d9dcf
[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_storage_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 private:
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<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(mc_region_type_t type, void *start_addr, void* permanent_addr, size_t size) :
165     region_type_(type),
166     storage_type_(MC_REGION_STORAGE_TYPE_NONE),
167     object_info_(nullptr),
168     start_addr_(start_addr),
169     size_(size),
170     permanent_addr_(permanent_addr)
171   {}
172   ~RegionSnapshot();
173   RegionSnapshot(RegionSnapshot const&) = default;
174   RegionSnapshot& operator=(RegionSnapshot const&) = default;
175   RegionSnapshot(RegionSnapshot&& that)
176   {
177     region_type_ = that.region_type_;
178     storage_type_ = that.storage_type_;
179     object_info_ = that.object_info_;
180     start_addr_ = that.start_addr_;
181     size_ = that.size_;
182     permanent_addr_ = that.permanent_addr_;
183     flat_data_ = std::move(that.flat_data_);
184     page_numbers_ = std::move(that.page_numbers_);
185     privatized_regions_ = std::move(that.privatized_regions_);
186     that.clear();
187   }
188   RegionSnapshot& operator=(RegionSnapshot&& that)
189   {
190     region_type_ = that.region_type_;
191     storage_type_ = that.storage_type_;
192     object_info_ = that.object_info_;
193     start_addr_ = that.start_addr_;
194     size_ = that.size_;
195     permanent_addr_ = that.permanent_addr_;
196     flat_data_ = std::move(that.flat_data_);
197     page_numbers_ = std::move(that.page_numbers_);
198     privatized_regions_ = std::move(that.privatized_regions_);
199     that.clear();
200     return *this;
201   }
202
203   // Data
204
205   void clear()
206   {
207     region_type_ = MC_REGION_TYPE_UNKNOWN;
208     storage_type_ = MC_REGION_STORAGE_TYPE_NONE;
209     privatized_regions_.clear();
210     page_numbers_.clear();
211     flat_data_.clear();
212     object_info_ = nullptr;
213     start_addr_ = nullptr;
214     size_ = 0;
215     permanent_addr_ = nullptr;
216   }
217
218   void clear_data()
219   {
220     storage_type_ = MC_REGION_STORAGE_TYPE_NONE;
221     flat_data_.clear();
222     page_numbers_.clear();
223     privatized_regions_.clear();
224   }
225   
226   void flat_data(std::vector<char> data)
227   {
228     storage_type_ = MC_REGION_STORAGE_TYPE_FLAT;
229     flat_data_ = std::move(data);
230     page_numbers_.clear();
231     privatized_regions_.clear();
232   }
233   std::vector<char> const& flat_data() const { return flat_data_; }
234
235   void page_data(PerPageCopy page_data)
236   {
237     storage_type_ = MC_REGION_STORAGE_TYPE_CHUNKED;
238     flat_data_.clear();
239     page_numbers_ = std::move(page_data);
240     privatized_regions_.clear();
241   }
242   PerPageCopy const& page_data() const { return page_numbers_; }
243
244   void privatized_data(std::vector<RegionSnapshot> data)
245   {
246     storage_type_ = MC_REGION_STORAGE_TYPE_PRIVATIZED;
247     flat_data_.clear();
248     page_numbers_.clear();
249     privatized_regions_ = std::move(data);
250   }
251   std::vector<RegionSnapshot> const& privatized_data() const
252   {
253     return privatized_regions_;
254   }
255   std::vector<RegionSnapshot>& privatized_data()
256   {
257     return privatized_regions_;
258   }
259
260   mc_object_info_t object_info() const { return object_info_; }
261   void object_info(mc_object_info_t info) { object_info_ = info; }
262
263   // Other getters
264
265   remote_ptr<void> start() const { return remote(start_addr_); }
266   remote_ptr<void> end() const { return remote((char*)start_addr_ + size_); }
267   remote_ptr<void> permanent_address() const { return remote(permanent_addr_); }
268   std::size_t size() const { return size_; }
269   mc_region_storage_type_t storage_type() const { return storage_type_; }
270   mc_region_type_t region_type() const { return region_type_; }
271
272   bool contain(remote_ptr<void> p) const
273   {
274     return p >= start() && p < end();
275   }
276 };
277
278 simgrid::mc::RegionSnapshot privatized_region(
279   mc_region_type_t type, void *start_addr, void* data_addr, size_t size);
280 simgrid::mc::RegionSnapshot dense_region(
281   mc_region_type_t type, void *start_addr, void* data_addr, size_t size);
282 simgrid::mc::RegionSnapshot sparse_region(
283   mc_region_type_t type, void *start_addr, void* data_addr, size_t size);
284 simgrid::mc::RegionSnapshot region(
285   mc_region_type_t type, void *start_addr, void* data_addr, size_t size);
286
287 }
288 }
289
290 typedef class simgrid::mc::RegionSnapshot s_mc_mem_region_t, *mc_mem_region_t;
291
292 namespace simgrid {
293 namespace mc {
294
295 }
296 }
297
298 XBT_INTERNAL void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t reg);
299
300 static inline  __attribute__ ((always_inline))
301 bool mc_region_contain(mc_mem_region_t region, const void* p)
302 {
303   return region->contain(simgrid::mc::remote(p));
304 }
305
306 static inline __attribute__((always_inline))
307 void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
308 {
309   size_t pageno = mc_page_number((void*)region->start().address(), (void*) addr);
310   const void* snapshot_page =
311     region->page_data().page(pageno);
312   return (char*) snapshot_page + mc_page_offset((void*) addr);
313 }
314
315 static inline __attribute__((always_inline))
316 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
317 {
318   switch (region->storage_type()) {
319   case MC_REGION_STORAGE_TYPE_NONE:
320   default:
321     xbt_die("Storage type not supported");
322
323   case MC_REGION_STORAGE_TYPE_FLAT:
324     {
325       uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
326       return (void *) ((uintptr_t) region->flat_data().data() + offset);
327     }
328
329   case MC_REGION_STORAGE_TYPE_CHUNKED:
330     return mc_translate_address_region_chunked(addr, region);
331
332   case MC_REGION_STORAGE_TYPE_PRIVATIZED:
333     {
334       xbt_assert(process_index >=0,
335         "Missing process index for privatized region");
336       xbt_assert((size_t) process_index < region->privatized_data().size(),
337         "Out of range process index");
338       simgrid::mc::RegionSnapshot& subregion= region->privatized_data()[process_index];
339       return mc_translate_address_region(addr, &subregion, process_index);
340     }
341   }
342 }
343
344 XBT_INTERNAL mc_mem_region_t mc_get_snapshot_region(
345   const void* addr, const s_mc_snapshot_t *snapshot, int process_index);
346
347 // ***** MC Snapshot
348
349 /** Ignored data
350  *
351  *  Some parts of the snapshot are ignored by zeroing them out: the real
352  *  values is stored here.
353  * */
354 typedef struct s_mc_snapshot_ignored_data {
355   void* start;
356   size_t size;
357   void* data;
358 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
359
360 typedef struct s_fd_infos{
361   char *filename;
362   int number;
363   off_t current_position;
364   int flags;
365 }s_fd_infos_t, *fd_infos_t;
366
367 }
368
369 namespace simgrid {
370 namespace mc {
371
372 class Snapshot : public AddressSpace {
373 public:
374   Snapshot();
375   ~Snapshot();
376   const void* read_bytes(void* buffer, std::size_t size,
377     remote_ptr<void> address, int process_index = ProcessIndexAny,
378     ReadMode mode = Normal) const MC_OVERRIDE;
379 public: // To be private
380   mc_process_t process;
381   int num_state;
382   size_t heap_bytes_used;
383   mc_mem_region_t* snapshot_regions;
384   size_t snapshot_regions_count;
385   xbt_dynar_t enabled_processes;
386   int privatization_index;
387   size_t *stack_sizes;
388   xbt_dynar_t stacks;
389   xbt_dynar_t to_ignore;
390   uint64_t hash;
391   xbt_dynar_t ignored_data;
392   int total_fd;
393   fd_infos_t *current_fd;
394 };
395
396 }
397 }
398
399 extern "C" {
400
401 static inline __attribute__ ((always_inline))
402 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
403 {
404   if (mc_region_contain(region, addr))
405     return region;
406   else
407     return mc_get_snapshot_region(addr, snapshot, process_index);
408 }
409
410 /** Information about a given stack frame
411  *
412  */
413 typedef struct s_mc_stack_frame {
414   /** Instruction pointer */
415   unw_word_t ip;
416   /** Stack pointer */
417   unw_word_t sp;
418   unw_word_t frame_base;
419   dw_frame_t frame;
420   char* frame_name;
421   unw_cursor_t unw_cursor;
422 } s_mc_stack_frame_t, *mc_stack_frame_t;
423
424 typedef struct s_mc_snapshot_stack{
425   xbt_dynar_t local_variables;
426   mc_unw_context_t context;
427   xbt_dynar_t stack_frames; // mc_stack_frame_t
428   int process_index;
429 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
430
431 typedef struct s_mc_global_t {
432   mc_snapshot_t snapshot;
433   int prev_pair;
434   char *prev_req;
435   int initial_communications_pattern_done;
436   int recv_deterministic;
437   int send_deterministic;
438   char *send_diff;
439   char *recv_diff;
440 }s_mc_global_t, *mc_global_t;
441
442 static const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
443
444 XBT_INTERNAL mc_snapshot_t MC_take_snapshot(int num_state);
445 XBT_INTERNAL void MC_restore_snapshot(mc_snapshot_t);
446
447 XBT_INTERNAL void mc_restore_page_snapshot_region(
448   mc_process_t process,
449   void* start_addr, simgrid::mc::PerPageCopy const& pagenos);
450
451 MC_SHOULD_BE_INTERNAL const void* MC_region_read_fragmented(
452   mc_mem_region_t region, void* target, const void* addr, size_t size);
453
454 MC_SHOULD_BE_INTERNAL int MC_snapshot_region_memcmp(
455   const void* addr1, mc_mem_region_t region1,
456   const void* addr2, mc_mem_region_t region2, size_t size);
457 XBT_INTERNAL int MC_snapshot_memcmp(
458   const void* addr1, mc_snapshot_t snapshot1,
459   const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
460
461 static inline __attribute__ ((always_inline))
462 const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot)
463 {
464   if(snapshot==NULL)
465       xbt_die("snapshot is NULL");
466   return mc_model_checker->process().get_heap()->breakval;
467 }
468
469 /** @brief Read memory from a snapshot region
470  *
471  *  @param addr    Process (non-snapshot) address of the data
472  *  @param region  Snapshot memory region where the data is located
473  *  @param target  Buffer to store the value
474  *  @param size    Size of the data to read in bytes
475  *  @return Pointer where the data is located (target buffer of original location)
476  */
477 static inline __attribute__((always_inline))
478 const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr, size_t size)
479 {
480   xbt_assert(region);
481
482   uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
483
484   xbt_assert(mc_region_contain(region, addr),
485     "Trying to read out of the region boundary.");
486
487   switch (region->storage_type()) {
488   case MC_REGION_STORAGE_TYPE_NONE:
489   default:
490     xbt_die("Storage type not supported");
491
492   case MC_REGION_STORAGE_TYPE_FLAT:
493     return (char*) region->flat_data().data() + offset;
494
495   case MC_REGION_STORAGE_TYPE_CHUNKED:
496     {
497       // Last byte of the region:
498       void* end = (char*) addr + size - 1;
499       if (mc_same_page(addr, end) ) {
500         // The memory is contained in a single page:
501         return mc_translate_address_region_chunked((uintptr_t) addr, region);
502       } else {
503         // The memory spans several pages:
504         return MC_region_read_fragmented(region, target, addr, size);
505       }
506     }
507
508   // We currently do not pass the process_index to this function so we assume
509   // that the privatized region has been resolved in the callers:
510   case MC_REGION_STORAGE_TYPE_PRIVATIZED:
511     xbt_die("Storage type not supported");
512   }
513 }
514
515 static inline __attribute__ ((always_inline))
516 void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
517 {
518   void* res;
519   return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
520 }
521
522 SG_END_DECL()
523
524 #endif