Debian Codegraph @

src/gallium/drivers/freedreno/freedreno_perfetto.cc

raw symbols SCIP
1 /*
2 * Copyright © 2021 Google, Inc.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <perfetto.h>
7
8 #include "util/perf/u_perfetto.h"
9 #include "util/perf/u_perfetto_renderpass.h"
10
11 #include "freedreno_tracepoints.h"
12
13 static uint32_t gpu_clock_id;
14 static uint64_t next_clock_sync_ns; /* cpu time of next clk sync */
15
16 /**
17 * The timestamp at the point where we first emitted the clock_sync..
18 * this will be a *later* timestamp that the first GPU traces (since
19 * we capture the first clock_sync from the CPU *after* the first GPU
20 * tracepoints happen). To avoid confusing perfetto we need to drop
21 * the GPU traces with timestamps before this.
22 */
23 static uint64_t sync_gpu_ts;
24
25 struct FdRenderpassTraits : public perfetto::DefaultDataSourceTraits {
26 using IncrementalStateType = MesaRenderpassIncrementalState;
27 };
28
29 class FdRenderpassDataSource : public MesaRenderpassDataSource<FdRenderpassDataSource, FdRenderpassTraits> {
30 public:
31
32 void OnStart(const StartArgs &args) override
33 {
34 MesaRenderpassDataSource<FdRenderpassDataSource, FdRenderpassTraits>::OnStart(args);
35
36 /* Note: clock_id's below 128 are reserved.. for custom clock sources,
37 * using the hash of a namespaced string is the recommended approach.
38 * See: https://perfetto.dev/docs/concepts/clock-sync
39 */
40 gpu_clock_id =
41 _mesa_hash_string("org.freedesktop.mesa.freedreno") | 0x80000000;
42 }
43 };
44
45 PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(FdRenderpassDataSource);
46 PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(FdRenderpassDataSource);
47
48 static void
49 send_descriptors(FdRenderpassDataSource::TraceContext &ctx, uint64_t ts_ns)
50 {
51 PERFETTO_LOG("Sending renderstage descriptors");
52
53 auto packet = ctx.NewTracePacket();
54
55 packet->set_timestamp(0);
56 // packet->set_timestamp(ts_ns);
57 // packet->set_timestamp_clock_id(gpu_clock_id);
58
59 auto event = packet->set_gpu_render_stage_event();
60 event->set_gpu_id(0);
61
62 auto spec = event->set_specifications();
63
64 for (unsigned i = 0; i < ARRAY_SIZE(queues); i++) {
65 auto desc = spec->add_hw_queue();
66
67 desc->set_name(queues[i].name);
68 desc->set_description(queues[i].desc);
69 }
70
71 for (unsigned i = 0; i < ARRAY_SIZE(stages); i++) {
72 auto desc = spec->add_stage();
73
74 desc->set_name(stages[i].name);
75 if (stages[i].desc)
76 desc->set_description(stages[i].desc);
77 }
78 }
79
80 static void
81 stage_start(struct pipe_context *pctx, uint64_t ts_ns, enum fd_stage_id stage)
82 {
83 struct fd_context *ctx = fd_context(pctx);
84 struct fd_perfetto_state *p = &ctx->perfetto;
85
86 p->start_ts[stage] = ts_ns;
87 }
88
89 static void
90 stage_end(struct pipe_context *pctx, uint64_t ts_ns, enum fd_stage_id stage)
91 {
92 struct fd_context *ctx = fd_context(pctx);
93 struct fd_perfetto_state *p = &ctx->perfetto;
94
95 /* If we haven't managed to calibrate the alignment between GPU and CPU
96 * timestamps yet, then skip this trace, otherwise perfetto won't know
97 * what to do with it.
98 */
99 if (!sync_gpu_ts)
100 return;
101
102 FdRenderpassDataSource::Trace([=](FdRenderpassDataSource::TraceContext tctx) {
103 if (auto state = tctx.GetIncrementalState(); state->was_cleared) {
104 send_descriptors(tctx, p->start_ts[stage]);
105 state->was_cleared = false;
106 }
107
108 auto packet = tctx.NewTracePacket();
109
110 packet->set_timestamp(p->start_ts[stage]);
111 packet->set_timestamp_clock_id(gpu_clock_id);
112
113 auto event = packet->set_gpu_render_stage_event();
114 event->set_event_id(0); // ???
115 event->set_hw_queue_id(DEFAULT_HW_QUEUE_ID);
116 event->set_duration(ts_ns - p->start_ts[stage]);
117 event->set_stage_id(stage);
118 event->set_context((uintptr_t)pctx);
119
120 /* The "surface" meta-stage has extra info about render target: */
121 if (stage == SURFACE_STAGE_ID) {
122
123 event->set_submission_id(p->submit_id);
124
125 if (p->cbuf0_format) {
126 auto data = event->add_extra_data();
127
128 data->set_name("color0 format");
129 data->set_value(util_format_short_name(p->cbuf0_format));
130 }
131
132 if (p->zs_format) {
133 auto data = event->add_extra_data();
134
135 data->set_name("zs format");
136 data->set_value(util_format_short_name(p->zs_format));
137 }
138
139 {
140 auto data = event->add_extra_data();
141
142 data->set_name("width");
143 data->set_value(std::to_string(p->width));
144 }
145
146 {
147 auto data = event->add_extra_data();
148
149 data->set_name("height");
150 data->set_value(std::to_string(p->height));
151 }
152
153 {
154 auto data = event->add_extra_data();
155
156 data->set_name("MSAA");
157 data->set_value(std::to_string(p->samples));
158 }
159
160 {
161 auto data = event->add_extra_data();
162
163 data->set_name("MRTs");
164 data->set_value(std::to_string(p->mrts));
165 }
166
167 // "renderMode"
168 // "surfaceID"
169
170 if (p->nbins) {
171 auto data = event->add_extra_data();
172
173 data->set_name("numberOfBins");
174 data->set_value(std::to_string(p->nbins));
175 }
176
177 if (p->binw) {
178 auto data = event->add_extra_data();
179
180 data->set_name("binWidth");
181 data->set_value(std::to_string(p->binw));
182 }
183
184 if (p->binh) {
185 auto data = event->add_extra_data();
186
187 data->set_name("binHeight");
188 data->set_value(std::to_string(p->binh));
189 }
190 } else if (stage == COMPUTE_STAGE_ID) {
191 {
192 auto data = event->add_extra_data();
193
194 data->set_name("indirect");
195 data->set_value(std::to_string(p->indirect));
196 }
197
198 {
199 auto data = event->add_extra_data();
200
201 data->set_name("work_dim");
202 data->set_value(std::to_string(p->work_dim));
203 }
204
205 {
206 auto data = event->add_extra_data();
207
208 data->set_name("local_size_x");
209 data->set_value(std::to_string(p->local_size_x));
210 }
211
212 {
213 auto data = event->add_extra_data();
214
215 data->set_name("local_size_y");
216 data->set_value(std::to_string(p->local_size_y));
217 }
218
219 {
220 auto data = event->add_extra_data();
221
222 data->set_name("local_size_z");
223 data->set_value(std::to_string(p->local_size_z));
224 }
225
226 {
227 auto data = event->add_extra_data();
228
229 data->set_name("num_groups_x");
230 data->set_value(std::to_string(p->num_groups_x));
231 }
232
233 {
234 auto data = event->add_extra_data();
235
236 data->set_name("num_groups_y");
237 data->set_value(std::to_string(p->num_groups_y));
238 }
239
240 {
241 auto data = event->add_extra_data();
242
243 data->set_name("num_groups_z");
244 data->set_value(std::to_string(p->num_groups_z));
245 }
246
247 {
248 auto data = event->add_extra_data();
249
250 data->set_name("shader_id");
251 data->set_value(std::to_string(p->shader_id));
252 }
253 }
254 });
255 }
256
257 #ifdef __cplusplus
258 extern "C" {
259 #endif
260
261 void
262 fd_perfetto_init(void)
263 {
264 perfetto::DataSourceDescriptor dsd;
265 #if DETECT_OS_ANDROID
266 // Android tooling expects this data source name
267 dsd.set_name("gpu.renderstages");
268 #else
269 dsd.set_name("gpu.renderstages.msm");
270 #endif
271 FdRenderpassDataSource::Register(dsd);
272 }
273
274 static void
275 sync_timestamp(struct fd_context *ctx)
276 {
277 uint64_t cpu_ts = perfetto::base::GetBootTimeNs().count();
278 uint64_t gpu_ts;
279
280 if (!ctx->ts_to_ns)
281 return;
282
283 if (cpu_ts < next_clock_sync_ns)
284 return;
285
286 if (fd_pipe_get_param(ctx->pipe, FD_TIMESTAMP, &gpu_ts)) {
287 PERFETTO_ELOG("Could not sync CPU and GPU clocks");
288 return;
289 }
290
291 /* get cpu timestamp again because FD_TIMESTAMP can take >100us */
292 uint32_t cpu_clock_id = perfetto::protos::pbzero::BUILTIN_CLOCK_BOOTTIME;
293 cpu_ts = perfetto::base::GetBootTimeNs().count();
294
295 /* convert GPU ts into ns: */
296 gpu_ts = ctx->ts_to_ns(gpu_ts);
297
298 FdRenderpassDataSource::Trace([=](auto tctx) {
299 MesaRenderpassDataSource<FdRenderpassDataSource,
300 FdRenderpassTraits>::EmitClockSync(tctx, cpu_ts,
301 gpu_ts, cpu_clock_id,
302 gpu_clock_id);
303 });
304
305 sync_gpu_ts = gpu_ts;
306 next_clock_sync_ns = cpu_ts + 30000000;
307 }
308
309 static void
310 emit_submit_id(struct fd_context *ctx)
311 {
312 FdRenderpassDataSource::Trace([=](FdRenderpassDataSource::TraceContext tctx) {
313 auto packet = tctx.NewTracePacket();
314
315 packet->set_timestamp(perfetto::base::GetBootTimeNs().count());
316
317 auto event = packet->set_vulkan_api_event();
318 auto submit = event->set_vk_queue_submit();
319
320 submit->set_submission_id(ctx->submit_count);
321 });
322 }
323
324 void
325 fd_perfetto_submit(struct fd_context *ctx)
326 {
327 /* sync_timestamp isn't free */
328 if (!u_trace_perfetto_active(&ctx->trace_context))
329 return;
330
331 sync_timestamp(ctx);
332 emit_submit_id(ctx);
333 }
334
335 /*
336 * Trace callbacks, called from u_trace once the timestamps from GPU have been
337 * collected.
338 */
339
340 void
341 fd_start_render_pass(struct pipe_context *pctx, uint64_t ts_ns,
342 uint16_t tp_idx, const void *flush_data,
343 const struct trace_start_render_pass *payload,
344 const void *indirect_data)
345 {
346 stage_start(pctx, ts_ns, SURFACE_STAGE_ID);
347
348 struct fd_perfetto_state *p = &fd_context(pctx)->perfetto;
349
350 p->submit_id = payload->submit_id;
351 p->cbuf0_format = payload->cbuf0_format;
352 p->zs_format = payload->zs_format;
353 p->width = payload->width;
354 p->height = payload->height;
355 p->mrts = payload->mrts;
356 p->samples = payload->samples;
357 p->nbins = payload->nbins;
358 p->binw = payload->binw;
359 p->binh = payload->binh;
360 }
361
362 void
363 fd_end_render_pass(struct pipe_context *pctx, uint64_t ts_ns,
364 uint16_t tp_idx, const void *flush_data,
365 const struct trace_end_render_pass *payload,
366 const void *indirect_data)
367 {
368 stage_end(pctx, ts_ns, SURFACE_STAGE_ID);
369 }
370
371 void
372 fd_start_binning_ib(struct pipe_context *pctx, uint64_t ts_ns,
373 uint16_t tp_idx, const void *flush_data,
374 const struct trace_start_binning_ib *payload,
375 const void *indirect_data)
376 {
377 stage_start(pctx, ts_ns, BINNING_STAGE_ID);
378 }
379
380 void
381 fd_end_binning_ib(struct pipe_context *pctx, uint64_t ts_ns,
382 uint16_t tp_idx, const void *flush_data,
383 const struct trace_end_binning_ib *payload,
384 const void *indirect_data)
385 {
386 stage_end(pctx, ts_ns, BINNING_STAGE_ID);
387 }
388
389 void
390 fd_start_draw_ib(struct pipe_context *pctx, uint64_t ts_ns,
391 uint16_t tp_idx, const void *flush_data,
392 const struct trace_start_draw_ib *payload,
393 const void *indirect_data)
394 {
395 stage_start(
396 pctx, ts_ns,
397 fd_context(pctx)->perfetto.nbins ? GMEM_STAGE_ID : BYPASS_STAGE_ID);
398 }
399
400 void
401 fd_end_draw_ib(struct pipe_context *pctx, uint64_t ts_ns,
402 uint16_t tp_idx, const void *flush_data,
403 const struct trace_end_draw_ib *payload,
404 const void *indirect_data)
405 {
406 stage_end(
407 pctx, ts_ns,
408 fd_context(pctx)->perfetto.nbins ? GMEM_STAGE_ID : BYPASS_STAGE_ID);
409 }
410
411 void
412 fd_start_blit(struct pipe_context *pctx, uint64_t ts_ns,
413 uint16_t tp_idx, const void *flush_data,
414 const struct trace_start_blit *payload,
415 const void *indirect_data)
416 {
417 stage_start(pctx, ts_ns, BLIT_STAGE_ID);
418 }
419
420 void
421 fd_end_blit(struct pipe_context *pctx, uint64_t ts_ns,
422 uint16_t tp_idx, const void *flush_data,
423 const struct trace_end_blit *payload,
424 const void *indirect_data)
425 {
426 stage_end(pctx, ts_ns, BLIT_STAGE_ID);
427 }
428
429 void
430 fd_start_compute(struct pipe_context *pctx, uint64_t ts_ns,
431 uint16_t tp_idx, const void *flush_data,
432 const struct trace_start_compute *payload,
433 const void *indirect_data)
434 {
435 stage_start(pctx, ts_ns, COMPUTE_STAGE_ID);
436
437 struct fd_perfetto_state *p = &fd_context(pctx)->perfetto;
438
439 p->indirect = payload->indirect;
440 p->work_dim = payload->work_dim;
441 p->local_size_x = payload->local_size_x;
442 p->local_size_y = payload->local_size_y;
443 p->local_size_z = payload->local_size_z;
444 p->num_groups_x = payload->num_groups_x;
445 p->num_groups_y = payload->num_groups_y;
446 p->num_groups_z = payload->num_groups_z;
447 p->shader_id = payload->shader_id;
448 }
449
450 void
451 fd_end_compute(struct pipe_context *pctx, uint64_t ts_ns,
452 uint16_t tp_idx, const void *flush_data,
453 const struct trace_end_compute *payload,
454 const void *indirect_data)
455 {
456 stage_end(pctx, ts_ns, COMPUTE_STAGE_ID);
457 }
458
459 void
460 fd_start_clears(struct pipe_context *pctx, uint64_t ts_ns,
461 uint16_t tp_idx, const void *flush_data,
462 const struct trace_start_clears *payload,
463 const void *indirect_data)
464 {
465 stage_start(pctx, ts_ns, CLEAR_STAGE_ID);
466 }
467
468 void
469 fd_end_clears(struct pipe_context *pctx, uint64_t ts_ns,
470 uint16_t tp_idx, const void *flush_data,
471 const struct trace_end_clears *payload,
472 const void *indirect_data)
473 {
474 stage_end(pctx, ts_ns, CLEAR_STAGE_ID);
475 }
476
477 void
478 fd_start_tile_loads(struct pipe_context *pctx, uint64_t ts_ns,
479 uint16_t tp_idx, const void *flush_data,
480 const struct trace_start_tile_loads *payload,
481 const void *indirect_data)
482 {
483 stage_start(pctx, ts_ns, TILE_LOAD_STAGE_ID);
484 }
485
486 void
487 fd_end_tile_loads(struct pipe_context *pctx, uint64_t ts_ns,
488 uint16_t tp_idx, const void *flush_data,
489 const struct trace_end_tile_loads *payload,
490 const void *indirect_data)
491 {
492 stage_end(pctx, ts_ns, TILE_LOAD_STAGE_ID);
493 }
494
495 void
496 fd_start_tile_stores(struct pipe_context *pctx, uint64_t ts_ns,
497 uint16_t tp_idx, const void *flush_data,
498 const struct trace_start_tile_stores *payload,
499 const void *indirect_data)
500 {
501 stage_start(pctx, ts_ns, TILE_STORE_STAGE_ID);
502 }
503
504 void
505 fd_end_tile_stores(struct pipe_context *pctx, uint64_t ts_ns,
506 uint16_t tp_idx, const void *flush_data,
507 const struct trace_end_tile_stores *payload,
508 const void *indirect_data)
509 {
510 stage_end(pctx, ts_ns, TILE_STORE_STAGE_ID);
511 }
512
513 void
514 fd_start_state_restore(struct pipe_context *pctx, uint64_t ts_ns,
515 uint16_t tp_idx, const void *flush_data,
516 const struct trace_start_state_restore *payload,
517 const void *indirect_data)
518 {
519 stage_start(pctx, ts_ns, STATE_RESTORE_STAGE_ID);
520 }
521
522 void
523 fd_end_state_restore(struct pipe_context *pctx, uint64_t ts_ns,
524 uint16_t tp_idx, const void *flush_data,
525 const struct trace_end_state_restore *payload,
526 const void *indirect_data)
527 {
528 stage_end(pctx, ts_ns, STATE_RESTORE_STAGE_ID);
529 }
530
531 void
532 fd_start_vsc_overflow_test(struct pipe_context *pctx, uint64_t ts_ns,
533 uint16_t tp_idx, const void *flush_data,
534 const struct trace_start_vsc_overflow_test *payload,
535 const void *indirect_data)
536 {
537 stage_start(pctx, ts_ns, VSC_OVERFLOW_STAGE_ID);
538 }
539
540 void
541 fd_end_vsc_overflow_test(struct pipe_context *pctx, uint64_t ts_ns,
542 uint16_t tp_idx, const void *flush_data,
543 const struct trace_end_vsc_overflow_test *payload,
544 const void *indirect_data)
545 {
546 stage_end(pctx, ts_ns, VSC_OVERFLOW_STAGE_ID);
547 }
548
549 void
550 fd_start_prologue(struct pipe_context *pctx, uint64_t ts_ns,
551 uint16_t tp_idx, const void *flush_data,
552 const struct trace_start_prologue *payload,
553 const void *indirect_data)
554 {
555 stage_start(pctx, ts_ns, PROLOGUE_STAGE_ID);
556 }
557
558 void
559 fd_end_prologue(struct pipe_context *pctx, uint64_t ts_ns,
560 uint16_t tp_idx, const void *flush_data,
561 const struct trace_end_prologue *payload,
562 const void *indirect_data)
563 {
564 stage_end(pctx, ts_ns, PROLOGUE_STAGE_ID);
565 }
566
567 #ifdef __cplusplus
568 }
569 #endif