/
traffic_structures_performance_tests.cpp
1111 lines (809 loc) · 42.4 KB
/
traffic_structures_performance_tests.cpp
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <boost/thread.hpp>
#include <boost/unordered/unordered_flat_map.hpp>
#include <boost/unordered_map.hpp>
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale.h>
#include <map>
#include <memory>
#include <mutex>
#include <stdint.h>
#include <sys/time.h>
#include <unistd.h>
#include <unordered_map>
#include <vector>
#include "../fast_endianless.hpp"
#include <boost/container/flat_map.hpp>
#include <boost/pool/pool_alloc.hpp>
#include <boost/pool/singleton_pool.hpp>
#include "../fastnetmon_types.hpp"
#include "../all_logcpp_libraries.hpp"
#ifdef TEST_TBB_LIBRARY
#ifndef __APPLE__
#include "tbb/concurrent_unordered_map.h"
#endif
#endif
#include <sys/ipc.h>
#include <sys/shm.h>
#ifdef ABSEIL_TESTS
#include "absl/container/flat_hash_map.h"
#include "absl/container/node_hash_map.h"
#endif
// It's not enabled because it crashes: https://github.com/sparsehash/sparsehash/issues/166
//#define TEST_SPARSE_HASH
#ifdef TEST_SPARSE_HASH
#include <sparsehash/dense_hash_map>
#endif
#include "../fast_library.hpp"
log4cpp::Category& logger = log4cpp::Category::getRoot();
std::mutex data_counter_mutex;
struct eqint {
bool operator()(uint32_t a, uint32_t b) const {
return a == b;
}
};
using namespace std;
int number_of_ips = 10 * 1000 * 1000;
int number_of_retries = 1;
// #define enable_mutexes_in_test
unsigned int number_of_threads = 1;
key_t generate_ipc_key() {
// 121 is unique project id
key_t ipc_key = ftok(__FILE__, 121);
if (ipc_key < 0) {
std::cerr << "Failed to Generate IPC Key" << std::endl;
return 0;
}
return ipc_key;
}
template <typename T> void packet_collector(T& data_structure) {
for (int iteration = 0; iteration < number_of_retries; iteration++) {
for (uint32_t i = 0; i < number_of_ips; i++) {
#ifdef enable_mutexes_in_test
data_counter_mutex.lock();
#endif
data_structure[i].udp.in_bytes++;
#ifdef enable_mutexes_in_test
data_counter_mutex.unlock();
#endif
}
}
}
template <typename T> void packet_collector_big_endian(T& data_structure) {
for (int iteration = 0; iteration < number_of_retries; iteration++) {
for (uint32_t i = 0; i < number_of_ips; i++) {
#ifdef enable_mutexes_in_test
data_counter_mutex.lock();
#endif
// Explicitly convert data to big endian to emulate our logic closely
data_structure[fast_hton(i)].udp.in_bytes++;
#ifdef enable_mutexes_in_test
data_counter_mutex.unlock();
#endif
}
}
}
// This function only implements conversion to big endian and accumulates result
template <typename T> void packet_collector_big_endian_conversion_only(T& accumulator) {
for (int iteration = 0; iteration < number_of_retries; iteration++) {
for (uint32_t i = 0; i < number_of_ips; i++) {
#ifdef enable_mutexes_in_test
data_counter_mutex.lock();
#endif
// Explicitly convert data to big endian to emulate our logic closely
accumulator += fast_hton(i);
#ifdef enable_mutexes_in_test
data_counter_mutex.unlock();
#endif
}
}
}
// We use it to avoid compiler to drop this value
uint64_t value_accumulator = 0;
// This function does full scan over hash table
template <typename T> void do_full_table_scan(T& accumulator) {
for (auto& elem : accumulator) {
#ifdef enable_mutexes_in_test
data_counter_mutex.lock();
#endif
value_accumulator += elem.second.udp.in_bytes;
#ifdef enable_mutexes_in_test
data_counter_mutex.unlock();
#endif
}
}
// This function does full scan over vector
template <typename T> void do_full_table_scan_vector(T& accumulator) {
for (auto& elem : accumulator) {
#ifdef enable_mutexes_in_test
data_counter_mutex.lock();
#endif
value_accumulator += elem.udp.in_bytes;
#ifdef enable_mutexes_in_test
data_counter_mutex.unlock();
#endif
}
}
// We just execute time read here
void packet_collector_time_calculaitons(int) {
struct timespec current_time;
for (int iteration = 0; iteration < number_of_retries; iteration++) {
for (uint32_t i = 0; i < number_of_ips; i++) {
clock_gettime(CLOCK_REALTIME, ¤t_time);
}
}
}
// We just execute time read here
void packet_collector_time_calculaitons_monotonic(int) {
struct timespec current_time;
for (int iteration = 0; iteration < number_of_retries; iteration++) {
for (uint32_t i = 0; i < number_of_ips; i++) {
clock_gettime(CLOCK_MONOTONIC, ¤t_time);
}
}
}
// We just execute time read here
void packet_collector_time_calculaitons_monotonic_coarse(int) {
struct timespec current_time;
for (int iteration = 0; iteration < number_of_retries; iteration++) {
for (uint32_t i = 0; i < number_of_ips; i++) {
clock_gettime(CLOCK_MONOTONIC_COARSE, ¤t_time);
}
}
}
// We just execute time read here
void packet_collector_time_calculaitons_gettimeofday(int) {
struct timeval current_time;
for (int iteration = 0; iteration < number_of_retries; iteration++) {
for (uint32_t i = 0; i < number_of_ips; i++) {
gettimeofday(¤t_time, NULL);
}
}
}
// We just execute time read here
/*
void packet_collector_time_calculaitons_rdtsc(int) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
uint64_t current_time;
for (int iteration = 0; iteration < number_of_retries; iteration++) {
for (uint32_t i = 0; i < number_of_ips; i++) {
current_time = read_tsc_cpu_register();
}
}
#pragma GCC diagnostic pop
}
*/
template <typename T> int run_tests(double total_operations, std::function<void(T&)> tested_function, T& value) {
timeval start_time;
gettimeofday(&start_time, NULL);
// std::cout << "Run "<< number_of_threads <<" threads" << endl;
boost::thread* threads[number_of_threads];
for (int i = 0; i < number_of_threads; i++) {
threads[i] = new boost::thread(tested_function, boost::ref(value));
}
// std::cout << "All threads started" << endl;
// std::cout << "Wait for finishing" << endl;
for (int i = 0; i < number_of_threads; i++) {
threads[i]->join();
}
// cout << "All threads finished" << endl;
timeval finish_time;
gettimeofday(&finish_time, NULL);
// We use ' for pretty print of long numbers
// http://stackoverflow.com/questions/1499156/convert-astronomically-large-numbers-into-human-readable-form-in-c-c
setlocale(LC_NUMERIC, "en_US.utf-8"); /* important */
timeval interval;
timeval_subtract(&interval, &finish_time, &start_time);
// Build time with float part
double used_time = (double)interval.tv_sec + (double)interval.tv_usec / 1000000;
// printf("We spent %f seconds\n", used_time);
double ops_per_second = total_operations / used_time;
double mega_ops_per_second = ops_per_second / 1000 / 1000;
printf("%'.1f mega ops per second\n", mega_ops_per_second);
return 0;
}
void init_logging() {
log4cpp::PatternLayout* console_layout = new log4cpp::PatternLayout();
console_layout->setConversionPattern("%d [%p] %m%n");
log4cpp::Appender* console_appender = new log4cpp::OstreamAppender("console", &std::cout);
console_appender->setLayout(console_layout);
logger.setPriority(log4cpp::Priority::DEBUG);
logger.addAppender(console_appender);
}
int main(int argc, char* argv[]) {
init_logging();
double total_operations = number_of_ips * number_of_retries * number_of_threads;
bool test_monotonic_coarse = false;
bool test_gettimeofday = false;
bool test_std_map = false;
bool test_std_map_big_endian = false;
bool test_tbb_concurrent_unordered_map = false;
bool test_boost_unordered_map = false;
bool test_boost_unordered_map_big_endian = false;
bool test_boost_unordered_map_preallocated = false;
bool test_boost_unordered_map_preallocated_big_endian = false;
bool test_boost_unordered_map_precreated = false;
bool test_boost_unordered_map_precreated_big_endian = false;
bool test_boost_unordered_flat_map = false;
bool test_boost_unordered_flat_map_big_endian = false;
bool test_boost_unordered_flat_map_preallocated = false;
bool test_boost_unordered_flat_map_preallocated_big_endian = false;
bool test_boost_unordered_flat_map_precreated = false;
bool test_boost_unordered_flat_map_precreated_big_endian = false;
bool test_boost_container_flat_map = false;
bool test_unordered_map_cpp11 = false;
bool test_unordered_map_cpp11_big_endian = false;
bool test_unordered_map_cpp11_preallocated = false;
bool test_unordered_map_cpp11_precreated = false;
bool test_vector_preallocated = false;
bool test_std_map_precreated = false;
bool test_clock_gettime_realtime = false;
bool test_clock_gettime_monotonic = false;
bool test_rdtsc_time = false;
bool test_c_array_preallocated = false;
bool test_c_array_huge_pages_preallocated = false;
bool tests_endian_less_conversion = false;
if (argc > 1) {
std::string first_argument = argv[1];
if (first_argument == "test_std_map") {
test_std_map = true;
} else if (first_argument == "test_std_map_big_endian") {
test_std_map_big_endian = true;
} else if (first_argument == "tests_endian_less_conversion") {
tests_endian_less_conversion = true;
} else if (first_argument == "test_tbb_concurrent_unordered_map") {
test_tbb_concurrent_unordered_map = true;
} else if (first_argument == "test_boost_unordered_map") {
test_boost_unordered_map = true;
} else if (first_argument == "test_boost_unordered_map_big_endian") {
test_boost_unordered_map_big_endian = true;
} else if (first_argument == "test_boost_unordered_map_preallocated") {
test_boost_unordered_map_preallocated = true;
} else if (first_argument == "test_boost_unordered_map_preallocated_big_endian") {
test_boost_unordered_map_preallocated_big_endian = true;
} else if (first_argument == "test_boost_unordered_map_precreated") {
test_boost_unordered_map_precreated = true;
} else if (first_argument == "test_boost_unordered_map_precreated_big_endian") {
test_boost_unordered_map_precreated_big_endian = true;
} else if (first_argument == "test_boost_container_flat_map") {
test_boost_container_flat_map = true;
} else if (first_argument == "test_unordered_map_cpp11") {
test_unordered_map_cpp11 = true;
} else if (first_argument == "test_unordered_map_cpp11_big_endian") {
test_unordered_map_cpp11_big_endian = true;
} else if (first_argument == "test_unordered_map_cpp11_preallocated") {
test_unordered_map_cpp11_preallocated = true;
} else if (first_argument == "test_vector_preallocated") {
test_vector_preallocated = true;
} else if (first_argument == "test_unordered_map_cpp11_precreated") {
test_unordered_map_cpp11_precreated = true;
} else if (first_argument == "test_std_map_precreated") {
test_std_map_precreated = true;
} else if (first_argument == "test_clock_gettime_realtime") {
test_clock_gettime_realtime = true;
} else if (first_argument == "test_clock_gettime_monotonic") {
test_clock_gettime_monotonic = true;
} else if (first_argument == "test_rdtsc_time") {
test_rdtsc_time = true;
} else if (first_argument == "test_gettimeofday") {
test_gettimeofday = true;
} else if (first_argument == "test_c_array_preallocated") {
test_c_array_preallocated = true;
} else if (first_argument == "test_c_array_huge_pages_preallocated") {
test_c_array_huge_pages_preallocated = true;
} else if (first_argument == "test_monotonic_coarse") {
test_monotonic_coarse = true;
}
} else {
test_monotonic_coarse = false;
test_clock_gettime_monotonic = false;
test_gettimeofday = false;
test_clock_gettime_realtime = false;
test_std_map = true;
test_std_map_big_endian = true;
test_std_map_precreated = true;
test_tbb_concurrent_unordered_map = true;
test_boost_unordered_map = true;
test_boost_unordered_map_big_endian = true;
test_boost_unordered_map_preallocated = true;
test_boost_unordered_map_preallocated_big_endian = true;
test_boost_unordered_map_precreated = true;
test_boost_unordered_map_precreated_big_endian = true;
test_boost_unordered_flat_map = true;
test_boost_unordered_flat_map_big_endian = true;
test_boost_unordered_flat_map_preallocated = true;
test_boost_unordered_flat_map_preallocated_big_endian = true;
test_boost_unordered_flat_map_precreated = true;
test_boost_unordered_flat_map_precreated_big_endian = true;
test_boost_container_flat_map = true;
test_unordered_map_cpp11 = true;
test_unordered_map_cpp11_big_endian = true;
test_unordered_map_cpp11_preallocated = true;
test_unordered_map_cpp11_precreated = true;
test_vector_preallocated = true;
test_rdtsc_time = true;
test_c_array_preallocated = true;
test_c_array_huge_pages_preallocated = false;
tests_endian_less_conversion = true;
}
std::cout << "Element size: " << sizeof(subnet_counter_t) << " bytes" << std::endl;
std::cout << "Total structure size: " << sizeof(subnet_counter_t) * number_of_ips / 1024 / 1024 << " Mbytes" << std::endl;
std::cout << std::endl << std::endl;
if (test_std_map) {
std::map<uint32_t, subnet_counter_t> DataCounter;
std::cout << "std::map: ";
run_tests(total_operations, packet_collector<std::map<uint32_t, subnet_counter_t>>, DataCounter);
std::cout << "std::map big endian keys full scan: ";
run_tests(DataCounter.size(), do_full_table_scan<std::map<uint32_t, subnet_counter_t>>, DataCounter);
DataCounter.clear();
}
std::cout << std::endl;
if (test_std_map_big_endian) {
std::map<uint32_t, subnet_counter_t> DataCounter;
std::cout << "std::map big endian keys: ";
run_tests(total_operations, packet_collector_big_endian<std::map<uint32_t, subnet_counter_t>>, DataCounter);
std::cout << "std::map big endian keys full scan: ";
run_tests(DataCounter.size(), do_full_table_scan<std::map<uint32_t, subnet_counter_t>>, DataCounter);
DataCounter.clear();
}
std::cout << std::endl;
if (test_std_map_precreated) {
std::map<uint32_t, subnet_counter_t> DataCounterPrecreated;
// Pre-create all elements
for (uint32_t i = 0; i < number_of_ips; i++) {
subnet_counter_t current_map_element;
DataCounterPrecreated.insert(std::make_pair(i, current_map_element));
}
std::cout << "std::map pre-created: ";
run_tests(total_operations, packet_collector<std::map<uint32_t, subnet_counter_t>>, DataCounterPrecreated);
std::cout << "std::map pre-created full scan: ";
run_tests(DataCounterPrecreated.size(), do_full_table_scan<std::map<uint32_t, subnet_counter_t>>, DataCounterPrecreated);
DataCounterPrecreated.clear();
}
std::cout << std::endl << std::endl;
if (test_boost_unordered_map) {
boost::unordered_map<uint32_t, subnet_counter_t> DataCounterBoostUnordered;
std::cout << "boost::unordered_map: ";
run_tests(total_operations, packet_collector<boost::unordered_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnordered);
std::cout << "boost::unordered_map full scan: ";
run_tests(DataCounterBoostUnordered.size(),
do_full_table_scan<boost::unordered_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnordered);
DataCounterBoostUnordered.clear();
}
std::cout << std::endl;
if (test_boost_unordered_map_big_endian) {
boost::unordered_map<uint32_t, subnet_counter_t> DataCounterBoostUnordered;
std::cout << "boost::unordered_map big endian keys: ";
run_tests(total_operations, packet_collector_big_endian<boost::unordered_map<uint32_t, subnet_counter_t>>,
DataCounterBoostUnordered);
std::cout << "boost::unordered_map big endian keys full scan: ";
run_tests(DataCounterBoostUnordered.size(),
do_full_table_scan<boost::unordered_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnordered);
DataCounterBoostUnordered.clear();
}
std::cout << std::endl;
if (test_boost_unordered_map_preallocated) {
boost::unordered_map<uint32_t, subnet_counter_t> DataCounterBoostUnorderedPreallocated;
std::cout << "boost::unordered_map with preallocated elements: ";
DataCounterBoostUnorderedPreallocated.reserve(number_of_ips);
run_tests(total_operations, packet_collector<boost::unordered_map<uint32_t, subnet_counter_t>>,
DataCounterBoostUnorderedPreallocated);
std::cout << "boost::unordered_map with preallocated elements full scan: ";
run_tests(DataCounterBoostUnorderedPreallocated.size(),
do_full_table_scan<boost::unordered_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnorderedPreallocated);
DataCounterBoostUnorderedPreallocated.clear();
}
std::cout << std::endl;
if (test_boost_unordered_map_preallocated_big_endian) {
boost::unordered_map<uint32_t, subnet_counter_t> DataCounterBoostUnorderedPreallocated;
std::cout << "boost::unordered_map big endian keys with preallocated elements: ";
DataCounterBoostUnorderedPreallocated.reserve(number_of_ips);
run_tests(total_operations, packet_collector_big_endian<boost::unordered_map<uint32_t, subnet_counter_t>>,
DataCounterBoostUnorderedPreallocated);
std::cout << "boost::unordered_map big endian keys with preallocated elements full scan: ";
run_tests(DataCounterBoostUnorderedPreallocated.size(),
do_full_table_scan<boost::unordered_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnorderedPreallocated);
DataCounterBoostUnorderedPreallocated.clear();
}
std::cout << std::endl;
if (test_boost_unordered_map_precreated) {
boost::unordered_map<uint32_t, subnet_counter_t> DataCounterBoostUnorderedPrecreated;
std::cout << "boost::unordered_map with pre-created elements: ";
for (uint32_t i = 0; i < number_of_ips; i++) {
subnet_counter_t current_map_element;
DataCounterBoostUnorderedPrecreated.insert(std::make_pair(i, current_map_element));
}
run_tests(total_operations, packet_collector<boost::unordered_map<uint32_t, subnet_counter_t>>,
DataCounterBoostUnorderedPrecreated);
std::cout << "boost::unordered_map with pre-created elements full scan: ";
run_tests(DataCounterBoostUnorderedPrecreated.size(),
do_full_table_scan<boost::unordered_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnorderedPrecreated);
DataCounterBoostUnorderedPrecreated.clear();
}
std::cout << std::endl;
if (test_boost_unordered_map_precreated_big_endian) {
boost::unordered_map<uint32_t, subnet_counter_t> DataCounterBoostUnorderedPrecreated;
std::cout << "boost::unordered_map big endian keys with pre-created elements: ";
for (uint32_t i = 0; i < number_of_ips; i++) {
subnet_counter_t current_map_element;
DataCounterBoostUnorderedPrecreated.insert(std::make_pair(fast_hton(i), current_map_element));
}
run_tests(total_operations, packet_collector_big_endian<boost::unordered_map<uint32_t, subnet_counter_t>>,
DataCounterBoostUnorderedPrecreated);
std::cout << "boost::unordered_map big endian with pre-created elements full scan: ";
run_tests(DataCounterBoostUnorderedPrecreated.size(),
do_full_table_scan<boost::unordered_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnorderedPrecreated);
DataCounterBoostUnorderedPrecreated.clear();
}
std::cout << std::endl << std::endl;
std::cout << std::endl << std::endl;
if (test_boost_unordered_flat_map) {
boost::unordered_flat_map<uint32_t, subnet_counter_t> DataCounterBoostUnordered;
std::cout << "boost::unordered_flat_map: ";
run_tests(total_operations, packet_collector<boost::unordered_flat_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnordered);
std::cout << "boost::unordered_flat_map full scan: ";
run_tests(DataCounterBoostUnordered.size(),
do_full_table_scan<boost::unordered_flat_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnordered);
DataCounterBoostUnordered.clear();
}
std::cout << std::endl;
if (test_boost_unordered_flat_map_big_endian) {
boost::unordered_flat_map<uint32_t, subnet_counter_t> DataCounterBoostUnordered;
std::cout << "boost::unordered_flat_map big endian keys: ";
run_tests(total_operations, packet_collector_big_endian<boost::unordered_flat_map<uint32_t, subnet_counter_t>>,
DataCounterBoostUnordered);
std::cout << "boost::unordered_flat_map big endian keys full scan: ";
run_tests(DataCounterBoostUnordered.size(),
do_full_table_scan<boost::unordered_flat_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnordered);
DataCounterBoostUnordered.clear();
}
std::cout << std::endl;
std::cout << std::endl;
if (test_boost_unordered_flat_map_preallocated) {
boost::unordered_flat_map<uint32_t, subnet_counter_t> DataCounterBoostUnorderedPreallocated;
std::cout << "boost::unordered_flat_map with preallocated elements: ";
DataCounterBoostUnorderedPreallocated.reserve(number_of_ips);
run_tests(total_operations, packet_collector<boost::unordered_flat_map<uint32_t, subnet_counter_t>>,
DataCounterBoostUnorderedPreallocated);
std::cout << "boost::unordered_flat_map with preallocated elements full scan: ";
run_tests(DataCounterBoostUnorderedPreallocated.size(),
do_full_table_scan<boost::unordered_flat_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnorderedPreallocated);
DataCounterBoostUnorderedPreallocated.clear();
}
std::cout << std::endl;
if (test_boost_unordered_flat_map_preallocated_big_endian) {
boost::unordered_flat_map<uint32_t, subnet_counter_t> DataCounterBoostUnorderedPreallocated;
std::cout << "boost::unordered_flat_map big endian keys with preallocated elements: ";
DataCounterBoostUnorderedPreallocated.reserve(number_of_ips);
run_tests(total_operations, packet_collector_big_endian<boost::unordered_flat_map<uint32_t, subnet_counter_t>>,
DataCounterBoostUnorderedPreallocated);
std::cout << "boost::unordered_flat_map big endian keys with preallocated elements full scan: ";
run_tests(DataCounterBoostUnorderedPreallocated.size(),
do_full_table_scan<boost::unordered_flat_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnorderedPreallocated);
DataCounterBoostUnorderedPreallocated.clear();
}
std::cout << std::endl;
if (test_boost_unordered_flat_map_precreated) {
boost::unordered_flat_map<uint32_t, subnet_counter_t> DataCounterBoostUnorderedPrecreated;
std::cout << "boost::unordered_flat_map with pre-created elements: ";
for (uint32_t i = 0; i < number_of_ips; i++) {
subnet_counter_t current_map_element;
DataCounterBoostUnorderedPrecreated.insert(std::make_pair(i, current_map_element));
}
run_tests(total_operations, packet_collector<boost::unordered_flat_map<uint32_t, subnet_counter_t>>,
DataCounterBoostUnorderedPrecreated);
std::cout << "boost::unordered_flat_map with pre-created elements full scan: ";
run_tests(DataCounterBoostUnorderedPrecreated.size(),
do_full_table_scan<boost::unordered_flat_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnorderedPrecreated);
DataCounterBoostUnorderedPrecreated.clear();
}
std::cout << std::endl;
if (test_boost_unordered_flat_map_precreated_big_endian) {
boost::unordered_flat_map<uint32_t, subnet_counter_t> DataCounterBoostUnorderedPrecreated;
std::cout << "boost::unordered_flat_map big endian keys with pre-created elements: ";
for (uint32_t i = 0; i < number_of_ips; i++) {
subnet_counter_t current_map_element;
DataCounterBoostUnorderedPrecreated.insert(std::make_pair(fast_hton(i), current_map_element));
}
run_tests(total_operations, packet_collector_big_endian<boost::unordered_flat_map<uint32_t, subnet_counter_t>>,
DataCounterBoostUnorderedPrecreated);
std::cout << "boost::unordered_flat_map big endian with pre-created elements full scan: ";
run_tests(DataCounterBoostUnorderedPrecreated.size(),
do_full_table_scan<boost::unordered_flat_map<uint32_t, subnet_counter_t>>, DataCounterBoostUnorderedPrecreated);
DataCounterBoostUnorderedPrecreated.clear();
}
std::cout << std::endl << std::endl;
if (test_boost_container_flat_map) {
boost::container::flat_map<uint32_t, subnet_counter_t> DataCounterBoostFlatMap;
// Boost flat_map
DataCounterBoostFlatMap.reserve(number_of_ips);
std::cout << "boost::container::flat_map with preallocated elements: ";
run_tests(total_operations, packet_collector<boost::container::flat_map<uint32_t, subnet_counter_t>>, DataCounterBoostFlatMap);
std::cout << "boost::container::flat_map with pre-allocated elements full scan: ";
run_tests(DataCounterBoostFlatMap.size(),
do_full_table_scan<boost::container::flat_map<uint32_t, subnet_counter_t>>, DataCounterBoostFlatMap);
DataCounterBoostFlatMap.clear();
}
std::cout << std::endl;
if (test_unordered_map_cpp11) {
std::unordered_map<uint32_t, subnet_counter_t> DataCounterUnordered;
std::cout << "std::unordered_map: ";
run_tests(total_operations, packet_collector<std::unordered_map<uint32_t, subnet_counter_t>>, DataCounterUnordered);
std::cout << "std::unordered_map full scan: ";
run_tests(DataCounterUnordered.size(), do_full_table_scan<std::unordered_map<uint32_t, subnet_counter_t>>, DataCounterUnordered);
DataCounterUnordered.clear();
}
std::cout << std::endl;
if (test_unordered_map_cpp11_big_endian) {
std::unordered_map<uint32_t, subnet_counter_t> DataCounterUnordered;
std::cout << "std::unordered_map big endian keys: ";
run_tests(total_operations, packet_collector_big_endian<std::unordered_map<uint32_t, subnet_counter_t>>, DataCounterUnordered);
std::cout << "std::unordered_map big endian keys full scan: ";
run_tests(DataCounterUnordered.size(), do_full_table_scan<std::unordered_map<uint32_t, subnet_counter_t>>, DataCounterUnordered);
DataCounterUnordered.clear();
}
std::cout << std::endl;
if (test_unordered_map_cpp11_preallocated) {
std::unordered_map<uint32_t, subnet_counter_t> DataCounterUnorderedPreallocated;
// Preallocate hash buckets
DataCounterUnorderedPreallocated.reserve(number_of_ips);
std::cout << "std::unordered_map preallocated buckets: ";
run_tests(total_operations, packet_collector<std::unordered_map<uint32_t, subnet_counter_t>>, DataCounterUnorderedPreallocated);
// std::cout << "Number of buckets: " << DataCounterUnorderedPreallocated.bucket_count() << std::endl;
// std::cout << "Number of IP's: " << DataCounterUnorderedPreallocated.size() << std::endl;
// std::cout << "Load factor: " << DataCounterUnorderedPreallocated.load_factor() << std::endl;
std::cout << "std::unordered_map preallocated buckets full scan: ";
run_tests(DataCounterUnorderedPreallocated.size(),
do_full_table_scan<std::unordered_map<uint32_t, subnet_counter_t>>, DataCounterUnorderedPreallocated);
DataCounterUnorderedPreallocated.clear();
}
std::cout << std::endl;
if (test_unordered_map_cpp11_precreated) {
std::unordered_map<uint32_t, subnet_counter_t> DataCounterUnorderedPrecreated;
// Pre-create all elements in hash
for (uint32_t i = 0; i < number_of_ips; i++) {
subnet_counter_t current_map_element;
DataCounterUnorderedPrecreated.insert(std::make_pair(i, current_map_element));
}
std::cout << "std::unordered_map pre-created elements: ";
run_tests(total_operations, packet_collector<std::unordered_map<uint32_t, subnet_counter_t>>, DataCounterUnorderedPrecreated);
std::cout << "std::unordered_map pre-created elements full scan: ";
run_tests(DataCounterUnorderedPrecreated.size(),
do_full_table_scan<std::unordered_map<uint32_t, subnet_counter_t>>, DataCounterUnorderedPrecreated);
DataCounterUnorderedPrecreated.clear();
}
std::cout << std::endl << std::endl;
#ifdef ABSEIL_TESTS
{
absl::flat_hash_map<uint32_t, subnet_counter_t> DataCounterAbseilFlatHashMap;
std::cout << "abesil::flat_hash_map: ";
run_tests(total_operations, packet_collector<absl::flat_hash_map<uint32_t, subnet_counter_t>>, DataCounterAbseilFlatHashMap);
std::cout << "abesil::flat_hash_map full scan: ";
run_tests(DataCounterAbseilFlatHashMap.size(),
do_full_table_scan<absl::flat_hash_map<uint32_t, subnet_counter_t>>, DataCounterAbseilFlatHashMap);
DataCounterAbseilFlatHashMap.clear();
}
#endif
std::cout << std::endl << std::endl;
#ifdef ABSEIL_TESTS
{
absl::flat_hash_map<uint32_t, subnet_counter_t> DataCounterAbseilFlatHashMap;
// Pre-create all elements in hash
for (uint32_t i = 0; i < number_of_ips; i++) {
subnet_counter_t current_map_element;
DataCounterAbseilFlatHashMap.insert(std::make_pair(i, current_map_element));
}
std::cout << "abesil::flat_hash_map pre-created elements : ";
run_tests(total_operations, packet_collector<absl::flat_hash_map<uint32_t, subnet_counter_t>>, DataCounterAbseilFlatHashMap);
std::cout << "abesil::flat_hash_map pre-created elements full scan: ";
run_tests(DataCounterAbseilFlatHashMap.size(),
do_full_table_scan<absl::flat_hash_map<uint32_t, subnet_counter_t>>, DataCounterAbseilFlatHashMap);
DataCounterAbseilFlatHashMap.clear();
}
#endif
std::cout << std::endl << std::endl;
#ifdef ABSEIL_TESTS
{
absl::node_hash_map<uint32_t, subnet_counter_t> DataCounterAbseilNodeHashMap;
std::cout << "abesil::node_hash_map: ";
run_tests(total_operations, packet_collector<absl::node_hash_map<uint32_t, subnet_counter_t>>, DataCounterAbseilNodeHashMap);
std::cout << "abesil::node_hash_map full scan: ";
run_tests(DataCounterAbseilNodeHashMap.size(),
do_full_table_scan<absl::node_hash_map<uint32_t, subnet_counter_t>>, DataCounterAbseilNodeHashMap);
DataCounterAbseilNodeHashMap.clear();
}
#endif
std::cout << std::endl << std::endl;
#ifdef ABSEIL_TESTS
{
absl::node_hash_map<uint32_t, subnet_counter_t> DataCounterAbseilNodeHashMap;
// Pre-create all elements in hash
for (uint32_t i = 0; i < number_of_ips; i++) {
subnet_counter_t current_map_element;
DataCounterAbseilNodeHashMap.insert(std::make_pair(i, current_map_element));
}
std::cout << "abesil::node_hash_map pre-created elements: ";
run_tests(total_operations, packet_collector<absl::node_hash_map<uint32_t, subnet_counter_t>>, DataCounterAbseilNodeHashMap);
std::cout << "abesil::node_hash_map pre-created elements full scan: ";
run_tests(DataCounterAbseilNodeHashMap.size(),
do_full_table_scan<absl::node_hash_map<uint32_t, subnet_counter_t>>, DataCounterAbseilNodeHashMap);
DataCounterAbseilNodeHashMap.clear();
}
#endif
std::cout << std::endl << std::endl;
#ifdef TEST_SPARSE_HASH
google::dense_hash_map<uint32_t, subnet_counter_t, std::hash<uint32_t>, eqint, google::libc_allocator_with_realloc<std::pair<const uint32_t, subnet_counter_t>>> DataCounterGoogleDensehashMap;
std::cout << "google:dense_hashmap without preallocation: ";
DataCounterGoogleDensehashMap.set_empty_key(UINT32_MAX); // We will got assert without it!
run_tests(total_operations,
packet_collector<google::dense_hash_map<uint32_t, subnet_counter_t, std::hash<uint32_t>, eqint,
google::libc_allocator_with_realloc<std::pair<const uint32_t, subnet_counter_t>>>>,
DataCounterGoogleDensehashMap);
DataCounterGoogleDensehashMap.clear();
#endif
#ifdef TEST_SPARSE_HASH
google::dense_hash_map<uint32_t, subnet_counter_t, std::hash<uint32_t>, eqint, google::libc_allocator_with_realloc<std::pair<const uint32_t, subnet_counter_t>>> DataCounterGoogleDensehashMapPreallocated;
std::cout << "google:dense_hashmap preallocated buckets: ";
// We use UINT32_MAX as "empty" here, not a good idea but OK for tests
DataCounterGoogleDensehashMapPreallocated.set_empty_key(UINT32_MAX); // We will got assert without it!
DataCounterGoogleDensehashMapPreallocated.resize(number_of_ips);
run_tests(total_operations,
packet_collector<google::dense_hash_map<uint32_t, subnet_counter_t, std::hash<uint32_t>, eqint,
google::libc_allocator_with_realloc<std::pair<const uint32_t, subnet_counter_t>>>>,
DataCounterGoogleDensehashMapPreallocated);
DataCounterGoogleDensehashMapPreallocated.clear();
#endif
if (test_tbb_concurrent_unordered_map) {
#ifdef TEST_TBB_LIBRARY
#ifndef __APPLE_
tbb::concurrent_unordered_map<uint32_t, subnet_counter_t> DataCounterUnorderedConcurrent;
std::cout << "tbb::concurrent_unordered_map: ";
run_tests(total_operations, packet_collector<tbb::concurrent_unordered_map<uint32_t, subnet_counter_t>>,
DataCounterUnorderedConcurrent);
DataCounterUnorderedConcurrent.clear();
#endif
#endif
}
std::cout << std::endl;
if (test_vector_preallocated) {
std::vector<subnet_counter_t> DataCounterVector(number_of_ips);
std::cout << "std::vector preallocated: ";
run_tests(total_operations, packet_collector<std::vector<subnet_counter_t>>, DataCounterVector);
std::cout << "std::vector full scan: ";
run_tests(DataCounterVector.size(), do_full_table_scan_vector<std::vector<subnet_counter_t>>, DataCounterVector);
DataCounterVector.clear();
}
std::cout << std::endl;
if (test_c_array_preallocated) {
subnet_counter_t* data_counter_c_array_ptr = nullptr;
data_counter_c_array_ptr = new subnet_counter_t[number_of_ips];
std::cout << "C array preallocated: ";
run_tests(total_operations, packet_collector<subnet_counter_t*>, data_counter_c_array_ptr);
delete[] data_counter_c_array_ptr;
data_counter_c_array_ptr = NULL;
}
if (test_c_array_huge_pages_preallocated) {
// Here you could find awesome example for this option:
// http://lxr.free-electrons.com/source/tools/testing/selftests/vm/hugepage-shm.c
uint32_t required_number_of_bytes = sizeof(subnet_counter_t) * number_of_ips;
std::map<std::string, uint64_t> meminfo_map;
bool parse_meminfo = parse_meminfo_into_map(meminfo_map);
if (!parse_meminfo) {
std::cerr << "Could not parse meminfo" << std::endl;
exit(-1);
}
uint64_t required_number_of_hugetlb_pages = ceil(required_number_of_bytes / meminfo_map["Hugepagesize"]);
// std::cout << "We need least: " << required_number_of_hugetlb_pages << " of huge TLB pages" << std::endl;
if (meminfo_map["HugePages_Free"] < required_number_of_hugetlb_pages) {
// std::cerr << "We need " << required_number_of_hugetlb_pages << " hugetlb pages. But we have only: " <<
// meminfo_map["HugePages_Free"] << std::endl;
std::cerr << "Let's try to allocated required number of pages" << std::endl;
std::string allocate_required_number_of_huge_pages =
"echo " + std::to_string(required_number_of_hugetlb_pages) + "> /proc/sys/vm/nr_hugepages";
exec_no_error_check(allocate_required_number_of_huge_pages);
}
// If huge tlb pages allocation failed
uint64_t we_have_huge_tlb_memory = meminfo_map["Hugepagesize"] * meminfo_map["HugePages_Free"];
if (we_have_huge_tlb_memory < required_number_of_bytes) {
std::cerr << "We need least " << required_number_of_bytes
<< " bytes but we have free only: " << we_have_huge_tlb_memory << std::endl;
std::cerr << "Total number of huge pages is: " << meminfo_map["HugePages_Total"] << std::endl;
exit(-1);
}
// Also we teed to tune kernel options about maximum shm memory segment size and overall shm memory size for
// whole system becuase by default they are pretty low
// std::cerr << "We need " << required_number_of_bytes << " bytes"<< std::endl;
// TODO: please use print to handle for correct error handling because echo ignores all write errors
std::string increase_shm_all_command = "echo " + std::to_string(required_number_of_bytes) + " > /proc/sys/kernel/shmall";
std::string increase_shm_max_command = "echo " + std::to_string(required_number_of_bytes) + " > /proc/sys/kernel/shmmax";
exec_no_error_check(increase_shm_all_command);
exec_no_error_check(increase_shm_max_command);
// Let's check new values right now!
int shm_all_value = 0;