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 * Remove a query from the global list of unaccumulated queries once
249 * after successfully accumulating the OA reports associated with the
250 * query in accumulate_oa_reports() or when discarding unwanted query
254 drop_from_unaccumulated_query_list(struct brw_context
*brw
,
255 struct gen_perf_query_object
*obj
)
257 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
258 for (int i
= 0; i
< perf_ctx
->unaccumulated_elements
; i
++) {
259 if (perf_ctx
->unaccumulated
[i
] == obj
) {
260 int last_elt
= --perf_ctx
->unaccumulated_elements
;
263 perf_ctx
->unaccumulated
[i
] = NULL
;
265 perf_ctx
->unaccumulated
[i
] =
266 perf_ctx
->unaccumulated
[last_elt
];
273 /* Drop our samples_head reference so that associated periodic
274 * sample data buffers can potentially be reaped if they aren't
275 * referenced by any other queries...
278 struct oa_sample_buf
*buf
=
279 exec_node_data(struct oa_sample_buf
, obj
->oa
.samples_head
, link
);
281 assert(buf
->refcount
> 0);
284 obj
->oa
.samples_head
= NULL
;
286 gen_perf_reap_old_sample_buffers(&brw
->perf_ctx
);
289 /* In general if we see anything spurious while accumulating results,
290 * we don't try and continue accumulating the current query, hoping
291 * for the best, we scrap anything outstanding, and then hope for the
292 * best with new queries.
295 discard_all_queries(struct brw_context
*brw
)
297 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
298 while (perf_ctx
->unaccumulated_elements
) {
299 struct gen_perf_query_object
*obj
= perf_ctx
->unaccumulated
[0];
301 obj
->oa
.results_accumulated
= true;
302 drop_from_unaccumulated_query_list(brw
, perf_ctx
->unaccumulated
[0]);
304 gen_perf_dec_n_users(perf_ctx
);
309 OA_READ_STATUS_ERROR
,
310 OA_READ_STATUS_UNFINISHED
,
311 OA_READ_STATUS_FINISHED
,
314 static enum OaReadStatus
315 read_oa_samples_until(struct brw_context
*brw
,
316 uint32_t start_timestamp
,
317 uint32_t end_timestamp
)
319 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
320 struct exec_node
*tail_node
=
321 exec_list_get_tail(&perf_ctx
->sample_buffers
);
322 struct oa_sample_buf
*tail_buf
=
323 exec_node_data(struct oa_sample_buf
, tail_node
, link
);
324 uint32_t last_timestamp
= tail_buf
->last_timestamp
;
327 struct oa_sample_buf
*buf
= gen_perf_get_free_sample_buf(perf_ctx
);
331 while ((len
= read(perf_ctx
->oa_stream_fd
, buf
->buf
,
332 sizeof(buf
->buf
))) < 0 && errno
== EINTR
)
336 exec_list_push_tail(&perf_ctx
->free_sample_buffers
, &buf
->link
);
340 return ((last_timestamp
- start_timestamp
) >=
341 (end_timestamp
- start_timestamp
)) ?
342 OA_READ_STATUS_FINISHED
:
343 OA_READ_STATUS_UNFINISHED
;
345 DBG("Error reading i915 perf samples: %m\n");
348 DBG("Spurious EOF reading i915 perf samples\n");
350 return OA_READ_STATUS_ERROR
;
354 exec_list_push_tail(&perf_ctx
->sample_buffers
, &buf
->link
);
356 /* Go through the reports and update the last timestamp. */
358 while (offset
< buf
->len
) {
359 const struct drm_i915_perf_record_header
*header
=
360 (const struct drm_i915_perf_record_header
*) &buf
->buf
[offset
];
361 uint32_t *report
= (uint32_t *) (header
+ 1);
363 if (header
->type
== DRM_I915_PERF_RECORD_SAMPLE
)
364 last_timestamp
= report
[1];
366 offset
+= header
->size
;
369 buf
->last_timestamp
= last_timestamp
;
372 unreachable("not reached");
373 return OA_READ_STATUS_ERROR
;
377 * Try to read all the reports until either the delimiting timestamp
378 * or an error arises.
381 read_oa_samples_for_query(struct brw_context
*brw
,
382 struct gen_perf_query_object
*obj
)
388 /* We need the MI_REPORT_PERF_COUNT to land before we can start
390 assert(!brw_batch_references(&brw
->batch
, obj
->oa
.bo
) &&
391 !brw_bo_busy(obj
->oa
.bo
));
393 /* Map the BO once here and let accumulate_oa_reports() unmap
395 if (obj
->oa
.map
== NULL
)
396 obj
->oa
.map
= brw
->perf_ctx
.perf
->vtbl
.bo_map(brw
, obj
->oa
.bo
, MAP_READ
);
398 start
= last
= obj
->oa
.map
;
399 end
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
401 if (start
[0] != obj
->oa
.begin_report_id
) {
402 DBG("Spurious start report id=%"PRIu32
"\n", start
[0]);
405 if (end
[0] != (obj
->oa
.begin_report_id
+ 1)) {
406 DBG("Spurious end report id=%"PRIu32
"\n", end
[0]);
410 /* Read the reports until the end timestamp. */
411 switch (read_oa_samples_until(brw
, start
[1], end
[1])) {
412 case OA_READ_STATUS_ERROR
:
413 /* Fallthrough and let accumulate_oa_reports() deal with the
415 case OA_READ_STATUS_FINISHED
:
417 case OA_READ_STATUS_UNFINISHED
:
421 unreachable("invalid read status");
426 * Accumulate raw OA counter values based on deltas between pairs of
429 * Accumulation starts from the first report captured via
430 * MI_REPORT_PERF_COUNT (MI_RPC) by brw_begin_perf_query() until the
431 * last MI_RPC report requested by brw_end_perf_query(). Between these
432 * two reports there may also some number of periodically sampled OA
433 * reports collected via the i915 perf interface - depending on the
434 * duration of the query.
436 * These periodic snapshots help to ensure we handle counter overflow
437 * correctly by being frequent enough to ensure we don't miss multiple
438 * overflows of a counter between snapshots. For Gen8+ the i915 perf
439 * snapshots provide the extra context-switch reports that let us
440 * subtract out the progress of counters associated with other
441 * contexts running on the system.
444 accumulate_oa_reports(struct brw_context
*brw
,
445 struct brw_perf_query_object
*brw_query
)
447 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
448 struct gen_perf_query_object
*obj
= brw_query
->query
;
449 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
453 struct exec_node
*first_samples_node
;
455 int out_duration
= 0;
457 assert(brw_query
->base
.Ready
);
458 assert(obj
->oa
.map
!= NULL
);
460 start
= last
= obj
->oa
.map
;
461 end
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
463 if (start
[0] != obj
->oa
.begin_report_id
) {
464 DBG("Spurious start report id=%"PRIu32
"\n", start
[0]);
467 if (end
[0] != (obj
->oa
.begin_report_id
+ 1)) {
468 DBG("Spurious end report id=%"PRIu32
"\n", end
[0]);
472 /* See if we have any periodic reports to accumulate too... */
474 /* N.B. The oa.samples_head was set when the query began and
475 * pointed to the tail of the perf_ctx->sample_buffers list at
476 * the time the query started. Since the buffer existed before the
477 * first MI_REPORT_PERF_COUNT command was emitted we therefore know
478 * that no data in this particular node's buffer can possibly be
479 * associated with the query - so skip ahead one...
481 first_samples_node
= obj
->oa
.samples_head
->next
;
483 foreach_list_typed_from(struct oa_sample_buf
, buf
, link
,
484 &brw
->perf_ctx
.sample_buffers
,
489 while (offset
< buf
->len
) {
490 const struct drm_i915_perf_record_header
*header
=
491 (const struct drm_i915_perf_record_header
*)(buf
->buf
+ offset
);
493 assert(header
->size
!= 0);
494 assert(header
->size
<= buf
->len
);
496 offset
+= header
->size
;
498 switch (header
->type
) {
499 case DRM_I915_PERF_RECORD_SAMPLE
: {
500 uint32_t *report
= (uint32_t *)(header
+ 1);
503 /* Ignore reports that come before the start marker.
504 * (Note: takes care to allow overflow of 32bit timestamps)
506 if (gen_device_info_timebase_scale(devinfo
,
507 report
[1] - start
[1]) > 5000000000) {
511 /* Ignore reports that come after the end marker.
512 * (Note: takes care to allow overflow of 32bit timestamps)
514 if (gen_device_info_timebase_scale(devinfo
,
515 report
[1] - end
[1]) <= 5000000000) {
519 /* For Gen8+ since the counters continue while other
520 * contexts are running we need to discount any unrelated
521 * deltas. The hardware automatically generates a report
522 * on context switch which gives us a new reference point
523 * to continuing adding deltas from.
525 * For Haswell we can rely on the HW to stop the progress
526 * of OA counters while any other context is acctive.
528 if (devinfo
->gen
>= 8) {
529 if (in_ctx
&& report
[2] != obj
->oa
.result
.hw_id
) {
530 DBG("i915 perf: Switch AWAY (observed by ID change)\n");
533 } else if (in_ctx
== false && report
[2] == obj
->oa
.result
.hw_id
) {
534 DBG("i915 perf: Switch TO\n");
537 /* From experimentation in IGT, we found that the OA unit
538 * might label some report as "idle" (using an invalid
539 * context ID), right after a report for a given context.
540 * Deltas generated by those reports actually belong to the
541 * previous context, even though they're not labelled as
544 * We didn't *really* Switch AWAY in the case that we e.g.
545 * saw a single periodic report while idle...
547 if (out_duration
>= 1)
550 assert(report
[2] == obj
->oa
.result
.hw_id
);
551 DBG("i915 perf: Continuation IN\n");
553 assert(report
[2] != obj
->oa
.result
.hw_id
);
554 DBG("i915 perf: Continuation OUT\n");
561 gen_perf_query_result_accumulate(&obj
->oa
.result
, obj
->queryinfo
,
570 case DRM_I915_PERF_RECORD_OA_BUFFER_LOST
:
571 DBG("i915 perf: OA error: all reports lost\n");
573 case DRM_I915_PERF_RECORD_OA_REPORT_LOST
:
574 DBG("i915 perf: OA report lost\n");
582 gen_perf_query_result_accumulate(&obj
->oa
.result
, obj
->queryinfo
,
585 DBG("Marking %d accumulated - results gathered\n", brw_query
->base
.Id
);
587 obj
->oa
.results_accumulated
= true;
588 drop_from_unaccumulated_query_list(brw
, obj
);
589 gen_perf_dec_n_users(perf_ctx
);
595 discard_all_queries(brw
);
598 /******************************************************************************/
601 capture_frequency_stat_register(struct brw_context
*brw
,
605 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
607 if (devinfo
->gen
>= 7 && devinfo
->gen
<= 8 &&
608 !devinfo
->is_baytrail
&& !devinfo
->is_cherryview
) {
609 brw_store_register_mem32(brw
, bo
, GEN7_RPSTAT1
, bo_offset
);
610 } else if (devinfo
->gen
>= 9) {
611 brw_store_register_mem32(brw
, bo
, GEN9_RPSTAT0
, bo_offset
);
616 * Driver hook for glBeginPerfQueryINTEL().
619 brw_begin_perf_query(struct gl_context
*ctx
,
620 struct gl_perf_query_object
*o
)
622 struct brw_context
*brw
= brw_context(ctx
);
623 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
624 struct gen_perf_query_object
*obj
= brw_query
->query
;
625 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
627 /* We can assume the frontend hides mistaken attempts to Begin a
628 * query object multiple times before its End. Similarly if an
629 * application reuses a query object before results have arrived
630 * the frontend will wait for prior results so we don't need
631 * to support abandoning in-flight results.
634 assert(!o
->Used
|| o
->Ready
); /* no in-flight query to worry about */
636 DBG("Begin(%d)\n", o
->Id
);
638 gen_perf_begin_query(perf_ctx
, obj
);
640 if (INTEL_DEBUG
& DEBUG_PERFMON
)
641 dump_perf_queries(brw
);
647 * Driver hook for glEndPerfQueryINTEL().
650 brw_end_perf_query(struct gl_context
*ctx
,
651 struct gl_perf_query_object
*o
)
653 struct brw_context
*brw
= brw_context(ctx
);
654 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
655 struct gen_perf_query_object
*obj
= brw_query
->query
;
656 struct gen_perf_config
*perf_cfg
= brw
->perf_ctx
.perf
;
657 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
659 DBG("End(%d)\n", o
->Id
);
661 /* Ensure that the work associated with the queried commands will have
662 * finished before taking our query end counter readings.
664 * For more details see comment in brw_begin_perf_query for
665 * corresponding flush.
667 perf_cfg
->vtbl
.emit_mi_flush(perf_ctx
->ctx
);
669 switch (obj
->queryinfo
->kind
) {
670 case GEN_PERF_QUERY_TYPE_OA
:
671 case GEN_PERF_QUERY_TYPE_RAW
:
673 /* NB: It's possible that the query will have already been marked
674 * as 'accumulated' if an error was seen while reading samples
675 * from perf. In this case we mustn't try and emit a closing
676 * MI_RPC command in case the OA unit has already been disabled
678 if (!obj
->oa
.results_accumulated
) {
679 /* Take an ending OA counter snapshot. */
680 perf_cfg
->vtbl
.capture_frequency_stat_register(perf_ctx
->ctx
, obj
->oa
.bo
,
681 MI_FREQ_END_OFFSET_BYTES
);
682 brw
->vtbl
.emit_mi_report_perf_count(perf_ctx
->ctx
, obj
->oa
.bo
,
683 MI_RPC_BO_END_OFFSET_BYTES
,
684 obj
->oa
.begin_report_id
+ 1);
687 --perf_ctx
->n_active_oa_queries
;
689 /* NB: even though the query has now ended, it can't be accumulated
690 * until the end MI_REPORT_PERF_COUNT snapshot has been written
695 case GEN_PERF_QUERY_TYPE_PIPELINE
:
696 gen_perf_snapshot_statistics_registers(brw
, perf_cfg
, obj
,
697 STATS_BO_END_OFFSET_BYTES
);
698 --perf_ctx
->n_active_pipeline_stats_queries
;
702 unreachable("Unknown query type");
708 brw_wait_perf_query(struct gl_context
*ctx
, struct gl_perf_query_object
*o
)
710 struct brw_context
*brw
= brw_context(ctx
);
711 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
712 struct gen_perf_query_object
*obj
= brw_query
->query
;
713 struct brw_bo
*bo
= NULL
;
714 struct gen_perf_config
*perf_cfg
= brw
->perf_ctx
.perf
;
718 switch (obj
->queryinfo
->kind
) {
719 case GEN_PERF_QUERY_TYPE_OA
:
720 case GEN_PERF_QUERY_TYPE_RAW
:
724 case GEN_PERF_QUERY_TYPE_PIPELINE
:
725 bo
= obj
->pipeline_stats
.bo
;
729 unreachable("Unknown query type");
736 /* If the current batch references our results bo then we need to
739 if (brw_batch_references(&brw
->batch
, bo
))
740 perf_cfg
->vtbl
.batchbuffer_flush(brw
, __FILE__
, __LINE__
);
742 brw_bo_wait_rendering(bo
);
744 /* Due to a race condition between the OA unit signaling report
745 * availability and the report actually being written into memory,
746 * we need to wait for all the reports to come in before we can
749 if (obj
->queryinfo
->kind
== GEN_PERF_QUERY_TYPE_OA
||
750 obj
->queryinfo
->kind
== GEN_PERF_QUERY_TYPE_RAW
) {
751 while (!read_oa_samples_for_query(brw
, obj
))
757 brw_is_perf_query_ready(struct gl_context
*ctx
,
758 struct gl_perf_query_object
*o
)
760 struct brw_context
*brw
= brw_context(ctx
);
761 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
762 struct gen_perf_query_object
*obj
= brw_query
->query
;
767 switch (obj
->queryinfo
->kind
) {
768 case GEN_PERF_QUERY_TYPE_OA
:
769 case GEN_PERF_QUERY_TYPE_RAW
:
770 return (obj
->oa
.results_accumulated
||
772 !brw_batch_references(&brw
->batch
, obj
->oa
.bo
) &&
773 !brw_bo_busy(obj
->oa
.bo
) &&
774 read_oa_samples_for_query(brw
, obj
)));
775 case GEN_PERF_QUERY_TYPE_PIPELINE
:
776 return (obj
->pipeline_stats
.bo
&&
777 !brw_batch_references(&brw
->batch
, obj
->pipeline_stats
.bo
) &&
778 !brw_bo_busy(obj
->pipeline_stats
.bo
));
781 unreachable("Unknown query type");
789 read_slice_unslice_frequencies(struct brw_context
*brw
,
790 struct gen_perf_query_object
*obj
)
792 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
793 uint32_t *begin_report
= obj
->oa
.map
, *end_report
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
795 gen_perf_query_result_read_frequencies(&obj
->oa
.result
,
796 devinfo
, begin_report
, end_report
);
800 read_gt_frequency(struct brw_context
*brw
,
801 struct gen_perf_query_object
*obj
)
803 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
804 uint32_t start
= *((uint32_t *)(obj
->oa
.map
+ MI_FREQ_START_OFFSET_BYTES
)),
805 end
= *((uint32_t *)(obj
->oa
.map
+ MI_FREQ_END_OFFSET_BYTES
));
807 switch (devinfo
->gen
) {
810 obj
->oa
.gt_frequency
[0] = GET_FIELD(start
, GEN7_RPSTAT1_CURR_GT_FREQ
) * 50ULL;
811 obj
->oa
.gt_frequency
[1] = GET_FIELD(end
, GEN7_RPSTAT1_CURR_GT_FREQ
) * 50ULL;
816 obj
->oa
.gt_frequency
[0] = GET_FIELD(start
, GEN9_RPSTAT0_CURR_GT_FREQ
) * 50ULL / 3ULL;
817 obj
->oa
.gt_frequency
[1] = GET_FIELD(end
, GEN9_RPSTAT0_CURR_GT_FREQ
) * 50ULL / 3ULL;
820 unreachable("unexpected gen");
823 /* Put the numbers into Hz. */
824 obj
->oa
.gt_frequency
[0] *= 1000000ULL;
825 obj
->oa
.gt_frequency
[1] *= 1000000ULL;
829 get_oa_counter_data(struct brw_context
*brw
,
830 struct gen_perf_query_object
*obj
,
834 struct gen_perf_config
*perf
= brw
->perf_ctx
.perf
;
835 const struct gen_perf_query_info
*query
= obj
->queryinfo
;
836 int n_counters
= query
->n_counters
;
839 for (int i
= 0; i
< n_counters
; i
++) {
840 const struct gen_perf_query_counter
*counter
= &query
->counters
[i
];
841 uint64_t *out_uint64
;
843 size_t counter_size
= gen_perf_query_counter_get_size(counter
);
846 switch (counter
->data_type
) {
847 case GEN_PERF_COUNTER_DATA_TYPE_UINT64
:
848 out_uint64
= (uint64_t *)(data
+ counter
->offset
);
850 counter
->oa_counter_read_uint64(perf
, query
,
851 obj
->oa
.result
.accumulator
);
853 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT
:
854 out_float
= (float *)(data
+ counter
->offset
);
856 counter
->oa_counter_read_float(perf
, query
,
857 obj
->oa
.result
.accumulator
);
860 /* So far we aren't using uint32, double or bool32... */
861 unreachable("unexpected counter data type");
863 written
= counter
->offset
+ counter_size
;
871 get_pipeline_stats_data(struct brw_context
*brw
,
872 struct gen_perf_query_object
*obj
,
877 const struct gen_perf_query_info
*query
= obj
->queryinfo
;
878 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
879 struct gen_perf_config
*perf_cfg
= perf_ctx
->perf
;
880 int n_counters
= obj
->queryinfo
->n_counters
;
883 uint64_t *start
= perf_cfg
->vtbl
.bo_map(perf_ctx
->ctx
, obj
->pipeline_stats
.bo
, MAP_READ
);
884 uint64_t *end
= start
+ (STATS_BO_END_OFFSET_BYTES
/ sizeof(uint64_t));
886 for (int i
= 0; i
< n_counters
; i
++) {
887 const struct gen_perf_query_counter
*counter
= &query
->counters
[i
];
888 uint64_t value
= end
[i
] - start
[i
];
890 if (counter
->pipeline_stat
.numerator
!=
891 counter
->pipeline_stat
.denominator
) {
892 value
*= counter
->pipeline_stat
.numerator
;
893 value
/= counter
->pipeline_stat
.denominator
;
896 *((uint64_t *)p
) = value
;
900 perf_cfg
->vtbl
.bo_unmap(obj
->pipeline_stats
.bo
);
906 * Driver hook for glGetPerfQueryDataINTEL().
909 brw_get_perf_query_data(struct gl_context
*ctx
,
910 struct gl_perf_query_object
*o
,
913 GLuint
*bytes_written
)
915 struct brw_context
*brw
= brw_context(ctx
);
916 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
917 struct gen_perf_query_object
*obj
= brw_query
->query
;
920 assert(brw_is_perf_query_ready(ctx
, o
));
922 DBG("GetData(%d)\n", o
->Id
);
924 if (INTEL_DEBUG
& DEBUG_PERFMON
)
925 dump_perf_queries(brw
);
927 /* We expect that the frontend only calls this hook when it knows
928 * that results are available.
932 switch (obj
->queryinfo
->kind
) {
933 case GEN_PERF_QUERY_TYPE_OA
:
934 case GEN_PERF_QUERY_TYPE_RAW
:
935 if (!obj
->oa
.results_accumulated
) {
936 read_gt_frequency(brw
, obj
);
937 read_slice_unslice_frequencies(brw
, obj
);
938 accumulate_oa_reports(brw
, brw_query
);
939 assert(obj
->oa
.results_accumulated
);
941 brw
->perf_ctx
.perf
->vtbl
.bo_unmap(obj
->oa
.bo
);
944 if (obj
->queryinfo
->kind
== GEN_PERF_QUERY_TYPE_OA
) {
945 written
= get_oa_counter_data(brw
, obj
, data_size
, (uint8_t *)data
);
947 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
949 written
= gen_perf_query_result_write_mdapi((uint8_t *)data
, data_size
,
950 devinfo
, &obj
->oa
.result
,
951 obj
->oa
.gt_frequency
[0],
952 obj
->oa
.gt_frequency
[1]);
956 case GEN_PERF_QUERY_TYPE_PIPELINE
:
957 written
= get_pipeline_stats_data(brw
, obj
, data_size
, (uint8_t *)data
);
961 unreachable("Unknown query type");
966 *bytes_written
= written
;
969 static struct gl_perf_query_object
*
970 brw_new_perf_query_object(struct gl_context
*ctx
, unsigned query_index
)
972 struct brw_context
*brw
= brw_context(ctx
);
973 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
974 const struct gen_perf_query_info
*queryinfo
=
975 &perf_ctx
->perf
->queries
[query_index
];
976 struct gen_perf_query_object
*obj
=
977 calloc(1, sizeof(struct gen_perf_query_object
));
982 obj
->queryinfo
= queryinfo
;
984 perf_ctx
->n_query_instances
++;
986 struct brw_perf_query_object
*brw_query
= calloc(1, sizeof(struct brw_perf_query_object
));
987 if (unlikely(!brw_query
))
989 brw_query
->query
= obj
;
990 return &brw_query
->base
;
994 * Driver hook for glDeletePerfQueryINTEL().
997 brw_delete_perf_query(struct gl_context
*ctx
,
998 struct gl_perf_query_object
*o
)
1000 struct brw_context
*brw
= brw_context(ctx
);
1001 struct gen_perf_config
*perf_cfg
= brw
->perf_ctx
.perf
;
1002 struct brw_perf_query_object
*brw_query
= brw_perf_query(o
);
1003 struct gen_perf_query_object
*obj
= brw_query
->query
;
1004 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
1006 /* We can assume that the frontend waits for a query to complete
1007 * before ever calling into here, so we don't have to worry about
1008 * deleting an in-flight query object.
1011 assert(!o
->Used
|| o
->Ready
);
1013 DBG("Delete(%d)\n", o
->Id
);
1015 switch (obj
->queryinfo
->kind
) {
1016 case GEN_PERF_QUERY_TYPE_OA
:
1017 case GEN_PERF_QUERY_TYPE_RAW
:
1019 if (!obj
->oa
.results_accumulated
) {
1020 drop_from_unaccumulated_query_list(brw
, obj
);
1021 gen_perf_dec_n_users(perf_ctx
);
1024 perf_cfg
->vtbl
.bo_unreference(obj
->oa
.bo
);
1028 obj
->oa
.results_accumulated
= false;
1031 case GEN_PERF_QUERY_TYPE_PIPELINE
:
1032 if (obj
->pipeline_stats
.bo
) {
1033 perf_cfg
->vtbl
.bo_unreference(obj
->pipeline_stats
.bo
);
1034 obj
->pipeline_stats
.bo
= NULL
;
1039 unreachable("Unknown query type");
1043 /* As an indication that the INTEL_performance_query extension is no
1044 * longer in use, it's a good time to free our cache of sample
1045 * buffers and close any current i915-perf stream.
1047 if (--perf_ctx
->n_query_instances
== 0) {
1048 gen_perf_free_sample_bufs(perf_ctx
);
1049 gen_perf_close(perf_ctx
, obj
->queryinfo
);
1056 /******************************************************************************/
1059 init_pipeline_statistic_query_registers(struct brw_context
*brw
)
1061 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1062 struct gen_perf_config
*perf
= brw
->perf_ctx
.perf
;
1063 struct gen_perf_query_info
*query
=
1064 gen_perf_query_append_query_info(perf
, MAX_STAT_COUNTERS
);
1066 query
->kind
= GEN_PERF_QUERY_TYPE_PIPELINE
;
1067 query
->name
= "Pipeline Statistics Registers";
1069 gen_perf_query_info_add_basic_stat_reg(query
, IA_VERTICES_COUNT
,
1070 "N vertices submitted");
1071 gen_perf_query_info_add_basic_stat_reg(query
, IA_PRIMITIVES_COUNT
,
1072 "N primitives submitted");
1073 gen_perf_query_info_add_basic_stat_reg(query
, VS_INVOCATION_COUNT
,
1074 "N vertex shader invocations");
1076 if (devinfo
->gen
== 6) {
1077 gen_perf_query_info_add_stat_reg(query
, GEN6_SO_PRIM_STORAGE_NEEDED
, 1, 1,
1078 "SO_PRIM_STORAGE_NEEDED",
1079 "N geometry shader stream-out primitives (total)");
1080 gen_perf_query_info_add_stat_reg(query
, GEN6_SO_NUM_PRIMS_WRITTEN
, 1, 1,
1081 "SO_NUM_PRIMS_WRITTEN",
1082 "N geometry shader stream-out primitives (written)");
1084 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(0), 1, 1,
1085 "SO_PRIM_STORAGE_NEEDED (Stream 0)",
1086 "N stream-out (stream 0) primitives (total)");
1087 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(1), 1, 1,
1088 "SO_PRIM_STORAGE_NEEDED (Stream 1)",
1089 "N stream-out (stream 1) primitives (total)");
1090 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(2), 1, 1,
1091 "SO_PRIM_STORAGE_NEEDED (Stream 2)",
1092 "N stream-out (stream 2) primitives (total)");
1093 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(3), 1, 1,
1094 "SO_PRIM_STORAGE_NEEDED (Stream 3)",
1095 "N stream-out (stream 3) primitives (total)");
1096 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(0), 1, 1,
1097 "SO_NUM_PRIMS_WRITTEN (Stream 0)",
1098 "N stream-out (stream 0) primitives (written)");
1099 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(1), 1, 1,
1100 "SO_NUM_PRIMS_WRITTEN (Stream 1)",
1101 "N stream-out (stream 1) primitives (written)");
1102 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(2), 1, 1,
1103 "SO_NUM_PRIMS_WRITTEN (Stream 2)",
1104 "N stream-out (stream 2) primitives (written)");
1105 gen_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(3), 1, 1,
1106 "SO_NUM_PRIMS_WRITTEN (Stream 3)",
1107 "N stream-out (stream 3) primitives (written)");
1110 gen_perf_query_info_add_basic_stat_reg(query
, HS_INVOCATION_COUNT
,
1111 "N TCS shader invocations");
1112 gen_perf_query_info_add_basic_stat_reg(query
, DS_INVOCATION_COUNT
,
1113 "N TES shader invocations");
1115 gen_perf_query_info_add_basic_stat_reg(query
, GS_INVOCATION_COUNT
,
1116 "N geometry shader invocations");
1117 gen_perf_query_info_add_basic_stat_reg(query
, GS_PRIMITIVES_COUNT
,
1118 "N geometry shader primitives emitted");
1120 gen_perf_query_info_add_basic_stat_reg(query
, CL_INVOCATION_COUNT
,
1121 "N primitives entering clipping");
1122 gen_perf_query_info_add_basic_stat_reg(query
, CL_PRIMITIVES_COUNT
,
1123 "N primitives leaving clipping");
1125 if (devinfo
->is_haswell
|| devinfo
->gen
== 8) {
1126 gen_perf_query_info_add_stat_reg(query
, PS_INVOCATION_COUNT
, 1, 4,
1127 "N fragment shader invocations",
1128 "N fragment shader invocations");
1130 gen_perf_query_info_add_basic_stat_reg(query
, PS_INVOCATION_COUNT
,
1131 "N fragment shader invocations");
1134 gen_perf_query_info_add_basic_stat_reg(query
, PS_DEPTH_COUNT
,
1135 "N z-pass fragments");
1137 if (devinfo
->gen
>= 7) {
1138 gen_perf_query_info_add_basic_stat_reg(query
, CS_INVOCATION_COUNT
,
1139 "N compute shader invocations");
1142 query
->data_size
= sizeof(uint64_t) * query
->n_counters
;
1145 /* gen_device_info will have incorrect default topology values for unsupported kernels.
1146 * verify kernel support to ensure OA metrics are accurate.
1149 oa_metrics_kernel_support(int fd
, const struct gen_device_info
*devinfo
)
1151 if (devinfo
->gen
>= 10) {
1152 /* topology uAPI required for CNL+ (kernel 4.17+) make a call to the api
1155 struct drm_i915_query_item item
= {
1156 .query_id
= DRM_I915_QUERY_TOPOLOGY_INFO
,
1158 struct drm_i915_query query
= {
1160 .items_ptr
= (uintptr_t) &item
,
1163 /* kernel 4.17+ supports the query */
1164 return drmIoctl(fd
, DRM_IOCTL_I915_QUERY
, &query
) == 0;
1167 if (devinfo
->gen
>= 8) {
1168 /* 4.13+ api required for gen8 - gen9 */
1170 struct drm_i915_getparam gp
= {
1171 .param
= I915_PARAM_SLICE_MASK
,
1174 /* kernel 4.13+ supports this parameter */
1175 return drmIoctl(fd
, DRM_IOCTL_I915_GETPARAM
, &gp
) == 0;
1178 if (devinfo
->gen
== 7)
1179 /* default topology values are correct for HSW */
1182 /* oa not supported before gen 7*/
1187 brw_oa_bo_alloc(void *bufmgr
, const char *name
, uint64_t size
)
1189 return brw_bo_alloc(bufmgr
, name
, size
, BRW_MEMZONE_OTHER
);
1193 brw_oa_emit_mi_report_perf_count(void *c
,
1195 uint32_t offset_in_bytes
,
1198 struct brw_context
*ctx
= c
;
1199 ctx
->vtbl
.emit_mi_report_perf_count(ctx
,
1205 typedef void (*bo_unreference_t
)(void *);
1206 typedef void *(*bo_map_t
)(void *, void *, unsigned flags
);
1207 typedef void (*bo_unmap_t
)(void *);
1208 typedef void (* emit_mi_report_t
)(void *, void *, uint32_t, uint32_t);
1209 typedef void (*emit_mi_flush_t
)(void *);
1212 brw_oa_batchbuffer_flush(void *c
, const char *file
, int line
)
1214 struct brw_context
*ctx
= c
;
1215 _intel_batchbuffer_flush_fence(ctx
, -1, NULL
, file
, line
);
1218 typedef void (*capture_frequency_stat_register_t
)(void *, void *, uint32_t );
1219 typedef void (*store_register_mem64_t
)(void *ctx
, void *bo
,
1220 uint32_t reg
, uint32_t offset
);
1223 brw_init_perf_query_info(struct gl_context
*ctx
)
1225 struct brw_context
*brw
= brw_context(ctx
);
1226 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1228 struct gen_perf_context
*perf_ctx
= &brw
->perf_ctx
;
1230 return perf_ctx
->perf
->n_queries
;
1232 perf_ctx
->perf
= gen_perf_new(brw
);
1233 struct gen_perf_config
*perf_cfg
= perf_ctx
->perf
;
1235 perf_cfg
->vtbl
.bo_alloc
= brw_oa_bo_alloc
;
1236 perf_cfg
->vtbl
.bo_unreference
= (bo_unreference_t
)brw_bo_unreference
;
1237 perf_cfg
->vtbl
.bo_map
= (bo_map_t
)brw_bo_map
;
1238 perf_cfg
->vtbl
.bo_unmap
= (bo_unmap_t
)brw_bo_unmap
;
1239 perf_cfg
->vtbl
.emit_mi_flush
= (emit_mi_flush_t
)brw_emit_mi_flush
;
1240 perf_cfg
->vtbl
.emit_mi_report_perf_count
=
1241 (emit_mi_report_t
)brw_oa_emit_mi_report_perf_count
;
1242 perf_cfg
->vtbl
.batchbuffer_flush
= brw_oa_batchbuffer_flush
;
1243 perf_cfg
->vtbl
.capture_frequency_stat_register
=
1244 (capture_frequency_stat_register_t
) capture_frequency_stat_register
;
1245 perf_cfg
->vtbl
.store_register_mem64
=
1246 (store_register_mem64_t
) brw_store_register_mem64
;
1248 gen_perf_init_context(perf_ctx
, perf_cfg
, brw
, brw
->bufmgr
, devinfo
,
1249 brw
->hw_ctx
, brw
->screen
->driScrnPriv
->fd
);
1251 init_pipeline_statistic_query_registers(brw
);
1252 gen_perf_query_register_mdapi_statistic_query(devinfo
, perf_cfg
);
1254 if ((oa_metrics_kernel_support(perf_ctx
->drm_fd
, devinfo
)) &&
1255 (gen_perf_load_oa_metrics(perf_cfg
, perf_ctx
->drm_fd
, devinfo
)))
1256 gen_perf_query_register_mdapi_oa_query(devinfo
, perf_cfg
);
1258 return perf_cfg
->n_queries
;
1262 brw_init_performance_queries(struct brw_context
*brw
)
1264 struct gl_context
*ctx
= &brw
->ctx
;
1266 ctx
->Driver
.InitPerfQueryInfo
= brw_init_perf_query_info
;
1267 ctx
->Driver
.GetPerfQueryInfo
= brw_get_perf_query_info
;
1268 ctx
->Driver
.GetPerfCounterInfo
= brw_get_perf_counter_info
;
1269 ctx
->Driver
.NewPerfQueryObject
= brw_new_perf_query_object
;
1270 ctx
->Driver
.DeletePerfQuery
= brw_delete_perf_query
;
1271 ctx
->Driver
.BeginPerfQuery
= brw_begin_perf_query
;
1272 ctx
->Driver
.EndPerfQuery
= brw_end_perf_query
;
1273 ctx
->Driver
.WaitPerfQuery
= brw_wait_perf_query
;
1274 ctx
->Driver
.IsPerfQueryReady
= brw_is_perf_query_ready
;
1275 ctx
->Driver
.GetPerfQueryData
= brw_get_perf_query_data
;