-
Notifications
You must be signed in to change notification settings - Fork 4
/
branch_sampling.cpp
159 lines (139 loc) · 4.95 KB
/
branch_sampling.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
#include "access_benchmark.h"
#include <iostream>
#include <perfcpp/sampler.h>
/**
* A function using multiple branches hard to optimize for the compiler for
* demonstrating branch-sampling.
*
* @param cache_line Cache line to use as an input.
* @return Another value through a handful of branches.
*/
[[nodiscard]] std::uint64_t
branchy_function(const perf::example::AccessBenchmark::cache_line& cache_line);
int
main()
{
std::cout << "libperf-cpp example: Record perf branch samples for "
"single-threaded sequential access to an in-memory array."
<< std::endl;
/// Initialize counter definitions.
/// Note that the perf::CounterDefinition holds all counter names and must be
/// alive until the benchmark finishes.
auto counter_definitions = perf::CounterDefinition{};
/// Initialize sampler.
auto perf_config = perf::SampleConfig{};
perf_config.period(1000000U); /// Record every 1,000,000th event.
auto sampler = perf::Sampler{ counter_definitions, perf_config };
/// Setup which counters trigger the writing of samples.
sampler.trigger("cycles", perf::Precision::AllowArbitrarySkid);
/// Setup which data will be included into samples (timestamp and stack of branches).
sampler.values().time(true).branch_stack(
{ perf::BranchType::User, perf::BranchType::Conditional }) /// Only sample conditional branches in user-mode.
;
/// Create random access benchmark.
auto benchmark = perf::example::AccessBenchmark{ /*sequential accesses*/ false,
/* create benchmark of 512 MB */ 512U };
/// Start sampling.
try {
sampler.start();
} catch (std::runtime_error& exception) {
std::cerr << exception.what() << std::endl;
return 1;
}
/// Execute the benchmark (accessing cache lines in a random order).
auto value = 0ULL;
for (auto index = 0U; index < benchmark.size(); ++index) {
value += branchy_function(benchmark[index]);
}
asm volatile(""
: "+r,m"(value)
:
: "memory"); /// We do not want the compiler to optimize away
/// this unused value.
/// Stop sampling.
sampler.stop();
/// Get all the recorded samples.
const auto samples = sampler.result();
/// Print the first samples.
const auto count_show_samples = std::min<std::size_t>(samples.size(), 10U);
std::cout << "\nRecorded " << samples.size() << " samples." << std::endl;
std::cout << "Here are the first " << count_show_samples << " recorded samples:\n" << std::endl;
for (auto index = 0U; index < count_show_samples; ++index) {
const auto& sample = samples[index];
/// Since we recorded the time, period, the instruction pointer, and the CPU
/// id, we can only read these values.
if (sample.time().has_value() && sample.branches().has_value()) {
std::cout << "Time = " << sample.time().value() << "\n";
for (const auto& branch : sample.branches().value()) {
std::cout << "\tpredicted correct = " << branch.is_predicted() << " | from instruction "
<< branch.instruction_pointer_from() << " | to instruction " << branch.instruction_pointer_to()
<< "\n";
}
}
}
std::cout << std::flush;
/// Close the sampler.
/// Note that the sampler can only be closed after reading the samples.
sampler.close();
return 0;
}
std::uint64_t
branchy_function(const perf::example::AccessBenchmark::cache_line& cache_line)
{
auto result = cache_line.value;
for (auto i = 0U; i < 10U; ++i) {
switch ((cache_line.value >> (4U * i)) & 0xF) { // Extract 4 bits at a time
case 0ULL:
result += cache_line.value * (i + 1U);
break;
case 1ULL:
result -= cache_line.value / (i + 2U);
break;
case 2ULL:
result *= cache_line.value + (i * 3U);
break;
case 3ULL:
result /= (cache_line.value - i) | 1U;
break; // Avoid division by zero
case 4ULL:
result ^= cache_line.value << i;
break;
case 5ULL:
result %= (cache_line.value >> i) | 1U;
break;
case 6ULL:
result = ~result;
break;
case 7ULL:
result &= cache_line.value | (std::uint64_t(0xFF) << (i * 8U));
break;
case 8ULL:
result |= cache_line.value & (std::uint64_t(0xFFFF) << (i * 16U));
break;
case 9ULL:
result >>= cache_line.value % (i + 1);
break;
case 10ULL:
result <<= cache_line.value % (i + 2);
break;
case 11ULL:
result += cache_line.value + i * 7;
break;
case 12ULL:
result -= cache_line.value - i * 11;
break;
case 13ULL:
result *= cache_line.value * (i + 5);
break;
case 14ULL:
result /= (cache_line.value / (i + 3)) | 1;
break;
case 15ULL:
result ^= cache_line.value ^ (i * 13);
break;
default:
result = cache_line.value;
}
}
return result;
}