Ada
4.1.1
Fast spec-compliant URL parser
Toggle main menu visibility
Loading...
Searching...
No Matches
event_counter.h
Go to the documentation of this file.
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 <chrono>
9
#include <cinttypes>
10
#include <cstring>
11
#include <vector>
12
13
#include "
linux_perf_events.h
"
14
#ifdef __linux__
15
#include <libgen.h>
16
#endif
17
18
#if __APPLE__ && __aarch64__
19
#include "
apple_arm_events.h
"
20
#endif
21
22
struct
event_count
{
23
std::chrono::duration<double>
elapsed
;
24
std::vector<unsigned long long>
event_counts
;
25
event_count
() :
elapsed
(0),
event_counts
{0, 0, 0, 0, 0} {}
26
event_count
(
const
std::chrono::duration<double> _elapsed,
27
const
std::vector<unsigned long long> _event_counts)
28
:
elapsed
(_elapsed),
event_counts
(_event_counts) {}
29
event_count
(
const
event_count
& other)
30
:
elapsed
(other.
elapsed
),
event_counts
(other.
event_counts
) {}
31
32
// The types of counters (so we can read the getter more easily)
33
enum
event_counter_types
{
34
CPU_CYCLES
,
35
INSTRUCTIONS
,
36
};
37
38
double
elapsed_sec
()
const
{
39
return
std::chrono::duration<double>(
elapsed
).count();
40
}
41
double
elapsed_ns
()
const
{
42
return
std::chrono::duration<double, std::nano>(
elapsed
).count();
43
}
44
double
cycles
()
const
{
45
return
static_cast<
double
>
(
event_counts
[
CPU_CYCLES
]);
46
}
47
double
instructions
()
const
{
48
return
static_cast<
double
>
(
event_counts
[
INSTRUCTIONS
]);
49
}
50
51
event_count
&
operator=
(
const
event_count
& other) {
52
this->elapsed = other.
elapsed
;
53
this->event_counts = other.
event_counts
;
54
return
*
this
;
55
}
56
event_count
operator+
(
const
event_count
& other)
const
{
57
return
event_count
(
elapsed
+ other.
elapsed
,
58
{
59
event_counts[0] + other.event_counts[0],
60
event_counts[1] + other.event_counts[1],
61
event_counts[2] + other.event_counts[2],
62
event_counts[3] + other.event_counts[3],
63
event_counts[4] + other.event_counts[4],
64
});
65
}
66
67
void
operator+=
(
const
event_count
& other) { *
this
= *
this
+ other; }
68
};
69
70
struct
event_aggregate
{
71
bool
has_events
=
false
;
72
int
iterations
= 0;
73
event_count
total
{};
74
event_count
best
{};
75
event_count
worst
{};
76
77
event_aggregate
() =
default
;
78
79
void
operator<<
(
const
event_count
& other) {
80
if
(
iterations
== 0 || other.
elapsed
<
best
.elapsed) {
81
best
= other;
82
}
83
if
(
iterations
== 0 || other.
elapsed
>
worst
.elapsed) {
84
worst
= other;
85
}
86
iterations
++;
87
total
+= other;
88
}
89
90
double
elapsed_sec
()
const
{
return
total
.elapsed_sec() /
iterations
; }
91
double
elapsed_ns
()
const
{
return
total
.elapsed_ns() /
iterations
; }
92
double
cycles
()
const
{
return
total
.cycles() /
iterations
; }
93
double
instructions
()
const
{
return
total
.instructions() /
iterations
; }
94
};
95
96
struct
event_collector
{
97
event_count
count
{};
98
std::chrono::time_point<std::chrono::steady_clock>
start_clock
{};
99
100
#if defined(__linux__)
101
LinuxEvents<PERF_TYPE_HARDWARE> linux_events;
102
event_collector
()
103
: linux_events(std::vector<int>{
104
PERF_COUNT_HW_CPU_CYCLES,
105
PERF_COUNT_HW_INSTRUCTIONS,
106
}) {}
107
bool
has_events
() {
return
linux_events.is_working(); }
108
#elif __APPLE__ && __aarch64__
109
performance_counters diff;
110
event_collector
() : diff(0) {
setup_performance_counters
(); }
111
bool
has_events
() {
return
setup_performance_counters
(); }
112
#else
113
event_collector
() {}
114
bool
has_events
() {
return
false
; }
115
#endif
116
117
inline
void
start
() {
118
#if defined(__linux)
119
linux_events.start();
120
#elif __APPLE__ && __aarch64__
121
if
(
has_events
()) {
122
diff =
get_counters
();
123
}
124
#endif
125
start_clock
= std::chrono::steady_clock::now();
126
}
127
inline
event_count
&
end
() {
128
const
auto
end_clock = std::chrono::steady_clock::now();
129
#if defined(__linux)
130
linux_events.end(
count
.event_counts);
131
#elif __APPLE__ && __aarch64__
132
if
(
has_events
()) {
133
performance_counters
end
=
get_counters
();
134
diff =
end
- diff;
135
}
136
count
.event_counts[0] = diff.
cycles
;
137
count
.event_counts[1] = diff.
instructions
;
138
count
.event_counts[2] = diff.
missed_branches
;
139
count
.event_counts[3] = 0;
140
count
.event_counts[4] = diff.
branches
;
141
#endif
142
count
.elapsed = end_clock -
start_clock
;
143
return
count
;
144
}
145
};
146
147
#endif
apple_arm_events.h
setup_performance_counters
bool setup_performance_counters()
Definition
apple_arm_events.h:967
get_counters
performance_counters get_counters()
Definition
apple_arm_events.h:1081
linux_perf_events.h
event_aggregate::elapsed_sec
double elapsed_sec() const
Definition
event_counter.h:90
event_aggregate::instructions
double instructions() const
Definition
event_counter.h:93
event_aggregate::elapsed_ns
double elapsed_ns() const
Definition
event_counter.h:91
event_aggregate::has_events
bool has_events
Definition
event_counter.h:71
event_aggregate::total
event_count total
Definition
event_counter.h:73
event_aggregate::cycles
double cycles() const
Definition
event_counter.h:92
event_aggregate::best
event_count best
Definition
event_counter.h:74
event_aggregate::iterations
int iterations
Definition
event_counter.h:72
event_aggregate::worst
event_count worst
Definition
event_counter.h:75
event_aggregate::operator<<
void operator<<(const event_count &other)
Definition
event_counter.h:79
event_aggregate::event_aggregate
event_aggregate()=default
event_collector::count
event_count count
Definition
event_counter.h:97
event_collector::start_clock
std::chrono::time_point< std::chrono::steady_clock > start_clock
Definition
event_counter.h:98
event_collector::start
void start()
Definition
event_counter.h:117
event_collector::end
event_count & end()
Definition
event_counter.h:127
event_collector::event_collector
event_collector()
Definition
event_counter.h:113
event_collector::has_events
bool has_events()
Definition
event_counter.h:114
event_count
Definition
event_counter.h:22
event_count::instructions
double instructions() const
Definition
event_counter.h:47
event_count::elapsed
std::chrono::duration< double > elapsed
Definition
event_counter.h:23
event_count::cycles
double cycles() const
Definition
event_counter.h:44
event_count::elapsed_sec
double elapsed_sec() const
Definition
event_counter.h:38
event_count::operator+
event_count operator+(const event_count &other) const
Definition
event_counter.h:56
event_count::elapsed_ns
double elapsed_ns() const
Definition
event_counter.h:41
event_count::event_count
event_count(const event_count &other)
Definition
event_counter.h:29
event_count::event_counts
std::vector< unsigned long long > event_counts
Definition
event_counter.h:24
event_count::event_counter_types
event_counter_types
Definition
event_counter.h:33
event_count::INSTRUCTIONS
@ INSTRUCTIONS
Definition
event_counter.h:35
event_count::CPU_CYCLES
@ CPU_CYCLES
Definition
event_counter.h:34
event_count::event_count
event_count(const std::chrono::duration< double > _elapsed, const std::vector< unsigned long long > _event_counts)
Definition
event_counter.h:26
event_count::operator=
event_count & operator=(const event_count &other)
Definition
event_counter.h:51
event_count::event_count
event_count()
Definition
event_counter.h:25
event_count::operator+=
void operator+=(const event_count &other)
Definition
event_counter.h:67
performance_counters
Definition
apple_arm_events.h:50
performance_counters::missed_branches
double missed_branches
Definition
apple_arm_events.h:53
performance_counters::instructions
double instructions
Definition
apple_arm_events.h:54
performance_counters::branches
double branches
Definition
apple_arm_events.h:52
performance_counters::cycles
double cycles
Definition
apple_arm_events.h:51