Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6b8d25e2f776b5d99dfa226db858144413b77055
[simgrid.git] / src / mc / snapshot / unitTest / mc_snapshot_unit_BOOST.cpp
1 /*************************************************
2 TODO: comment
3 *************************************************/
4 #define BOOST_TEST_MODULE snapshots
5 #define BOOST_TEST_DYN_LINK
6 #include <boost/test/unit_test.hpp>
7
8
9 // /*******************************/
10 // /* GENERATED FILE, DO NOT EDIT */
11 // /*******************************/
12 // 
13 // #include <stdio.h>
14 // #include "xbt.h"
15 // /*******************************/
16 // /* GENERATED FILE, DO NOT EDIT */
17 // /*******************************/
18 // 
19 // #line 180 "mc/mc_snapshot.cpp" 
20
21 #include <cstdlib>
22 #include <cstring>
23
24 #include <sys/mman.h>
25
26 #include "src/mc/mc_config.hpp"
27 #include "src/mc/mc_mmu.hpp"
28 #include "src/mc/mc_private.hpp"
29 #include "src/mc/mc_snapshot.hpp"
30
31
32 static inline void init_memory(void* mem, size_t size)
33 {
34   char* dest = (char*) mem;
35   for (size_t i = 0; i < size; ++i) {
36     dest[i] = rand() & 255;
37   }
38 }
39
40 static int test_snapshot(bool sparse_checkpoint);
41
42 BOOST_AUTO_TEST_SUITE(Snapshots)
43 BOOST_AUTO_TEST_CASE(flat_snapshots) {
44   BOOST_CHECK_EQUAL(test_snapshot(0), 1);
45 }
46 BOOST_AUTO_TEST_CASE(page_snapshots) {
47   BOOST_CHECK_EQUAL(test_snapshot(1), 1);
48 }
49 BOOST_AUTO_TEST_SUITE_END()
50
51 static int test_snapshot(bool sparse_checkpoint) {
52
53   // xbt_test_add("Initialization");
54   _sg_mc_sparse_checkpoint = sparse_checkpoint;
55   BOOST_CHECK_EQUAL(xbt_pagesize, getpagesize());
56   BOOST_CHECK_EQUAL(1 << xbt_pagebits, xbt_pagesize);
57
58   std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(getpid(), -1));
59   process->init();
60   mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
61
62   for(int n=1; n!=256; ++n) {
63
64     // Store region page(s):
65     size_t byte_size = n * xbt_pagesize;
66     void* source = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
67     BOOST_CHECK_MESSAGE(source!=MAP_FAILED, "Could not allocate source memory");
68
69     // Init memory and take snapshots:
70     init_memory(source, byte_size);
71     simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region(
72       simgrid::mc::RegionType::Unknown, source, source, byte_size);
73     for(int i=0; i<n; i+=2) {
74       init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
75     }
76     simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
77       simgrid::mc::RegionType::Unknown, source, source, byte_size);
78
79     void* destination = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
80     BOOST_CHECK_MESSAGE(source!=MAP_FAILED, "Could not allocate destination memory");
81
82     // xbt_test_add("Reading whole region data for %i page(s)", n);
83     const void* read = MC_region_read(&region, destination, source, byte_size);
84     BOOST_CHECK_MESSAGE(not memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
85
86     // xbt_test_add("Reading parts of region data for %i page(s)", n);
87     for(int j=0; j!=100; ++j) {
88       size_t offset = rand() % byte_size;
89       size_t size = rand() % (byte_size - offset);
90       const void* read = MC_region_read(&region, destination, (const char*) source+offset, size);
91       BOOST_CHECK_MESSAGE(not memcmp((char*)source + offset, read, size), "Mismatch in MC_region_read()");
92     }
93
94     // xbt_test_add("Compare whole region data for %i page(s)", n);
95
96     BOOST_CHECK_MESSAGE(MC_snapshot_region_memcmp(source, &region0, source, &region, byte_size),
97       "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
98
99     // xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
100     for(int j=0; j!=100; ++j) {
101       size_t offset = rand() % byte_size;
102       size_t size = rand() % (byte_size - offset);
103       BOOST_CHECK_MESSAGE(
104           not MC_snapshot_region_memcmp((char*)source + offset, &region, (char*)source + offset, &region, size),
105           "Mismatch in MC_snapshot_region_memcmp()");
106     }
107
108     if (n==1) {
109       // xbt_test_add("Read pointer for %i page(s)", n);
110       memcpy(source, &mc_model_checker, sizeof(void*));
111       simgrid::mc::RegionSnapshot region2 = simgrid::mc::sparse_region(
112         simgrid::mc::RegionType::Unknown, source, source, byte_size);
113       BOOST_CHECK_MESSAGE(MC_region_read_pointer(&region2, source) == mc_model_checker,
114         "Mismtach in MC_region_read_pointer()");
115     }
116
117     munmap(destination, byte_size);
118     munmap(source, byte_size);
119   }
120
121   delete mc_model_checker;
122   mc_model_checker = nullptr;
123
124   return 1; // dummy value, for BOOST unit test
125 }
126
127 /*******************************/
128 /* GENERATED FILE, DO NOT EDIT */
129 /*******************************/
130