Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8de5bc98e68caef0c811da27615d624e9c83282f
[simgrid.git] / src / mc / sosp / PageStore_test.cpp
1 /* Copyright (c) 2015-2023. 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/3rd-party/catch.hpp"
7
8 #include <array>
9 #include <cstdint>
10 #include <cstring>
11 #include <iostream>
12
13 #include <sys/mman.h>
14 #include <unistd.h>
15
16 #include <memory>
17
18 #include "src/mc/sosp/PageStore.hpp"
19
20 /***********************************/
21 // a class to hold the variable used in the test cases
22 class pstore_test_helper {
23   const size_t pagesize = getpagesize();
24   simgrid::mc::PageStore store{50};
25   std::byte* data =
26       static_cast<std::byte*>(mmap(nullptr, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
27   std::array<size_t, 4> pageno = {0, 0, 0, 0};
28   int value                    = 0;
29
30   void new_content(std::byte* buf, size_t size);
31
32 public:
33   // member functions used by the test suite(s)
34   void init();
35   void store_page_once();
36   void store_same_page();
37   void store_new_page();
38   void unref_pages();
39   void reallocate_page();
40 };
41
42 void pstore_test_helper::init()
43 {
44   REQUIRE(data != nullptr);
45   REQUIRE(store.size() == 0);
46 }
47
48 void pstore_test_helper::store_page_once()
49 {
50   new_content(data, pagesize);
51   pageno[0] = store.store_page(data);
52   REQUIRE(store.get_ref(pageno[0]) == 1);
53   const auto* copy = store.get_page(pageno[0]);
54   REQUIRE(::memcmp(data, copy, pagesize) == 0); // The page data should be the same
55   REQUIRE(store.size() == 1);
56 }
57
58 void pstore_test_helper::store_same_page()
59 {
60   pageno[1] = store.store_page(data);
61   REQUIRE(pageno[0] == pageno[1]); // Page should be the same
62   REQUIRE(store.get_ref(pageno[0]) == 2);
63   REQUIRE(store.size() == 1);
64 }
65
66 void pstore_test_helper::store_new_page()
67 {
68   new_content(data, pagesize);
69   pageno[2] = store.store_page(data);
70   REQUIRE(pageno[0] != pageno[2]); // The new page should be different
71   REQUIRE(store.size() == 2);
72 }
73
74 void pstore_test_helper::unref_pages()
75 {
76   store.unref_page(pageno[0]);
77   REQUIRE(store.get_ref(pageno[0]) == 1);
78   REQUIRE(store.size() == 2);
79
80   store.unref_page(pageno[1]);
81   REQUIRE(store.size() == 1);
82 }
83
84 void pstore_test_helper::reallocate_page()
85 {
86   new_content(data, pagesize);
87   pageno[3] = store.store_page(data);
88   REQUIRE(pageno[0] == pageno[3]); // The old page should be reused
89   REQUIRE(store.get_ref(pageno[3]) == 1);
90   REQUIRE(store.size() == 2);
91 }
92
93 void pstore_test_helper::new_content(std::byte* buf, size_t size)
94 {
95   value++;
96   std::fill_n(buf, size, static_cast<std::byte>(value));
97 }
98
99 TEST_CASE("MC page store, used during checkpoint", "MC::PageStore")
100 {
101   pstore_test_helper pstore_test;
102   pstore_test.init();
103
104   INFO("Store page once");
105   pstore_test.store_page_once();
106
107   INFO("Store the same page");
108   pstore_test.store_same_page();
109
110   INFO("Store a new page");
111   pstore_test.store_new_page();
112
113   INFO("Unref pages");
114   pstore_test.unref_pages();
115
116   INFO("Reallocate pages");
117   pstore_test.reallocate_page();
118 }