Ada 4.1.1
Fast spec-compliant URL parser
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
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) {}
30 : elapsed(other.elapsed), event_counts(other.event_counts) {}
31
32 // The types of counters (so we can read the getter more easily)
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
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
71 bool has_events = false;
72 int iterations = 0;
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
98 std::chrono::time_point<std::chrono::steady_clock> start_clock{};
99
100#if defined(__linux__)
101 LinuxEvents<PERF_TYPE_HARDWARE> linux_events;
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;
111 bool has_events() { return setup_performance_counters(); }
112#else
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()) {
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
bool setup_performance_counters()
performance_counters get_counters()
double elapsed_sec() const
double instructions() const
double elapsed_ns() const
event_count total
double cycles() const
event_count best
event_count worst
void operator<<(const event_count &other)
event_aggregate()=default
event_count count
std::chrono::time_point< std::chrono::steady_clock > start_clock
event_count & end()
double instructions() const
std::chrono::duration< double > elapsed
double cycles() const
double elapsed_sec() const
event_count operator+(const event_count &other) const
double elapsed_ns() const
event_count(const event_count &other)
std::vector< unsigned long long > event_counts
event_count(const std::chrono::duration< double > _elapsed, const std::vector< unsigned long long > _event_counts)
event_count & operator=(const event_count &other)
void operator+=(const event_count &other)