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"
20 static inline void init_memory(void* mem, size_t size)
22 char* dest = (char*) mem;
23 for (size_t i = 0; i < size; ++i) {
24 dest[i] = rand() & 255;
28 static int test_snapshot(bool sparse_checkpoint);
30 BOOST_AUTO_TEST_SUITE(Snapshots)
31 BOOST_AUTO_TEST_CASE(flat_snapshots) {
34 BOOST_AUTO_TEST_CASE(page_snapshots) {
37 BOOST_AUTO_TEST_SUITE_END()
39 static int test_snapshot(bool sparse_checkpoint) {
41 _sg_mc_sparse_checkpoint = sparse_checkpoint;
42 BOOST_CHECK_EQUAL(xbt_pagesize, getpagesize());
43 BOOST_CHECK_EQUAL(1 << xbt_pagebits, xbt_pagesize);
45 std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(getpid(), -1));
47 mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
49 for(int n=1; n!=256; ++n) {
51 size_t byte_size = n * xbt_pagesize;
52 void* source = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
53 BOOST_CHECK_MESSAGE(source!=MAP_FAILED, "Could not allocate source memory");
55 init_memory(source, byte_size);
56 simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region(
57 simgrid::mc::RegionType::Unknown, source, source, byte_size);
58 for(int i=0; i<n; i+=2) {
59 init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
61 simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
62 simgrid::mc::RegionType::Unknown, source, source, byte_size);
64 void* destination = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
65 BOOST_CHECK_MESSAGE(source!=MAP_FAILED, "Could not allocate destination memory");
67 const void* read = MC_region_read(®ion, destination, source, byte_size);
68 BOOST_CHECK_MESSAGE(not memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
70 for(int j=0; j!=100; ++j) {
71 size_t offset = rand() % byte_size;
72 size_t size = rand() % (byte_size - offset);
73 const void* read = MC_region_read(®ion, destination, (const char*) source+offset, size);
74 BOOST_CHECK_MESSAGE(not memcmp((char*)source + offset, read, size), "Mismatch in MC_region_read()");
78 BOOST_CHECK_MESSAGE(MC_snapshot_region_memcmp(source, ®ion0, source, ®ion, byte_size),
79 "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
81 for(int j=0; j!=100; ++j) {
82 size_t offset = rand() % byte_size;
83 size_t size = rand() % (byte_size - offset);
85 not MC_snapshot_region_memcmp((char*)source + offset, ®ion, (char*)source + offset, ®ion, size),
86 "Mismatch in MC_snapshot_region_memcmp()");
90 memcpy(source, &mc_model_checker, sizeof(void*));
91 simgrid::mc::RegionSnapshot region2 = simgrid::mc::sparse_region(
92 simgrid::mc::RegionType::Unknown, source, source, byte_size);
93 BOOST_CHECK_MESSAGE(MC_region_read_pointer(®ion2, source) == mc_model_checker,
94 "Mismtach in MC_region_read_pointer()");
97 munmap(destination, byte_size);
98 munmap(source, byte_size);
101 delete mc_model_checker;
102 mc_model_checker = nullptr;
104 return 1; // dummy value, for BOOST unit test