Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'operation-plugin' into 'master'
[simgrid.git] / src / mc / sosp / Snapshot_test.cpp
1 /* Copyright (c) 2014-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/3rd-party/catch.hpp"
7 #include "src/mc/mc_config.hpp"
8 #include "src/mc/sosp/Snapshot.hpp"
9
10 #include <cstddef>
11 #include <memory>
12 #include <sys/mman.h>
13 #include <xbt/random.hpp>
14
15 /**************** Class BOOST_tests *************************/
16 using simgrid::mc::Region;
17 class snap_test_helper {
18   static simgrid::mc::PageStore page_store_;
19   static std::unique_ptr<simgrid::mc::RemoteProcessMemory> memory_;
20
21 public:
22   static void init_memory(void* mem, size_t size);
23   static void Init();
24   struct prologue_return {
25     size_t size;
26     void* src;
27     void* dstn;
28     std::unique_ptr<Region> region0;
29     std::unique_ptr<Region> region;
30   };
31   static prologue_return prologue(int n); // common to the below 5 fxs
32   static void read_whole_region();
33   static void read_region_parts();
34   static void compare_whole_region();
35   static void compare_region_parts();
36   static void read_pointer();
37
38   static void cleanup() { memory_ = nullptr; }
39 };
40
41 // static member variables init.
42 simgrid::mc::PageStore snap_test_helper::page_store_(500);
43 std::unique_ptr<simgrid::mc::RemoteProcessMemory> snap_test_helper::memory_ = nullptr;
44
45 void snap_test_helper::init_memory(void* mem, size_t size)
46 {
47   auto* dest = static_cast<char*>(mem);
48   for (size_t i = 0; i < size; ++i) {
49     dest[i] = simgrid::xbt::random::uniform_int(0, 0xff);
50   }
51 }
52
53 void snap_test_helper::Init()
54 {
55   REQUIRE(xbt_pagesize == getpagesize());
56   REQUIRE(1 << xbt_pagebits == xbt_pagesize);
57
58   memory_ = std::make_unique<simgrid::mc::RemoteProcessMemory>(getpid(), nullptr);
59 }
60
61 snap_test_helper::prologue_return snap_test_helper::prologue(int n)
62 {
63   // Store region page(s):
64   size_t byte_size = n * xbt_pagesize;
65   void* source     = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
66   INFO("Could not allocate source memory");
67   REQUIRE(source != MAP_FAILED);
68
69   // Init memory and take snapshots:
70   init_memory(source, byte_size);
71   auto region0 = std::make_unique<simgrid::mc::Region>(page_store_, *memory_.get(), simgrid::mc::RegionType::Data,
72                                                        source, byte_size);
73   for (int i = 0; i < n; i += 2) {
74     init_memory((char*)source + i * xbt_pagesize, xbt_pagesize);
75   }
76   auto region = std::make_unique<simgrid::mc::Region>(page_store_, *memory_.get(), simgrid::mc::RegionType::Data,
77                                                       source, byte_size);
78
79   void* destination = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
80   INFO("Could not allocate destination memory");
81   REQUIRE(destination != MAP_FAILED);
82
83   return {.size    = byte_size,
84           .src     = source,
85           .dstn    = destination,
86           .region0 = std::move(region0),
87           .region  = std::move(region)};
88 }
89
90 void snap_test_helper::read_whole_region()
91 {
92   for (int n = 1; n != 32; ++n) {
93     prologue_return ret = prologue(n);
94     const void* read    = ret.region->read(ret.dstn, ret.src, ret.size);
95     INFO("Mismatch in MC_region_read()");
96     REQUIRE(not memcmp(ret.src, read, ret.size));
97
98     munmap(ret.dstn, ret.size);
99     munmap(ret.src, ret.size);
100   }
101 }
102
103 void snap_test_helper::read_region_parts()
104 {
105   for (int n = 1; n != 32; ++n) {
106     prologue_return ret = prologue(n);
107
108     for (int j = 0; j != 100; ++j) {
109       size_t offset    = simgrid::xbt::random::uniform_int(0, ret.size - 1);
110       size_t size      = simgrid::xbt::random::uniform_int(0, ret.size - offset - 1);
111       const void* read = ret.region->read(ret.dstn, (const char*)ret.src + offset, size);
112       INFO("Mismatch in MC_region_read()");
113       REQUIRE(not memcmp((char*)ret.src + offset, read, size));
114     }
115     munmap(ret.dstn, ret.size);
116     munmap(ret.src, ret.size);
117   }
118 }
119
120 void snap_test_helper::compare_whole_region()
121 {
122   for (int n = 1; n != 32; ++n) {
123     prologue_return ret = prologue(n);
124
125     INFO("Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
126     REQUIRE(MC_snapshot_region_memcmp(ret.src, ret.region0.get(), ret.src, ret.region.get(), ret.size));
127
128     munmap(ret.dstn, ret.size);
129     munmap(ret.src, ret.size);
130   }
131 }
132
133 void snap_test_helper::compare_region_parts()
134 {
135   for (int n = 1; n != 32; ++n) {
136     prologue_return ret = prologue(n);
137
138     for (int j = 0; j != 100; ++j) {
139       size_t offset = simgrid::xbt::random::uniform_int(0, ret.size - 1);
140       size_t size   = simgrid::xbt::random::uniform_int(0, ret.size - offset - 1);
141
142       INFO("Mismatch in MC_snapshot_region_memcmp()");
143       REQUIRE(not MC_snapshot_region_memcmp((char*)ret.src + offset, ret.region.get(), (char*)ret.src + offset,
144                                             ret.region.get(), size));
145     }
146     munmap(ret.dstn, ret.size);
147     munmap(ret.src, ret.size);
148   }
149 }
150
151 const int some_global_variable  = 42;
152 const void* some_global_pointer = &some_global_variable;
153 void snap_test_helper::read_pointer()
154 {
155   prologue_return ret = prologue(1);
156   memcpy(ret.src, &some_global_pointer, sizeof(void*));
157   const simgrid::mc::Region region2(page_store_, *memory_.get(), simgrid::mc::RegionType::Data, ret.src, ret.size);
158   INFO("Mismtach in MC_region_read_pointer()");
159   REQUIRE(MC_region_read_pointer(&region2, ret.src) == some_global_pointer);
160
161   munmap(ret.dstn, ret.size);
162   munmap(ret.src, ret.size);
163 }
164
165 /*************** End: class snap_test_helper *****************************/
166
167 TEST_CASE("MC::Snapshot: A copy/snapshot of a given memory region", "MC::Snapshot")
168 {
169   INFO("Sparse snapshot (using pages)");
170
171   snap_test_helper::Init();
172
173   INFO("Read whole region");
174   snap_test_helper::read_whole_region();
175
176   INFO("Read region parts");
177   snap_test_helper::read_region_parts();
178
179   INFO("Compare whole region");
180   snap_test_helper::compare_whole_region();
181
182   INFO("Compare region parts");
183   snap_test_helper::compare_region_parts();
184
185   INFO("Read pointer");
186   snap_test_helper::read_pointer();
187
188   snap_test_helper::cleanup();
189 }