1 /* Copyright (c) 2014. The SimGrid Team.
2 * All rights reserved. */
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. */
9 #include "internal_config.h"
10 #include "smpi/private.h"
12 #include "mc_snapshot.h"
13 #include "mc_private.h"
15 #include "mc_page_store.h"
17 /** @brief Find the snapshoted region from a 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)
24 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot, int process_index)
27 if (snapshot->privatization_regions) {
29 if (process_index < 0) {
31 mc_mem_region_t region = snapshot->privatization_regions[0];
32 if( mc_region_contain(region, addr) ) {
33 xbt_die("Missing process index");
37 if (process_index >= smpi_process_count()) {
38 xbt_die("Invalid process index");
41 mc_mem_region_t region = snapshot->privatization_regions[process_index];
42 if( mc_region_contain(region, addr) ) {
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) ) {
60 /** @brief Read memory from a snapshot region broken across fragmented pages
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)
68 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size)
70 // Last byte of the memory area:
71 void* end = (char*) addr + size - 1;
73 // Page of the last byte of the memory area:
74 size_t page_end = mc_page_number(NULL, end);
79 xbt_die("Missing destination buffer for fragmented memory access");
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;
94 void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, region);
95 memcpy(dest, snapshot_addr, size);
100 /** @brief Read memory from a snapshot
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)
108 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, int process_index, void* target, size_t size)
111 mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot, process_index);
112 return mc_snapshot_read_region(addr, region, target, size);
118 /** Compare memory between snapshots (with known regions)
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
126 int mc_snapshot_region_memcmp(
127 void* addr1, mc_mem_region_t region1,
128 void* addr2, mc_mem_region_t region2,
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);
139 if (buffer1 == buffer2) {
142 res = memcmp(buffer1, buffer2, size);
151 /** Compare memory between snapshots
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
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)
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);
173 #include <sys/mman.h>
175 #include "mc/mc_private.h"
176 #include "mc/mc_snapshot.h"
177 #include "mc/mc_mmu.h"
179 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
181 static inline void init_memory(void* mem, size_t size)
183 char* dest = (char*) mem;
184 for (int i=0; i!=size; ++i) {
185 dest[i] = rand() & 255;
189 static void test_snapshot(bool sparse_checkpoint);
191 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
197 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
203 static void test_snapshot(bool sparse_checkpoint) {
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();
213 for(int n=1; n!=256; ++n) {
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");
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);
226 mc_mem_region_t region = mc_region_new_sparse(0, source, source, byte_size, NULL);
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");
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()");
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()");
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");
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()");
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()");
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);
275 MC_region_destroy(region);
276 MC_region_destroy(region0);
277 munmap(destination, byte_size);
278 munmap(source, byte_size);
281 mc_pages_store_delete(mc_model_checker->pages);
282 xbt_free(mc_model_checker);
283 mc_model_checker = NULL;
286 #endif /* SIMGRID_TEST */