Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branches 'auto_restart' and 'auto_restart' of framagit.org:simgrid/simgrid
[simgrid.git] / src / mc / sosp / mc_snapshot.cpp
1 /* Copyright (c) 2014-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <cstddef>
7
8 #include <memory>
9 #include <utility>
10
11 #include "xbt/asserts.h"
12 #include "xbt/sysdep.h"
13
14 #include "src/internal_config.h"
15 #include "src/smpi/include/private.hpp"
16
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"
21
22 /** @brief Find the snapshoted region from a pointer
23  *
24  *  @param addr     Pointer
25  *  @param snapshot Snapshot
26  *  @param process_index rank requesting the region
27  * */
28 mc_mem_region_t mc_get_snapshot_region(const void* addr, const simgrid::mc::Snapshot* snapshot, int process_index)
29 {
30   size_t n = snapshot->snapshot_regions.size();
31   for (size_t i = 0; i != n; ++i) {
32     mc_mem_region_t region = snapshot->snapshot_regions[i].get();
33     if (not(region && region->contain(simgrid::mc::remote(addr))))
34       continue;
35
36     if (region->storage_type() == simgrid::mc::StorageType::Privatized) {
37 #if HAVE_SMPI
38       // Use the current process index of the snapshot:
39       if (process_index == simgrid::mc::ProcessIndexDisabled)
40         process_index = snapshot->privatization_index;
41       if (process_index < 0)
42         xbt_die("Missing process index");
43       if (process_index >= (int)region->privatized_data().size())
44         xbt_die("Invalid process index");
45       simgrid::mc::RegionSnapshot& priv_region = region->privatized_data()[process_index];
46       xbt_assert(priv_region.contain(simgrid::mc::remote(addr)));
47       return &priv_region;
48 #else
49       xbt_die("Privatized region in a non SMPI build (this should not happen)");
50 #endif
51     }
52
53     return region;
54   }
55
56   return nullptr;
57 }
58
59 /** @brief Read memory from a snapshot region broken across fragmented pages
60  *
61  *  @param addr    Process (non-snapshot) address of the data
62  *  @param region  Snapshot memory region where the data is located
63  *  @param target  Buffer to store the value
64  *  @param size    Size of the data to read in bytes
65  *  @return Pointer where the data is located (target buffer of original location)
66  */
67 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, size_t size)
68 {
69   // Last byte of the memory area:
70   void* end = (char*)addr + size - 1;
71
72   // TODO, we assume the chunks are aligned to natural chunk boundaries.
73   // We should remove this assumption.
74
75   // Page of the last byte of the memory area:
76   size_t page_end = simgrid::mc::mmu::split((std::uintptr_t)end).first;
77
78   void* dest = target;
79
80   if (dest == nullptr)
81     xbt_die("Missing destination buffer for fragmented memory access");
82
83   // Read each page:
84   while (simgrid::mc::mmu::split((std::uintptr_t)addr).first != page_end) {
85     void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
86     void* next_page     = (void*)simgrid::mc::mmu::join(simgrid::mc::mmu::split((std::uintptr_t)addr).first + 1, 0);
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;
91     size -= readable;
92   }
93
94   // Read the end:
95   void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
96   memcpy(dest, snapshot_addr, size);
97
98   return target;
99 }
100
101 /** Compare memory between snapshots (with known regions)
102  *
103  * @param addr1 Address in the first snapshot
104  * @param region1 Region of the address in the first snapshot
105  * @param addr2 Address in the second snapshot
106  * @param region2 Region of the address in the second snapshot
107  * @return same semantic as memcmp
108  */
109 int MC_snapshot_region_memcmp(const void* addr1, mc_mem_region_t region1, const void* addr2, mc_mem_region_t region2,
110                               size_t size)
111 {
112   // Using alloca() for large allocations may trigger stack overflow:
113   // use malloc if the buffer is too big.
114   bool stack_alloc = size < 64;
115   void* buffer1a   = nullptr;
116   void* buffer2a   = nullptr;
117   if (region1 != nullptr && region1->storage_type() != simgrid::mc::StorageType::Flat)
118     buffer1a = stack_alloc ? alloca(size) : ::operator new(size);
119   if (region2 != nullptr && region2->storage_type() != simgrid::mc::StorageType::Flat)
120     buffer2a = stack_alloc ? alloca(size) : ::operator new(size);
121   const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
122   const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
123   int res;
124   if (buffer1 == buffer2)
125     res = 0;
126   else
127     res = memcmp(buffer1, buffer2, size);
128   if (not stack_alloc) {
129     ::operator delete(buffer1a);
130     ::operator delete(buffer2a);
131   }
132   return res;
133 }
134
135 namespace simgrid {
136 namespace mc {
137
138 Snapshot::Snapshot(RemoteClient* process, int _num_state)
139     : AddressSpace(process)
140     , num_state(_num_state)
141     , heap_bytes_used(0)
142     , enabled_processes()
143     , privatization_index(0)
144     , hash(0)
145 {
146 }
147
148 const void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, int process_index,
149                                  ReadOptions options) const
150 {
151   mc_mem_region_t region = mc_get_snapshot_region((void*)address.address(), this, process_index);
152   if (region) {
153     const void* res = MC_region_read(region, buffer, (void*)address.address(), size);
154     if (buffer == res || options & ReadOptions::lazy())
155       return res;
156     else {
157       memcpy(buffer, res, size);
158       return buffer;
159     }
160   } else
161     return this->process()->read_bytes(buffer, size, address, process_index, options);
162 }
163
164 } // namespace mc
165 } // namespace simgrid