-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbenchmark.h
852 lines (732 loc) · 27.3 KB
/
benchmark.h
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
#pragma once
#include <immintrin.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <cfenv>
#include <dtl/thread.hpp>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "config.h"
#include "searches/branching_binary_search.h"
#include "util.h"
#include "utils/perf_event.h"
#ifdef __linux__
#define checkLinux(x) (x)
#else
#define checkLinux(x) \
{ util::fail("Only supported on Linux."); }
#endif
// Get the CPU affinity for the process.
static const auto cpu_mask = dtl::this_thread::get_cpu_affinity();
// Batch size in number of lookups.
static constexpr std::size_t batch_size = 1u << 16;
namespace sosd {
// KeyType: Controls the type of the key (the value will always be uint64_t)
// Use uint64_t for 64 bit types and uint32_t for 32 bit types
// KeyType must implement operator<
template <typename KeyType = uint64_t,
template <typename> typename SearchClass = BranchingBinarySearch>
class Benchmark {
public:
Benchmark(const std::string& data_filename,
const std::string& lookups_filename, const size_t num_repeats,
const bool perf, const bool build, const bool csv, const bool sep,
const bool tail, const double sampling_ratio, const int perf_no,
const size_t num_threads, const SearchClass<KeyType> searcher,
const std::string& csv_suffix)
: data_filename_(data_filename),
lookups_filename_(lookups_filename),
num_repeats_(num_repeats),
first_run_(true),
perf_(perf),
build_(build),
csv_(csv),
sep_(sep),
tail_(tail),
sampling_ratio_(sampling_ratio),
perf_no_(perf_no),
num_threads_(num_threads),
searcher_(searcher),
csv_suffix_(csv_suffix) {
static constexpr const char* prefix = "data/";
dataset_name_ = data_filename.data();
dataset_name_.erase(
dataset_name_.begin(),
dataset_name_.begin() + dataset_name_.find(prefix) + strlen(prefix));
// Load data.
std::vector<KeyType> keys = util::load_data<KeyType>(data_filename_);
log_sum_search_bound_ = 0.0;
l1_sum_search_bound_ = 0.0;
l2_sum_search_bound_ = 0.0;
if (!is_sorted(keys.begin(), keys.end()))
util::fail("keys have to be sorted");
// Check whether keys are unique.
unique_keys_ = util::is_unique(keys);
if (unique_keys_)
std::cout << "data is unique" << std::endl;
else
std::cout << "data contains duplicates" << std::endl;
// Add artificial values to keys.
data_ = util::add_values(keys);
// Load lookups.
lookups_ = util::load_data<EqualLookup<KeyType>>(lookups_filename_);
// Create the data for the index (key -> position).
for (uint64_t pos = 0; pos < data_.size(); pos++) {
index_data_.push_back((KeyValue<KeyType>){data_[pos].key, pos});
}
}
template <class Index>
void Run() {
// Build index.
Index index;
if (!index.applicable(unique_keys_, data_filename_)) {
std::cout << "index " << index.name() << " is not applicable"
<< std::endl;
return;
}
build_ns_ = index.Build(index_data_, lookups_);
if (index.name().find("Expo") == std::string::npos) {
DoLookups<Index>(index);
} else {
DoLookupsExpo<Index>(index);
}
PrintResult(index);
first_run_ = false;
}
bool uses_binary_search() const {
return (searcher_.name() == "BinarySearch") ||
(searcher_.name() == "BranchlessBinarySearch");
}
bool uses_lienar_search() const { return searcher_.name() == "LinearSearch"; }
private:
bool CheckResults(uint64_t actual, uint64_t expected, KeyType lookup_key,
SearchBound bound) {
if (actual != expected) {
const auto pos = std::find_if(
data_.begin(), data_.end(),
[lookup_key](const auto& kv) { return kv.key == lookup_key; });
const auto idx = std::distance(data_.begin(), pos);
std::cout << "equality lookup returned wrong result:" << std::endl;
std::cout << "lookup key: " << lookup_key << std::endl;
std::cout << "actual: " << actual << ", expected: " << expected
<< std::endl
<< "correct index is: " << idx << std::endl
<< "index start: " << bound.start << " stop: " << bound.stop
<< std::endl;
return false;
}
return true;
}
template <class Index>
void DoLookups(Index& index) {
if (build_) return;
// Atomic counter used to assign work to threads.
std::atomic<std::size_t> cntr(0);
bool run_failed = false;
runs_.resize(num_repeats_);
for (unsigned int i = 0; i < num_repeats_; ++i) {
random_sum_ = 0;
individual_ns_sum_ = 0;
uint64_t ms;
if (num_threads_ == 1) {
if (sep_) {
ms = util::timing([&] {
SepBinLookupsLoop<Index>(index, 0, lookups_.size(), run_failed);
});
} else if (tail_) {
tail_latencys_.resize(lookups_.size() * sampling_ratio_);
ms = util::timing([&] {
TailBinLookupsLoop<Index>(index, 0, lookups_.size(), run_failed);
});
} else if (perf_) {
ms = util::timing([&] {
PerfBinLookupsLoop<Index>(index, 0, lookups_.size(), run_failed);
});
} else {
ms = util::timing([&] {
AvgBinLookupsLoop<Index>(index, 0, lookups_.size(), run_failed);
});
}
} else {
// Reset atomic counter.
cntr.store(0);
ms = util::timing([&] {
while (true) {
const size_t begin = cntr.fetch_add(batch_size);
if (begin >= lookups_.size()) break;
unsigned int limit = std::min(begin + batch_size, lookups_.size());
AvgBinLookupsLoop<Index>(index, begin, limit, run_failed);
}
});
}
runs_[i] = ms;
if (run_failed) {
runs_ = std::vector<uint64_t>(num_repeats_, 0);
return;
}
}
}
template <class Index>
void DoLookupsExpo(Index& index) {
if (build_) return;
// Atomic counter used to assign work to threads.
std::atomic<std::size_t> cntr(0);
bool run_failed = false;
runs_.resize(num_repeats_);
for (unsigned int i = 0; i < num_repeats_; ++i) {
random_sum_ = 0;
individual_ns_sum_ = 0;
uint64_t ms;
if (num_threads_ == 1) {
if (sep_) {
ms = util::timing([&] {
SepExpoLookupsLoop<Index>(index, 0, lookups_.size(), run_failed);
});
} else if (tail_) {
tail_latencys_.resize(lookups_.size() * sampling_ratio_);
ms = util::timing([&] {
TailExpoLookupsLoop<Index>(index, 0, lookups_.size(), run_failed);
});
} else if (perf_) {
ms = util::timing([&] {
PerfExpoLookupsLoop<Index>(index, 0, lookups_.size(), run_failed);
});
} else {
ms = util::timing([&] {
AvgExpoLookupsLoop<Index>(index, 0, lookups_.size(), run_failed);
});
}
} else {
// Reset atomic counter.
cntr.store(0);
ms = util::timing([&] {
while (true) {
const size_t begin = cntr.fetch_add(batch_size);
if (begin >= lookups_.size()) break;
unsigned int limit = std::min(begin + batch_size, lookups_.size());
AvgExpoLookupsLoop<Index>(index, begin, limit, run_failed);
}
});
}
runs_[i] = ms;
if (run_failed) {
runs_ = std::vector<uint64_t>(num_repeats_, 0);
return;
}
}
}
template <class Index>
void AvgBinLookupsLoop(Index& index, unsigned int start, unsigned int limit,
bool& run_failed) {
SearchBound bound = {};
uint64_t result;
typename std::vector<Row<KeyType>>::iterator iter;
for (unsigned int idx = start; idx < limit; ++idx) {
// Compute the actual index for debugging.
const volatile uint64_t lookup_key = lookups_[idx].key;
const volatile uint64_t expected = lookups_[idx].result;
// not tracking errors, measure the lookup time.
bound = index.EqualityLookup(lookup_key);
iter = std::lower_bound(
data_.begin() + bound.start, data_.begin() + bound.stop, lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
result = 0;
while (iter != data_.end() && iter->key == lookup_key) {
result += iter->data[0];
++iter;
}
if (result != expected) run_failed = true;
}
}
template <class Index>
void TailBinLookupsLoop(Index& index, unsigned int start_idx,
unsigned int limit, bool& run_failed) {
SearchBound bound = {};
uint64_t result;
typename std::vector<Row<KeyType>>::iterator iter;
auto start = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
size_t sampling_interval = limit / (limit * sampling_ratio_);
for (unsigned int idx = start_idx; idx < limit; ++idx) {
if (idx % sampling_interval == 0) {
start = std::chrono::high_resolution_clock::now();
}
const volatile uint64_t lookup_key = lookups_[idx].key;
const volatile uint64_t expected = lookups_[idx].result;
bound = index.EqualityLookup(lookup_key);
iter = std::lower_bound(
data_.begin() + bound.start, data_.begin() + bound.stop, lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
result = 0;
while (iter != data_.end() && iter->key == lookup_key) {
result += iter->data[0];
++iter;
}
if (result != expected) run_failed = true;
if (idx % sampling_interval == 0) {
end = std::chrono::high_resolution_clock::now();
tail_latencys_.push_back(
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start)
.count());
}
}
}
template <class Index>
void SepBinLookupsLoop(Index& index, unsigned int start_idx,
unsigned int limit, bool& run_failed) {
SearchBound bound = {};
uint64_t result;
typename std::vector<Row<KeyType>>::iterator iter;
auto start = std::chrono::high_resolution_clock::now();
auto mid = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
auto sum_total =
std::chrono::duration_cast<std::chrono::nanoseconds>(start - start);
auto sum_pred =
std::chrono::duration_cast<std::chrono::nanoseconds>(mid - mid);
auto sum_corr =
std::chrono::duration_cast<std::chrono::nanoseconds>(end - end);
for (unsigned int idx = start_idx; idx < limit; ++idx) {
start = std::chrono::high_resolution_clock::now();
const volatile uint64_t lookup_key = lookups_[idx].key;
const volatile uint64_t expected = lookups_[idx].result;
bound = index.EqualityLookup(lookup_key);
mid = std::chrono::high_resolution_clock::now();
iter = std::lower_bound(
data_.begin() + bound.start, data_.begin() + bound.stop, lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
result = 0;
while (iter != data_.end() && iter->key == lookup_key) {
result += iter->data[0];
++iter;
}
if (result != expected) {
run_failed = true;
}
end = std::chrono::high_resolution_clock::now();
sum_total +=
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
sum_pred +=
std::chrono::duration_cast<std::chrono::nanoseconds>(mid - start);
sum_corr +=
std::chrono::duration_cast<std::chrono::nanoseconds>(end - mid);
}
avg_total_ = static_cast<double>(sum_total.count()) / lookups_.size();
avg_pred_ = static_cast<double>(sum_pred.count()) / lookups_.size();
avg_corr_ = static_cast<double>(sum_corr.count()) / lookups_.size();
}
template <class Index>
void PerfBinLookupsLoop(Index& index, unsigned int start, unsigned int limit,
bool& run_failed) {
SearchBound bound = {};
uint64_t result;
typename std::vector<Row<KeyType>>::iterator iter;
int pred_fd = setup_perf_event(perf_no_);
int corr_fd = setup_perf_event(perf_no_);
for (unsigned int idx = start; idx < limit; ++idx) {
enable_perf_event(pred_fd);
const volatile uint64_t lookup_key = lookups_[idx].key;
const volatile uint64_t expected = lookups_[idx].result;
bound = index.EqualityLookup(lookup_key);
disable_perf_event(pred_fd);
enable_perf_event(corr_fd);
iter = std::lower_bound(
data_.begin() + bound.start, data_.begin() + bound.stop, lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
result = 0;
while (iter != data_.end() && iter->key == lookup_key) {
result += iter->data[0];
++iter;
}
if (result != expected) run_failed = true;
disable_perf_event(corr_fd);
}
pred_perf_ = close_perf_event(pred_fd);
corr_perf_ = close_perf_event(corr_fd);
}
template <class Index>
void AvgExpoLookupsLoop(Index& index, unsigned int start, unsigned int limit,
bool& run_failed) {
SearchBound bound = {};
uint64_t result;
typename std::vector<Row<KeyType>>::iterator iter;
for (unsigned int idx = start; idx < limit; ++idx) {
// Compute the actual index for debugging.
const volatile uint64_t lookup_key = lookups_[idx].key;
const volatile uint64_t expected = lookups_[idx].result;
// not tracking errors, measure the lookup time.
bound = index.EqualityLookup(lookup_key);
auto pred = data_.begin() + bound.start;
size_t b = 1;
if ((*pred).key < lookup_key) { // search right side
auto curr = pred + b;
while ((curr < data_.end()) && ((*curr).key < lookup_key)) {
b *= 2;
pred = curr;
curr += b;
}
iter = std::lower_bound(
pred, std::min(curr + 1, data_.end()), lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
} else { // search left side
auto curr = pred - b;
while ((curr > data_.begin()) && ((*curr).key > lookup_key)) {
b *= 2;
pred = curr;
curr -= b;
}
iter = std::lower_bound(
std::max(data_.begin(), curr), pred, lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
}
result = 0;
while (iter != data_.end() && iter->key == lookup_key) {
result += iter->data[0];
++iter;
}
if (result != expected) run_failed = true;
}
}
template <class Index>
void TailExpoLookupsLoop(Index& index, unsigned int start_idx,
unsigned int limit, bool& run_failed) {
SearchBound bound = {};
uint64_t result;
typename std::vector<Row<KeyType>>::iterator iter;
auto start = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
size_t sampling_interval = limit / (limit * sampling_ratio_);
for (unsigned int idx = start_idx; idx < limit; ++idx) {
if (idx % sampling_interval == 0) {
start = std::chrono::high_resolution_clock::now();
}
// Compute the actual index for debugging.
const volatile uint64_t lookup_key = lookups_[idx].key;
const volatile uint64_t expected = lookups_[idx].result;
// not tracking errors, measure the lookup time.
bound = index.EqualityLookup(lookup_key);
auto pred = data_.begin() + bound.start;
size_t b = 1;
if ((*pred).key < lookup_key) { // search right side
auto curr = pred + b;
while ((curr < data_.end()) && ((*curr).key < lookup_key)) {
b *= 2;
pred = curr;
curr += b;
}
iter = std::lower_bound(
pred, std::min(curr + 1, data_.end()), lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
} else { // search left side
auto curr = pred - b;
while ((curr > data_.begin()) && ((*curr).key > lookup_key)) {
b *= 2;
pred = curr;
curr -= b;
}
iter = std::lower_bound(
std::max(data_.begin(), curr), pred, lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
}
result = 0;
while (iter != data_.end() && iter->key == lookup_key) {
result += iter->data[0];
++iter;
}
if (result != expected) run_failed = true;
if (idx % sampling_interval == 0) {
end = std::chrono::high_resolution_clock::now();
tail_latencys_.push_back(
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start)
.count());
}
}
}
template <class Index>
void SepExpoLookupsLoop(Index& index, unsigned int start_idx,
unsigned int limit, bool& run_failed) {
SearchBound bound = {};
uint64_t result;
typename std::vector<Row<KeyType>>::iterator iter;
auto start = std::chrono::high_resolution_clock::now();
auto mid = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
auto sum_total =
std::chrono::duration_cast<std::chrono::nanoseconds>(start - start);
auto sum_pred =
std::chrono::duration_cast<std::chrono::nanoseconds>(mid - mid);
auto sum_corr =
std::chrono::duration_cast<std::chrono::nanoseconds>(end - end);
for (unsigned int idx = start_idx; idx < limit; ++idx) {
start = std::chrono::high_resolution_clock::now();
const volatile uint64_t lookup_key = lookups_[idx].key;
const volatile uint64_t expected = lookups_[idx].result;
bound = index.EqualityLookup(lookup_key);
mid = std::chrono::high_resolution_clock::now();
auto pred = data_.begin() + bound.start;
size_t b = 1;
if ((*pred).key < lookup_key) { // search right side
auto curr = pred + b;
while ((curr < data_.end()) && ((*curr).key < lookup_key)) {
b *= 2;
pred = curr;
curr += b;
}
iter = std::lower_bound(
pred, std::min(curr + 1, data_.end()), lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
} else { // search left side
auto curr = pred - b;
while ((curr > data_.begin()) && ((*curr).key > lookup_key)) {
b *= 2;
pred = curr;
curr -= b;
}
iter = std::lower_bound(
std::max(data_.begin(), curr), pred, lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
}
result = 0;
while (iter != data_.end() && iter->key == lookup_key) {
result += iter->data[0];
++iter;
}
if (result != expected) run_failed = true;
end = std::chrono::high_resolution_clock::now();
sum_total +=
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
sum_pred +=
std::chrono::duration_cast<std::chrono::nanoseconds>(mid - start);
sum_corr +=
std::chrono::duration_cast<std::chrono::nanoseconds>(end - mid);
}
avg_total_ = static_cast<double>(sum_total.count()) / lookups_.size();
avg_pred_ = static_cast<double>(sum_pred.count()) / lookups_.size();
avg_corr_ = static_cast<double>(sum_corr.count()) / lookups_.size();
}
template <class Index>
void PerfExpoLookupsLoop(Index& index, unsigned int start, unsigned int limit,
bool& run_failed) {
SearchBound bound = {};
uint64_t result;
typename std::vector<Row<KeyType>>::iterator iter;
int pred_fd = setup_perf_event(perf_no_);
int corr_fd = setup_perf_event(perf_no_);
for (unsigned int idx = start; idx < limit; ++idx) {
enable_perf_event(pred_fd);
const volatile uint64_t lookup_key = lookups_[idx].key;
const volatile uint64_t expected = lookups_[idx].result;
bound = index.EqualityLookup(lookup_key);
disable_perf_event(pred_fd);
enable_perf_event(corr_fd);
auto pred = data_.begin() + bound.start;
size_t b = 1;
if ((*pred).key < lookup_key) { // search right side
auto curr = pred + b;
while ((curr < data_.end()) && ((*curr).key < lookup_key)) {
b *= 2;
pred = curr;
curr += b;
}
iter = std::lower_bound(
pred, std::min(curr + 1, data_.end()), lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
} else { // search left side
auto curr = pred - b;
while ((curr > data_.begin()) && ((*curr).key > lookup_key)) {
b *= 2;
pred = curr;
curr -= b;
}
iter = std::lower_bound(
std::max(data_.begin(), curr), pred, lookup_key,
[](const Row<KeyType>& lhs, const KeyType lookup_key) {
return lhs.key < lookup_key;
});
}
result = 0;
while (iter != data_.end() && iter->key == lookup_key) {
result += iter->data[0];
++iter;
}
if (result != expected) run_failed = true;
disable_perf_event(corr_fd);
}
pred_perf_ = close_perf_event(pred_fd);
corr_perf_ = close_perf_event(corr_fd);
}
template <class Index>
void PrintResult(Index& index) {
std::string result_str = "";
if (build_) {
result_str = "build_only";
} else if (tail_) {
std::sort(tail_latencys_.begin(), tail_latencys_.end());
result_str =
std::to_string(tail_latencys_[0]) + "," +
std::to_string(tail_latencys_[0.5 * tail_latencys_.size()]) + "," +
std::to_string(tail_latencys_[0.9 * tail_latencys_.size()]) + "," +
std::to_string(tail_latencys_[0.99 * tail_latencys_.size()]) + "," +
std::to_string(tail_latencys_[0.999 * tail_latencys_.size()]) + "," +
std::to_string(tail_latencys_[0.9999 * tail_latencys_.size()]) + "," +
std::to_string(tail_latencys_[tail_latencys_.size() - 1]);
} else if (sep_) {
result_str = std::to_string(avg_total_) + "," +
std::to_string(avg_pred_) + "," + std::to_string(avg_corr_);
} else if (perf_) {
std::string perf_name = "";
if (perf_no_ == 0){
perf_name = ",LLC miss";
} else if (perf_no_ == 1){
perf_name = ",TLB miss";
} else if (perf_no_ == 2){
perf_name = ",Branch miss";
} else if (perf_no_ == 3){
perf_name = ",Instructions";
}
result_str += std::to_string(pred_perf_) + ","
+ std::to_string(corr_perf_) + perf_name;
} else {
// compute median time
std::vector<double> times;
double median_time;
for (unsigned int i = 0; i < runs_.size(); ++i) {
const double ns_per_lookup =
static_cast<double>(runs_[i]) / lookups_.size();
times.push_back(ns_per_lookup);
}
std::sort(times.begin(), times.end());
if (times.size() % 2 == 0) {
median_time =
0.5 * (times[times.size() / 2 - 1] + times[times.size() / 2]);
} else {
median_time = times[times.size() / 2];
}
result_str = std::to_string(median_time);
}
// don't print a line if (the first) run failed
if (!build_ && runs_[0] == 0) result_str = "fail";
std::string variant_str = index.variant();
std::cout << "RESULT: " << index.name() << "," << variant_str << ","
<< index.size() << "," << build_ns_ << "," << result_str
<< std::endl;
if (csv_) PrintResultCSV(index, result_str, variant_str);
}
template <class Index>
void PrintResultCSV(Index& index, std::string result_str, std::string variant_str) {
std::string filename = "";
if (sep_) {
filename = "./results/" + dataset_name_ + "_results_sep";
} else if (build_) {
filename = "./results/" + dataset_name_ + "_results_build";
} else if (tail_) {
filename = "./results/" + dataset_name_ + "_results_tail";
} else if (perf_) {
filename = "./results/" + dataset_name_ + "_results_perf";
} else {
filename = "./results/" + dataset_name_ + "_results_table";
}
if (csv_suffix_ != "") {
filename = filename + "_" + csv_suffix_ + ".csv";
} else {
filename = filename + ".csv";
}
bool is_new = false;
// 파일이 존재하는지 확인
std::ifstream fin(filename);
if (!fin.is_open()) {
is_new = true;
fin.close();
}
std::ofstream fout(filename, std::ofstream::out | std::ofstream::app);
if (!fout.is_open()) {
std::cerr << "Failure to print CSV on " << filename << std::endl;
return;
}
if (is_new){
std::string header;
if (sep_) {
header = "index,options,space,build,total,predict,correct,dataset";
} else if (build_) {
header = "index,options,space,build,average,dataset";
} else if (tail_) {
header = "index,options,space,build,0.0,0.5,0.9,0.99,0.999,0.9999,1.0,dataset";
} else if (perf_) {
header = "index,options,space,build,perf_predict,perf_correct,perf type,dataset";
} else {
header = "index,options,space,build,average,dataset";
}
fout << header << std::endl;
}
if (!build_ && runs_[0] == 0)
std::string index_name = index_name + "[FAIL]";
fout << index.name() << "," << variant_str << "," << index.size() << ","
<< build_ns_ << "," << result_str << "," << dataset_name_ << std::endl;
fout.close();
return;
}
uint64_t random_sum_ = 0;
uint64_t individual_ns_sum_ = 0;
const std::string data_filename_;
const std::string lookups_filename_;
std::string dataset_name_;
std::vector<Row<KeyType>> data_;
std::vector<KeyValue<KeyType>> index_data_;
bool unique_keys_;
std::vector<EqualLookup<KeyType>> lookups_;
uint64_t build_ns_;
double log_sum_search_bound_;
double l1_sum_search_bound_;
double l2_sum_search_bound_;
double avg_total_;
double avg_pred_;
double avg_corr_;
// Run times.
std::vector<uint64_t> runs_;
// Number of times we repeat the lookup code.
size_t num_repeats_;
// Used to only print profiling header information for first run.
bool first_run_;
bool perf_;
bool build_;
bool measure_each_;
bool csv_;
bool sep_;
bool tail_;
double sampling_ratio_;
int perf_no_;
// Number of lookup threads.
const size_t num_threads_;
std::vector<double> tail_latencys_;
long long pred_perf_ = 0;
long long corr_perf_ = 0;
std::vector<uint64_t> memory_; // Some memory we can read to flush the cache
SearchClass<KeyType> searcher_;
std::string csv_suffix_;
};
} // namespace sosd