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 "PageStore.hpp"
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 == simgrid::mc::ProcessIndexDisabled) {
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 /** Compare memory between snapshots (with known regions)
102 * @param addr1 Address in the first snapshot
103 * @param snapshot2 Region of the address in the first snapshot
104 * @param addr2 Address in the second snapshot
105 * @param snapshot2 Region of the address in the second snapshot
106 * @return same as memcmp
108 int MC_snapshot_region_memcmp(
109 const void* addr1, mc_mem_region_t region1,
110 const void* addr2, mc_mem_region_t region2,
113 // Using alloca() for large allocations may trigger stack overflow:
114 // use malloc if the buffer is too big.
115 bool stack_alloc = size < 64;
116 const bool region1_need_buffer = region1==NULL || region1->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
117 const bool region2_need_buffer = region2==NULL || region2->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
118 void* buffer1a = region1_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
119 void* buffer2a = region2_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
120 const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
121 const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
123 if (buffer1 == buffer2) {
126 res = memcmp(buffer1, buffer2, size);
135 /** Compare memory between snapshots
137 * @param addr1 Address in the first snapshot
138 * @param snapshot1 First snapshot
139 * @param addr2 Address in the second snapshot
140 * @param snapshot2 Second snapshot
141 * @return same as memcmp
143 int MC_snapshot_memcmp(
144 const void* addr1, mc_snapshot_t snapshot1,
145 const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
147 mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
148 mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
149 return MC_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
155 Snapshot::Snapshot() :
159 snapshot_regions(nullptr),
160 snapshot_regions_count(0),
161 enabled_processes(0),
162 privatization_index(0),
163 stack_sizes(nullptr),
167 ignored_data(nullptr),
173 Snapshot::~Snapshot()
175 for (size_t i = 0; i < this->snapshot_regions_count; i++) {
176 MC_region_destroy(this->snapshot_regions[i]);
178 xbt_free(this->snapshot_regions);
179 xbt_free(this->stack_sizes);
180 xbt_dynar_free(&(this->stacks));
181 xbt_dynar_free(&(this->to_ignore));
182 xbt_dynar_free(&this->ignored_data);
185 const void* Snapshot::read_bytes(void* buffer, std::size_t size,
186 std::uint64_t address, int process_index,
187 AddressSpace::ReadMode mode)
189 mc_mem_region_t region = mc_get_snapshot_region((void*)address, this, process_index);
191 const void* res = MC_region_read(region, buffer, (void*)address, size);
192 if (buffer == res || mode == AddressSpace::Lazy)
195 memcpy(buffer, res, size);
200 return MC_process_read(this->process, mode, buffer, (void*)address, size, process_index);
211 #include <sys/mman.h>
213 #include "mc/mc_private.h"
214 #include "mc/mc_snapshot.h"
215 #include "mc/mc_mmu.h"
219 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
221 static inline void init_memory(void* mem, size_t size)
223 char* dest = (char*) mem;
224 for (size_t i = 0; i < size; ++i) {
225 dest[i] = rand() & 255;
229 static void test_snapshot(bool sparse_checkpoint);
231 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
236 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
241 static void test_snapshot(bool sparse_checkpoint) {
243 xbt_test_add("Initialisation");
244 _sg_mc_sparse_checkpoint = sparse_checkpoint;
245 xbt_assert(xbt_pagesize == getpagesize());
246 xbt_assert(1 << xbt_pagebits == xbt_pagesize);
247 mc_model_checker = new ::simgrid::mc::ModelChecker(getpid(), -1);
249 for(int n=1; n!=256; ++n) {
251 // Store region page(s):
252 size_t byte_size = n * xbt_pagesize;
253 void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
254 xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
256 // Init memory and take snapshots:
257 init_memory(source, byte_size);
258 mc_mem_region_t region0 = mc_region_new_sparse(
259 MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
260 for(int i=0; i<n; i+=2) {
261 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
263 mc_mem_region_t region = mc_region_new_sparse(
264 MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
266 void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
267 xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
269 xbt_test_add("Reading whole region data for %i page(s)", n);
270 const void* read = MC_region_read(region, destination, source, byte_size);
271 xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
273 xbt_test_add("Reading parts of region data for %i page(s)", n);
274 for(int j=0; j!=100; ++j) {
275 size_t offset = rand() % byte_size;
276 size_t size = rand() % (byte_size - offset);
277 const void* read = MC_region_read(region, destination, (const char*) source+offset, size);
278 xbt_test_assert(!memcmp((char*) source+offset, read, size),
279 "Mismatch in MC_region_read()");
282 xbt_test_add("Compare whole region data for %i page(s)", n);
283 xbt_test_assert(!MC_snapshot_region_memcmp(source, NULL, source, region, byte_size),
284 "Mismatch in MC_snapshot_region_memcmp() for the whole region");
285 xbt_test_assert(MC_snapshot_region_memcmp(source, region0, source, region, byte_size),
286 "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
288 xbt_test_add("Compare parts of region data for %i page(s) with current value", n);
289 for(int j=0; j!=100; ++j) {
290 size_t offset = rand() % byte_size;
291 size_t size = rand() % (byte_size - offset);
292 xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
293 "Mismatch in MC_snapshot_region_memcmp()");
296 xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
297 for(int j=0; j!=100; ++j) {
298 size_t offset = rand() % byte_size;
299 size_t size = rand() % (byte_size - offset);
300 xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, region, (char*) source+offset, region, size),
301 "Mismatch in MC_snapshot_region_memcmp()");
305 xbt_test_add("Read pointer for %i page(s)", n);
306 memcpy(source, &mc_model_checker, sizeof(void*));
307 mc_mem_region_t region2 = mc_region_new_sparse(
308 MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
309 xbt_test_assert(MC_region_read_pointer(region2, source) == mc_model_checker,
310 "Mismtach in MC_region_read_pointer()");
311 MC_region_destroy(region2);
314 MC_region_destroy(region);
315 MC_region_destroy(region0);
316 munmap(destination, byte_size);
317 munmap(source, byte_size);
320 delete mc_model_checker;
321 mc_model_checker = NULL;
326 #endif /* SIMGRID_TEST */