Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to fix the build on centos
[simgrid.git] / src / mc / sosp / mc_snapshot_test.cpp
1 /* Copyright (c) 2014-2018. 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 public:
48   static bool sparse_checkpoint;
49   static std::unique_ptr<simgrid::mc::RemoteClient> process;
50 };
51
52 // static member variables init.
53 bool BOOST_tests::sparse_checkpoint                             = 0;
54 std::unique_ptr<simgrid::mc::RemoteClient> BOOST_tests::process = nullptr;
55
56 void BOOST_tests::init_memory(void* mem, size_t size)
57 {
58   char* dest = (char*)mem;
59   for (size_t i = 0; i < size; ++i) {
60     dest[i] = rand() & 255;
61   }
62 }
63
64 void BOOST_tests::Init(bool sparse_ckpt)
65 {
66   _sg_mc_sparse_checkpoint = sparse_ckpt;
67   BOOST_CHECK_EQUAL(xbt_pagesize, getpagesize());
68   BOOST_CHECK_EQUAL(1 << xbt_pagebits, xbt_pagesize);
69
70   process = std::unique_ptr<simgrid::mc::RemoteClient>(new simgrid::mc::RemoteClient(getpid(), -1));
71   process->init();
72   mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
73 }
74
75 BOOST_tests::prologue_return BOOST_tests::prologue(int n)
76 {
77   // Store region page(s):
78   size_t byte_size = n * xbt_pagesize;
79   void* source     = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
80   BOOST_CHECK_MESSAGE(source != MAP_FAILED, "Could not allocate source memory");
81
82   // Init memory and take snapshots:
83   init_memory(source, byte_size);
84   simgrid::mc::RegionSnapshot region0 =
85       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
86   for (int i = 0; i < n; i += 2) {
87     init_memory((char*)source + i * xbt_pagesize, xbt_pagesize);
88   }
89   simgrid::mc::RegionSnapshot region =
90       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
91
92   void* destination = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
93   BOOST_CHECK_MESSAGE(source != MAP_FAILED, "Could not allocate destination memory");
94
95   return {.size    = byte_size,
96           .src     = source,
97           .dstn    = destination,
98           .region0 = std::move(region0),
99           .region  = std::move(region)};
100 }
101
102 void BOOST_tests::read_whole_region()
103 {
104   for (int n = 1; n != 256; ++n) {
105
106     prologue_return ret = prologue(n);
107     const void* read    = MC_region_read(&(ret.region), ret.dstn, ret.src, ret.size);
108     BOOST_CHECK_MESSAGE(not memcmp(ret.src, read, ret.size), "Mismatch in MC_region_read()");
109
110     munmap(ret.dstn, ret.size);
111     munmap(ret.src, ret.size);
112   }
113 }
114
115 void BOOST_tests::read_region_parts()
116 {
117   for (int n = 1; n != 256; ++n) {
118
119     prologue_return ret = prologue(n);
120
121     for (int j = 0; j != 100; ++j) {
122       size_t offset    = rand() % ret.size;
123       size_t size      = rand() % (ret.size - offset);
124       const void* read = MC_region_read(&(ret.region), ret.dstn, (const char*)ret.src + offset, size);
125       BOOST_CHECK_MESSAGE(not memcmp((char*)ret.src + offset, read, size), "Mismatch in MC_region_read()");
126     }
127     munmap(ret.dstn, ret.size);
128     munmap(ret.src, ret.size);
129   }
130 }
131
132 void BOOST_tests::compare_whole_region()
133 {
134   for (int n = 1; n != 256; ++n) {
135
136     prologue_return ret = prologue(n);
137
138     BOOST_CHECK_MESSAGE(MC_snapshot_region_memcmp(ret.src, &(ret.region0), ret.src, &(ret.region), ret.size),
139                         "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
140
141     munmap(ret.dstn, ret.size);
142     munmap(ret.src, ret.size);
143   }
144 }
145
146 void BOOST_tests::compare_region_parts()
147 {
148   for (int n = 1; n != 256; ++n) {
149
150     prologue_return ret = prologue(n);
151
152     // xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
153     for (int j = 0; j != 100; ++j) {
154       size_t offset = rand() % ret.size;
155       size_t size   = rand() % (ret.size - offset);
156       BOOST_CHECK_MESSAGE(not MC_snapshot_region_memcmp((char*)ret.src + offset, &(ret.region), (char*)ret.src + offset,
157                                                         &(ret.region), size),
158                           "Mismatch in MC_snapshot_region_memcmp()");
159     }
160     munmap(ret.dstn, ret.size);
161     munmap(ret.src, ret.size);
162   }
163 }
164
165 void BOOST_tests::read_pointer()
166 {
167
168   prologue_return ret = prologue(1);
169   // xbt_test_add("Read pointer for %i page(s)", n);
170   memcpy(ret.src, &mc_model_checker, sizeof(void*));
171   simgrid::mc::RegionSnapshot region2 =
172       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, ret.src, ret.src, ret.size);
173   BOOST_CHECK_MESSAGE(MC_region_read_pointer(&region2, ret.src) == mc_model_checker,
174                       "Mismtach in MC_region_read_pointer()");
175
176   munmap(ret.dstn, ret.size);
177   munmap(ret.src, ret.size);
178 }
179
180 /*************** End: class BOOST_tests *****************************/
181
182 namespace utf = boost::unit_test; // for test case dependence
183
184 BOOST_AUTO_TEST_SUITE(flat_snapshot)
185 BOOST_AUTO_TEST_CASE(Init)
186 {
187   BOOST_tests::Init(0);
188 }
189
190 BOOST_AUTO_TEST_CASE(read_whole_region, *utf::depends_on("flat_snapshot/Init"))
191 {
192   BOOST_tests::read_whole_region();
193 }
194
195 BOOST_AUTO_TEST_CASE(read_region_parts, *utf::depends_on("flat_snapshot/read_whole_region"))
196 {
197   BOOST_tests::read_region_parts();
198 }
199
200 BOOST_AUTO_TEST_CASE(compare_whole_region, *utf::depends_on("flat_snapshot/read_region_parts"))
201 {
202   BOOST_tests::compare_whole_region();
203 }
204
205 BOOST_AUTO_TEST_CASE(compare_region_parts, *utf::depends_on("flat_snapshot/compare_whole_region"))
206 {
207   BOOST_tests::compare_region_parts();
208 }
209
210 BOOST_AUTO_TEST_CASE(read_pointer, *utf::depends_on("flat_snapshot/compare_region_parts"))
211 {
212   BOOST_tests::read_pointer();
213 }
214
215 // not really a test, just for cleanup the resources
216 BOOST_AUTO_TEST_CASE(cleanup, *utf::depends_on("flat_snapshot/read_pointer"))
217 {
218   BOOST_tests::cleanup();
219 }
220 BOOST_AUTO_TEST_SUITE_END()
221
222 BOOST_AUTO_TEST_SUITE(page_snapshots)
223 BOOST_AUTO_TEST_CASE(Init)
224 {
225   BOOST_tests::Init(1);
226 }
227
228 BOOST_AUTO_TEST_CASE(read_whole_region, *utf::depends_on("page_snapshots/Init"))
229 {
230   BOOST_tests::read_whole_region();
231 }
232
233 BOOST_AUTO_TEST_CASE(read_region_parts, *utf::depends_on("page_snapshots/read_whole_region"))
234 {
235   BOOST_tests::read_region_parts();
236 }
237
238 BOOST_AUTO_TEST_CASE(compare_whole_region, *utf::depends_on("page_snapshots/read_region_parts"))
239 {
240   BOOST_tests::compare_whole_region();
241 }
242
243 BOOST_AUTO_TEST_CASE(compare_region_parts, *utf::depends_on("page_snapshots/compare_whole_region"))
244 {
245   BOOST_tests::compare_region_parts();
246 }
247
248 BOOST_AUTO_TEST_CASE(read_pointer, *utf::depends_on("page_snapshots/compare_region_parts"))
249 {
250   BOOST_tests::read_pointer();
251 }
252
253 // not really a test, just for cleanup the resources
254 BOOST_AUTO_TEST_CASE(cleanup, *utf::depends_on("page_snapshots/read_pointer"))
255 {
256   BOOST_tests::cleanup();
257 }
258 BOOST_AUTO_TEST_SUITE_END()