Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Make C++ classes out of addres_space, process, snapshot
[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(const void* addr, mc_snapshot_t snapshot, int process_index)
27 {
28   size_t n = snapshot->snapshot_regions_count;
29   for (size_t i = 0; i != n; ++i) {
30     mc_mem_region_t region = snapshot->snapshot_regions[i];
31     if (!(region && mc_region_contain(region, addr)))
32       continue;
33
34     if (region->storage_type == MC_REGION_STORAGE_TYPE_PRIVATIZED) {
35 #ifdef HAVE_SMPI
36       // Use the current process index of the snapshot:
37       if (process_index == MC_PROCESS_INDEX_DISABLED) {
38         process_index = snapshot->privatization_index;
39       }
40       if (process_index < 0) {
41         xbt_die("Missing process index");
42       }
43       if (process_index >= (int) region->privatized.regions_count) {
44         xbt_die("Invalid process index");
45       }
46       mc_mem_region_t priv_region = region->privatized.regions[process_index];
47       xbt_assert(mc_region_contain(priv_region, 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 NULL;
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   // Page of the last byte of the memory area:
74   size_t page_end = mc_page_number(NULL, end);
75
76   void* dest = target;
77
78   if (dest==NULL) {
79     xbt_die("Missing destination buffer for fragmented memory access");
80   }
81
82   // Read each page:
83   while (mc_page_number(NULL, addr) != page_end) {
84     void* snapshot_addr = mc_translate_address_region((uintptr_t) addr, region);
85     void* next_page = mc_page_from_number(NULL, mc_page_number(NULL, addr) + 1);
86     size_t readable = (char*) next_page - (char*) addr;
87     memcpy(dest, snapshot_addr, readable);
88     addr = (char*) addr + readable;
89     dest = (char*) dest + readable;
90     size -= readable;
91   }
92
93   // Read the end:
94   void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, region);
95   memcpy(dest, snapshot_addr, size);
96
97   return target;
98 }
99
100 /** Compare memory between snapshots (with known regions)
101  *
102  * @param addr1 Address in the first snapshot
103  * @param snapshot2 Region of the address in the first snapshot
104  * @param addr2 Address in the second snapshot
105  * @param snapshot2 Region of the address in the second snapshot
106  * @return same as memcmp
107  * */
108 int MC_snapshot_region_memcmp(
109   const void* addr1, mc_mem_region_t region1,
110   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   const bool region1_need_buffer = region1==NULL || region1->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
117   const bool region2_need_buffer = region2==NULL || region2->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
118   void* buffer1a = region1_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
119   void* buffer2a = region2_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
120   const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
121   const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
122   int res;
123   if (buffer1 == buffer2) {
124     res = 0;
125   } else {
126     res = memcmp(buffer1, buffer2, size);
127   }
128   if (!stack_alloc) {
129     free(buffer1a);
130     free(buffer2a);
131   }
132   return res;
133 }
134
135 /** Compare memory between snapshots
136  *
137  * @param addr1 Address in the first snapshot
138  * @param snapshot1 First snapshot
139  * @param addr2 Address in the second snapshot
140  * @param snapshot2 Second snapshot
141  * @return same as memcmp
142  * */
143 int MC_snapshot_memcmp(
144   const void* addr1, mc_snapshot_t snapshot1,
145   const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
146 {
147   mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
148   mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
149   return MC_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
150 }
151
152 namespace simgrid {
153 namespace mc {
154
155 Snapshot::Snapshot() :
156   process(nullptr),
157   num_state(0),
158   heap_bytes_used(0),
159   snapshot_regions(nullptr),
160   snapshot_regions_count(0),
161   enabled_processes(0),
162   privatization_index(0),
163   stack_sizes(nullptr),
164   stacks(nullptr),
165   to_ignore(nullptr),
166   hash(0),
167   ignored_data(nullptr),
168   total_fd(0),
169   current_fd(nullptr)
170 {
171
172 }
173 Snapshot::~Snapshot()
174 {
175   for (size_t i = 0; i < this->snapshot_regions_count; i++) {
176     MC_region_destroy(this->snapshot_regions[i]);
177   }
178   xbt_free(this->snapshot_regions);
179   xbt_free(this->stack_sizes);
180   xbt_dynar_free(&(this->stacks));
181   xbt_dynar_free(&(this->to_ignore));
182   xbt_dynar_free(&this->ignored_data);
183 }
184
185 const void* Snapshot::read_bytes(void* buffer, std::size_t size,
186   std::uint64_t address, int process_index,
187   AddressSpace::ReadMode mode)
188 {
189   mc_mem_region_t region = mc_get_snapshot_region((void*)address, this, process_index);
190   if (region)
191     return MC_region_read(region, buffer, (void*)address, size);
192   else
193     return MC_process_read(this->process, mode, buffer, (void*)address, size, process_index);
194 }
195
196 }
197 }
198
199 #ifdef SIMGRID_TEST
200
201 #include <string.h>
202 #include <stdlib.h>
203
204 #include <sys/mman.h>
205
206 #include "mc/mc_private.h"
207 #include "mc/mc_snapshot.h"
208 #include "mc/mc_mmu.h"
209
210 extern "C" {
211
212 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
213
214 static inline void init_memory(void* mem, size_t size)
215 {
216   char* dest = (char*) mem;
217   for (size_t i = 0; i < size; ++i) {
218     dest[i] = rand() & 255;
219   }
220 }
221
222 static void test_snapshot(bool sparse_checkpoint);
223
224 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
225 {
226   test_snapshot(0);
227 }
228
229 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
230 {
231   test_snapshot(1);
232 }
233
234 static void test_snapshot(bool sparse_checkpoint) {
235
236   xbt_test_add("Initialisation");
237   _sg_mc_sparse_checkpoint = sparse_checkpoint;
238   xbt_assert(xbt_pagesize == getpagesize());
239   xbt_assert(1 << xbt_pagebits == xbt_pagesize);
240   mc_model_checker = new ::simgrid::mc::ModelChecker(getpid(), -1);
241
242   for(int n=1; n!=256; ++n) {
243
244     // Store region page(s):
245     size_t byte_size = n * xbt_pagesize;
246     void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
247     xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
248
249     // Init memory and take snapshots:
250     init_memory(source, byte_size);
251     mc_mem_region_t region0 = mc_region_new_sparse(
252       MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
253     for(int i=0; i<n; i+=2) {
254       init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
255     }
256     mc_mem_region_t region = mc_region_new_sparse(
257       MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
258
259     void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
260     xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
261
262     xbt_test_add("Reading whole region data for %i page(s)", n);
263     const void* read = MC_region_read(region, destination, source, byte_size);
264     xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
265
266     xbt_test_add("Reading parts of region data for %i page(s)", n);
267     for(int j=0; j!=100; ++j) {
268       size_t offset = rand() % byte_size;
269       size_t size = rand() % (byte_size - offset);
270       const void* read = MC_region_read(region, destination, (const char*) source+offset, size);
271       xbt_test_assert(!memcmp((char*) source+offset, read, size),
272         "Mismatch in MC_region_read()");
273     }
274
275     xbt_test_add("Compare whole region data for %i page(s)", n);
276     xbt_test_assert(!MC_snapshot_region_memcmp(source, NULL, source, region, byte_size),
277       "Mismatch in MC_snapshot_region_memcmp() for the whole region");
278     xbt_test_assert(MC_snapshot_region_memcmp(source, region0, source, region, byte_size),
279       "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
280
281     xbt_test_add("Compare parts of region data for %i page(s) with current value", n);
282     for(int j=0; j!=100; ++j) {
283       size_t offset = rand() % byte_size;
284       size_t size = rand() % (byte_size - offset);
285       xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
286         "Mismatch in MC_snapshot_region_memcmp()");
287     }
288
289     xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
290     for(int j=0; j!=100; ++j) {
291       size_t offset = rand() % byte_size;
292       size_t size = rand() % (byte_size - offset);
293       xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, region, (char*) source+offset, region, size),
294         "Mismatch in MC_snapshot_region_memcmp()");
295     }
296
297     if (n==1) {
298       xbt_test_add("Read pointer for %i page(s)", n);
299       memcpy(source, &mc_model_checker, sizeof(void*));
300       mc_mem_region_t region2 = mc_region_new_sparse(
301         MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
302       xbt_test_assert(MC_region_read_pointer(region2, source) == mc_model_checker,
303         "Mismtach in MC_region_read_pointer()");
304       MC_region_destroy(region2);
305     }
306
307     MC_region_destroy(region);
308     MC_region_destroy(region0);
309     munmap(destination, byte_size);
310     munmap(source, byte_size);
311   }
312
313   delete mc_model_checker;
314   mc_model_checker = NULL;
315 }
316
317 }
318
319 #endif /* SIMGRID_TEST */
320
321 }