Logo AND Algorithmique Numérique Distribuée

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