1 /* Copyright (c) 2014-2019. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/asserts.h"
12 #include "xbt/sysdep.h"
14 #include "src/internal_config.h"
15 #include "src/smpi/include/private.hpp"
17 #include "src/mc/mc_mmu.hpp"
18 #include "src/mc/mc_private.hpp"
19 #include "src/mc/sosp/PageStore.hpp"
20 #include "src/mc/sosp/mc_snapshot.hpp"
22 /** @brief Find the snapshoted region from a pointer
25 * @param snapshot Snapshot
26 * @param process_index rank requesting the region
28 simgrid::mc::RegionSnapshot* mc_get_snapshot_region(const void* addr, const simgrid::mc::Snapshot* snapshot,
31 size_t n = snapshot->snapshot_regions.size();
32 for (size_t i = 0; i != n; ++i) {
33 simgrid::mc::RegionSnapshot* region = snapshot->snapshot_regions[i].get();
34 if (not(region && region->contain(simgrid::mc::remote(addr))))
37 if (region->storage_type() == simgrid::mc::StorageType::Privatized) {
39 // Use the current process index of the snapshot:
40 if (process_index == simgrid::mc::ProcessIndexDisabled)
41 process_index = snapshot->privatization_index;
42 if (process_index < 0)
43 xbt_die("Missing process index");
44 if (process_index >= (int)region->privatized_data().size())
45 xbt_die("Invalid process index");
46 simgrid::mc::RegionSnapshot& priv_region = region->privatized_data()[process_index];
47 xbt_assert(priv_region.contain(simgrid::mc::remote(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(simgrid::mc::RegionSnapshot* region, void* target, const void* addr, size_t size)
70 // Last byte of the memory area:
71 void* end = (char*)addr + size - 1;
73 // TODO, we assume the chunks are aligned to natural chunk boundaries.
74 // We should remove this assumption.
76 // Page of the last byte of the memory area:
77 size_t page_end = simgrid::mc::mmu::split((std::uintptr_t)end).first;
82 xbt_die("Missing destination buffer for fragmented memory access");
85 while (simgrid::mc::mmu::split((std::uintptr_t)addr).first != page_end) {
86 void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
87 void* next_page = (void*)simgrid::mc::mmu::join(simgrid::mc::mmu::split((std::uintptr_t)addr).first + 1, 0);
88 size_t readable = (char*)next_page - (char*)addr;
89 memcpy(dest, snapshot_addr, readable);
90 addr = (char*)addr + readable;
91 dest = (char*)dest + readable;
96 void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
97 memcpy(dest, snapshot_addr, size);
102 /** Compare memory between snapshots (with known regions)
104 * @param addr1 Address in the first snapshot
105 * @param region1 Region of the address in the first snapshot
106 * @param addr2 Address in the second snapshot
107 * @param region2 Region of the address in the second snapshot
108 * @return same semantic as memcmp
110 int MC_snapshot_region_memcmp(const void* addr1, simgrid::mc::RegionSnapshot* region1, const void* addr2,
111 simgrid::mc::RegionSnapshot* region2, size_t size)
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 void* buffer1a = nullptr;
117 void* buffer2a = nullptr;
118 if (region1 != nullptr && region1->storage_type() != simgrid::mc::StorageType::Flat)
119 buffer1a = stack_alloc ? alloca(size) : ::operator new(size);
120 if (region2 != nullptr && region2->storage_type() != simgrid::mc::StorageType::Flat)
121 buffer2a = stack_alloc ? alloca(size) : ::operator new(size);
122 const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
123 const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
125 if (buffer1 == buffer2)
128 res = memcmp(buffer1, buffer2, size);
129 if (not stack_alloc) {
130 ::operator delete(buffer1a);
131 ::operator delete(buffer2a);
139 Snapshot::Snapshot(RemoteClient* process, int _num_state)
140 : AddressSpace(process)
141 , num_state(_num_state)
143 , enabled_processes()
144 , privatization_index(0)
149 const void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, int process_index,
150 ReadOptions options) const
152 RegionSnapshot* region = mc_get_snapshot_region((void*)address.address(), this, process_index);
154 const void* res = MC_region_read(region, buffer, (void*)address.address(), size);
155 if (buffer == res || options & ReadOptions::lazy())
158 memcpy(buffer, res, size);
162 return this->process()->read_bytes(buffer, size, address, process_index, options);
166 } // namespace simgrid