Debian Codegraph @

benchmarks/event_counter.h

raw
1 #ifndef __EVENT_COUNTER_H
2 #define __EVENT_COUNTER_H
3
4 #include <cctype>
5 #ifndef _MSC_VER
6 #include <dirent.h>
7 #endif
8 #include <cinttypes>
9
10 #include <cstring>
11
12 #include <chrono>
13 #include <vector>
14
15 #include "linux-perf-events.h"
16 #ifdef __linux__
17 #include <libgen.h>
18 #endif
19
20 #if (defined(__APPLE__) && __APPLE__) && (defined(__aarch64__) && __aarch64__)
21 #include "apple_arm_events.h"
22 #endif
23
24 struct event_count {
25 std::chrono::duration<double> elapsed;
26 std::vector<unsigned long long> event_counts;
27
28 event_count() : elapsed(0), event_counts{0, 0, 0, 0, 0} {}
29
30 event_count(const std::chrono::duration<double> _elapsed,
31 const std::vector<unsigned long long> _event_counts)
32 : elapsed(_elapsed), event_counts(_event_counts) {}
33
34 event_count(const event_count &other)
35 : elapsed(other.elapsed), event_counts(other.event_counts) {}
36
37 // The types of counters (so we can read the getter more easily)
38 enum event_counter_types {
39 CPU_CYCLES = 0,
40 INSTRUCTIONS = 1,
41 BRANCHES = 2,
42 MISSED_BRANCHES = 3
43 };
44
45 double elapsed_sec() const {
46 return std::chrono::duration<double>(elapsed).count();
47 }
48
49 double elapsed_ns() const {
50 return std::chrono::duration<double, std::nano>(elapsed).count();
51 }
52
53 double cycles() const {
54 return static_cast<double>(event_counts[CPU_CYCLES]);
55 }
56
57 double instructions() const {
58 return static_cast<double>(event_counts[INSTRUCTIONS]);
59 }
60
61 double branches() const {
62 return static_cast<double>(event_counts[BRANCHES]);
63 }
64
65 double missed_branches() const {
66 return static_cast<double>(event_counts[MISSED_BRANCHES]);
67 }
68
69 event_count &operator=(const event_count &other) {
70 this->elapsed = other.elapsed;
71 this->event_counts = other.event_counts;
72 return *this;
73 }
74
75 event_count operator+(const event_count &other) const {
76 return event_count(elapsed + other.elapsed,
77 {
78 event_counts[0] + other.event_counts[0],
79 event_counts[1] + other.event_counts[1],
80 event_counts[2] + other.event_counts[2],
81 event_counts[3] + other.event_counts[3],
82 event_counts[4] + other.event_counts[4],
83 });
84 }
85
86 void operator+=(const event_count &other) { *this = *this + other; }
87 };
88
89 struct event_aggregate {
90 bool has_events = false;
91 int iterations = 0;
92 event_count total{};
93 event_count best{};
94 event_count worst{};
95
96 event_aggregate() = default;
97
98 void operator<<(const event_count &other) {
99 if (iterations == 0 || other.elapsed < best.elapsed) {
100 best = other;
101 }
102 if (iterations == 0 || other.elapsed > worst.elapsed) {
103 worst = other;
104 }
105 iterations++;
106 total += other;
107 }
108
109 double elapsed_sec() const { return total.elapsed_sec() / iterations; }
110
111 double elapsed_ns() const { return total.elapsed_ns() / iterations; }
112
113 double cycles() const { return total.cycles() / iterations; }
114
115 double instructions() const { return total.instructions() / iterations; }
116
117 double branches() const { return total.branches() / iterations; }
118
119 double missed_branches() const {
120 return total.missed_branches() / iterations;
121 }
122 };
123
124 struct event_collector {
125 event_count count{};
126 std::chrono::time_point<std::chrono::steady_clock> start_clock{};
127
128 #if defined(__linux__)
129 LinuxEvents<PERF_TYPE_HARDWARE> linux_events;
130
131 event_collector()
132 : linux_events(std::vector<int>{
133 PERF_COUNT_HW_CPU_CYCLES, PERF_COUNT_HW_INSTRUCTIONS,
134 PERF_COUNT_HW_BRANCH_INSTRUCTIONS, // Retired branch instructions
135 PERF_COUNT_HW_BRANCH_MISSES}) {}
136
137 bool has_events() { return linux_events.is_working(); }
138 #elif __APPLE__ && __aarch64__
139 performance_counters diff;
140
141 event_collector() : diff(0) { setup_performance_counters(); }
142
143 bool has_events() { return setup_performance_counters(); }
144 #else
145 event_collector() {}
146
147 bool has_events() { return false; }
148 #endif
149
150 inline void start() {
151 #if defined(__linux)
152 linux_events.start();
153 #elif __APPLE__ && __aarch64__
154 if (has_events()) {
155 diff = get_counters();
156 }
157 #endif
158 start_clock = std::chrono::steady_clock::now();
159 }
160
161 inline event_count &end() {
162 const auto end_clock = std::chrono::steady_clock::now();
163 #if defined(__linux)
164 linux_events.end(count.event_counts);
165 #elif __APPLE__ && __aarch64__
166 if (has_events()) {
167 performance_counters end = get_counters();
168 diff = end - diff;
169 }
170 count.event_counts[0] = diff.cycles;
171 count.event_counts[1] = diff.instructions;
172 count.event_counts[2] = diff.branches;
173 count.event_counts[3] = diff.missed_branches;
174 count.event_counts[4] = 0;
175 #endif
176 count.elapsed = end_clock - start_clock;
177 return count;
178 }
179 };
180
181 #endif