Logo AND Algorithmique Numérique Distribuée

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