Logo AND Algorithmique Numérique Distribuée

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