Debian Codegraph @

src/panfrost/vulkan/panvk_utrace_perfetto.cc

raw
1 /*
2 * Copyright 2024 Google LLC
3 * Copyright 2025 Arm Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "panvk_utrace_perfetto.h"
8
9 #include <functional>
10
11 #ifndef ANDROID_LIBPERFETTO
12 #include <perfetto.h>
13 #else
14 #include <perfetto/tracing.h>
15 #endif
16
17 #include "c11/threads.h"
18 #include "util/log.h"
19 #include "util/perf/u_perfetto.h"
20 #include "util/perf/u_perfetto_renderpass.h"
21 #include "util/timespec.h"
22 #include "util/u_process.h"
23
24 #include "panvk_device.h"
25 #include "panvk_tracepoints.h"
26 #include "panvk_tracepoints_perfetto.h"
27 #include "panvk_utrace.h"
28
29 struct PanVKRenderpassTraits : public perfetto::DefaultDataSourceTraits {
30 using IncrementalStateType = MesaRenderpassIncrementalState;
31 };
32
33 class PanVKRenderpassDataSource
34 : public MesaRenderpassDataSource<PanVKRenderpassDataSource,
35 PanVKRenderpassTraits> {};
36
37 PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(PanVKRenderpassDataSource);
38 PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(PanVKRenderpassDataSource);
39
40 static const char *
41 get_stage_name(enum panvk_utrace_perfetto_stage stage)
42 {
43 switch (stage) {
44 #define CASE(x) \
45 case PANVK_UTRACE_PERFETTO_STAGE_##x: \
46 return #x
47 CASE(CMDBUF);
48 CASE(META);
49 CASE(RENDER);
50 CASE(DISPATCH);
51 CASE(BARRIER);
52 CASE(FLUSH_CACHE);
53 CASE(SYNC_ADD);
54 CASE(SYNC_WAIT);
55 #undef CASE
56 default:
57 UNREACHABLE("bad stage");
58 }
59 }
60
61 static void
62 emit_interned_data_packet(struct panvk_device *dev,
63 PanVKRenderpassDataSource::TraceContext &ctx,
64 uint64_t now)
65 {
66 const struct panvk_utrace_perfetto *utp = &dev->utrace.utp;
67
68 auto packet = ctx.NewTracePacket();
69 packet->set_timestamp(now);
70 packet->set_sequence_flags(
71 perfetto::protos::pbzero::TracePacket::SEQ_INCREMENTAL_STATE_CLEARED);
72
73 auto interned_data = packet->set_interned_data();
74
75 for (uint32_t i = 0; i < ARRAY_SIZE(utp->queue_iids); i++) {
76 char name[64];
77 snprintf(name, sizeof(name), "%s-queue-%d", util_get_process_name(), i);
78
79 auto specs = interned_data->add_gpu_specifications();
80 specs->set_iid(utp->queue_iids[i]);
81 specs->set_name(name);
82 }
83
84 for (uint32_t i = 0; i < ARRAY_SIZE(utp->stage_iids); i++) {
85 auto specs = interned_data->add_gpu_specifications();
86 specs->set_iid(utp->stage_iids[i]);
87 specs->set_name(get_stage_name((enum panvk_utrace_perfetto_stage)i));
88 }
89 }
90
91 static uint64_t
92 get_gpu_time_ns(struct panvk_device *dev)
93 {
94 const struct panvk_physical_device *pdev =
95 to_panvk_physical_device(dev->vk.physical);
96 const struct pan_kmod_dev_props *props = &pdev->kmod.dev->props;
97
98 const uint64_t ts = pan_kmod_query_timestamp(dev->kmod.dev);
99 return ts * NSEC_PER_SEC / props->timestamp_frequency;
100 }
101
102 static void
103 emit_clock_snapshot_packet(struct panvk_device *dev,
104 PanVKRenderpassDataSource::TraceContext &ctx)
105 {
106 const struct panvk_utrace_perfetto *utp = &dev->utrace.utp;
107 const uint64_t gpu_ns = get_gpu_time_ns(dev);
108 const uint32_t cpu_clock_id =
109 perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC_RAW;
110 const uint64_t cpu_ns = perfetto::base::GetWallTimeRawNs().count();
111
112 MesaRenderpassDataSource<PanVKRenderpassDataSource, PanVKRenderpassTraits>::
113 EmitClockSync(ctx, cpu_ns, gpu_ns, cpu_clock_id, utp->gpu_clock_id);
114 }
115
116 static void
117 emit_setup_packets(struct panvk_device *dev,
118 PanVKRenderpassDataSource::TraceContext &ctx)
119 {
120 struct panvk_utrace_perfetto *utp = &dev->utrace.utp;
121
122 const uint64_t now = perfetto::base::GetBootTimeNs().count();
123
124 /* emit interned data if cleared */
125 auto state = ctx.GetIncrementalState();
126 if (state->was_cleared) {
127 emit_interned_data_packet(dev, ctx, now);
128
129 state->was_cleared = false;
130 utp->next_clock_snapshot = 0;
131 }
132
133 /* emit clock snapshots periodically */
134 if (now >= utp->next_clock_snapshot) {
135 emit_clock_snapshot_packet(dev, ctx);
136
137 utp->next_clock_snapshot = now + NSEC_PER_SEC;
138 }
139 }
140
141 static struct panvk_utrace_perfetto_event *
142 begin_event(struct panvk_device *dev,
143 const struct panvk_utrace_flush_data *data,
144 enum panvk_utrace_perfetto_stage stage)
145 {
146 struct panvk_utrace_perfetto *utp = &dev->utrace.utp;
147 struct panvk_utrace_perfetto_queue *queue = &utp->queues[data->subqueue];
148 struct panvk_utrace_perfetto_event *ev = &queue->stack[queue->stack_depth++];
149
150 assert(data->subqueue < PANVK_UTRACE_PERFETTO_QUEUE_COUNT);
151
152 if (queue->stack_depth > PANVK_UTRACE_PERFETTO_STACK_DEPTH) {
153 PERFETTO_ELOG("queue %d stage %d too deep", data->subqueue, stage);
154 return NULL;
155 }
156
157 ev->stage = stage;
158 return ev;
159 }
160
161 static struct panvk_utrace_perfetto_event *
162 end_event(struct panvk_device *dev, const struct panvk_utrace_flush_data *data,
163 enum panvk_utrace_perfetto_stage stage)
164 {
165 struct panvk_utrace_perfetto *utp = &dev->utrace.utp;
166 struct panvk_utrace_perfetto_queue *queue = &utp->queues[data->subqueue];
167
168 assert(data->subqueue < PANVK_UTRACE_PERFETTO_QUEUE_COUNT);
169
170 if (!queue->stack_depth)
171 return NULL;
172
173 struct panvk_utrace_perfetto_event *ev = &queue->stack[--queue->stack_depth];
174 if (queue->stack_depth >= PANVK_UTRACE_PERFETTO_STACK_DEPTH)
175 return NULL;
176
177 assert(ev->stage == stage);
178 return ev;
179 }
180
181 static void
182 panvk_utrace_perfetto_begin_event(struct panvk_device *dev,
183 const struct panvk_utrace_flush_data *data,
184 enum panvk_utrace_perfetto_stage stage,
185 uint64_t ts_ns)
186 {
187 struct panvk_utrace_perfetto_event *ev = begin_event(dev, data, stage);
188 if (!ev)
189 return;
190
191 struct panvk_utrace_perfetto *utp = &dev->utrace.utp;
192 if (utp->queues[data->subqueue].stack_depth == 1) {
193 utp->base_ts_ns = ts_ns;
194 }
195
196 ev->begin_ns = ts_ns;
197 }
198
199 static void
200 panvk_utrace_perfetto_end_event(
201 struct panvk_device *dev, const struct panvk_utrace_flush_data *data,
202 enum panvk_utrace_perfetto_stage stage, uint64_t ts_ns,
203 std::function<void(perfetto::protos::pbzero::GpuRenderStageEvent *)>
204 emit_event_extra)
205 {
206 const struct panvk_utrace_perfetto_event *ev = end_event(dev, data, stage);
207 if (!ev)
208 return;
209
210 struct panvk_utrace_perfetto *utp = &dev->utrace.utp;
211 if (ev->begin_ns < utp->base_ts_ns) {
212 /* The event has started before the base event of this nested stack of
213 * events. Is is likely data from a previous submission, so we just drop
214 * it here. */
215 return;
216 }
217
218 uint64_t duration = ts_ns - ev->begin_ns;
219 /* Drop unwritten and zero duration events.
220 * If the timestamp wraps around, end can be before start. Also drop those. */
221 if (duration == 0 || ts_ns == 0 || ts_ns < ev->begin_ns) {
222 return;
223 }
224
225 PanVKRenderpassDataSource::Trace(
226 [=](PanVKRenderpassDataSource::TraceContext ctx) {
227 struct panvk_utrace_perfetto *utp = &dev->utrace.utp;
228
229 emit_setup_packets(dev, ctx);
230
231 auto packet = ctx.NewTracePacket();
232 packet->set_timestamp(ev->begin_ns);
233 packet->set_timestamp_clock_id(utp->gpu_clock_id);
234
235 auto event = packet->set_gpu_render_stage_event();
236 event->set_event_id(utp->event_id++);
237 event->set_duration(ts_ns - ev->begin_ns);
238 event->set_hw_queue_iid(utp->queue_iids[data->subqueue]);
239 event->set_stage_iid(utp->stage_iids[stage]);
240 event->set_context(utp->device_id);
241
242 emit_event_extra(event);
243 });
244 }
245
246 #define PANVK_UTRACE_PERFETTO_PROCESS_EVENT(tp, stage) \
247 void panvk_utrace_perfetto_begin_##tp( \
248 struct panvk_device *dev, uint64_t ts_ns, uint16_t tp_idx, \
249 const void *flush_data, const struct trace_begin_##tp *payload, \
250 const void *indirect_data) \
251 { \
252 /* we can ignore them or save them if we choose to */ \
253 assert(!payload && !indirect_data); \
254 panvk_utrace_perfetto_begin_event( \
255 dev, (const struct panvk_utrace_flush_data *)flush_data, \
256 PANVK_UTRACE_PERFETTO_STAGE_##stage, ts_ns); \
257 } \
258 \
259 void panvk_utrace_perfetto_end_##tp( \
260 struct panvk_device *dev, uint64_t ts_ns, uint16_t tp_idx, \
261 const void *flush_data, const struct trace_end_##tp *payload, \
262 const void *indirect_data) \
263 { \
264 auto emit_event_extra = \
265 [=](perfetto::protos::pbzero::GpuRenderStageEvent *event) { \
266 trace_payload_as_extra_end_##tp(event, payload, indirect_data); \
267 }; \
268 panvk_utrace_perfetto_end_event( \
269 dev, (const struct panvk_utrace_flush_data *)flush_data, \
270 PANVK_UTRACE_PERFETTO_STAGE_##stage, ts_ns, emit_event_extra); \
271 }
272
273 /* u_trace_context_process dispatches trace events to a background thread
274 * (traceq) for processing. These callbacks are called from traceq.
275 */
276 PANVK_UTRACE_PERFETTO_PROCESS_EVENT(cmdbuf, CMDBUF)
277 PANVK_UTRACE_PERFETTO_PROCESS_EVENT(meta, META)
278 PANVK_UTRACE_PERFETTO_PROCESS_EVENT(render, RENDER)
279 PANVK_UTRACE_PERFETTO_PROCESS_EVENT(dispatch, DISPATCH)
280 PANVK_UTRACE_PERFETTO_PROCESS_EVENT(dispatch_indirect, DISPATCH)
281 PANVK_UTRACE_PERFETTO_PROCESS_EVENT(barrier, BARRIER)
282 PANVK_UTRACE_PERFETTO_PROCESS_EVENT(flush_cache, FLUSH_CACHE)
283 PANVK_UTRACE_PERFETTO_PROCESS_EVENT(sync32_add, SYNC_ADD)
284 PANVK_UTRACE_PERFETTO_PROCESS_EVENT(sync64_add, SYNC_ADD)
285 PANVK_UTRACE_PERFETTO_PROCESS_EVENT(sync32_wait, SYNC_WAIT)
286 PANVK_UTRACE_PERFETTO_PROCESS_EVENT(sync64_wait, SYNC_WAIT)
287
288 static uint32_t
289 get_gpu_clock_id(void)
290 {
291 /* see https://perfetto.dev/docs/concepts/clock-sync
292 *
293 * Use sequence-scoped clock (64 <= ID < 128) for GPU clock because there's
294 * no central daemon emitting consistent snapshots for synchronization
295 * between CPU and GPU clocks on behalf of renderstages and counters
296 * producers.
297 *
298 * When CPU clock is the same with the authoritative trace clock (normally
299 * default to CLOCK_BOOTTIME), perfetto drops the non-monotonic snapshots
300 * to ensure validity of the global source clock in the resolution graph.
301 * When they are different, the clocks are marked invalid and the rest of
302 * the clock syncs will fail during trace processing.
303 *
304 * Now that PanVK has switched to use CLOCK_MONOTONIC_RAW CPU clock, here
305 * we make the GPU clock sequence-scoped so that clock sync for all the
306 * trace packets emitted with the same trusted_packet_sequence_id will be
307 * done in an isolated manner (basically perfetto makes each scoped GPU
308 * clock globally unique).
309 *
310 * Meanwhile, since the clock is now sequence-scoped (unique per producer +
311 * writer pair within the tracing session), we can simply pick 64 to start
312 * with since there's no multi-mali-gpu setup yet.
313 */
314 return 64;
315 }
316
317 static void
318 register_data_source(void)
319 {
320 perfetto::DataSourceDescriptor dsd;
321 #if DETECT_OS_ANDROID
322 // Android tooling expects this data source name
323 dsd.set_name("gpu.renderstages");
324 #else
325 dsd.set_name("gpu.renderstages.panfrost");
326 #endif
327 PanVKRenderpassDataSource::Register(dsd);
328 }
329
330 void
331 panvk_utrace_perfetto_init(struct panvk_device *dev, uint32_t queue_count)
332 {
333 const struct panvk_physical_device *pdev =
334 to_panvk_physical_device(dev->vk.physical);
335 const struct pan_kmod_dev_props *props = &pdev->kmod.dev->props;
336 struct panvk_utrace_perfetto *utp = &dev->utrace.utp;
337
338 if (queue_count > PANVK_UTRACE_PERFETTO_QUEUE_COUNT) {
339 assert(!"PANVK_UTRACE_PERFETTO_QUEUE_COUNT too small");
340 return;
341 }
342
343 /* check for timestamp support */
344 if (!props->gpu_can_query_timestamp || !props->timestamp_frequency ||
345 !get_gpu_time_ns(dev))
346 return;
347
348 utp->gpu_clock_id = get_gpu_clock_id();
349 utp->device_id = (uintptr_t)dev;
350
351 uint64_t next_iid = 1;
352 for (uint32_t i = 0; i < ARRAY_SIZE(utp->queue_iids); i++)
353 utp->queue_iids[i] = next_iid++;
354 for (uint32_t i = 0; i < ARRAY_SIZE(utp->stage_iids); i++)
355 utp->stage_iids[i] = next_iid++;
356
357 /* Mali GPU timestamps map to the system arch counter. CLOCK_MONOTONIC_RAW
358 * is therefore better for synchronization with the GPU timestamps than the
359 * default CLOCK_BOOTTIME, which drifts from the arch counter's rate
360 * slightly due to NTP adjustment. */
361 util_perfetto_set_default_clock(CLOCK_MONOTONIC_RAW);
362
363 static once_flag register_ds_once = ONCE_FLAG_INIT;
364 call_once(&register_ds_once, register_data_source);
365 }