Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b2698ea5493038deab7beb1abb0c344681165e13
[simgrid.git] / src / mc / snapshot / unitTest / mc_snapshot_unit.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 #include <boost/test/unit_test.hpp>
9
10 #include <cstdlib>
11 #include <cstring>
12
13 #include <sys/mman.h>
14
15 #include "src/mc/mc_config.hpp"
16 #include "src/mc/mc_mmu.hpp"
17 #include "src/mc/mc_private.hpp"
18 #include "src/mc/mc_snapshot.hpp"
19
20 /**************** Class BOOST_tests *************************/
21 using simgrid::mc::RegionSnapshot;
22 class BOOST_tests {
23   public:
24     static void init_memory(void* mem, size_t size);
25     static void Init(bool sparse_ckpt);
26     typedef struct {
27       size_t size;
28       void* src;
29       void* dstn;
30       RegionSnapshot region0;
31       RegionSnapshot region;
32     } prologue_return;
33     static prologue_return prologue(int n);
34     static void epilogue(); // 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       delete mc_model_checker;
43       mc_model_checker = nullptr;
44     }
45
46   public:
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
56 BOOST_tests::init_memory(void* mem, size_t size) {
57   char* dest = (char*) mem;
58   for (size_t i = 0; i < size; ++i) {
59     dest[i] = rand() & 255;
60   }
61 }
62
63 void
64 BOOST_tests::Init(bool sparse_ckpt) {
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
75 BOOST_tests::prologue_return BOOST_tests::prologue(int n) {
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 = simgrid::mc::sparse_region(
84     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 = simgrid::mc::sparse_region(
89     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, .src = source, .dstn = destination, .region0 = std::move(region0), .region = std::move(region)};
95 }
96
97 void
98 BOOST_tests::read_whole_region() {
99   for(int n=1; n!=256; ++n) {
100
101     prologue_return ret = prologue(n);
102     const void* read = MC_region_read(&(ret.region), ret.dstn, ret.src, ret.size);
103     BOOST_CHECK_MESSAGE(not memcmp(ret.src, read, ret.size), "Mismatch in MC_region_read()");
104
105     munmap(ret.dstn, ret.size);
106     munmap(ret.src, ret.size);
107   }
108 }
109
110 void
111 BOOST_tests::read_region_parts() {
112   for(int n=1; n!=256; ++n) {
113
114      prologue_return ret = prologue(n);
115
116      for(int j=0; j!=100; ++j) {
117        size_t offset = rand() % ret.size;
118        size_t size = rand() % (ret.size - offset);
119        const void* read = MC_region_read(&(ret.region), ret.dstn, (const char*) ret.src+offset, size);
120        BOOST_CHECK_MESSAGE(not memcmp((char*)ret.src + offset, read, size), "Mismatch in MC_region_read()");
121      }
122      munmap(ret.dstn, ret.size);
123      munmap(ret.src, ret.size);
124   }
125 }
126
127 void
128 BOOST_tests::compare_whole_region() {
129   for(int n=1; n!=256; ++n) {
130
131     prologue_return ret = prologue(n);
132
133     BOOST_CHECK_MESSAGE(MC_snapshot_region_memcmp(ret.src, &(ret.region0), ret.src, &(ret.region), ret.size),
134       "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
135       
136     munmap(ret.dstn, ret.size);
137     munmap(ret.src, ret.size);
138   }
139 }
140
141 void
142 BOOST_tests::compare_region_parts() {
143    for(int n=1; n!=256; ++n) {
144
145      prologue_return ret = prologue(n);
146
147      // xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
148      for(int j=0; j!=100; ++j) {
149        size_t offset = rand() % ret.size;
150        size_t size = rand() % (ret.size - offset);
151        BOOST_CHECK_MESSAGE(
152            not MC_snapshot_region_memcmp((char*)ret.src + offset, &(ret.region), (char*)ret.src + offset, &(ret.region), size),
153            "Mismatch in MC_snapshot_region_memcmp()");
154      }
155      munmap(ret.dstn, ret.size);
156      munmap(ret.src, ret.size);
157   }
158 }
159
160 void
161 BOOST_tests::read_pointer() {
162
163   prologue_return ret = prologue(1);
164   // xbt_test_add("Read pointer for %i page(s)", n);
165   memcpy(ret.src, &mc_model_checker, sizeof(void*));
166   simgrid::mc::RegionSnapshot region2 = simgrid::mc::sparse_region(
167     simgrid::mc::RegionType::Unknown, ret.src, ret.src, ret.size);
168   BOOST_CHECK_MESSAGE(MC_region_read_pointer(&region2, ret.src) == mc_model_checker,
169     "Mismtach in MC_region_read_pointer()");
170
171   munmap(ret.dstn, ret.size);
172   munmap(ret.src, ret.size);
173 }
174
175 /*************** End: class BOOST_tests *****************************/
176
177 namespace utf = boost::unit_test; // for test case dependence
178
179 BOOST_AUTO_TEST_SUITE(flat_snapshot)
180 BOOST_AUTO_TEST_CASE(Init) {
181   BOOST_tests::Init(0);
182 }
183
184 BOOST_AUTO_TEST_CASE(read_whole_region, * utf::depends_on("flat_snapshot/Init")) {
185   BOOST_tests::read_whole_region();
186 }
187
188 BOOST_AUTO_TEST_CASE(read_region_parts, * utf::depends_on("flat_snapshot/read_whole_region")) {
189   BOOST_tests::read_region_parts();
190 }
191
192 BOOST_AUTO_TEST_CASE(compare_whole_region, * utf::depends_on("flat_snapshot/read_region_parts")) {
193   BOOST_tests::compare_whole_region();
194 }
195
196 BOOST_AUTO_TEST_CASE(compare_region_parts, * utf::depends_on("flat_snapshot/compare_whole_region")) {
197   BOOST_tests::compare_region_parts();
198 }
199
200 BOOST_AUTO_TEST_CASE(read_pointer, * utf::depends_on("flat_snapshot/compare_region_parts")) {
201   BOOST_tests::read_pointer();
202 }
203
204 // not really a test, just for cleanup the resources
205 BOOST_AUTO_TEST_CASE(cleanup, * utf::depends_on("flat_snapshot/read_pointer")) {
206   BOOST_tests::cleanup();
207 }
208 BOOST_AUTO_TEST_SUITE_END()
209
210 BOOST_AUTO_TEST_SUITE(page_snapshots)
211 BOOST_AUTO_TEST_CASE(Init) {
212   BOOST_tests::Init(1);
213 }
214
215 BOOST_AUTO_TEST_CASE(read_whole_region, * utf::depends_on("page_snapshots/Init")) {
216   BOOST_tests::read_whole_region();
217 }
218
219 BOOST_AUTO_TEST_CASE(read_region_parts, * utf::depends_on("page_snapshots/read_whole_region")) {
220   BOOST_tests::read_region_parts();
221 }
222
223 BOOST_AUTO_TEST_CASE(compare_whole_region, * utf::depends_on("page_snapshots/read_region_parts")) {
224   BOOST_tests::compare_whole_region();
225 }
226
227 BOOST_AUTO_TEST_CASE(compare_region_parts, * utf::depends_on("page_snapshots/compare_whole_region")) {
228   BOOST_tests::compare_region_parts();
229 }
230
231 BOOST_AUTO_TEST_CASE(read_pointer, * utf::depends_on("page_snapshots/compare_region_parts")) {
232   BOOST_tests::read_pointer();
233 }
234
235 // not really a test, just for cleanup the resources
236 BOOST_AUTO_TEST_CASE(cleanup, * utf::depends_on("page_snapshots/read_pointer")) {
237   BOOST_tests::cleanup();
238 }
239 BOOST_AUTO_TEST_SUITE_END()