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)
219 #define MI_FREQ_START_OFFSET_BYTES (3072)
220 #define MI_FREQ_END_OFFSET_BYTES (3076)
222 /******************************************************************************/
225 read_file_uint64(const char *file
, uint64_t *val
)
233 while ((n
= read(fd
, buf
, sizeof (buf
) - 1)) < 0 &&
240 *val
= strtoull(buf
, NULL
, 0);
246 read_sysfs_drm_device_file_uint64(struct brw_context
*brw
,
253 len
= snprintf(buf
, sizeof(buf
), "%s/%s",
254 brw
->perfquery
.sysfs_dev_dir
, file
);
255 if (len
< 0 || len
>= sizeof(buf
)) {
256 DBG("Failed to concatenate sys filename to read u64 from\n");
260 return read_file_uint64(buf
, value
);
263 /******************************************************************************/
266 brw_is_perf_query_ready(struct gl_context
*ctx
,
267 struct gl_perf_query_object
*o
);
270 dump_perf_query_callback(GLuint id
, void *query_void
, void *brw_void
)
272 struct gl_context
*ctx
= brw_void
;
273 struct gl_perf_query_object
*o
= query_void
;
274 struct brw_perf_query_object
*obj
= query_void
;
276 switch (obj
->query
->kind
) {
278 DBG("%4d: %-6s %-8s BO: %-4s OA data: %-10s %-15s\n",
280 o
->Used
? "Dirty," : "New,",
281 o
->Active
? "Active," : (o
->Ready
? "Ready," : "Pending,"),
282 obj
->oa
.bo
? "yes," : "no,",
283 brw_is_perf_query_ready(ctx
, o
) ? "ready," : "not ready,",
284 obj
->oa
.results_accumulated
? "accumulated" : "not accumulated");
287 DBG("%4d: %-6s %-8s BO: %-4s\n",
289 o
->Used
? "Dirty," : "New,",
290 o
->Active
? "Active," : (o
->Ready
? "Ready," : "Pending,"),
291 obj
->pipeline_stats
.bo
? "yes" : "no");
294 unreachable("Unknown query type");
300 dump_perf_queries(struct brw_context
*brw
)
302 struct gl_context
*ctx
= &brw
->ctx
;
303 DBG("Queries: (Open queries = %d, OA users = %d)\n",
304 brw
->perfquery
.n_active_oa_queries
, brw
->perfquery
.n_oa_users
);
305 _mesa_HashWalk(ctx
->PerfQuery
.Objects
, dump_perf_query_callback
, brw
);
308 /******************************************************************************/
310 static struct brw_oa_sample_buf
*
311 get_free_sample_buf(struct brw_context
*brw
)
313 struct exec_node
*node
= exec_list_pop_head(&brw
->perfquery
.free_sample_buffers
);
314 struct brw_oa_sample_buf
*buf
;
317 buf
= exec_node_data(struct brw_oa_sample_buf
, node
, link
);
319 buf
= ralloc_size(brw
, sizeof(*buf
));
321 exec_node_init(&buf
->link
);
330 reap_old_sample_buffers(struct brw_context
*brw
)
332 struct exec_node
*tail_node
=
333 exec_list_get_tail(&brw
->perfquery
.sample_buffers
);
334 struct brw_oa_sample_buf
*tail_buf
=
335 exec_node_data(struct brw_oa_sample_buf
, tail_node
, link
);
337 /* Remove all old, unreferenced sample buffers walking forward from
338 * the head of the list, except always leave at least one node in
339 * the list so we always have a node to reference when we Begin
342 foreach_list_typed_safe(struct brw_oa_sample_buf
, buf
, link
,
343 &brw
->perfquery
.sample_buffers
)
345 if (buf
->refcount
== 0 && buf
!= tail_buf
) {
346 exec_node_remove(&buf
->link
);
347 exec_list_push_head(&brw
->perfquery
.free_sample_buffers
, &buf
->link
);
354 free_sample_bufs(struct brw_context
*brw
)
356 foreach_list_typed_safe(struct brw_oa_sample_buf
, buf
, link
,
357 &brw
->perfquery
.free_sample_buffers
)
360 exec_list_make_empty(&brw
->perfquery
.free_sample_buffers
);
363 /******************************************************************************/
366 * Driver hook for glGetPerfQueryInfoINTEL().
369 brw_get_perf_query_info(struct gl_context
*ctx
,
370 unsigned query_index
,
376 struct brw_context
*brw
= brw_context(ctx
);
377 const struct brw_perf_query_info
*query
=
378 &brw
->perfquery
.queries
[query_index
];
381 *data_size
= query
->data_size
;
382 *n_counters
= query
->n_counters
;
384 switch (query
->kind
) {
386 *n_active
= brw
->perfquery
.n_active_oa_queries
;
390 *n_active
= brw
->perfquery
.n_active_pipeline_stats_queries
;
394 unreachable("Unknown query type");
400 * Driver hook for glGetPerfCounterInfoINTEL().
403 brw_get_perf_counter_info(struct gl_context
*ctx
,
404 unsigned query_index
,
405 unsigned counter_index
,
411 GLuint
*data_type_enum
,
414 struct brw_context
*brw
= brw_context(ctx
);
415 const struct brw_perf_query_info
*query
=
416 &brw
->perfquery
.queries
[query_index
];
417 const struct brw_perf_query_counter
*counter
=
418 &query
->counters
[counter_index
];
420 *name
= counter
->name
;
421 *desc
= counter
->desc
;
422 *offset
= counter
->offset
;
423 *data_size
= counter
->size
;
424 *type_enum
= counter
->type
;
425 *data_type_enum
= counter
->data_type
;
426 *raw_max
= counter
->raw_max
;
429 /******************************************************************************/
432 * Emit MI_STORE_REGISTER_MEM commands to capture all of the
433 * pipeline statistics for the performance query object.
436 snapshot_statistics_registers(struct brw_context
*brw
,
437 struct brw_perf_query_object
*obj
,
438 uint32_t offset_in_bytes
)
440 const struct brw_perf_query_info
*query
= obj
->query
;
441 const int n_counters
= query
->n_counters
;
443 for (int i
= 0; i
< n_counters
; i
++) {
444 const struct brw_perf_query_counter
*counter
= &query
->counters
[i
];
446 assert(counter
->data_type
== GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL
);
448 brw_store_register_mem64(brw
, obj
->pipeline_stats
.bo
,
449 counter
->pipeline_stat
.reg
,
450 offset_in_bytes
+ i
* sizeof(uint64_t));
455 * Add a query to the global list of "unaccumulated queries."
457 * Queries are tracked here until all the associated OA reports have
458 * been accumulated via accumulate_oa_reports() after the end
459 * MI_REPORT_PERF_COUNT has landed in query->oa.bo.
462 add_to_unaccumulated_query_list(struct brw_context
*brw
,
463 struct brw_perf_query_object
*obj
)
465 if (brw
->perfquery
.unaccumulated_elements
>=
466 brw
->perfquery
.unaccumulated_array_size
)
468 brw
->perfquery
.unaccumulated_array_size
*= 1.5;
469 brw
->perfquery
.unaccumulated
=
470 reralloc(brw
, brw
->perfquery
.unaccumulated
,
471 struct brw_perf_query_object
*,
472 brw
->perfquery
.unaccumulated_array_size
);
475 brw
->perfquery
.unaccumulated
[brw
->perfquery
.unaccumulated_elements
++] = obj
;
479 * Remove a query from the global list of unaccumulated queries once
480 * after successfully accumulating the OA reports associated with the
481 * query in accumulate_oa_reports() or when discarding unwanted query
485 drop_from_unaccumulated_query_list(struct brw_context
*brw
,
486 struct brw_perf_query_object
*obj
)
488 for (int i
= 0; i
< brw
->perfquery
.unaccumulated_elements
; i
++) {
489 if (brw
->perfquery
.unaccumulated
[i
] == obj
) {
490 int last_elt
= --brw
->perfquery
.unaccumulated_elements
;
493 brw
->perfquery
.unaccumulated
[i
] = NULL
;
495 brw
->perfquery
.unaccumulated
[i
] =
496 brw
->perfquery
.unaccumulated
[last_elt
];
503 /* Drop our samples_head reference so that associated periodic
504 * sample data buffers can potentially be reaped if they aren't
505 * referenced by any other queries...
508 struct brw_oa_sample_buf
*buf
=
509 exec_node_data(struct brw_oa_sample_buf
, obj
->oa
.samples_head
, link
);
511 assert(buf
->refcount
> 0);
514 obj
->oa
.samples_head
= NULL
;
516 reap_old_sample_buffers(brw
);
520 * Given pointers to starting and ending OA snapshots, add the deltas for each
521 * counter to the results.
524 add_deltas(struct brw_context
*brw
,
525 struct brw_perf_query_object
*obj
,
526 const uint32_t *start
,
529 const struct brw_perf_query_info
*query
= obj
->query
;
530 uint64_t *accumulator
= obj
->oa
.accumulator
;
534 obj
->oa
.reports_accumulated
++;
536 switch (query
->oa_format
) {
537 case I915_OA_FORMAT_A32u40_A4u32_B8_C8
:
538 brw_perf_query_accumulate_uint32(start
+ 1, end
+ 1, accumulator
+ idx
++); /* timestamp */
539 brw_perf_query_accumulate_uint32(start
+ 3, end
+ 3, accumulator
+ idx
++); /* clock */
541 /* 32x 40bit A counters... */
542 for (i
= 0; i
< 32; i
++)
543 brw_perf_query_accumulate_uint40(i
, start
, end
, accumulator
+ idx
++);
545 /* 4x 32bit A counters... */
546 for (i
= 0; i
< 4; i
++)
547 brw_perf_query_accumulate_uint32(start
+ 36 + i
, end
+ 36 + i
,
548 accumulator
+ idx
++);
550 /* 8x 32bit B counters + 8x 32bit C counters... */
551 for (i
= 0; i
< 16; i
++)
552 brw_perf_query_accumulate_uint32(start
+ 48 + i
, end
+ 48 + i
,
553 accumulator
+ idx
++);
556 case I915_OA_FORMAT_A45_B8_C8
:
557 brw_perf_query_accumulate_uint32(start
+ 1, end
+ 1, accumulator
); /* timestamp */
559 for (i
= 0; i
< 61; i
++)
560 brw_perf_query_accumulate_uint32(start
+ 3 + i
, end
+ 3 + i
, accumulator
+ 1 + i
);
564 unreachable("Can't accumulate OA counters in unknown format");
569 inc_n_oa_users(struct brw_context
*brw
)
571 if (brw
->perfquery
.n_oa_users
== 0 &&
572 drmIoctl(brw
->perfquery
.oa_stream_fd
,
573 I915_PERF_IOCTL_ENABLE
, 0) < 0)
577 ++brw
->perfquery
.n_oa_users
;
583 dec_n_oa_users(struct brw_context
*brw
)
585 /* Disabling the i915 perf stream will effectively disable the OA
586 * counters. Note it's important to be sure there are no outstanding
587 * MI_RPC commands at this point since they could stall the CS
588 * indefinitely once OACONTROL is disabled.
590 --brw
->perfquery
.n_oa_users
;
591 if (brw
->perfquery
.n_oa_users
== 0 &&
592 drmIoctl(brw
->perfquery
.oa_stream_fd
, I915_PERF_IOCTL_DISABLE
, 0) < 0)
594 DBG("WARNING: Error disabling i915 perf stream: %m\n");
598 /* In general if we see anything spurious while accumulating results,
599 * we don't try and continue accumulating the current query, hoping
600 * for the best, we scrap anything outstanding, and then hope for the
601 * best with new queries.
604 discard_all_queries(struct brw_context
*brw
)
606 while (brw
->perfquery
.unaccumulated_elements
) {
607 struct brw_perf_query_object
*obj
= brw
->perfquery
.unaccumulated
[0];
609 obj
->oa
.results_accumulated
= true;
610 drop_from_unaccumulated_query_list(brw
, brw
->perfquery
.unaccumulated
[0]);
617 OA_READ_STATUS_ERROR
,
618 OA_READ_STATUS_UNFINISHED
,
619 OA_READ_STATUS_FINISHED
,
622 static enum OaReadStatus
623 read_oa_samples_until(struct brw_context
*brw
,
624 uint32_t start_timestamp
,
625 uint32_t end_timestamp
)
627 struct exec_node
*tail_node
=
628 exec_list_get_tail(&brw
->perfquery
.sample_buffers
);
629 struct brw_oa_sample_buf
*tail_buf
=
630 exec_node_data(struct brw_oa_sample_buf
, tail_node
, link
);
631 uint32_t last_timestamp
= tail_buf
->last_timestamp
;
634 struct brw_oa_sample_buf
*buf
= get_free_sample_buf(brw
);
638 while ((len
= read(brw
->perfquery
.oa_stream_fd
, buf
->buf
,
639 sizeof(buf
->buf
))) < 0 && errno
== EINTR
)
643 exec_list_push_tail(&brw
->perfquery
.free_sample_buffers
, &buf
->link
);
647 return ((last_timestamp
- start_timestamp
) >=
648 (end_timestamp
- start_timestamp
)) ?
649 OA_READ_STATUS_FINISHED
:
650 OA_READ_STATUS_UNFINISHED
;
652 DBG("Error reading i915 perf samples: %m\n");
655 DBG("Spurious EOF reading i915 perf samples\n");
657 return OA_READ_STATUS_ERROR
;
661 exec_list_push_tail(&brw
->perfquery
.sample_buffers
, &buf
->link
);
663 /* Go through the reports and update the last timestamp. */
665 while (offset
< buf
->len
) {
666 const struct drm_i915_perf_record_header
*header
=
667 (const struct drm_i915_perf_record_header
*) &buf
->buf
[offset
];
668 uint32_t *report
= (uint32_t *) (header
+ 1);
670 if (header
->type
== DRM_I915_PERF_RECORD_SAMPLE
)
671 last_timestamp
= report
[1];
673 offset
+= header
->size
;
676 buf
->last_timestamp
= last_timestamp
;
679 unreachable("not reached");
680 return OA_READ_STATUS_ERROR
;
684 * Try to read all the reports until either the delimiting timestamp
685 * or an error arises.
688 read_oa_samples_for_query(struct brw_context
*brw
,
689 struct brw_perf_query_object
*obj
)
695 /* We need the MI_REPORT_PERF_COUNT to land before we can start
697 assert(!brw_batch_references(&brw
->batch
, obj
->oa
.bo
) &&
698 !brw_bo_busy(obj
->oa
.bo
));
700 /* Map the BO once here and let accumulate_oa_reports() unmap
702 if (obj
->oa
.map
== NULL
)
703 obj
->oa
.map
= brw_bo_map(brw
, obj
->oa
.bo
, MAP_READ
);
705 start
= last
= obj
->oa
.map
;
706 end
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
708 if (start
[0] != obj
->oa
.begin_report_id
) {
709 DBG("Spurious start report id=%"PRIu32
"\n", start
[0]);
712 if (end
[0] != (obj
->oa
.begin_report_id
+ 1)) {
713 DBG("Spurious end report id=%"PRIu32
"\n", end
[0]);
717 /* Read the reports until the end timestamp. */
718 switch (read_oa_samples_until(brw
, start
[1], end
[1])) {
719 case OA_READ_STATUS_ERROR
:
720 /* Fallthrough and let accumulate_oa_reports() deal with the
722 case OA_READ_STATUS_FINISHED
:
724 case OA_READ_STATUS_UNFINISHED
:
728 unreachable("invalid read status");
733 * Accumulate raw OA counter values based on deltas between pairs of
736 * Accumulation starts from the first report captured via
737 * MI_REPORT_PERF_COUNT (MI_RPC) by brw_begin_perf_query() until the
738 * last MI_RPC report requested by brw_end_perf_query(). Between these
739 * two reports there may also some number of periodically sampled OA
740 * reports collected via the i915 perf interface - depending on the
741 * duration of the query.
743 * These periodic snapshots help to ensure we handle counter overflow
744 * correctly by being frequent enough to ensure we don't miss multiple
745 * overflows of a counter between snapshots. For Gen8+ the i915 perf
746 * snapshots provide the extra context-switch reports that let us
747 * subtract out the progress of counters associated with other
748 * contexts running on the system.
751 accumulate_oa_reports(struct brw_context
*brw
,
752 struct brw_perf_query_object
*obj
)
754 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
755 struct gl_perf_query_object
*o
= &obj
->base
;
759 struct exec_node
*first_samples_node
;
761 int out_duration
= 0;
764 assert(obj
->oa
.map
!= NULL
);
766 start
= last
= obj
->oa
.map
;
767 end
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
769 if (start
[0] != obj
->oa
.begin_report_id
) {
770 DBG("Spurious start report id=%"PRIu32
"\n", start
[0]);
773 if (end
[0] != (obj
->oa
.begin_report_id
+ 1)) {
774 DBG("Spurious end report id=%"PRIu32
"\n", end
[0]);
778 obj
->oa
.hw_id
= start
[2];
780 /* See if we have any periodic reports to accumulate too... */
782 /* N.B. The oa.samples_head was set when the query began and
783 * pointed to the tail of the brw->perfquery.sample_buffers list at
784 * the time the query started. Since the buffer existed before the
785 * first MI_REPORT_PERF_COUNT command was emitted we therefore know
786 * that no data in this particular node's buffer can possibly be
787 * associated with the query - so skip ahead one...
789 first_samples_node
= obj
->oa
.samples_head
->next
;
791 foreach_list_typed_from(struct brw_oa_sample_buf
, buf
, link
,
792 &brw
->perfquery
.sample_buffers
,
797 while (offset
< buf
->len
) {
798 const struct drm_i915_perf_record_header
*header
=
799 (const struct drm_i915_perf_record_header
*)(buf
->buf
+ offset
);
801 assert(header
->size
!= 0);
802 assert(header
->size
<= buf
->len
);
804 offset
+= header
->size
;
806 switch (header
->type
) {
807 case DRM_I915_PERF_RECORD_SAMPLE
: {
808 uint32_t *report
= (uint32_t *)(header
+ 1);
811 /* Ignore reports that come before the start marker.
812 * (Note: takes care to allow overflow of 32bit timestamps)
814 if (brw_timebase_scale(brw
, report
[1] - start
[1]) > 5000000000)
817 /* Ignore reports that come after the end marker.
818 * (Note: takes care to allow overflow of 32bit timestamps)
820 if (brw_timebase_scale(brw
, report
[1] - end
[1]) <= 5000000000)
823 /* For Gen8+ since the counters continue while other
824 * contexts are running we need to discount any unrelated
825 * deltas. The hardware automatically generates a report
826 * on context switch which gives us a new reference point
827 * to continuing adding deltas from.
829 * For Haswell we can rely on the HW to stop the progress
830 * of OA counters while any other context is acctive.
832 if (devinfo
->gen
>= 8) {
833 if (in_ctx
&& report
[2] != obj
->oa
.hw_id
) {
834 DBG("i915 perf: Switch AWAY (observed by ID change)\n");
837 } else if (in_ctx
== false && report
[2] == obj
->oa
.hw_id
) {
838 DBG("i915 perf: Switch TO\n");
841 /* From experimentation in IGT, we found that the OA unit
842 * might label some report as "idle" (using an invalid
843 * context ID), right after a report for a given context.
844 * Deltas generated by those reports actually belong to the
845 * previous context, even though they're not labelled as
848 * We didn't *really* Switch AWAY in the case that we e.g.
849 * saw a single periodic report while idle...
851 if (out_duration
>= 1)
854 assert(report
[2] == obj
->oa
.hw_id
);
855 DBG("i915 perf: Continuation IN\n");
857 assert(report
[2] != obj
->oa
.hw_id
);
858 DBG("i915 perf: Continuation OUT\n");
865 add_deltas(brw
, obj
, last
, report
);
872 case DRM_I915_PERF_RECORD_OA_BUFFER_LOST
:
873 DBG("i915 perf: OA error: all reports lost\n");
875 case DRM_I915_PERF_RECORD_OA_REPORT_LOST
:
876 DBG("i915 perf: OA report lost\n");
884 add_deltas(brw
, obj
, last
, end
);
886 DBG("Marking %d accumulated - results gathered\n", o
->Id
);
888 obj
->oa
.results_accumulated
= true;
889 drop_from_unaccumulated_query_list(brw
, obj
);
896 discard_all_queries(brw
);
899 /******************************************************************************/
902 open_i915_perf_oa_stream(struct brw_context
*brw
,
909 uint64_t properties
[] = {
910 /* Single context sampling */
911 DRM_I915_PERF_PROP_CTX_HANDLE
, ctx_id
,
913 /* Include OA reports in samples */
914 DRM_I915_PERF_PROP_SAMPLE_OA
, true,
916 /* OA unit configuration */
917 DRM_I915_PERF_PROP_OA_METRICS_SET
, metrics_set_id
,
918 DRM_I915_PERF_PROP_OA_FORMAT
, report_format
,
919 DRM_I915_PERF_PROP_OA_EXPONENT
, period_exponent
,
921 struct drm_i915_perf_open_param param
= {
922 .flags
= I915_PERF_FLAG_FD_CLOEXEC
|
923 I915_PERF_FLAG_FD_NONBLOCK
|
924 I915_PERF_FLAG_DISABLED
,
925 .num_properties
= ARRAY_SIZE(properties
) / 2,
926 .properties_ptr
= (uintptr_t) properties
,
928 int fd
= drmIoctl(drm_fd
, DRM_IOCTL_I915_PERF_OPEN
, ¶m
);
930 DBG("Error opening i915 perf OA stream: %m\n");
934 brw
->perfquery
.oa_stream_fd
= fd
;
936 brw
->perfquery
.current_oa_metrics_set_id
= metrics_set_id
;
937 brw
->perfquery
.current_oa_format
= report_format
;
943 close_perf(struct brw_context
*brw
)
945 if (brw
->perfquery
.oa_stream_fd
!= -1) {
946 close(brw
->perfquery
.oa_stream_fd
);
947 brw
->perfquery
.oa_stream_fd
= -1;
952 capture_frequency_stat_register(struct brw_context
*brw
,
956 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
958 if (devinfo
->gen
>= 7 && devinfo
->gen
<= 8 &&
959 !devinfo
->is_baytrail
&& !devinfo
->is_cherryview
) {
960 brw_store_register_mem32(brw
, bo
, GEN7_RPSTAT1
, bo_offset
);
961 } else if (devinfo
->gen
>= 9) {
962 brw_store_register_mem32(brw
, bo
, GEN9_RPSTAT0
, bo_offset
);
967 * Driver hook for glBeginPerfQueryINTEL().
970 brw_begin_perf_query(struct gl_context
*ctx
,
971 struct gl_perf_query_object
*o
)
973 struct brw_context
*brw
= brw_context(ctx
);
974 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
975 const struct brw_perf_query_info
*query
= obj
->query
;
977 /* We can assume the frontend hides mistaken attempts to Begin a
978 * query object multiple times before its End. Similarly if an
979 * application reuses a query object before results have arrived
980 * the frontend will wait for prior results so we don't need
981 * to support abandoning in-flight results.
984 assert(!o
->Used
|| o
->Ready
); /* no in-flight query to worry about */
986 DBG("Begin(%d)\n", o
->Id
);
988 /* XXX: We have to consider that the command parser unit that parses batch
989 * buffer commands and is used to capture begin/end counter snapshots isn't
990 * implicitly synchronized with what's currently running across other GPU
991 * units (such as the EUs running shaders) that the performance counters are
994 * The intention of performance queries is to measure the work associated
995 * with commands between the begin/end delimiters and so for that to be the
996 * case we need to explicitly synchronize the parsing of commands to capture
997 * Begin/End counter snapshots with what's running across other parts of the
1000 * When the command parser reaches a Begin marker it effectively needs to
1001 * drain everything currently running on the GPU until the hardware is idle
1002 * before capturing the first snapshot of counters - otherwise the results
1003 * would also be measuring the effects of earlier commands.
1005 * When the command parser reaches an End marker it needs to stall until
1006 * everything currently running on the GPU has finished before capturing the
1007 * end snapshot - otherwise the results won't be a complete representation
1010 * Theoretically there could be opportunities to minimize how much of the
1011 * GPU pipeline is drained, or that we stall for, when we know what specific
1012 * units the performance counters being queried relate to but we don't
1013 * currently attempt to be clever here.
1015 * Note: with our current simple approach here then for back-to-back queries
1016 * we will redundantly emit duplicate commands to synchronize the command
1017 * streamer with the rest of the GPU pipeline, but we assume that in HW the
1018 * second synchronization is effectively a NOOP.
1020 * N.B. The final results are based on deltas of counters between (inside)
1021 * Begin/End markers so even though the total wall clock time of the
1022 * workload is stretched by larger pipeline bubbles the bubbles themselves
1023 * are generally invisible to the query results. Whether that's a good or a
1024 * bad thing depends on the use case. For a lower real-time impact while
1025 * capturing metrics then periodic sampling may be a better choice than
1026 * INTEL_performance_query.
1029 * This is our Begin synchronization point to drain current work on the
1030 * GPU before we capture our first counter snapshot...
1032 brw_emit_mi_flush(brw
);
1034 switch (query
->kind
) {
1037 /* Opening an i915 perf stream implies exclusive access to the OA unit
1038 * which will generate counter reports for a specific counter set with a
1039 * specific layout/format so we can't begin any OA based queries that
1040 * require a different counter set or format unless we get an opportunity
1041 * to close the stream and open a new one...
1043 if (brw
->perfquery
.oa_stream_fd
!= -1 &&
1044 brw
->perfquery
.current_oa_metrics_set_id
!=
1045 query
->oa_metrics_set_id
) {
1047 if (brw
->perfquery
.n_oa_users
!= 0)
1053 /* If the OA counters aren't already on, enable them. */
1054 if (brw
->perfquery
.oa_stream_fd
== -1) {
1055 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1056 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1058 /* The period_exponent gives a sampling period as follows:
1059 * sample_period = timestamp_period * 2^(period_exponent + 1)
1061 * The timestamps increments every 80ns (HSW), ~52ns (GEN9LP) or
1064 * The counter overflow period is derived from the EuActive counter
1065 * which reads a counter that increments by the number of clock
1066 * cycles multiplied by the number of EUs. It can be calculated as:
1068 * 2^(number of bits in A counter) / (n_eus * max_gen_freq * 2)
1070 * (E.g. 40 EUs @ 1GHz = ~53ms)
1072 * We select a sampling period inferior to that overflow period to
1073 * ensure we cannot see more than 1 counter overflow, otherwise we
1074 * could loose information.
1077 int a_counter_in_bits
= 32;
1078 if (devinfo
->gen
>= 8)
1079 a_counter_in_bits
= 40;
1081 uint64_t overflow_period
= pow(2, a_counter_in_bits
) /
1082 (brw
->perfquery
.sys_vars
.n_eus
*
1083 /* drop 1GHz freq to have units in nanoseconds */
1086 DBG("A counter overflow period: %"PRIu64
"ns, %"PRIu64
"ms (n_eus=%"PRIu64
")\n",
1087 overflow_period
, overflow_period
/ 1000000ul, brw
->perfquery
.sys_vars
.n_eus
);
1089 int period_exponent
= 0;
1090 uint64_t prev_sample_period
, next_sample_period
;
1091 for (int e
= 0; e
< 30; e
++) {
1092 prev_sample_period
= 1000000000ull * pow(2, e
+ 1) / devinfo
->timestamp_frequency
;
1093 next_sample_period
= 1000000000ull * pow(2, e
+ 2) / devinfo
->timestamp_frequency
;
1095 /* Take the previous sampling period, lower than the overflow
1098 if (prev_sample_period
< overflow_period
&&
1099 next_sample_period
> overflow_period
)
1100 period_exponent
= e
+ 1;
1103 if (period_exponent
== 0) {
1104 DBG("WARNING: enable to find a sampling exponent\n");
1108 DBG("OA sampling exponent: %i ~= %"PRIu64
"ms\n", period_exponent
,
1109 prev_sample_period
/ 1000000ul);
1111 if (!open_i915_perf_oa_stream(brw
,
1112 query
->oa_metrics_set_id
,
1115 screen
->fd
, /* drm fd */
1119 assert(brw
->perfquery
.current_oa_metrics_set_id
==
1120 query
->oa_metrics_set_id
&&
1121 brw
->perfquery
.current_oa_format
==
1125 if (!inc_n_oa_users(brw
)) {
1126 DBG("WARNING: Error enabling i915 perf stream: %m\n");
1131 brw_bo_unreference(obj
->oa
.bo
);
1136 brw_bo_alloc(brw
->bufmgr
, "perf. query OA MI_RPC bo", MI_RPC_BO_SIZE
);
1138 /* Pre-filling the BO helps debug whether writes landed. */
1139 void *map
= brw_bo_map(brw
, obj
->oa
.bo
, MAP_WRITE
);
1140 memset(map
, 0x80, MI_RPC_BO_SIZE
);
1141 brw_bo_unmap(obj
->oa
.bo
);
1144 obj
->oa
.begin_report_id
= brw
->perfquery
.next_query_start_report_id
;
1145 brw
->perfquery
.next_query_start_report_id
+= 2;
1147 /* We flush the batchbuffer here to minimize the chances that MI_RPC
1148 * delimiting commands end up in different batchbuffers. If that's the
1149 * case, the measurement will include the time it takes for the kernel
1150 * scheduler to load a new request into the hardware. This is manifested in
1151 * tools like frameretrace by spikes in the "GPU Core Clocks" counter.
1153 intel_batchbuffer_flush(brw
);
1155 /* Take a starting OA counter snapshot. */
1156 brw
->vtbl
.emit_mi_report_perf_count(brw
, obj
->oa
.bo
, 0,
1157 obj
->oa
.begin_report_id
);
1158 capture_frequency_stat_register(brw
, obj
->oa
.bo
, MI_FREQ_START_OFFSET_BYTES
);
1160 ++brw
->perfquery
.n_active_oa_queries
;
1162 /* No already-buffered samples can possibly be associated with this query
1163 * so create a marker within the list of sample buffers enabling us to
1164 * easily ignore earlier samples when processing this query after
1167 assert(!exec_list_is_empty(&brw
->perfquery
.sample_buffers
));
1168 obj
->oa
.samples_head
= exec_list_get_tail(&brw
->perfquery
.sample_buffers
);
1170 struct brw_oa_sample_buf
*buf
=
1171 exec_node_data(struct brw_oa_sample_buf
, obj
->oa
.samples_head
, link
);
1173 /* This reference will ensure that future/following sample
1174 * buffers (that may relate to this query) can't be freed until
1175 * this drops to zero.
1179 obj
->oa
.hw_id
= 0xffffffff;
1180 memset(obj
->oa
.accumulator
, 0, sizeof(obj
->oa
.accumulator
));
1181 obj
->oa
.results_accumulated
= false;
1183 add_to_unaccumulated_query_list(brw
, obj
);
1186 case PIPELINE_STATS
:
1187 if (obj
->pipeline_stats
.bo
) {
1188 brw_bo_unreference(obj
->pipeline_stats
.bo
);
1189 obj
->pipeline_stats
.bo
= NULL
;
1192 obj
->pipeline_stats
.bo
=
1193 brw_bo_alloc(brw
->bufmgr
, "perf. query pipeline stats bo",
1196 /* Take starting snapshots. */
1197 snapshot_statistics_registers(brw
, obj
, 0);
1199 ++brw
->perfquery
.n_active_pipeline_stats_queries
;
1203 unreachable("Unknown query type");
1207 if (INTEL_DEBUG
& DEBUG_PERFMON
)
1208 dump_perf_queries(brw
);
1214 * Driver hook for glEndPerfQueryINTEL().
1217 brw_end_perf_query(struct gl_context
*ctx
,
1218 struct gl_perf_query_object
*o
)
1220 struct brw_context
*brw
= brw_context(ctx
);
1221 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1223 DBG("End(%d)\n", o
->Id
);
1225 /* Ensure that the work associated with the queried commands will have
1226 * finished before taking our query end counter readings.
1228 * For more details see comment in brw_begin_perf_query for
1229 * corresponding flush.
1231 brw_emit_mi_flush(brw
);
1233 switch (obj
->query
->kind
) {
1236 /* NB: It's possible that the query will have already been marked
1237 * as 'accumulated' if an error was seen while reading samples
1238 * from perf. In this case we mustn't try and emit a closing
1239 * MI_RPC command in case the OA unit has already been disabled
1241 if (!obj
->oa
.results_accumulated
) {
1242 /* Take an ending OA counter snapshot. */
1243 capture_frequency_stat_register(brw
, obj
->oa
.bo
, MI_FREQ_END_OFFSET_BYTES
);
1244 brw
->vtbl
.emit_mi_report_perf_count(brw
, obj
->oa
.bo
,
1245 MI_RPC_BO_END_OFFSET_BYTES
,
1246 obj
->oa
.begin_report_id
+ 1);
1249 --brw
->perfquery
.n_active_oa_queries
;
1251 /* NB: even though the query has now ended, it can't be accumulated
1252 * until the end MI_REPORT_PERF_COUNT snapshot has been written
1257 case PIPELINE_STATS
:
1258 snapshot_statistics_registers(brw
, obj
,
1259 STATS_BO_END_OFFSET_BYTES
);
1260 --brw
->perfquery
.n_active_pipeline_stats_queries
;
1264 unreachable("Unknown query type");
1270 brw_wait_perf_query(struct gl_context
*ctx
, struct gl_perf_query_object
*o
)
1272 struct brw_context
*brw
= brw_context(ctx
);
1273 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1274 struct brw_bo
*bo
= NULL
;
1278 switch (obj
->query
->kind
) {
1283 case PIPELINE_STATS
:
1284 bo
= obj
->pipeline_stats
.bo
;
1288 unreachable("Unknown query type");
1295 /* If the current batch references our results bo then we need to
1298 if (brw_batch_references(&brw
->batch
, bo
))
1299 intel_batchbuffer_flush(brw
);
1301 brw_bo_wait_rendering(bo
);
1303 /* Due to a race condition between the OA unit signaling report
1304 * availability and the report actually being written into memory,
1305 * we need to wait for all the reports to come in before we can
1308 if (obj
->query
->kind
== OA_COUNTERS
) {
1309 while (!read_oa_samples_for_query(brw
, obj
))
1315 brw_is_perf_query_ready(struct gl_context
*ctx
,
1316 struct gl_perf_query_object
*o
)
1318 struct brw_context
*brw
= brw_context(ctx
);
1319 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1324 switch (obj
->query
->kind
) {
1326 return (obj
->oa
.results_accumulated
||
1328 !brw_batch_references(&brw
->batch
, obj
->oa
.bo
) &&
1329 !brw_bo_busy(obj
->oa
.bo
) &&
1330 read_oa_samples_for_query(brw
, obj
)));
1331 case PIPELINE_STATS
:
1332 return (obj
->pipeline_stats
.bo
&&
1333 !brw_batch_references(&brw
->batch
, obj
->pipeline_stats
.bo
) &&
1334 !brw_bo_busy(obj
->pipeline_stats
.bo
));
1337 unreachable("Unknown query type");
1345 read_gt_frequency(struct brw_context
*brw
,
1346 struct brw_perf_query_object
*obj
)
1348 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1349 uint32_t start
= *((uint32_t *)(obj
->oa
.map
+ MI_FREQ_START_OFFSET_BYTES
)),
1350 end
= *((uint32_t *)(obj
->oa
.map
+ MI_FREQ_END_OFFSET_BYTES
));
1352 switch (devinfo
->gen
) {
1355 obj
->oa
.gt_frequency
[0] = GET_FIELD(start
, GEN7_RPSTAT1_CURR_GT_FREQ
) * 50ULL;
1356 obj
->oa
.gt_frequency
[1] = GET_FIELD(end
, GEN7_RPSTAT1_CURR_GT_FREQ
) * 50ULL;
1361 obj
->oa
.gt_frequency
[0] = GET_FIELD(start
, GEN9_RPSTAT0_CURR_GT_FREQ
) * 50ULL / 3ULL;
1362 obj
->oa
.gt_frequency
[1] = GET_FIELD(end
, GEN9_RPSTAT0_CURR_GT_FREQ
) * 50ULL / 3ULL;
1365 unreachable("unexpected gen");
1368 /* Put the numbers into Hz. */
1369 obj
->oa
.gt_frequency
[0] *= 1000000ULL;
1370 obj
->oa
.gt_frequency
[1] *= 1000000ULL;
1374 get_oa_counter_data(struct brw_context
*brw
,
1375 struct brw_perf_query_object
*obj
,
1379 const struct brw_perf_query_info
*query
= obj
->query
;
1380 int n_counters
= query
->n_counters
;
1383 if (!obj
->oa
.results_accumulated
) {
1384 read_gt_frequency(brw
, obj
);
1385 accumulate_oa_reports(brw
, obj
);
1386 assert(obj
->oa
.results_accumulated
);
1388 brw_bo_unmap(obj
->oa
.bo
);
1392 for (int i
= 0; i
< n_counters
; i
++) {
1393 const struct brw_perf_query_counter
*counter
= &query
->counters
[i
];
1394 uint64_t *out_uint64
;
1397 if (counter
->size
) {
1398 switch (counter
->data_type
) {
1399 case GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL
:
1400 out_uint64
= (uint64_t *)(data
+ counter
->offset
);
1401 *out_uint64
= counter
->oa_counter_read_uint64(brw
, query
,
1402 obj
->oa
.accumulator
);
1404 case GL_PERFQUERY_COUNTER_DATA_FLOAT_INTEL
:
1405 out_float
= (float *)(data
+ counter
->offset
);
1406 *out_float
= counter
->oa_counter_read_float(brw
, query
,
1407 obj
->oa
.accumulator
);
1410 /* So far we aren't using uint32, double or bool32... */
1411 unreachable("unexpected counter data type");
1413 written
= counter
->offset
+ counter
->size
;
1421 get_pipeline_stats_data(struct brw_context
*brw
,
1422 struct brw_perf_query_object
*obj
,
1427 const struct brw_perf_query_info
*query
= obj
->query
;
1428 int n_counters
= obj
->query
->n_counters
;
1431 uint64_t *start
= brw_bo_map(brw
, obj
->pipeline_stats
.bo
, MAP_READ
);
1432 uint64_t *end
= start
+ (STATS_BO_END_OFFSET_BYTES
/ sizeof(uint64_t));
1434 for (int i
= 0; i
< n_counters
; i
++) {
1435 const struct brw_perf_query_counter
*counter
= &query
->counters
[i
];
1436 uint64_t value
= end
[i
] - start
[i
];
1438 if (counter
->pipeline_stat
.numerator
!=
1439 counter
->pipeline_stat
.denominator
) {
1440 value
*= counter
->pipeline_stat
.numerator
;
1441 value
/= counter
->pipeline_stat
.denominator
;
1444 *((uint64_t *)p
) = value
;
1448 brw_bo_unmap(obj
->pipeline_stats
.bo
);
1454 * Driver hook for glGetPerfQueryDataINTEL().
1457 brw_get_perf_query_data(struct gl_context
*ctx
,
1458 struct gl_perf_query_object
*o
,
1461 GLuint
*bytes_written
)
1463 struct brw_context
*brw
= brw_context(ctx
);
1464 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1467 assert(brw_is_perf_query_ready(ctx
, o
));
1469 DBG("GetData(%d)\n", o
->Id
);
1471 if (INTEL_DEBUG
& DEBUG_PERFMON
)
1472 dump_perf_queries(brw
);
1474 /* We expect that the frontend only calls this hook when it knows
1475 * that results are available.
1479 switch (obj
->query
->kind
) {
1481 written
= get_oa_counter_data(brw
, obj
, data_size
, (uint8_t *)data
);
1484 case PIPELINE_STATS
:
1485 written
= get_pipeline_stats_data(brw
, obj
, data_size
, (uint8_t *)data
);
1489 unreachable("Unknown query type");
1494 *bytes_written
= written
;
1497 static struct gl_perf_query_object
*
1498 brw_new_perf_query_object(struct gl_context
*ctx
, unsigned query_index
)
1500 struct brw_context
*brw
= brw_context(ctx
);
1501 const struct brw_perf_query_info
*query
=
1502 &brw
->perfquery
.queries
[query_index
];
1503 struct brw_perf_query_object
*obj
=
1504 calloc(1, sizeof(struct brw_perf_query_object
));
1511 brw
->perfquery
.n_query_instances
++;
1517 * Driver hook for glDeletePerfQueryINTEL().
1520 brw_delete_perf_query(struct gl_context
*ctx
,
1521 struct gl_perf_query_object
*o
)
1523 struct brw_context
*brw
= brw_context(ctx
);
1524 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1526 /* We can assume that the frontend waits for a query to complete
1527 * before ever calling into here, so we don't have to worry about
1528 * deleting an in-flight query object.
1531 assert(!o
->Used
|| o
->Ready
);
1533 DBG("Delete(%d)\n", o
->Id
);
1535 switch (obj
->query
->kind
) {
1538 if (!obj
->oa
.results_accumulated
) {
1539 drop_from_unaccumulated_query_list(brw
, obj
);
1540 dec_n_oa_users(brw
);
1543 brw_bo_unreference(obj
->oa
.bo
);
1547 obj
->oa
.results_accumulated
= false;
1550 case PIPELINE_STATS
:
1551 if (obj
->pipeline_stats
.bo
) {
1552 brw_bo_unreference(obj
->pipeline_stats
.bo
);
1553 obj
->pipeline_stats
.bo
= NULL
;
1558 unreachable("Unknown query type");
1564 /* As an indication that the INTEL_performance_query extension is no
1565 * longer in use, it's a good time to free our cache of sample
1566 * buffers and close any current i915-perf stream.
1568 if (--brw
->perfquery
.n_query_instances
== 0) {
1569 free_sample_bufs(brw
);
1574 /******************************************************************************/
1577 init_pipeline_statistic_query_registers(struct brw_context
*brw
)
1579 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1580 struct brw_perf_query_info
*query
= brw_perf_query_append_query_info(brw
);
1582 query
->kind
= PIPELINE_STATS
;
1583 query
->name
= "Pipeline Statistics Registers";
1584 query
->n_counters
= 0;
1586 rzalloc_array(brw
, struct brw_perf_query_counter
, MAX_STAT_COUNTERS
);
1588 brw_perf_query_info_add_basic_stat_reg(query
, IA_VERTICES_COUNT
,
1589 "N vertices submitted");
1590 brw_perf_query_info_add_basic_stat_reg(query
, IA_PRIMITIVES_COUNT
,
1591 "N primitives submitted");
1592 brw_perf_query_info_add_basic_stat_reg(query
, VS_INVOCATION_COUNT
,
1593 "N vertex shader invocations");
1595 if (devinfo
->gen
== 6) {
1596 brw_perf_query_info_add_stat_reg(query
, GEN6_SO_PRIM_STORAGE_NEEDED
, 1, 1,
1597 "SO_PRIM_STORAGE_NEEDED",
1598 "N geometry shader stream-out primitives (total)");
1599 brw_perf_query_info_add_stat_reg(query
, GEN6_SO_NUM_PRIMS_WRITTEN
, 1, 1,
1600 "SO_NUM_PRIMS_WRITTEN",
1601 "N geometry shader stream-out primitives (written)");
1603 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(0), 1, 1,
1604 "SO_PRIM_STORAGE_NEEDED (Stream 0)",
1605 "N stream-out (stream 0) primitives (total)");
1606 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(1), 1, 1,
1607 "SO_PRIM_STORAGE_NEEDED (Stream 1)",
1608 "N stream-out (stream 1) primitives (total)");
1609 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(2), 1, 1,
1610 "SO_PRIM_STORAGE_NEEDED (Stream 2)",
1611 "N stream-out (stream 2) primitives (total)");
1612 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(3), 1, 1,
1613 "SO_PRIM_STORAGE_NEEDED (Stream 3)",
1614 "N stream-out (stream 3) primitives (total)");
1615 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(0), 1, 1,
1616 "SO_NUM_PRIMS_WRITTEN (Stream 0)",
1617 "N stream-out (stream 0) primitives (written)");
1618 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(1), 1, 1,
1619 "SO_NUM_PRIMS_WRITTEN (Stream 1)",
1620 "N stream-out (stream 1) primitives (written)");
1621 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(2), 1, 1,
1622 "SO_NUM_PRIMS_WRITTEN (Stream 2)",
1623 "N stream-out (stream 2) primitives (written)");
1624 brw_perf_query_info_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(3), 1, 1,
1625 "SO_NUM_PRIMS_WRITTEN (Stream 3)",
1626 "N stream-out (stream 3) primitives (written)");
1629 brw_perf_query_info_add_basic_stat_reg(query
, HS_INVOCATION_COUNT
,
1630 "N TCS shader invocations");
1631 brw_perf_query_info_add_basic_stat_reg(query
, DS_INVOCATION_COUNT
,
1632 "N TES shader invocations");
1634 brw_perf_query_info_add_basic_stat_reg(query
, GS_INVOCATION_COUNT
,
1635 "N geometry shader invocations");
1636 brw_perf_query_info_add_basic_stat_reg(query
, GS_PRIMITIVES_COUNT
,
1637 "N geometry shader primitives emitted");
1639 brw_perf_query_info_add_basic_stat_reg(query
, CL_INVOCATION_COUNT
,
1640 "N primitives entering clipping");
1641 brw_perf_query_info_add_basic_stat_reg(query
, CL_PRIMITIVES_COUNT
,
1642 "N primitives leaving clipping");
1644 if (devinfo
->is_haswell
|| devinfo
->gen
== 8)
1645 brw_perf_query_info_add_stat_reg(query
, PS_INVOCATION_COUNT
, 1, 4,
1646 "N fragment shader invocations",
1647 "N fragment shader invocations");
1649 brw_perf_query_info_add_basic_stat_reg(query
, PS_INVOCATION_COUNT
,
1650 "N fragment shader invocations");
1652 brw_perf_query_info_add_basic_stat_reg(query
, PS_DEPTH_COUNT
, "N z-pass fragments");
1654 if (devinfo
->gen
>= 7)
1655 brw_perf_query_info_add_basic_stat_reg(query
, CS_INVOCATION_COUNT
,
1656 "N compute shader invocations");
1658 query
->data_size
= sizeof(uint64_t) * query
->n_counters
;
1662 register_oa_config(struct brw_context
*brw
,
1663 const struct brw_perf_query_info
*query
,
1666 struct brw_perf_query_info
*registred_query
=
1667 brw_perf_query_append_query_info(brw
);
1669 *registred_query
= *query
;
1670 registred_query
->oa_metrics_set_id
= config_id
;
1671 DBG("metric set registred: id = %" PRIu64
", guid = %s\n",
1672 registred_query
->oa_metrics_set_id
, query
->guid
);
1676 enumerate_sysfs_metrics(struct brw_context
*brw
)
1679 DIR *metricsdir
= NULL
;
1680 struct dirent
*metric_entry
;
1683 len
= snprintf(buf
, sizeof(buf
), "%s/metrics", brw
->perfquery
.sysfs_dev_dir
);
1684 if (len
< 0 || len
>= sizeof(buf
)) {
1685 DBG("Failed to concatenate path to sysfs metrics/ directory\n");
1689 metricsdir
= opendir(buf
);
1691 DBG("Failed to open %s: %m\n", buf
);
1695 while ((metric_entry
= readdir(metricsdir
))) {
1696 struct hash_entry
*entry
;
1698 if ((metric_entry
->d_type
!= DT_DIR
&&
1699 metric_entry
->d_type
!= DT_LNK
) ||
1700 metric_entry
->d_name
[0] == '.')
1703 DBG("metric set: %s\n", metric_entry
->d_name
);
1704 entry
= _mesa_hash_table_search(brw
->perfquery
.oa_metrics_table
,
1705 metric_entry
->d_name
);
1709 len
= snprintf(buf
, sizeof(buf
), "%s/metrics/%s/id",
1710 brw
->perfquery
.sysfs_dev_dir
, metric_entry
->d_name
);
1711 if (len
< 0 || len
>= sizeof(buf
)) {
1712 DBG("Failed to concatenate path to sysfs metric id file\n");
1716 if (!read_file_uint64(buf
, &id
)) {
1717 DBG("Failed to read metric set id from %s: %m", buf
);
1721 register_oa_config(brw
, (const struct brw_perf_query_info
*)entry
->data
, id
);
1723 DBG("metric set not known by mesa (skipping)\n");
1726 closedir(metricsdir
);
1730 kernel_has_dynamic_config_support(struct brw_context
*brw
)
1732 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1733 struct hash_entry
*entry
;
1735 hash_table_foreach(brw
->perfquery
.oa_metrics_table
, entry
) {
1736 struct brw_perf_query_info
*query
= entry
->data
;
1737 char config_path
[280];
1740 snprintf(config_path
, sizeof(config_path
), "%s/metrics/%s/id",
1741 brw
->perfquery
.sysfs_dev_dir
, query
->guid
);
1743 /* Look for the test config, which we know we can't replace. */
1744 if (read_file_uint64(config_path
, &config_id
) && config_id
== 1) {
1745 return drmIoctl(screen
->fd
, DRM_IOCTL_I915_PERF_REMOVE_CONFIG
,
1746 &config_id
) < 0 && errno
== ENOENT
;
1754 init_oa_configs(struct brw_context
*brw
)
1756 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1757 struct hash_entry
*entry
;
1759 hash_table_foreach(brw
->perfquery
.oa_metrics_table
, entry
) {
1760 const struct brw_perf_query_info
*query
= entry
->data
;
1761 struct drm_i915_perf_oa_config config
;
1762 char config_path
[280];
1766 snprintf(config_path
, sizeof(config_path
), "%s/metrics/%s/id",
1767 brw
->perfquery
.sysfs_dev_dir
, query
->guid
);
1769 /* Don't recreate already loaded configs. */
1770 if (read_file_uint64(config_path
, &config_id
)) {
1771 DBG("metric set: %s (already loaded)\n", query
->guid
);
1772 register_oa_config(brw
, query
, config_id
);
1776 memset(&config
, 0, sizeof(config
));
1778 memcpy(config
.uuid
, query
->guid
, sizeof(config
.uuid
));
1780 config
.n_mux_regs
= query
->n_mux_regs
;
1781 config
.mux_regs_ptr
= (uintptr_t) query
->mux_regs
;
1783 config
.n_boolean_regs
= query
->n_b_counter_regs
;
1784 config
.boolean_regs_ptr
= (uintptr_t) query
->b_counter_regs
;
1786 config
.n_flex_regs
= query
->n_flex_regs
;
1787 config
.flex_regs_ptr
= (uintptr_t) query
->flex_regs
;
1789 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_I915_PERF_ADD_CONFIG
, &config
);
1791 DBG("Failed to load \"%s\" (%s) metrics set in kernel: %s\n",
1792 query
->name
, query
->guid
, strerror(errno
));
1796 register_oa_config(brw
, query
, ret
);
1797 DBG("metric set: %s (added)\n", query
->guid
);
1802 query_topology(struct brw_context
*brw
)
1804 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1805 struct drm_i915_query_item item
= {
1806 .query_id
= DRM_I915_QUERY_TOPOLOGY_INFO
,
1808 struct drm_i915_query query
= {
1810 .items_ptr
= (uintptr_t) &item
,
1813 if (drmIoctl(screen
->fd
, DRM_IOCTL_I915_QUERY
, &query
))
1816 struct drm_i915_query_topology_info
*topo_info
=
1817 (struct drm_i915_query_topology_info
*) calloc(1, item
.length
);
1818 item
.data_ptr
= (uintptr_t) topo_info
;
1820 if (drmIoctl(screen
->fd
, DRM_IOCTL_I915_QUERY
, &query
) ||
1824 gen_device_info_update_from_topology(&brw
->screen
->devinfo
,
1833 getparam_topology(struct brw_context
*brw
)
1835 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1836 drm_i915_getparam_t gp
;
1840 gp
.param
= I915_PARAM_SLICE_MASK
;
1841 gp
.value
= &slice_mask
;
1842 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
1846 int subslice_mask
= 0;
1847 gp
.param
= I915_PARAM_SUBSLICE_MASK
;
1848 gp
.value
= &subslice_mask
;
1849 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
1853 gen_device_info_update_from_masks(&brw
->screen
->devinfo
,
1856 brw
->screen
->eu_total
);
1862 compute_topology_builtins(struct brw_context
*brw
)
1864 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1866 brw
->perfquery
.sys_vars
.slice_mask
= devinfo
->slice_masks
;
1867 brw
->perfquery
.sys_vars
.n_eu_slices
= devinfo
->num_slices
;
1869 for (int i
= 0; i
< sizeof(devinfo
->subslice_masks
[i
]); i
++) {
1870 brw
->perfquery
.sys_vars
.n_eu_sub_slices
+=
1871 _mesa_bitcount(devinfo
->subslice_masks
[i
]);
1874 for (int i
= 0; i
< sizeof(devinfo
->eu_masks
); i
++)
1875 brw
->perfquery
.sys_vars
.n_eus
+= _mesa_bitcount(devinfo
->eu_masks
[i
]);
1877 brw
->perfquery
.sys_vars
.eu_threads_count
=
1878 brw
->perfquery
.sys_vars
.n_eus
* devinfo
->num_thread_per_eu
;
1880 /* At the moment the subslice mask builtin has groups of 3bits for each
1883 * Ideally equations would be updated to have a slice/subslice query
1884 * function/operator.
1886 brw
->perfquery
.sys_vars
.subslice_mask
= 0;
1887 for (int s
= 0; s
< util_last_bit(devinfo
->slice_masks
); s
++) {
1888 for (int ss
= 0; ss
< (devinfo
->subslice_slice_stride
* 8); ss
++) {
1889 if (gen_device_info_subslice_available(devinfo
, s
, ss
))
1890 brw
->perfquery
.sys_vars
.subslice_mask
|= 1UL << (s
* 3 + ss
);
1896 init_oa_sys_vars(struct brw_context
*brw
)
1898 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1899 uint64_t min_freq_mhz
= 0, max_freq_mhz
= 0;
1900 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1902 if (!read_sysfs_drm_device_file_uint64(brw
, "gt_min_freq_mhz", &min_freq_mhz
))
1905 if (!read_sysfs_drm_device_file_uint64(brw
, "gt_max_freq_mhz", &max_freq_mhz
))
1908 if (!query_topology(brw
)) {
1909 /* We need the i915 query uAPI on CNL+ (kernel 4.17+). */
1910 if (devinfo
->gen
>= 10)
1913 if (!getparam_topology(brw
)) {
1914 /* We need the SLICE_MASK/SUBSLICE_MASK on gen8+ (kernel 4.13+). */
1915 if (devinfo
->gen
>= 8)
1918 /* On Haswell, the values are already computed for us in
1924 memset(&brw
->perfquery
.sys_vars
, 0, sizeof(brw
->perfquery
.sys_vars
));
1925 brw
->perfquery
.sys_vars
.gt_min_freq
= min_freq_mhz
* 1000000;
1926 brw
->perfquery
.sys_vars
.gt_max_freq
= max_freq_mhz
* 1000000;
1927 brw
->perfquery
.sys_vars
.timestamp_frequency
= devinfo
->timestamp_frequency
;
1928 brw
->perfquery
.sys_vars
.revision
= intel_device_get_revision(screen
->fd
);
1929 compute_topology_builtins(brw
);
1935 get_sysfs_dev_dir(struct brw_context
*brw
)
1937 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1941 struct dirent
*drm_entry
;
1944 brw
->perfquery
.sysfs_dev_dir
[0] = '\0';
1946 if (fstat(screen
->fd
, &sb
)) {
1947 DBG("Failed to stat DRM fd\n");
1951 maj
= major(sb
.st_rdev
);
1952 min
= minor(sb
.st_rdev
);
1954 if (!S_ISCHR(sb
.st_mode
)) {
1955 DBG("DRM fd is not a character device as expected\n");
1959 len
= snprintf(brw
->perfquery
.sysfs_dev_dir
,
1960 sizeof(brw
->perfquery
.sysfs_dev_dir
),
1961 "/sys/dev/char/%d:%d/device/drm", maj
, min
);
1962 if (len
< 0 || len
>= sizeof(brw
->perfquery
.sysfs_dev_dir
)) {
1963 DBG("Failed to concatenate sysfs path to drm device\n");
1967 drmdir
= opendir(brw
->perfquery
.sysfs_dev_dir
);
1969 DBG("Failed to open %s: %m\n", brw
->perfquery
.sysfs_dev_dir
);
1973 while ((drm_entry
= readdir(drmdir
))) {
1974 if ((drm_entry
->d_type
== DT_DIR
||
1975 drm_entry
->d_type
== DT_LNK
) &&
1976 strncmp(drm_entry
->d_name
, "card", 4) == 0)
1978 len
= snprintf(brw
->perfquery
.sysfs_dev_dir
,
1979 sizeof(brw
->perfquery
.sysfs_dev_dir
),
1980 "/sys/dev/char/%d:%d/device/drm/%s",
1981 maj
, min
, drm_entry
->d_name
);
1983 if (len
< 0 || len
>= sizeof(brw
->perfquery
.sysfs_dev_dir
))
1992 DBG("Failed to find cardX directory under /sys/dev/char/%d:%d/device/drm\n",
1998 typedef void (*perf_register_oa_queries_t
)(struct brw_context
*);
2000 static perf_register_oa_queries_t
2001 get_register_queries_function(const struct gen_device_info
*devinfo
)
2003 if (devinfo
->is_haswell
)
2004 return brw_oa_register_queries_hsw
;
2005 if (devinfo
->is_cherryview
)
2006 return brw_oa_register_queries_chv
;
2007 if (devinfo
->is_broadwell
)
2008 return brw_oa_register_queries_bdw
;
2009 if (devinfo
->is_broxton
)
2010 return brw_oa_register_queries_bxt
;
2011 if (devinfo
->is_skylake
) {
2012 if (devinfo
->gt
== 2)
2013 return brw_oa_register_queries_sklgt2
;
2014 if (devinfo
->gt
== 3)
2015 return brw_oa_register_queries_sklgt3
;
2016 if (devinfo
->gt
== 4)
2017 return brw_oa_register_queries_sklgt4
;
2019 if (devinfo
->is_kabylake
) {
2020 if (devinfo
->gt
== 2)
2021 return brw_oa_register_queries_kblgt2
;
2022 if (devinfo
->gt
== 3)
2023 return brw_oa_register_queries_kblgt3
;
2025 if (devinfo
->is_geminilake
)
2026 return brw_oa_register_queries_glk
;
2027 if (devinfo
->is_coffeelake
) {
2028 if (devinfo
->gt
== 2)
2029 return brw_oa_register_queries_cflgt2
;
2030 if (devinfo
->gt
== 3)
2031 return brw_oa_register_queries_cflgt3
;
2033 if (devinfo
->is_cannonlake
)
2034 return brw_oa_register_queries_cnl
;
2040 brw_init_perf_query_info(struct gl_context
*ctx
)
2042 struct brw_context
*brw
= brw_context(ctx
);
2043 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
2044 bool i915_perf_oa_available
= false;
2046 perf_register_oa_queries_t oa_register
;
2048 if (brw
->perfquery
.n_queries
)
2049 return brw
->perfquery
.n_queries
;
2051 init_pipeline_statistic_query_registers(brw
);
2053 oa_register
= get_register_queries_function(devinfo
);
2055 /* The existence of this sysctl parameter implies the kernel supports
2056 * the i915 perf interface.
2058 if (stat("/proc/sys/dev/i915/perf_stream_paranoid", &sb
) == 0) {
2060 /* If _paranoid == 1 then on Gen8+ we won't be able to access OA
2061 * metrics unless running as root.
2063 if (devinfo
->is_haswell
)
2064 i915_perf_oa_available
= true;
2066 uint64_t paranoid
= 1;
2068 read_file_uint64("/proc/sys/dev/i915/perf_stream_paranoid", ¶noid
);
2070 if (paranoid
== 0 || geteuid() == 0)
2071 i915_perf_oa_available
= true;
2075 if (i915_perf_oa_available
&&
2077 get_sysfs_dev_dir(brw
) &&
2078 init_oa_sys_vars(brw
))
2080 brw
->perfquery
.oa_metrics_table
=
2081 _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
2082 _mesa_key_string_equal
);
2084 /* Index all the metric sets mesa knows about before looking to see what
2085 * the kernel is advertising.
2089 if (likely((INTEL_DEBUG
& DEBUG_NO_OACONFIG
) == 0) &&
2090 kernel_has_dynamic_config_support(brw
))
2091 init_oa_configs(brw
);
2093 enumerate_sysfs_metrics(brw
);
2096 brw
->perfquery
.unaccumulated
=
2097 ralloc_array(brw
, struct brw_perf_query_object
*, 2);
2098 brw
->perfquery
.unaccumulated_elements
= 0;
2099 brw
->perfquery
.unaccumulated_array_size
= 2;
2101 exec_list_make_empty(&brw
->perfquery
.sample_buffers
);
2102 exec_list_make_empty(&brw
->perfquery
.free_sample_buffers
);
2104 /* It's convenient to guarantee that this linked list of sample
2105 * buffers is never empty so we add an empty head so when we
2106 * Begin an OA query we can always take a reference on a buffer
2109 struct brw_oa_sample_buf
*buf
= get_free_sample_buf(brw
);
2110 exec_list_push_head(&brw
->perfquery
.sample_buffers
, &buf
->link
);
2112 brw
->perfquery
.oa_stream_fd
= -1;
2114 brw
->perfquery
.next_query_start_report_id
= 1000;
2116 return brw
->perfquery
.n_queries
;
2120 brw_init_performance_queries(struct brw_context
*brw
)
2122 struct gl_context
*ctx
= &brw
->ctx
;
2124 ctx
->Driver
.InitPerfQueryInfo
= brw_init_perf_query_info
;
2125 ctx
->Driver
.GetPerfQueryInfo
= brw_get_perf_query_info
;
2126 ctx
->Driver
.GetPerfCounterInfo
= brw_get_perf_counter_info
;
2127 ctx
->Driver
.NewPerfQueryObject
= brw_new_perf_query_object
;
2128 ctx
->Driver
.DeletePerfQuery
= brw_delete_perf_query
;
2129 ctx
->Driver
.BeginPerfQuery
= brw_begin_perf_query
;
2130 ctx
->Driver
.EndPerfQuery
= brw_end_perf_query
;
2131 ctx
->Driver
.WaitPerfQuery
= brw_wait_perf_query
;
2132 ctx
->Driver
.IsPerfQueryReady
= brw_is_perf_query_ready
;
2133 ctx
->Driver
.GetPerfQueryData
= brw_get_perf_query_data
;