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(const 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 // Use the current process index of the snapshot:
35 if (process_index == MC_PROCESS_INDEX_DISABLED) {
36 process_index = snapshot->privatization_index;
38 if (process_index < 0) {
39 xbt_die("Missing process index");
41 if (process_index >= region->privatized.regions_count) {
42 xbt_die("Invalid process index");
44 mc_mem_region_t priv_region = region->privatized.regions[process_index];
45 xbt_assert(mc_region_contain(priv_region, addr));
48 xbt_die("Privatized region in a non SMPI build (this should not happen)");
58 /** @brief Read memory from a snapshot region broken across fragmented pages
60 * @param addr Process (non-snapshot) address of the data
61 * @param region Snapshot memory region where the data is located
62 * @param target Buffer to store the value
63 * @param size Size of the data to read in bytes
64 * @return Pointer where the data is located (target buffer of original location)
66 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, size_t size)
68 // Last byte of the memory area:
69 void* end = (char*) addr + size - 1;
71 // Page of the last byte of the memory area:
72 size_t page_end = mc_page_number(NULL, end);
77 xbt_die("Missing destination buffer for fragmented memory access");
81 while (mc_page_number(NULL, addr) != page_end) {
82 void* snapshot_addr = mc_translate_address_region((uintptr_t) addr, region);
83 void* next_page = mc_page_from_number(NULL, mc_page_number(NULL, addr) + 1);
84 size_t readable = (char*) next_page - (char*) addr;
85 memcpy(dest, snapshot_addr, readable);
86 addr = (char*) addr + readable;
87 dest = (char*) dest + readable;
92 void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, region);
93 memcpy(dest, snapshot_addr, size);
98 /** @brief Read memory from a snapshot
100 * @param addr Process (non-snapshot) address of the data
101 * @param snapshot Snapshot (or NULL is no snapshot)
102 * @param target Buffer to store the value
103 * @param size Size of the data to read in bytes
104 * @return Pointer where the data is located (target buffer or original location)
106 const void* MC_snapshot_read(
107 mc_snapshot_t snapshot, e_adress_space_read_flags_t flags,
108 void* target, const void* addr, size_t size, int process_index)
110 mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot, process_index);
112 return MC_region_read(region, target, addr, size);
114 return MC_process_read(snapshot->process, flags, target, addr, size, process_index);
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 const void* addr1, mc_mem_region_t region1,
127 const 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 const bool region1_need_buffer = region1==NULL || region1->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
134 const bool region2_need_buffer = region2==NULL || region2->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
135 void* buffer1a = region1_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
136 void* buffer2a = region2_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
137 const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
138 const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
140 if (buffer1 == buffer2) {
143 res = memcmp(buffer1, buffer2, size);
152 /** Compare memory between snapshots
154 * @param addr1 Address in the first snapshot
155 * @param snapshot1 First snapshot
156 * @param addr2 Address in the second snapshot
157 * @param snapshot2 Second snapshot
158 * @return same as memcmp
160 int MC_snapshot_memcmp(
161 const void* addr1, mc_snapshot_t snapshot1,
162 const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
164 mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
165 mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
166 return MC_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
174 #include <sys/mman.h>
176 #include "mc/mc_private.h"
177 #include "mc/mc_snapshot.h"
178 #include "mc/mc_mmu.h"
180 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
182 static inline void init_memory(void* mem, size_t size)
184 char* dest = (char*) mem;
185 for (int i=0; i!=size; ++i) {
186 dest[i] = rand() & 255;
190 static void test_snapshot(bool sparse_checkpoint);
192 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
198 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
204 static void test_snapshot(bool sparse_checkpoint) {
206 xbt_test_add("Initialisation");
207 _sg_mc_soft_dirty = 0;
208 _sg_mc_sparse_checkpoint = sparse_checkpoint;
209 xbt_assert(xbt_pagesize == getpagesize());
210 xbt_assert(1 << xbt_pagebits == xbt_pagesize);
211 mc_model_checker = xbt_new0(s_mc_model_checker_t, 1);
212 mc_model_checker->pages = mc_pages_store_new();
214 for(int n=1; n!=256; ++n) {
216 // Store region page(s):
217 size_t byte_size = n * xbt_pagesize;
218 void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
219 xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
221 // Init memory and take snapshots:
222 init_memory(source, byte_size);
223 mc_mem_region_t region0 = mc_region_new_sparse(MC_REGION_TYPE_UNKNOWN, source, source, byte_size, NULL);
224 for(int i=0; i<n; i+=2) {
225 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
227 mc_mem_region_t region = mc_region_new_sparse(MC_REGION_TYPE_UNKNOWN, source, source, byte_size, NULL);
229 void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
230 xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
232 xbt_test_add("Reading whole region data for %i page(s)", n);
233 const void* read = MC_region_read(region, source, destination, byte_size);
234 xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
236 xbt_test_add("Reading parts of region data for %i page(s)", n);
237 for(int j=0; j!=100; ++j) {
238 size_t offset = rand() % byte_size;
239 size_t size = rand() % (byte_size - offset);
240 const void* read = MC_region_read(region, destination, (const char*) source+offset, size);
241 xbt_test_assert(!memcmp((char*) source+offset, read, size),
242 "Mismatch in MC_region_read()");
245 xbt_test_add("Compare whole region data for %i page(s)", n);
246 xbt_test_assert(!MC_snapshot_region_memcmp(source, NULL, source, region, byte_size),
247 "Mismatch in MC_snapshot_region_memcmp() for the whole region");
248 xbt_test_assert(MC_snapshot_region_memcmp(source, region0, source, region, byte_size),
249 "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
251 xbt_test_add("Compare parts of region data for %i page(s) with current value", n);
252 for(int j=0; j!=100; ++j) {
253 size_t offset = rand() % byte_size;
254 size_t size = rand() % (byte_size - offset);
255 xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
256 "Mismatch in MC_snapshot_region_memcmp()");
259 xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
260 for(int j=0; j!=100; ++j) {
261 size_t offset = rand() % byte_size;
262 size_t size = rand() % (byte_size - offset);
263 xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, region, (char*) source+offset, region, size),
264 "Mismatch in MC_snapshot_region_memcmp()");
268 xbt_test_add("Read pointer for %i page(s)", n);
269 memcpy(source, &mc_model_checker, sizeof(void*));
270 mc_mem_region_t region2 = mc_region_new_sparse(MC_REGION_TYPE_UNKNOWN, source, source, byte_size, NULL);
271 xbt_test_assert(MC_region_read_pointer(region2, source) == mc_model_checker,
272 "Mismtach in MC_region_read_pointer()");
273 MC_region_destroy(region2);
276 MC_region_destroy(region);
277 MC_region_destroy(region0);
278 munmap(destination, byte_size);
279 munmap(source, byte_size);
282 mc_pages_store_delete(mc_model_checker->pages);
283 xbt_free(mc_model_checker);
284 mc_model_checker = NULL;
287 #endif /* SIMGRID_TEST */