1 /*
2 * Copyright © 2021 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 #include "common/intel_gem.h"
28 #include "perf/intel_perf.h"
29
30 #include "util/hash_table.h"
31 #include "util/u_process.h"
32
33 #include "intel_driver_ds.h"
34 #include "intel_pps_priv.h"
35 #include "intel_tracepoints.h"
36
37 #ifdef HAVE_PERFETTO
38
39 #include "util/perf/u_perfetto.h"
40 #include "util/perf/u_perfetto_renderpass.h"
41
42 #include "intel_tracepoints_perfetto.h"
43
44 /* Just naming stages */
45 static const struct {
46 const char *name;
47
48 /* The perfetto UI requires that there is a parent-child relationship
49 * within a row of elements. Which means that all children elements must
50 * end within the lifespan of their parent.
51 *
52 * Some elements like stalls and command buffers follow that relationship,
53 * but not all. This tells us in which UI row the elements should live.
54 */
55 enum intel_ds_queue_stage draw_stage;
56 } intel_queue_stage_desc[INTEL_DS_QUEUE_STAGE_N_STAGES] = {
57 /* Order must match the enum! */
58 {
59 "queue",
60 INTEL_DS_QUEUE_STAGE_QUEUE,
61 },
62 {
63 "frame",
64 INTEL_DS_QUEUE_STAGE_FRAME,
65 },
66 {
67 "cmd-buffer",
68 INTEL_DS_QUEUE_STAGE_CMD_BUFFER,
69 },
70 {
71 "internal-ops",
72 INTEL_DS_QUEUE_STAGE_INTERNAL_OPS,
73 },
74 {
75 "stall",
76 INTEL_DS_QUEUE_STAGE_STALL,
77 },
78 {
79 "compute",
80 INTEL_DS_QUEUE_STAGE_COMPUTE,
81 },
82 {
83 "as-build",
84 INTEL_DS_QUEUE_STAGE_AS,
85 },
86 {
87 "RT",
88 INTEL_DS_QUEUE_STAGE_RT,
89 },
90 {
91 "render-pass",
92 INTEL_DS_QUEUE_STAGE_RENDER_PASS,
93 },
94 {
95 "blorp",
96 INTEL_DS_QUEUE_STAGE_BLORP,
97 },
98 {
99 "draw",
100 INTEL_DS_QUEUE_STAGE_DRAW,
101 },
102 {
103 "draw_mesh",
104 INTEL_DS_QUEUE_STAGE_DRAW_MESH,
105 },
106 };
107
108 struct IntelRenderpassIncrementalState {
109 bool was_cleared = true;
110 };
111
112 struct IntelRenderpassTraits : public perfetto::DefaultDataSourceTraits {
113 using IncrementalStateType = IntelRenderpassIncrementalState;
114 };
115
116 class IntelRenderpassDataSource : public MesaRenderpassDataSource<IntelRenderpassDataSource,
117 IntelRenderpassTraits> {
118 public:
119 /* Make sure we're not losing traces due to lack of shared memory space */
120 constexpr static perfetto::BufferExhaustedPolicy kBufferExhaustedPolicy =
121 perfetto::BufferExhaustedPolicy::kDrop;
122 };
123
124 PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(IntelRenderpassDataSource);
125 PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(IntelRenderpassDataSource);
126
127 using perfetto::protos::pbzero::InternedGpuRenderStageSpecification_RenderStageCategory;
128
129 static void
sync_timestamp(IntelRenderpassDataSource::TraceContext & ctx,struct intel_ds_device * device)130 sync_timestamp(IntelRenderpassDataSource::TraceContext &ctx,
131 struct intel_ds_device *device)
132 {
133 uint64_t cpu_ts, gpu_ts;
134
135 if (!intel_gem_read_correlate_cpu_gpu_timestamp(device->fd,
136 device->info.kmd_type,
137 INTEL_ENGINE_CLASS_RENDER, 0,
138 CLOCK_BOOTTIME,
139 &cpu_ts, &gpu_ts, NULL)) {
140 cpu_ts = perfetto::base::GetBootTimeNs().count();
141 intel_gem_read_render_timestamp(device->fd, device->info.kmd_type,
142 &gpu_ts);
143 }
144 gpu_ts = intel_device_info_timebase_scale(&device->info, gpu_ts);
145
146 if (cpu_ts < device->next_clock_sync_ns)
147 return;
148
149 PERFETTO_LOG("sending clocks gpu=0x%08x", device->gpu_clock_id);
150
151 device->sync_gpu_ts = gpu_ts;
152 device->next_clock_sync_ns = cpu_ts + 1000000000ull;
153
154 MesaRenderpassDataSource<IntelRenderpassDataSource, IntelRenderpassTraits>::EmitClockSync(ctx,
155 cpu_ts, gpu_ts, device->gpu_clock_id);
156 }
157
158 static void
send_descriptors(IntelRenderpassDataSource::TraceContext & ctx,struct intel_ds_device * device)159 send_descriptors(IntelRenderpassDataSource::TraceContext &ctx,
160 struct intel_ds_device *device)
161 {
162 PERFETTO_LOG("Sending renderstage descriptors");
163
164 device->event_id = 0;
165 list_for_each_entry_safe(struct intel_ds_queue, queue, &device->queues, link) {
166 for (uint32_t s = 0; s < ARRAY_SIZE(queue->stages); s++) {
167 queue->stages[s].start_ns[0] = 0;
168 }
169 }
170
171 {
172 auto packet = ctx.NewTracePacket();
173
174 packet->set_timestamp(perfetto::base::GetBootTimeNs().count());
175 packet->set_timestamp_clock_id(perfetto::protos::pbzero::BUILTIN_CLOCK_BOOTTIME);
176 packet->set_sequence_flags(perfetto::protos::pbzero::TracePacket::SEQ_INCREMENTAL_STATE_CLEARED);
177
178 auto interned_data = packet->set_interned_data();
179
180 {
181 auto desc = interned_data->add_graphics_contexts();
182 desc->set_iid(device->iid);
183 desc->set_pid(getpid());
184 switch (device->api) {
185 case INTEL_DS_API_OPENGL:
186 desc->set_api(perfetto::protos::pbzero::InternedGraphicsContext_Api::OPEN_GL);
187 break;
188 case INTEL_DS_API_VULKAN:
189 desc->set_api(perfetto::protos::pbzero::InternedGraphicsContext_Api::VULKAN);
190 break;
191 default:
192 break;
193 }
194 }
195
196 /* Emit all the IID picked at device/queue creation. */
197 list_for_each_entry_safe(struct intel_ds_queue, queue, &device->queues, link) {
198 for (unsigned s = 0; s < INTEL_DS_QUEUE_STAGE_N_STAGES; s++) {
199 {
200 /* We put the stage number in there so that all rows are order
201 * by intel_ds_queue_stage.
202 */
203 char name[100];
204 snprintf(name, sizeof(name), "%.10s-%s-%u-%s",
205 util_get_process_name(),
206 queue->name, s, intel_queue_stage_desc[s].name);
207
208 auto desc = interned_data->add_gpu_specifications();
209 desc->set_iid(queue->stages[s].queue_iid);
210 desc->set_name(name);
211 }
212 }
213 }
214
215 for (unsigned i = 0; i < ARRAY_SIZE(intel_tracepoint_names); i++) {
216 /* Skip the begin tracepoint, the label represent the couple of
217 * begin/end tracepoints.
218 */
219 if (strstr(intel_tracepoint_names[i], "intel_begin_") != NULL)
220 continue;
221
222 auto desc = interned_data->add_gpu_specifications();
223 desc->set_iid(device->tracepoint_iids[i]);
224 desc->set_name(intel_tracepoint_names[i] + strlen("intel_end_"));
225 }
226 }
227
228 device->next_clock_sync_ns = 0;
229 sync_timestamp(ctx, device);
230 }
231
232 typedef void (*trace_payload_as_extra_func)(perfetto::protos::pbzero::GpuRenderStageEvent *, const void*);
233
234 static void
begin_event(struct intel_ds_queue * queue,uint64_t ts_ns,enum intel_ds_queue_stage stage_id)235 begin_event(struct intel_ds_queue *queue, uint64_t ts_ns,
236 enum intel_ds_queue_stage stage_id)
237 {
238 uint32_t level = queue->stages[stage_id].level;
239 /* If we haven't managed to calibrate the alignment between GPU and CPU
240 * timestamps yet, then skip this trace, otherwise perfetto won't know
241 * what to do with it.
242 */
243 if (!queue->device->sync_gpu_ts) {
244 queue->stages[stage_id].start_ns[level] = 0;
245 return;
246 }
247
248 if (level >= (ARRAY_SIZE(queue->stages[stage_id].start_ns) - 1))
249 return;
250
251 queue->stages[stage_id].start_ns[level] = ts_ns;
252 queue->stages[stage_id].level++;
253 }
254
255 static void
end_event(struct intel_ds_queue * queue,uint64_t ts_ns,enum intel_ds_queue_stage stage_id,uint32_t submission_id,uint16_t tracepoint_idx,const char * app_event,const void * payload=nullptr,trace_payload_as_extra_func payload_as_extra=nullptr)256 end_event(struct intel_ds_queue *queue, uint64_t ts_ns,
257 enum intel_ds_queue_stage stage_id,
258 uint32_t submission_id,
259 uint16_t tracepoint_idx,
260 const char *app_event,
261 const void* payload = nullptr,
262 trace_payload_as_extra_func payload_as_extra = nullptr)
263 {
264 struct intel_ds_device *device = queue->device;
265
266 /* If we haven't managed to calibrate the alignment between GPU and CPU
267 * timestamps yet, then skip this trace, otherwise perfetto won't know
268 * what to do with it.
269 */
270 if (!device->sync_gpu_ts)
271 return;
272
273 if (queue->stages[stage_id].level == 0)
274 return;
275
276 uint32_t level = --queue->stages[stage_id].level;
277 struct intel_ds_stage *stage = &queue->stages[stage_id];
278 uint64_t start_ns = stage->start_ns[level];
279
280 if (!start_ns)
281 return;
282
283
284 IntelRenderpassDataSource::Trace([=](IntelRenderpassDataSource::TraceContext tctx) {
285 if (auto state = tctx.GetIncrementalState(); state->was_cleared) {
286 send_descriptors(tctx, queue->device);
287 state->was_cleared = false;
288 }
289
290 sync_timestamp(tctx, queue->device);
291
292 uint64_t evt_id = device->event_id++;
293
294 /* If this is an application event, we might need to generate a new
295 * stage_iid if not already seen. Otherwise, it's a driver event and we
296 * have use the internal stage_iid.
297 */
298 uint64_t stage_iid = app_event ?
299 tctx.GetDataSourceLocked()->debug_marker_stage(tctx, app_event) :
300 device->tracepoint_iids[tracepoint_idx];
301
302 auto packet = tctx.NewTracePacket();
303
304 packet->set_timestamp(start_ns);
305 packet->set_timestamp_clock_id(queue->device->gpu_clock_id);
306
307 assert(ts_ns >= start_ns);
308
309 auto event = packet->set_gpu_render_stage_event();
310 event->set_gpu_id(queue->device->gpu_id);
311
312 event->set_hw_queue_iid(stage->queue_iid);
313 event->set_stage_iid(stage_iid);
314 event->set_context(queue->device->iid);
315 event->set_event_id(evt_id);
316 event->set_duration(ts_ns - start_ns);
317 event->set_submission_id(submission_id);
318
319 if (payload && payload_as_extra) {
320 payload_as_extra(event, payload);
321 }
322 });
323
324 stage->start_ns[level] = 0;
325 }
326
327 static void
custom_trace_payload_as_extra_end_stall(perfetto::protos::pbzero::GpuRenderStageEvent * event,const struct trace_intel_end_stall * payload)328 custom_trace_payload_as_extra_end_stall(perfetto::protos::pbzero::GpuRenderStageEvent *event,
329 const struct trace_intel_end_stall *payload)
330 {
331 char buf[256];
332
333 {
334 auto data = event->add_extra_data();
335 data->set_name("stall_reason");
336
337 snprintf(buf, sizeof(buf), "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s : %s",
338 (payload->flags & INTEL_DS_DEPTH_CACHE_FLUSH_BIT) ? "+depth_flush" : "",
339 (payload->flags & INTEL_DS_DATA_CACHE_FLUSH_BIT) ? "+dc_flush" : "",
340 (payload->flags & INTEL_DS_HDC_PIPELINE_FLUSH_BIT) ? "+hdc_flush" : "",
341 (payload->flags & INTEL_DS_RENDER_TARGET_CACHE_FLUSH_BIT) ? "+rt_flush" : "",
342 (payload->flags & INTEL_DS_TILE_CACHE_FLUSH_BIT) ? "+tile_flush" : "",
343 (payload->flags & INTEL_DS_STATE_CACHE_INVALIDATE_BIT) ? "+state_inv" : "",
344 (payload->flags & INTEL_DS_CONST_CACHE_INVALIDATE_BIT) ? "+const_inv" : "",
345 (payload->flags & INTEL_DS_VF_CACHE_INVALIDATE_BIT) ? "+vf_inv" : "",
346 (payload->flags & INTEL_DS_TEXTURE_CACHE_INVALIDATE_BIT) ? "+tex_inv" : "",
347 (payload->flags & INTEL_DS_INST_CACHE_INVALIDATE_BIT) ? "+inst_inv" : "",
348 (payload->flags & INTEL_DS_STALL_AT_SCOREBOARD_BIT) ? "+pb_stall" : "",
349 (payload->flags & INTEL_DS_DEPTH_STALL_BIT) ? "+depth_stall" : "",
350 (payload->flags & INTEL_DS_HDC_PIPELINE_FLUSH_BIT) ? "+hdc_flush" : "",
351 (payload->flags & INTEL_DS_CS_STALL_BIT) ? "+cs_stall" : "",
352 (payload->flags & INTEL_DS_UNTYPED_DATAPORT_CACHE_FLUSH_BIT) ? "+udp_flush" : "",
353 (payload->flags & INTEL_DS_END_OF_PIPE_BIT) ? "+eop" : "",
354 (payload->flags & INTEL_DS_CCS_CACHE_FLUSH_BIT) ? "+ccs_flush" : "",
355 payload->reason ? payload->reason : "unknown");
356
357 assert(strlen(buf) > 0);
358
359 data->set_value(buf);
360 }
361 }
362
363 #endif /* HAVE_PERFETTO */
364
365 #ifdef __cplusplus
366 extern "C" {
367 #endif
368
369 #ifdef HAVE_PERFETTO
370
371 /*
372 * Trace callbacks, called from u_trace once the timestamps from GPU have been
373 * collected.
374 */
375
376 #define CREATE_DUAL_EVENT_CALLBACK(event_name, stage) \
377 void \
378 intel_ds_begin_##event_name(struct intel_ds_device *device, \
379 uint64_t ts_ns, \
380 uint16_t tp_idx, \
381 const void *flush_data, \
382 const struct trace_intel_begin_##event_name *payload) \
383 { \
384 const struct intel_ds_flush_data *flush = \
385 (const struct intel_ds_flush_data *) flush_data; \
386 begin_event(flush->queue, ts_ns, stage); \
387 } \
388 \
389 void \
390 intel_ds_end_##event_name(struct intel_ds_device *device, \
391 uint64_t ts_ns, \
392 uint16_t tp_idx, \
393 const void *flush_data, \
394 const struct trace_intel_end_##event_name *payload) \
395 { \
396 const struct intel_ds_flush_data *flush = \
397 (const struct intel_ds_flush_data *) flush_data; \
398 end_event(flush->queue, ts_ns, stage, flush->submission_id, \
399 tp_idx, NULL, payload, \
400 (trace_payload_as_extra_func) \
401 &trace_payload_as_extra_intel_end_##event_name); \
402 } \
403
CREATE_DUAL_EVENT_CALLBACK(frame,INTEL_DS_QUEUE_STAGE_FRAME)404 CREATE_DUAL_EVENT_CALLBACK(frame, INTEL_DS_QUEUE_STAGE_FRAME)
405 CREATE_DUAL_EVENT_CALLBACK(batch, INTEL_DS_QUEUE_STAGE_CMD_BUFFER)
406 CREATE_DUAL_EVENT_CALLBACK(cmd_buffer, INTEL_DS_QUEUE_STAGE_CMD_BUFFER)
407 CREATE_DUAL_EVENT_CALLBACK(render_pass, INTEL_DS_QUEUE_STAGE_RENDER_PASS)
408 CREATE_DUAL_EVENT_CALLBACK(blorp, INTEL_DS_QUEUE_STAGE_BLORP)
409 CREATE_DUAL_EVENT_CALLBACK(draw, INTEL_DS_QUEUE_STAGE_DRAW)
410 CREATE_DUAL_EVENT_CALLBACK(draw_indexed, INTEL_DS_QUEUE_STAGE_DRAW)
411 CREATE_DUAL_EVENT_CALLBACK(draw_indexed_multi, INTEL_DS_QUEUE_STAGE_DRAW)
412 CREATE_DUAL_EVENT_CALLBACK(draw_indexed_indirect, INTEL_DS_QUEUE_STAGE_DRAW)
413 CREATE_DUAL_EVENT_CALLBACK(draw_multi, INTEL_DS_QUEUE_STAGE_DRAW)
414 CREATE_DUAL_EVENT_CALLBACK(draw_indirect, INTEL_DS_QUEUE_STAGE_DRAW)
415 CREATE_DUAL_EVENT_CALLBACK(draw_indirect_count, INTEL_DS_QUEUE_STAGE_DRAW)
416 CREATE_DUAL_EVENT_CALLBACK(draw_indirect_byte_count, INTEL_DS_QUEUE_STAGE_DRAW)
417 CREATE_DUAL_EVENT_CALLBACK(draw_indexed_indirect_count, INTEL_DS_QUEUE_STAGE_DRAW)
418 CREATE_DUAL_EVENT_CALLBACK(draw_mesh, INTEL_DS_QUEUE_STAGE_DRAW_MESH)
419 CREATE_DUAL_EVENT_CALLBACK(draw_mesh_indirect, INTEL_DS_QUEUE_STAGE_DRAW_MESH)
420 CREATE_DUAL_EVENT_CALLBACK(draw_mesh_indirect_count, INTEL_DS_QUEUE_STAGE_DRAW_MESH)
421 CREATE_DUAL_EVENT_CALLBACK(xfb, INTEL_DS_QUEUE_STAGE_CMD_BUFFER)
422 CREATE_DUAL_EVENT_CALLBACK(compute, INTEL_DS_QUEUE_STAGE_COMPUTE)
423 CREATE_DUAL_EVENT_CALLBACK(generate_draws, INTEL_DS_QUEUE_STAGE_INTERNAL_OPS)
424 CREATE_DUAL_EVENT_CALLBACK(generate_commands, INTEL_DS_QUEUE_STAGE_INTERNAL_OPS)
425 CREATE_DUAL_EVENT_CALLBACK(trace_copy, INTEL_DS_QUEUE_STAGE_INTERNAL_OPS)
426 CREATE_DUAL_EVENT_CALLBACK(trace_copy_cb, INTEL_DS_QUEUE_STAGE_INTERNAL_OPS)
427 CREATE_DUAL_EVENT_CALLBACK(query_clear_blorp, INTEL_DS_QUEUE_STAGE_INTERNAL_OPS)
428 CREATE_DUAL_EVENT_CALLBACK(query_clear_cs, INTEL_DS_QUEUE_STAGE_INTERNAL_OPS)
429 CREATE_DUAL_EVENT_CALLBACK(query_copy_cs, INTEL_DS_QUEUE_STAGE_INTERNAL_OPS)
430 CREATE_DUAL_EVENT_CALLBACK(query_copy_shader, INTEL_DS_QUEUE_STAGE_INTERNAL_OPS)
431 CREATE_DUAL_EVENT_CALLBACK(write_buffer_marker, INTEL_DS_QUEUE_STAGE_CMD_BUFFER)
432 CREATE_DUAL_EVENT_CALLBACK(rays, INTEL_DS_QUEUE_STAGE_RT)
433 CREATE_DUAL_EVENT_CALLBACK(as_build, INTEL_DS_QUEUE_STAGE_AS)
434
435 void
436 intel_ds_begin_cmd_buffer_annotation(struct intel_ds_device *device,
437 uint64_t ts_ns,
438 uint16_t tp_idx,
439 const void *flush_data,
440 const struct trace_intel_begin_cmd_buffer_annotation *payload)
441 {
442 const struct intel_ds_flush_data *flush =
443 (const struct intel_ds_flush_data *) flush_data;
444 begin_event(flush->queue, ts_ns, INTEL_DS_QUEUE_STAGE_CMD_BUFFER);
445 }
446
447 void
intel_ds_end_cmd_buffer_annotation(struct intel_ds_device * device,uint64_t ts_ns,uint16_t tp_idx,const void * flush_data,const struct trace_intel_end_cmd_buffer_annotation * payload)448 intel_ds_end_cmd_buffer_annotation(struct intel_ds_device *device,
449 uint64_t ts_ns,
450 uint16_t tp_idx,
451 const void *flush_data,
452 const struct trace_intel_end_cmd_buffer_annotation *payload)
453 {
454 const struct intel_ds_flush_data *flush =
455 (const struct intel_ds_flush_data *) flush_data;
456 end_event(flush->queue, ts_ns, INTEL_DS_QUEUE_STAGE_CMD_BUFFER,
457 flush->submission_id, tp_idx, payload->str, NULL, NULL);
458 }
459
460 void
intel_ds_begin_queue_annotation(struct intel_ds_device * device,uint64_t ts_ns,uint16_t tp_idx,const void * flush_data,const struct trace_intel_begin_queue_annotation * payload)461 intel_ds_begin_queue_annotation(struct intel_ds_device *device,
462 uint64_t ts_ns,
463 uint16_t tp_idx,
464 const void *flush_data,
465 const struct trace_intel_begin_queue_annotation *payload)
466 {
467 const struct intel_ds_flush_data *flush =
468 (const struct intel_ds_flush_data *) flush_data;
469 begin_event(flush->queue, ts_ns, INTEL_DS_QUEUE_STAGE_QUEUE);
470 }
471
472 void
intel_ds_end_queue_annotation(struct intel_ds_device * device,uint64_t ts_ns,uint16_t tp_idx,const void * flush_data,const struct trace_intel_end_queue_annotation * payload)473 intel_ds_end_queue_annotation(struct intel_ds_device *device,
474 uint64_t ts_ns,
475 uint16_t tp_idx,
476 const void *flush_data,
477 const struct trace_intel_end_queue_annotation *payload)
478 {
479 const struct intel_ds_flush_data *flush =
480 (const struct intel_ds_flush_data *) flush_data;
481 end_event(flush->queue, ts_ns, INTEL_DS_QUEUE_STAGE_QUEUE,
482 flush->submission_id, tp_idx, payload->str, NULL, NULL);
483 }
484
485 void
intel_ds_begin_stall(struct intel_ds_device * device,uint64_t ts_ns,uint16_t tp_idx,const void * flush_data,const struct trace_intel_begin_stall * payload)486 intel_ds_begin_stall(struct intel_ds_device *device,
487 uint64_t ts_ns,
488 uint16_t tp_idx,
489 const void *flush_data,
490 const struct trace_intel_begin_stall *payload)
491 {
492 const struct intel_ds_flush_data *flush =
493 (const struct intel_ds_flush_data *) flush_data;
494 begin_event(flush->queue, ts_ns, INTEL_DS_QUEUE_STAGE_STALL);
495 }
496
497 void
intel_ds_end_stall(struct intel_ds_device * device,uint64_t ts_ns,uint16_t tp_idx,const void * flush_data,const struct trace_intel_end_stall * payload)498 intel_ds_end_stall(struct intel_ds_device *device,
499 uint64_t ts_ns,
500 uint16_t tp_idx,
501 const void *flush_data,
502 const struct trace_intel_end_stall *payload)
503 {
504 const struct intel_ds_flush_data *flush =
505 (const struct intel_ds_flush_data *) flush_data;
506 end_event(flush->queue, ts_ns, INTEL_DS_QUEUE_STAGE_STALL,
507 flush->submission_id, tp_idx, NULL, payload,
508 (trace_payload_as_extra_func)custom_trace_payload_as_extra_end_stall);
509 }
510
511 uint64_t
intel_ds_begin_submit(struct intel_ds_queue * queue)512 intel_ds_begin_submit(struct intel_ds_queue *queue)
513 {
514 return perfetto::base::GetBootTimeNs().count();
515 }
516
517 void
intel_ds_end_submit(struct intel_ds_queue * queue,uint64_t start_ts)518 intel_ds_end_submit(struct intel_ds_queue *queue,
519 uint64_t start_ts)
520 {
521 if (!u_trace_should_process(&queue->device->trace_context)) {
522 queue->device->sync_gpu_ts = 0;
523 queue->device->next_clock_sync_ns = 0;
524 return;
525 }
526
527 uint64_t end_ts = perfetto::base::GetBootTimeNs().count();
528 uint32_t submission_id = queue->submission_id++;
529
530 IntelRenderpassDataSource::Trace([=](IntelRenderpassDataSource::TraceContext tctx) {
531 if (auto state = tctx.GetIncrementalState(); state->was_cleared) {
532 send_descriptors(tctx, queue->device);
533 state->was_cleared = false;
534 }
535
536 sync_timestamp(tctx, queue->device);
537
538 auto packet = tctx.NewTracePacket();
539
540 packet->set_timestamp(start_ts);
541
542 auto event = packet->set_vulkan_api_event();
543 auto submit = event->set_vk_queue_submit();
544
545 // submit->set_pid(os_get_pid());
546 // submit->set_tid(os_get_tid());
547 submit->set_duration_ns(end_ts - start_ts);
548 submit->set_vk_queue((uintptr_t) queue);
549 submit->set_submission_id(submission_id);
550 });
551 }
552
553 #endif /* HAVE_PERFETTO */
554
555 static void
intel_driver_ds_init_once(void)556 intel_driver_ds_init_once(void)
557 {
558 #ifdef HAVE_PERFETTO
559 util_perfetto_init();
560 perfetto::DataSourceDescriptor dsd;
561 dsd.set_name("gpu.renderstages.intel");
562 IntelRenderpassDataSource::Register(dsd);
563 #endif
564 }
565
566 static once_flag intel_driver_ds_once_flag = ONCE_FLAG_INIT;
567 static uint64_t iid = 1;
568
get_iid()569 static uint64_t get_iid()
570 {
571 return iid++;
572 }
573
574 void
intel_driver_ds_init(void)575 intel_driver_ds_init(void)
576 {
577 call_once(&intel_driver_ds_once_flag,
578 intel_driver_ds_init_once);
579 intel_gpu_tracepoint_config_variable();
580 }
581
582 void
intel_ds_device_init(struct intel_ds_device * device,const struct intel_device_info * devinfo,int drm_fd,uint32_t gpu_id,enum intel_ds_api api)583 intel_ds_device_init(struct intel_ds_device *device,
584 const struct intel_device_info *devinfo,
585 int drm_fd,
586 uint32_t gpu_id,
587 enum intel_ds_api api)
588 {
589 memset(device, 0, sizeof(*device));
590
591 device->gpu_id = gpu_id;
592 device->gpu_clock_id = intel_pps_clock_id(gpu_id);
593 device->fd = drm_fd;
594 device->info = *devinfo;
595 device->iid = get_iid();
596 device->api = api;
597
598 #ifdef HAVE_PERFETTO
599 assert(ARRAY_SIZE(intel_tracepoint_names) < ARRAY_SIZE(device->tracepoint_iids));
600 for (unsigned i = 0; i < ARRAY_SIZE(intel_tracepoint_names); i++)
601 device->tracepoint_iids[i] = get_iid();
602 #endif
603
604 list_inithead(&device->queues);
605 simple_mtx_init(&device->trace_context_mutex, mtx_plain);
606 }
607
608 void
intel_ds_device_fini(struct intel_ds_device * device)609 intel_ds_device_fini(struct intel_ds_device *device)
610 {
611 u_trace_context_fini(&device->trace_context);
612 simple_mtx_destroy(&device->trace_context_mutex);
613 }
614
615 struct intel_ds_queue *
intel_ds_device_init_queue(struct intel_ds_device * device,struct intel_ds_queue * queue,const char * fmt_name,...)616 intel_ds_device_init_queue(struct intel_ds_device *device,
617 struct intel_ds_queue *queue,
618 const char *fmt_name,
619 ...)
620 {
621 va_list ap;
622
623 memset(queue, 0, sizeof(*queue));
624
625 queue->device = device;
626
627 va_start(ap, fmt_name);
628 vsnprintf(queue->name, sizeof(queue->name), fmt_name, ap);
629 va_end(ap);
630
631 for (unsigned s = 0; s < INTEL_DS_QUEUE_STAGE_N_STAGES; s++) {
632 queue->stages[s].queue_iid = get_iid();
633 }
634
635 list_add(&queue->link, &device->queues);
636
637 return queue;
638 }
639
intel_ds_flush_data_init(struct intel_ds_flush_data * data,struct intel_ds_queue * queue,uint64_t submission_id)640 void intel_ds_flush_data_init(struct intel_ds_flush_data *data,
641 struct intel_ds_queue *queue,
642 uint64_t submission_id)
643 {
644 memset(data, 0, sizeof(*data));
645
646 data->queue = queue;
647 data->submission_id = submission_id;
648
649 u_trace_init(&data->trace, &queue->device->trace_context);
650 }
651
intel_ds_flush_data_fini(struct intel_ds_flush_data * data)652 void intel_ds_flush_data_fini(struct intel_ds_flush_data *data)
653 {
654 u_trace_fini(&data->trace);
655 }
656
intel_ds_queue_flush_data(struct intel_ds_queue * queue,struct u_trace * ut,struct intel_ds_flush_data * data,bool free_data)657 void intel_ds_queue_flush_data(struct intel_ds_queue *queue,
658 struct u_trace *ut,
659 struct intel_ds_flush_data *data,
660 bool free_data)
661 {
662 simple_mtx_lock(&queue->device->trace_context_mutex);
663 u_trace_flush(ut, data, free_data);
664 simple_mtx_unlock(&queue->device->trace_context_mutex);
665 }
666
intel_ds_device_process(struct intel_ds_device * device,bool eof)667 void intel_ds_device_process(struct intel_ds_device *device,
668 bool eof)
669 {
670 simple_mtx_lock(&device->trace_context_mutex);
671 u_trace_context_process(&device->trace_context, eof);
672 simple_mtx_unlock(&device->trace_context_mutex);
673 }
674
675 #ifdef __cplusplus
676 }
677 #endif
678