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)
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)))
32 if (region->storage_type == MC_REGION_STORAGE_TYPE_PRIVATIZED) {
34 if (process_index < 0) {
35 xbt_die("Missing process index");
37 if (process_index >= region->privatized.regions_count) {
38 xbt_die("Invalid process index");
40 mc_mem_region_t priv_region = region->privatized.regions[process_index];
41 xbt_assert(mc_region_contain(priv_region, addr));
44 xbt_die("Privatized region in a non SMPI build (this should not happen)");
54 /** @brief Read memory from a snapshot region broken across fragmented pages
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)
62 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size)
64 // Last byte of the memory area:
65 void* end = (char*) addr + size - 1;
67 // Page of the last byte of the memory area:
68 size_t page_end = mc_page_number(NULL, end);
73 xbt_die("Missing destination buffer for fragmented memory access");
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;
88 void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, region);
89 memcpy(dest, snapshot_addr, size);
94 /** @brief Read memory from a snapshot
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)
102 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, int process_index, void* target, size_t size)
105 mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot, process_index);
106 return mc_snapshot_read_region(addr, region, target, size);
112 /** Compare memory between snapshots (with known regions)
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
120 int mc_snapshot_region_memcmp(
121 void* addr1, mc_mem_region_t region1,
122 void* addr2, mc_mem_region_t region2,
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);
135 if (buffer1 == buffer2) {
138 res = memcmp(buffer1, buffer2, size);
147 /** Compare memory between snapshots
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
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)
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);
169 #include <sys/mman.h>
171 #include "mc/mc_private.h"
172 #include "mc/mc_snapshot.h"
173 #include "mc/mc_mmu.h"
175 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
177 static inline void init_memory(void* mem, size_t size)
179 char* dest = (char*) mem;
180 for (int i=0; i!=size; ++i) {
181 dest[i] = rand() & 255;
185 static void test_snapshot(bool sparse_checkpoint);
187 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
193 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
199 static void test_snapshot(bool sparse_checkpoint) {
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();
209 for(int n=1; n!=256; ++n) {
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");
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);
222 mc_mem_region_t region = mc_region_new_sparse(MC_REGION_TYPE_UNKNOWN, source, source, byte_size, NULL);
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");
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()");
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()");
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");
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()");
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()");
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);
271 MC_region_destroy(region);
272 MC_region_destroy(region0);
273 munmap(destination, byte_size);
274 munmap(source, byte_size);
277 mc_pages_store_delete(mc_model_checker->pages);
278 xbt_free(mc_model_checker);
279 mc_model_checker = NULL;
282 #endif /* SIMGRID_TEST */