1 /* Copyright (c) 2014-2019. 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;
47 static bool sparse_checkpoint;
48 static std::unique_ptr<simgrid::mc::RemoteClient> process;
51 // static member variables init.
52 bool BOOST_tests::sparse_checkpoint = 0;
53 std::unique_ptr<simgrid::mc::RemoteClient> BOOST_tests::process = nullptr;
55 void 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;
63 void 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);
69 process = std::unique_ptr<simgrid::mc::RemoteClient>(new simgrid::mc::RemoteClient(getpid(), -1));
71 mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
74 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");
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);
88 simgrid::mc::RegionSnapshot region =
89 simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
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");
94 return {.size = byte_size,
97 .region0 = std::move(region0),
98 .region = std::move(region)};
101 void BOOST_tests::read_whole_region()
103 for (int n = 1; n != 256; ++n) {
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()");
109 munmap(ret.dstn, ret.size);
110 munmap(ret.src, ret.size);
114 void BOOST_tests::read_region_parts()
116 for (int n = 1; n != 256; ++n) {
118 prologue_return ret = prologue(n);
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()");
126 munmap(ret.dstn, ret.size);
127 munmap(ret.src, ret.size);
131 void BOOST_tests::compare_whole_region()
133 for (int n = 1; n != 256; ++n) {
135 prologue_return ret = prologue(n);
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");
140 munmap(ret.dstn, ret.size);
141 munmap(ret.src, ret.size);
145 void BOOST_tests::compare_region_parts()
147 for (int n = 1; n != 256; ++n) {
149 prologue_return ret = prologue(n);
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()");
158 munmap(ret.dstn, ret.size);
159 munmap(ret.src, ret.size);
163 void BOOST_tests::read_pointer()
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(®ion2, ret.src) == mc_model_checker,
171 "Mismtach in MC_region_read_pointer()");
173 munmap(ret.dstn, ret.size);
174 munmap(ret.src, ret.size);
177 /*************** End: class BOOST_tests *****************************/
179 namespace utf = boost::unit_test; // for test case dependence
181 BOOST_AUTO_TEST_SUITE(flat_snapshot)
182 BOOST_AUTO_TEST_CASE(Init)
184 BOOST_tests::Init(0);
187 BOOST_AUTO_TEST_CASE(read_whole_region, *utf::depends_on("flat_snapshot/Init"))
189 BOOST_tests::read_whole_region();
192 BOOST_AUTO_TEST_CASE(read_region_parts, *utf::depends_on("flat_snapshot/read_whole_region"))
194 BOOST_tests::read_region_parts();
197 BOOST_AUTO_TEST_CASE(compare_whole_region, *utf::depends_on("flat_snapshot/read_region_parts"))
199 BOOST_tests::compare_whole_region();
202 BOOST_AUTO_TEST_CASE(compare_region_parts, *utf::depends_on("flat_snapshot/compare_whole_region"))
204 BOOST_tests::compare_region_parts();
207 BOOST_AUTO_TEST_CASE(read_pointer, *utf::depends_on("flat_snapshot/compare_region_parts"))
209 BOOST_tests::read_pointer();
212 // not really a test, just for cleanup the resources
213 BOOST_AUTO_TEST_CASE(cleanup, *utf::depends_on("flat_snapshot/read_pointer"))
215 BOOST_tests::cleanup();
217 BOOST_AUTO_TEST_SUITE_END()
219 BOOST_AUTO_TEST_SUITE(page_snapshots)
220 BOOST_AUTO_TEST_CASE(Init)
222 BOOST_tests::Init(1);
225 BOOST_AUTO_TEST_CASE(read_whole_region, *utf::depends_on("page_snapshots/Init"))
227 BOOST_tests::read_whole_region();
230 BOOST_AUTO_TEST_CASE(read_region_parts, *utf::depends_on("page_snapshots/read_whole_region"))
232 BOOST_tests::read_region_parts();
235 BOOST_AUTO_TEST_CASE(compare_whole_region, *utf::depends_on("page_snapshots/read_region_parts"))
237 BOOST_tests::compare_whole_region();
240 BOOST_AUTO_TEST_CASE(compare_region_parts, *utf::depends_on("page_snapshots/compare_whole_region"))
242 BOOST_tests::compare_region_parts();
245 BOOST_AUTO_TEST_CASE(read_pointer, *utf::depends_on("page_snapshots/compare_region_parts"))
247 BOOST_tests::read_pointer();
250 // not really a test, just for cleanup the resources
251 BOOST_AUTO_TEST_CASE(cleanup, *utf::depends_on("page_snapshots/read_pointer"))
253 BOOST_tests::cleanup();
255 BOOST_AUTO_TEST_SUITE_END()