guanaqo main
Utilities for scientific software
Loading...
Searching...
No Matches
trace.cpp
Go to the documentation of this file.
2
3#if GUANAQO_WITH_PERFETTO
4
5#include <fstream>
6#include <future>
7#include <stdexcept>
8
9namespace guanaqo::trace {
10
12 static thread_local uint64_t gflop_count = 0;
13 return gflop_count;
14}
15
17 ::perfetto::TracingInitArgs args;
18 args.backends = ::perfetto::kInProcessBackend;
19
20 ::perfetto::Tracing::Initialize(args);
21 guanaqo::trace::TrackEvent::Register();
22}
23
24std::unique_ptr<::perfetto::TracingSession> start_tracing(uint32_t memory_kb) {
25 ::perfetto::TraceConfig cfg;
26 cfg.add_buffers()->set_size_kb(memory_kb);
27
28 auto *ds_cfg = cfg.add_data_sources()->mutable_config();
29 ds_cfg->set_name("track_event");
30
31 std::unique_ptr<::perfetto::TracingSession> tracing_session =
32 ::perfetto::Tracing::NewTrace();
33 tracing_session->Setup(cfg);
34 tracing_session->StartBlocking();
35 return tracing_session;
36}
37
39 std::unique_ptr<::perfetto::TracingSession> tracing_session) {
40 tracing_session->StopBlocking();
41}
42
43void stop_tracing(std::unique_ptr<::perfetto::TracingSession> tracing_session,
44 const fs::path &output_path) {
45 std::ofstream output{output_path, std::ios::out | std::ios::binary};
46 if (!output)
47 throw std::runtime_error(std::format("Failed to open output file: {}",
48 output_path.string()));
49 if (!tracing_session->FlushBlocking())
50 return;
51 tracing_session->StopBlocking();
52 std::promise<void> done;
53 tracing_session->ReadTrace(
54 [&](::perfetto::TracingSession::ReadTraceCallbackArgs args) {
55 output.write(args.data, std::streamsize(args.size));
56 if (!args.has_more)
57 done.set_value();
58 });
59 done.get_future().wait();
60 tracing_session.reset();
61}
62
63#if GUANAQO_WITH_PCM_TRACING
64#define GUANAQO_COUNT_PCM(ctr, name, track, time) \
65 TRACE_COUNTER("pcm", ::perfetto::CounterTrack(#name, track), time, \
66 (ctr)->name)
67
68// Having this in the header results in absolutely insane compile times ...
70 auto time = TrackEvent::GetTraceTimeNs();
71 TRACE_COUNTER("gflops", ::perfetto::CounterTrack("gflops", parent_track),
73 if (ctr) {
74 GUANAQO_COUNT_PCM(ctr, instructions, parent_track, time);
75 GUANAQO_COUNT_PCM(ctr, cycles, parent_track, time);
76 GUANAQO_COUNT_PCM(ctr, ref_cycles, parent_track, time);
77 GUANAQO_COUNT_PCM(ctr, l2_misses, parent_track, time);
78 GUANAQO_COUNT_PCM(ctr, l2_hits, parent_track, time);
79 GUANAQO_COUNT_PCM(ctr, l3_misses, parent_track, time);
80 GUANAQO_COUNT_PCM(ctr, branch_misses, parent_track, time);
81 GUANAQO_COUNT_PCM(ctr, all_slots, parent_track, time);
82 GUANAQO_COUNT_PCM(ctr, frontend_bound_slots, parent_track, time);
83 GUANAQO_COUNT_PCM(ctr, backend_bound_slots, parent_track, time);
84 GUANAQO_COUNT_PCM(ctr, bad_speculation_slots, parent_track, time);
85 GUANAQO_COUNT_PCM(ctr, retiring_slots, parent_track, time);
86 GUANAQO_COUNT_PCM(ctr, mem_bound_slots, parent_track, time);
87 GUANAQO_COUNT_PCM(ctr, fetch_lat_slots, parent_track, time);
88 }
89}
90#endif
91
92} // namespace guanaqo::trace
93
95 GUANAQO_EXPORT);
96
97#endif
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
PERFETTO_TRACK_EVENT_STATIC_STORAGE_IN_NAMESPACE_WITH_ATTRS(guanaqo::trace,)
Perfetto tracing macros and session helpers.
::perfetto::ThreadTrack parent_track
Definition trace.hpp:82