Logo AND Algorithmique Numérique Distribuée

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