Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f19fb12fa6a69103149506add137f75c9b706d26
[simgrid.git] / src / mc / mc_snapshot.h
1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_SNAPSHOT_H
7 #define SIMGRID_MC_SNAPSHOT_H
8
9 #include <cstdint>
10 #include <cstddef>
11
12 #include <vector>
13 #include <set>
14 #include <string>
15 #include <memory>
16
17 #include <sys/types.h> // off_t
18
19 #include "src/xbt/mmalloc/mmprivate.h"
20 #include <xbt/asserts.h>
21 #include <xbt/base.h>
22
23 #include "src/mc/mc_forward.hpp"
24 #include "src/mc/ModelChecker.hpp"
25 #include "src/mc/PageStore.hpp"
26 #include "src/mc/AddressSpace.hpp"
27 #include "src/mc/mc_unw.h"
28 #include "src/mc/RegionSnapshot.hpp"
29
30 SG_BEGIN_DECL()
31
32 // ***** Snapshot region
33
34 XBT_PRIVATE void mc_region_restore_sparse(simgrid::mc::Process* process, mc_mem_region_t reg);
35
36 static XBT_ALWAYS_INLINE void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
37 {
38   auto split = simgrid::mc::mmu::split(addr - region->start().address());
39   auto pageno = split.first;
40   auto offset = split.second;
41   const void* snapshot_page = region->page_data().page(pageno);
42   return (char*) snapshot_page + offset;
43 }
44
45 static XBT_ALWAYS_INLINE void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
46 {
47   switch (region->storage_type()) {
48   case simgrid::mc::StorageType::NoData:
49   default:
50     xbt_die("Storage type not supported");
51
52   case simgrid::mc::StorageType::Flat:
53     {
54       uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
55       return (void *) ((uintptr_t) region->flat_data().get() + offset);
56     }
57
58   case simgrid::mc::StorageType::Chunked:
59     return mc_translate_address_region_chunked(addr, region);
60
61   case simgrid::mc::StorageType::Privatized:
62     {
63       xbt_assert(process_index >=0,
64         "Missing process index for privatized region");
65       xbt_assert((size_t) process_index < region->privatized_data().size(),
66         "Out of range process index");
67       simgrid::mc::RegionSnapshot& subregion= region->privatized_data()[process_index];
68       return mc_translate_address_region(addr, &subregion, process_index);
69     }
70   }
71 }
72
73 XBT_PRIVATE mc_mem_region_t mc_get_snapshot_region(
74   const void* addr, const simgrid::mc::Snapshot *snapshot, int process_index);
75
76 }
77
78 // ***** MC Snapshot
79
80 /** Ignored data
81  *
82  *  Some parts of the snapshot are ignored by zeroing them out: the real
83  *  values is stored here.
84  * */
85 typedef struct s_mc_snapshot_ignored_data {
86   void* start;
87   std::vector<char> data;
88 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
89
90 typedef struct s_fd_infos{
91   std::string filename;
92   int number;
93   off_t current_position;
94   int flags;
95 }s_fd_infos_t, *fd_infos_t;
96
97 /** Information about a given stack frame
98  *
99  */
100 typedef struct s_mc_stack_frame {
101   /** Instruction pointer */
102   unw_word_t ip;
103   /** Stack pointer */
104   unw_word_t sp;
105   unw_word_t frame_base;
106   simgrid::mc::Frame* frame;
107   std::string frame_name;
108   unw_cursor_t unw_cursor;
109 } s_mc_stack_frame_t, *mc_stack_frame_t;
110
111 typedef struct s_local_variable{
112   simgrid::mc::Frame* subprogram;
113   unsigned long ip;
114   std::string name;
115   simgrid::mc::Type* type;
116   void *address;
117   int region;
118 } s_local_variable_t, *local_variable_t;
119
120 typedef struct XBT_PRIVATE s_mc_snapshot_stack {
121   std::vector<s_local_variable> local_variables;
122   simgrid::mc::UnwindContext context;
123   std::vector<s_mc_stack_frame_t> stack_frames;
124   int process_index;
125 } s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
126
127 namespace simgrid {
128 namespace mc {
129
130 class XBT_PRIVATE Snapshot final : public AddressSpace {
131 public:
132   Snapshot(Process* process, int num_state);
133   ~Snapshot() = default;
134   const void* read_bytes(void* buffer, std::size_t size,
135     RemotePtr<void> address, int process_index = ProcessIndexAny,
136     ReadOptions options = ReadOptions::none()) const override;
137 public: // To be private
138   int num_state;
139   std::size_t heap_bytes_used;
140   std::vector<std::unique_ptr<s_mc_mem_region_t>> snapshot_regions;
141   std::set<pid_t> enabled_processes;
142   int privatization_index;
143   std::vector<std::size_t> stack_sizes;
144   std::vector<s_mc_snapshot_stack_t> stacks;
145   std::vector<simgrid::mc::IgnoredHeapRegion> to_ignore;
146   std::uint64_t hash;
147   std::vector<s_mc_snapshot_ignored_data> ignored_data;
148   std::vector<s_fd_infos_t> current_fds;
149 };
150
151 }
152 }
153
154 extern "C" {
155
156 static XBT_ALWAYS_INLINE mc_mem_region_t mc_get_region_hinted(void* addr, simgrid::mc::Snapshot* snapshot,
157                                                               int process_index, mc_mem_region_t region)
158 {
159   if (region->contain(simgrid::mc::remote(addr)))
160     return region;
161   else
162     return mc_get_snapshot_region(addr, snapshot, process_index);
163 }
164
165 static const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot);
166
167 }
168
169 #ifdef __cplusplus
170
171 namespace simgrid {
172 namespace mc {
173
174 XBT_PRIVATE std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state);
175 XBT_PRIVATE void restore_snapshot(std::shared_ptr<simgrid::mc::Snapshot> snapshot);
176
177 }
178 }
179
180 #endif
181
182 extern "C" {
183
184 XBT_PRIVATE void mc_restore_page_snapshot_region(
185   simgrid::mc::Process* process,
186   void* start_addr, simgrid::mc::ChunkedData const& pagenos);
187
188 const void* MC_region_read_fragmented(
189   mc_mem_region_t region, void* target, const void* addr, std::size_t size);
190
191 int MC_snapshot_region_memcmp(
192   const void* addr1, mc_mem_region_t region1,
193   const void* addr2, mc_mem_region_t region2, std::size_t size);
194 XBT_PRIVATE int MC_snapshot_memcmp(
195   const void* addr1, simgrid::mc::Snapshot* snapshot1,
196   const void* addr2, simgrid::mc::Snapshot* snapshot2, int process_index, std::size_t size);
197
198 static XBT_ALWAYS_INLINE const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot)
199 {
200   if(snapshot==nullptr)
201       xbt_die("snapshot is nullptr");
202   return mc_model_checker->process().get_heap()->breakval;
203 }
204
205 /** @brief Read memory from a snapshot region
206  *
207  *  @param addr    Process (non-snapshot) address of the data
208  *  @param region  Snapshot memory region where the data is located
209  *  @param target  Buffer to store the value
210  *  @param size    Size of the data to read in bytes
211  *  @return Pointer where the data is located (target buffer of original location)
212  */
213 static XBT_ALWAYS_INLINE const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr,
214                                                     std::size_t size)
215 {
216   xbt_assert(region);
217
218   std::uintptr_t offset = (std::uintptr_t)addr - (std::uintptr_t)region->start().address();
219
220   xbt_assert(region->contain(simgrid::mc::remote(addr)), "Trying to read out of the region boundary.");
221
222   switch (region->storage_type()) {
223   case simgrid::mc::StorageType::NoData:
224   default:
225     xbt_die("Storage type not supported");
226
227   case simgrid::mc::StorageType::Flat:
228     return (char*) region->flat_data().get() + offset;
229
230   case simgrid::mc::StorageType::Chunked:
231     {
232       // Last byte of the region:
233       void* end = (char*) addr + size - 1;
234       if (simgrid::mc::mmu::sameChunk((std::uintptr_t) addr, (std::uintptr_t) end) ) {
235         // The memory is contained in a single page:
236         return mc_translate_address_region_chunked((uintptr_t) addr, region);
237       } else {
238         // The memory spans several pages:
239         return MC_region_read_fragmented(region, target, addr, size);
240       }
241     }
242
243   // We currently do not pass the process_index to this function so we assume
244   // that the privatized region has been resolved in the callers:
245   case simgrid::mc::StorageType::Privatized:
246     xbt_die("Storage type not supported");
247   }
248 }
249
250 static XBT_ALWAYS_INLINE void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
251 {
252   void* res;
253   return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
254 }
255
256 SG_END_DECL()
257
258 #endif