Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ad4bae7e52bbea2317cbe2a164ac97e64bb6b2bd
[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 #define BOOST_TEST_MODULE snapshots
7 #define BOOST_TEST_DYN_LINK
8 bool init_unit_test(); // boost sometimes forget to give this prototype (NetBSD and other), which does not fit our paranoid flags
9 #include <boost/test/unit_test.hpp>
10
11 #include <cstdlib>
12 #include <cstring>
13
14 #include <sys/mman.h>
15
16 #include "src/mc/mc_config.hpp"
17 #include "src/mc/mc_mmu.hpp"
18 #include "src/mc/mc_private.hpp"
19 #include "src/mc/sosp/mc_snapshot.hpp"
20
21 /**************** Class BOOST_tests *************************/
22 using simgrid::mc::RegionSnapshot;
23 class BOOST_tests {
24 public:
25   static void init_memory(void* mem, size_t size);
26   static void Init(bool sparse_ckpt);
27   typedef struct {
28     size_t size;
29     void* src;
30     void* dstn;
31     RegionSnapshot region0;
32     RegionSnapshot region;
33   } prologue_return;
34   static prologue_return prologue(int n); // common to the below 5 fxs
35   static void read_whole_region();
36   static void read_region_parts();
37   static void compare_whole_region();
38   static void compare_region_parts();
39   static void read_pointer();
40
41   static void cleanup()
42   {
43     delete mc_model_checker;
44     mc_model_checker = nullptr;
45   }
46
47   static bool sparse_checkpoint;
48   static std::unique_ptr<simgrid::mc::RemoteClient> process;
49 };
50
51 // static member variables init.
52 bool BOOST_tests::sparse_checkpoint                             = 0;
53 std::unique_ptr<simgrid::mc::RemoteClient> BOOST_tests::process = nullptr;
54
55 void BOOST_tests::init_memory(void* mem, size_t size)
56 {
57   char* dest = (char*)mem;
58   for (size_t i = 0; i < size; ++i) {
59     dest[i] = rand() & 255;
60   }
61 }
62
63 void BOOST_tests::Init(bool sparse_ckpt)
64 {
65   _sg_mc_sparse_checkpoint = sparse_ckpt;
66   BOOST_CHECK_EQUAL(xbt_pagesize, getpagesize());
67   BOOST_CHECK_EQUAL(1 << xbt_pagebits, xbt_pagesize);
68
69   process = std::unique_ptr<simgrid::mc::RemoteClient>(new simgrid::mc::RemoteClient(getpid(), -1));
70   process->init();
71   mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
72 }
73
74 BOOST_tests::prologue_return BOOST_tests::prologue(int n)
75 {
76   // Store region page(s):
77   size_t byte_size = n * xbt_pagesize;
78   void* source     = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
79   BOOST_CHECK_MESSAGE(source != MAP_FAILED, "Could not allocate source memory");
80
81   // Init memory and take snapshots:
82   init_memory(source, byte_size);
83   simgrid::mc::RegionSnapshot region0 =
84       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
85   for (int i = 0; i < n; i += 2) {
86     init_memory((char*)source + i * xbt_pagesize, xbt_pagesize);
87   }
88   simgrid::mc::RegionSnapshot region =
89       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
90
91   void* destination = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
92   BOOST_CHECK_MESSAGE(source != MAP_FAILED, "Could not allocate destination memory");
93
94   return {.size    = byte_size,
95           .src     = source,
96           .dstn    = destination,
97           .region0 = std::move(region0),
98           .region  = std::move(region)};
99 }
100
101 void BOOST_tests::read_whole_region()
102 {
103   for (int n = 1; n != 256; ++n) {
104
105     prologue_return ret = prologue(n);
106     const void* read    = MC_region_read(&(ret.region), ret.dstn, ret.src, ret.size);
107     BOOST_CHECK_MESSAGE(not memcmp(ret.src, read, ret.size), "Mismatch in MC_region_read()");
108
109     munmap(ret.dstn, ret.size);
110     munmap(ret.src, ret.size);
111   }
112 }
113
114 void BOOST_tests::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       BOOST_CHECK_MESSAGE(not memcmp((char*)ret.src + offset, read, size), "Mismatch in MC_region_read()");
125     }
126     munmap(ret.dstn, ret.size);
127     munmap(ret.src, ret.size);
128   }
129 }
130
131 void BOOST_tests::compare_whole_region()
132 {
133   for (int n = 1; n != 256; ++n) {
134
135     prologue_return ret = prologue(n);
136
137     BOOST_CHECK_MESSAGE(MC_snapshot_region_memcmp(ret.src, &(ret.region0), ret.src, &(ret.region), ret.size),
138                         "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
139
140     munmap(ret.dstn, ret.size);
141     munmap(ret.src, ret.size);
142   }
143 }
144
145 void BOOST_tests::compare_region_parts()
146 {
147   for (int n = 1; n != 256; ++n) {
148
149     prologue_return ret = prologue(n);
150
151     for (int j = 0; j != 100; ++j) {
152       size_t offset = rand() % ret.size;
153       size_t size   = rand() % (ret.size - offset);
154       BOOST_CHECK_MESSAGE(not MC_snapshot_region_memcmp((char*)ret.src + offset, &(ret.region), (char*)ret.src + offset,
155                                                         &(ret.region), size),
156                           "Mismatch in MC_snapshot_region_memcmp()");
157     }
158     munmap(ret.dstn, ret.size);
159     munmap(ret.src, ret.size);
160   }
161 }
162
163 void BOOST_tests::read_pointer()
164 {
165
166   prologue_return ret = prologue(1);
167   memcpy(ret.src, &mc_model_checker, sizeof(void*));
168   simgrid::mc::RegionSnapshot region2 =
169       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, ret.src, ret.src, ret.size);
170   BOOST_CHECK_MESSAGE(MC_region_read_pointer(&region2, ret.src) == mc_model_checker,
171                       "Mismtach in MC_region_read_pointer()");
172
173   munmap(ret.dstn, ret.size);
174   munmap(ret.src, ret.size);
175 }
176
177 /*************** End: class BOOST_tests *****************************/
178
179 namespace utf = boost::unit_test; // for test case dependence
180
181 BOOST_AUTO_TEST_SUITE(flat_snapshot)
182 BOOST_AUTO_TEST_CASE(Init)
183 {
184   BOOST_tests::Init(0);
185 }
186
187 BOOST_AUTO_TEST_CASE(read_whole_region, *utf::depends_on("flat_snapshot/Init"))
188 {
189   BOOST_tests::read_whole_region();
190 }
191
192 BOOST_AUTO_TEST_CASE(read_region_parts, *utf::depends_on("flat_snapshot/read_whole_region"))
193 {
194   BOOST_tests::read_region_parts();
195 }
196
197 BOOST_AUTO_TEST_CASE(compare_whole_region, *utf::depends_on("flat_snapshot/read_region_parts"))
198 {
199   BOOST_tests::compare_whole_region();
200 }
201
202 BOOST_AUTO_TEST_CASE(compare_region_parts, *utf::depends_on("flat_snapshot/compare_whole_region"))
203 {
204   BOOST_tests::compare_region_parts();
205 }
206
207 BOOST_AUTO_TEST_CASE(read_pointer, *utf::depends_on("flat_snapshot/compare_region_parts"))
208 {
209   BOOST_tests::read_pointer();
210 }
211
212 // not really a test, just for cleanup the resources
213 BOOST_AUTO_TEST_CASE(cleanup, *utf::depends_on("flat_snapshot/read_pointer"))
214 {
215   BOOST_tests::cleanup();
216 }
217 BOOST_AUTO_TEST_SUITE_END()
218
219 BOOST_AUTO_TEST_SUITE(page_snapshots)
220 BOOST_AUTO_TEST_CASE(Init)
221 {
222   BOOST_tests::Init(1);
223 }
224
225 BOOST_AUTO_TEST_CASE(read_whole_region, *utf::depends_on("page_snapshots/Init"))
226 {
227   BOOST_tests::read_whole_region();
228 }
229
230 BOOST_AUTO_TEST_CASE(read_region_parts, *utf::depends_on("page_snapshots/read_whole_region"))
231 {
232   BOOST_tests::read_region_parts();
233 }
234
235 BOOST_AUTO_TEST_CASE(compare_whole_region, *utf::depends_on("page_snapshots/read_region_parts"))
236 {
237   BOOST_tests::compare_whole_region();
238 }
239
240 BOOST_AUTO_TEST_CASE(compare_region_parts, *utf::depends_on("page_snapshots/compare_whole_region"))
241 {
242   BOOST_tests::compare_region_parts();
243 }
244
245 BOOST_AUTO_TEST_CASE(read_pointer, *utf::depends_on("page_snapshots/compare_region_parts"))
246 {
247   BOOST_tests::read_pointer();
248 }
249
250 // not really a test, just for cleanup the resources
251 BOOST_AUTO_TEST_CASE(cleanup, *utf::depends_on("page_snapshots/read_pointer"))
252 {
253   BOOST_tests::cleanup();
254 }
255 BOOST_AUTO_TEST_SUITE_END()