Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into mc
[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 /** @brief Read memory from a snapshot
101  *
102  *  @param addr     Process (non-snapshot) address of the data
103  *  @param snapshot Snapshot (or NULL is no snapshot)
104  *  @param target   Buffer to store the value
105  *  @param size     Size of the data to read in bytes
106  *  @return Pointer where the data is located (target buffer or original location)
107  */
108 const void* MC_snapshot_read(
109   mc_snapshot_t snapshot, adress_space_read_flags_t flags,
110   void* target, const void* addr, size_t size, int process_index)
111 {
112   mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot, process_index);
113   if (region)
114     return MC_region_read(region, target, addr, size);
115   else
116     return MC_process_read(snapshot->process, flags, target, addr, size, process_index);
117 }
118
119 /** Compare memory between snapshots (with known regions)
120  *
121  * @param addr1 Address in the first snapshot
122  * @param snapshot2 Region of the address in the first snapshot
123  * @param addr2 Address in the second snapshot
124  * @param snapshot2 Region of the address in the second snapshot
125  * @return same as memcmp
126  * */
127 int MC_snapshot_region_memcmp(
128   const void* addr1, mc_mem_region_t region1,
129   const void* addr2, mc_mem_region_t region2,
130   size_t size)
131 {
132   // Using alloca() for large allocations may trigger stack overflow:
133   // use malloc if the buffer is too big.
134   bool stack_alloc = size < 64;
135   const bool region1_need_buffer = region1==NULL || region1->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
136   const bool region2_need_buffer = region2==NULL || region2->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
137   void* buffer1a = region1_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
138   void* buffer2a = region2_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
139   const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
140   const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
141   int res;
142   if (buffer1 == buffer2) {
143     res = 0;
144   } else {
145     res = memcmp(buffer1, buffer2, size);
146   }
147   if (!stack_alloc) {
148     free(buffer1a);
149     free(buffer2a);
150   }
151   return res;
152 }
153
154 /** Compare memory between snapshots
155  *
156  * @param addr1 Address in the first snapshot
157  * @param snapshot1 First snapshot
158  * @param addr2 Address in the second snapshot
159  * @param snapshot2 Second snapshot
160  * @return same as memcmp
161  * */
162 int MC_snapshot_memcmp(
163   const void* addr1, mc_snapshot_t snapshot1,
164   const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
165 {
166   mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
167   mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
168   return MC_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
169 }
170
171 #ifdef SIMGRID_TEST
172
173 #include <string.h>
174 #include <stdlib.h>
175
176 #include <sys/mman.h>
177
178 #include "mc/mc_private.h"
179 #include "mc/mc_snapshot.h"
180 #include "mc/mc_mmu.h"
181
182 extern "C" {
183
184 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
185
186 static inline void init_memory(void* mem, size_t size)
187 {
188   char* dest = (char*) mem;
189   for (size_t i = 0; i < size; ++i) {
190     dest[i] = rand() & 255;
191   }
192 }
193
194 static void test_snapshot(bool sparse_checkpoint);
195
196 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
197 {
198   test_snapshot(1);
199 }
200
201
202 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
203 {
204   test_snapshot(0);
205 }
206
207
208 static void test_snapshot(bool sparse_checkpoint) {
209
210   xbt_test_add("Initialisation");
211   _sg_mc_sparse_checkpoint = sparse_checkpoint;
212   xbt_assert(xbt_pagesize == getpagesize());
213   xbt_assert(1 << xbt_pagebits == xbt_pagesize);
214   mc_model_checker = new ::simgrid::mc::ModelChecker(getpid(), -1);
215
216   for(int n=1; n!=256; ++n) {
217
218     // Store region page(s):
219     size_t byte_size = n * xbt_pagesize;
220     void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
221     xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
222
223     // Init memory and take snapshots:
224     init_memory(source, byte_size);
225     mc_mem_region_t region0 = mc_region_new_sparse(
226       MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
227     for(int i=0; i<n; i+=2) {
228       init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
229     }
230     mc_mem_region_t region = mc_region_new_sparse(
231       MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
232
233     void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
234     xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
235
236     xbt_test_add("Reading whole region data for %i page(s)", n);
237     const void* read = MC_region_read(region, source, destination, byte_size);
238     xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
239
240     xbt_test_add("Reading parts of region data for %i page(s)", n);
241     for(int j=0; j!=100; ++j) {
242       size_t offset = rand() % byte_size;
243       size_t size = rand() % (byte_size - offset);
244       const void* read = MC_region_read(region, destination, (const char*) source+offset, size);
245       xbt_test_assert(!memcmp((char*) source+offset, read, size),
246         "Mismatch in MC_region_read()");
247     }
248
249     xbt_test_add("Compare whole region data for %i page(s)", n);
250     xbt_test_assert(!MC_snapshot_region_memcmp(source, NULL, source, region, byte_size),
251       "Mismatch in MC_snapshot_region_memcmp() for the whole region");
252     xbt_test_assert(MC_snapshot_region_memcmp(source, region0, source, region, byte_size),
253       "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
254
255     xbt_test_add("Compare parts of region data for %i page(s) with current value", n);
256     for(int j=0; j!=100; ++j) {
257       size_t offset = rand() % byte_size;
258       size_t size = rand() % (byte_size - offset);
259       xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
260         "Mismatch in MC_snapshot_region_memcmp()");
261     }
262
263     xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
264     for(int j=0; j!=100; ++j) {
265       size_t offset = rand() % byte_size;
266       size_t size = rand() % (byte_size - offset);
267       xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, region, (char*) source+offset, region, size),
268         "Mismatch in MC_snapshot_region_memcmp()");
269     }
270
271     if (n==1) {
272       xbt_test_add("Read pointer for %i page(s)", n);
273       memcpy(source, &mc_model_checker, sizeof(void*));
274       mc_mem_region_t region2 = mc_region_new_sparse(
275         MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
276       xbt_test_assert(MC_region_read_pointer(region2, source) == mc_model_checker,
277         "Mismtach in MC_region_read_pointer()");
278       MC_region_destroy(region2);
279     }
280
281     MC_region_destroy(region);
282     MC_region_destroy(region0);
283     munmap(destination, byte_size);
284     munmap(source, byte_size);
285   }
286
287   delete mc_model_checker;
288   mc_model_checker = NULL;
289 }
290
291 }
292
293 #endif /* SIMGRID_TEST */
294
295 }