Logo AND Algorithmique Numérique Distribuée

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