1 /* Copyright (c) 2014-2018. 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/include/private.hpp"
18 #include "src/mc/PageStore.hpp"
19 #include "src/mc/mc_mmu.hpp"
20 #include "src/mc/mc_private.hpp"
21 #include "src/mc/mc_snapshot.hpp"
23 /** @brief Find the snapshoted region from a pointer
26 * @param snapshot Snapshot
27 * @param Snapshot region in the snapshot this pointer belongs to
28 * (or nullptr if it does not belong to any snapshot region)
30 mc_mem_region_t mc_get_snapshot_region(
31 const void* addr, const simgrid::mc::Snapshot* snapshot, int process_index)
33 size_t n = snapshot->snapshot_regions.size();
34 for (size_t i = 0; i != n; ++i) {
35 mc_mem_region_t region = snapshot->snapshot_regions[i].get();
36 if (not(region && region->contain(simgrid::mc::remote(addr))))
39 if (region->storage_type() == simgrid::mc::StorageType::Privatized) {
41 // Use the current process index of the snapshot:
42 if (process_index == simgrid::mc::ProcessIndexDisabled)
43 process_index = snapshot->privatization_index;
44 if (process_index < 0)
45 xbt_die("Missing process index");
46 if (process_index >= (int) region->privatized_data().size())
47 xbt_die("Invalid process index");
48 simgrid::mc::RegionSnapshot& priv_region = region->privatized_data()[process_index];
49 xbt_assert(priv_region.contain(simgrid::mc::remote(addr)));
52 xbt_die("Privatized region in a non SMPI build (this should not happen)");
62 /** @brief Read memory from a snapshot region broken across fragmented pages
64 * @param addr Process (non-snapshot) address of the data
65 * @param region Snapshot memory region where the data is located
66 * @param target Buffer to store the value
67 * @param size Size of the data to read in bytes
68 * @return Pointer where the data is located (target buffer of original location)
70 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, size_t size)
72 // Last byte of the memory area:
73 void* end = (char*) addr + size - 1;
75 // TODO, we assume the chunks are aligned to natural chunk boundaries.
76 // We should remove this assumption.
78 // Page of the last byte of the memory area:
79 size_t page_end = simgrid::mc::mmu::split((std::uintptr_t) end).first;
84 xbt_die("Missing destination buffer for fragmented memory access");
87 while (simgrid::mc::mmu::split((std::uintptr_t) addr).first != page_end) {
88 void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t) addr, region);
89 void* next_page = (void*) simgrid::mc::mmu::join(
90 simgrid::mc::mmu::split((std::uintptr_t) addr).first + 1,
92 size_t readable = (char*) next_page - (char*) addr;
93 memcpy(dest, snapshot_addr, readable);
94 addr = (char*) addr + readable;
95 dest = (char*) dest + readable;
100 void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
101 memcpy(dest, snapshot_addr, size);
106 /** Compare memory between snapshots (with known regions)
108 * @param addr1 Address in the first snapshot
109 * @param snapshot2 Region of the address in the first snapshot
110 * @param addr2 Address in the second snapshot
111 * @param snapshot2 Region of the address in the second snapshot
112 * @return same as memcmp
114 int MC_snapshot_region_memcmp(
115 const void* addr1, mc_mem_region_t region1,
116 const void* addr2, mc_mem_region_t region2,
119 // Using alloca() for large allocations may trigger stack overflow:
120 // use malloc if the buffer is too big.
121 bool stack_alloc = size < 64;
122 void* buffer1a = nullptr;
123 void* buffer2a = nullptr;
124 if (region1 != nullptr && region1->storage_type() != simgrid::mc::StorageType::Flat)
125 buffer1a = stack_alloc ? alloca(size) : ::operator new(size);
126 if (region2 != nullptr && region2->storage_type() != simgrid::mc::StorageType::Flat)
127 buffer2a = stack_alloc ? alloca(size) : ::operator new(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);
135 if (not stack_alloc) {
136 ::operator delete(buffer1a);
137 ::operator delete(buffer2a);
145 Snapshot::Snapshot(RemoteClient* process, int _num_state)
146 : AddressSpace(process)
147 , num_state(_num_state)
149 , enabled_processes()
150 , privatization_index(0)
156 const void* Snapshot::read_bytes(void* buffer, std::size_t size,
157 RemotePtr<void> address, int process_index,
158 ReadOptions options) const
160 mc_mem_region_t region = mc_get_snapshot_region((void*)address.address(), this, process_index);
162 const void* res = MC_region_read(region, buffer, (void*)address.address(), size);
163 if (buffer == res || options & ReadOptions::lazy())
166 memcpy(buffer, res, size);
171 return this->process()->read_bytes(
172 buffer, size, address, process_index, options);
183 #include <sys/mman.h>
185 #include "src/mc/mc_mmu.hpp"
186 #include "src/mc/mc_private.hpp"
187 #include "src/mc/mc_snapshot.hpp"
189 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
191 static inline void init_memory(void* mem, size_t size)
193 char* dest = (char*) mem;
194 for (size_t i = 0; i < size; ++i) {
195 dest[i] = rand() & 255;
199 static void test_snapshot(bool sparse_checkpoint);
201 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
206 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
211 static void test_snapshot(bool sparse_checkpoint) {
213 xbt_test_add("Initialization");
214 _sg_mc_sparse_checkpoint = sparse_checkpoint;
215 xbt_assert(xbt_pagesize == getpagesize());
216 xbt_assert(1 << xbt_pagebits == xbt_pagesize);
218 std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(getpid(), -1));
220 mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
222 for(int n=1; n!=256; ++n) {
224 // Store region page(s):
225 size_t byte_size = n * xbt_pagesize;
226 void* source = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
227 xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
229 // Init memory and take snapshots:
230 init_memory(source, byte_size);
231 simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region(
232 simgrid::mc::RegionType::Unknown, source, source, byte_size);
233 for(int i=0; i<n; i+=2) {
234 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
236 simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
237 simgrid::mc::RegionType::Unknown, source, source, byte_size);
239 void* destination = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
240 xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
242 xbt_test_add("Reading whole region data for %i page(s)", n);
243 const void* read = MC_region_read(®ion, destination, source, byte_size);
244 xbt_test_assert(not memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
246 xbt_test_add("Reading parts of region data for %i page(s)", n);
247 for(int j=0; j!=100; ++j) {
248 size_t offset = rand() % byte_size;
249 size_t size = rand() % (byte_size - offset);
250 const void* read = MC_region_read(®ion, destination, (const char*) source+offset, size);
251 xbt_test_assert(not memcmp((char*)source + offset, read, size), "Mismatch in MC_region_read()");
254 xbt_test_add("Compare whole region data for %i page(s)", n);
256 xbt_test_assert(MC_snapshot_region_memcmp(source, ®ion0, source, ®ion, byte_size),
257 "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
259 xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
260 for(int j=0; j!=100; ++j) {
261 size_t offset = rand() % byte_size;
262 size_t size = rand() % (byte_size - offset);
264 not MC_snapshot_region_memcmp((char*)source + offset, ®ion, (char*)source + offset, ®ion, size),
265 "Mismatch in MC_snapshot_region_memcmp()");
269 xbt_test_add("Read pointer for %i page(s)", n);
270 memcpy(source, &mc_model_checker, sizeof(void*));
271 simgrid::mc::RegionSnapshot region2 = simgrid::mc::sparse_region(
272 simgrid::mc::RegionType::Unknown, source, source, byte_size);
273 xbt_test_assert(MC_region_read_pointer(®ion2, source) == mc_model_checker,
274 "Mismtach in MC_region_read_pointer()");
277 munmap(destination, byte_size);
278 munmap(source, byte_size);
281 delete mc_model_checker;
282 mc_model_checker = nullptr;
285 #endif /* SIMGRID_TEST */