Logo AND Algorithmique Numérique Distribuée

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