Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
80abca9a44d19cb13209d56c58dac8b22ac670c3
[simgrid.git] / src / mc / sosp / mc_snapshot_test.cpp
1 /* Copyright (c) 2014-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/include/catch.hpp"
7
8 #include <cstdlib>
9 #include <cstring>
10
11 #include <sys/mman.h>
12
13 #include "src/mc/mc_config.hpp"
14 #include "src/mc/mc_mmu.hpp"
15 #include "src/mc/mc_private.hpp"
16 #include "src/mc/sosp/mc_snapshot.hpp"
17
18 /**************** Class BOOST_tests *************************/
19 using simgrid::mc::RegionSnapshot;
20 class snap_test_helper {
21 public:
22   static void init_memory(void* mem, size_t size);
23   static void Init(bool sparse_ckpt);
24   typedef struct {
25     size_t size;
26     void* src;
27     void* dstn;
28     RegionSnapshot region0;
29     RegionSnapshot region;
30   } prologue_return;
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()
39   {
40     delete mc_model_checker;
41     mc_model_checker = nullptr;
42   }
43
44   static bool sparse_checkpoint;
45   static std::unique_ptr<simgrid::mc::RemoteClient> process;
46 };
47
48 // static member variables init.
49 bool snap_test_helper::sparse_checkpoint                             = 0;
50 std::unique_ptr<simgrid::mc::RemoteClient> snap_test_helper::process = nullptr;
51
52 void snap_test_helper::init_memory(void* mem, size_t size)
53 {
54   char* dest = (char*)mem;
55   for (size_t i = 0; i < size; ++i) {
56     dest[i] = rand() & 255;
57   }
58 }
59
60 void snap_test_helper::Init(bool sparse_ckpt)
61 {
62   _sg_mc_sparse_checkpoint = sparse_ckpt;
63   REQUIRE(xbt_pagesize == getpagesize());
64   REQUIRE(1 << xbt_pagebits == xbt_pagesize);
65
66   process = std::unique_ptr<simgrid::mc::RemoteClient>(new simgrid::mc::RemoteClient(getpid(), -1));
67   process->init();
68   mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
69 }
70
71 snap_test_helper::prologue_return snap_test_helper::prologue(int n)
72 {
73   // Store region page(s):
74   size_t byte_size = n * xbt_pagesize;
75   void* source     = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
76   INFO("Could not allocate source memory")
77   REQUIRE(source != MAP_FAILED);
78
79   // Init memory and take snapshots:
80   init_memory(source, byte_size);
81   simgrid::mc::RegionSnapshot region0 =
82       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
83   for (int i = 0; i < n; i += 2) {
84     init_memory((char*)source + i * xbt_pagesize, xbt_pagesize);
85   }
86   simgrid::mc::RegionSnapshot region =
87       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
88
89   void* destination = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
90   INFO("Could not allocate destination memory");
91   REQUIRE(source != MAP_FAILED);
92
93   return {.size    = byte_size,
94           .src     = source,
95           .dstn    = destination,
96           .region0 = std::move(region0),
97           .region  = std::move(region)};
98 }
99
100 void snap_test_helper::read_whole_region()
101 {
102   for (int n = 1; n != 256; ++n) {
103
104     prologue_return ret = prologue(n);
105     const void* read    = MC_region_read(&(ret.region), ret.dstn, ret.src, ret.size);
106     INFO("Mismatch in MC_region_read()");
107     REQUIRE(not memcmp(ret.src, read, ret.size));
108
109     munmap(ret.dstn, ret.size);
110     munmap(ret.src, ret.size);
111   }
112 }
113
114 void snap_test_helper::read_region_parts()
115 {
116   for (int n = 1; n != 256; ++n) {
117
118     prologue_return ret = prologue(n);
119
120     for (int j = 0; j != 100; ++j) {
121       size_t offset    = rand() % ret.size;
122       size_t size      = rand() % (ret.size - offset);
123       const void* read = MC_region_read(&(ret.region), ret.dstn, (const char*)ret.src + offset, size);
124       INFO("Mismatch in MC_region_read()");
125       REQUIRE(not memcmp((char*)ret.src + offset, read, size));
126     }
127     munmap(ret.dstn, ret.size);
128     munmap(ret.src, ret.size);
129   }
130 }
131
132 void snap_test_helper::compare_whole_region()
133 {
134   for (int n = 1; n != 256; ++n) {
135
136     prologue_return ret = prologue(n);
137
138     INFO("Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
139     REQUIRE(MC_snapshot_region_memcmp(ret.src, &(ret.region0), ret.src, &(ret.region), ret.size));
140
141     munmap(ret.dstn, ret.size);
142     munmap(ret.src, ret.size);
143   }
144 }
145
146 void snap_test_helper::compare_region_parts()
147 {
148   for (int n = 1; n != 256; ++n) {
149
150     prologue_return ret = prologue(n);
151
152     for (int j = 0; j != 100; ++j) {
153       size_t offset = rand() % ret.size;
154       size_t size   = rand() % (ret.size - offset);
155
156       INFO("Mismatch in MC_snapshot_region_memcmp()");
157       REQUIRE(not MC_snapshot_region_memcmp((char*)ret.src + offset, &(ret.region), (char*)ret.src + offset,
158                                             &(ret.region), size));
159     }
160     munmap(ret.dstn, ret.size);
161     munmap(ret.src, ret.size);
162   }
163 }
164
165 void snap_test_helper::read_pointer()
166 {
167
168   prologue_return ret = prologue(1);
169   memcpy(ret.src, &mc_model_checker, sizeof(void*));
170   simgrid::mc::RegionSnapshot region2 =
171       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, ret.src, ret.src, ret.size);
172   INFO("Mismtach in MC_region_read_pointer()");
173   REQUIRE(MC_region_read_pointer(&region2, ret.src) == mc_model_checker);
174
175   munmap(ret.dstn, ret.size);
176   munmap(ret.src, ret.size);
177 }
178
179 /*************** End: class BOOST_tests *****************************/
180 TEST_CASE("MC::Snapshot: A copy/snapshot of a given memory region", "MC::Snapshot")
181 {
182
183   SECTION("Flat snapshot (no pages)")
184   {
185     snap_test_helper::Init(0);
186
187     INFO("Read whole region");
188     snap_test_helper::read_whole_region();
189
190     INFO("Read region parts");
191     snap_test_helper::read_region_parts();
192
193     INFO("Compare whole region");
194     snap_test_helper::compare_whole_region();
195
196     INFO("Compare region parts");
197     snap_test_helper::compare_region_parts();
198
199     INFO("Read pointer");
200     snap_test_helper::read_pointer();
201
202     snap_test_helper::cleanup();
203   }
204
205   SECTION("Sparse snapshot (using pages)")
206   {
207     snap_test_helper::Init(1);
208
209     INFO("Read whole region");
210     snap_test_helper::read_whole_region();
211
212     INFO("Read region parts");
213     snap_test_helper::read_region_parts();
214
215     INFO("Compare whole region");
216     snap_test_helper::compare_whole_region();
217
218     INFO("Compare region parts");
219     snap_test_helper::compare_region_parts();
220
221     INFO("Read pointer");
222     snap_test_helper::read_pointer();
223
224     snap_test_helper::cleanup();
225   }
226 }