Logo AND Algorithmique Numérique Distribuée

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