1 /* Copyright (c) 2014-2015. 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. */
12 #include <xbt/asserts.h>
13 #include <xbt/sysdep.h>
15 #include "src/internal_config.h"
16 #include "src/smpi/private.h"
18 #include "src/mc/mc_snapshot.h"
19 #include "src/mc/mc_private.h"
20 #include "src/mc/mc_mmu.h"
21 #include "src/mc/PageStore.hpp"
25 /** @brief Find the snapshoted region from a pointer
28 * @param snapshot Snapshot
29 * @param Snapshot region in the snapshot this pointer belongs to
30 * (or nullptr if it does not belong to any snapshot region)
32 mc_mem_region_t mc_get_snapshot_region(
33 const void* addr, const simgrid::mc::Snapshot* snapshot, int process_index)
35 size_t n = snapshot->snapshot_regions.size();
36 for (size_t i = 0; i != n; ++i) {
37 mc_mem_region_t region = snapshot->snapshot_regions[i].get();
38 if (!(region && region->contain(simgrid::mc::remote(addr))))
41 if (region->storage_type() == simgrid::mc::StorageType::Privatized) {
43 // Use the current process index of the snapshot:
44 if (process_index == simgrid::mc::ProcessIndexDisabled)
45 process_index = snapshot->privatization_index;
46 if (process_index < 0)
47 xbt_die("Missing process index");
48 if (process_index >= (int) region->privatized_data().size())
49 xbt_die("Invalid process index");
50 simgrid::mc::RegionSnapshot& priv_region = region->privatized_data()[process_index];
51 xbt_assert(priv_region.contain(simgrid::mc::remote(addr)));
54 xbt_die("Privatized region in a non SMPI build (this should not happen)");
64 /** @brief Read memory from a snapshot region broken across fragmented pages
66 * @param addr Process (non-snapshot) address of the data
67 * @param region Snapshot memory region where the data is located
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 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, size_t size)
74 // Last byte of the memory area:
75 void* end = (char*) addr + size - 1;
77 // TODO, we assume the chunks are aligned to natural chunk boundaries.
78 // We should remove this assumption.
80 // Page of the last byte of the memory area:
81 size_t page_end = simgrid::mc::mmu::split((std::uintptr_t) end).first;
86 xbt_die("Missing destination buffer for fragmented memory access");
89 while (simgrid::mc::mmu::split((std::uintptr_t) addr).first != page_end) {
90 void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t) addr, region);
91 void* next_page = (void*) simgrid::mc::mmu::join(
92 simgrid::mc::mmu::split((std::uintptr_t) addr).first + 1,
94 size_t readable = (char*) next_page - (char*) addr;
95 memcpy(dest, snapshot_addr, readable);
96 addr = (char*) addr + readable;
97 dest = (char*) dest + readable;
102 void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
103 memcpy(dest, snapshot_addr, size);
108 /** Compare memory between snapshots (with known regions)
110 * @param addr1 Address in the first snapshot
111 * @param snapshot2 Region of the address in the first snapshot
112 * @param addr2 Address in the second snapshot
113 * @param snapshot2 Region of the address in the second snapshot
114 * @return same as memcmp
116 int MC_snapshot_region_memcmp(
117 const void* addr1, mc_mem_region_t region1,
118 const void* addr2, mc_mem_region_t region2,
121 // Using alloca() for large allocations may trigger stack overflow:
122 // use malloc if the buffer is too big.
123 bool stack_alloc = size < 64;
124 const bool region1_need_buffer = region1==nullptr || region1->storage_type()==simgrid::mc::StorageType::Flat;
125 const bool region2_need_buffer = region2==nullptr || region2->storage_type()==simgrid::mc::StorageType::Flat;
126 void* buffer1a = region1_need_buffer ? nullptr : stack_alloc ? alloca(size) : malloc(size);
127 void* buffer2a = region2_need_buffer ? nullptr : stack_alloc ? alloca(size) : malloc(size);
128 const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
129 const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
131 if (buffer1 == buffer2)
134 res = memcmp(buffer1, buffer2, size);
142 /** Compare memory between snapshots
144 * @param addr1 Address in the first snapshot
145 * @param snapshot1 First snapshot
146 * @param addr2 Address in the second snapshot
147 * @param snapshot2 Second snapshot
148 * @return same as memcmp
150 int MC_snapshot_memcmp(
151 const void* addr1, simgrid::mc::Snapshot* snapshot1,
152 const void* addr2, simgrid::mc::Snapshot* snapshot2, int process_index, size_t size)
154 mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
155 mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
156 return MC_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
162 Snapshot::Snapshot(Process* process) :
163 AddressSpace(process),
167 privatization_index(0),
173 Snapshot::~Snapshot()
178 const void* Snapshot::read_bytes(void* buffer, std::size_t size,
179 RemotePtr<void> address, int process_index,
180 ReadOptions options) const
182 mc_mem_region_t region = mc_get_snapshot_region((void*)address.address(), this, process_index);
184 const void* res = MC_region_read(region, buffer, (void*)address.address(), size);
185 if (buffer == res || options & ReadOptions::lazy())
188 memcpy(buffer, res, size);
193 return this->process()->read_bytes(
194 buffer, size, address, process_index, options);
207 #include <sys/mman.h>
209 #include "src/mc/mc_private.h"
210 #include "src/mc/mc_snapshot.h"
211 #include "src/mc/mc_mmu.h"
213 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
215 static inline void init_memory(void* mem, size_t size)
217 char* dest = (char*) mem;
218 for (size_t i = 0; i < size; ++i) {
219 dest[i] = rand() & 255;
223 static void test_snapshot(bool sparse_checkpoint);
225 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
230 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
235 static void test_snapshot(bool sparse_checkpoint) {
237 xbt_test_add("Initialisation");
238 _sg_mc_sparse_checkpoint = sparse_checkpoint;
239 xbt_assert(xbt_pagesize == getpagesize());
240 xbt_assert(1 << xbt_pagebits == xbt_pagesize);
242 std::unique_ptr<simgrid::mc::Process> process(new simgrid::mc::Process(getpid(), -1));
244 mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
246 for(int n=1; n!=256; ++n) {
248 // Store region page(s):
249 size_t byte_size = n * xbt_pagesize;
250 void* source = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
251 xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
253 // Init memory and take snapshots:
254 init_memory(source, byte_size);
255 simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region(
256 simgrid::mc::RegionType::Unknown, source, source, byte_size);
257 for(int i=0; i<n; i+=2) {
258 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
260 simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
261 simgrid::mc::RegionType::Unknown, source, source, byte_size);
263 void* destination = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
264 xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
266 xbt_test_add("Reading whole region data for %i page(s)", n);
267 const void* read = MC_region_read(®ion, destination, source, byte_size);
268 xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
270 xbt_test_add("Reading parts of region data for %i page(s)", n);
271 for(int j=0; j!=100; ++j) {
272 size_t offset = rand() % byte_size;
273 size_t size = rand() % (byte_size - offset);
274 const void* read = MC_region_read(®ion, destination, (const char*) source+offset, size);
275 xbt_test_assert(!memcmp((char*) source+offset, read, size),
276 "Mismatch in MC_region_read()");
279 xbt_test_add("Compare whole region data for %i page(s)", n);
281 xbt_test_assert(MC_snapshot_region_memcmp(source, ®ion0, source, ®ion, byte_size),
282 "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
284 xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
285 for(int j=0; j!=100; ++j) {
286 size_t offset = rand() % byte_size;
287 size_t size = rand() % (byte_size - offset);
288 xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, ®ion, (char*) source+offset, ®ion, size),
289 "Mismatch in MC_snapshot_region_memcmp()");
293 xbt_test_add("Read pointer for %i page(s)", n);
294 memcpy(source, &mc_model_checker, sizeof(void*));
295 simgrid::mc::RegionSnapshot region2 = simgrid::mc::sparse_region(
296 simgrid::mc::RegionType::Unknown, source, source, byte_size);
297 xbt_test_assert(MC_region_read_pointer(®ion2, source) == mc_model_checker,
298 "Mismtach in MC_region_read_pointer()");
301 munmap(destination, byte_size);
302 munmap(source, byte_size);
305 delete mc_model_checker;
306 mc_model_checker = nullptr;
309 #endif /* SIMGRID_TEST */