Logo AND Algorithmique Numérique Distribuée

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