guanaqo 1.0.0-alpha.27
Utilities for scientific software
Loading...
Searching...
No Matches
trace.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @ingroup trace_perfetto
5/// Perfetto tracing macros and session helpers.
6
7#include <guanaqo/export.h>
9
10#if GUANAQO_WITH_PERFETTO
11
13#include <perfetto.h>
14
15#include <filesystem>
16
19 ::perfetto::Category("trace").SetDescription(
20 "General trace events for regions of code"),
21 ::perfetto::Category("gflops").SetDescription(
22 "Counts the total number of floating point operations"),
23 ::perfetto::Category("pcm").SetDescription(
24 "Performance counters for CPU metrics"));
25
26namespace guanaqo {
27
29
30namespace trace {
31
32namespace fs = std::filesystem;
33
34/// @addtogroup trace_perfetto
35/// @{
36
37/// Initialize Perfetto for in-process tracing and register guanaqo's track event categories.
38GUANAQO_EXPORT void initialize_tracing();
39/// Start a new tracing session with the specified buffer size in KiB.
40GUANAQO_EXPORT std::unique_ptr<::perfetto::TracingSession>
41start_tracing(uint32_t memory_kb = 128 * 1024);
42/// Stop the tracing session and discard the trace data.
43GUANAQO_EXPORT void
44abort_tracing(std::unique_ptr<::perfetto::TracingSession> tracing_session);
45/// Stop the tracing session and write the trace data to the specified output file.
46GUANAQO_EXPORT void
47stop_tracing(std::unique_ptr<::perfetto::TracingSession> tracing_session,
48 const fs::path &output_path);
49
50/// @}
51
52namespace detail {
53inline ::perfetto::base::PlatformThreadId get_thread_id_fast() {
54 static thread_local auto thread_id = ::perfetto::base::GetThreadId();
55 return thread_id;
56}
57inline auto get_thread_track() {
58 return ::perfetto::ThreadTrack::ForThread(get_thread_id_fast());
59}
60} // namespace detail
61
62/// Get a reference to the thread-local GFLOP counter.
63/// @ingroup trace_perfetto
64GUANAQO_EXPORT uint64_t &get_thread_gflop_count();
65
66#if GUANAQO_WITH_PCM_TRACING
67/// @ingroup trace_perfetto
68GUANAQO_EXPORT struct ScopedLinalgCounters {
69 ::perfetto::ThreadTrack parent_track = detail::get_thread_track();
70 std::unique_ptr<pcm::detail::ScopedCounters> impl = pcm::start_counters();
71
72 ScopedLinalgCounters() { trace_gflops(impl ? &impl->get() : nullptr); }
75 ~ScopedLinalgCounters() { trace_gflops(impl ? &impl->stop() : nullptr); }
76
77 void trace_gflops(pcm::ThreadPerfCounters *ctr) const;
78};
79#else
80/// @ingroup trace_perfetto
81GUANAQO_EXPORT struct ScopedLinalgCounters {
82 ::perfetto::ThreadTrack parent_track = detail::get_thread_track();
83
88
89 void trace_gflops() const {
90 TRACE_COUNTER("gflops",
91 ::perfetto::CounterTrack("gflops", parent_track),
93 }
94};
95#endif
96
97} // namespace trace
98} // namespace guanaqo
99
100#define GUANAQO_TRACE_LINALG_PRIVATE(name, gflops, uid) \
101 ::guanaqo::trace::ScopedLinalgCounters GUANAQO_CONCATENATE_TOKENS(ctr_, \
102 uid){}; \
103 ::guanaqo::trace::get_thread_gflop_count() += (gflops); \
104 TRACE_EVENT("gflops", name, \
105 GUANAQO_CONCATENATE_TOKENS(ctr_, uid).parent_track)
106
107#define GUANAQO_TRACE_LINALG_IMPL(name, gflops) \
108 GUANAQO_TRACE_LINALG_PRIVATE(name, gflops, __COUNTER__)
109
110#define GUANAQO_TRACE_INSTANT_IMPL(name, instance) \
111 TRACE_EVENT_INSTANT("trace", name, "instance", instance)
112
113#define GUANAQO_TRACE_REGION_IMPL(name, instance) \
114 TRACE_EVENT("trace", name, "instance", instance)
115
116/// @addtogroup trace_perfetto
117/// @{
118
119#define GUANAQO_TRACE_LINALG(name, gflops) \
120 PERFETTO_USE_CATEGORIES_FROM_NAMESPACE_SCOPED(guanaqo::trace); \
121 GUANAQO_TRACE_LINALG_IMPL(name, gflops)
122
123#define GUANAQO_TRACE_INSTANT(name, instance) \
124 PERFETTO_USE_CATEGORIES_FROM_NAMESPACE_SCOPED(guanaqo::trace); \
125 GUANAQO_TRACE_INSTANT_IMPL(name, instance)
126
127#define GUANAQO_TRACE_REGION(name, instance) \
128 PERFETTO_USE_CATEGORIES_FROM_NAMESPACE_SCOPED(guanaqo::trace); \
129 GUANAQO_TRACE_REGION_IMPL(name, instance)
130
131// TODO: deprecate
132#define GUANAQO_TRACE(name, instance, ...) \
133 PERFETTO_USE_CATEGORIES_FROM_NAMESPACE_SCOPED(guanaqo::trace); \
134 GUANAQO_TRACE_REGION(name, instance)
135
136#define GUANAQO_TRACE_STATIC_STR(s) \
137 ::perfetto::StaticString { s }
138
139/// @}
140
141#endif
Performance counter snapshots and scoped collectors.
std::unique_ptr< detail::ScopedCounters > start_counters()
May return null if PCM is not available.
Definition counters.cpp:398
void stop_tracing(std::unique_ptr<::perfetto::TracingSession > tracing_session, const fs::path &output_path)
Stop the tracing session and write the trace data to the specified output file.
Definition trace.cpp:43
void abort_tracing(std::unique_ptr<::perfetto::TracingSession > tracing_session)
Stop the tracing session and discard the trace data.
Definition trace.cpp:38
std::unique_ptr<::perfetto::TracingSession > start_tracing(uint32_t memory_kb=128 *1024)
Start a new tracing session with the specified buffer size in KiB.
Definition trace.cpp:24
uint64_t & get_thread_gflop_count()
Get a reference to the thread-local GFLOP counter.
Definition trace.cpp:11
void initialize_tracing()
Initialize Perfetto for in-process tracing and register guanaqo's track event categories.
Definition trace.cpp:16
inline ::perfetto::base::PlatformThreadId get_thread_id_fast()
Definition trace.hpp:53
PERFETTO_USE_CATEGORIES_FROM_NAMESPACE(guanaqo::trace)
PERFETTO_DEFINE_CATEGORIES_IN_NAMESPACE(guanaqo::trace, ::perfetto::Category("trace").SetDescription("General trace events for regions of code"), ::perfetto::Category("gflops").SetDescription("Counts the total number of floating point operations"), ::perfetto::Category("pcm").SetDescription("Performance counters for CPU metrics"))
Token concatenation and argument-counting helpers.
::perfetto::ThreadTrack parent_track
Definition trace.hpp:82
ScopedLinalgCounters(const ScopedLinalgCounters &)=delete
ScopedLinalgCounters & operator=(const ScopedLinalgCounters &)=delete