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 #include <boost/test/unit_test.hpp>
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/sosp/mc_snapshot.hpp"
20 /**************** Class BOOST_tests *************************/
21 using simgrid::mc::RegionSnapshot;
24 static void init_memory(void* mem, size_t size);
25 static void Init(bool sparse_ckpt);
30 RegionSnapshot region0;
31 RegionSnapshot region;
33 static prologue_return prologue(int n); // common to the below 5 fxs
34 static void read_whole_region();
35 static void read_region_parts();
36 static void compare_whole_region();
37 static void compare_region_parts();
38 static void read_pointer();
40 static void cleanup() {
41 delete mc_model_checker;
42 mc_model_checker = nullptr;
46 static bool sparse_checkpoint;
47 static std::unique_ptr<simgrid::mc::RemoteClient> process;
50 // static member variables init.
51 bool BOOST_tests::sparse_checkpoint = 0;
52 std::unique_ptr<simgrid::mc::RemoteClient> BOOST_tests::process = nullptr;
55 BOOST_tests::init_memory(void* mem, size_t size) {
56 char* dest = (char*) mem;
57 for (size_t i = 0; i < size; ++i) {
58 dest[i] = rand() & 255;
63 BOOST_tests::Init(bool sparse_ckpt) {
64 _sg_mc_sparse_checkpoint = sparse_ckpt;
65 BOOST_CHECK_EQUAL(xbt_pagesize, getpagesize());
66 BOOST_CHECK_EQUAL(1 << xbt_pagebits, xbt_pagesize);
68 process = std::unique_ptr<simgrid::mc::RemoteClient>(new simgrid::mc::RemoteClient(getpid(), -1));
70 mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
74 BOOST_tests::prologue_return BOOST_tests::prologue(int n) {
75 // Store region page(s):
76 size_t byte_size = n * xbt_pagesize;
77 void* source = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
78 BOOST_CHECK_MESSAGE(source!=MAP_FAILED, "Could not allocate source memory");
80 // Init memory and take snapshots:
81 init_memory(source, byte_size);
82 simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region(
83 simgrid::mc::RegionType::Unknown, source, source, byte_size);
84 for(int i=0; i<n; i+=2) {
85 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
87 simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
88 simgrid::mc::RegionType::Unknown, source, source, byte_size);
90 void* destination = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
91 BOOST_CHECK_MESSAGE(source!=MAP_FAILED, "Could not allocate destination memory");
93 return {.size = byte_size, .src = source, .dstn = destination,
94 .region0 = std::move(region0), .region = std::move(region)};
98 BOOST_tests::read_whole_region() {
99 for(int n=1; n!=256; ++n) {
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()");
105 munmap(ret.dstn, ret.size);
106 munmap(ret.src, ret.size);
111 BOOST_tests::read_region_parts() {
112 for(int n=1; n!=256; ++n) {
114 prologue_return ret = prologue(n);
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()");
122 munmap(ret.dstn, ret.size);
123 munmap(ret.src, ret.size);
128 BOOST_tests::compare_whole_region() {
129 for(int n=1; n!=256; ++n) {
131 prologue_return ret = prologue(n);
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");
136 munmap(ret.dstn, ret.size);
137 munmap(ret.src, ret.size);
142 BOOST_tests::compare_region_parts() {
143 for(int n=1; n!=256; ++n) {
145 prologue_return ret = prologue(n);
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);
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()");
155 munmap(ret.dstn, ret.size);
156 munmap(ret.src, ret.size);
161 BOOST_tests::read_pointer() {
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(®ion2, ret.src) == mc_model_checker,
169 "Mismtach in MC_region_read_pointer()");
171 munmap(ret.dstn, ret.size);
172 munmap(ret.src, ret.size);
175 /*************** End: class BOOST_tests *****************************/
177 namespace utf = boost::unit_test; // for test case dependence
179 BOOST_AUTO_TEST_SUITE(flat_snapshot)
180 BOOST_AUTO_TEST_CASE(Init) {
181 BOOST_tests::Init(0);
184 BOOST_AUTO_TEST_CASE(read_whole_region, * utf::depends_on("flat_snapshot/Init")) {
185 BOOST_tests::read_whole_region();
188 BOOST_AUTO_TEST_CASE(read_region_parts, * utf::depends_on("flat_snapshot/read_whole_region")) {
189 BOOST_tests::read_region_parts();
192 BOOST_AUTO_TEST_CASE(compare_whole_region, * utf::depends_on("flat_snapshot/read_region_parts")) {
193 BOOST_tests::compare_whole_region();
196 BOOST_AUTO_TEST_CASE(compare_region_parts, * utf::depends_on("flat_snapshot/compare_whole_region")) {
197 BOOST_tests::compare_region_parts();
200 BOOST_AUTO_TEST_CASE(read_pointer, * utf::depends_on("flat_snapshot/compare_region_parts")) {
201 BOOST_tests::read_pointer();
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();
208 BOOST_AUTO_TEST_SUITE_END()
210 BOOST_AUTO_TEST_SUITE(page_snapshots)
211 BOOST_AUTO_TEST_CASE(Init) {
212 BOOST_tests::Init(1);
215 BOOST_AUTO_TEST_CASE(read_whole_region, * utf::depends_on("page_snapshots/Init")) {
216 BOOST_tests::read_whole_region();
219 BOOST_AUTO_TEST_CASE(read_region_parts, * utf::depends_on("page_snapshots/read_whole_region")) {
220 BOOST_tests::read_region_parts();
223 BOOST_AUTO_TEST_CASE(compare_whole_region, * utf::depends_on("page_snapshots/read_region_parts")) {
224 BOOST_tests::compare_whole_region();
227 BOOST_AUTO_TEST_CASE(compare_region_parts, * utf::depends_on("page_snapshots/compare_whole_region")) {
228 BOOST_tests::compare_region_parts();
231 BOOST_AUTO_TEST_CASE(read_pointer, * utf::depends_on("page_snapshots/compare_region_parts")) {
232 BOOST_tests::read_pointer();
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();
239 BOOST_AUTO_TEST_SUITE_END()