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_private.h"
14 #include "mc_page_store.h"
16 /** @brief Find the snapshoted region from a pointer
19 * @param snapshot Snapshot
20 * @param Snapshot region in the snapshot this pointer belongs to
21 * (or NULL if it does not belong to any snapshot region)
23 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot, int process_index)
26 if (snapshot->privatization_regions) {
28 if (process_index < 0) {
30 mc_mem_region_t region = snapshot->privatization_regions[0];
31 if( mc_region_contain(region, addr) ) {
32 xbt_die("Missing process index");
36 if (process_index >= smpi_process_count()) {
37 xbt_die("Invalid process index");
40 mc_mem_region_t region = snapshot->privatization_regions[process_index];
41 if( mc_region_contain(region, addr) ) {
49 for (size_t i = 0; i != NB_REGIONS; ++i) {
50 mc_mem_region_t region = snapshot->regions[i];
51 if ( region && mc_region_contain(region, addr) ) {
59 /** @brief Read memory from a snapshot region broken across fragmented pages
61 * @param addr Process (non-snapshot) address of the data
62 * @param region Snapshot memory region where the data is located
63 * @param target Buffer to store the value
64 * @param size Size of the data to read in bytes
65 * @return Pointer where the data is located (target buffer of original location)
67 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size)
69 // Last byte of the memory area:
70 void* end = (char*) addr + size - 1;
72 // Page of the last byte of the memory area:
73 size_t page_end = mc_page_number(NULL, end);
78 xbt_die("Missing destination buffer for fragmented memory access");
82 while (mc_page_number(NULL, addr) != page_end) {
83 void* snapshot_addr = mc_translate_address_region((uintptr_t) addr, region);
84 void* next_page = mc_page_from_number(NULL, mc_page_number(NULL, addr) + 1);
85 size_t readable = (char*) next_page - (char*) addr;
86 memcpy(dest, snapshot_addr, readable);
87 addr = (char*) addr + readable;
88 dest = (char*) dest + readable;
93 void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, region);
94 memcpy(dest, snapshot_addr, size);
99 /** @brief Read memory from a snapshot
101 * @param addr Process (non-snapshot) address of the data
102 * @param snapshot Snapshot (or NULL is no snapshot)
103 * @param target Buffer to store the value
104 * @param size Size of the data to read in bytes
105 * @return Pointer where the data is located (target buffer or original location)
107 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, int process_index, void* target, size_t size)
110 mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot, process_index);
111 return mc_snapshot_read_region(addr, region, target, size);
117 /** Compare memory between snapshots (with known regions)
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
125 int mc_snapshot_region_memcmp(
126 void* addr1, mc_mem_region_t region1,
127 void* addr2, mc_mem_region_t region2,
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 void* buffer1a = (region1==NULL || region1->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
134 void* buffer2a = (region2==NULL || region2->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
135 void* buffer1 = mc_snapshot_read_region(addr1, region1, buffer1a, size);
136 void* buffer2 = mc_snapshot_read_region(addr2, region2, buffer2a, size);
138 if (buffer1 == buffer2) {
141 res = memcmp(buffer1, buffer2, size);
150 /** Compare memory between snapshots
152 * @param addr1 Address in the first snapshot
153 * @param snapshot1 First snapshot
154 * @param addr2 Address in the second snapshot
155 * @param snapshot2 Second snapshot
156 * @return same as memcmp
158 int mc_snapshot_memcmp(
159 void* addr1, mc_snapshot_t snapshot1,
160 void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
162 mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
163 mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
164 return mc_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
172 #include <sys/mman.h>
174 #include "mc/mc_private.h"
176 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
178 static inline void init_memory(void* mem, size_t size)
180 char* dest = (char*) mem;
181 for (int i=0; i!=size; ++i) {
182 dest[i] = rand() & 255;
186 static void test_snapshot(bool sparse_checkpoint);
188 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
194 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
200 static void test_snapshot(bool sparse_checkpoint) {
202 xbt_test_add("Initialisation");
203 _sg_mc_soft_dirty = 0;
204 _sg_mc_sparse_checkpoint = sparse_checkpoint;
205 xbt_assert(xbt_pagesize == getpagesize());
206 xbt_assert(1 << xbt_pagebits == xbt_pagesize);
207 mc_model_checker = xbt_new0(s_mc_model_checker_t, 1);
208 mc_model_checker->pages = mc_pages_store_new();
210 for(int n=1; n!=256; ++n) {
212 // Store region page(s):
213 size_t byte_size = n * xbt_pagesize;
214 void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
215 xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
217 // Init memory and take snapshots:
218 init_memory(source, byte_size);
219 mc_mem_region_t region0 = mc_region_new_sparse(0, source, source, byte_size, NULL);
220 for(int i=0; i<n; i+=2) {
221 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
223 mc_mem_region_t region = mc_region_new_sparse(0, source, source, byte_size, NULL);
225 void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
226 xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
228 xbt_test_add("Reading whole region data for %i page(s)", n);
229 void* read = mc_snapshot_read_region(source, region, destination, byte_size);
230 xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in mc_snapshot_read_region()");
232 xbt_test_add("Reading parts of region data for %i page(s)", n);
233 for(int j=0; j!=100; ++j) {
234 size_t offset = rand() % byte_size;
235 size_t size = rand() % (byte_size - offset);
236 void* read = mc_snapshot_read_region((char*) source+offset, region, destination, size);
237 xbt_test_assert(!memcmp((char*) source+offset, read, size),
238 "Mismatch in mc_snapshot_read_region()");
241 xbt_test_add("Compare whole region data for %i page(s)", n);
242 xbt_test_assert(!mc_snapshot_region_memcmp(source, NULL, source, region, byte_size),
243 "Mismatch in mc_snapshot_region_memcmp() for the whole region");
244 xbt_test_assert(mc_snapshot_region_memcmp(source, region0, source, region, byte_size),
245 "Unexpected match in mc_snapshot_region_memcmp() with previous snapshot");
247 xbt_test_add("Compare parts of region data for %i page(s) with current value", n);
248 for(int j=0; j!=100; ++j) {
249 size_t offset = rand() % byte_size;
250 size_t size = rand() % (byte_size - offset);
251 xbt_test_assert(!mc_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
252 "Mismatch in mc_snapshot_region_memcmp()");
255 xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
256 for(int j=0; j!=100; ++j) {
257 size_t offset = rand() % byte_size;
258 size_t size = rand() % (byte_size - offset);
259 xbt_test_assert(!mc_snapshot_region_memcmp((char*) source+offset, region, (char*) source+offset, region, size),
260 "Mismatch in mc_snapshot_region_memcmp()");
264 xbt_test_add("Read pointer for %i page(s)", n);
265 memcpy(source, &mc_model_checker, sizeof(void*));
266 mc_mem_region_t region2 = mc_region_new_sparse(0, source, source, byte_size, NULL);
267 xbt_test_assert(mc_snapshot_read_pointer_region(source, region2) == mc_model_checker,
268 "Mismtach in mc_snapshot_read_pointer_region()");
269 MC_region_destroy(region2);
272 MC_region_destroy(region);
273 MC_region_destroy(region0);
274 munmap(destination, byte_size);
275 munmap(source, byte_size);
278 mc_pages_store_delete(mc_model_checker->pages);
279 xbt_free(mc_model_checker);
280 mc_model_checker = NULL;
283 #endif /* SIMGRID_TEST */