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+)
45 /* put before sys/types.h to silence glibc warnings */
47 #include <sys/mkdev.h>
49 #ifdef MAJOR_IN_SYSMACROS
50 #include <sys/sysmacros.h>
52 #include <sys/types.h>
56 #include <sys/ioctl.h>
61 #include "main/hash.h"
62 #include "main/macros.h"
63 #include "main/mtypes.h"
64 #include "main/performance_query.h"
66 #include "util/bitset.h"
67 #include "util/ralloc.h"
68 #include "util/hash_table.h"
69 #include "util/list.h"
71 #include "brw_context.h"
72 #include "brw_defines.h"
73 #include "brw_performance_query.h"
74 #include "brw_oa_metrics.h"
75 #include "intel_batchbuffer.h"
77 #define FILE_DEBUG_FLAG DEBUG_PERFMON
79 #define OAREPORT_REASON_MASK 0x3f
80 #define OAREPORT_REASON_SHIFT 19
81 #define OAREPORT_REASON_TIMER (1<<0)
82 #define OAREPORT_REASON_TRIGGER1 (1<<1)
83 #define OAREPORT_REASON_TRIGGER2 (1<<2)
84 #define OAREPORT_REASON_CTX_SWITCH (1<<3)
85 #define OAREPORT_REASON_GO_TRANSITION (1<<4)
87 #define I915_PERF_OA_SAMPLE_SIZE (8 + /* drm_i915_perf_record_header */ \
88 256) /* OA counter report */
91 * Periodic OA samples are read() into these buffer structures via the
92 * i915 perf kernel interface and appended to the
93 * brw->perfquery.sample_buffers linked list. When we process the
94 * results of an OA metrics query we need to consider all the periodic
95 * samples between the Begin and End MI_REPORT_PERF_COUNT command
98 * 'Periodic' is a simplification as there are other automatic reports
99 * written by the hardware also buffered here.
101 * Considering three queries, A, B and C:
104 * ________________A_________________
106 * | ________B_________ _____C___________
109 * And an illustration of sample buffers read over this time frame:
110 * [HEAD ][ ][ ][ ][ ][ ][ ][ ][TAIL ]
112 * These nodes may hold samples for query A:
113 * [ ][ ][ A ][ A ][ A ][ A ][ A ][ ][ ]
115 * These nodes may hold samples for query B:
116 * [ ][ ][ B ][ B ][ B ][ ][ ][ ][ ]
118 * These nodes may hold samples for query C:
119 * [ ][ ][ ][ ][ ][ C ][ C ][ C ][ ]
121 * The illustration assumes we have an even distribution of periodic
122 * samples so all nodes have the same size plotted against time:
124 * Note, to simplify code, the list is never empty.
126 * With overlapping queries we can see that periodic OA reports may
127 * relate to multiple queries and care needs to be take to keep
128 * track of sample buffers until there are no queries that might
129 * depend on their contents.
131 * We use a node ref counting system where a reference ensures that a
132 * node and all following nodes can't be freed/recycled until the
133 * reference drops to zero.
135 * E.g. with a ref of one here:
136 * [ 0 ][ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ]
138 * These nodes could be freed or recycled ("reaped"):
141 * These must be preserved until the leading ref drops to zero:
142 * [ 1 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ]
144 * When a query starts we take a reference on the current tail of
145 * the list, knowing that no already-buffered samples can possibly
146 * relate to the newly-started query. A pointer to this node is
147 * also saved in the query object's ->oa.samples_head.
149 * E.g. starting query A while there are two nodes in .sample_buffers:
150 * ________________A________
154 * ^_______ Add a reference and store pointer to node in
157 * Moving forward to when the B query starts with no new buffer nodes:
158 * (for reference, i915 perf reads() are only done when queries finish)
159 * ________________A_______
164 * ^_______ Add a reference and store pointer to
165 * node in B->oa.samples_head
167 * Once a query is finished, after an OA query has become 'Ready',
168 * once the End OA report has landed and after we we have processed
169 * all the intermediate periodic samples then we drop the
170 * ->oa.samples_head reference we took at the start.
172 * So when the B query has finished we have:
173 * ________________A________
174 * | ______B___________
176 * [ 0 ][ 1 ][ 0 ][ 0 ][ 0 ]
177 * ^_______ Drop B->oa.samples_head reference
179 * We still can't free these due to the A->oa.samples_head ref:
180 * [ 1 ][ 0 ][ 0 ][ 0 ]
182 * When the A query finishes: (note there's a new ref for C's samples_head)
183 * ________________A_________________
187 * [ 0 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ][ 0 ]
188 * ^_______ Drop A->oa.samples_head reference
190 * And we can now reap these nodes up to the C->oa.samples_head:
191 * [ X ][ X ][ X ][ X ]
192 * keeping -> [ 1 ][ 0 ][ 0 ]
194 * We reap old sample buffers each time we finish processing an OA
195 * query by iterating the sample_buffers list from the head until we
196 * find a referenced node and stop.
198 * Reaped buffers move to a perfquery.free_sample_buffers list and
199 * when we come to read() we first look to recycle a buffer from the
200 * free_sample_buffers list before allocating a new buffer.
202 struct brw_oa_sample_buf
{
203 struct exec_node link
;
206 uint8_t buf
[I915_PERF_OA_SAMPLE_SIZE
* 10];
207 uint32_t last_timestamp
;
210 /** Downcasting convenience macro. */
211 static inline struct brw_perf_query_object
*
212 brw_perf_query(struct gl_perf_query_object
*o
)
214 return (struct brw_perf_query_object
*) o
;
217 #define MI_RPC_BO_SIZE 4096
218 #define MI_RPC_BO_END_OFFSET_BYTES (MI_RPC_BO_SIZE / 2)
220 /******************************************************************************/
223 read_file_uint64(const char *file
, uint64_t *val
)
231 while ((n
= read(fd
, buf
, sizeof (buf
) - 1)) < 0 &&
238 *val
= strtoull(buf
, NULL
, 0);
244 read_sysfs_drm_device_file_uint64(struct brw_context
*brw
,
251 len
= snprintf(buf
, sizeof(buf
), "%s/%s",
252 brw
->perfquery
.sysfs_dev_dir
, file
);
253 if (len
< 0 || len
>= sizeof(buf
)) {
254 DBG("Failed to concatenate sys filename to read u64 from\n");
258 return read_file_uint64(buf
, value
);
261 /******************************************************************************/
264 brw_is_perf_query_ready(struct gl_context
*ctx
,
265 struct gl_perf_query_object
*o
);
268 dump_perf_query_callback(GLuint id
, void *query_void
, void *brw_void
)
270 struct gl_context
*ctx
= brw_void
;
271 struct gl_perf_query_object
*o
= query_void
;
272 struct brw_perf_query_object
*obj
= query_void
;
274 switch (obj
->query
->kind
) {
276 DBG("%4d: %-6s %-8s BO: %-4s OA data: %-10s %-15s\n",
278 o
->Used
? "Dirty," : "New,",
279 o
->Active
? "Active," : (o
->Ready
? "Ready," : "Pending,"),
280 obj
->oa
.bo
? "yes," : "no,",
281 brw_is_perf_query_ready(ctx
, o
) ? "ready," : "not ready,",
282 obj
->oa
.results_accumulated
? "accumulated" : "not accumulated");
285 DBG("%4d: %-6s %-8s BO: %-4s\n",
287 o
->Used
? "Dirty," : "New,",
288 o
->Active
? "Active," : (o
->Ready
? "Ready," : "Pending,"),
289 obj
->pipeline_stats
.bo
? "yes" : "no");
292 unreachable("Unknown query type");
298 dump_perf_queries(struct brw_context
*brw
)
300 struct gl_context
*ctx
= &brw
->ctx
;
301 DBG("Queries: (Open queries = %d, OA users = %d)\n",
302 brw
->perfquery
.n_active_oa_queries
, brw
->perfquery
.n_oa_users
);
303 _mesa_HashWalk(ctx
->PerfQuery
.Objects
, dump_perf_query_callback
, brw
);
306 /******************************************************************************/
308 static struct brw_oa_sample_buf
*
309 get_free_sample_buf(struct brw_context
*brw
)
311 struct exec_node
*node
= exec_list_pop_head(&brw
->perfquery
.free_sample_buffers
);
312 struct brw_oa_sample_buf
*buf
;
315 buf
= exec_node_data(struct brw_oa_sample_buf
, node
, link
);
317 buf
= ralloc_size(brw
, sizeof(*buf
));
319 exec_node_init(&buf
->link
);
328 reap_old_sample_buffers(struct brw_context
*brw
)
330 struct exec_node
*tail_node
=
331 exec_list_get_tail(&brw
->perfquery
.sample_buffers
);
332 struct brw_oa_sample_buf
*tail_buf
=
333 exec_node_data(struct brw_oa_sample_buf
, tail_node
, link
);
335 /* Remove all old, unreferenced sample buffers walking forward from
336 * the head of the list, except always leave at least one node in
337 * the list so we always have a node to reference when we Begin
340 foreach_list_typed_safe(struct brw_oa_sample_buf
, buf
, link
,
341 &brw
->perfquery
.sample_buffers
)
343 if (buf
->refcount
== 0 && buf
!= tail_buf
) {
344 exec_node_remove(&buf
->link
);
345 exec_list_push_head(&brw
->perfquery
.free_sample_buffers
, &buf
->link
);
352 free_sample_bufs(struct brw_context
*brw
)
354 foreach_list_typed_safe(struct brw_oa_sample_buf
, buf
, link
,
355 &brw
->perfquery
.free_sample_buffers
)
358 exec_list_make_empty(&brw
->perfquery
.free_sample_buffers
);
361 /******************************************************************************/
364 * Driver hook for glGetPerfQueryInfoINTEL().
367 brw_get_perf_query_info(struct gl_context
*ctx
,
368 unsigned query_index
,
374 struct brw_context
*brw
= brw_context(ctx
);
375 const struct brw_perf_query_info
*query
=
376 &brw
->perfquery
.queries
[query_index
];
379 *data_size
= query
->data_size
;
380 *n_counters
= query
->n_counters
;
382 switch (query
->kind
) {
384 *n_active
= brw
->perfquery
.n_active_oa_queries
;
388 *n_active
= brw
->perfquery
.n_active_pipeline_stats_queries
;
392 unreachable("Unknown query type");
398 * Driver hook for glGetPerfCounterInfoINTEL().
401 brw_get_perf_counter_info(struct gl_context
*ctx
,
402 unsigned query_index
,
403 unsigned counter_index
,
409 GLuint
*data_type_enum
,
412 struct brw_context
*brw
= brw_context(ctx
);
413 const struct brw_perf_query_info
*query
=
414 &brw
->perfquery
.queries
[query_index
];
415 const struct brw_perf_query_counter
*counter
=
416 &query
->counters
[counter_index
];
418 *name
= counter
->name
;
419 *desc
= counter
->desc
;
420 *offset
= counter
->offset
;
421 *data_size
= counter
->size
;
422 *type_enum
= counter
->type
;
423 *data_type_enum
= counter
->data_type
;
424 *raw_max
= counter
->raw_max
;
427 /******************************************************************************/
430 * Emit MI_STORE_REGISTER_MEM commands to capture all of the
431 * pipeline statistics for the performance query object.
434 snapshot_statistics_registers(struct brw_context
*brw
,
435 struct brw_perf_query_object
*obj
,
436 uint32_t offset_in_bytes
)
438 const struct brw_perf_query_info
*query
= obj
->query
;
439 const int n_counters
= query
->n_counters
;
441 for (int i
= 0; i
< n_counters
; i
++) {
442 const struct brw_perf_query_counter
*counter
= &query
->counters
[i
];
444 assert(counter
->data_type
== GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL
);
446 brw_store_register_mem64(brw
, obj
->pipeline_stats
.bo
,
447 counter
->pipeline_stat
.reg
,
448 offset_in_bytes
+ i
* sizeof(uint64_t));
453 * Add a query to the global list of "unaccumulated queries."
455 * Queries are tracked here until all the associated OA reports have
456 * been accumulated via accumulate_oa_reports() after the end
457 * MI_REPORT_PERF_COUNT has landed in query->oa.bo.
460 add_to_unaccumulated_query_list(struct brw_context
*brw
,
461 struct brw_perf_query_object
*obj
)
463 if (brw
->perfquery
.unaccumulated_elements
>=
464 brw
->perfquery
.unaccumulated_array_size
)
466 brw
->perfquery
.unaccumulated_array_size
*= 1.5;
467 brw
->perfquery
.unaccumulated
=
468 reralloc(brw
, brw
->perfquery
.unaccumulated
,
469 struct brw_perf_query_object
*,
470 brw
->perfquery
.unaccumulated_array_size
);
473 brw
->perfquery
.unaccumulated
[brw
->perfquery
.unaccumulated_elements
++] = obj
;
477 * Remove a query from the global list of unaccumulated queries once
478 * after successfully accumulating the OA reports associated with the
479 * query in accumulate_oa_reports() or when discarding unwanted query
483 drop_from_unaccumulated_query_list(struct brw_context
*brw
,
484 struct brw_perf_query_object
*obj
)
486 for (int i
= 0; i
< brw
->perfquery
.unaccumulated_elements
; i
++) {
487 if (brw
->perfquery
.unaccumulated
[i
] == obj
) {
488 int last_elt
= --brw
->perfquery
.unaccumulated_elements
;
491 brw
->perfquery
.unaccumulated
[i
] = NULL
;
493 brw
->perfquery
.unaccumulated
[i
] =
494 brw
->perfquery
.unaccumulated
[last_elt
];
501 /* Drop our samples_head reference so that associated periodic
502 * sample data buffers can potentially be reaped if they aren't
503 * referenced by any other queries...
506 struct brw_oa_sample_buf
*buf
=
507 exec_node_data(struct brw_oa_sample_buf
, obj
->oa
.samples_head
, link
);
509 assert(buf
->refcount
> 0);
512 obj
->oa
.samples_head
= NULL
;
514 reap_old_sample_buffers(brw
);
518 * Given pointers to starting and ending OA snapshots, add the deltas for each
519 * counter to the results.
522 add_deltas(struct brw_context
*brw
,
523 struct brw_perf_query_object
*obj
,
524 const uint32_t *start
,
527 const struct brw_perf_query_info
*query
= obj
->query
;
528 uint64_t *accumulator
= obj
->oa
.accumulator
;
532 obj
->oa
.reports_accumulated
++;
534 switch (query
->oa_format
) {
535 case I915_OA_FORMAT_A32u40_A4u32_B8_C8
:
536 brw_perf_query_accumulate_uint32(start
+ 1, end
+ 1, accumulator
+ idx
++); /* timestamp */
537 brw_perf_query_accumulate_uint32(start
+ 3, end
+ 3, accumulator
+ idx
++); /* clock */
539 /* 32x 40bit A counters... */
540 for (i
= 0; i
< 32; i
++)
541 brw_perf_query_accumulate_uint40(i
, start
, end
, accumulator
+ idx
++);
543 /* 4x 32bit A counters... */
544 for (i
= 0; i
< 4; i
++)
545 brw_perf_query_accumulate_uint32(start
+ 36 + i
, end
+ 36 + i
,
546 accumulator
+ idx
++);
548 /* 8x 32bit B counters + 8x 32bit C counters... */
549 for (i
= 0; i
< 16; i
++)
550 brw_perf_query_accumulate_uint32(start
+ 48 + i
, end
+ 48 + i
,
551 accumulator
+ idx
++);
554 case I915_OA_FORMAT_A45_B8_C8
:
555 brw_perf_query_accumulate_uint32(start
+ 1, end
+ 1, accumulator
); /* timestamp */
557 for (i
= 0; i
< 61; i
++)
558 brw_perf_query_accumulate_uint32(start
+ 3 + i
, end
+ 3 + i
, accumulator
+ 1 + i
);
562 unreachable("Can't accumulate OA counters in unknown format");
567 inc_n_oa_users(struct brw_context
*brw
)
569 if (brw
->perfquery
.n_oa_users
== 0 &&
570 drmIoctl(brw
->perfquery
.oa_stream_fd
,
571 I915_PERF_IOCTL_ENABLE
, 0) < 0)
575 ++brw
->perfquery
.n_oa_users
;
581 dec_n_oa_users(struct brw_context
*brw
)
583 /* Disabling the i915 perf stream will effectively disable the OA
584 * counters. Note it's important to be sure there are no outstanding
585 * MI_RPC commands at this point since they could stall the CS
586 * indefinitely once OACONTROL is disabled.
588 --brw
->perfquery
.n_oa_users
;
589 if (brw
->perfquery
.n_oa_users
== 0 &&
590 drmIoctl(brw
->perfquery
.oa_stream_fd
, I915_PERF_IOCTL_DISABLE
, 0) < 0)
592 DBG("WARNING: Error disabling i915 perf stream: %m\n");
596 /* In general if we see anything spurious while accumulating results,
597 * we don't try and continue accumulating the current query, hoping
598 * for the best, we scrap anything outstanding, and then hope for the
599 * best with new queries.
602 discard_all_queries(struct brw_context
*brw
)
604 while (brw
->perfquery
.unaccumulated_elements
) {
605 struct brw_perf_query_object
*obj
= brw
->perfquery
.unaccumulated
[0];
607 obj
->oa
.results_accumulated
= true;
608 drop_from_unaccumulated_query_list(brw
, brw
->perfquery
.unaccumulated
[0]);
615 OA_READ_STATUS_ERROR
,
616 OA_READ_STATUS_UNFINISHED
,
617 OA_READ_STATUS_FINISHED
,
620 static enum OaReadStatus
621 read_oa_samples_until(struct brw_context
*brw
,
622 uint32_t start_timestamp
,
623 uint32_t end_timestamp
)
625 struct exec_node
*tail_node
=
626 exec_list_get_tail(&brw
->perfquery
.sample_buffers
);
627 struct brw_oa_sample_buf
*tail_buf
=
628 exec_node_data(struct brw_oa_sample_buf
, tail_node
, link
);
629 uint32_t last_timestamp
= tail_buf
->last_timestamp
;
632 struct brw_oa_sample_buf
*buf
= get_free_sample_buf(brw
);
636 while ((len
= read(brw
->perfquery
.oa_stream_fd
, buf
->buf
,
637 sizeof(buf
->buf
))) < 0 && errno
== EINTR
)
641 exec_list_push_tail(&brw
->perfquery
.free_sample_buffers
, &buf
->link
);
645 return ((last_timestamp
- start_timestamp
) >=
646 (end_timestamp
- start_timestamp
)) ?
647 OA_READ_STATUS_FINISHED
:
648 OA_READ_STATUS_UNFINISHED
;
650 DBG("Error reading i915 perf samples: %m\n");
653 DBG("Spurious EOF reading i915 perf samples\n");
655 return OA_READ_STATUS_ERROR
;
659 exec_list_push_tail(&brw
->perfquery
.sample_buffers
, &buf
->link
);
661 /* Go through the reports and update the last timestamp. */
663 while (offset
< buf
->len
) {
664 const struct drm_i915_perf_record_header
*header
=
665 (const struct drm_i915_perf_record_header
*) &buf
->buf
[offset
];
666 uint32_t *report
= (uint32_t *) (header
+ 1);
668 if (header
->type
== DRM_I915_PERF_RECORD_SAMPLE
)
669 last_timestamp
= report
[1];
671 offset
+= header
->size
;
674 buf
->last_timestamp
= last_timestamp
;
677 unreachable("not reached");
678 return OA_READ_STATUS_ERROR
;
682 * Try to read all the reports until either the delimiting timestamp
683 * or an error arises.
686 read_oa_samples_for_query(struct brw_context
*brw
,
687 struct brw_perf_query_object
*obj
)
693 /* We need the MI_REPORT_PERF_COUNT to land before we can start
695 assert(!brw_batch_references(&brw
->batch
, obj
->oa
.bo
) &&
696 !brw_bo_busy(obj
->oa
.bo
));
698 /* Map the BO once here and let accumulate_oa_reports() unmap
700 if (obj
->oa
.map
== NULL
)
701 obj
->oa
.map
= brw_bo_map(brw
, obj
->oa
.bo
, MAP_READ
);
703 start
= last
= obj
->oa
.map
;
704 end
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
706 if (start
[0] != obj
->oa
.begin_report_id
) {
707 DBG("Spurious start report id=%"PRIu32
"\n", start
[0]);
710 if (end
[0] != (obj
->oa
.begin_report_id
+ 1)) {
711 DBG("Spurious end report id=%"PRIu32
"\n", end
[0]);
715 /* Read the reports until the end timestamp. */
716 switch (read_oa_samples_until(brw
, start
[1], end
[1])) {
717 case OA_READ_STATUS_ERROR
:
718 /* Fallthrough and let accumulate_oa_reports() deal with the
720 case OA_READ_STATUS_FINISHED
:
722 case OA_READ_STATUS_UNFINISHED
:
726 unreachable("invalid read status");
731 * Accumulate raw OA counter values based on deltas between pairs of
734 * Accumulation starts from the first report captured via
735 * MI_REPORT_PERF_COUNT (MI_RPC) by brw_begin_perf_query() until the
736 * last MI_RPC report requested by brw_end_perf_query(). Between these
737 * two reports there may also some number of periodically sampled OA
738 * reports collected via the i915 perf interface - depending on the
739 * duration of the query.
741 * These periodic snapshots help to ensure we handle counter overflow
742 * correctly by being frequent enough to ensure we don't miss multiple
743 * overflows of a counter between snapshots. For Gen8+ the i915 perf
744 * snapshots provide the extra context-switch reports that let us
745 * subtract out the progress of counters associated with other
746 * contexts running on the system.
749 accumulate_oa_reports(struct brw_context
*brw
,
750 struct brw_perf_query_object
*obj
)
752 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
753 struct gl_perf_query_object
*o
= &obj
->base
;
757 struct exec_node
*first_samples_node
;
759 int out_duration
= 0;
762 assert(obj
->oa
.map
!= NULL
);
764 start
= last
= obj
->oa
.map
;
765 end
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
767 if (start
[0] != obj
->oa
.begin_report_id
) {
768 DBG("Spurious start report id=%"PRIu32
"\n", start
[0]);
771 if (end
[0] != (obj
->oa
.begin_report_id
+ 1)) {
772 DBG("Spurious end report id=%"PRIu32
"\n", end
[0]);
776 obj
->oa
.hw_id
= start
[2];
778 /* See if we have any periodic reports to accumulate too... */
780 /* N.B. The oa.samples_head was set when the query began and
781 * pointed to the tail of the brw->perfquery.sample_buffers list at
782 * the time the query started. Since the buffer existed before the
783 * first MI_REPORT_PERF_COUNT command was emitted we therefore know
784 * that no data in this particular node's buffer can possibly be
785 * associated with the query - so skip ahead one...
787 first_samples_node
= obj
->oa
.samples_head
->next
;
789 foreach_list_typed_from(struct brw_oa_sample_buf
, buf
, link
,
790 &brw
->perfquery
.sample_buffers
,
795 while (offset
< buf
->len
) {
796 const struct drm_i915_perf_record_header
*header
=
797 (const struct drm_i915_perf_record_header
*)(buf
->buf
+ offset
);
799 assert(header
->size
!= 0);
800 assert(header
->size
<= buf
->len
);
802 offset
+= header
->size
;
804 switch (header
->type
) {
805 case DRM_I915_PERF_RECORD_SAMPLE
: {
806 uint32_t *report
= (uint32_t *)(header
+ 1);
809 /* Ignore reports that come before the start marker.
810 * (Note: takes care to allow overflow of 32bit timestamps)
812 if (brw_timebase_scale(brw
, report
[1] - start
[1]) > 5000000000)
815 /* Ignore reports that come after the end marker.
816 * (Note: takes care to allow overflow of 32bit timestamps)
818 if (brw_timebase_scale(brw
, report
[1] - end
[1]) <= 5000000000)
821 /* For Gen8+ since the counters continue while other
822 * contexts are running we need to discount any unrelated
823 * deltas. The hardware automatically generates a report
824 * on context switch which gives us a new reference point
825 * to continuing adding deltas from.
827 * For Haswell we can rely on the HW to stop the progress
828 * of OA counters while any other context is acctive.
830 if (devinfo
->gen
>= 8) {
831 if (in_ctx
&& report
[2] != obj
->oa
.hw_id
) {
832 DBG("i915 perf: Switch AWAY (observed by ID change)\n");
835 } else if (in_ctx
== false && report
[2] == obj
->oa
.hw_id
) {
836 DBG("i915 perf: Switch TO\n");
839 /* From experimentation in IGT, we found that the OA unit
840 * might label some report as "idle" (using an invalid
841 * context ID), right after a report for a given context.
842 * Deltas generated by those reports actually belong to the
843 * previous context, even though they're not labelled as
846 * We didn't *really* Switch AWAY in the case that we e.g.
847 * saw a single periodic report while idle...
849 if (out_duration
>= 1)
852 assert(report
[2] == obj
->oa
.hw_id
);
853 DBG("i915 perf: Continuation IN\n");
855 assert(report
[2] != obj
->oa
.hw_id
);
856 DBG("i915 perf: Continuation OUT\n");
863 add_deltas(brw
, obj
, last
, report
);
870 case DRM_I915_PERF_RECORD_OA_BUFFER_LOST
:
871 DBG("i915 perf: OA error: all reports lost\n");
873 case DRM_I915_PERF_RECORD_OA_REPORT_LOST
:
874 DBG("i915 perf: OA report lost\n");
882 add_deltas(brw
, obj
, last
, end
);
884 DBG("Marking %d accumulated - results gathered\n", o
->Id
);
886 obj
->oa
.results_accumulated
= true;
887 drop_from_unaccumulated_query_list(brw
, obj
);
894 discard_all_queries(brw
);
897 /******************************************************************************/
900 open_i915_perf_oa_stream(struct brw_context
*brw
,
907 uint64_t properties
[] = {
908 /* Single context sampling */
909 DRM_I915_PERF_PROP_CTX_HANDLE
, ctx_id
,
911 /* Include OA reports in samples */
912 DRM_I915_PERF_PROP_SAMPLE_OA
, true,
914 /* OA unit configuration */
915 DRM_I915_PERF_PROP_OA_METRICS_SET
, metrics_set_id
,
916 DRM_I915_PERF_PROP_OA_FORMAT
, report_format
,
917 DRM_I915_PERF_PROP_OA_EXPONENT
, period_exponent
,
919 struct drm_i915_perf_open_param param
= {
920 .flags
= I915_PERF_FLAG_FD_CLOEXEC
|
921 I915_PERF_FLAG_FD_NONBLOCK
|
922 I915_PERF_FLAG_DISABLED
,
923 .num_properties
= ARRAY_SIZE(properties
) / 2,
924 .properties_ptr
= (uintptr_t) properties
,
926 int fd
= drmIoctl(drm_fd
, DRM_IOCTL_I915_PERF_OPEN
, ¶m
);
928 DBG("Error opening i915 perf OA stream: %m\n");
932 brw
->perfquery
.oa_stream_fd
= fd
;
934 brw
->perfquery
.current_oa_metrics_set_id
= metrics_set_id
;
935 brw
->perfquery
.current_oa_format
= report_format
;
941 close_perf(struct brw_context
*brw
)
943 if (brw
->perfquery
.oa_stream_fd
!= -1) {
944 close(brw
->perfquery
.oa_stream_fd
);
945 brw
->perfquery
.oa_stream_fd
= -1;
950 * Driver hook for glBeginPerfQueryINTEL().
953 brw_begin_perf_query(struct gl_context
*ctx
,
954 struct gl_perf_query_object
*o
)
956 struct brw_context
*brw
= brw_context(ctx
);
957 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
958 const struct brw_perf_query_info
*query
= obj
->query
;
960 /* We can assume the frontend hides mistaken attempts to Begin a
961 * query object multiple times before its End. Similarly if an
962 * application reuses a query object before results have arrived
963 * the frontend will wait for prior results so we don't need
964 * to support abandoning in-flight results.
967 assert(!o
->Used
|| o
->Ready
); /* no in-flight query to worry about */
969 DBG("Begin(%d)\n", o
->Id
);
971 /* XXX: We have to consider that the command parser unit that parses batch
972 * buffer commands and is used to capture begin/end counter snapshots isn't
973 * implicitly synchronized with what's currently running across other GPU
974 * units (such as the EUs running shaders) that the performance counters are
977 * The intention of performance queries is to measure the work associated
978 * with commands between the begin/end delimiters and so for that to be the
979 * case we need to explicitly synchronize the parsing of commands to capture
980 * Begin/End counter snapshots with what's running across other parts of the
983 * When the command parser reaches a Begin marker it effectively needs to
984 * drain everything currently running on the GPU until the hardware is idle
985 * before capturing the first snapshot of counters - otherwise the results
986 * would also be measuring the effects of earlier commands.
988 * When the command parser reaches an End marker it needs to stall until
989 * everything currently running on the GPU has finished before capturing the
990 * end snapshot - otherwise the results won't be a complete representation
993 * Theoretically there could be opportunities to minimize how much of the
994 * GPU pipeline is drained, or that we stall for, when we know what specific
995 * units the performance counters being queried relate to but we don't
996 * currently attempt to be clever here.
998 * Note: with our current simple approach here then for back-to-back queries
999 * we will redundantly emit duplicate commands to synchronize the command
1000 * streamer with the rest of the GPU pipeline, but we assume that in HW the
1001 * second synchronization is effectively a NOOP.
1003 * N.B. The final results are based on deltas of counters between (inside)
1004 * Begin/End markers so even though the total wall clock time of the
1005 * workload is stretched by larger pipeline bubbles the bubbles themselves
1006 * are generally invisible to the query results. Whether that's a good or a
1007 * bad thing depends on the use case. For a lower real-time impact while
1008 * capturing metrics then periodic sampling may be a better choice than
1009 * INTEL_performance_query.
1012 * This is our Begin synchronization point to drain current work on the
1013 * GPU before we capture our first counter snapshot...
1015 brw_emit_mi_flush(brw
);
1017 switch (query
->kind
) {
1020 /* Opening an i915 perf stream implies exclusive access to the OA unit
1021 * which will generate counter reports for a specific counter set with a
1022 * specific layout/format so we can't begin any OA based queries that
1023 * require a different counter set or format unless we get an opportunity
1024 * to close the stream and open a new one...
1026 if (brw
->perfquery
.oa_stream_fd
!= -1 &&
1027 brw
->perfquery
.current_oa_metrics_set_id
!=
1028 query
->oa_metrics_set_id
) {
1030 if (brw
->perfquery
.n_oa_users
!= 0)
1036 /* If the OA counters aren't already on, enable them. */
1037 if (brw
->perfquery
.oa_stream_fd
== -1) {
1038 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1039 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1041 /* The period_exponent gives a sampling period as follows:
1042 * sample_period = timestamp_period * 2^(period_exponent + 1)
1044 * The timestamps increments every 80ns (HSW), ~52ns (GEN9LP) or
1047 * The counter overflow period is derived from the EuActive counter
1048 * which reads a counter that increments by the number of clock
1049 * cycles multiplied by the number of EUs. It can be calculated as:
1051 * 2^(number of bits in A counter) / (n_eus * max_gen_freq * 2)
1053 * (E.g. 40 EUs @ 1GHz = ~53ms)
1055 * We select a sampling period inferior to that overflow period to
1056 * ensure we cannot see more than 1 counter overflow, otherwise we
1057 * could loose information.
1060 int a_counter_in_bits
= 32;
1061 if (devinfo
->gen
>= 8)
1062 a_counter_in_bits
= 40;
1064 uint64_t overflow_period
= pow(2, a_counter_in_bits
) /
1065 (brw
->perfquery
.sys_vars
.n_eus
*
1066 /* drop 1GHz freq to have units in nanoseconds */
1069 DBG("A counter overflow period: %"PRIu64
"ns, %"PRIu64
"ms (n_eus=%"PRIu64
")\n",
1070 overflow_period
, overflow_period
/ 1000000ul, brw
->perfquery
.sys_vars
.n_eus
);
1072 int period_exponent
= 0;
1073 uint64_t prev_sample_period
, next_sample_period
;
1074 for (int e
= 0; e
< 30; e
++) {
1075 prev_sample_period
= 1000000000ull * pow(2, e
+ 1) / devinfo
->timestamp_frequency
;
1076 next_sample_period
= 1000000000ull * pow(2, e
+ 2) / devinfo
->timestamp_frequency
;
1078 /* Take the previous sampling period, lower than the overflow
1081 if (prev_sample_period
< overflow_period
&&
1082 next_sample_period
> overflow_period
)
1083 period_exponent
= e
+ 1;
1086 if (period_exponent
== 0) {
1087 DBG("WARNING: enable to find a sampling exponent\n");
1091 DBG("OA sampling exponent: %i ~= %"PRIu64
"ms\n", period_exponent
,
1092 prev_sample_period
/ 1000000ul);
1094 if (!open_i915_perf_oa_stream(brw
,
1095 query
->oa_metrics_set_id
,
1098 screen
->fd
, /* drm fd */
1102 assert(brw
->perfquery
.current_oa_metrics_set_id
==
1103 query
->oa_metrics_set_id
&&
1104 brw
->perfquery
.current_oa_format
==
1108 if (!inc_n_oa_users(brw
)) {
1109 DBG("WARNING: Error enabling i915 perf stream: %m\n");
1114 brw_bo_unreference(obj
->oa
.bo
);
1119 brw_bo_alloc(brw
->bufmgr
, "perf. query OA MI_RPC bo", MI_RPC_BO_SIZE
);
1121 /* Pre-filling the BO helps debug whether writes landed. */
1122 void *map
= brw_bo_map(brw
, obj
->oa
.bo
, MAP_WRITE
);
1123 memset(map
, 0x80, MI_RPC_BO_SIZE
);
1124 brw_bo_unmap(obj
->oa
.bo
);
1127 obj
->oa
.begin_report_id
= brw
->perfquery
.next_query_start_report_id
;
1128 brw
->perfquery
.next_query_start_report_id
+= 2;
1130 /* We flush the batchbuffer here to minimize the chances that MI_RPC
1131 * delimiting commands end up in different batchbuffers. If that's the
1132 * case, the measurement will include the time it takes for the kernel
1133 * scheduler to load a new request into the hardware. This is manifested in
1134 * tools like frameretrace by spikes in the "GPU Core Clocks" counter.
1136 intel_batchbuffer_flush(brw
);
1138 /* Take a starting OA counter snapshot. */
1139 brw
->vtbl
.emit_mi_report_perf_count(brw
, obj
->oa
.bo
, 0,
1140 obj
->oa
.begin_report_id
);
1141 ++brw
->perfquery
.n_active_oa_queries
;
1143 /* No already-buffered samples can possibly be associated with this query
1144 * so create a marker within the list of sample buffers enabling us to
1145 * easily ignore earlier samples when processing this query after
1148 assert(!exec_list_is_empty(&brw
->perfquery
.sample_buffers
));
1149 obj
->oa
.samples_head
= exec_list_get_tail(&brw
->perfquery
.sample_buffers
);
1151 struct brw_oa_sample_buf
*buf
=
1152 exec_node_data(struct brw_oa_sample_buf
, obj
->oa
.samples_head
, link
);
1154 /* This reference will ensure that future/following sample
1155 * buffers (that may relate to this query) can't be freed until
1156 * this drops to zero.
1160 obj
->oa
.hw_id
= 0xffffffff;
1161 memset(obj
->oa
.accumulator
, 0, sizeof(obj
->oa
.accumulator
));
1162 obj
->oa
.results_accumulated
= false;
1164 add_to_unaccumulated_query_list(brw
, obj
);
1167 case PIPELINE_STATS
:
1168 if (obj
->pipeline_stats
.bo
) {
1169 brw_bo_unreference(obj
->pipeline_stats
.bo
);
1170 obj
->pipeline_stats
.bo
= NULL
;
1173 obj
->pipeline_stats
.bo
=
1174 brw_bo_alloc(brw
->bufmgr
, "perf. query pipeline stats bo",
1177 /* Take starting snapshots. */
1178 snapshot_statistics_registers(brw
, obj
, 0);
1180 ++brw
->perfquery
.n_active_pipeline_stats_queries
;
1184 unreachable("Unknown query type");
1188 if (INTEL_DEBUG
& DEBUG_PERFMON
)
1189 dump_perf_queries(brw
);
1195 * Driver hook for glEndPerfQueryINTEL().
1198 brw_end_perf_query(struct gl_context
*ctx
,
1199 struct gl_perf_query_object
*o
)
1201 struct brw_context
*brw
= brw_context(ctx
);
1202 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1204 DBG("End(%d)\n", o
->Id
);
1206 /* Ensure that the work associated with the queried commands will have
1207 * finished before taking our query end counter readings.
1209 * For more details see comment in brw_begin_perf_query for
1210 * corresponding flush.
1212 brw_emit_mi_flush(brw
);
1214 switch (obj
->query
->kind
) {
1217 /* NB: It's possible that the query will have already been marked
1218 * as 'accumulated' if an error was seen while reading samples
1219 * from perf. In this case we mustn't try and emit a closing
1220 * MI_RPC command in case the OA unit has already been disabled
1222 if (!obj
->oa
.results_accumulated
) {
1223 /* Take an ending OA counter snapshot. */
1224 brw
->vtbl
.emit_mi_report_perf_count(brw
, obj
->oa
.bo
,
1225 MI_RPC_BO_END_OFFSET_BYTES
,
1226 obj
->oa
.begin_report_id
+ 1);
1229 --brw
->perfquery
.n_active_oa_queries
;
1231 /* NB: even though the query has now ended, it can't be accumulated
1232 * until the end MI_REPORT_PERF_COUNT snapshot has been written
1237 case PIPELINE_STATS
:
1238 snapshot_statistics_registers(brw
, obj
,
1239 STATS_BO_END_OFFSET_BYTES
);
1240 --brw
->perfquery
.n_active_pipeline_stats_queries
;
1244 unreachable("Unknown query type");
1250 brw_wait_perf_query(struct gl_context
*ctx
, struct gl_perf_query_object
*o
)
1252 struct brw_context
*brw
= brw_context(ctx
);
1253 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1254 struct brw_bo
*bo
= NULL
;
1258 switch (obj
->query
->kind
) {
1263 case PIPELINE_STATS
:
1264 bo
= obj
->pipeline_stats
.bo
;
1268 unreachable("Unknown query type");
1275 /* If the current batch references our results bo then we need to
1278 if (brw_batch_references(&brw
->batch
, bo
))
1279 intel_batchbuffer_flush(brw
);
1281 brw_bo_wait_rendering(bo
);
1283 /* Due to a race condition between the OA unit signaling report
1284 * availability and the report actually being written into memory,
1285 * we need to wait for all the reports to come in before we can
1288 if (obj
->query
->kind
== OA_COUNTERS
) {
1289 while (!read_oa_samples_for_query(brw
, obj
))
1295 brw_is_perf_query_ready(struct gl_context
*ctx
,
1296 struct gl_perf_query_object
*o
)
1298 struct brw_context
*brw
= brw_context(ctx
);
1299 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1304 switch (obj
->query
->kind
) {
1306 return (obj
->oa
.results_accumulated
||
1308 !brw_batch_references(&brw
->batch
, obj
->oa
.bo
) &&
1309 !brw_bo_busy(obj
->oa
.bo
) &&
1310 read_oa_samples_for_query(brw
, obj
)));
1311 case PIPELINE_STATS
:
1312 return (obj
->pipeline_stats
.bo
&&
1313 !brw_batch_references(&brw
->batch
, obj
->pipeline_stats
.bo
) &&
1314 !brw_bo_busy(obj
->pipeline_stats
.bo
));
1317 unreachable("Unknown query type");
1325 get_oa_counter_data(struct brw_context
*brw
,
1326 struct brw_perf_query_object
*obj
,
1330 const struct brw_perf_query_info
*query
= obj
->query
;
1331 int n_counters
= query
->n_counters
;
1334 if (!obj
->oa
.results_accumulated
) {
1335 accumulate_oa_reports(brw
, obj
);
1336 assert(obj
->oa
.results_accumulated
);
1338 brw_bo_unmap(obj
->oa
.bo
);
1342 for (int i
= 0; i
< n_counters
; i
++) {
1343 const struct brw_perf_query_counter
*counter
= &query
->counters
[i
];
1344 uint64_t *out_uint64
;
1347 if (counter
->size
) {
1348 switch (counter
->data_type
) {
1349 case GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL
:
1350 out_uint64
= (uint64_t *)(data
+ counter
->offset
);
1351 *out_uint64
= counter
->oa_counter_read_uint64(brw
, query
,
1352 obj
->oa
.accumulator
);
1354 case GL_PERFQUERY_COUNTER_DATA_FLOAT_INTEL
:
1355 out_float
= (float *)(data
+ counter
->offset
);
1356 *out_float
= counter
->oa_counter_read_float(brw
, query
,
1357 obj
->oa
.accumulator
);
1360 /* So far we aren't using uint32, double or bool32... */
1361 unreachable("unexpected counter data type");
1363 written
= counter
->offset
+ counter
->size
;
1371 get_pipeline_stats_data(struct brw_context
*brw
,
1372 struct brw_perf_query_object
*obj
,
1377 const struct brw_perf_query_info
*query
= obj
->query
;
1378 int n_counters
= obj
->query
->n_counters
;
1381 uint64_t *start
= brw_bo_map(brw
, obj
->pipeline_stats
.bo
, MAP_READ
);
1382 uint64_t *end
= start
+ (STATS_BO_END_OFFSET_BYTES
/ sizeof(uint64_t));
1384 for (int i
= 0; i
< n_counters
; i
++) {
1385 const struct brw_perf_query_counter
*counter
= &query
->counters
[i
];
1386 uint64_t value
= end
[i
] - start
[i
];
1388 if (counter
->pipeline_stat
.numerator
!=
1389 counter
->pipeline_stat
.denominator
) {
1390 value
*= counter
->pipeline_stat
.numerator
;
1391 value
/= counter
->pipeline_stat
.denominator
;
1394 *((uint64_t *)p
) = value
;
1398 brw_bo_unmap(obj
->pipeline_stats
.bo
);
1404 * Driver hook for glGetPerfQueryDataINTEL().
1407 brw_get_perf_query_data(struct gl_context
*ctx
,
1408 struct gl_perf_query_object
*o
,
1411 GLuint
*bytes_written
)
1413 struct brw_context
*brw
= brw_context(ctx
);
1414 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1417 assert(brw_is_perf_query_ready(ctx
, o
));
1419 DBG("GetData(%d)\n", o
->Id
);
1421 if (INTEL_DEBUG
& DEBUG_PERFMON
)
1422 dump_perf_queries(brw
);
1424 /* We expect that the frontend only calls this hook when it knows
1425 * that results are available.
1429 switch (obj
->query
->kind
) {
1431 written
= get_oa_counter_data(brw
, obj
, data_size
, (uint8_t *)data
);
1434 case PIPELINE_STATS
:
1435 written
= get_pipeline_stats_data(brw
, obj
, data_size
, (uint8_t *)data
);
1439 unreachable("Unknown query type");
1444 *bytes_written
= written
;
1447 static struct gl_perf_query_object
*
1448 brw_new_perf_query_object(struct gl_context
*ctx
, unsigned query_index
)
1450 struct brw_context
*brw
= brw_context(ctx
);
1451 const struct brw_perf_query_info
*query
=
1452 &brw
->perfquery
.queries
[query_index
];
1453 struct brw_perf_query_object
*obj
=
1454 calloc(1, sizeof(struct brw_perf_query_object
));
1461 brw
->perfquery
.n_query_instances
++;
1467 * Driver hook for glDeletePerfQueryINTEL().
1470 brw_delete_perf_query(struct gl_context
*ctx
,
1471 struct gl_perf_query_object
*o
)
1473 struct brw_context
*brw
= brw_context(ctx
);
1474 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1476 /* We can assume that the frontend waits for a query to complete
1477 * before ever calling into here, so we don't have to worry about
1478 * deleting an in-flight query object.
1481 assert(!o
->Used
|| o
->Ready
);
1483 DBG("Delete(%d)\n", o
->Id
);
1485 switch (obj
->query
->kind
) {
1488 if (!obj
->oa
.results_accumulated
) {
1489 drop_from_unaccumulated_query_list(brw
, obj
);
1490 dec_n_oa_users(brw
);
1493 brw_bo_unreference(obj
->oa
.bo
);
1497 obj
->oa
.results_accumulated
= false;
1500 case PIPELINE_STATS
:
1501 if (obj
->pipeline_stats
.bo
) {
1502 brw_bo_unreference(obj
->pipeline_stats
.bo
);
1503 obj
->pipeline_stats
.bo
= NULL
;
1508 unreachable("Unknown query type");
1514 /* As an indication that the INTEL_performance_query extension is no
1515 * longer in use, it's a good time to free our cache of sample
1516 * buffers and close any current i915-perf stream.
1518 if (--brw
->perfquery
.n_query_instances
== 0) {
1519 free_sample_bufs(brw
);
1524 /******************************************************************************/
1527 init_pipeline_statistic_query_registers(struct brw_context
*brw
)
1529 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1530 struct brw_perf_query_info
*query
= brw_perf_query_append_query_info(brw
);
1532 query
->kind
= PIPELINE_STATS
;
1533 query
->name
= "Pipeline Statistics Registers";
1534 query
->n_counters
= 0;
1536 rzalloc_array(brw
, struct brw_perf_query_counter
, MAX_STAT_COUNTERS
);
1538 brw_perf_query_info_add_basic_stat_reg(query
, IA_VERTICES_COUNT
,
1539 "N vertices submitted");
1540 brw_perf_query_info_add_basic_stat_reg(query
, IA_PRIMITIVES_COUNT
,
1541 "N primitives submitted");
1542 brw_perf_query_info_add_basic_stat_reg(query
, VS_INVOCATION_COUNT
,
1543 "N vertex shader invocations");
1545 if (devinfo
->gen
== 6) {
1546 brw_perf_query_info_add_stat_reg(query
, GEN6_SO_PRIM_STORAGE_NEEDED
, 1, 1,
1547 "SO_PRIM_STORAGE_NEEDED",
1548 "N geometry shader stream-out primitives (total)");
1549 brw_perf_query_info_add_stat_reg(query
, GEN6_SO_NUM_PRIMS_WRITTEN
, 1, 1,
1550 "SO_NUM_PRIMS_WRITTEN",
1551 "N geometry shader stream-out primitives (written)");
1553 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(0), 1, 1,
1554 "SO_PRIM_STORAGE_NEEDED (Stream 0)",
1555 "N stream-out (stream 0) primitives (total)");
1556 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(1), 1, 1,
1557 "SO_PRIM_STORAGE_NEEDED (Stream 1)",
1558 "N stream-out (stream 1) primitives (total)");
1559 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(2), 1, 1,
1560 "SO_PRIM_STORAGE_NEEDED (Stream 2)",
1561 "N stream-out (stream 2) primitives (total)");
1562 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(3), 1, 1,
1563 "SO_PRIM_STORAGE_NEEDED (Stream 3)",
1564 "N stream-out (stream 3) primitives (total)");
1565 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(0), 1, 1,
1566 "SO_NUM_PRIMS_WRITTEN (Stream 0)",
1567 "N stream-out (stream 0) primitives (written)");
1568 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(1), 1, 1,
1569 "SO_NUM_PRIMS_WRITTEN (Stream 1)",
1570 "N stream-out (stream 1) primitives (written)");
1571 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(2), 1, 1,
1572 "SO_NUM_PRIMS_WRITTEN (Stream 2)",
1573 "N stream-out (stream 2) primitives (written)");
1574 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(3), 1, 1,
1575 "SO_NUM_PRIMS_WRITTEN (Stream 3)",
1576 "N stream-out (stream 3) primitives (written)");
1579 brw_perf_query_info_add_basic_stat_reg(query
, HS_INVOCATION_COUNT
,
1580 "N TCS shader invocations");
1581 brw_perf_query_info_add_basic_stat_reg(query
, DS_INVOCATION_COUNT
,
1582 "N TES shader invocations");
1584 brw_perf_query_info_add_basic_stat_reg(query
, GS_INVOCATION_COUNT
,
1585 "N geometry shader invocations");
1586 brw_perf_query_info_add_basic_stat_reg(query
, GS_PRIMITIVES_COUNT
,
1587 "N geometry shader primitives emitted");
1589 brw_perf_query_info_add_basic_stat_reg(query
, CL_INVOCATION_COUNT
,
1590 "N primitives entering clipping");
1591 brw_perf_query_info_add_basic_stat_reg(query
, CL_PRIMITIVES_COUNT
,
1592 "N primitives leaving clipping");
1594 if (devinfo
->is_haswell
|| devinfo
->gen
== 8)
1595 brw_perf_query_info_add_stat_reg(query
, PS_INVOCATION_COUNT
, 1, 4,
1596 "N fragment shader invocations",
1597 "N fragment shader invocations");
1599 brw_perf_query_info_add_basic_stat_reg(query
, PS_INVOCATION_COUNT
,
1600 "N fragment shader invocations");
1602 brw_perf_query_info_add_basic_stat_reg(query
, PS_DEPTH_COUNT
, "N z-pass fragments");
1604 if (devinfo
->gen
>= 7)
1605 brw_perf_query_info_add_basic_stat_reg(query
, CS_INVOCATION_COUNT
,
1606 "N compute shader invocations");
1608 query
->data_size
= sizeof(uint64_t) * query
->n_counters
;
1612 register_oa_config(struct brw_context
*brw
,
1613 const struct brw_perf_query_info
*query
,
1616 struct brw_perf_query_info
*registred_query
=
1617 brw_perf_query_append_query_info(brw
);
1619 *registred_query
= *query
;
1620 registred_query
->oa_metrics_set_id
= config_id
;
1621 DBG("metric set registred: id = %" PRIu64
", guid = %s\n",
1622 registred_query
->oa_metrics_set_id
, query
->guid
);
1626 enumerate_sysfs_metrics(struct brw_context
*brw
)
1629 DIR *metricsdir
= NULL
;
1630 struct dirent
*metric_entry
;
1633 len
= snprintf(buf
, sizeof(buf
), "%s/metrics", brw
->perfquery
.sysfs_dev_dir
);
1634 if (len
< 0 || len
>= sizeof(buf
)) {
1635 DBG("Failed to concatenate path to sysfs metrics/ directory\n");
1639 metricsdir
= opendir(buf
);
1641 DBG("Failed to open %s: %m\n", buf
);
1645 while ((metric_entry
= readdir(metricsdir
))) {
1646 struct hash_entry
*entry
;
1648 if ((metric_entry
->d_type
!= DT_DIR
&&
1649 metric_entry
->d_type
!= DT_LNK
) ||
1650 metric_entry
->d_name
[0] == '.')
1653 DBG("metric set: %s\n", metric_entry
->d_name
);
1654 entry
= _mesa_hash_table_search(brw
->perfquery
.oa_metrics_table
,
1655 metric_entry
->d_name
);
1659 len
= snprintf(buf
, sizeof(buf
), "%s/metrics/%s/id",
1660 brw
->perfquery
.sysfs_dev_dir
, metric_entry
->d_name
);
1661 if (len
< 0 || len
>= sizeof(buf
)) {
1662 DBG("Failed to concatenate path to sysfs metric id file\n");
1666 if (!read_file_uint64(buf
, &id
)) {
1667 DBG("Failed to read metric set id from %s: %m", buf
);
1671 register_oa_config(brw
, (const struct brw_perf_query_info
*)entry
->data
, id
);
1673 DBG("metric set not known by mesa (skipping)\n");
1676 closedir(metricsdir
);
1680 kernel_has_dynamic_config_support(struct brw_context
*brw
)
1682 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1683 struct hash_entry
*entry
;
1685 hash_table_foreach(brw
->perfquery
.oa_metrics_table
, entry
) {
1686 struct brw_perf_query_info
*query
= entry
->data
;
1687 char config_path
[280];
1690 snprintf(config_path
, sizeof(config_path
), "%s/metrics/%s/id",
1691 brw
->perfquery
.sysfs_dev_dir
, query
->guid
);
1693 /* Look for the test config, which we know we can't replace. */
1694 if (read_file_uint64(config_path
, &config_id
) && config_id
== 1) {
1695 return drmIoctl(screen
->fd
, DRM_IOCTL_I915_PERF_REMOVE_CONFIG
,
1696 &config_id
) < 0 && errno
== ENOENT
;
1704 init_oa_configs(struct brw_context
*brw
)
1706 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1707 struct hash_entry
*entry
;
1709 hash_table_foreach(brw
->perfquery
.oa_metrics_table
, entry
) {
1710 const struct brw_perf_query_info
*query
= entry
->data
;
1711 struct drm_i915_perf_oa_config config
;
1712 char config_path
[280];
1716 snprintf(config_path
, sizeof(config_path
), "%s/metrics/%s/id",
1717 brw
->perfquery
.sysfs_dev_dir
, query
->guid
);
1719 /* Don't recreate already loaded configs. */
1720 if (read_file_uint64(config_path
, &config_id
)) {
1721 DBG("metric set: %s (already loaded)\n", query
->guid
);
1722 register_oa_config(brw
, query
, config_id
);
1726 memset(&config
, 0, sizeof(config
));
1728 memcpy(config
.uuid
, query
->guid
, sizeof(config
.uuid
));
1730 config
.n_mux_regs
= query
->n_mux_regs
;
1731 config
.mux_regs_ptr
= (uintptr_t) query
->mux_regs
;
1733 config
.n_boolean_regs
= query
->n_b_counter_regs
;
1734 config
.boolean_regs_ptr
= (uintptr_t) query
->b_counter_regs
;
1736 config
.n_flex_regs
= query
->n_flex_regs
;
1737 config
.flex_regs_ptr
= (uintptr_t) query
->flex_regs
;
1739 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_I915_PERF_ADD_CONFIG
, &config
);
1741 DBG("Failed to load \"%s\" (%s) metrics set in kernel: %s\n",
1742 query
->name
, query
->guid
, strerror(errno
));
1746 register_oa_config(brw
, query
, ret
);
1747 DBG("metric set: %s (added)\n", query
->guid
);
1752 query_topology(struct brw_context
*brw
)
1754 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1755 struct drm_i915_query_item item
= {
1756 .query_id
= DRM_I915_QUERY_TOPOLOGY_INFO
,
1758 struct drm_i915_query query
= {
1760 .items_ptr
= (uintptr_t) &item
,
1763 if (drmIoctl(screen
->fd
, DRM_IOCTL_I915_QUERY
, &query
))
1766 struct drm_i915_query_topology_info
*topo_info
=
1767 (struct drm_i915_query_topology_info
*) calloc(1, item
.length
);
1768 item
.data_ptr
= (uintptr_t) topo_info
;
1770 if (drmIoctl(screen
->fd
, DRM_IOCTL_I915_QUERY
, &query
) ||
1774 gen_device_info_update_from_topology(&brw
->screen
->devinfo
,
1783 getparam_topology(struct brw_context
*brw
)
1785 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1786 drm_i915_getparam_t gp
;
1790 gp
.param
= I915_PARAM_SLICE_MASK
;
1791 gp
.value
= &slice_mask
;
1792 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
1796 int subslice_mask
= 0;
1797 gp
.param
= I915_PARAM_SUBSLICE_MASK
;
1798 gp
.value
= &subslice_mask
;
1799 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
1803 gen_device_info_update_from_masks(&brw
->screen
->devinfo
,
1806 brw
->screen
->eu_total
);
1812 compute_topology_builtins(struct brw_context
*brw
)
1814 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1816 brw
->perfquery
.sys_vars
.slice_mask
= devinfo
->slice_masks
;
1817 brw
->perfquery
.sys_vars
.n_eu_slices
= devinfo
->num_slices
;
1819 for (int i
= 0; i
< sizeof(devinfo
->subslice_masks
[i
]); i
++) {
1820 brw
->perfquery
.sys_vars
.n_eu_sub_slices
+=
1821 _mesa_bitcount(devinfo
->subslice_masks
[i
]);
1824 for (int i
= 0; i
< sizeof(devinfo
->eu_masks
); i
++)
1825 brw
->perfquery
.sys_vars
.n_eus
+= _mesa_bitcount(devinfo
->eu_masks
[i
]);
1827 brw
->perfquery
.sys_vars
.eu_threads_count
=
1828 brw
->perfquery
.sys_vars
.n_eus
* devinfo
->num_thread_per_eu
;
1830 /* At the moment the subslice mask builtin has groups of 3bits for each
1833 * Ideally equations would be updated to have a slice/subslice query
1834 * function/operator.
1836 brw
->perfquery
.sys_vars
.subslice_mask
= 0;
1837 for (int s
= 0; s
< util_last_bit(devinfo
->slice_masks
); s
++) {
1838 for (int ss
= 0; ss
< (devinfo
->subslice_slice_stride
* 8); ss
++) {
1839 if (gen_device_info_subslice_available(devinfo
, s
, ss
))
1840 brw
->perfquery
.sys_vars
.subslice_mask
|= 1UL << (s
* 3 + ss
);
1846 init_oa_sys_vars(struct brw_context
*brw
)
1848 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1849 uint64_t min_freq_mhz
= 0, max_freq_mhz
= 0;
1850 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1852 if (!read_sysfs_drm_device_file_uint64(brw
, "gt_min_freq_mhz", &min_freq_mhz
))
1855 if (!read_sysfs_drm_device_file_uint64(brw
, "gt_max_freq_mhz", &max_freq_mhz
))
1858 if (!query_topology(brw
)) {
1859 /* We need the i915 query uAPI on CNL+ (kernel 4.17+). */
1860 if (devinfo
->gen
>= 10)
1863 if (!getparam_topology(brw
)) {
1864 /* We need the SLICE_MASK/SUBSLICE_MASK on gen8+ (kernel 4.13+). */
1865 if (devinfo
->gen
>= 8)
1868 /* On Haswell, the values are already computed for us in
1874 memset(&brw
->perfquery
.sys_vars
, 0, sizeof(brw
->perfquery
.sys_vars
));
1875 brw
->perfquery
.sys_vars
.gt_min_freq
= min_freq_mhz
* 1000000;
1876 brw
->perfquery
.sys_vars
.gt_max_freq
= max_freq_mhz
* 1000000;
1877 brw
->perfquery
.sys_vars
.timestamp_frequency
= devinfo
->timestamp_frequency
;
1878 brw
->perfquery
.sys_vars
.revision
= intel_device_get_revision(screen
->fd
);
1879 compute_topology_builtins(brw
);
1885 get_sysfs_dev_dir(struct brw_context
*brw
)
1887 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1891 struct dirent
*drm_entry
;
1894 brw
->perfquery
.sysfs_dev_dir
[0] = '\0';
1896 if (fstat(screen
->fd
, &sb
)) {
1897 DBG("Failed to stat DRM fd\n");
1901 maj
= major(sb
.st_rdev
);
1902 min
= minor(sb
.st_rdev
);
1904 if (!S_ISCHR(sb
.st_mode
)) {
1905 DBG("DRM fd is not a character device as expected\n");
1909 len
= snprintf(brw
->perfquery
.sysfs_dev_dir
,
1910 sizeof(brw
->perfquery
.sysfs_dev_dir
),
1911 "/sys/dev/char/%d:%d/device/drm", maj
, min
);
1912 if (len
< 0 || len
>= sizeof(brw
->perfquery
.sysfs_dev_dir
)) {
1913 DBG("Failed to concatenate sysfs path to drm device\n");
1917 drmdir
= opendir(brw
->perfquery
.sysfs_dev_dir
);
1919 DBG("Failed to open %s: %m\n", brw
->perfquery
.sysfs_dev_dir
);
1923 while ((drm_entry
= readdir(drmdir
))) {
1924 if ((drm_entry
->d_type
== DT_DIR
||
1925 drm_entry
->d_type
== DT_LNK
) &&
1926 strncmp(drm_entry
->d_name
, "card", 4) == 0)
1928 len
= snprintf(brw
->perfquery
.sysfs_dev_dir
,
1929 sizeof(brw
->perfquery
.sysfs_dev_dir
),
1930 "/sys/dev/char/%d:%d/device/drm/%s",
1931 maj
, min
, drm_entry
->d_name
);
1933 if (len
< 0 || len
>= sizeof(brw
->perfquery
.sysfs_dev_dir
))
1942 DBG("Failed to find cardX directory under /sys/dev/char/%d:%d/device/drm\n",
1948 typedef void (*perf_register_oa_queries_t
)(struct brw_context
*);
1950 static perf_register_oa_queries_t
1951 get_register_queries_function(const struct gen_device_info
*devinfo
)
1953 if (devinfo
->is_haswell
)
1954 return brw_oa_register_queries_hsw
;
1955 if (devinfo
->is_cherryview
)
1956 return brw_oa_register_queries_chv
;
1957 if (devinfo
->is_broadwell
)
1958 return brw_oa_register_queries_bdw
;
1959 if (devinfo
->is_broxton
)
1960 return brw_oa_register_queries_bxt
;
1961 if (devinfo
->is_skylake
) {
1962 if (devinfo
->gt
== 2)
1963 return brw_oa_register_queries_sklgt2
;
1964 if (devinfo
->gt
== 3)
1965 return brw_oa_register_queries_sklgt3
;
1966 if (devinfo
->gt
== 4)
1967 return brw_oa_register_queries_sklgt4
;
1969 if (devinfo
->is_kabylake
) {
1970 if (devinfo
->gt
== 2)
1971 return brw_oa_register_queries_kblgt2
;
1972 if (devinfo
->gt
== 3)
1973 return brw_oa_register_queries_kblgt3
;
1975 if (devinfo
->is_geminilake
)
1976 return brw_oa_register_queries_glk
;
1977 if (devinfo
->is_coffeelake
) {
1978 if (devinfo
->gt
== 2)
1979 return brw_oa_register_queries_cflgt2
;
1980 if (devinfo
->gt
== 3)
1981 return brw_oa_register_queries_cflgt3
;
1983 if (devinfo
->is_cannonlake
)
1984 return brw_oa_register_queries_cnl
;
1990 brw_init_perf_query_info(struct gl_context
*ctx
)
1992 struct brw_context
*brw
= brw_context(ctx
);
1993 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1994 bool i915_perf_oa_available
= false;
1996 perf_register_oa_queries_t oa_register
;
1998 if (brw
->perfquery
.n_queries
)
1999 return brw
->perfquery
.n_queries
;
2001 init_pipeline_statistic_query_registers(brw
);
2003 oa_register
= get_register_queries_function(devinfo
);
2005 /* The existence of this sysctl parameter implies the kernel supports
2006 * the i915 perf interface.
2008 if (stat("/proc/sys/dev/i915/perf_stream_paranoid", &sb
) == 0) {
2010 /* If _paranoid == 1 then on Gen8+ we won't be able to access OA
2011 * metrics unless running as root.
2013 if (devinfo
->is_haswell
)
2014 i915_perf_oa_available
= true;
2016 uint64_t paranoid
= 1;
2018 read_file_uint64("/proc/sys/dev/i915/perf_stream_paranoid", ¶noid
);
2020 if (paranoid
== 0 || geteuid() == 0)
2021 i915_perf_oa_available
= true;
2025 if (i915_perf_oa_available
&&
2027 get_sysfs_dev_dir(brw
) &&
2028 init_oa_sys_vars(brw
))
2030 brw
->perfquery
.oa_metrics_table
=
2031 _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
2032 _mesa_key_string_equal
);
2034 /* Index all the metric sets mesa knows about before looking to see what
2035 * the kernel is advertising.
2039 if (likely((INTEL_DEBUG
& DEBUG_NO_OACONFIG
) == 0) &&
2040 kernel_has_dynamic_config_support(brw
))
2041 init_oa_configs(brw
);
2043 enumerate_sysfs_metrics(brw
);
2046 brw
->perfquery
.unaccumulated
=
2047 ralloc_array(brw
, struct brw_perf_query_object
*, 2);
2048 brw
->perfquery
.unaccumulated_elements
= 0;
2049 brw
->perfquery
.unaccumulated_array_size
= 2;
2051 exec_list_make_empty(&brw
->perfquery
.sample_buffers
);
2052 exec_list_make_empty(&brw
->perfquery
.free_sample_buffers
);
2054 /* It's convenient to guarantee that this linked list of sample
2055 * buffers is never empty so we add an empty head so when we
2056 * Begin an OA query we can always take a reference on a buffer
2059 struct brw_oa_sample_buf
*buf
= get_free_sample_buf(brw
);
2060 exec_list_push_head(&brw
->perfquery
.sample_buffers
, &buf
->link
);
2062 brw
->perfquery
.oa_stream_fd
= -1;
2064 brw
->perfquery
.next_query_start_report_id
= 1000;
2066 return brw
->perfquery
.n_queries
;
2070 brw_init_performance_queries(struct brw_context
*brw
)
2072 struct gl_context
*ctx
= &brw
->ctx
;
2074 ctx
->Driver
.InitPerfQueryInfo
= brw_init_perf_query_info
;
2075 ctx
->Driver
.GetPerfQueryInfo
= brw_get_perf_query_info
;
2076 ctx
->Driver
.GetPerfCounterInfo
= brw_get_perf_counter_info
;
2077 ctx
->Driver
.NewPerfQueryObject
= brw_new_perf_query_object
;
2078 ctx
->Driver
.DeletePerfQuery
= brw_delete_perf_query
;
2079 ctx
->Driver
.BeginPerfQuery
= brw_begin_perf_query
;
2080 ctx
->Driver
.EndPerfQuery
= brw_end_perf_query
;
2081 ctx
->Driver
.WaitPerfQuery
= brw_wait_perf_query
;
2082 ctx
->Driver
.IsPerfQueryReady
= brw_is_perf_query_ready
;
2083 ctx
->Driver
.GetPerfQueryData
= brw_get_perf_query_data
;