Logo AND Algorithmique Numérique Distribuée

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