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"
19 /** @brief Find the snapshoted region from a pointer
22 * @param snapshot Snapshot
23 * @param Snapshot region in the snapshot this pointer belongs to
24 * (or NULL if it does not belong to any snapshot region)
26 mc_mem_region_t mc_get_snapshot_region(const void* addr, mc_snapshot_t snapshot, int process_index)
28 size_t n = snapshot->snapshot_regions_count;
29 for (size_t i = 0; i != n; ++i) {
30 mc_mem_region_t region = snapshot->snapshot_regions[i];
31 if (!(region && mc_region_contain(region, addr)))
34 if (region->storage_type == MC_REGION_STORAGE_TYPE_PRIVATIZED) {
36 // Use the current process index of the snapshot:
37 if (process_index == MC_PROCESS_INDEX_DISABLED) {
38 process_index = snapshot->privatization_index;
40 if (process_index < 0) {
41 xbt_die("Missing process index");
43 if (process_index >= (int) region->privatized.regions_count) {
44 xbt_die("Invalid process index");
46 mc_mem_region_t priv_region = region->privatized.regions[process_index];
47 xbt_assert(mc_region_contain(priv_region, addr));
50 xbt_die("Privatized region in a non SMPI build (this should not happen)");
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 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, 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 const void* MC_snapshot_read(
109 mc_snapshot_t snapshot, adress_space_read_flags_t flags,
110 void* target, const void* addr, size_t size, int process_index)
112 mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot, process_index);
114 return MC_region_read(region, target, addr, size);
116 return MC_process_read(snapshot->process, flags, target, addr, size, process_index);
119 /** Compare memory between snapshots (with known regions)
121 * @param addr1 Address in the first snapshot
122 * @param snapshot2 Region of the address in the first snapshot
123 * @param addr2 Address in the second snapshot
124 * @param snapshot2 Region of the address in the second snapshot
125 * @return same as memcmp
127 int MC_snapshot_region_memcmp(
128 const void* addr1, mc_mem_region_t region1,
129 const void* addr2, mc_mem_region_t region2,
132 // Using alloca() for large allocations may trigger stack overflow:
133 // use malloc if the buffer is too big.
134 bool stack_alloc = size < 64;
135 const bool region1_need_buffer = region1==NULL || region1->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
136 const bool region2_need_buffer = region2==NULL || region2->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
137 void* buffer1a = region1_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
138 void* buffer2a = region2_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
139 const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
140 const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
142 if (buffer1 == buffer2) {
145 res = memcmp(buffer1, buffer2, size);
154 /** Compare memory between snapshots
156 * @param addr1 Address in the first snapshot
157 * @param snapshot1 First snapshot
158 * @param addr2 Address in the second snapshot
159 * @param snapshot2 Second snapshot
160 * @return same as memcmp
162 int MC_snapshot_memcmp(
163 const void* addr1, mc_snapshot_t snapshot1,
164 const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
166 mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
167 mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
168 return MC_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
176 #include <sys/mman.h>
178 #include "mc/mc_private.h"
179 #include "mc/mc_snapshot.h"
180 #include "mc/mc_mmu.h"
184 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
186 static inline void init_memory(void* mem, size_t size)
188 char* dest = (char*) mem;
189 for (size_t i = 0; i < size; ++i) {
190 dest[i] = rand() & 255;
194 static void test_snapshot(bool sparse_checkpoint);
196 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
202 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
208 static void test_snapshot(bool sparse_checkpoint) {
210 xbt_test_add("Initialisation");
211 _sg_mc_soft_dirty = 0;
212 _sg_mc_sparse_checkpoint = sparse_checkpoint;
213 xbt_assert(xbt_pagesize == getpagesize());
214 xbt_assert(1 << xbt_pagebits == xbt_pagesize);
215 mc_model_checker = xbt_new0(s_mc_model_checker_t, 1);
216 mc_model_checker->pages = mc_pages_store_new();
218 for(int n=1; n!=256; ++n) {
220 // Store region page(s):
221 size_t byte_size = n * xbt_pagesize;
222 void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
223 xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
225 // Init memory and take snapshots:
226 init_memory(source, byte_size);
227 mc_mem_region_t region0 = mc_region_new_sparse(MC_REGION_TYPE_UNKNOWN, source, source, byte_size, NULL);
228 for(int i=0; i<n; i+=2) {
229 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
231 mc_mem_region_t region = mc_region_new_sparse(MC_REGION_TYPE_UNKNOWN, source, source, byte_size, NULL);
233 void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
234 xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
236 xbt_test_add("Reading whole region data for %i page(s)", n);
237 const void* read = MC_region_read(region, source, destination, byte_size);
238 xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
240 xbt_test_add("Reading parts of region data for %i page(s)", n);
241 for(int j=0; j!=100; ++j) {
242 size_t offset = rand() % byte_size;
243 size_t size = rand() % (byte_size - offset);
244 const void* read = MC_region_read(region, destination, (const char*) source+offset, size);
245 xbt_test_assert(!memcmp((char*) source+offset, read, size),
246 "Mismatch in MC_region_read()");
249 xbt_test_add("Compare whole region data for %i page(s)", n);
250 xbt_test_assert(!MC_snapshot_region_memcmp(source, NULL, source, region, byte_size),
251 "Mismatch in MC_snapshot_region_memcmp() for the whole region");
252 xbt_test_assert(MC_snapshot_region_memcmp(source, region0, source, region, byte_size),
253 "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
255 xbt_test_add("Compare parts of region data for %i page(s) with current value", 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, NULL, (char*) source+offset, region, size),
260 "Mismatch in MC_snapshot_region_memcmp()");
263 xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
264 for(int j=0; j!=100; ++j) {
265 size_t offset = rand() % byte_size;
266 size_t size = rand() % (byte_size - offset);
267 xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, region, (char*) source+offset, region, size),
268 "Mismatch in MC_snapshot_region_memcmp()");
272 xbt_test_add("Read pointer for %i page(s)", n);
273 memcpy(source, &mc_model_checker, sizeof(void*));
274 mc_mem_region_t region2 = mc_region_new_sparse(MC_REGION_TYPE_UNKNOWN, source, source, byte_size, NULL);
275 xbt_test_assert(MC_region_read_pointer(region2, source) == mc_model_checker,
276 "Mismtach in MC_region_read_pointer()");
277 MC_region_destroy(region2);
280 MC_region_destroy(region);
281 MC_region_destroy(region0);
282 munmap(destination, byte_size);
283 munmap(source, byte_size);
286 mc_pages_store_delete(mc_model_checker->pages);
287 xbt_free(mc_model_checker);
288 mc_model_checker = NULL;
293 #endif /* SIMGRID_TEST */