45452b5800a8773a2fb412bdd237f2f9e6e8733b
[folly.git] / folly / experimental / hazptr / test / HazptrTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #define HAZPTR_DEBUG true
17 #define HAZPTR_STATS true
18 #define HAZPTR_SCAN_THRESHOLD 10
19
20 #include <folly/experimental/hazptr/test/HazptrUse1.h>
21 #include <folly/experimental/hazptr/test/HazptrUse2.h>
22 #include <folly/experimental/hazptr/example/LockFreeLIFO.h>
23 #include <folly/experimental/hazptr/example/SWMRList.h>
24 #include <folly/experimental/hazptr/example/WideCAS.h>
25 #include <folly/experimental/hazptr/debug.h>
26 #include <folly/experimental/hazptr/hazptr.h>
27
28 #include <folly/portability/GFlags.h>
29 #include <folly/portability/GTest.h>
30
31 #include <thread>
32
33 DEFINE_int32(num_threads, 5, "Number of threads");
34 DEFINE_int64(num_reps, 1, "Number of test reps");
35 DEFINE_int64(num_ops, 10, "Number of ops or pairs of ops per rep");
36
37 using namespace folly::hazptr;
38
39 class HazptrTest : public testing::Test {
40  public:
41   HazptrTest() : Test() {
42     DEBUG_PRINT("========== start of test scope");
43   }
44   ~HazptrTest() override {
45     DEBUG_PRINT("========== end of test scope");
46   }
47 };
48
49 TEST_F(HazptrTest, Test1) {
50   DEBUG_PRINT("");
51   Node1* node0 = (Node1*)malloc(sizeof(Node1));
52   DEBUG_PRINT("=== new    node0 " << node0 << " " << sizeof(*node0));
53   Node1* node1 = (Node1*)malloc(sizeof(Node1));
54   DEBUG_PRINT("=== malloc node1 " << node1 << " " << sizeof(*node1));
55   Node1* node2 = (Node1*)malloc(sizeof(Node1));
56   DEBUG_PRINT("=== malloc node2 " << node2 << " " << sizeof(*node2));
57   Node1* node3 = (Node1*)malloc(sizeof(Node1));
58   DEBUG_PRINT("=== malloc node3 " << node3 << " " << sizeof(*node3));
59
60   DEBUG_PRINT("");
61
62   std::atomic<Node1*> shared0 = {node0};
63   std::atomic<Node1*> shared1 = {node1};
64   std::atomic<Node1*> shared2 = {node2};
65   std::atomic<Node1*> shared3 = {node3};
66
67   MyMemoryResource myMr;
68   DEBUG_PRINT("=== myMr " << &myMr);
69   hazptr_domain myDomain0;
70   DEBUG_PRINT("=== myDomain0 " << &myDomain0);
71   hazptr_domain myDomain1(&myMr);
72   DEBUG_PRINT("=== myDomain1 " << &myDomain1);
73
74   DEBUG_PRINT("");
75
76   DEBUG_PRINT("=== hptr0");
77   hazptr_owner<Node1> hptr0;
78   DEBUG_PRINT("=== hptr1");
79   hazptr_owner<Node1> hptr1(myDomain0);
80   DEBUG_PRINT("=== hptr2");
81   hazptr_owner<Node1> hptr2(myDomain1);
82   DEBUG_PRINT("=== hptr3");
83   hazptr_owner<Node1> hptr3;
84
85   DEBUG_PRINT("");
86
87   Node1* n0 = shared0.load();
88   Node1* n1 = shared1.load();
89   Node1* n2 = shared2.load();
90   Node1* n3 = shared3.load();
91
92   if (hptr0.try_protect(n0, shared0)) {}
93   if (hptr1.try_protect(n1, shared1)) {}
94   hptr1.clear();
95   hptr1.set(n2);
96   if (hptr2.try_protect(n3, shared3)) {}
97   swap(hptr1, hptr2);
98   hptr3.clear();
99
100   DEBUG_PRINT("");
101
102   DEBUG_PRINT("=== retire n0 " << n0);
103   n0->retire();
104   DEBUG_PRINT("=== retire n1 " << n1);
105   n1->retire(default_hazptr_domain());
106   DEBUG_PRINT("=== retire n2 " << n2);
107   n2->retire(myDomain0);
108   DEBUG_PRINT("=== retire n3 " << n3);
109   n3->retire(myDomain1);
110 }
111
112 TEST_F(HazptrTest, Test2) {
113   Node2* node0 = new Node2;
114   DEBUG_PRINT("=== new    node0 " << node0 << " " << sizeof(*node0));
115   Node2* node1 = (Node2*)malloc(sizeof(Node2));
116   DEBUG_PRINT("=== malloc node1 " << node1 << " " << sizeof(*node1));
117   Node2* node2 = (Node2*)malloc(sizeof(Node2));
118   DEBUG_PRINT("=== malloc node2 " << node2 << " " << sizeof(*node2));
119   Node2* node3 = (Node2*)malloc(sizeof(Node2));
120   DEBUG_PRINT("=== malloc node3 " << node3 << " " << sizeof(*node3));
121
122   DEBUG_PRINT("");
123
124   std::atomic<Node2*> shared0 = {node0};
125   std::atomic<Node2*> shared1 = {node1};
126   std::atomic<Node2*> shared2 = {node2};
127   std::atomic<Node2*> shared3 = {node3};
128
129   MineMemoryResource mineMr;
130   DEBUG_PRINT("=== mineMr " << &mineMr);
131   hazptr_domain mineDomain0;
132   DEBUG_PRINT("=== mineDomain0 " << &mineDomain0);
133   hazptr_domain mineDomain1(&mineMr);
134   DEBUG_PRINT("=== mineDomain1 " << &mineDomain1);
135
136   DEBUG_PRINT("");
137
138   DEBUG_PRINT("=== hptr0");
139   hazptr_owner<Node2> hptr0;
140   DEBUG_PRINT("=== hptr1");
141   hazptr_owner<Node2> hptr1(mineDomain0);
142   DEBUG_PRINT("=== hptr2");
143   hazptr_owner<Node2> hptr2(mineDomain1);
144   DEBUG_PRINT("=== hptr3");
145   hazptr_owner<Node2> hptr3;
146
147   DEBUG_PRINT("");
148
149   Node2* n0 = shared0.load();
150   Node2* n1 = shared1.load();
151   Node2* n2 = shared2.load();
152   Node2* n3 = shared3.load();
153
154   if (hptr0.try_protect(n0, shared0)) {}
155   if (hptr1.try_protect(n1, shared1)) {}
156   hptr1.clear();
157   hptr1.set(n2);
158   if (hptr2.try_protect(n3, shared3)) {}
159   swap(hptr1, hptr2);
160   hptr3.clear();
161
162   DEBUG_PRINT("");
163
164   DEBUG_PRINT("=== retire n0 " << n0);
165   n0->retire(default_hazptr_domain(), &mineReclaimFnDelete);
166   DEBUG_PRINT("=== retire n1 " << n1);
167   n1->retire(default_hazptr_domain(), &mineReclaimFnFree);
168   DEBUG_PRINT("=== retire n2 " << n2);
169   n2->retire(mineDomain0, &mineReclaimFnFree);
170   DEBUG_PRINT("=== retire n3 " << n3);
171   n3->retire(mineDomain1, &mineReclaimFnFree);
172 }
173
174 TEST_F(HazptrTest, LIFO) {
175   using T = uint32_t;
176   CHECK_GT(FLAGS_num_threads, 0);
177   for (int i = 0; i < FLAGS_num_reps; ++i) {
178     DEBUG_PRINT("========== start of rep scope");
179     LockFreeLIFO<T> s;
180     std::vector<std::thread> threads(FLAGS_num_threads);
181     for (int tid = 0; tid < FLAGS_num_threads; ++tid) {
182       threads[tid] = std::thread([&s, tid]() {
183         for (int j = tid; j < FLAGS_num_ops; j += FLAGS_num_threads) {
184           s.push(j);
185           T res;
186           while (!s.pop(res)) {}
187         }
188       });
189     }
190     for (auto& t : threads) {
191       t.join();
192     }
193     DEBUG_PRINT("========== end of rep scope");
194   }
195 }
196
197 TEST_F(HazptrTest, SWMRLIST) {
198   using T = uint64_t;
199   hazptr_domain custom_domain;
200
201   CHECK_GT(FLAGS_num_threads, 0);
202   for (int i = 0; i < FLAGS_num_reps; ++i) {
203     DEBUG_PRINT("========== start of rep scope");
204     SWMRListSet<T> s(custom_domain);
205     std::vector<std::thread> threads(FLAGS_num_threads);
206     for (int tid = 0; tid < FLAGS_num_threads; ++tid) {
207       threads[tid] = std::thread([&s, tid]() {
208         for (int j = tid; j < FLAGS_num_ops; j += FLAGS_num_threads) {
209           s.contains(j);
210         }
211       });
212     }
213     for (int j = 0; j < 10; ++j) {
214       s.add(j);
215     }
216     for (int j = 0; j < 10; ++j) {
217       s.remove(j);
218     }
219     for (auto& t : threads) {
220       t.join();
221     }
222     DEBUG_PRINT("========== end of rep scope");
223   }
224 }
225
226 TEST_F(HazptrTest, WIDECAS) {
227   WideCAS s;
228   std::string u = "";
229   std::string v = "11112222";
230   auto ret = s.cas(u, v);
231   CHECK(ret);
232   u = "";
233   v = "11112222";
234   ret = s.cas(u, v);
235   CHECK(!ret);
236   u = "11112222";
237   v = "22223333";
238   ret = s.cas(u, v);
239   CHECK(ret);
240   u = "22223333";
241   v = "333344445555";
242   ret = s.cas(u, v);
243   CHECK(ret);
244 }
245
246 TEST_F(HazptrTest, VirtualTest) {
247   struct Thing : public hazptr_obj_base<Thing> {
248     virtual ~Thing() {
249       DEBUG_PRINT("this: " << this << " &a: " << &a << " a: " << a);
250     }
251     int a;
252   };
253   for (int i = 0; i < 100; i++) {
254     auto bar = new Thing;
255     bar->a = i;
256
257     hazptr_owner<Thing> hptr;
258     hptr.set(bar);
259     bar->retire();
260     EXPECT_EQ(bar->a, i);
261   }
262 }
263
264 TEST_F(HazptrTest, DestructionTest) {
265   hazptr_domain myDomain0;
266   struct Thing : public hazptr_obj_base<Thing> {
267     Thing* next;
268     Thing(Thing* n) : next(n) {}
269     ~Thing() {
270       DEBUG_PRINT("this: " << this << " next: " << next);
271       if (next) {
272         next->retire();
273       }
274     }
275   };
276   Thing* last{nullptr};
277   for (int i = 0; i < 2000; i++) {
278     last = new Thing(last);
279   }
280   last->retire();
281 }