Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Rename PerPageCopy to ChunkedData (and move in its own files)
[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 <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 simgrid::mc::Snapshot* snapshot, int process_index)
28 {
29   size_t n = snapshot->snapshot_regions.size();
30   for (size_t i = 0; i != n; ++i) {
31     mc_mem_region_t region = snapshot->snapshot_regions[i].get();
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(Process* process) :
157   AddressSpace(process),
158   num_state(0),
159   heap_bytes_used(0),
160   enabled_processes(),
161   privatization_index(0),
162   hash(0)
163 {
164
165 }
166
167 Snapshot::~Snapshot()
168 {
169
170 }
171
172 const void* Snapshot::read_bytes(void* buffer, std::size_t size,
173   remote_ptr<void> address, int process_index,
174   AddressSpace::ReadMode mode) const
175 {
176   mc_mem_region_t region = mc_get_snapshot_region((void*)address.address(), this, process_index);
177   if (region) {
178     const void* res = MC_region_read(region, buffer, (void*)address.address(), size);
179     if (buffer == res || mode == AddressSpace::Lazy)
180       return res;
181     else {
182       memcpy(buffer, res, size);
183       return buffer;
184     }
185   }
186   else
187     return this->process()->read_bytes(
188       buffer, size, address, process_index, mode);
189 }
190
191 }
192 }
193
194 #ifdef SIMGRID_TEST
195
196 #include <string.h>
197 #include <stdlib.h>
198
199 #include <sys/mman.h>
200
201 #include "mc/mc_private.h"
202 #include "mc/mc_snapshot.h"
203 #include "mc/mc_mmu.h"
204
205 extern "C" {
206
207 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
208
209 static inline void init_memory(void* mem, size_t size)
210 {
211   char* dest = (char*) mem;
212   for (size_t i = 0; i < size; ++i) {
213     dest[i] = rand() & 255;
214   }
215 }
216
217 static void test_snapshot(bool sparse_checkpoint);
218
219 XBT_TEST_UNIT("flat_snapshot", test_flat_snapshots, "Test flat snapshots")
220 {
221   test_snapshot(0);
222 }
223
224 XBT_TEST_UNIT("page_snapshots", test_per_snpashots, "Test per-page snapshots")
225 {
226   test_snapshot(1);
227 }
228
229 static void test_snapshot(bool sparse_checkpoint) {
230
231   xbt_test_add("Initialisation");
232   _sg_mc_sparse_checkpoint = sparse_checkpoint;
233   xbt_assert(xbt_pagesize == getpagesize());
234   xbt_assert(1 << xbt_pagebits == xbt_pagesize);
235   mc_model_checker = new ::simgrid::mc::ModelChecker(getpid(), -1);
236
237   for(int n=1; n!=256; ++n) {
238
239     // Store region page(s):
240     size_t byte_size = n * xbt_pagesize;
241     void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
242     xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
243
244     // Init memory and take snapshots:
245     init_memory(source, byte_size);
246     simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region(
247       simgrid::mc::RegionType::Unknown, source, source, byte_size, nullptr);
248     for(int i=0; i<n; i+=2) {
249       init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
250     }
251     simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
252       simgrid::mc::RegionType::Unknown, source, source, byte_size, nullptr);
253
254     void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
255     xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
256
257     xbt_test_add("Reading whole region data for %i page(s)", n);
258     const void* read = MC_region_read(&region, destination, source, byte_size);
259     xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
260
261     xbt_test_add("Reading parts of region data for %i page(s)", n);
262     for(int j=0; j!=100; ++j) {
263       size_t offset = rand() % byte_size;
264       size_t size = rand() % (byte_size - offset);
265       const void* read = MC_region_read(&region, destination, (const char*) source+offset, size);
266       xbt_test_assert(!memcmp((char*) source+offset, read, size),
267         "Mismatch in MC_region_read()");
268     }
269
270     xbt_test_add("Compare whole region data for %i page(s)", n);
271
272     xbt_test_assert(MC_snapshot_region_memcmp(source, &region0, source, &region, byte_size),
273       "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
274
275     xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
276     for(int j=0; j!=100; ++j) {
277       size_t offset = rand() % byte_size;
278       size_t size = rand() % (byte_size - offset);
279       xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, &region, (char*) source+offset, &region, size),
280         "Mismatch in MC_snapshot_region_memcmp()");
281     }
282
283     if (n==1) {
284       xbt_test_add("Read pointer for %i page(s)", n);
285       memcpy(source, &mc_model_checker, sizeof(void*));
286       simgrid::mc::RegionSnapshot region2 = simgrid::mc::sparse_region(
287         simgrid::mc::RegionType::Unknown, source, source, byte_size, nullptr);
288       xbt_test_assert(MC_region_read_pointer(&region2, source) == mc_model_checker,
289         "Mismtach in MC_region_read_pointer()");
290     }
291
292     munmap(destination, byte_size);
293     munmap(source, byte_size);
294   }
295
296   delete mc_model_checker;
297   mc_model_checker = NULL;
298 }
299
300 }
301
302 #endif /* SIMGRID_TEST */
303
304 }