Logo AND Algorithmique Numérique Distribuée

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