Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove useless braces
[simgrid.git] / src / mc / mc_snapshot.cpp
1 /* Copyright (c) 2014-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <cstddef>
8
9 #include <memory>
10 #include <utility>
11
12 #include <xbt/asserts.h>
13 #include <xbt/sysdep.h>
14
15 #include "src/internal_config.h"
16 #include "src/smpi/private.h"
17
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"
22
23 extern "C" {
24
25 /** @brief Find the snapshoted region from a pointer
26  *
27  *  @param addr     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)
31  * */
32 mc_mem_region_t mc_get_snapshot_region(
33   const void* addr, const simgrid::mc::Snapshot* snapshot, int process_index)
34 {
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))))
39       continue;
40
41     if (region->storage_type() == simgrid::mc::StorageType::Privatized) {
42 #ifdef HAVE_SMPI
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)));
52       return &priv_region;
53 #else
54       xbt_die("Privatized region in a non SMPI build (this should not happen)");
55 #endif
56     }
57
58     return region;
59   }
60
61   return nullptr;
62 }
63
64 /** @brief Read memory from a snapshot region broken across fragmented pages
65  *
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)
71  */
72 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, size_t size)
73 {
74   // Last byte of the memory area:
75   void* end = (char*) addr + size - 1;
76
77   // Page of the last byte of the memory area:
78   size_t page_end = mc_page_number(nullptr, end);
79
80   void* dest = target;
81
82   if (dest==nullptr)
83     xbt_die("Missing destination buffer for fragmented memory access");
84
85   // Read each page:
86   while (mc_page_number(nullptr, addr) != page_end) {
87     void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t) addr, region);
88     void* next_page = mc_page_from_number(nullptr, mc_page_number(NULL, addr) + 1);
89     size_t readable = (char*) next_page - (char*) addr;
90     memcpy(dest, snapshot_addr, readable);
91     addr = (char*) addr + readable;
92     dest = (char*) dest + readable;
93     size -= readable;
94   }
95
96   // Read the end:
97   void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
98   memcpy(dest, snapshot_addr, size);
99
100   return target;
101 }
102
103 /** Compare memory between snapshots (with known regions)
104  *
105  * @param addr1 Address in the first snapshot
106  * @param snapshot2 Region of the address in the first snapshot
107  * @param addr2 Address in the second snapshot
108  * @param snapshot2 Region of the address in the second snapshot
109  * @return same as memcmp
110  * */
111 int MC_snapshot_region_memcmp(
112   const void* addr1, mc_mem_region_t region1,
113   const void* addr2, mc_mem_region_t region2,
114   size_t size)
115 {
116   // Using alloca() for large allocations may trigger stack overflow:
117   // use malloc if the buffer is too big.
118   bool stack_alloc = size < 64;
119   const bool region1_need_buffer = region1==nullptr || region1->storage_type()==simgrid::mc::StorageType::Flat;
120   const bool region2_need_buffer = region2==nullptr || region2->storage_type()==simgrid::mc::StorageType::Flat;
121   void* buffer1a = region1_need_buffer ? nullptr : stack_alloc ? alloca(size) : malloc(size);
122   void* buffer2a = region2_need_buffer ? nullptr : stack_alloc ? alloca(size) : malloc(size);
123   const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
124   const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
125   int res;
126   if (buffer1 == buffer2)
127     res = 0;
128   else
129     res = memcmp(buffer1, buffer2, size);
130   if (!stack_alloc) {
131     free(buffer1a);
132     free(buffer2a);
133   }
134   return res;
135 }
136
137 /** Compare memory between snapshots
138  *
139  * @param addr1 Address in the first snapshot
140  * @param snapshot1 First snapshot
141  * @param addr2 Address in the second snapshot
142  * @param snapshot2 Second snapshot
143  * @return same as memcmp
144  * */
145 int MC_snapshot_memcmp(
146   const void* addr1, mc_snapshot_t snapshot1,
147   const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
148 {
149   mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
150   mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
151   return MC_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
152 }
153
154 namespace simgrid {
155 namespace mc {
156
157 Snapshot::Snapshot(Process* process) :
158   AddressSpace(process),
159   num_state(0),
160   heap_bytes_used(0),
161   enabled_processes(),
162   privatization_index(0),
163   hash(0)
164 {
165
166 }
167
168 Snapshot::~Snapshot()
169 {
170
171 }
172
173 const void* Snapshot::read_bytes(void* buffer, std::size_t size,
174   RemotePtr<void> address, int process_index,
175   ReadOptions options) const
176 {
177   mc_mem_region_t region = mc_get_snapshot_region((void*)address.address(), this, process_index);
178   if (region) {
179     const void* res = MC_region_read(region, buffer, (void*)address.address(), size);
180     if (buffer == res || options & ReadOptions::lazy())
181       return res;
182     else {
183       memcpy(buffer, res, size);
184       return buffer;
185     }
186   }
187   else
188     return this->process()->read_bytes(
189       buffer, size, address, process_index, options);
190 }
191
192 }
193 }
194
195 }
196
197 #ifdef SIMGRID_TEST
198
199 #include <string.h>
200 #include <stdlib.h>
201
202 #include <sys/mman.h>
203
204 #include "src/mc/mc_private.h"
205 #include "src/mc/mc_snapshot.h"
206 #include "src/mc/mc_mmu.h"
207
208 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
209
210 static inline void init_memory(void* mem, size_t size)
211 {
212   char* dest = (char*) mem;
213   for (size_t i = 0; i < size; ++i) {
214     dest[i] = rand() & 255;
215   }
216 }
217
218 static void test_snapshot(bool sparse_checkpoint);
219
220 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
221 {
222   test_snapshot(0);
223 }
224
225 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
226 {
227   test_snapshot(1);
228 }
229
230 static void test_snapshot(bool sparse_checkpoint) {
231
232   xbt_test_add("Initialisation");
233   _sg_mc_sparse_checkpoint = sparse_checkpoint;
234   xbt_assert(xbt_pagesize == getpagesize());
235   xbt_assert(1 << xbt_pagebits == xbt_pagesize);
236
237   std::unique_ptr<simgrid::mc::Process> process(new simgrid::mc::Process(getpid(), -1));
238   process->init();
239   mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
240
241   for(int n=1; n!=256; ++n) {
242
243     // Store region page(s):
244     size_t byte_size = n * xbt_pagesize;
245     void* source = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
246     xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
247
248     // Init memory and take snapshots:
249     init_memory(source, byte_size);
250     simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region(
251       simgrid::mc::RegionType::Unknown, source, source, byte_size, nullptr);
252     for(int i=0; i<n; i+=2) {
253       init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
254     }
255     simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
256       simgrid::mc::RegionType::Unknown, source, source, byte_size, nullptr);
257
258     void* destination = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
259     xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
260
261     xbt_test_add("Reading whole region data for %i page(s)", n);
262     const void* read = MC_region_read(&region, destination, source, byte_size);
263     xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
264
265     xbt_test_add("Reading parts of region data for %i page(s)", n);
266     for(int j=0; j!=100; ++j) {
267       size_t offset = rand() % byte_size;
268       size_t size = rand() % (byte_size - offset);
269       const void* read = MC_region_read(&region, destination, (const char*) source+offset, size);
270       xbt_test_assert(!memcmp((char*) source+offset, read, size),
271         "Mismatch in MC_region_read()");
272     }
273
274     xbt_test_add("Compare whole region data for %i page(s)", n);
275
276     xbt_test_assert(MC_snapshot_region_memcmp(source, &region0, source, &region, byte_size),
277       "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
278
279     xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
280     for(int j=0; j!=100; ++j) {
281       size_t offset = rand() % byte_size;
282       size_t size = rand() % (byte_size - offset);
283       xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, &region, (char*) source+offset, &region, size),
284         "Mismatch in MC_snapshot_region_memcmp()");
285     }
286
287     if (n==1) {
288       xbt_test_add("Read pointer for %i page(s)", n);
289       memcpy(source, &mc_model_checker, sizeof(void*));
290       simgrid::mc::RegionSnapshot region2 = simgrid::mc::sparse_region(
291         simgrid::mc::RegionType::Unknown, source, source, byte_size, nullptr);
292       xbt_test_assert(MC_region_read_pointer(&region2, source) == mc_model_checker,
293         "Mismtach in MC_region_read_pointer()");
294     }
295
296     munmap(destination, byte_size);
297     munmap(source, byte_size);
298   }
299
300   delete mc_model_checker;
301   mc_model_checker = nullptr;
302 }
303
304 #endif /* SIMGRID_TEST */
305