2 * Copyright © 2013 Intel Corporation
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:
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
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file brw_performance_query.c
27 * Implementation of the GL_INTEL_performance_query extension.
29 * Currently there are two possible counter sources exposed here:
31 * On Gen6+ hardware we have numerous 64bit Pipeline Statistics Registers
32 * that we can snapshot at the beginning and end of a query.
34 * On Gen7.5+ we have Observability Architecture counters which are
35 * covered in separate document from the rest of the PRMs. It is available at:
36 * https://01.org/linuxgraphics/documentation/driver-documentation-prms
37 * => 2013 Intel Core Processor Family => Observability Performance Counters
38 * (This one volume covers Sandybridge, Ivybridge, Baytrail, and Haswell,
39 * though notably we currently only support OA counters for Haswell+)
44 /* put before sys/types.h to silence glibc warnings */
46 #include <sys/mkdev.h>
48 #ifdef MAJOR_IN_SYSMACROS
49 #include <sys/sysmacros.h>
51 #include <sys/types.h>
55 #include <sys/ioctl.h>
58 #include "drm-uapi/i915_drm.h"
60 #include "main/hash.h"
61 #include "main/macros.h"
62 #include "main/mtypes.h"
63 #include "main/performance_query.h"
65 #include "util/bitset.h"
66 #include "util/ralloc.h"
67 #include "util/hash_table.h"
68 #include "util/list.h"
69 #include "util/u_math.h"
71 #include "brw_context.h"
72 #include "brw_defines.h"
73 #include "intel_batchbuffer.h"
75 #include "perf/gen_perf.h"
76 #include "perf/gen_perf_mdapi.h"
78 #define FILE_DEBUG_FLAG DEBUG_PERFMON
80 #define OAREPORT_REASON_MASK 0x3f
81 #define OAREPORT_REASON_SHIFT 19
82 #define OAREPORT_REASON_TIMER (1<<0)
83 #define OAREPORT_REASON_TRIGGER1 (1<<1)
84 #define OAREPORT_REASON_TRIGGER2 (1<<2)
85 #define OAREPORT_REASON_CTX_SWITCH (1<<3)
86 #define OAREPORT_REASON_GO_TRANSITION (1<<4)
88 struct brw_perf_query_object
{
89 struct gl_perf_query_object base
;
90 struct gen_perf_query_object
*query
;
93 /** Downcasting convenience macro. */
94 static inline struct brw_perf_query_object
*
95 brw_perf_query(struct gl_perf_query_object
*o
)
97 return (struct brw_perf_query_object
*) o
;
100 #define MI_RPC_BO_SIZE 4096
101 #define MI_RPC_BO_END_OFFSET_BYTES (MI_RPC_BO_SIZE / 2)
102 #define MI_FREQ_START_OFFSET_BYTES (3072)
103 #define MI_FREQ_END_OFFSET_BYTES (3076)
105 /******************************************************************************/
108 brw_is_perf_query_ready(struct gl_context
*ctx
,
109 struct gl_perf_query_object
*o
);
112 dump_perf_query_callback(GLuint id
, void *query_void
, void *brw_void
)
114 struct gl_context
*ctx
= brw_void
;
115 struct gl_perf_query_object
*o
= query_void
;
116 struct brw_perf_query_object
* brw_query
= brw_perf_query(o
);
117 struct gen_perf_query_object
*obj
= brw_query
->query
;
119 switch (obj
->queryinfo
->kind
) {
120 case GEN_PERF_QUERY_TYPE_OA
:
121 case GEN_PERF_QUERY_TYPE_RAW
:
122 DBG("%4d: %-6s %-8s BO: %-4s OA data: %-10s %-15s\n",
124 o
->Used
? "Dirty," : "New,",
125 o
->Active
? "Active," : (o
->Ready
? "Ready," : "Pending,"),
126 obj
->oa
.bo
? "yes," : "no,",
127 brw_is_perf_query_ready(ctx
, o
) ? "ready," : "not ready,",
128 obj
->oa
.results_accumulated
? "accumulated" : "not accumulated");
130 case GEN_PERF_QUERY_TYPE_PIPELINE
:
131 DBG("%4d: %-6s %-8s BO: %-4s\n",
133 o
->Used
? "Dirty," : "New,",
134 o
->Active
? "Active," : (o
->Ready
? "Ready," : "Pending,"),
135 obj
->pipeline_stats
.bo
? "yes" : "no");
138 unreachable("Unknown query type");
144 dump_perf_queries(struct brw_context
*brw
)
146 struct gl_context
*ctx
= &brw
->ctx
;
147 DBG("Queries: (Open queries = %d, OA users = %d)\n",
148 brw
->perf_ctx
.n_active_oa_queries
, brw
->perf_ctx
.n_oa_users
);
149 _mesa_HashWalk(ctx
->PerfQuery
.Objects
, dump_perf_query_callback
, brw
);
153 * Driver hook for glGetPerfQueryInfoINTEL().
156 brw_get_perf_query_info(struct gl_context
*ctx
,
157 unsigned query_index
,
163 struct brw_context
*brw
= brw_context(ctx
);
164 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
165 const struct gen_perf_query_info
*query
=
166 &perf_ctx
->perf
->queries
[query_index
];
169 *data_size
= query
->data_size
;
170 *n_counters
= query
->n_counters
;
172 switch (query
->kind
) {
173 case GEN_PERF_QUERY_TYPE_OA
:
174 case GEN_PERF_QUERY_TYPE_RAW
:
175 *n_active
= perf_ctx
->n_active_oa_queries
;
178 case GEN_PERF_QUERY_TYPE_PIPELINE
:
179 *n_active
= perf_ctx
->n_active_pipeline_stats_queries
;
183 unreachable("Unknown query type");
189 gen_counter_type_enum_to_gl_type(enum gen_perf_counter_type type
)
192 case GEN_PERF_COUNTER_TYPE_EVENT
: return GL_PERFQUERY_COUNTER_EVENT_INTEL
;
193 case GEN_PERF_COUNTER_TYPE_DURATION_NORM
: return GL_PERFQUERY_COUNTER_DURATION_NORM_INTEL
;
194 case GEN_PERF_COUNTER_TYPE_DURATION_RAW
: return GL_PERFQUERY_COUNTER_DURATION_RAW_INTEL
;
195 case GEN_PERF_COUNTER_TYPE_THROUGHPUT
: return GL_PERFQUERY_COUNTER_THROUGHPUT_INTEL
;
196 case GEN_PERF_COUNTER_TYPE_RAW
: return GL_PERFQUERY_COUNTER_RAW_INTEL
;
197 case GEN_PERF_COUNTER_TYPE_TIMESTAMP
: return GL_PERFQUERY_COUNTER_TIMESTAMP_INTEL
;
199 unreachable("Unknown counter type");
204 gen_counter_data_type_to_gl_type(enum gen_perf_counter_data_type type
)
207 case GEN_PERF_COUNTER_DATA_TYPE_BOOL32
: return GL_PERFQUERY_COUNTER_DATA_BOOL32_INTEL
;
208 case GEN_PERF_COUNTER_DATA_TYPE_UINT32
: return GL_PERFQUERY_COUNTER_DATA_UINT32_INTEL
;
209 case GEN_PERF_COUNTER_DATA_TYPE_UINT64
: return GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL
;
210 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT
: return GL_PERFQUERY_COUNTER_DATA_FLOAT_INTEL
;
211 case GEN_PERF_COUNTER_DATA_TYPE_DOUBLE
: return GL_PERFQUERY_COUNTER_DATA_DOUBLE_INTEL
;
213 unreachable("Unknown counter data type");
218 * Driver hook for glGetPerfCounterInfoINTEL().
221 brw_get_perf_counter_info(struct gl_context
*ctx
,
222 unsigned query_index
,
223 unsigned counter_index
,
229 GLuint
*data_type_enum
,
232 struct brw_context
*brw
= brw_context(ctx
);
233 const struct gen_perf_query_info
*query
=
234 &brw
->perf_ctx
.perf
->queries
[query_index
];
235 const struct gen_perf_query_counter
*counter
=
236 &query
->counters
[counter_index
];
238 *name
= counter
->name
;
239 *desc
= counter
->desc
;
240 *offset
= counter
->offset
;
241 *data_size
= gen_perf_query_counter_get_size(counter
);
242 *type_enum
= gen_counter_type_enum_to_gl_type(counter
->type
);
243 *data_type_enum
= gen_counter_data_type_to_gl_type(counter
->data_type
);
244 *raw_max
= counter
->raw_max
;
248 * Add a query to the global list of "unaccumulated queries."
250 * Queries are tracked here until all the associated OA reports have
251 * been accumulated via accumulate_oa_reports() after the end
252 * MI_REPORT_PERF_COUNT has landed in query->oa.bo.
255 add_to_unaccumulated_query_list(struct brw_context
*brw
,
256 struct gen_perf_query_object
*obj
)
258 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
259 if (perf_ctx
->unaccumulated_elements
>=
260 perf_ctx
->unaccumulated_array_size
)
262 perf_ctx
->unaccumulated_array_size
*= 1.5;
263 perf_ctx
->unaccumulated
=
264 reralloc(brw
, perf_ctx
->unaccumulated
,
265 struct gen_perf_query_object
*,
266 perf_ctx
->unaccumulated_array_size
);
269 perf_ctx
->unaccumulated
[perf_ctx
->unaccumulated_elements
++] = obj
;
273 * Remove a query from the global list of unaccumulated queries once
274 * after successfully accumulating the OA reports associated with the
275 * query in accumulate_oa_reports() or when discarding unwanted query
279 drop_from_unaccumulated_query_list(struct brw_context
*brw
,
280 struct gen_perf_query_object
*obj
)
282 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
283 for (int i
= 0; i
< perf_ctx
->unaccumulated_elements
; i
++) {
284 if (perf_ctx
->unaccumulated
[i
] == obj
) {
285 int last_elt
= --perf_ctx
->unaccumulated_elements
;
288 perf_ctx
->unaccumulated
[i
] = NULL
;
290 perf_ctx
->unaccumulated
[i
] =
291 perf_ctx
->unaccumulated
[last_elt
];
298 /* Drop our samples_head reference so that associated periodic
299 * sample data buffers can potentially be reaped if they aren't
300 * referenced by any other queries...
303 struct oa_sample_buf
*buf
=
304 exec_node_data(struct oa_sample_buf
, obj
->oa
.samples_head
, link
);
306 assert(buf
->refcount
> 0);
309 obj
->oa
.samples_head
= NULL
;
311 gen_perf_reap_old_sample_buffers(&brw
->perf_ctx
);
314 /* In general if we see anything spurious while accumulating results,
315 * we don't try and continue accumulating the current query, hoping
316 * for the best, we scrap anything outstanding, and then hope for the
317 * best with new queries.
320 discard_all_queries(struct brw_context
*brw
)
322 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
323 while (perf_ctx
->unaccumulated_elements
) {
324 struct gen_perf_query_object
*obj
= perf_ctx
->unaccumulated
[0];
326 obj
->oa
.results_accumulated
= true;
327 drop_from_unaccumulated_query_list(brw
, perf_ctx
->unaccumulated
[0]);
329 gen_perf_dec_n_users(perf_ctx
);
334 OA_READ_STATUS_ERROR
,
335 OA_READ_STATUS_UNFINISHED
,
336 OA_READ_STATUS_FINISHED
,
339 static enum OaReadStatus
340 read_oa_samples_until(struct brw_context
*brw
,
341 uint32_t start_timestamp
,
342 uint32_t end_timestamp
)
344 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
345 struct exec_node
*tail_node
=
346 exec_list_get_tail(&perf_ctx
->sample_buffers
);
347 struct oa_sample_buf
*tail_buf
=
348 exec_node_data(struct oa_sample_buf
, tail_node
, link
);
349 uint32_t last_timestamp
= tail_buf
->last_timestamp
;
352 struct oa_sample_buf
*buf
= gen_perf_get_free_sample_buf(perf_ctx
);
356 while ((len
= read(perf_ctx
->oa_stream_fd
, buf
->buf
,
357 sizeof(buf
->buf
))) < 0 && errno
== EINTR
)
361 exec_list_push_tail(&perf_ctx
->free_sample_buffers
, &buf
->link
);
365 return ((last_timestamp
- start_timestamp
) >=
366 (end_timestamp
- start_timestamp
)) ?
367 OA_READ_STATUS_FINISHED
:
368 OA_READ_STATUS_UNFINISHED
;
370 DBG("Error reading i915 perf samples: %m\n");
373 DBG("Spurious EOF reading i915 perf samples\n");
375 return OA_READ_STATUS_ERROR
;
379 exec_list_push_tail(&perf_ctx
->sample_buffers
, &buf
->link
);
381 /* Go through the reports and update the last timestamp. */
383 while (offset
< buf
->len
) {
384 const struct drm_i915_perf_record_header
*header
=
385 (const struct drm_i915_perf_record_header
*) &buf
->buf
[offset
];
386 uint32_t *report
= (uint32_t *) (header
+ 1);
388 if (header
->type
== DRM_I915_PERF_RECORD_SAMPLE
)
389 last_timestamp
= report
[1];
391 offset
+= header
->size
;
394 buf
->last_timestamp
= last_timestamp
;
397 unreachable("not reached");
398 return OA_READ_STATUS_ERROR
;
402 * Try to read all the reports until either the delimiting timestamp
403 * or an error arises.
406 read_oa_samples_for_query(struct brw_context
*brw
,
407 struct gen_perf_query_object
*obj
)
413 /* We need the MI_REPORT_PERF_COUNT to land before we can start
415 assert(!brw_batch_references(&brw
->batch
, obj
->oa
.bo
) &&
416 !brw_bo_busy(obj
->oa
.bo
));
418 /* Map the BO once here and let accumulate_oa_reports() unmap
420 if (obj
->oa
.map
== NULL
)
421 obj
->oa
.map
= brw_bo_map(brw
, obj
->oa
.bo
, MAP_READ
);
423 start
= last
= obj
->oa
.map
;
424 end
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
426 if (start
[0] != obj
->oa
.begin_report_id
) {
427 DBG("Spurious start report id=%"PRIu32
"\n", start
[0]);
430 if (end
[0] != (obj
->oa
.begin_report_id
+ 1)) {
431 DBG("Spurious end report id=%"PRIu32
"\n", end
[0]);
435 /* Read the reports until the end timestamp. */
436 switch (read_oa_samples_until(brw
, start
[1], end
[1])) {
437 case OA_READ_STATUS_ERROR
:
438 /* Fallthrough and let accumulate_oa_reports() deal with the
440 case OA_READ_STATUS_FINISHED
:
442 case OA_READ_STATUS_UNFINISHED
:
446 unreachable("invalid read status");
451 * Accumulate raw OA counter values based on deltas between pairs of
454 * Accumulation starts from the first report captured via
455 * MI_REPORT_PERF_COUNT (MI_RPC) by brw_begin_perf_query() until the
456 * last MI_RPC report requested by brw_end_perf_query(). Between these
457 * two reports there may also some number of periodically sampled OA
458 * reports collected via the i915 perf interface - depending on the
459 * duration of the query.
461 * These periodic snapshots help to ensure we handle counter overflow
462 * correctly by being frequent enough to ensure we don't miss multiple
463 * overflows of a counter between snapshots. For Gen8+ the i915 perf
464 * snapshots provide the extra context-switch reports that let us
465 * subtract out the progress of counters associated with other
466 * contexts running on the system.
469 accumulate_oa_reports(struct brw_context
*brw
,
470 struct brw_perf_query_object
*brw_query
)
472 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
473 struct gen_perf_query_object
*obj
= brw_query
->query
;
474 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
478 struct exec_node
*first_samples_node
;
480 int out_duration
= 0;
482 assert(brw_query
->base
.Ready
);
483 assert(obj
->oa
.map
!= NULL
);
485 start
= last
= obj
->oa
.map
;
486 end
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
488 if (start
[0] != obj
->oa
.begin_report_id
) {
489 DBG("Spurious start report id=%"PRIu32
"\n", start
[0]);
492 if (end
[0] != (obj
->oa
.begin_report_id
+ 1)) {
493 DBG("Spurious end report id=%"PRIu32
"\n", end
[0]);
497 /* See if we have any periodic reports to accumulate too... */
499 /* N.B. The oa.samples_head was set when the query began and
500 * pointed to the tail of the perf_ctx->sample_buffers list at
501 * the time the query started. Since the buffer existed before the
502 * first MI_REPORT_PERF_COUNT command was emitted we therefore know
503 * that no data in this particular node's buffer can possibly be
504 * associated with the query - so skip ahead one...
506 first_samples_node
= obj
->oa
.samples_head
->next
;
508 foreach_list_typed_from(struct oa_sample_buf
, buf
, link
,
509 &brw
->perf_ctx
.sample_buffers
,
514 while (offset
< buf
->len
) {
515 const struct drm_i915_perf_record_header
*header
=
516 (const struct drm_i915_perf_record_header
*)(buf
->buf
+ offset
);
518 assert(header
->size
!= 0);
519 assert(header
->size
<= buf
->len
);
521 offset
+= header
->size
;
523 switch (header
->type
) {
524 case DRM_I915_PERF_RECORD_SAMPLE
: {
525 uint32_t *report
= (uint32_t *)(header
+ 1);
528 /* Ignore reports that come before the start marker.
529 * (Note: takes care to allow overflow of 32bit timestamps)
531 if (gen_device_info_timebase_scale(devinfo
,
532 report
[1] - start
[1]) > 5000000000) {
536 /* Ignore reports that come after the end marker.
537 * (Note: takes care to allow overflow of 32bit timestamps)
539 if (gen_device_info_timebase_scale(devinfo
,
540 report
[1] - end
[1]) <= 5000000000) {
544 /* For Gen8+ since the counters continue while other
545 * contexts are running we need to discount any unrelated
546 * deltas. The hardware automatically generates a report
547 * on context switch which gives us a new reference point
548 * to continuing adding deltas from.
550 * For Haswell we can rely on the HW to stop the progress
551 * of OA counters while any other context is acctive.
553 if (devinfo
->gen
>= 8) {
554 if (in_ctx
&& report
[2] != obj
->oa
.result
.hw_id
) {
555 DBG("i915 perf: Switch AWAY (observed by ID change)\n");
558 } else if (in_ctx
== false && report
[2] == obj
->oa
.result
.hw_id
) {
559 DBG("i915 perf: Switch TO\n");
562 /* From experimentation in IGT, we found that the OA unit
563 * might label some report as "idle" (using an invalid
564 * context ID), right after a report for a given context.
565 * Deltas generated by those reports actually belong to the
566 * previous context, even though they're not labelled as
569 * We didn't *really* Switch AWAY in the case that we e.g.
570 * saw a single periodic report while idle...
572 if (out_duration
>= 1)
575 assert(report
[2] == obj
->oa
.result
.hw_id
);
576 DBG("i915 perf: Continuation IN\n");
578 assert(report
[2] != obj
->oa
.result
.hw_id
);
579 DBG("i915 perf: Continuation OUT\n");
586 gen_perf_query_result_accumulate(&obj
->oa
.result
, obj
->queryinfo
,
595 case DRM_I915_PERF_RECORD_OA_BUFFER_LOST
:
596 DBG("i915 perf: OA error: all reports lost\n");
598 case DRM_I915_PERF_RECORD_OA_REPORT_LOST
:
599 DBG("i915 perf: OA report lost\n");
607 gen_perf_query_result_accumulate(&obj
->oa
.result
, obj
->queryinfo
,
610 DBG("Marking %d accumulated - results gathered\n", brw_query
->base
.Id
);
612 obj
->oa
.results_accumulated
= true;
613 drop_from_unaccumulated_query_list(brw
, obj
);
614 gen_perf_dec_n_users(perf_ctx
);
620 discard_all_queries(brw
);
623 /******************************************************************************/
626 capture_frequency_stat_register(struct brw_context
*brw
,
630 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
632 if (devinfo
->gen
>= 7 && devinfo
->gen
<= 8 &&
633 !devinfo
->is_baytrail
&& !devinfo
->is_cherryview
) {
634 brw_store_register_mem32(brw
, bo
, GEN7_RPSTAT1
, bo_offset
);
635 } else if (devinfo
->gen
>= 9) {
636 brw_store_register_mem32(brw
, bo
, GEN9_RPSTAT0
, bo_offset
);
641 * Driver hook for glBeginPerfQueryINTEL().
644 brw_begin_perf_query(struct gl_context
*ctx
,
645 struct gl_perf_query_object
*o
)
647 struct brw_context
*brw
= brw_context(ctx
);
648 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
649 struct gen_perf_query_object
*obj
= brw_query
->query
;
650 const struct gen_perf_query_info
*query
= obj
->queryinfo
;
651 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
652 struct gen_perf_config
*perf_cfg
= perf_ctx
->perf
;
654 /* We can assume the frontend hides mistaken attempts to Begin a
655 * query object multiple times before its End. Similarly if an
656 * application reuses a query object before results have arrived
657 * the frontend will wait for prior results so we don't need
658 * to support abandoning in-flight results.
661 assert(!o
->Used
|| o
->Ready
); /* no in-flight query to worry about */
663 DBG("Begin(%d)\n", o
->Id
);
665 /* XXX: We have to consider that the command parser unit that parses batch
666 * buffer commands and is used to capture begin/end counter snapshots isn't
667 * implicitly synchronized with what's currently running across other GPU
668 * units (such as the EUs running shaders) that the performance counters are
671 * The intention of performance queries is to measure the work associated
672 * with commands between the begin/end delimiters and so for that to be the
673 * case we need to explicitly synchronize the parsing of commands to capture
674 * Begin/End counter snapshots with what's running across other parts of the
677 * When the command parser reaches a Begin marker it effectively needs to
678 * drain everything currently running on the GPU until the hardware is idle
679 * before capturing the first snapshot of counters - otherwise the results
680 * would also be measuring the effects of earlier commands.
682 * When the command parser reaches an End marker it needs to stall until
683 * everything currently running on the GPU has finished before capturing the
684 * end snapshot - otherwise the results won't be a complete representation
687 * Theoretically there could be opportunities to minimize how much of the
688 * GPU pipeline is drained, or that we stall for, when we know what specific
689 * units the performance counters being queried relate to but we don't
690 * currently attempt to be clever here.
692 * Note: with our current simple approach here then for back-to-back queries
693 * we will redundantly emit duplicate commands to synchronize the command
694 * streamer with the rest of the GPU pipeline, but we assume that in HW the
695 * second synchronization is effectively a NOOP.
697 * N.B. The final results are based on deltas of counters between (inside)
698 * Begin/End markers so even though the total wall clock time of the
699 * workload is stretched by larger pipeline bubbles the bubbles themselves
700 * are generally invisible to the query results. Whether that's a good or a
701 * bad thing depends on the use case. For a lower real-time impact while
702 * capturing metrics then periodic sampling may be a better choice than
703 * INTEL_performance_query.
706 * This is our Begin synchronization point to drain current work on the
707 * GPU before we capture our first counter snapshot...
709 perf_cfg
->vtbl
.emit_mi_flush(brw
);
711 switch (query
->kind
) {
712 case GEN_PERF_QUERY_TYPE_OA
:
713 case GEN_PERF_QUERY_TYPE_RAW
: {
715 /* Opening an i915 perf stream implies exclusive access to the OA unit
716 * which will generate counter reports for a specific counter set with a
717 * specific layout/format so we can't begin any OA based queries that
718 * require a different counter set or format unless we get an opportunity
719 * to close the stream and open a new one...
721 uint64_t metric_id
= gen_perf_query_get_metric_id(perf_ctx
->perf
, query
);
723 if (perf_ctx
->oa_stream_fd
!= -1 &&
724 perf_ctx
->current_oa_metrics_set_id
!= metric_id
) {
726 if (perf_ctx
->n_oa_users
!= 0) {
727 DBG("WARNING: Begin(%d) failed already using perf config=%i/%"PRIu64
"\n",
728 o
->Id
, perf_ctx
->current_oa_metrics_set_id
, metric_id
);
731 gen_perf_close(perf_ctx
, query
);
734 /* If the OA counters aren't already on, enable them. */
736 if (perf_ctx
->oa_stream_fd
== -1) {
737 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
738 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
740 /* The period_exponent gives a sampling period as follows:
741 * sample_period = timestamp_period * 2^(period_exponent + 1)
743 * The timestamps increments every 80ns (HSW), ~52ns (GEN9LP) or
746 * The counter overflow period is derived from the EuActive counter
747 * which reads a counter that increments by the number of clock
748 * cycles multiplied by the number of EUs. It can be calculated as:
750 * 2^(number of bits in A counter) / (n_eus * max_gen_freq * 2)
752 * (E.g. 40 EUs @ 1GHz = ~53ms)
754 * We select a sampling period inferior to that overflow period to
755 * ensure we cannot see more than 1 counter overflow, otherwise we
756 * could loose information.
759 int a_counter_in_bits
= 32;
760 if (devinfo
->gen
>= 8)
761 a_counter_in_bits
= 40;
763 uint64_t overflow_period
= pow(2, a_counter_in_bits
) /
764 (perf_cfg
->sys_vars
.n_eus
*
765 /* drop 1GHz freq to have units in nanoseconds */
768 DBG("A counter overflow period: %"PRIu64
"ns, %"PRIu64
"ms (n_eus=%"PRIu64
")\n",
769 overflow_period
, overflow_period
/ 1000000ul, perf_cfg
->sys_vars
.n_eus
);
771 int period_exponent
= 0;
772 uint64_t prev_sample_period
, next_sample_period
;
773 for (int e
= 0; e
< 30; e
++) {
774 prev_sample_period
= 1000000000ull * pow(2, e
+ 1) / devinfo
->timestamp_frequency
;
775 next_sample_period
= 1000000000ull * pow(2, e
+ 2) / devinfo
->timestamp_frequency
;
777 /* Take the previous sampling period, lower than the overflow
780 if (prev_sample_period
< overflow_period
&&
781 next_sample_period
> overflow_period
)
782 period_exponent
= e
+ 1;
785 if (period_exponent
== 0) {
786 DBG("WARNING: enable to find a sampling exponent\n");
790 DBG("OA sampling exponent: %i ~= %"PRIu64
"ms\n", period_exponent
,
791 prev_sample_period
/ 1000000ul);
793 if (!gen_perf_open(perf_ctx
, metric_id
, query
->oa_format
,
794 period_exponent
, screen
->fd
, brw
->hw_ctx
))
797 assert(perf_ctx
->current_oa_metrics_set_id
== metric_id
&&
798 perf_ctx
->current_oa_format
== query
->oa_format
);
801 if (!gen_perf_inc_n_users(perf_ctx
)) {
802 DBG("WARNING: Error enabling i915 perf stream: %m\n");
807 perf_cfg
->vtbl
.bo_unreference(obj
->oa
.bo
);
812 brw
->perf_ctx
.perf
->vtbl
.bo_alloc(brw
->bufmgr
,
813 "perf. query OA MI_RPC bo",
816 /* Pre-filling the BO helps debug whether writes landed. */
817 void *map
= brw_bo_map(brw
, obj
->oa
.bo
, MAP_WRITE
);
818 memset(map
, 0x80, MI_RPC_BO_SIZE
);
819 brw_bo_unmap(obj
->oa
.bo
);
822 obj
->oa
.begin_report_id
= perf_ctx
->next_query_start_report_id
;
823 perf_ctx
->next_query_start_report_id
+= 2;
825 /* We flush the batchbuffer here to minimize the chances that MI_RPC
826 * delimiting commands end up in different batchbuffers. If that's the
827 * case, the measurement will include the time it takes for the kernel
828 * scheduler to load a new request into the hardware. This is manifested in
829 * tools like frameretrace by spikes in the "GPU Core Clocks" counter.
831 perf_cfg
->vtbl
.batchbuffer_flush(brw
, __FILE__
, __LINE__
);
833 /* Take a starting OA counter snapshot. */
834 perf_cfg
->vtbl
.emit_mi_report_perf_count(brw
, obj
->oa
.bo
, 0,
835 obj
->oa
.begin_report_id
);
836 perf_cfg
->vtbl
.capture_frequency_stat_register(brw
, obj
->oa
.bo
,
837 MI_FREQ_START_OFFSET_BYTES
);
839 ++perf_ctx
->n_active_oa_queries
;
841 /* No already-buffered samples can possibly be associated with this query
842 * so create a marker within the list of sample buffers enabling us to
843 * easily ignore earlier samples when processing this query after
846 assert(!exec_list_is_empty(&perf_ctx
->sample_buffers
));
847 obj
->oa
.samples_head
= exec_list_get_tail(&perf_ctx
->sample_buffers
);
849 struct oa_sample_buf
*buf
=
850 exec_node_data(struct oa_sample_buf
, obj
->oa
.samples_head
, link
);
852 /* This reference will ensure that future/following sample
853 * buffers (that may relate to this query) can't be freed until
854 * this drops to zero.
858 gen_perf_query_result_clear(&obj
->oa
.result
);
859 obj
->oa
.results_accumulated
= false;
861 add_to_unaccumulated_query_list(brw
, obj
);
865 case GEN_PERF_QUERY_TYPE_PIPELINE
:
866 if (obj
->pipeline_stats
.bo
) {
867 brw
->perf_ctx
.perf
->vtbl
.bo_unreference(obj
->pipeline_stats
.bo
);
868 obj
->pipeline_stats
.bo
= NULL
;
871 obj
->pipeline_stats
.bo
=
872 brw
->perf_ctx
.perf
->vtbl
.bo_alloc(brw
->bufmgr
,
873 "perf. query pipeline stats bo",
876 /* Take starting snapshots. */
877 gen_perf_snapshot_statistics_registers(brw
, perf_cfg
, obj
, 0);
879 ++perf_ctx
->n_active_pipeline_stats_queries
;
883 unreachable("Unknown query type");
887 if (INTEL_DEBUG
& DEBUG_PERFMON
)
888 dump_perf_queries(brw
);
894 * Driver hook for glEndPerfQueryINTEL().
897 brw_end_perf_query(struct gl_context
*ctx
,
898 struct gl_perf_query_object
*o
)
900 struct brw_context
*brw
= brw_context(ctx
);
901 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
902 struct gen_perf_query_object
*obj
= brw_query
->query
;
903 struct gen_perf_config
*perf_cfg
= brw
->perf_ctx
.perf
;
904 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
906 DBG("End(%d)\n", o
->Id
);
908 /* Ensure that the work associated with the queried commands will have
909 * finished before taking our query end counter readings.
911 * For more details see comment in brw_begin_perf_query for
912 * corresponding flush.
914 perf_cfg
->vtbl
.emit_mi_flush(brw
);
916 switch (obj
->queryinfo
->kind
) {
917 case GEN_PERF_QUERY_TYPE_OA
:
918 case GEN_PERF_QUERY_TYPE_RAW
:
920 /* NB: It's possible that the query will have already been marked
921 * as 'accumulated' if an error was seen while reading samples
922 * from perf. In this case we mustn't try and emit a closing
923 * MI_RPC command in case the OA unit has already been disabled
925 if (!obj
->oa
.results_accumulated
) {
926 /* Take an ending OA counter snapshot. */
927 perf_cfg
->vtbl
.capture_frequency_stat_register(brw
, obj
->oa
.bo
,
928 MI_FREQ_END_OFFSET_BYTES
);
929 brw
->vtbl
.emit_mi_report_perf_count(brw
, obj
->oa
.bo
,
930 MI_RPC_BO_END_OFFSET_BYTES
,
931 obj
->oa
.begin_report_id
+ 1);
934 --perf_ctx
->n_active_oa_queries
;
936 /* NB: even though the query has now ended, it can't be accumulated
937 * until the end MI_REPORT_PERF_COUNT snapshot has been written
942 case GEN_PERF_QUERY_TYPE_PIPELINE
:
943 gen_perf_snapshot_statistics_registers(brw
, perf_cfg
, obj
,
944 STATS_BO_END_OFFSET_BYTES
);
945 --perf_ctx
->n_active_pipeline_stats_queries
;
949 unreachable("Unknown query type");
955 brw_wait_perf_query(struct gl_context
*ctx
, struct gl_perf_query_object
*o
)
957 struct brw_context
*brw
= brw_context(ctx
);
958 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
959 struct gen_perf_query_object
*obj
= brw_query
->query
;
960 struct brw_bo
*bo
= NULL
;
961 struct gen_perf_config
*perf_cfg
= brw
->perf_ctx
.perf
;
965 switch (obj
->queryinfo
->kind
) {
966 case GEN_PERF_QUERY_TYPE_OA
:
967 case GEN_PERF_QUERY_TYPE_RAW
:
971 case GEN_PERF_QUERY_TYPE_PIPELINE
:
972 bo
= obj
->pipeline_stats
.bo
;
976 unreachable("Unknown query type");
983 /* If the current batch references our results bo then we need to
986 if (brw_batch_references(&brw
->batch
, bo
))
987 perf_cfg
->vtbl
.batchbuffer_flush(brw
, __FILE__
, __LINE__
);
989 brw_bo_wait_rendering(bo
);
991 /* Due to a race condition between the OA unit signaling report
992 * availability and the report actually being written into memory,
993 * we need to wait for all the reports to come in before we can
996 if (obj
->queryinfo
->kind
== GEN_PERF_QUERY_TYPE_OA
||
997 obj
->queryinfo
->kind
== GEN_PERF_QUERY_TYPE_RAW
) {
998 while (!read_oa_samples_for_query(brw
, obj
))
1004 brw_is_perf_query_ready(struct gl_context
*ctx
,
1005 struct gl_perf_query_object
*o
)
1007 struct brw_context
*brw
= brw_context(ctx
);
1008 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
1009 struct gen_perf_query_object
*obj
= brw_query
->query
;
1014 switch (obj
->queryinfo
->kind
) {
1015 case GEN_PERF_QUERY_TYPE_OA
:
1016 case GEN_PERF_QUERY_TYPE_RAW
:
1017 return (obj
->oa
.results_accumulated
||
1019 !brw_batch_references(&brw
->batch
, obj
->oa
.bo
) &&
1020 !brw_bo_busy(obj
->oa
.bo
) &&
1021 read_oa_samples_for_query(brw
, obj
)));
1022 case GEN_PERF_QUERY_TYPE_PIPELINE
:
1023 return (obj
->pipeline_stats
.bo
&&
1024 !brw_batch_references(&brw
->batch
, obj
->pipeline_stats
.bo
) &&
1025 !brw_bo_busy(obj
->pipeline_stats
.bo
));
1028 unreachable("Unknown query type");
1036 read_slice_unslice_frequencies(struct brw_context
*brw
,
1037 struct gen_perf_query_object
*obj
)
1039 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1040 uint32_t *begin_report
= obj
->oa
.map
, *end_report
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
1042 gen_perf_query_result_read_frequencies(&obj
->oa
.result
,
1043 devinfo
, begin_report
, end_report
);
1047 read_gt_frequency(struct brw_context
*brw
,
1048 struct gen_perf_query_object
*obj
)
1050 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1051 uint32_t start
= *((uint32_t *)(obj
->oa
.map
+ MI_FREQ_START_OFFSET_BYTES
)),
1052 end
= *((uint32_t *)(obj
->oa
.map
+ MI_FREQ_END_OFFSET_BYTES
));
1054 switch (devinfo
->gen
) {
1057 obj
->oa
.gt_frequency
[0] = GET_FIELD(start
, GEN7_RPSTAT1_CURR_GT_FREQ
) * 50ULL;
1058 obj
->oa
.gt_frequency
[1] = GET_FIELD(end
, GEN7_RPSTAT1_CURR_GT_FREQ
) * 50ULL;
1063 obj
->oa
.gt_frequency
[0] = GET_FIELD(start
, GEN9_RPSTAT0_CURR_GT_FREQ
) * 50ULL / 3ULL;
1064 obj
->oa
.gt_frequency
[1] = GET_FIELD(end
, GEN9_RPSTAT0_CURR_GT_FREQ
) * 50ULL / 3ULL;
1067 unreachable("unexpected gen");
1070 /* Put the numbers into Hz. */
1071 obj
->oa
.gt_frequency
[0] *= 1000000ULL;
1072 obj
->oa
.gt_frequency
[1] *= 1000000ULL;
1076 get_oa_counter_data(struct brw_context
*brw
,
1077 struct gen_perf_query_object
*obj
,
1081 struct gen_perf_config
*perf
= brw
->perf_ctx
.perf
;
1082 const struct gen_perf_query_info
*query
= obj
->queryinfo
;
1083 int n_counters
= query
->n_counters
;
1086 for (int i
= 0; i
< n_counters
; i
++) {
1087 const struct gen_perf_query_counter
*counter
= &query
->counters
[i
];
1088 uint64_t *out_uint64
;
1090 size_t counter_size
= gen_perf_query_counter_get_size(counter
);
1093 switch (counter
->data_type
) {
1094 case GEN_PERF_COUNTER_DATA_TYPE_UINT64
:
1095 out_uint64
= (uint64_t *)(data
+ counter
->offset
);
1097 counter
->oa_counter_read_uint64(perf
, query
,
1098 obj
->oa
.result
.accumulator
);
1100 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT
:
1101 out_float
= (float *)(data
+ counter
->offset
);
1103 counter
->oa_counter_read_float(perf
, query
,
1104 obj
->oa
.result
.accumulator
);
1107 /* So far we aren't using uint32, double or bool32... */
1108 unreachable("unexpected counter data type");
1110 written
= counter
->offset
+ counter_size
;
1118 get_pipeline_stats_data(struct brw_context
*brw
,
1119 struct gen_perf_query_object
*obj
,
1124 const struct gen_perf_query_info
*query
= obj
->queryinfo
;
1125 int n_counters
= obj
->queryinfo
->n_counters
;
1128 uint64_t *start
= brw_bo_map(brw
, obj
->pipeline_stats
.bo
, MAP_READ
);
1129 uint64_t *end
= start
+ (STATS_BO_END_OFFSET_BYTES
/ sizeof(uint64_t));
1131 for (int i
= 0; i
< n_counters
; i
++) {
1132 const struct gen_perf_query_counter
*counter
= &query
->counters
[i
];
1133 uint64_t value
= end
[i
] - start
[i
];
1135 if (counter
->pipeline_stat
.numerator
!=
1136 counter
->pipeline_stat
.denominator
) {
1137 value
*= counter
->pipeline_stat
.numerator
;
1138 value
/= counter
->pipeline_stat
.denominator
;
1141 *((uint64_t *)p
) = value
;
1145 brw_bo_unmap(obj
->pipeline_stats
.bo
);
1151 * Driver hook for glGetPerfQueryDataINTEL().
1154 brw_get_perf_query_data(struct gl_context
*ctx
,
1155 struct gl_perf_query_object
*o
,
1158 GLuint
*bytes_written
)
1160 struct brw_context
*brw
= brw_context(ctx
);
1161 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
1162 struct gen_perf_query_object
*obj
= brw_query
->query
;
1165 assert(brw_is_perf_query_ready(ctx
, o
));
1167 DBG("GetData(%d)\n", o
->Id
);
1169 if (INTEL_DEBUG
& DEBUG_PERFMON
)
1170 dump_perf_queries(brw
);
1172 /* We expect that the frontend only calls this hook when it knows
1173 * that results are available.
1177 switch (obj
->queryinfo
->kind
) {
1178 case GEN_PERF_QUERY_TYPE_OA
:
1179 case GEN_PERF_QUERY_TYPE_RAW
:
1180 if (!obj
->oa
.results_accumulated
) {
1181 read_gt_frequency(brw
, obj
);
1182 read_slice_unslice_frequencies(brw
, obj
);
1183 accumulate_oa_reports(brw
, brw_query
);
1184 assert(obj
->oa
.results_accumulated
);
1186 brw_bo_unmap(obj
->oa
.bo
);
1189 if (obj
->queryinfo
->kind
== GEN_PERF_QUERY_TYPE_OA
) {
1190 written
= get_oa_counter_data(brw
, obj
, data_size
, (uint8_t *)data
);
1192 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1194 written
= gen_perf_query_result_write_mdapi((uint8_t *)data
, data_size
,
1195 devinfo
, &obj
->oa
.result
,
1196 obj
->oa
.gt_frequency
[0],
1197 obj
->oa
.gt_frequency
[1]);
1201 case GEN_PERF_QUERY_TYPE_PIPELINE
:
1202 written
= get_pipeline_stats_data(brw
, obj
, data_size
, (uint8_t *)data
);
1206 unreachable("Unknown query type");
1211 *bytes_written
= written
;
1214 static struct gl_perf_query_object
*
1215 brw_new_perf_query_object(struct gl_context
*ctx
, unsigned query_index
)
1217 struct brw_context
*brw
= brw_context(ctx
);
1218 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
1219 const struct gen_perf_query_info
*queryinfo
=
1220 &perf_ctx
->perf
->queries
[query_index
];
1221 struct gen_perf_query_object
*obj
=
1222 calloc(1, sizeof(struct gen_perf_query_object
));
1227 obj
->queryinfo
= queryinfo
;
1229 perf_ctx
->n_query_instances
++;
1231 struct brw_perf_query_object
*brw_query
= calloc(1, sizeof(struct brw_perf_query_object
));
1232 if (unlikely(!brw_query
))
1234 brw_query
->query
= obj
;
1235 return &brw_query
->base
;
1239 * Driver hook for glDeletePerfQueryINTEL().
1242 brw_delete_perf_query(struct gl_context
*ctx
,
1243 struct gl_perf_query_object
*o
)
1245 struct brw_context
*brw
= brw_context(ctx
);
1246 struct gen_perf_config
*perf_cfg
= brw
->perf_ctx
.perf
;
1247 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
1248 struct gen_perf_query_object
*obj
= brw_query
->query
;
1249 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
1251 /* We can assume that the frontend waits for a query to complete
1252 * before ever calling into here, so we don't have to worry about
1253 * deleting an in-flight query object.
1256 assert(!o
->Used
|| o
->Ready
);
1258 DBG("Delete(%d)\n", o
->Id
);
1260 switch (obj
->queryinfo
->kind
) {
1261 case GEN_PERF_QUERY_TYPE_OA
:
1262 case GEN_PERF_QUERY_TYPE_RAW
:
1264 if (!obj
->oa
.results_accumulated
) {
1265 drop_from_unaccumulated_query_list(brw
, obj
);
1266 gen_perf_dec_n_users(perf_ctx
);
1269 perf_cfg
->vtbl
.bo_unreference(obj
->oa
.bo
);
1273 obj
->oa
.results_accumulated
= false;
1276 case GEN_PERF_QUERY_TYPE_PIPELINE
:
1277 if (obj
->pipeline_stats
.bo
) {
1278 perf_cfg
->vtbl
.bo_unreference(obj
->pipeline_stats
.bo
);
1279 obj
->pipeline_stats
.bo
= NULL
;
1284 unreachable("Unknown query type");
1288 /* As an indication that the INTEL_performance_query extension is no
1289 * longer in use, it's a good time to free our cache of sample
1290 * buffers and close any current i915-perf stream.
1292 if (--perf_ctx
->n_query_instances
== 0) {
1293 gen_perf_free_sample_bufs(perf_ctx
);
1294 gen_perf_close(perf_ctx
, obj
->queryinfo
);
1301 /******************************************************************************/
1304 init_pipeline_statistic_query_registers(struct brw_context
*brw
)
1306 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1307 struct gen_perf_config
*perf
= brw
->perf_ctx
.perf
;
1308 struct gen_perf_query_info
*query
=
1309 gen_perf_query_append_query_info(perf
, MAX_STAT_COUNTERS
);
1311 query
->kind
= GEN_PERF_QUERY_TYPE_PIPELINE
;
1312 query
->name
= "Pipeline Statistics Registers";
1314 gen_perf_query_info_add_basic_stat_reg(query
, IA_VERTICES_COUNT
,
1315 "N vertices submitted");
1316 gen_perf_query_info_add_basic_stat_reg(query
, IA_PRIMITIVES_COUNT
,
1317 "N primitives submitted");
1318 gen_perf_query_info_add_basic_stat_reg(query
, VS_INVOCATION_COUNT
,
1319 "N vertex shader invocations");
1321 if (devinfo
->gen
== 6) {
1322 gen_perf_query_info_add_stat_reg(query
, GEN6_SO_PRIM_STORAGE_NEEDED
, 1, 1,
1323 "SO_PRIM_STORAGE_NEEDED",
1324 "N geometry shader stream-out primitives (total)");
1325 gen_perf_query_info_add_stat_reg(query
, GEN6_SO_NUM_PRIMS_WRITTEN
, 1, 1,
1326 "SO_NUM_PRIMS_WRITTEN",
1327 "N geometry shader stream-out primitives (written)");
1329 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(0), 1, 1,
1330 "SO_PRIM_STORAGE_NEEDED (Stream 0)",
1331 "N stream-out (stream 0) primitives (total)");
1332 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(1), 1, 1,
1333 "SO_PRIM_STORAGE_NEEDED (Stream 1)",
1334 "N stream-out (stream 1) primitives (total)");
1335 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(2), 1, 1,
1336 "SO_PRIM_STORAGE_NEEDED (Stream 2)",
1337 "N stream-out (stream 2) primitives (total)");
1338 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(3), 1, 1,
1339 "SO_PRIM_STORAGE_NEEDED (Stream 3)",
1340 "N stream-out (stream 3) primitives (total)");
1341 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(0), 1, 1,
1342 "SO_NUM_PRIMS_WRITTEN (Stream 0)",
1343 "N stream-out (stream 0) primitives (written)");
1344 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(1), 1, 1,
1345 "SO_NUM_PRIMS_WRITTEN (Stream 1)",
1346 "N stream-out (stream 1) primitives (written)");
1347 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(2), 1, 1,
1348 "SO_NUM_PRIMS_WRITTEN (Stream 2)",
1349 "N stream-out (stream 2) primitives (written)");
1350 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(3), 1, 1,
1351 "SO_NUM_PRIMS_WRITTEN (Stream 3)",
1352 "N stream-out (stream 3) primitives (written)");
1355 gen_perf_query_info_add_basic_stat_reg(query
, HS_INVOCATION_COUNT
,
1356 "N TCS shader invocations");
1357 gen_perf_query_info_add_basic_stat_reg(query
, DS_INVOCATION_COUNT
,
1358 "N TES shader invocations");
1360 gen_perf_query_info_add_basic_stat_reg(query
, GS_INVOCATION_COUNT
,
1361 "N geometry shader invocations");
1362 gen_perf_query_info_add_basic_stat_reg(query
, GS_PRIMITIVES_COUNT
,
1363 "N geometry shader primitives emitted");
1365 gen_perf_query_info_add_basic_stat_reg(query
, CL_INVOCATION_COUNT
,
1366 "N primitives entering clipping");
1367 gen_perf_query_info_add_basic_stat_reg(query
, CL_PRIMITIVES_COUNT
,
1368 "N primitives leaving clipping");
1370 if (devinfo
->is_haswell
|| devinfo
->gen
== 8) {
1371 gen_perf_query_info_add_stat_reg(query
, PS_INVOCATION_COUNT
, 1, 4,
1372 "N fragment shader invocations",
1373 "N fragment shader invocations");
1375 gen_perf_query_info_add_basic_stat_reg(query
, PS_INVOCATION_COUNT
,
1376 "N fragment shader invocations");
1379 gen_perf_query_info_add_basic_stat_reg(query
, PS_DEPTH_COUNT
,
1380 "N z-pass fragments");
1382 if (devinfo
->gen
>= 7) {
1383 gen_perf_query_info_add_basic_stat_reg(query
, CS_INVOCATION_COUNT
,
1384 "N compute shader invocations");
1387 query
->data_size
= sizeof(uint64_t) * query
->n_counters
;
1390 /* gen_device_info will have incorrect default topology values for unsupported kernels.
1391 * verify kernel support to ensure OA metrics are accurate.
1394 oa_metrics_kernel_support(int fd
, const struct gen_device_info
*devinfo
)
1396 if (devinfo
->gen
>= 10) {
1397 /* topology uAPI required for CNL+ (kernel 4.17+) make a call to the api
1400 struct drm_i915_query_item item
= {
1401 .query_id
= DRM_I915_QUERY_TOPOLOGY_INFO
,
1403 struct drm_i915_query query
= {
1405 .items_ptr
= (uintptr_t) &item
,
1408 /* kernel 4.17+ supports the query */
1409 return drmIoctl(fd
, DRM_IOCTL_I915_QUERY
, &query
) == 0;
1412 if (devinfo
->gen
>= 8) {
1413 /* 4.13+ api required for gen8 - gen9 */
1415 struct drm_i915_getparam gp
= {
1416 .param
= I915_PARAM_SLICE_MASK
,
1419 /* kernel 4.13+ supports this parameter */
1420 return drmIoctl(fd
, DRM_IOCTL_I915_GETPARAM
, &gp
) == 0;
1423 if (devinfo
->gen
== 7)
1424 /* default topology values are correct for HSW */
1427 /* oa not supported before gen 7*/
1432 brw_oa_bo_alloc(void *bufmgr
, const char *name
, uint64_t size
)
1434 return brw_bo_alloc(bufmgr
, name
, size
, BRW_MEMZONE_OTHER
);
1438 brw_oa_emit_mi_report_perf_count(void *c
,
1440 uint32_t offset_in_bytes
,
1443 struct brw_context
*ctx
= c
;
1444 ctx
->vtbl
.emit_mi_report_perf_count(ctx
,
1450 typedef void (*bo_unreference_t
)(void *);
1451 typedef void (* emit_mi_report_t
)(void *, void *, uint32_t, uint32_t);
1452 typedef void (*emit_mi_flush_t
)(void *);
1455 brw_oa_batchbuffer_flush(void *c
, const char *file
, int line
)
1457 struct brw_context
*ctx
= c
;
1458 _intel_batchbuffer_flush_fence(ctx
, -1, NULL
, file
, line
);
1461 typedef void (*capture_frequency_stat_register_t
)(void *, void *, uint32_t );
1462 typedef void (*store_register_mem64_t
)(void *ctx
, void *bo
,
1463 uint32_t reg
, uint32_t offset
);
1466 brw_init_perf_query_info(struct gl_context
*ctx
)
1468 struct brw_context
*brw
= brw_context(ctx
);
1469 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1470 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1472 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
1474 return perf_ctx
->perf
->n_queries
;
1476 perf_ctx
->perf
= gen_perf_new(brw
);
1477 struct gen_perf_config
*perf_cfg
= perf_ctx
->perf
;
1479 perf_cfg
->vtbl
.bo_alloc
= brw_oa_bo_alloc
;
1480 perf_cfg
->vtbl
.bo_unreference
= (bo_unreference_t
)brw_bo_unreference
;
1481 perf_cfg
->vtbl
.emit_mi_flush
= (emit_mi_flush_t
)brw_emit_mi_flush
;
1482 perf_cfg
->vtbl
.emit_mi_report_perf_count
=
1483 (emit_mi_report_t
)brw_oa_emit_mi_report_perf_count
;
1484 perf_cfg
->vtbl
.batchbuffer_flush
= brw_oa_batchbuffer_flush
;
1485 perf_cfg
->vtbl
.capture_frequency_stat_register
=
1486 (capture_frequency_stat_register_t
) capture_frequency_stat_register
;
1487 perf_cfg
->vtbl
.store_register_mem64
=
1488 (store_register_mem64_t
) brw_store_register_mem64
;
1490 init_pipeline_statistic_query_registers(brw
);
1491 gen_perf_query_register_mdapi_statistic_query(&brw
->screen
->devinfo
,
1492 brw
->perf_ctx
.perf
);
1494 if ((oa_metrics_kernel_support(screen
->fd
, devinfo
)) &&
1495 (gen_perf_load_oa_metrics(perf_cfg
, screen
->fd
, devinfo
)))
1496 gen_perf_query_register_mdapi_oa_query(devinfo
, perf_cfg
);
1498 perf_ctx
->unaccumulated
=
1499 ralloc_array(brw
, struct gen_perf_query_object
*, 2);
1500 perf_ctx
->unaccumulated_elements
= 0;
1501 perf_ctx
->unaccumulated_array_size
= 2;
1503 exec_list_make_empty(&perf_ctx
->sample_buffers
);
1504 exec_list_make_empty(&perf_ctx
->free_sample_buffers
);
1506 /* It's convenient to guarantee that this linked list of sample
1507 * buffers is never empty so we add an empty head so when we
1508 * Begin an OA query we can always take a reference on a buffer
1511 struct oa_sample_buf
*buf
= gen_perf_get_free_sample_buf(&brw
->perf_ctx
);
1512 exec_list_push_head(&perf_ctx
->sample_buffers
, &buf
->link
);
1514 perf_ctx
->oa_stream_fd
= -1;
1516 perf_ctx
->next_query_start_report_id
= 1000;
1518 return perf_cfg
->n_queries
;
1522 brw_init_performance_queries(struct brw_context
*brw
)
1524 struct gl_context
*ctx
= &brw
->ctx
;
1526 ctx
->Driver
.InitPerfQueryInfo
= brw_init_perf_query_info
;
1527 ctx
->Driver
.GetPerfQueryInfo
= brw_get_perf_query_info
;
1528 ctx
->Driver
.GetPerfCounterInfo
= brw_get_perf_counter_info
;
1529 ctx
->Driver
.NewPerfQueryObject
= brw_new_perf_query_object
;
1530 ctx
->Driver
.DeletePerfQuery
= brw_delete_perf_query
;
1531 ctx
->Driver
.BeginPerfQuery
= brw_begin_perf_query
;
1532 ctx
->Driver
.EndPerfQuery
= brw_end_perf_query
;
1533 ctx
->Driver
.WaitPerfQuery
= brw_wait_perf_query
;
1534 ctx
->Driver
.IsPerfQueryReady
= brw_is_perf_query_ready
;
1535 ctx
->Driver
.GetPerfQueryData
= brw_get_perf_query_data
;