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(
27 const void* addr, const s_mc_snapshot_t* snapshot, int process_index)
29 size_t n = snapshot->snapshot_regions.size();
30 for (size_t i = 0; i != n; ++i) {
31 mc_mem_region_t region = snapshot->snapshot_regions[i].get();
32 if (!(region && region->contain(simgrid::mc::remote(addr))))
35 if (region->storage_type() == simgrid::mc::StorageType::Privatized) {
37 // Use the current process index of the snapshot:
38 if (process_index == simgrid::mc::ProcessIndexDisabled) {
39 process_index = snapshot->privatization_index;
41 if (process_index < 0) {
42 xbt_die("Missing process index");
44 if (process_index >= (int) region->privatized_data().size()) {
45 xbt_die("Invalid process index");
47 simgrid::mc::RegionSnapshot& priv_region = region->privatized_data()[process_index];
48 xbt_assert(priv_region.contain(simgrid::mc::remote(addr)));
51 xbt_die("Privatized region in a non SMPI build (this should not happen)");
61 /** @brief Read memory from a snapshot region broken across fragmented pages
63 * @param addr Process (non-snapshot) address of the data
64 * @param region Snapshot memory region where the data is located
65 * @param target Buffer to store the value
66 * @param size Size of the data to read in bytes
67 * @return Pointer where the data is located (target buffer of original location)
69 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, size_t size)
71 // Last byte of the memory area:
72 void* end = (char*) addr + size - 1;
74 // Page of the last byte of the memory area:
75 size_t page_end = mc_page_number(NULL, end);
80 xbt_die("Missing destination buffer for fragmented memory access");
84 while (mc_page_number(NULL, addr) != page_end) {
85 void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t) addr, region);
86 void* next_page = mc_page_from_number(NULL, mc_page_number(NULL, addr) + 1);
87 size_t readable = (char*) next_page - (char*) addr;
88 memcpy(dest, snapshot_addr, readable);
89 addr = (char*) addr + readable;
90 dest = (char*) dest + readable;
95 void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
96 memcpy(dest, snapshot_addr, size);
101 /** Compare memory between snapshots (with known regions)
103 * @param addr1 Address in the first snapshot
104 * @param snapshot2 Region of the address in the first snapshot
105 * @param addr2 Address in the second snapshot
106 * @param snapshot2 Region of the address in the second snapshot
107 * @return same as memcmp
109 int MC_snapshot_region_memcmp(
110 const void* addr1, mc_mem_region_t region1,
111 const void* addr2, mc_mem_region_t region2,
114 // Using alloca() for large allocations may trigger stack overflow:
115 // use malloc if the buffer is too big.
116 bool stack_alloc = size < 64;
117 const bool region1_need_buffer = region1==NULL || region1->storage_type()==simgrid::mc::StorageType::Flat;
118 const bool region2_need_buffer = region2==NULL || region2->storage_type()==simgrid::mc::StorageType::Flat;
119 void* buffer1a = region1_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
120 void* buffer2a = region2_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
121 const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
122 const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
124 if (buffer1 == buffer2) {
127 res = memcmp(buffer1, buffer2, size);
136 /** Compare memory between snapshots
138 * @param addr1 Address in the first snapshot
139 * @param snapshot1 First snapshot
140 * @param addr2 Address in the second snapshot
141 * @param snapshot2 Second snapshot
142 * @return same as memcmp
144 int MC_snapshot_memcmp(
145 const void* addr1, mc_snapshot_t snapshot1,
146 const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
148 mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
149 mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
150 return MC_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
156 Snapshot::Snapshot() :
161 privatization_index(0),
167 Snapshot::~Snapshot()
172 const void* Snapshot::read_bytes(void* buffer, std::size_t size,
173 remote_ptr<void> address, int process_index,
174 AddressSpace::ReadMode mode) const
176 mc_mem_region_t region = mc_get_snapshot_region((void*)address.address(), this, process_index);
178 const void* res = MC_region_read(region, buffer, (void*)address.address(), size);
179 if (buffer == res || mode == AddressSpace::Lazy)
182 memcpy(buffer, res, size);
187 return this->read_bytes(buffer, size, address, process_index, mode);
198 #include <sys/mman.h>
200 #include "mc/mc_private.h"
201 #include "mc/mc_snapshot.h"
202 #include "mc/mc_mmu.h"
206 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
208 static inline void init_memory(void* mem, size_t size)
210 char* dest = (char*) mem;
211 for (size_t i = 0; i < size; ++i) {
212 dest[i] = rand() & 255;
216 static void test_snapshot(bool sparse_checkpoint);
218 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
223 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
228 static void test_snapshot(bool sparse_checkpoint) {
230 xbt_test_add("Initialisation");
231 _sg_mc_sparse_checkpoint = sparse_checkpoint;
232 xbt_assert(xbt_pagesize == getpagesize());
233 xbt_assert(1 << xbt_pagebits == xbt_pagesize);
234 mc_model_checker = new ::simgrid::mc::ModelChecker(getpid(), -1);
236 for(int n=1; n!=256; ++n) {
238 // Store region page(s):
239 size_t byte_size = n * xbt_pagesize;
240 void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
241 xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
243 // Init memory and take snapshots:
244 init_memory(source, byte_size);
245 simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region(
246 simgrid::mc::RegionType::Unknown, source, source, byte_size);
247 for(int i=0; i<n; i+=2) {
248 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
250 simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
251 simgrid::mc::RegionType::Unknown, source, source, byte_size);
253 void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
254 xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
256 xbt_test_add("Reading whole region data for %i page(s)", n);
257 const void* read = MC_region_read(®ion, destination, source, byte_size);
258 xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
260 xbt_test_add("Reading parts of region data for %i page(s)", n);
261 for(int j=0; j!=100; ++j) {
262 size_t offset = rand() % byte_size;
263 size_t size = rand() % (byte_size - offset);
264 const void* read = MC_region_read(®ion, destination, (const char*) source+offset, size);
265 xbt_test_assert(!memcmp((char*) source+offset, read, size),
266 "Mismatch in MC_region_read()");
269 xbt_test_add("Compare whole region data for %i page(s)", n);
271 xbt_test_assert(MC_snapshot_region_memcmp(source, ®ion0, source, ®ion, byte_size),
272 "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
274 xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
275 for(int j=0; j!=100; ++j) {
276 size_t offset = rand() % byte_size;
277 size_t size = rand() % (byte_size - offset);
278 xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, ®ion, (char*) source+offset, ®ion, size),
279 "Mismatch in MC_snapshot_region_memcmp()");
283 xbt_test_add("Read pointer for %i page(s)", n);
284 memcpy(source, &mc_model_checker, sizeof(void*));
285 simgrid::mc::RegionSnapshot region2 = simgrid::mc::sparse_region(
286 simgrid::mc::RegionType::Unknown, source, source, byte_size);
287 xbt_test_assert(MC_region_read_pointer(®ion2, source) == mc_model_checker,
288 "Mismtach in MC_region_read_pointer()");
291 munmap(destination, byte_size);
292 munmap(source, byte_size);
295 delete mc_model_checker;
296 mc_model_checker = NULL;
301 #endif /* SIMGRID_TEST */