Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move libsosp into its own directory
[simgrid.git] / src / mc / sosp / mc_snapshot.cpp
1 /* Copyright (c) 2014-2018. 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 #include <cstddef>
7
8 #include <memory>
9 #include <utility>
10
11 #include "xbt/asserts.h"
12 #include "xbt/sysdep.h"
13
14 #include "src/internal_config.h"
15 #include "src/smpi/include/private.hpp"
16
17 #include "src/mc/mc_mmu.hpp"
18 #include "src/mc/mc_private.hpp"
19 #include "src/mc/sosp/PageStore.hpp"
20 #include "src/mc/sosp/mc_snapshot.hpp"
21
22 /** @brief Find the snapshoted region from a pointer
23  *
24  *  @param addr     Pointer
25  *  @param snapshot Snapshot
26  *  @param Snapshot region in the snapshot this pointer belongs to
27  *         (or nullptr if it does not belong to any snapshot region)
28  * */
29 mc_mem_region_t mc_get_snapshot_region(const void* addr, const simgrid::mc::Snapshot* snapshot, int process_index)
30 {
31   size_t n = snapshot->snapshot_regions.size();
32   for (size_t i = 0; i != n; ++i) {
33     mc_mem_region_t region = snapshot->snapshot_regions[i].get();
34     if (not(region && region->contain(simgrid::mc::remote(addr))))
35       continue;
36
37     if (region->storage_type() == simgrid::mc::StorageType::Privatized) {
38 #if HAVE_SMPI
39       // Use the current process index of the snapshot:
40       if (process_index == simgrid::mc::ProcessIndexDisabled)
41         process_index = snapshot->privatization_index;
42       if (process_index < 0)
43         xbt_die("Missing process index");
44       if (process_index >= (int)region->privatized_data().size())
45         xbt_die("Invalid process index");
46       simgrid::mc::RegionSnapshot& priv_region = region->privatized_data()[process_index];
47       xbt_assert(priv_region.contain(simgrid::mc::remote(addr)));
48       return &priv_region;
49 #else
50       xbt_die("Privatized region in a non SMPI build (this should not happen)");
51 #endif
52     }
53
54     return region;
55   }
56
57   return nullptr;
58 }
59
60 /** @brief Read memory from a snapshot region broken across fragmented pages
61  *
62  *  @param addr    Process (non-snapshot) address of the data
63  *  @param region  Snapshot memory region where the data is located
64  *  @param target  Buffer to store the value
65  *  @param size    Size of the data to read in bytes
66  *  @return Pointer where the data is located (target buffer of original location)
67  */
68 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, size_t size)
69 {
70   // Last byte of the memory area:
71   void* end = (char*)addr + size - 1;
72
73   // TODO, we assume the chunks are aligned to natural chunk boundaries.
74   // We should remove this assumption.
75
76   // Page of the last byte of the memory area:
77   size_t page_end = simgrid::mc::mmu::split((std::uintptr_t)end).first;
78
79   void* dest = target;
80
81   if (dest == nullptr)
82     xbt_die("Missing destination buffer for fragmented memory access");
83
84   // Read each page:
85   while (simgrid::mc::mmu::split((std::uintptr_t)addr).first != page_end) {
86     void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
87     void* next_page     = (void*)simgrid::mc::mmu::join(simgrid::mc::mmu::split((std::uintptr_t)addr).first + 1, 0);
88     size_t readable     = (char*)next_page - (char*)addr;
89     memcpy(dest, snapshot_addr, readable);
90     addr = (char*)addr + readable;
91     dest = (char*)dest + readable;
92     size -= readable;
93   }
94
95   // Read the end:
96   void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
97   memcpy(dest, snapshot_addr, size);
98
99   return target;
100 }
101
102 /** Compare memory between snapshots (with known regions)
103  *
104  * @param addr1 Address in the first snapshot
105  * @param snapshot2 Region of the address in the first snapshot
106  * @param addr2 Address in the second snapshot
107  * @param snapshot2 Region of the address in the second snapshot
108  * @return same as memcmp
109  * */
110 int MC_snapshot_region_memcmp(const void* addr1, mc_mem_region_t region1, const void* addr2, mc_mem_region_t region2,
111                               size_t size)
112 {
113   // Using alloca() for large allocations may trigger stack overflow:
114   // use malloc if the buffer is too big.
115   bool stack_alloc = size < 64;
116   void* buffer1a   = nullptr;
117   void* buffer2a   = nullptr;
118   if (region1 != nullptr && region1->storage_type() != simgrid::mc::StorageType::Flat)
119     buffer1a = stack_alloc ? alloca(size) : ::operator new(size);
120   if (region2 != nullptr && region2->storage_type() != simgrid::mc::StorageType::Flat)
121     buffer2a = stack_alloc ? alloca(size) : ::operator new(size);
122   const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
123   const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
124   int res;
125   if (buffer1 == buffer2)
126     res = 0;
127   else
128     res = memcmp(buffer1, buffer2, size);
129   if (not stack_alloc) {
130     ::operator delete(buffer1a);
131     ::operator delete(buffer2a);
132   }
133   return res;
134 }
135
136 namespace simgrid {
137 namespace mc {
138
139 Snapshot::Snapshot(RemoteClient* process, int _num_state)
140     : AddressSpace(process)
141     , num_state(_num_state)
142     , heap_bytes_used(0)
143     , enabled_processes()
144     , privatization_index(0)
145     , hash(0)
146 {
147 }
148
149 const void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, int process_index,
150                                  ReadOptions options) const
151 {
152   mc_mem_region_t region = mc_get_snapshot_region((void*)address.address(), this, process_index);
153   if (region) {
154     const void* res = MC_region_read(region, buffer, (void*)address.address(), size);
155     if (buffer == res || options & ReadOptions::lazy())
156       return res;
157     else {
158       memcpy(buffer, res, size);
159       return buffer;
160     }
161   } else
162     return this->process()->read_bytes(buffer, size, address, process_index, options);
163 }
164
165 } // namespace mc
166 } // namespace simgrid
167
168 #ifdef SIMGRID_TEST
169
170 #include <cstdlib>
171 #include <cstring>
172
173 #include <sys/mman.h>
174
175 #include "src/mc/mc_config.hpp"
176 #include "src/mc/mc_mmu.hpp"
177 #include "src/mc/mc_private.hpp"
178 #include "src/mc/mc_snapshot.hpp"
179
180 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
181
182 static inline void init_memory(void* mem, size_t size)
183 {
184   char* dest = (char*)mem;
185   for (size_t i = 0; i < size; ++i) {
186     dest[i] = rand() & 255;
187   }
188 }
189
190 static void test_snapshot(bool sparse_checkpoint);
191
192 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
193 {
194   test_snapshot(0);
195 }
196
197 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
198 {
199   test_snapshot(1);
200 }
201
202 static void test_snapshot(bool sparse_checkpoint)
203 {
204
205   xbt_test_add("Initialization");
206   _sg_mc_sparse_checkpoint = sparse_checkpoint;
207   xbt_assert(xbt_pagesize == getpagesize());
208   xbt_assert(1 << xbt_pagebits == xbt_pagesize);
209
210   std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(getpid(), -1));
211   process->init();
212   mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
213
214   for (int n = 1; n != 256; ++n) {
215
216     // Store region page(s):
217     size_t byte_size = n * xbt_pagesize;
218     void* source     = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
219     xbt_assert(source != MAP_FAILED, "Could not allocate source memory");
220
221     // Init memory and take snapshots:
222     init_memory(source, byte_size);
223     simgrid::mc::RegionSnapshot region0 =
224         simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
225     for (int i = 0; i < n; i += 2) {
226       init_memory((char*)source + i * xbt_pagesize, xbt_pagesize);
227     }
228     simgrid::mc::RegionSnapshot region =
229         simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
230
231     void* destination = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
232     xbt_assert(source != MAP_FAILED, "Could not allocate destination memory");
233
234     xbt_test_add("Reading whole region data for %i page(s)", n);
235     const void* read = MC_region_read(&region, destination, source, byte_size);
236     xbt_test_assert(not memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
237
238     xbt_test_add("Reading parts of region data for %i page(s)", n);
239     for (int j = 0; j != 100; ++j) {
240       size_t offset    = rand() % byte_size;
241       size_t size      = rand() % (byte_size - offset);
242       const void* read = MC_region_read(&region, destination, (const char*)source + offset, size);
243       xbt_test_assert(not memcmp((char*)source + offset, read, size), "Mismatch in MC_region_read()");
244     }
245
246     xbt_test_add("Compare whole region data for %i page(s)", n);
247
248     xbt_test_assert(MC_snapshot_region_memcmp(source, &region0, source, &region, byte_size),
249                     "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
250
251     xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
252     for (int j = 0; j != 100; ++j) {
253       size_t offset = rand() % byte_size;
254       size_t size   = rand() % (byte_size - offset);
255       xbt_test_assert(
256           not MC_snapshot_region_memcmp((char*)source + offset, &region, (char*)source + offset, &region, size),
257           "Mismatch in MC_snapshot_region_memcmp()");
258     }
259
260     if (n == 1) {
261       xbt_test_add("Read pointer for %i page(s)", n);
262       memcpy(source, &mc_model_checker, sizeof(void*));
263       simgrid::mc::RegionSnapshot region2 =
264           simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
265       xbt_test_assert(MC_region_read_pointer(&region2, source) == mc_model_checker,
266                       "Mismtach in MC_region_read_pointer()");
267     }
268
269     munmap(destination, byte_size);
270     munmap(source, byte_size);
271   }
272
273   delete mc_model_checker;
274   mc_model_checker = nullptr;
275 }
276
277 #endif /* SIMGRID_TEST */