Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MC: switch to xxhash as a (fast) hashing function.
[simgrid.git] / src / mc / sosp / Region.cpp
1 /* Copyright (c) 2007-2019. 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 "src/mc/ModelChecker.hpp"
7 #include "src/mc/mc_config.hpp"
8 #include "src/mc/mc_forward.hpp"
9
10 #include "src/mc/mc_smx.hpp"
11 #include "src/mc/sosp/Region.hpp"
12
13 #include <cstdlib>
14 #include <sys/mman.h>
15 #ifdef __FreeBSD__
16 #define MAP_POPULATE MAP_PREFAULT_READ
17 #endif
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc, "Logging specific to region snapshots");
20
21 namespace simgrid {
22 namespace mc {
23
24 Region::Region(RegionType region_type, void* start_addr, size_t size)
25     : region_type_(region_type), start_addr_(start_addr), size_(size)
26 {
27   xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize - 1)) == 0, "Start address not at the beginning of a page");
28
29   chunks_ = ChunkedData(mc_model_checker->page_store(), mc_model_checker->process(), RemotePtr<void>(start_addr),
30                         mmu::chunk_count(size));
31 }
32
33 /** @brief Restore a region from a snapshot
34  *
35  *  @param region     Target region
36  */
37 void Region::restore()
38 {
39   xbt_assert(((start().address()) & (xbt_pagesize - 1)) == 0, "Not at the beginning of a page");
40   xbt_assert(simgrid::mc::mmu::chunk_count(size()) == get_chunks().page_count());
41
42   for (size_t i = 0; i != get_chunks().page_count(); ++i) {
43     void* target_page       = (void*)simgrid::mc::mmu::join(i, (std::uintptr_t)(void*)start().address());
44     const void* source_page = get_chunks().page(i);
45     mc_model_checker->process().write_bytes(source_page, xbt_pagesize, remote(target_page));
46   }
47 }
48
49 static XBT_ALWAYS_INLINE void* mc_translate_address_region(uintptr_t addr, simgrid::mc::Region* region)
50 {
51   auto split                = simgrid::mc::mmu::split(addr - region->start().address());
52   auto pageno               = split.first;
53   auto offset               = split.second;
54   const void* snapshot_page = region->get_chunks().page(pageno);
55   return (char*)snapshot_page + offset;
56 }
57
58 void* Region::read(void* target, const void* addr, std::size_t size)
59 {
60   xbt_assert(contain(simgrid::mc::remote(addr)), "Trying to read out of the region boundary.");
61
62   // Last byte of the region:
63   void* end = (char*)addr + size - 1;
64   if (simgrid::mc::mmu::same_chunk((std::uintptr_t)addr, (std::uintptr_t)end)) {
65     // The memory is contained in a single page:
66     return mc_translate_address_region((uintptr_t)addr, this);
67   }
68   // Otherwise, the memory spans several pages. Let's copy it all into the provided buffer
69   xbt_assert(target != nullptr, "Missing destination buffer for fragmented memory access");
70
71   // TODO, we assume the chunks are aligned to natural chunk boundaries.
72   // We should remove this assumption.
73
74   // Page of the last byte of the memory area:
75   size_t page_end = simgrid::mc::mmu::split((std::uintptr_t)end).first;
76
77   void* dest = target; // iterator in the buffer to where we should copy next
78
79   // Read each page:
80   while (simgrid::mc::mmu::split((std::uintptr_t)addr).first != page_end) {
81     void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, this);
82     void* next_page     = (void*)simgrid::mc::mmu::join(simgrid::mc::mmu::split((std::uintptr_t)addr).first + 1, 0);
83     size_t readable     = (char*)next_page - (char*)addr;
84     memcpy(dest, snapshot_addr, readable);
85     addr = (char*)addr + readable;
86     dest = (char*)dest + readable;
87     size -= readable;
88   }
89
90   // Read the end:
91   void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, this);
92   memcpy(dest, snapshot_addr, size);
93
94   return target;
95 }
96
97 } // namespace mc
98 } // namespace simgrid
99
100 /** Compare memory between snapshots (with known regions)
101  *
102  * @param addr1 Address in the first snapshot
103  * @param region1 Region of the address in the first snapshot
104  * @param addr2 Address in the second snapshot
105  * @param region2 Region of the address in the second snapshot
106  * @return same semantic as memcmp
107  */
108 int MC_snapshot_region_memcmp(const void* addr1, simgrid::mc::Region* region1, const void* addr2,
109                               simgrid::mc::Region* region2, size_t size)
110 {
111   // Using alloca() for large allocations may trigger stack overflow:
112   // use malloc if the buffer is too big.
113   bool stack_alloc    = size < 64;
114   void* buffer1a      = stack_alloc ? alloca(size) : ::operator new(size);
115   void* buffer2a      = stack_alloc ? alloca(size) : ::operator new(size);
116   const void* buffer1 = region1->read(buffer1a, addr1, size);
117   const void* buffer2 = region2->read(buffer2a, addr2, size);
118   int res;
119   if (buffer1 == buffer2)
120     res = 0;
121   else
122     res = memcmp(buffer1, buffer2, size);
123   if (not stack_alloc) {
124     ::operator delete(buffer1a);
125     ::operator delete(buffer2a);
126   }
127   return res;
128 }