Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix realloc() in mc_process.cpp
[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 #include "RegionSnapshot.hpp"
25
26 SG_BEGIN_DECL()
27
28 // ***** Snapshot region
29
30 XBT_INTERNAL void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t reg);
31
32 static inline __attribute__((always_inline))
33 void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
34 {
35   size_t pageno = mc_page_number((void*)region->start().address(), (void*) addr);
36   const void* snapshot_page =
37     region->page_data().page(pageno);
38   return (char*) snapshot_page + mc_page_offset((void*) addr);
39 }
40
41 static inline __attribute__((always_inline))
42 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
43 {
44   switch (region->storage_type()) {
45   case simgrid::mc::StorageType::NoData:
46   default:
47     xbt_die("Storage type not supported");
48
49   case simgrid::mc::StorageType::Flat:
50     {
51       uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
52       return (void *) ((uintptr_t) region->flat_data().data() + offset);
53     }
54
55   case simgrid::mc::StorageType::Chunked:
56     return mc_translate_address_region_chunked(addr, region);
57
58   case simgrid::mc::StorageType::Privatized:
59     {
60       xbt_assert(process_index >=0,
61         "Missing process index for privatized region");
62       xbt_assert((size_t) process_index < region->privatized_data().size(),
63         "Out of range process index");
64       simgrid::mc::RegionSnapshot& subregion= region->privatized_data()[process_index];
65       return mc_translate_address_region(addr, &subregion, process_index);
66     }
67   }
68 }
69
70 XBT_INTERNAL mc_mem_region_t mc_get_snapshot_region(
71   const void* addr, const s_mc_snapshot_t *snapshot, int process_index);
72
73 // ***** MC Snapshot
74
75 /** Ignored data
76  *
77  *  Some parts of the snapshot are ignored by zeroing them out: the real
78  *  values is stored here.
79  * */
80 typedef struct s_mc_snapshot_ignored_data {
81   void* start;
82   size_t size;
83   void* data;
84 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
85
86 typedef struct s_fd_infos{
87   char *filename;
88   int number;
89   off_t current_position;
90   int flags;
91 }s_fd_infos_t, *fd_infos_t;
92
93 }
94
95 namespace simgrid {
96 namespace mc {
97
98 class Snapshot : public AddressSpace {
99 public:
100   Snapshot();
101   ~Snapshot();
102   const void* read_bytes(void* buffer, std::size_t size,
103     remote_ptr<void> address, int process_index = ProcessIndexAny,
104     ReadMode mode = Normal) const MC_OVERRIDE;
105 public: // To be private
106   mc_process_t process;
107   int num_state;
108   size_t heap_bytes_used;
109   mc_mem_region_t* snapshot_regions;
110   size_t snapshot_regions_count;
111   xbt_dynar_t enabled_processes;
112   int privatization_index;
113   size_t *stack_sizes;
114   xbt_dynar_t stacks;
115   xbt_dynar_t to_ignore;
116   uint64_t hash;
117   xbt_dynar_t ignored_data;
118   int total_fd;
119   fd_infos_t *current_fd;
120 };
121
122 }
123 }
124
125 extern "C" {
126
127 static inline __attribute__ ((always_inline))
128 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
129 {
130   if (region->contain(simgrid::mc::remote(addr)))
131     return region;
132   else
133     return mc_get_snapshot_region(addr, snapshot, process_index);
134 }
135
136 /** Information about a given stack frame
137  *
138  */
139 typedef struct s_mc_stack_frame {
140   /** Instruction pointer */
141   unw_word_t ip;
142   /** Stack pointer */
143   unw_word_t sp;
144   unw_word_t frame_base;
145   dw_frame_t frame;
146   char* frame_name;
147   unw_cursor_t unw_cursor;
148 } s_mc_stack_frame_t, *mc_stack_frame_t;
149
150 typedef struct s_mc_snapshot_stack{
151   xbt_dynar_t local_variables;
152   mc_unw_context_t context;
153   xbt_dynar_t stack_frames; // mc_stack_frame_t
154   int process_index;
155 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
156
157 typedef struct s_mc_global_t {
158   mc_snapshot_t snapshot;
159   int prev_pair;
160   char *prev_req;
161   int initial_communications_pattern_done;
162   int recv_deterministic;
163   int send_deterministic;
164   char *send_diff;
165   char *recv_diff;
166 }s_mc_global_t, *mc_global_t;
167
168 static const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
169
170 XBT_INTERNAL mc_snapshot_t MC_take_snapshot(int num_state);
171 XBT_INTERNAL void MC_restore_snapshot(mc_snapshot_t);
172
173 XBT_INTERNAL void mc_restore_page_snapshot_region(
174   mc_process_t process,
175   void* start_addr, simgrid::mc::PerPageCopy const& pagenos);
176
177 MC_SHOULD_BE_INTERNAL const void* MC_region_read_fragmented(
178   mc_mem_region_t region, void* target, const void* addr, size_t size);
179
180 MC_SHOULD_BE_INTERNAL int MC_snapshot_region_memcmp(
181   const void* addr1, mc_mem_region_t region1,
182   const void* addr2, mc_mem_region_t region2, size_t size);
183 XBT_INTERNAL int MC_snapshot_memcmp(
184   const void* addr1, mc_snapshot_t snapshot1,
185   const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
186
187 static inline __attribute__ ((always_inline))
188 const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot)
189 {
190   if(snapshot==NULL)
191       xbt_die("snapshot is NULL");
192   return mc_model_checker->process().get_heap()->breakval;
193 }
194
195 /** @brief Read memory from a snapshot region
196  *
197  *  @param addr    Process (non-snapshot) address of the data
198  *  @param region  Snapshot memory region where the data is located
199  *  @param target  Buffer to store the value
200  *  @param size    Size of the data to read in bytes
201  *  @return Pointer where the data is located (target buffer of original location)
202  */
203 static inline __attribute__((always_inline))
204 const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr, size_t size)
205 {
206   xbt_assert(region);
207
208   uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
209
210   xbt_assert(region->contain(simgrid::mc::remote(addr)),
211     "Trying to read out of the region boundary.");
212
213   switch (region->storage_type()) {
214   case simgrid::mc::StorageType::NoData:
215   default:
216     xbt_die("Storage type not supported");
217
218   case simgrid::mc::StorageType::Flat:
219     return (char*) region->flat_data().data() + offset;
220
221   case simgrid::mc::StorageType::Chunked:
222     {
223       // Last byte of the region:
224       void* end = (char*) addr + size - 1;
225       if (mc_same_page(addr, end) ) {
226         // The memory is contained in a single page:
227         return mc_translate_address_region_chunked((uintptr_t) addr, region);
228       } else {
229         // The memory spans several pages:
230         return MC_region_read_fragmented(region, target, addr, size);
231       }
232     }
233
234   // We currently do not pass the process_index to this function so we assume
235   // that the privatized region has been resolved in the callers:
236   case simgrid::mc::StorageType::Privatized:
237     xbt_die("Storage type not supported");
238   }
239 }
240
241 static inline __attribute__ ((always_inline))
242 void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
243 {
244   void* res;
245   return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
246 }
247
248 SG_END_DECL()
249
250 #endif