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/mc_snapshot.hpp"
21 static inline void init_memory(void* mem, size_t size)
23 char* dest = (char*) mem;
24 for (size_t i = 0; i < size; ++i) {
25 dest[i] = rand() & 255;
29 static int test_snapshot(bool sparse_checkpoint);
31 BOOST_AUTO_TEST_SUITE(Snapshots)
32 BOOST_AUTO_TEST_CASE(flat_snapshots) {
33 BOOST_CHECK_EQUAL(test_snapshot(0), 1);
35 BOOST_AUTO_TEST_CASE(page_snapshots) {
36 BOOST_CHECK_EQUAL(test_snapshot(1), 1);
38 BOOST_AUTO_TEST_SUITE_END()
40 static int test_snapshot(bool sparse_checkpoint) {
42 _sg_mc_sparse_checkpoint = sparse_checkpoint;
43 BOOST_CHECK_EQUAL(xbt_pagesize, getpagesize());
44 BOOST_CHECK_EQUAL(1 << xbt_pagebits, xbt_pagesize);
46 std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(getpid(), -1));
48 mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
50 for(int n=1; n!=256; ++n) {
52 size_t byte_size = n * xbt_pagesize;
53 void* source = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
54 BOOST_CHECK_MESSAGE(source!=MAP_FAILED, "Could not allocate source memory");
56 init_memory(source, byte_size);
57 simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region(
58 simgrid::mc::RegionType::Unknown, source, source, byte_size);
59 for(int i=0; i<n; i+=2) {
60 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
62 simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
63 simgrid::mc::RegionType::Unknown, source, source, byte_size);
65 void* destination = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
66 BOOST_CHECK_MESSAGE(source!=MAP_FAILED, "Could not allocate destination memory");
68 const void* read = MC_region_read(®ion, destination, source, byte_size);
69 BOOST_CHECK_MESSAGE(not memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
71 for(int j=0; j!=100; ++j) {
72 size_t offset = rand() % byte_size;
73 size_t size = rand() % (byte_size - offset);
74 const void* read = MC_region_read(®ion, destination, (const char*) source+offset, size);
75 BOOST_CHECK_MESSAGE(not memcmp((char*)source + offset, read, size), "Mismatch in MC_region_read()");
79 BOOST_CHECK_MESSAGE(MC_snapshot_region_memcmp(source, ®ion0, source, ®ion, byte_size),
80 "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
82 for(int j=0; j!=100; ++j) {
83 size_t offset = rand() % byte_size;
84 size_t size = rand() % (byte_size - offset);
86 not MC_snapshot_region_memcmp((char*)source + offset, ®ion, (char*)source + offset, ®ion, size),
87 "Mismatch in MC_snapshot_region_memcmp()");
91 memcpy(source, &mc_model_checker, sizeof(void*));
92 simgrid::mc::RegionSnapshot region2 = simgrid::mc::sparse_region(
93 simgrid::mc::RegionType::Unknown, source, source, byte_size);
94 BOOST_CHECK_MESSAGE(MC_region_read_pointer(®ion2, source) == mc_model_checker,
95 "Mismtach in MC_region_read_pointer()");
98 munmap(destination, byte_size);
99 munmap(source, byte_size);
102 delete mc_model_checker;
103 mc_model_checker = nullptr;
105 return 1; // dummy value, for BOOST unit test