Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simcalls.py: improve indentation of generated files.
[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
73 typedef struct s_fd_infos{
74   std::string filename;
75   int number;
76   off_t current_position;
77   int flags;
78 } s_fd_infos_t;
79
80 /** Information about a given stack frame */
81 typedef struct s_mc_stack_frame {
82   /** Instruction pointer */
83   unw_word_t ip;
84   /** Stack pointer */
85   unw_word_t sp;
86   unw_word_t frame_base;
87   simgrid::mc::Frame* frame;
88   std::string frame_name;
89   unw_cursor_t unw_cursor;
90 } s_mc_stack_frame_t;
91 typedef s_mc_stack_frame_t* mc_stack_frame_t;
92
93 typedef struct s_local_variable{
94   simgrid::mc::Frame* subprogram;
95   unsigned long ip;
96   std::string name;
97   simgrid::mc::Type* type;
98   void *address;
99   int region;
100 } s_local_variable_t;
101 typedef s_local_variable_t* local_variable_t;
102
103 typedef struct XBT_PRIVATE s_mc_snapshot_stack {
104   std::vector<s_local_variable> local_variables;
105   simgrid::mc::UnwindContext context;
106   std::vector<s_mc_stack_frame_t> stack_frames;
107   int process_index;
108 } s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
109
110 namespace simgrid {
111 namespace mc {
112
113 class XBT_PRIVATE Snapshot final : public AddressSpace {
114 public:
115   Snapshot(RemoteClient* process, int num_state);
116   ~Snapshot() = default;
117   const void* read_bytes(void* buffer, std::size_t size,
118     RemotePtr<void> address, int process_index = ProcessIndexAny,
119     ReadOptions options = ReadOptions::none()) const override;
120
121   // To be private
122   int num_state;
123   std::size_t heap_bytes_used;
124   std::vector<std::unique_ptr<s_mc_mem_region_t>> snapshot_regions;
125   std::set<pid_t> enabled_processes;
126   int privatization_index;
127   std::vector<std::size_t> stack_sizes;
128   std::vector<s_mc_snapshot_stack_t> stacks;
129   std::vector<simgrid::mc::IgnoredHeapRegion> to_ignore;
130   std::uint64_t hash;
131   std::vector<s_mc_snapshot_ignored_data> ignored_data;
132   std::vector<s_fd_infos_t> current_fds;
133 };
134
135 }
136 }
137
138 extern "C" {
139
140 static XBT_ALWAYS_INLINE mc_mem_region_t mc_get_region_hinted(void* addr, simgrid::mc::Snapshot* snapshot,
141                                                               int process_index, mc_mem_region_t region)
142 {
143   if (region->contain(simgrid::mc::remote(addr)))
144     return region;
145   else
146     return mc_get_snapshot_region(addr, snapshot, process_index);
147 }
148
149 static const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot);
150
151 }
152
153 #ifdef __cplusplus
154
155 namespace simgrid {
156 namespace mc {
157
158 XBT_PRIVATE std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state);
159 XBT_PRIVATE void restore_snapshot(std::shared_ptr<simgrid::mc::Snapshot> snapshot);
160
161 }
162 }
163
164 #endif
165
166 extern "C" {
167
168 XBT_PRIVATE void mc_restore_page_snapshot_region(simgrid::mc::RemoteClient* process, void* start_addr,
169                                                  simgrid::mc::ChunkedData const& pagenos);
170
171 const void* MC_region_read_fragmented(
172   mc_mem_region_t region, void* target, const void* addr, std::size_t size);
173
174 int MC_snapshot_region_memcmp(
175   const void* addr1, mc_mem_region_t region1,
176   const void* addr2, mc_mem_region_t region2, std::size_t size);
177 XBT_PRIVATE int MC_snapshot_memcmp(
178   const void* addr1, simgrid::mc::Snapshot* snapshot1,
179   const void* addr2, simgrid::mc::Snapshot* snapshot2, int process_index, std::size_t size);
180
181 static XBT_ALWAYS_INLINE const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot)
182 {
183   if(snapshot==nullptr)
184       xbt_die("snapshot is nullptr");
185   return mc_model_checker->process().get_heap()->breakval;
186 }
187
188 /** @brief Read memory from a snapshot region
189  *
190  *  @param addr    Process (non-snapshot) address of the data
191  *  @param region  Snapshot memory region where the data is located
192  *  @param target  Buffer to store the value
193  *  @param size    Size of the data to read in bytes
194  *  @return Pointer where the data is located (target buffer of original location)
195  */
196 static XBT_ALWAYS_INLINE const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr,
197                                                     std::size_t size)
198 {
199   xbt_assert(region);
200
201   std::uintptr_t offset = (std::uintptr_t)addr - (std::uintptr_t)region->start().address();
202
203   xbt_assert(region->contain(simgrid::mc::remote(addr)), "Trying to read out of the region boundary.");
204
205   switch (region->storage_type()) {
206   case simgrid::mc::StorageType::NoData:
207   default:
208     xbt_die("Storage type not supported");
209
210   case simgrid::mc::StorageType::Flat:
211     return (char*) region->flat_data().get() + offset;
212
213   case simgrid::mc::StorageType::Chunked:
214     {
215       // Last byte of the region:
216       void* end = (char*) addr + size - 1;
217       if (simgrid::mc::mmu::sameChunk((std::uintptr_t) addr, (std::uintptr_t) end) ) {
218         // The memory is contained in a single page:
219         return mc_translate_address_region_chunked((uintptr_t) addr, region);
220       } else {
221         // The memory spans several pages:
222         return MC_region_read_fragmented(region, target, addr, size);
223       }
224     }
225
226   // We currently do not pass the process_index to this function so we assume
227   // that the privatized region has been resolved in the callers:
228   case simgrid::mc::StorageType::Privatized:
229     xbt_die("Storage type not supported");
230   }
231 }
232
233 static XBT_ALWAYS_INLINE void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
234 {
235   void* res;
236   return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
237 }
238
239 SG_END_DECL()
240
241 #endif