Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
50cd1a209fe24bdd144e932086281067eae7fe25
[simgrid.git] / src / mc / sosp / mc_snapshot_test.cpp
1 /* Copyright (c) 2014-2019. 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 #include "src/include/catch.hpp"
7
8 #include <cstdlib>
9 #include <cstring>
10 #include <random>
11
12 #include <sys/mman.h>
13
14 #include "src/mc/mc_config.hpp"
15 #include "src/mc/mc_mmu.hpp"
16 #include "src/mc/mc_private.hpp"
17 #include "src/mc/sosp/mc_snapshot.hpp"
18
19 /**************** Class BOOST_tests *************************/
20 using simgrid::mc::RegionSnapshot;
21 class snap_test_helper {
22 public:
23   static void init_memory(void* mem, size_t size);
24   static void Init(bool sparse_ckpt);
25   typedef struct {
26     size_t size;
27     void* src;
28     void* dstn;
29     RegionSnapshot region0;
30     RegionSnapshot region;
31   } prologue_return;
32   static prologue_return prologue(int n); // common to the below 5 fxs
33   static void read_whole_region();
34   static void read_region_parts();
35   static void compare_whole_region();
36   static void compare_region_parts();
37   static void read_pointer();
38
39   static void cleanup()
40   {
41     delete mc_model_checker;
42     mc_model_checker = nullptr;
43   }
44
45   static std::default_random_engine rnd_engine;
46   static bool sparse_checkpoint;
47   static std::unique_ptr<simgrid::mc::RemoteClient> process;
48 };
49
50 // static member variables init.
51 std::default_random_engine snap_test_helper::rnd_engine;
52 bool snap_test_helper::sparse_checkpoint                             = 0;
53 std::unique_ptr<simgrid::mc::RemoteClient> snap_test_helper::process = nullptr;
54
55 void snap_test_helper::init_memory(void* mem, size_t size)
56 {
57   char* dest = (char*)mem;
58   for (size_t i = 0; i < size; ++i) {
59     dest[i] = rnd_engine() & 255;
60   }
61 }
62
63 void snap_test_helper::Init(bool sparse_ckpt)
64 {
65   _sg_mc_sparse_checkpoint = sparse_ckpt;
66   REQUIRE(xbt_pagesize == getpagesize());
67   REQUIRE(1 << xbt_pagebits == xbt_pagesize);
68
69   process = std::unique_ptr<simgrid::mc::RemoteClient>(new simgrid::mc::RemoteClient(getpid(), -1));
70   process->init();
71   mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
72 }
73
74 snap_test_helper::prologue_return snap_test_helper::prologue(int n)
75 {
76   // Store region page(s):
77   size_t byte_size = n * xbt_pagesize;
78   void* source     = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
79   INFO("Could not allocate source memory")
80   REQUIRE(source != MAP_FAILED);
81
82   // Init memory and take snapshots:
83   init_memory(source, byte_size);
84   simgrid::mc::RegionSnapshot region0 =
85       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
86   for (int i = 0; i < n; i += 2) {
87     init_memory((char*)source + i * xbt_pagesize, xbt_pagesize);
88   }
89   simgrid::mc::RegionSnapshot region =
90       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, source, source, byte_size);
91
92   void* destination = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
93   INFO("Could not allocate destination memory");
94   REQUIRE(source != MAP_FAILED);
95
96   return {.size    = byte_size,
97           .src     = source,
98           .dstn    = destination,
99           .region0 = std::move(region0),
100           .region  = std::move(region)};
101 }
102
103 void snap_test_helper::read_whole_region()
104 {
105   for (int n = 1; n != 32; ++n) {
106
107     prologue_return ret = prologue(n);
108     const void* read    = MC_region_read(&(ret.region), ret.dstn, ret.src, ret.size);
109     INFO("Mismatch in MC_region_read()");
110     REQUIRE(not memcmp(ret.src, read, ret.size));
111
112     munmap(ret.dstn, ret.size);
113     munmap(ret.src, ret.size);
114   }
115 }
116
117 void snap_test_helper::read_region_parts()
118 {
119   for (int n = 1; n != 32; ++n) {
120
121     prologue_return ret = prologue(n);
122
123     for (int j = 0; j != 100; ++j) {
124       size_t offset    = rnd_engine() % ret.size;
125       size_t size      = rnd_engine() % (ret.size - offset);
126       const void* read = MC_region_read(&(ret.region), ret.dstn, (const char*)ret.src + offset, size);
127       INFO("Mismatch in MC_region_read()");
128       REQUIRE(not memcmp((char*)ret.src + offset, read, size));
129     }
130     munmap(ret.dstn, ret.size);
131     munmap(ret.src, ret.size);
132   }
133 }
134
135 void snap_test_helper::compare_whole_region()
136 {
137   for (int n = 1; n != 32; ++n) {
138
139     prologue_return ret = prologue(n);
140
141     INFO("Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
142     REQUIRE(MC_snapshot_region_memcmp(ret.src, &(ret.region0), ret.src, &(ret.region), ret.size));
143
144     munmap(ret.dstn, ret.size);
145     munmap(ret.src, ret.size);
146   }
147 }
148
149 void snap_test_helper::compare_region_parts()
150 {
151   for (int n = 1; n != 32; ++n) {
152
153     prologue_return ret = prologue(n);
154
155     for (int j = 0; j != 100; ++j) {
156       size_t offset = rnd_engine() % ret.size;
157       size_t size   = rnd_engine() % (ret.size - offset);
158
159       INFO("Mismatch in MC_snapshot_region_memcmp()");
160       REQUIRE(not MC_snapshot_region_memcmp((char*)ret.src + offset, &(ret.region), (char*)ret.src + offset,
161                                             &(ret.region), size));
162     }
163     munmap(ret.dstn, ret.size);
164     munmap(ret.src, ret.size);
165   }
166 }
167
168 void snap_test_helper::read_pointer()
169 {
170
171   prologue_return ret = prologue(1);
172   memcpy(ret.src, &mc_model_checker, sizeof(void*));
173   simgrid::mc::RegionSnapshot region2 =
174       simgrid::mc::sparse_region(simgrid::mc::RegionType::Unknown, ret.src, ret.src, ret.size);
175   INFO("Mismtach in MC_region_read_pointer()");
176   REQUIRE(MC_region_read_pointer(&region2, ret.src) == mc_model_checker);
177
178   munmap(ret.dstn, ret.size);
179   munmap(ret.src, ret.size);
180 }
181
182 /*************** End: class snap_test_helper *****************************/
183
184 TEST_CASE("MC::Snapshot: A copy/snapshot of a given memory region", "MC::Snapshot")
185 {
186   auto sparse = GENERATE(false, true);
187
188   if (sparse) {
189     INFO("Sparse snapshot (using pages)");
190   } else {
191     INFO("Flat snapshot (no pages)");
192   }
193
194   snap_test_helper::Init(sparse);
195
196   INFO("Read whole region");
197   snap_test_helper::read_whole_region();
198
199   INFO("Read region parts");
200   snap_test_helper::read_region_parts();
201
202   INFO("Compare whole region");
203   snap_test_helper::compare_whole_region();
204
205   INFO("Compare region parts");
206   snap_test_helper::compare_region_parts();
207
208   INFO("Read pointer");
209   snap_test_helper::read_pointer();
210
211   snap_test_helper::cleanup();
212 }