Logo AND Algorithmique Numérique Distribuée

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