-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrcu_bench2.cpp
More file actions
746 lines (611 loc) · 19.8 KB
/
rcu_bench2.cpp
File metadata and controls
746 lines (611 loc) · 19.8 KB
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#include <cppurcu/cppurcu.h>
#include <iostream>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <thread>
#include <mutex>
#include <chrono>
#include <random>
#include <atomic>
#include <urcu.h>
#include <urcu/rculist.h>
#include <urcu-call-rcu.h>
using namespace std;
using namespace chrono;
vector<pair<string, string>> generate_test_ips(size_t count)
{
vector<pair<string, string>> ips;
ips.reserve(count);
mt19937 gen(12345);
uniform_int_distribution<> dist(0, 255);
for (size_t i = 0; i < count; ++i)
{
string ip = to_string(dist(gen)) + "." +
to_string(dist(gen)) + "." +
to_string(dist(gen)) + "." +
to_string(dist(gen));
ips.push_back({ip, "test-data"});
}
return ips;
}
class MutexContainer
{
public:
MutexContainer()
{
ips_ = make_shared<unordered_map<string, string>>();
}
bool contains(const string &ip)
{
lock_guard<mutex> lock(mutex_);
return ips_->count(ip) > 0;
}
void update(shared_ptr<unordered_map<string, string>> new_ips)
{
lock_guard<mutex> lock(mutex_);
ips_ = new_ips;
}
private:
shared_ptr<unordered_map<string, string>> ips_;
mutex mutex_;
};
void benchmark_mutex(
size_t num_readers,
size_t num_writers,
seconds test_duration,
const vector<shared_ptr<unordered_map<string, string>>> &test_data_array,
const vector<pair<string, string>> &test_ips)
{
cout << "\n========================================\n";
cout << "std::mutex\n";
cout << "========================================\n";
cout << "Reader thread : " << num_readers << "\n";
cout << "Writer thread : " << num_writers << "\n";
cout << "test duration : " << test_duration.count() << " sec\n";
MutexContainer container;
container.update(test_data_array[0]);
atomic<bool> stop_flag{false};
atomic<bool> start_flag{false};
atomic<size_t> total_reads{0};
atomic<size_t> total_writes{0};
vector<thread> readers;
for (size_t i = 0; i < num_readers; ++i)
{
readers.emplace_back([&, i]()
{
mt19937 gen(12345 + i);
uniform_int_distribution<size_t> dist(0, test_ips.size() - 1);
for (int w = 0; w < 1000; ++w)
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
}
while (!start_flag.load(memory_order_acquire))
this_thread::yield();
while (!stop_flag.load(memory_order_relaxed))
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
total_reads.fetch_add(1, memory_order_relaxed);
}
});
}
vector<thread> writers;
for (size_t i = 0; i < num_writers; ++i)
{
writers.emplace_back([&, i]()
{
size_t index = i;
while (!start_flag.load(memory_order_acquire))
this_thread::yield();
while (!stop_flag.load(memory_order_relaxed))
{
container.update(test_data_array[index]);
index += num_writers;
total_writes.fetch_add(1, memory_order_relaxed);
this_thread::sleep_for(milliseconds(100));
}
});
}
this_thread::sleep_for(milliseconds(100));
start_flag.store(true, memory_order_release);
auto start = high_resolution_clock::now();
this_thread::sleep_for(test_duration);
stop_flag.store(true, memory_order_relaxed);
for (auto &t : readers)
t.join();
for (auto &t : writers)
t.join();
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
cout << "execution duration : " << duration.count() << " ms\n";
cout << "total read count : " << total_reads << "\n";
cout << "total write count : " << total_writes << "\n";
cout << "read throughput : " << (total_reads * 1000.0 / duration.count()) << " ops/sec\n";
cout << "read per second : " << (total_reads / test_duration.count()) << " reads/sec\n";
}
class CPPURCUContainer
{
public:
CPPURCUContainer()
: ips_(std::make_shared<unordered_map<string, string>>())
{
}
bool contains(const string &ip)
{
auto ips = ips_.load();
return ips->count(ip) > 0;
}
void update(shared_ptr<unordered_map<string, string>> new_ips)
{
ips_ = new_ips;
}
private:
cppurcu::storage<unordered_map<string, string>> ips_;
};
void benchmark_cppurcu(
size_t num_readers,
size_t num_writers,
seconds test_duration,
const vector<shared_ptr<unordered_map<string, string>>> &test_data_array,
const vector<pair<string, string>> &test_ips)
{
cout << "\n========================================\n";
cout << "cppurcu\n";
cout << "========================================\n";
cout << "Reader thread : " << num_readers << "\n";
cout << "Writer thread : " << num_writers << "\n";
cout << "test duration : " << test_duration.count() << " sec\n";
CPPURCUContainer container;
container.update(test_data_array[0]);
atomic<bool> stop_flag{false};
atomic<bool> start_flag{false};
atomic<size_t> total_reads{0};
atomic<size_t> total_writes{0};
vector<thread> readers;
for (size_t i = 0; i < num_readers; ++i)
{
readers.emplace_back([&, i]()
{
mt19937 gen(12345 + i);
uniform_int_distribution<size_t> dist(0, test_ips.size() - 1);
for (int w = 0; w < 1000; ++w)
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
}
while (!start_flag.load(memory_order_acquire))
this_thread::yield();
while (!stop_flag.load(memory_order_relaxed))
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
total_reads.fetch_add(1, memory_order_relaxed);
}
});
}
vector<thread> writers;
for (size_t i = 0; i < num_writers; ++i)
{
writers.emplace_back([&, i]()
{
size_t index = i;
while (!start_flag.load(memory_order_acquire))
this_thread::yield();
while (!stop_flag.load(memory_order_relaxed))
{
container.update(test_data_array[index]);
index += num_writers;
total_writes.fetch_add(1, memory_order_relaxed);
this_thread::sleep_for(milliseconds(100));
}
});
}
this_thread::sleep_for(milliseconds(100));
start_flag.store(true, memory_order_release);
auto start = high_resolution_clock::now();
this_thread::sleep_for(test_duration);
stop_flag.store(true, memory_order_relaxed);
for (auto &t : readers)
t.join();
for (auto &t : writers)
t.join();
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
cout << "execution duration : " << duration.count() << " ms\n";
cout << "total read count : " << total_reads << "\n";
cout << "total write count : " << total_writes << "\n";
cout << "read throughput : " << (total_reads * 1000.0 / duration.count()) << " ops/sec\n";
cout << "read per second : " << (total_reads / test_duration.count()) << " reads/sec\n";
}
// ============================================================================
// liburcu with synchronize_rcu (sync)
// ============================================================================
class LiburcuContainerSync
{
public:
LiburcuContainerSync()
{
rcu_assign_pointer(ptr_, &data_);
}
~LiburcuContainerSync()
{
synchronize_rcu();
}
bool contains(const string &ip)
{
rcu_read_lock();
auto *m = rcu_dereference(ptr_);
bool ok = (m->count(ip) > 0);
rcu_read_unlock();
return ok;
}
void update(unordered_map<string, string> *new_ips_sp)
{
rcu_xchg_pointer(&ptr_, new_ips_sp);
synchronize_rcu();
}
private:
unordered_map<string, string> data_;
unordered_map<string, string> *ptr_{nullptr};
};
void benchmark_liburcu_sync(
size_t num_readers,
size_t num_writers,
chrono::seconds test_duration,
const vector<shared_ptr<unordered_map<string, string>>> &test_data_array,
const vector<pair<string, string>> &test_ips)
{
cout << "\n========================================\n";
cout << "liburcu (synchronize_rcu - sync)\n";
cout << "========================================\n";
cout << "Reader thread : " << num_readers << "\n";
cout << "Writer thread : " << num_writers << "\n";
cout << "test duration : " << test_duration.count() << " sec\n";
rcu_init();
LiburcuContainerSync container;
container.update(test_data_array[0].get());
atomic<bool> stop_flag{false};
atomic<bool> start_flag{false};
atomic<size_t> total_reads{0};
atomic<size_t> total_writes{0};
vector<thread> readers;
readers.reserve(num_readers);
for (size_t i = 0; i < num_readers; ++i)
{
readers.emplace_back([&, i]()
{
rcu_register_thread();
mt19937 gen(12345 + i);
uniform_int_distribution<size_t> dist(0, test_ips.size() - 1);
for (int w = 0; w < 1000; ++w)
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
}
while (!start_flag.load(memory_order_acquire))
this_thread::yield();
while (!stop_flag.load(memory_order_relaxed))
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
total_reads.fetch_add(1, memory_order_relaxed);
}
rcu_unregister_thread();
});
}
vector<thread> writers;
writers.reserve(num_writers);
for (size_t i = 0; i < num_writers; ++i)
{
writers.emplace_back([&, i]()
{
size_t index = i;
while (!start_flag.load(memory_order_acquire))
this_thread::yield();
while (!stop_flag.load(memory_order_relaxed))
{
container.update(test_data_array[index].get());
index += num_writers;
total_writes.fetch_add(1, memory_order_relaxed);
this_thread::sleep_for(chrono::milliseconds(100));
}
});
}
this_thread::sleep_for(chrono::milliseconds(100));
start_flag.store(true, memory_order_release);
auto start = chrono::high_resolution_clock::now();
this_thread::sleep_for(test_duration);
stop_flag.store(true, memory_order_relaxed);
for (auto &t : readers)
t.join();
for (auto &t : writers)
t.join();
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << "execution duration : " << duration.count() << " ms\n";
cout << "total read count : " << total_reads << "\n";
cout << "total write count : " << total_writes << "\n";
cout << "read throughput : " << (total_reads * 1000.0 / duration.count()) << " ops/sec\n";
cout << "read per second : " << (total_reads / test_duration.count()) << " reads/sec\n";
}
// ============================================================================
// liburcu with call_rcu (async)
// ============================================================================
struct RcuMapWrapper
{
struct rcu_head rcu;
unordered_map<string, string> *map;
};
static void free_map_callback(struct rcu_head *head)
{
caa_container_of(head, RcuMapWrapper, rcu);
}
class LiburcuContainerAsync
{
public:
LiburcuContainerAsync()
{
rcu_assign_pointer(ptr_, &data_);
}
~LiburcuContainerAsync()
{
rcu_barrier();
}
bool contains(const string &ip)
{
rcu_read_lock();
auto *m = rcu_dereference(ptr_);
bool ok = (m->count(ip) > 0);
rcu_read_unlock();
return ok;
}
void update(unordered_map<string, string> *new_ips_sp, RcuMapWrapper *wrapper)
{
auto *old = rcu_xchg_pointer(&ptr_, new_ips_sp);
if (old != nullptr)
{
wrapper->map = old;
call_rcu(&wrapper->rcu, free_map_callback);
}
}
private:
unordered_map<string, string> data_;
unordered_map<string, string> *ptr_{nullptr};
};
void benchmark_liburcu_async(
size_t num_readers,
size_t num_writers,
chrono::seconds test_duration,
const vector<shared_ptr<unordered_map<string, string>>> &test_data_array,
const vector<pair<string, string>> &test_ips)
{
cout << "\n========================================\n";
cout << "liburcu (call_rcu - async)\n";
cout << "========================================\n";
cout << "Reader thread : " << num_readers << "\n";
cout << "Writer thread : " << num_writers << "\n";
cout << "test duration : " << test_duration.count() << " sec\n";
rcu_init();
RcuMapWrapper wrapper;
std::vector<RcuMapWrapper> wrappers(300);
LiburcuContainerAsync container;
container.update(test_data_array[0].get(), &wrapper);
atomic<bool> stop_flag{false};
atomic<bool> start_flag{false};
atomic<size_t> total_reads{0};
atomic<size_t> total_writes{0};
vector<thread> readers;
readers.reserve(num_readers);
for (size_t i = 0; i < num_readers; ++i)
{
readers.emplace_back([&, i]()
{
rcu_register_thread();
mt19937 gen(12345 + i);
uniform_int_distribution<size_t> dist(0, test_ips.size() - 1);
for (int w = 0; w < 1000; ++w)
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
}
while (!start_flag.load(memory_order_acquire))
this_thread::yield();
while (!stop_flag.load(memory_order_relaxed))
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
total_reads.fetch_add(1, memory_order_relaxed);
}
rcu_unregister_thread();
});
}
vector<thread> writers;
writers.reserve(num_writers);
for (size_t i = 0; i < num_writers; ++i)
{
writers.emplace_back([&, i]()
{
while (!start_flag.load(memory_order_acquire))
this_thread::yield();
size_t index = i;
while (!stop_flag.load(memory_order_relaxed))
{
container.update(test_data_array[index].get(), &wrappers[index]);
index += num_writers;
total_writes.fetch_add(1, memory_order_relaxed);
this_thread::sleep_for(chrono::milliseconds(100));
}
});
}
this_thread::sleep_for(chrono::milliseconds(100));
start_flag.store(true, memory_order_release);
auto start = chrono::high_resolution_clock::now();
this_thread::sleep_for(test_duration);
stop_flag.store(true, memory_order_relaxed);
for (auto &t : readers)
t.join();
for (auto &t : writers)
t.join();
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << "execution duration : " << duration.count() << " ms\n";
cout << "total read count : " << total_reads << "\n";
cout << "total write count : " << total_writes << "\n";
cout << "read throughput : " << (total_reads * 1000.0 / duration.count()) << " ops/sec\n";
cout << "read per second : " << (total_reads / test_duration.count()) << " reads/sec\n";
}
class CPPURCURetirementContainer
{
public:
CPPURCURetirementContainer()
: ips_(std::make_shared<unordered_map<string, string>>(),
std::make_shared<cppurcu::reclaimer_thread>())
{
}
bool contains(const string &ip)
{
auto ips = ips_.load();
return ips->count(ip) > 0;
}
void update(shared_ptr<unordered_map<string, string>> new_ips)
{
ips_ = new_ips;
}
private:
cppurcu::storage<unordered_map<string, string>> ips_;
};
void benchmark_reclaimer(
size_t num_readers,
size_t num_writers,
seconds test_duration,
const vector<shared_ptr<unordered_map<string, string>>> &test_data_array,
const vector<pair<string, string>> &test_ips)
{
cout << "\n========================================\n";
cout << "cppurcu + reclaimer_thread\n";
cout << "========================================\n";
cout << "Reader thread : " << num_readers << "\n";
cout << "Writer thread : " << num_writers << "\n";
cout << "test duration : " << test_duration.count() << " sec\n";
CPPURCURetirementContainer container;
container.update(test_data_array[0]);
atomic<bool> stop_flag{false};
atomic<bool> start_flag{false};
atomic<size_t> total_reads{0};
atomic<size_t> total_writes{0};
vector<thread> readers;
for (size_t i = 0; i < num_readers; ++i)
{
readers.emplace_back([&, i]()
{
mt19937 gen(12345 + i);
uniform_int_distribution<size_t> dist(0, test_ips.size() - 1);
for (int w = 0; w < 1000; ++w)
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
}
while (!start_flag.load(memory_order_acquire))
this_thread::yield();
while (!stop_flag.load(memory_order_relaxed))
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
total_reads.fetch_add(1, memory_order_relaxed);
}
});
}
vector<thread> writers;
for (size_t i = 0; i < num_writers; ++i)
{
writers.emplace_back([&, i]()
{
size_t index = i;
while (!start_flag.load(memory_order_acquire))
this_thread::yield();
while (!stop_flag.load(memory_order_relaxed))
{
container.update(test_data_array[index]);
index += num_writers;
total_writes.fetch_add(1, memory_order_relaxed);
this_thread::sleep_for(milliseconds(100));
}
});
}
this_thread::sleep_for(milliseconds(100));
start_flag.store(true, memory_order_release);
auto start = high_resolution_clock::now();
this_thread::sleep_for(test_duration);
stop_flag.store(true, memory_order_relaxed);
for (auto &t : readers)
t.join();
for (auto &t : writers)
t.join();
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
cout << "execution duration : " << duration.count() << " ms\n";
cout << "total read count : " << total_reads << "\n";
cout << "total write count : " << total_writes << "\n";
cout << "read throughput : " << (total_reads * 1000.0 / duration.count()) << " ops/sec\n";
cout << "read per second : " << (total_reads / test_duration.count()) << " reads/sec\n";
}
void flush_cache()
{
const size_t cache_size = 128 * 1024 * 1024;
volatile char *dummy = new char[cache_size];
for (size_t i = 0; i < cache_size; i += 64)
dummy[i] = 1;
delete[] dummy;
}
// ============================================================================
// main
// ============================================================================
int main(int argc, char **argv)
{
size_t gen_size = 1000;
int num_runs = 1;
if (argc >= 2)
gen_size = atoi(argv[1]);
size_t num_readers = 10;
size_t num_writers = 2;
seconds test_duration(10);
cout << "==================================\n";
cout << "TEST SET : " << gen_size << "\n";
cout << "- Reader thread : " << num_readers << "\n";
cout << "- Writer thread : " << num_writers << "\n";
cout << "- test duration : " << test_duration.count() << " sec\n";
cout << "- Update period : 100 ms\n";
cout << "generating test data...\n";
auto test_ips = generate_test_ips(gen_size);
auto test_data = make_shared<unordered_map<string, string>>();
for (const auto &[ip, value] : test_ips)
{
test_data->insert({ip, value});
}
vector<shared_ptr<unordered_map<string, string>>> test_data_array;
test_data_array.reserve(220);
for (int i = 0; i < 220; ++i)
test_data_array.push_back(make_shared<unordered_map<string, string>>(*test_data));
cout << "Test data generation completed (200 copies)\n";
for (int run = 0; run < num_runs; ++run)
{
if (num_runs > 1)
cout << "\n********** Run " << (run + 1) << " / " << num_runs << " **********\n";
flush_cache();
benchmark_mutex(num_readers, num_writers, test_duration, test_data_array, test_ips);
flush_cache();
benchmark_reclaimer(num_readers, num_writers, test_duration, test_data_array, test_ips);
flush_cache();
benchmark_cppurcu(num_readers, num_writers, test_duration, test_data_array, test_ips);
flush_cache();
benchmark_liburcu_sync(num_readers, num_writers, test_duration, test_data_array, test_ips);
flush_cache();
benchmark_liburcu_async(num_readers, num_writers, test_duration, test_data_array, test_ips);
}
cout << "\n==================================\n";
cout << "Test completed\n";
return 0;
}