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 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot)
15 for (size_t i = 0; i != NB_REGIONS; ++i) {
16 mc_mem_region_t region = snapshot->regions[i];
17 void* start = region->start_addr;
18 void* end = (char*) start + region->size;
20 if (addr >= start && addr < end) {
28 /** @brief Read memory from a snapshot region broken across fragmented pages
30 * @param addr Process (non-snapshot) address of the data
31 * @param region Snapshot memory region where the data is located
32 * @param target Buffer to store the value
33 * @param size Size of the data to read in bytes
34 * @return Pointer where the data is located (target buffer of original location)
36 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size)
38 void* end = (char*) addr + size - 1;
39 size_t page_end = mc_page_number(NULL, end);
43 xbt_die("Missing destination buffer for fragmented memory access");
47 while (mc_page_number(NULL, addr) != page_end) {
48 void* snapshot_addr = mc_translate_address_region((uintptr_t) addr, region);
49 void* next_page = mc_page_from_number(NULL, mc_page_number(NULL, addr) + 1);
50 size_t readable = (char*) next_page - (char*) addr;
51 memcpy(dest, snapshot_addr, readable);
52 addr = (char*) addr + readable;
53 dest = (char*) dest + readable;
58 void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, region);
59 memcpy(dest, snapshot_addr, size);
64 /** @brief Read memory from a snapshot
66 * @param addr Process (non-snapshot) address of the data
67 * @param snapshot Snapshot (or NULL is no snapshot)
68 * @param target Buffer to store the value
69 * @param size Size of the data to read in bytes
70 * @return Pointer where the data is located (target buffer of original location)
72 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, void* target, size_t size)
75 mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot);
76 return mc_snapshot_read_region(addr, region, target, size);
82 /** Compare memory between snapshots (with known regions)
84 * @param addr1 Address in the first snapshot
85 * @param snapshot2 Region of the address in the first snapshot
86 * @param addr2 Address in the second snapshot
87 * @param snapshot2 Region of the address in the second snapshot
88 * @return same as memcmp
90 int mc_snapshot_region_memcmp(
91 void* addr1, mc_mem_region_t region1,
92 void* addr2, mc_mem_region_t region2, size_t size)
94 // Using alloca() for large allocations may trigger stack overflow:
95 // use malloc if the buffer is too big.
96 bool stack_alloc = size < 64;
97 void* buffer1a = (region1==NULL || region1->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
98 void* buffer2a = (region2==NULL || region2->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
99 void* buffer1 = mc_snapshot_read_region(addr1, region1, buffer1a, size);
100 void* buffer2 = mc_snapshot_read_region(addr2, region2, buffer2a, size);
102 if (buffer1 == buffer2) {
105 res = memcmp(buffer1, buffer2, size);
114 /** Compare memory between snapshots
116 * @param addr1 Address in the first snapshot
117 * @param snapshot1 First snapshot
118 * @param addr2 Address in the second snapshot
119 * @param snapshot2 Second snapshot
120 * @return same as memcmp
122 int mc_snapshot_memcp(
123 void* addr1, mc_snapshot_t snapshot1,
124 void* addr2, mc_snapshot_t snapshot2, size_t size)
126 mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1);
127 mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2);
128 return mc_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
136 #include <sys/mman.h>
138 #include "mc/mc_private.h"
140 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
142 static inline void init_memory(void* mem, size_t size)
144 char* dest = (char*) mem;
145 for (int i=0; i!=size; ++i) {
146 dest[i] = rand() & 255;
150 static void test_snapshot(bool sparse_checkpoint);
152 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
158 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
164 static void test_snapshot(bool sparse_checkpoint) {
166 xbt_test_add("Initialisation");
167 _sg_mc_soft_dirty = 0;
168 _sg_mc_sparse_checkpoint = sparse_checkpoint;
169 xbt_assert(xbt_pagesize == getpagesize());
170 xbt_assert(1 << xbt_pagebits == xbt_pagesize);
171 mc_model_checker = xbt_new0(s_mc_model_checker_t, 1);
172 mc_model_checker->pages = mc_pages_store_new();
174 for(int n=1; n!=10; ++n) {
176 // Store region page(s):
177 size_t byte_size = n * xbt_pagesize;
178 void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
179 xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
181 // Init memory and take snapshots:
182 init_memory(source, byte_size);
183 mc_mem_region_t region0 = mc_region_new_sparse(0, source, byte_size, NULL);
184 for(int i=0; i<n; i+=2) {
185 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
187 mc_mem_region_t region = mc_region_new_sparse(0, source, byte_size, NULL);
189 void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
190 xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
192 xbt_test_add("Reading whole region data for %i page(s)", n);
193 void* read = mc_snapshot_read_region(source, region, destination, byte_size);
194 xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in mc_snapshot_read_region()");
196 xbt_test_add("Reading parts of region data for %i page(s)", n);
197 for(int j=0; j!=100; ++j) {
198 size_t offset = rand() % byte_size;
199 size_t size = rand() % (byte_size - offset);
200 void* read = mc_snapshot_read_region((char*) source+offset, region, destination, size);
201 xbt_test_assert(!memcmp((char*) source+offset, read, size),
202 "Mismatch in mc_snapshot_read_region()");
205 xbt_test_add("Compare whole region data for %i page(s)", n);
206 xbt_test_assert(!mc_snapshot_region_memcmp(source, NULL, source, region, byte_size),
207 "Mismatch in mc_snapshot_region_memcmp() for the whole region");
208 xbt_test_assert(mc_snapshot_region_memcmp(source, region0, source, region, byte_size),
209 "Unexpected match in mc_snapshot_region_memcmp() with previous snapshot");
211 xbt_test_add("Compare parts of region data for %i page(s)", n);
212 for(int j=0; j!=100; ++j) {
213 size_t offset = rand() % byte_size;
214 size_t size = rand() % (byte_size - offset);
215 xbt_test_assert(!mc_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
216 "Mismatch in mc_snapshot_region_memcmp()");
220 xbt_test_add("Read pointer for %i page(s)", n);
221 memcpy(source, &mc_model_checker, sizeof(void*));
222 mc_mem_region_t region2 = mc_region_new_sparse(0, source, byte_size, NULL);
223 xbt_test_assert(mc_snapshot_read_pointer_region(source, region2) == mc_model_checker,
224 "Mismtach in mc_snapshot_read_pointer_region()");
225 MC_region_destroy(region2);
228 MC_region_destroy(region);
229 MC_region_destroy(region0);
230 munmap(destination, byte_size);
231 munmap(source, byte_size);
234 mc_pages_store_delete(mc_model_checker->pages);
235 xbt_free(mc_model_checker);
236 mc_model_checker = NULL;
239 #endif /* SIMGRID_TEST */