Logo AND Algorithmique Numérique Distribuée

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