Logo AND Algorithmique Numérique Distribuée

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