Logo AND Algorithmique Numérique Distribuée

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