Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bc1b5535d1949d134305dabb788c4f18d144da98
[simgrid.git] / src / mc / mc_snapshot.c
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_private.h"
13 #include "mc_mmu.h"
14 #include "mc_page_store.h"
15
16 /** @brief Find the snapshoted region from a pointer
17  *
18  *  @param addr     Pointer
19  *  @param snapshot Snapshot
20  *  @param Snapshot region in the snapshot this pointer belongs to
21  *         (or NULL if it does not belong to any snapshot region)
22  * */
23 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot, int process_index)
24 {
25 #ifdef HAVE_SMPI
26   if (snapshot->privatization_regions) {
27
28     if (process_index < 0) {
29
30       mc_mem_region_t region = snapshot->privatization_regions[0];
31       if( mc_region_contain(region, addr) ) {
32         xbt_die("Missing process index");
33       }
34
35     } else {
36       if (process_index >= smpi_process_count()) {
37         xbt_die("Invalid process index");
38       }
39
40       mc_mem_region_t region = snapshot->privatization_regions[process_index];
41       if( mc_region_contain(region, addr) ) {
42         return region;
43       }
44
45     }
46   }
47 #endif
48
49   for (size_t i = 0; i != NB_REGIONS; ++i) {
50     mc_mem_region_t region = snapshot->regions[i];
51     if ( region && mc_region_contain(region, addr) ) {
52       return region;
53     }
54   }
55
56   return NULL;
57 }
58
59 /** @brief Read memory from a snapshot region broken across fragmented pages
60  *
61  *  @param addr    Process (non-snapshot) address of the data
62  *  @param region  Snapshot memory region where the data is located
63  *  @param target  Buffer to store the value
64  *  @param size    Size of the data to read in bytes
65  *  @return Pointer where the data is located (target buffer of original location)
66  */
67 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size)
68 {
69   // Last byte of the memory area:
70   void* end = (char*) addr + size - 1;
71
72   // Page of the last byte of the memory area:
73   size_t page_end = mc_page_number(NULL, end);
74
75   void* dest = target;
76
77   if (dest==NULL) {
78     xbt_die("Missing destination buffer for fragmented memory access");
79   }
80
81   // Read each page:
82   while (mc_page_number(NULL, addr) != page_end) {
83     void* snapshot_addr = mc_translate_address_region((uintptr_t) addr, region);
84     void* next_page = mc_page_from_number(NULL, mc_page_number(NULL, addr) + 1);
85     size_t readable = (char*) next_page - (char*) addr;
86     memcpy(dest, snapshot_addr, readable);
87     addr = (char*) addr + readable;
88     dest = (char*) dest + readable;
89     size -= readable;
90   }
91
92   // Read the end:
93   void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, region);
94   memcpy(dest, snapshot_addr, size);
95
96   return target;
97 }
98
99 /** @brief Read memory from a snapshot
100  *
101  *  @param addr     Process (non-snapshot) address of the data
102  *  @param snapshot Snapshot (or NULL is no snapshot)
103  *  @param target   Buffer to store the value
104  *  @param size     Size of the data to read in bytes
105  *  @return Pointer where the data is located (target buffer or original location)
106  */
107 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, int process_index, void* target, size_t size)
108 {
109   if (snapshot) {
110     mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot, process_index);
111     return mc_snapshot_read_region(addr, region, target, size);
112   } else {
113     return addr;
114   }
115 }
116
117 /** Compare memory between snapshots (with known regions)
118  *
119  * @param addr1 Address in the first snapshot
120  * @param snapshot2 Region of the address in the first snapshot
121  * @param addr2 Address in the second snapshot
122  * @param snapshot2 Region of the address in the second snapshot
123  * @return same as memcmp
124  * */
125 int mc_snapshot_region_memcmp(
126   void* addr1, mc_mem_region_t region1,
127   void* addr2, mc_mem_region_t region2,
128   size_t size)
129 {
130   // Using alloca() for large allocations may trigger stack overflow:
131   // use malloc if the buffer is too big.
132   bool stack_alloc = size < 64;
133   void* buffer1a = (region1==NULL || region1->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
134   void* buffer2a = (region2==NULL || region2->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
135   void* buffer1 = mc_snapshot_read_region(addr1, region1, buffer1a, size);
136   void* buffer2 = mc_snapshot_read_region(addr2, region2, buffer2a, size);
137   int res;
138   if (buffer1 == buffer2) {
139     res = 0;
140   } else {
141     res = memcmp(buffer1, buffer2, size);
142   }
143   if (!stack_alloc) {
144     free(buffer1a);
145     free(buffer2a);
146   }
147   return res;
148 }
149
150 /** Compare memory between snapshots
151  *
152  * @param addr1 Address in the first snapshot
153  * @param snapshot1 First snapshot
154  * @param addr2 Address in the second snapshot
155  * @param snapshot2 Second snapshot
156  * @return same as memcmp
157  * */
158 int mc_snapshot_memcmp(
159   void* addr1, mc_snapshot_t snapshot1,
160   void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
161 {
162   mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
163   mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
164   return mc_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
165 }
166
167 #ifdef SIMGRID_TEST
168
169 #include <string.h>
170 #include <stdlib.h>
171
172 #include <sys/mman.h>
173
174 #include "mc/mc_private.h"
175
176 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
177
178 static inline void init_memory(void* mem, size_t size)
179 {
180   char* dest = (char*) mem;
181   for (int i=0; i!=size; ++i) {
182     dest[i] = rand() & 255;
183   }
184 }
185
186 static void test_snapshot(bool sparse_checkpoint);
187
188 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
189 {
190   test_snapshot(1);
191 }
192
193
194 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
195 {
196   test_snapshot(0);
197 }
198
199
200 static void test_snapshot(bool sparse_checkpoint) {
201
202   xbt_test_add("Initialisation");
203   _sg_mc_soft_dirty = 0;
204   _sg_mc_sparse_checkpoint = sparse_checkpoint;
205   xbt_assert(xbt_pagesize == getpagesize());
206   xbt_assert(1 << xbt_pagebits == xbt_pagesize);
207   mc_model_checker = xbt_new0(s_mc_model_checker_t, 1);
208   mc_model_checker->pages = mc_pages_store_new();
209
210   for(int n=1; n!=256; ++n) {
211
212     // Store region page(s):
213     size_t byte_size = n * xbt_pagesize;
214     void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
215     xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
216
217     // Init memory and take snapshots:
218     init_memory(source, byte_size);
219     mc_mem_region_t region0 = mc_region_new_sparse(0, source, source, byte_size, NULL);
220     for(int i=0; i<n; i+=2) {
221       init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
222     }
223     mc_mem_region_t region = mc_region_new_sparse(0, source, source, byte_size, NULL);
224
225     void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
226     xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
227
228     xbt_test_add("Reading whole region data for %i page(s)", n);
229     void* read = mc_snapshot_read_region(source, region, destination, byte_size);
230     xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in mc_snapshot_read_region()");
231
232     xbt_test_add("Reading parts of region data for %i page(s)", n);
233     for(int j=0; j!=100; ++j) {
234       size_t offset = rand() % byte_size;
235       size_t size = rand() % (byte_size - offset);
236       void* read = mc_snapshot_read_region((char*) source+offset, region, destination, size);
237       xbt_test_assert(!memcmp((char*) source+offset, read, size),
238         "Mismatch in mc_snapshot_read_region()");
239     }
240
241     xbt_test_add("Compare whole region data for %i page(s)", n);
242     xbt_test_assert(!mc_snapshot_region_memcmp(source, NULL, source, region, byte_size),
243       "Mismatch in mc_snapshot_region_memcmp() for the whole region");
244     xbt_test_assert(mc_snapshot_region_memcmp(source, region0, source, region, byte_size),
245       "Unexpected match in mc_snapshot_region_memcmp() with previous snapshot");
246
247     xbt_test_add("Compare parts of region data for %i page(s) with current value", 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       xbt_test_assert(!mc_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
252         "Mismatch in mc_snapshot_region_memcmp()");
253     }
254
255     xbt_test_add("Compare parts of region data for %i page(s) with itself", 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, region, (char*) source+offset, region, size),
260         "Mismatch in mc_snapshot_region_memcmp()");
261     }
262
263     if (n==1) {
264       xbt_test_add("Read pointer for %i page(s)", n);
265       memcpy(source, &mc_model_checker, sizeof(void*));
266       mc_mem_region_t region2 = mc_region_new_sparse(0, source, source, byte_size, NULL);
267       xbt_test_assert(mc_snapshot_read_pointer_region(source, region2) == mc_model_checker,
268         "Mismtach in mc_snapshot_read_pointer_region()");
269       MC_region_destroy(region2);
270     }
271
272     MC_region_destroy(region);
273     MC_region_destroy(region0);
274     munmap(destination, byte_size);
275     munmap(source, byte_size);
276   }
277
278   mc_pages_store_delete(mc_model_checker->pages);
279   xbt_free(mc_model_checker);
280   mc_model_checker = NULL;
281 }
282
283 #endif /* SIMGRID_TEST */
284