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 == 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 /** 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 return MC_region_read(region, buffer, (void*)address, size);
193 return MC_process_read(this->process, mode, buffer, (void*)address, size, process_index);
204 #include <sys/mman.h>
206 #include "mc/mc_private.h"
207 #include "mc/mc_snapshot.h"
208 #include "mc/mc_mmu.h"
212 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
214 static inline void init_memory(void* mem, size_t size)
216 char* dest = (char*) mem;
217 for (size_t i = 0; i < size; ++i) {
218 dest[i] = rand() & 255;
222 static void test_snapshot(bool sparse_checkpoint);
224 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
229 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
234 static void test_snapshot(bool sparse_checkpoint) {
236 xbt_test_add("Initialisation");
237 _sg_mc_sparse_checkpoint = sparse_checkpoint;
238 xbt_assert(xbt_pagesize == getpagesize());
239 xbt_assert(1 << xbt_pagebits == xbt_pagesize);
240 mc_model_checker = new ::simgrid::mc::ModelChecker(getpid(), -1);
242 for(int n=1; n!=256; ++n) {
244 // Store region page(s):
245 size_t byte_size = n * xbt_pagesize;
246 void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
247 xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
249 // Init memory and take snapshots:
250 init_memory(source, byte_size);
251 mc_mem_region_t region0 = mc_region_new_sparse(
252 MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
253 for(int i=0; i<n; i+=2) {
254 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
256 mc_mem_region_t region = mc_region_new_sparse(
257 MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
259 void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
260 xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
262 xbt_test_add("Reading whole region data for %i page(s)", n);
263 const void* read = MC_region_read(region, destination, source, byte_size);
264 xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
266 xbt_test_add("Reading parts of region data for %i page(s)", n);
267 for(int j=0; j!=100; ++j) {
268 size_t offset = rand() % byte_size;
269 size_t size = rand() % (byte_size - offset);
270 const void* read = MC_region_read(region, destination, (const char*) source+offset, size);
271 xbt_test_assert(!memcmp((char*) source+offset, read, size),
272 "Mismatch in MC_region_read()");
275 xbt_test_add("Compare whole region data for %i page(s)", n);
276 xbt_test_assert(!MC_snapshot_region_memcmp(source, NULL, source, region, byte_size),
277 "Mismatch in MC_snapshot_region_memcmp() for the whole region");
278 xbt_test_assert(MC_snapshot_region_memcmp(source, region0, source, region, byte_size),
279 "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
281 xbt_test_add("Compare parts of region data for %i page(s) with current value", n);
282 for(int j=0; j!=100; ++j) {
283 size_t offset = rand() % byte_size;
284 size_t size = rand() % (byte_size - offset);
285 xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
286 "Mismatch in MC_snapshot_region_memcmp()");
289 xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
290 for(int j=0; j!=100; ++j) {
291 size_t offset = rand() % byte_size;
292 size_t size = rand() % (byte_size - offset);
293 xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, region, (char*) source+offset, region, size),
294 "Mismatch in MC_snapshot_region_memcmp()");
298 xbt_test_add("Read pointer for %i page(s)", n);
299 memcpy(source, &mc_model_checker, sizeof(void*));
300 mc_mem_region_t region2 = mc_region_new_sparse(
301 MC_REGION_TYPE_UNKNOWN, source, source, byte_size);
302 xbt_test_assert(MC_region_read_pointer(region2, source) == mc_model_checker,
303 "Mismtach in MC_region_read_pointer()");
304 MC_region_destroy(region2);
307 MC_region_destroy(region);
308 MC_region_destroy(region0);
309 munmap(destination, byte_size);
310 munmap(source, byte_size);
313 delete mc_model_checker;
314 mc_model_checker = NULL;
319 #endif /* SIMGRID_TEST */