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 "mc_private.h"
11 #include "mc_page_store.h"
13 /** @brief Find the snapshoted region from a pointer
16 * @param snapshot Snapshot
17 * @param Snapshot region in the snapshot this pointer belongs to
18 * (or NULL if it does not belong to any snapshot region)
20 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot)
22 for (size_t i = 0; i != NB_REGIONS; ++i) {
23 mc_mem_region_t region = snapshot->regions[i];
24 void* start = region->start_addr;
25 void* end = (char*) start + region->size;
27 if (addr >= start && addr < end) {
35 /** @brief Read memory from a snapshot region broken across fragmented pages
37 * @param addr Process (non-snapshot) address of the data
38 * @param region Snapshot memory region where the data is located
39 * @param target Buffer to store the value
40 * @param size Size of the data to read in bytes
41 * @return Pointer where the data is located (target buffer of original location)
43 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size)
45 // Last byte of the memory area:
46 void* end = (char*) addr + size - 1;
48 // Page of the last byte of the memory area:
49 size_t page_end = mc_page_number(NULL, end);
54 xbt_die("Missing destination buffer for fragmented memory access");
58 while (mc_page_number(NULL, addr) != page_end) {
59 void* snapshot_addr = mc_translate_address_region((uintptr_t) addr, region);
60 void* next_page = mc_page_from_number(NULL, mc_page_number(NULL, addr) + 1);
61 size_t readable = (char*) next_page - (char*) addr;
62 memcpy(dest, snapshot_addr, readable);
63 addr = (char*) addr + readable;
64 dest = (char*) dest + readable;
69 void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, region);
70 memcpy(dest, snapshot_addr, size);
75 /** @brief Read memory from a snapshot
77 * @param addr Process (non-snapshot) address of the data
78 * @param snapshot Snapshot (or NULL is no snapshot)
79 * @param target Buffer to store the value
80 * @param size Size of the data to read in bytes
81 * @return Pointer where the data is located (target buffer or original location)
83 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, void* target, size_t size)
86 mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot);
87 return mc_snapshot_read_region(addr, region, target, size);
93 /** Compare memory between snapshots (with known regions)
95 * @param addr1 Address in the first snapshot
96 * @param snapshot2 Region of the address in the first snapshot
97 * @param addr2 Address in the second snapshot
98 * @param snapshot2 Region of the address in the second snapshot
99 * @return same as memcmp
101 int mc_snapshot_region_memcmp(
102 void* addr1, mc_mem_region_t region1,
103 void* addr2, mc_mem_region_t region2, size_t size)
105 // Using alloca() for large allocations may trigger stack overflow:
106 // use malloc if the buffer is too big.
107 bool stack_alloc = size < 64;
108 void* buffer1a = (region1==NULL || region1->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
109 void* buffer2a = (region2==NULL || region2->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
110 void* buffer1 = mc_snapshot_read_region(addr1, region1, buffer1a, size);
111 void* buffer2 = mc_snapshot_read_region(addr2, region2, buffer2a, size);
113 if (buffer1 == buffer2) {
116 res = memcmp(buffer1, buffer2, size);
125 /** Compare memory between snapshots
127 * @param addr1 Address in the first snapshot
128 * @param snapshot1 First snapshot
129 * @param addr2 Address in the second snapshot
130 * @param snapshot2 Second snapshot
131 * @return same as memcmp
133 int mc_snapshot_memcmp(
134 void* addr1, mc_snapshot_t snapshot1,
135 void* addr2, mc_snapshot_t snapshot2, size_t size)
137 mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1);
138 mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2);
139 return mc_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
147 #include <sys/mman.h>
149 #include "mc/mc_private.h"
151 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
153 static inline void init_memory(void* mem, size_t size)
155 char* dest = (char*) mem;
156 for (int i=0; i!=size; ++i) {
157 dest[i] = rand() & 255;
161 static void test_snapshot(bool sparse_checkpoint);
163 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
169 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
175 static void test_snapshot(bool sparse_checkpoint) {
177 xbt_test_add("Initialisation");
178 _sg_mc_soft_dirty = 0;
179 _sg_mc_sparse_checkpoint = sparse_checkpoint;
180 xbt_assert(xbt_pagesize == getpagesize());
181 xbt_assert(1 << xbt_pagebits == xbt_pagesize);
182 mc_model_checker = xbt_new0(s_mc_model_checker_t, 1);
183 mc_model_checker->pages = mc_pages_store_new();
185 for(int n=1; n!=256; ++n) {
187 // Store region page(s):
188 size_t byte_size = n * xbt_pagesize;
189 void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
190 xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
192 // Init memory and take snapshots:
193 init_memory(source, byte_size);
194 mc_mem_region_t region0 = mc_region_new_sparse(0, source, byte_size, NULL);
195 for(int i=0; i<n; i+=2) {
196 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
198 mc_mem_region_t region = mc_region_new_sparse(0, source, byte_size, NULL);
200 void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
201 xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
203 xbt_test_add("Reading whole region data for %i page(s)", n);
204 void* read = mc_snapshot_read_region(source, region, destination, byte_size);
205 xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in mc_snapshot_read_region()");
207 xbt_test_add("Reading parts of region data for %i page(s)", n);
208 for(int j=0; j!=100; ++j) {
209 size_t offset = rand() % byte_size;
210 size_t size = rand() % (byte_size - offset);
211 void* read = mc_snapshot_read_region((char*) source+offset, region, destination, size);
212 xbt_test_assert(!memcmp((char*) source+offset, read, size),
213 "Mismatch in mc_snapshot_read_region()");
216 xbt_test_add("Compare whole region data for %i page(s)", n);
217 xbt_test_assert(!mc_snapshot_region_memcmp(source, NULL, source, region, byte_size),
218 "Mismatch in mc_snapshot_region_memcmp() for the whole region");
219 xbt_test_assert(mc_snapshot_region_memcmp(source, region0, source, region, byte_size),
220 "Unexpected match in mc_snapshot_region_memcmp() with previous snapshot");
222 xbt_test_add("Compare parts of region data for %i page(s) with current value", n);
223 for(int j=0; j!=100; ++j) {
224 size_t offset = rand() % byte_size;
225 size_t size = rand() % (byte_size - offset);
226 xbt_test_assert(!mc_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
227 "Mismatch in mc_snapshot_region_memcmp()");
230 xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
231 for(int j=0; j!=100; ++j) {
232 size_t offset = rand() % byte_size;
233 size_t size = rand() % (byte_size - offset);
234 xbt_test_assert(!mc_snapshot_region_memcmp((char*) source+offset, region, (char*) source+offset, region, size),
235 "Mismatch in mc_snapshot_region_memcmp()");
239 xbt_test_add("Read pointer for %i page(s)", n);
240 memcpy(source, &mc_model_checker, sizeof(void*));
241 mc_mem_region_t region2 = mc_region_new_sparse(0, source, byte_size, NULL);
242 xbt_test_assert(mc_snapshot_read_pointer_region(source, region2) == mc_model_checker,
243 "Mismtach in mc_snapshot_read_pointer_region()");
244 MC_region_destroy(region2);
247 MC_region_destroy(region);
248 MC_region_destroy(region0);
249 munmap(destination, byte_size);
250 munmap(source, byte_size);
253 mc_pages_store_delete(mc_model_checker->pages);
254 xbt_free(mc_model_checker);
255 mc_model_checker = NULL;
258 #endif /* SIMGRID_TEST */