Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove mc_region_contain(), use the method instead
[simgrid.git] / src / mc / mc_snapshot.cpp
1 /* Copyright (c) 2014. 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 <stdbool.h>
8
9 #include "internal_config.h"
10 #include "smpi/private.h"
11
12 #include "mc_snapshot.h"
13 #include "mc_private.h"
14 #include "mc_mmu.h"
15 #include "PageStore.hpp"
16
17 extern "C" {
18
19 /** @brief Find the snapshoted region from a pointer
20  *
21  *  @param addr     Pointer
22  *  @param snapshot Snapshot
23  *  @param Snapshot region in the snapshot this pointer belongs to
24  *         (or NULL if it does not belong to any snapshot region)
25  * */
26 mc_mem_region_t mc_get_snapshot_region(
27   const void* addr, const s_mc_snapshot_t* snapshot, int process_index)
28 {
29   size_t n = snapshot->snapshot_regions_count;
30   for (size_t i = 0; i != n; ++i) {
31     mc_mem_region_t region = snapshot->snapshot_regions[i];
32     if (!(region && region->contain(simgrid::mc::remote(addr))))
33       continue;
34
35     if (region->storage_type() == simgrid::mc::StorageType::Privatized) {
36 #ifdef HAVE_SMPI
37       // Use the current process index of the snapshot:
38       if (process_index == simgrid::mc::ProcessIndexDisabled) {
39         process_index = snapshot->privatization_index;
40       }
41       if (process_index < 0) {
42         xbt_die("Missing process index");
43       }
44       if (process_index >= (int) region->privatized_data().size()) {
45         xbt_die("Invalid process index");
46       }
47       simgrid::mc::RegionSnapshot& priv_region = region->privatized_data()[process_index];
48       xbt_assert(priv_region.contain(simgrid::mc::remote(addr)));
49       return &priv_region;
50 #else
51       xbt_die("Privatized region in a non SMPI build (this should not happen)");
52 #endif
53     }
54
55     return region;
56   }
57
58   return NULL;
59 }
60
61 /** @brief Read memory from a snapshot region broken across fragmented pages
62  *
63  *  @param addr    Process (non-snapshot) address of the data
64  *  @param region  Snapshot memory region where the data is located
65  *  @param target  Buffer to store the value
66  *  @param size    Size of the data to read in bytes
67  *  @return Pointer where the data is located (target buffer of original location)
68  */
69 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, size_t size)
70 {
71   // Last byte of the memory area:
72   void* end = (char*) addr + size - 1;
73
74   // Page of the last byte of the memory area:
75   size_t page_end = mc_page_number(NULL, end);
76
77   void* dest = target;
78
79   if (dest==NULL) {
80     xbt_die("Missing destination buffer for fragmented memory access");
81   }
82
83   // Read each page:
84   while (mc_page_number(NULL, addr) != page_end) {
85     void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t) addr, region);
86     void* next_page = mc_page_from_number(NULL, mc_page_number(NULL, addr) + 1);
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 snapshot2 Region of the address in the first snapshot
105  * @param addr2 Address in the second snapshot
106  * @param snapshot2 Region of the address in the second snapshot
107  * @return same as memcmp
108  * */
109 int MC_snapshot_region_memcmp(
110   const void* addr1, mc_mem_region_t region1,
111   const void* addr2, mc_mem_region_t region2,
112   size_t size)
113 {
114   // Using alloca() for large allocations may trigger stack overflow:
115   // use malloc if the buffer is too big.
116   bool stack_alloc = size < 64;
117   const bool region1_need_buffer = region1==NULL || region1->storage_type()==simgrid::mc::StorageType::Flat;
118   const bool region2_need_buffer = region2==NULL || region2->storage_type()==simgrid::mc::StorageType::Flat;
119   void* buffer1a = region1_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
120   void* buffer2a = region2_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(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   }
129   if (!stack_alloc) {
130     free(buffer1a);
131     free(buffer2a);
132   }
133   return res;
134 }
135
136 /** Compare memory between snapshots
137  *
138  * @param addr1 Address in the first snapshot
139  * @param snapshot1 First snapshot
140  * @param addr2 Address in the second snapshot
141  * @param snapshot2 Second snapshot
142  * @return same as memcmp
143  * */
144 int MC_snapshot_memcmp(
145   const void* addr1, mc_snapshot_t snapshot1,
146   const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
147 {
148   mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
149   mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
150   return MC_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
151 }
152
153 namespace simgrid {
154 namespace mc {
155
156 Snapshot::Snapshot() :
157   process(nullptr),
158   num_state(0),
159   heap_bytes_used(0),
160   snapshot_regions(nullptr),
161   snapshot_regions_count(0),
162   enabled_processes(0),
163   privatization_index(0),
164   stack_sizes(nullptr),
165   stacks(nullptr),
166   to_ignore(nullptr),
167   hash(0),
168   ignored_data(nullptr),
169   total_fd(0),
170   current_fd(nullptr)
171 {
172
173 }
174 Snapshot::~Snapshot()
175 {
176   for (size_t i = 0; i < this->snapshot_regions_count; i++) {
177     delete this->snapshot_regions[i];
178   }
179   xbt_free(this->snapshot_regions);
180   xbt_free(this->stack_sizes);
181   xbt_dynar_free(&(this->stacks));
182   xbt_dynar_free(&(this->to_ignore));
183   xbt_dynar_free(&this->ignored_data);
184 }
185
186 const void* Snapshot::read_bytes(void* buffer, std::size_t size,
187   remote_ptr<void> address, int process_index,
188   AddressSpace::ReadMode mode) const
189 {
190   mc_mem_region_t region = mc_get_snapshot_region((void*)address.address(), this, process_index);
191   if (region) {
192     const void* res = MC_region_read(region, buffer, (void*)address.address(), size);
193     if (buffer == res || mode == AddressSpace::Lazy)
194       return res;
195     else {
196       memcpy(buffer, res, size);
197       return buffer;
198     }
199   }
200   else
201     return this->read_bytes(buffer, size, address, process_index, mode);
202 }
203
204 }
205 }
206
207 #ifdef SIMGRID_TEST
208
209 #include <string.h>
210 #include <stdlib.h>
211
212 #include <sys/mman.h>
213
214 #include "mc/mc_private.h"
215 #include "mc/mc_snapshot.h"
216 #include "mc/mc_mmu.h"
217
218 extern "C" {
219
220 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
221
222 static inline void init_memory(void* mem, size_t size)
223 {
224   char* dest = (char*) mem;
225   for (size_t i = 0; i < size; ++i) {
226     dest[i] = rand() & 255;
227   }
228 }
229
230 static void test_snapshot(bool sparse_checkpoint);
231
232 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
233 {
234   test_snapshot(0);
235 }
236
237 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
238 {
239   test_snapshot(1);
240 }
241
242 static void test_snapshot(bool sparse_checkpoint) {
243
244   xbt_test_add("Initialisation");
245   _sg_mc_sparse_checkpoint = sparse_checkpoint;
246   xbt_assert(xbt_pagesize == getpagesize());
247   xbt_assert(1 << xbt_pagebits == xbt_pagesize);
248   mc_model_checker = new ::simgrid::mc::ModelChecker(getpid(), -1);
249
250   for(int n=1; n!=256; ++n) {
251
252     // Store region page(s):
253     size_t byte_size = n * xbt_pagesize;
254     void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
255     xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
256
257     // Init memory and take snapshots:
258     init_memory(source, byte_size);
259     simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region(
260       simgrid::mc::RegionType::Unknown, source, source, byte_size);
261     for(int i=0; i<n; i+=2) {
262       init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
263     }
264     simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
265       simgrid::mc::RegionType::Unknown, source, source, byte_size);
266
267     void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
268     xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
269
270     xbt_test_add("Reading whole region data for %i page(s)", n);
271     const void* read = MC_region_read(&region, destination, source, byte_size);
272     xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
273
274     xbt_test_add("Reading parts of region data for %i page(s)", n);
275     for(int j=0; j!=100; ++j) {
276       size_t offset = rand() % byte_size;
277       size_t size = rand() % (byte_size - offset);
278       const void* read = MC_region_read(&region, destination, (const char*) source+offset, size);
279       xbt_test_assert(!memcmp((char*) source+offset, read, size),
280         "Mismatch in MC_region_read()");
281     }
282
283     xbt_test_add("Compare whole region data for %i page(s)", n);
284
285     xbt_test_assert(MC_snapshot_region_memcmp(source, &region0, source, &region, byte_size),
286       "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
287
288     xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
289     for(int j=0; j!=100; ++j) {
290       size_t offset = rand() % byte_size;
291       size_t size = rand() % (byte_size - offset);
292       xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, &region, (char*) source+offset, &region, size),
293         "Mismatch in MC_snapshot_region_memcmp()");
294     }
295
296     if (n==1) {
297       xbt_test_add("Read pointer for %i page(s)", n);
298       memcpy(source, &mc_model_checker, sizeof(void*));
299       simgrid::mc::RegionSnapshot region2 = simgrid::mc::sparse_region(
300         simgrid::mc::RegionType::Unknown, source, source, byte_size);
301       xbt_test_assert(MC_region_read_pointer(&region2, source) == mc_model_checker,
302         "Mismtach in MC_region_read_pointer()");
303     }
304
305     munmap(destination, byte_size);
306     munmap(source, byte_size);
307   }
308
309   delete mc_model_checker;
310   mc_model_checker = NULL;
311 }
312
313 }
314
315 #endif /* SIMGRID_TEST */
316
317 }