1 /* Copyright (c) 2014-2018. The SimGrid Team. All rights reserved. */
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. */
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>
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"
21 /**************** Class BOOST_tests *************************/
22 using simgrid::mc::RegionSnapshot;
25 static void init_memory(void* mem, size_t size);
26 static void Init(bool sparse_ckpt);
31 RegionSnapshot region0;
32 RegionSnapshot region;
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();
43 delete mc_model_checker;
44 mc_model_checker = nullptr;
48 static bool sparse_checkpoint;
49 static std::unique_ptr<simgrid::mc::RemoteClient> process;
52 // static member variables init.
53 bool BOOST_tests::sparse_checkpoint = 0;
54 std::unique_ptr<simgrid::mc::RemoteClient> BOOST_tests::process = nullptr;
56 void BOOST_tests::init_memory(void* mem, size_t size)
58 char* dest = (char*)mem;
59 for (size_t i = 0; i < size; ++i) {
60 dest[i] = rand() & 255;
64 void BOOST_tests::Init(bool sparse_ckpt)
66 _sg_mc_sparse_checkpoint = sparse_ckpt;
67 BOOST_CHECK_EQUAL(xbt_pagesize, getpagesize());
68 BOOST_CHECK_EQUAL(1 << xbt_pagebits, xbt_pagesize);
70 process = std::unique_ptr<simgrid::mc::RemoteClient>(new simgrid::mc::RemoteClient(getpid(), -1));
72 mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
75 BOOST_tests::prologue_return BOOST_tests::prologue(int n)
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");
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);
89 simgrid::mc::RegionSnapshot region =
90 simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
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");
95 return {.size = byte_size,
98 .region0 = std::move(region0),
99 .region = std::move(region)};
102 void BOOST_tests::read_whole_region()
104 for (int n = 1; n != 256; ++n) {
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()");
110 munmap(ret.dstn, ret.size);
111 munmap(ret.src, ret.size);
115 void BOOST_tests::read_region_parts()
117 for (int n = 1; n != 256; ++n) {
119 prologue_return ret = prologue(n);
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()");
127 munmap(ret.dstn, ret.size);
128 munmap(ret.src, ret.size);
132 void BOOST_tests::compare_whole_region()
134 for (int n = 1; n != 256; ++n) {
136 prologue_return ret = prologue(n);
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");
141 munmap(ret.dstn, ret.size);
142 munmap(ret.src, ret.size);
146 void BOOST_tests::compare_region_parts()
148 for (int n = 1; n != 256; ++n) {
150 prologue_return ret = prologue(n);
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()");
160 munmap(ret.dstn, ret.size);
161 munmap(ret.src, ret.size);
165 void BOOST_tests::read_pointer()
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(®ion2, ret.src) == mc_model_checker,
174 "Mismtach in MC_region_read_pointer()");
176 munmap(ret.dstn, ret.size);
177 munmap(ret.src, ret.size);
180 /*************** End: class BOOST_tests *****************************/
182 namespace utf = boost::unit_test; // for test case dependence
184 BOOST_AUTO_TEST_SUITE(flat_snapshot)
185 BOOST_AUTO_TEST_CASE(Init)
187 BOOST_tests::Init(0);
190 BOOST_AUTO_TEST_CASE(read_whole_region, *utf::depends_on("flat_snapshot/Init"))
192 BOOST_tests::read_whole_region();
195 BOOST_AUTO_TEST_CASE(read_region_parts, *utf::depends_on("flat_snapshot/read_whole_region"))
197 BOOST_tests::read_region_parts();
200 BOOST_AUTO_TEST_CASE(compare_whole_region, *utf::depends_on("flat_snapshot/read_region_parts"))
202 BOOST_tests::compare_whole_region();
205 BOOST_AUTO_TEST_CASE(compare_region_parts, *utf::depends_on("flat_snapshot/compare_whole_region"))
207 BOOST_tests::compare_region_parts();
210 BOOST_AUTO_TEST_CASE(read_pointer, *utf::depends_on("flat_snapshot/compare_region_parts"))
212 BOOST_tests::read_pointer();
215 // not really a test, just for cleanup the resources
216 BOOST_AUTO_TEST_CASE(cleanup, *utf::depends_on("flat_snapshot/read_pointer"))
218 BOOST_tests::cleanup();
220 BOOST_AUTO_TEST_SUITE_END()
222 BOOST_AUTO_TEST_SUITE(page_snapshots)
223 BOOST_AUTO_TEST_CASE(Init)
225 BOOST_tests::Init(1);
228 BOOST_AUTO_TEST_CASE(read_whole_region, *utf::depends_on("page_snapshots/Init"))
230 BOOST_tests::read_whole_region();
233 BOOST_AUTO_TEST_CASE(read_region_parts, *utf::depends_on("page_snapshots/read_whole_region"))
235 BOOST_tests::read_region_parts();
238 BOOST_AUTO_TEST_CASE(compare_whole_region, *utf::depends_on("page_snapshots/read_region_parts"))
240 BOOST_tests::compare_whole_region();
243 BOOST_AUTO_TEST_CASE(compare_region_parts, *utf::depends_on("page_snapshots/compare_whole_region"))
245 BOOST_tests::compare_region_parts();
248 BOOST_AUTO_TEST_CASE(read_pointer, *utf::depends_on("page_snapshots/compare_region_parts"))
250 BOOST_tests::read_pointer();
253 // not really a test, just for cleanup the resources
254 BOOST_AUTO_TEST_CASE(cleanup, *utf::depends_on("page_snapshots/read_pointer"))
256 BOOST_tests::cleanup();
258 BOOST_AUTO_TEST_SUITE_END()