-
Notifications
You must be signed in to change notification settings - Fork 44
/
doubly_linked_list_tests.cc
501 lines (454 loc) · 15.3 KB
/
doubly_linked_list_tests.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include <vector>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include "common/allocator_internal.h"
#include "doubly_linked_list.h"
#include "include/pmwcas.h"
#include "include/environment.h"
#include "include/allocator.h"
#include "include/status.h"
#include "util/random_number_generator.h"
#include "util/performance_test.h"
#ifdef WIN32
#include "environment/environment_windows.h"
#else
#include "environment/environment_linux.h"
#endif
uint32_t descriptor_pool_size = 100000;
namespace pmwcas {
namespace test {
struct SingleThreadTest {
IDList* dll;
std::vector<DListNode*> nodes;
const uint32_t kInitialNodes = 1000;
SingleThreadTest(IDList* list) : dll(list) {
Initialize();
}
void Initialize() {
if(dll->GetSyncMethod() == IDList::kSyncMwCAS) {
MwCASMetrics::ThreadInitialize();
}
ASSERT_TRUE(nodes.size() == 0);
ASSERT_TRUE(dll->GetHead()->prev == nullptr);
ASSERT_TRUE(dll->GetHead()->next == dll->GetTail());
ASSERT_TRUE(dll->GetTail()->prev == dll->GetHead());
ASSERT_TRUE(dll->GetTail()->next == nullptr);
for(int i = 0; i < kInitialNodes; ++i) {
auto* n = new DListNode;
nodes.push_back(n);
auto s = dll->InsertAfter(dll->GetTail(), n, false);
ASSERT_TRUE(s.ok());
}
ASSERT_TRUE(nodes.size() == kInitialNodes);
for(uint32_t i = 0; i < nodes.size(); i++) {
if(dll->GetSyncMethod() == IDList::kSyncCAS) {
ASSERT_FALSE(CASDList::MarkedNext(nodes[i]));
ASSERT_FALSE(CASDList::MarkedPrev(nodes[i]));
}
}
SanityCheck();
}
~SingleThreadTest() {
Teardown();
}
void Teardown() {
SanityCheck();
for(uint32_t i = 0; i < nodes.size(); i++) {
delete nodes[i];
}
Thread::ClearRegistry();
}
void SanityCheck() {
if(nodes.size() == 0) { // TestDelete empties node the vector
return;
}
bool found_head = false, found_tail = false;
for(auto& n : nodes) {
ASSERT_TRUE(n->prev && n->next);
auto* next = dll->GetNext(n);
if(next) {
ASSERT_TRUE(dll->GetPrev(next) == n);
if(n->prev == dll->GetHead()) {
ASSERT_FALSE(found_head);
found_head = true;
}
}
auto* prev = dll->GetPrev(n);
if(prev) {
ASSERT_TRUE(prev->next == n);
if(n->next == dll->GetTail()) {
ASSERT_FALSE(found_tail);
found_tail = true;
}
}
}
ASSERT_TRUE(found_head);
ASSERT_TRUE(found_tail);
uint32_t n = 0;
DListCursor iter((IDList*)dll);
while(iter.Next() != dll->GetTail()) {
n++;
}
EXPECT_EQ(n, nodes.size());
dll->SingleThreadSanityCheck();
}
void TestInsert() {
RandomNumberGenerator rng(1234567);
// Insert some new nodes at random locations
const int kInserts = 1000;
for(int i = 0; i < kInserts; i++) {
int idx = rng.Generate(nodes.size());
ASSERT_TRUE(idx >= 0 && idx < nodes.size());
DListNode* n = new DListNode;
if(rng.Generate(2) == 0) {
auto s = dll->InsertBefore(nodes[idx], n, false);
ASSERT_TRUE(s.ok());
} else {
auto s = dll->InsertAfter(nodes[idx], n, false);
ASSERT_TRUE(s.ok());
}
nodes.push_back(n);
}
ASSERT_TRUE(nodes.size() == kInitialNodes + kInserts);
}
void TestDelete() {
RandomNumberGenerator rng(1234567);
// Delete some nodes at random locations
while(nodes.size()) {
int idx = rng.Generate(nodes.size());
ASSERT_TRUE(idx >= 0 && idx < nodes.size());
dll->Delete(nodes[idx], false);
delete nodes[idx];
nodes.erase(nodes.begin() + idx);
}
ASSERT_TRUE(nodes.size() == 0);
}
void TestInsertDelete() {
RandomNumberGenerator rng(1234567);
const int kInsertPct = 50;
const int kDeletePct = 100 - kInsertPct;
const int kOps = 1000;
for(int i = 0; i < kOps; i++) {
if(nodes.size() && rng.Generate(100) >= kDeletePct) {
int idx = rng.Generate(nodes.size());
ASSERT_TRUE(nodes[idx]->prev);
ASSERT_TRUE(nodes[idx]->next);
ASSERT_TRUE(dll->GetNext(dll->GetPrev(nodes[idx])) == dll->GetPrev(
dll->GetNext(nodes[idx])));
ASSERT_TRUE(dll->GetNext(dll->GetPrev(nodes[idx])) == nodes[idx]);
auto s = dll->Delete(nodes[idx], false);
ASSERT_TRUE(s.ok());
DListNode* prev = nodes[idx]->prev;
DListNode* next = nodes[idx]->next;
if(dll->GetSyncMethod() == IDList::kSyncCAS) {
ASSERT_TRUE(nodes[idx]->prev);
ASSERT_TRUE(nodes[idx]->next);
prev = ((CASDList*)dll)->DereferenceNodePointer(&(nodes[idx]->prev));
next = ((CASDList*)dll)->DereferenceNodePointer(&(nodes[idx]->next));
ASSERT_TRUE(dll->GetNext(prev) == next);
ASSERT_TRUE(dll->GetPrev(next) == prev);
}
delete nodes[idx];
nodes.erase(nodes.begin() + idx);
} else {
int idx = rng.Generate(nodes.size());
ASSERT_TRUE(idx >= 0 && idx < nodes.size());
DListNode* n = new DListNode(nullptr, nullptr, 0);
if(rng.Generate(2) == 0) {
auto s = dll->InsertBefore(nodes[idx], n, false);
ASSERT_TRUE(s.ok());
ASSERT_TRUE(dll->GetPrev(nodes[idx]) == n);
ASSERT_TRUE(n->next == nodes[idx]);
} else {
auto s = dll->InsertAfter(nodes[idx], n, false);
ASSERT_TRUE(s.ok());
ASSERT_TRUE(nodes[idx]->next == n);
ASSERT_TRUE(n->prev == nodes[idx]);
}
nodes.push_back(n);
}
}
}
};
struct MultiThreadInsertDeleteTest : public PerformanceTest {
static const int kInitialNodes = 1000;
std::vector<DListNode*> nodes;
std::atomic<int32_t> node_count;
IDList* dll;
Barrier barrier;
MultiThreadInsertDeleteTest(IDList* list, uint32_t thread_count)
: PerformanceTest(), node_count(0), dll(list), barrier{ thread_count } {}
void Entry(size_t thread_index) {
if(dll->GetSyncMethod() == IDList::kSyncMwCAS) {
MwCASMetrics::ThreadInitialize();
}
if(thread_index == 0) {
RandomNumberGenerator rng(1234567);
for(int i = 0; i < kInitialNodes; ++i) {
nodes.push_back(new DListNode());
dll->InsertAfter(dll->GetHead(), nodes[i], false);
node_count++;
}
dll->SingleThreadSanityCheck();
}
const int kOps = 1000;
RandomNumberGenerator rng(1234567);
int next_insert_idx = 0;
int done_ops = 0;
std::vector<DListNode*> my_nodes;
for(int i = 0; i < kOps; ++i) { // Lazy, this is more than enough
my_nodes.push_back(new DListNode());
}
WaitForStart();
while(done_ops++ != kOps) {
int32_t nc = node_count.load(std::memory_order_seq_cst);
uint8_t ops = 3; // insert-before/after/delete
if(nc <= 0) {
ops--; // exclude delete
}
ASSERT_TRUE(ops == 2 || ops == 3);
DListNode* target = dll->GetHead();
int idx = rng.Generate(nc);
DListCursor iter((IDList*)dll);
while(--idx > 0 && target) {
target = iter.Next();
}
if(!target) {
continue;
}
int op = rng.Generate(ops);
if(op == 0) {
auto s = dll->InsertBefore(target, my_nodes[next_insert_idx++], false);
ASSERT_TRUE(s.ok());
node_count++;
} else if(op == 1) {
auto s = dll->InsertAfter(target, my_nodes[next_insert_idx++], false);
ASSERT_TRUE(s.ok());
node_count++;
} else {
auto s = dll->Delete(target, false);
ASSERT_TRUE(s.ok());
if(target != dll->GetHead() && target != dll->GetTail()) {
node_count--;
}
}
}
barrier.CountAndWait();
if(thread_index == 0) {
dll->SingleThreadSanityCheck();
}
}
};
struct MultiThreadDeleteTest : public PerformanceTest {
static const int kInitialNodes = 1000;
std::vector<DListNode*> nodes;
IDList* dll;
Barrier barrier;
MultiThreadDeleteTest(IDList* list, uint32_t thread_count)
: PerformanceTest(), dll(list), barrier { thread_count } {}
void Entry(size_t thread_index) {
if(dll->GetSyncMethod() == IDList::kSyncMwCAS) {
MwCASMetrics::ThreadInitialize();
}
if(thread_index == 0) {
RandomNumberGenerator rng(1234567);
for(int i = 0; i < kInitialNodes; ++i) {
nodes.push_back(new DListNode());
dll->InsertAfter(dll->GetHead(), nodes[i], false);
}
}
RandomNumberGenerator rng(1234567);
int deletes = 200;
WaitForStart();
while(deletes-- > 0 && dll->GetHead()->next != dll->GetTail()) {
int idx = rng.Generate(kInitialNodes);
DListNode* node = nodes[idx];
ASSERT_TRUE(node);
ASSERT_TRUE(node != dll->GetHead());
ASSERT_TRUE(node != dll->GetTail());
dll->Delete(node, false);
}
barrier.CountAndWait();
if(thread_index == 0) {
dll->SingleThreadSanityCheck();
for(int i = 0; i < kInitialNodes; ++i) {
delete nodes[i];
}
}
}
};
struct MultiThreadInsertTest : public PerformanceTest {
std::atomic<uint32_t> insert_count;
IDList* dll;
Barrier barrier;
MultiThreadInsertTest(IDList* list, uint32_t thread_count)
: PerformanceTest(), insert_count(0), dll(list), barrier { thread_count } {}
void Entry(size_t thread_index) {
if(dll->GetSyncMethod() == IDList::kSyncMwCAS) {
MwCASMetrics::ThreadInitialize();
}
RandomNumberGenerator rng(1234567);
const int kInserts = 1000;
std::vector<DListNode*> thread_nodes;
// Populate my vector of nodes to be inserted
for(int i = 0; i < kInserts; ++i) {
thread_nodes.push_back(new DListNode());
}
for(int i = 0; i < kInserts; ++i) {
ASSERT_TRUE(((int64_t)thread_nodes[i] & 0x3) == 0);
}
WaitForStart();
for(int i = 0; i < kInserts; ++i) {
// Pick a neighbor: the dummy head if dll is empty, otherwise
// try a random guy in the middle of the list.
DListNode* neighbor = dll->GetHead();
if(insert_count > 0) {
int idx = rng.Generate(insert_count);
DListCursor iter((IDList*)dll);
do {
neighbor = iter.Next();
} while(--idx > 0);
ASSERT_TRUE(neighbor);
}
if(rng.Generate(2) == 0) {
auto s = dll->InsertBefore(neighbor, thread_nodes[i], false);
ASSERT_TRUE(s.ok());
} else {
auto s = dll->InsertAfter(neighbor, thread_nodes[i], false);
ASSERT_TRUE(s.ok());
}
++insert_count;
}
barrier.CountAndWait();
if(thread_index == 0) {
dll->SingleThreadSanityCheck();
uint32_t n = 0;
auto* prev = dll->GetHead();
DListCursor iter((IDList*)dll);
// Check the insert count too
while(true) {
auto* node = iter.Next();
ASSERT_TRUE(dll->GetPrev(node) == prev);
ASSERT_TRUE(prev->next == node);
if(node == dll->GetTail()) {
break;
}
n++;
prev = node;
}
EXPECT_EQ(n, insert_count);
}
}
};
GTEST_TEST(DListTest, CASDSingleThreadInsert) {
CASDList dll;
SingleThreadTest t(&dll);
t.TestInsert();
}
GTEST_TEST(DListTest, CASDSingleThreadDelete) {
CASDList dll;
SingleThreadTest t(&dll);
t.TestDelete();
}
GTEST_TEST(DListTest, CASDSingleThreadInsertDelete) {
CASDList dll;
SingleThreadTest t(&dll);
t.TestInsertDelete();
}
GTEST_TEST(DListTest, CASDMultiThreadInsert) {
CASDList dll;
auto thread_count = Environment::Get()->GetCoreCount();
MultiThreadInsertTest test(&dll, thread_count);
test.Run(thread_count);
}
GTEST_TEST(DListTest, CASDMultiThreadDelete) {
CASDList dll;
auto thread_count = Environment::Get()->GetCoreCount();
MultiThreadDeleteTest test(&dll, thread_count);
test.Run(thread_count);
}
GTEST_TEST(DListTest, CASDMultiThreadInsertDelete) {
CASDList dll;
auto thread_count = Environment::Get()->GetCoreCount();
MultiThreadInsertDeleteTest test(&dll, thread_count);
test.Run(thread_count);
}
GTEST_TEST(DListTest, MwCASSingleThreadInsert) {
auto thread_count = Environment::Get()->GetCoreCount();
std::unique_ptr<pmwcas::DescriptorPool> pool(
new pmwcas::DescriptorPool(descriptor_pool_size, thread_count));
std::unique_ptr<MwCASDList> dll(new MwCASDList(pool.get()));
std::unique_ptr<SingleThreadTest> t (new SingleThreadTest(dll.get()));
t->TestInsert();
}
GTEST_TEST(DListTest, MwCASSingleThreadDelete) {
auto thread_count = Environment::Get()->GetCoreCount();
std::unique_ptr<pmwcas::DescriptorPool> pool(
new pmwcas::DescriptorPool(descriptor_pool_size, thread_count));
std::unique_ptr<MwCASDList> dll(new MwCASDList(pool.get()));
std::unique_ptr<SingleThreadTest> t (new SingleThreadTest(dll.get()));
t->TestDelete();
}
GTEST_TEST(DListTest, MwCASSingleThreadInsertDelete) {
auto thread_count = Environment::Get()->GetCoreCount();
std::unique_ptr<pmwcas::DescriptorPool> pool(
new pmwcas::DescriptorPool(descriptor_pool_size, thread_count));
std::unique_ptr<MwCASDList> dll(new MwCASDList(pool.get()));
std::unique_ptr<SingleThreadTest> t (new SingleThreadTest(dll.get()));
t->TestInsertDelete();
}
GTEST_TEST(DListTest, MwCASMultiThreadDelete) {
auto thread_count = Environment::Get()->GetCoreCount();
std::unique_ptr<pmwcas::DescriptorPool> pool(
new pmwcas::DescriptorPool(descriptor_pool_size, thread_count));
std::unique_ptr<MwCASDList> dll(new MwCASDList(pool.get()));
MultiThreadDeleteTest* t = new MultiThreadDeleteTest(dll.get(), thread_count);
t->Run(thread_count);
}
GTEST_TEST(DListTest, MwCASMultiThreadInsert) {
auto thread_count = Environment::Get()->GetCoreCount();
std::unique_ptr<pmwcas::DescriptorPool> pool(
new pmwcas::DescriptorPool(descriptor_pool_size, thread_count));
std::unique_ptr<MwCASDList> dll(new MwCASDList(pool.get()));
MultiThreadInsertTest* t = new MultiThreadInsertTest(dll.get(), thread_count);
t->Run(thread_count);
}
GTEST_TEST(DListTest, MwCASMultiThreadInsertDelete) {
auto thread_count = Environment::Get()->GetCoreCount();
std::unique_ptr<pmwcas::DescriptorPool> pool(
new pmwcas::DescriptorPool(descriptor_pool_size, thread_count));
std::unique_ptr<MwCASDList> dll(new MwCASDList(pool.get()));
MultiThreadInsertDeleteTest* t = new MultiThreadInsertDeleteTest(
dll.get(), thread_count);
t->Run(thread_count);
}
} // namespace test
} // namespace pmwcas
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
::testing::InitGoogleTest(&argc, argv);
FLAGS_minloglevel = 2;
#ifdef WIN32
pmwcas::InitLibrary(pmwcas::DefaultAllocator::Create,
pmwcas::DefaultAllocator::Destroy,
pmwcas::WindowsEnvironment::Create,
pmwcas::WindowsEnvironment::Destroy);
#else
#ifdef PMDK
pmwcas::InitLibrary(pmwcas::PMDKAllocator::Create("doubly_linked_test_pool",
"doubly_linked_layout",
static_cast<uint64_t >(1024) * 1024 * 1204 * 5),
pmwcas::PMDKAllocator::Destroy,
pmwcas::LinuxEnvironment::Create,
pmwcas::LinuxEnvironment::Destroy);
#else
pmwcas::InitLibrary(pmwcas::TlsAllocator::Create,
pmwcas::TlsAllocator::Destroy,
pmwcas::LinuxEnvironment::Create,
pmwcas::LinuxEnvironment::Destroy);
#endif // PMDK
#endif // WIN32
return RUN_ALL_TESTS();
}