Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
minor changes.
[simgrid.git] / src / mc / snapshot / unitTest / mc_snapshot_unit.cpp
1 /* Copyright (c) 2014-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #define BOOST_TEST_MODULE snapshots
7 #define BOOST_TEST_DYN_LINK
8 #include <boost/test/unit_test.hpp>
9
10 #include <cstdlib>
11 #include <cstring>
12
13 #include <sys/mman.h>
14
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"
19
20 static inline void init_memory(void* mem, size_t size)
21 {
22   char* dest = (char*) mem;
23   for (size_t i = 0; i < size; ++i) {
24     dest[i] = rand() & 255;
25   }
26 }
27
28 static int test_snapshot(bool sparse_checkpoint);
29
30 BOOST_AUTO_TEST_SUITE(Snapshots)
31 BOOST_AUTO_TEST_CASE(flat_snapshots) {
32   test_snapshot(0);
33 }
34 BOOST_AUTO_TEST_CASE(page_snapshots) {
35   test_snapshot(1);
36 }
37 BOOST_AUTO_TEST_SUITE_END()
38
39 static int test_snapshot(bool sparse_checkpoint) {
40
41   _sg_mc_sparse_checkpoint = sparse_checkpoint;
42   BOOST_CHECK_EQUAL(xbt_pagesize, getpagesize());
43   BOOST_CHECK_EQUAL(1 << xbt_pagebits, xbt_pagesize);
44
45   std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(getpid(), -1));
46   process->init();
47   mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
48
49   for(int n=1; n!=256; ++n) {
50
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");
54
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);
60     }
61     simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
62       simgrid::mc::RegionType::Unknown, source, source, byte_size);
63
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");
66
67     const void* read = MC_region_read(&region, destination, source, byte_size);
68     BOOST_CHECK_MESSAGE(not memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
69
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(&region, destination, (const char*) source+offset, size);
74       BOOST_CHECK_MESSAGE(not memcmp((char*)source + offset, read, size), "Mismatch in MC_region_read()");
75     }
76
77
78     BOOST_CHECK_MESSAGE(MC_snapshot_region_memcmp(source, &region0, source, &region, byte_size),
79       "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
80
81     for(int j=0; j!=100; ++j) {
82       size_t offset = rand() % byte_size;
83       size_t size = rand() % (byte_size - offset);
84       BOOST_CHECK_MESSAGE(
85           not MC_snapshot_region_memcmp((char*)source + offset, &region, (char*)source + offset, &region, size),
86           "Mismatch in MC_snapshot_region_memcmp()");
87     }
88
89     if (n==1) {
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(&region2, source) == mc_model_checker,
94         "Mismtach in MC_region_read_pointer()");
95     }
96
97     munmap(destination, byte_size);
98     munmap(source, byte_size);
99   }
100
101   delete mc_model_checker;
102   mc_model_checker = nullptr;
103
104   return 1; // dummy value, for BOOST unit test
105 }