Logo AND Algorithmique Numérique Distribuée

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