guanaqo 1.0.0-alpha.27
Utilities for scientific software
Loading...
Searching...
No Matches
counters.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @ingroup trace_pcm
5/// Performance counter snapshots and scoped collectors.
6
7#include <guanaqo/export.h>
8#include <cstdint>
9#include <memory>
10#include <utility>
11
12namespace guanaqo::pcm {
13
14// Accumulated performance counters
15/// @ingroup trace_pcm
17 // Basic counters
18 uint64_t instructions = 0;
19 uint64_t cycles = 0;
20 uint64_t ref_cycles = 0;
21
22 // Cache counters
23 uint64_t l2_misses = 0;
24 uint64_t l2_hits = 0;
25 uint64_t l3_misses = 0;
26
27 // Branch prediction
28 uint64_t branch_misses = 0;
29
30 // Total slots
31 uint64_t all_slots = 0;
32
33 // Top-Down Microarchitecture Analysis (TMA) Level 1
35 uint64_t backend_bound_slots = 0;
37 uint64_t retiring_slots = 0;
38
39 // Top-Down Microarchitecture Analysis (TMA) Level 2
40 uint64_t mem_bound_slots = 0; // Subset of backend_bound
41 uint64_t fetch_lat_slots = 0; // Subset of frontend_bound
42};
43
44namespace detail {
45
46GUANAQO_EXPORT struct ScopedCounters {
47 virtual ~ScopedCounters() = default;
48 virtual ThreadPerfCounters &get() = 0;
49 virtual ThreadPerfCounters &stop() = 0;
50};
51
52} // namespace detail
53
54/// @addtogroup trace_pcm
55/// @{
56
57/// May return null if PCM is not available.
58GUANAQO_EXPORT std::unique_ptr<detail::ScopedCounters> start_counters();
59/// Disables performance counters globally. Blocks until all active counters have stopped.
60GUANAQO_EXPORT void disable_counters();
61/// Enables performance counters globally.
62GUANAQO_EXPORT void enable_counters();
63
64/// Pins the thread to the current CPU and records a snapshot of performance events.
65/// When stop is called or when the object is destroyed, another snapshot is taken and the
66/// counters for the thread are updated. The updated counters are passed to the provided function,
67/// which can be used for logging Perfetto counters or for other purposes.
68template <class F>
69GUANAQO_EXPORT struct ScopedCounters {
71 std::unique_ptr<detail::ScopedCounters> impl;
72
74 : func(std::forward<F>(func)), impl(start_counters()) {}
75 ScopedCounters(const ScopedCounters &) = delete;
78 ThreadPerfCounters *get() { return impl ? &impl->get() : nullptr; }
79 void stop() {
80 if (impl) {
81 func(impl->stop());
82 impl.reset();
83 }
84 }
85};
86
87template <class F>
89template <class F>
91
92/// @}
93
94} // namespace guanaqo::pcm
void disable_counters()
Disables performance counters globally. Blocks until all active counters have stopped.
Definition counters.cpp:389
ScopedCounters(F &&) -> ScopedCounters< F >
std::unique_ptr< detail::ScopedCounters > start_counters()
May return null if PCM is not available.
Definition counters.cpp:398
void enable_counters()
Enables performance counters globally.
Definition counters.cpp:394
Pins the thread to the current CPU and records a snapshot of performance events.
Definition counters.hpp:69
ScopedCounters(const ScopedCounters &)=delete
std::unique_ptr< detail::ScopedCounters > impl
Definition counters.hpp:71
ThreadPerfCounters * get()
Definition counters.hpp:78
ScopedCounters & operator=(const ScopedCounters &)=delete
virtual ThreadPerfCounters & stop()=0
virtual ThreadPerfCounters & get()=0