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_hsw.h"
75 #include "brw_oa_bdw.h"
76 #include "brw_oa_chv.h"
77 #include "brw_oa_sklgt2.h"
78 #include "brw_oa_sklgt3.h"
79 #include "brw_oa_sklgt4.h"
80 #include "brw_oa_bxt.h"
81 #include "brw_oa_kblgt2.h"
82 #include "brw_oa_kblgt3.h"
83 #include "brw_oa_glk.h"
84 #include "brw_oa_cflgt2.h"
85 #include "brw_oa_cflgt3.h"
86 #include "intel_batchbuffer.h"
88 #define FILE_DEBUG_FLAG DEBUG_PERFMON
91 * The largest OA formats we can use include:
93 * 1 timestamp, 45 A counters, 8 B counters and 8 C counters.
95 * 1 timestamp, 1 clock, 36 A counters, 8 B counters and 8 C counters
97 #define MAX_OA_REPORT_COUNTERS 62
99 #define OAREPORT_REASON_MASK 0x3f
100 #define OAREPORT_REASON_SHIFT 19
101 #define OAREPORT_REASON_TIMER (1<<0)
102 #define OAREPORT_REASON_TRIGGER1 (1<<1)
103 #define OAREPORT_REASON_TRIGGER2 (1<<2)
104 #define OAREPORT_REASON_CTX_SWITCH (1<<3)
105 #define OAREPORT_REASON_GO_TRANSITION (1<<4)
107 #define I915_PERF_OA_SAMPLE_SIZE (8 + /* drm_i915_perf_record_header */ \
108 256) /* OA counter report */
111 * Periodic OA samples are read() into these buffer structures via the
112 * i915 perf kernel interface and appended to the
113 * brw->perfquery.sample_buffers linked list. When we process the
114 * results of an OA metrics query we need to consider all the periodic
115 * samples between the Begin and End MI_REPORT_PERF_COUNT command
118 * 'Periodic' is a simplification as there are other automatic reports
119 * written by the hardware also buffered here.
121 * Considering three queries, A, B and C:
124 * ________________A_________________
126 * | ________B_________ _____C___________
129 * And an illustration of sample buffers read over this time frame:
130 * [HEAD ][ ][ ][ ][ ][ ][ ][ ][TAIL ]
132 * These nodes may hold samples for query A:
133 * [ ][ ][ A ][ A ][ A ][ A ][ A ][ ][ ]
135 * These nodes may hold samples for query B:
136 * [ ][ ][ B ][ B ][ B ][ ][ ][ ][ ]
138 * These nodes may hold samples for query C:
139 * [ ][ ][ ][ ][ ][ C ][ C ][ C ][ ]
141 * The illustration assumes we have an even distribution of periodic
142 * samples so all nodes have the same size plotted against time:
144 * Note, to simplify code, the list is never empty.
146 * With overlapping queries we can see that periodic OA reports may
147 * relate to multiple queries and care needs to be take to keep
148 * track of sample buffers until there are no queries that might
149 * depend on their contents.
151 * We use a node ref counting system where a reference ensures that a
152 * node and all following nodes can't be freed/recycled until the
153 * reference drops to zero.
155 * E.g. with a ref of one here:
156 * [ 0 ][ 0 ][ 1 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ]
158 * These nodes could be freed or recycled ("reaped"):
161 * These must be preserved until the leading ref drops to zero:
162 * [ 1 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ][ 0 ]
164 * When a query starts we take a reference on the current tail of
165 * the list, knowing that no already-buffered samples can possibly
166 * relate to the newly-started query. A pointer to this node is
167 * also saved in the query object's ->oa.samples_head.
169 * E.g. starting query A while there are two nodes in .sample_buffers:
170 * ________________A________
174 * ^_______ Add a reference and store pointer to node in
177 * Moving forward to when the B query starts with no new buffer nodes:
178 * (for reference, i915 perf reads() are only done when queries finish)
179 * ________________A_______
184 * ^_______ Add a reference and store pointer to
185 * node in B->oa.samples_head
187 * Once a query is finished, after an OA query has become 'Ready',
188 * once the End OA report has landed and after we we have processed
189 * all the intermediate periodic samples then we drop the
190 * ->oa.samples_head reference we took at the start.
192 * So when the B query has finished we have:
193 * ________________A________
194 * | ______B___________
196 * [ 0 ][ 1 ][ 0 ][ 0 ][ 0 ]
197 * ^_______ Drop B->oa.samples_head reference
199 * We still can't free these due to the A->oa.samples_head ref:
200 * [ 1 ][ 0 ][ 0 ][ 0 ]
202 * When the A query finishes: (note there's a new ref for C's samples_head)
203 * ________________A_________________
207 * [ 0 ][ 0 ][ 0 ][ 0 ][ 1 ][ 0 ][ 0 ]
208 * ^_______ Drop A->oa.samples_head reference
210 * And we can now reap these nodes up to the C->oa.samples_head:
211 * [ X ][ X ][ X ][ X ]
212 * keeping -> [ 1 ][ 0 ][ 0 ]
214 * We reap old sample buffers each time we finish processing an OA
215 * query by iterating the sample_buffers list from the head until we
216 * find a referenced node and stop.
218 * Reaped buffers move to a perfquery.free_sample_buffers list and
219 * when we come to read() we first look to recycle a buffer from the
220 * free_sample_buffers list before allocating a new buffer.
222 struct brw_oa_sample_buf
{
223 struct exec_node link
;
226 uint8_t buf
[I915_PERF_OA_SAMPLE_SIZE
* 10];
227 uint32_t last_timestamp
;
231 * i965 representation of a performance query object.
233 * NB: We want to keep this structure relatively lean considering that
234 * applications may expect to allocate enough objects to be able to
235 * query around all draw calls in a frame.
237 struct brw_perf_query_object
239 struct gl_perf_query_object base
;
241 const struct brw_perf_query_info
*query
;
243 /* See query->kind to know which state below is in use... */
248 * BO containing OA counter snapshots at query Begin/End time.
253 * Address of mapped of @bo
258 * The MI_REPORT_PERF_COUNT command lets us specify a unique
259 * ID that will be reflected in the resulting OA report
260 * that's written by the GPU. This is the ID we're expecting
261 * in the begin report and the the end report should be
262 * @begin_report_id + 1.
267 * Reference the head of the brw->perfquery.sample_buffers
268 * list at the time that the query started (so we only need
269 * to look at nodes after this point when looking for samples
270 * related to this query)
272 * (See struct brw_oa_sample_buf description for more details)
274 struct exec_node
*samples_head
;
277 * Storage for the final accumulated OA counters.
279 uint64_t accumulator
[MAX_OA_REPORT_COUNTERS
];
282 * false while in the unaccumulated_elements list, and set to
283 * true when the final, end MI_RPC snapshot has been
286 bool results_accumulated
;
292 * BO containing starting and ending snapshots for the
293 * statistics counters.
300 /** Downcasting convenience macro. */
301 static inline struct brw_perf_query_object
*
302 brw_perf_query(struct gl_perf_query_object
*o
)
304 return (struct brw_perf_query_object
*) o
;
307 #define STATS_BO_SIZE 4096
308 #define STATS_BO_END_OFFSET_BYTES (STATS_BO_SIZE / 2)
309 #define MAX_STAT_COUNTERS (STATS_BO_END_OFFSET_BYTES / 8)
311 #define MI_RPC_BO_SIZE 4096
312 #define MI_RPC_BO_END_OFFSET_BYTES (MI_RPC_BO_SIZE / 2)
314 /******************************************************************************/
317 brw_is_perf_query_ready(struct gl_context
*ctx
,
318 struct gl_perf_query_object
*o
);
321 dump_perf_query_callback(GLuint id
, void *query_void
, void *brw_void
)
323 struct gl_context
*ctx
= brw_void
;
324 struct gl_perf_query_object
*o
= query_void
;
325 struct brw_perf_query_object
*obj
= query_void
;
327 switch (obj
->query
->kind
) {
329 DBG("%4d: %-6s %-8s BO: %-4s OA data: %-10s %-15s\n",
331 o
->Used
? "Dirty," : "New,",
332 o
->Active
? "Active," : (o
->Ready
? "Ready," : "Pending,"),
333 obj
->oa
.bo
? "yes," : "no,",
334 brw_is_perf_query_ready(ctx
, o
) ? "ready," : "not ready,",
335 obj
->oa
.results_accumulated
? "accumulated" : "not accumulated");
338 DBG("%4d: %-6s %-8s BO: %-4s\n",
340 o
->Used
? "Dirty," : "New,",
341 o
->Active
? "Active," : (o
->Ready
? "Ready," : "Pending,"),
342 obj
->pipeline_stats
.bo
? "yes" : "no");
348 dump_perf_queries(struct brw_context
*brw
)
350 struct gl_context
*ctx
= &brw
->ctx
;
351 DBG("Queries: (Open queries = %d, OA users = %d)\n",
352 brw
->perfquery
.n_active_oa_queries
, brw
->perfquery
.n_oa_users
);
353 _mesa_HashWalk(ctx
->PerfQuery
.Objects
, dump_perf_query_callback
, brw
);
356 /******************************************************************************/
358 static struct brw_oa_sample_buf
*
359 get_free_sample_buf(struct brw_context
*brw
)
361 struct exec_node
*node
= exec_list_pop_head(&brw
->perfquery
.free_sample_buffers
);
362 struct brw_oa_sample_buf
*buf
;
365 buf
= exec_node_data(struct brw_oa_sample_buf
, node
, link
);
367 buf
= ralloc_size(brw
, sizeof(*buf
));
369 exec_node_init(&buf
->link
);
378 reap_old_sample_buffers(struct brw_context
*brw
)
380 struct exec_node
*tail_node
=
381 exec_list_get_tail(&brw
->perfquery
.sample_buffers
);
382 struct brw_oa_sample_buf
*tail_buf
=
383 exec_node_data(struct brw_oa_sample_buf
, tail_node
, link
);
385 /* Remove all old, unreferenced sample buffers walking forward from
386 * the head of the list, except always leave at least one node in
387 * the list so we always have a node to reference when we Begin
390 foreach_list_typed_safe(struct brw_oa_sample_buf
, buf
, link
,
391 &brw
->perfquery
.sample_buffers
)
393 if (buf
->refcount
== 0 && buf
!= tail_buf
) {
394 exec_node_remove(&buf
->link
);
395 exec_list_push_head(&brw
->perfquery
.free_sample_buffers
, &buf
->link
);
402 free_sample_bufs(struct brw_context
*brw
)
404 foreach_list_typed_safe(struct brw_oa_sample_buf
, buf
, link
,
405 &brw
->perfquery
.free_sample_buffers
)
408 exec_list_make_empty(&brw
->perfquery
.free_sample_buffers
);
411 /******************************************************************************/
414 * Driver hook for glGetPerfQueryInfoINTEL().
417 brw_get_perf_query_info(struct gl_context
*ctx
,
418 unsigned query_index
,
424 struct brw_context
*brw
= brw_context(ctx
);
425 const struct brw_perf_query_info
*query
=
426 &brw
->perfquery
.queries
[query_index
];
429 *data_size
= query
->data_size
;
430 *n_counters
= query
->n_counters
;
432 switch (query
->kind
) {
434 *n_active
= brw
->perfquery
.n_active_oa_queries
;
438 *n_active
= brw
->perfquery
.n_active_pipeline_stats_queries
;
444 * Driver hook for glGetPerfCounterInfoINTEL().
447 brw_get_perf_counter_info(struct gl_context
*ctx
,
448 unsigned query_index
,
449 unsigned counter_index
,
455 GLuint
*data_type_enum
,
458 struct brw_context
*brw
= brw_context(ctx
);
459 const struct brw_perf_query_info
*query
=
460 &brw
->perfquery
.queries
[query_index
];
461 const struct brw_perf_query_counter
*counter
=
462 &query
->counters
[counter_index
];
464 *name
= counter
->name
;
465 *desc
= counter
->desc
;
466 *offset
= counter
->offset
;
467 *data_size
= counter
->size
;
468 *type_enum
= counter
->type
;
469 *data_type_enum
= counter
->data_type
;
470 *raw_max
= counter
->raw_max
;
473 /******************************************************************************/
476 * Emit MI_STORE_REGISTER_MEM commands to capture all of the
477 * pipeline statistics for the performance query object.
480 snapshot_statistics_registers(struct brw_context
*brw
,
481 struct brw_perf_query_object
*obj
,
482 uint32_t offset_in_bytes
)
484 const struct brw_perf_query_info
*query
= obj
->query
;
485 const int n_counters
= query
->n_counters
;
487 for (int i
= 0; i
< n_counters
; i
++) {
488 const struct brw_perf_query_counter
*counter
= &query
->counters
[i
];
490 assert(counter
->data_type
== GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL
);
492 brw_store_register_mem64(brw
, obj
->pipeline_stats
.bo
,
493 counter
->pipeline_stat
.reg
,
494 offset_in_bytes
+ i
* sizeof(uint64_t));
499 * Add a query to the global list of "unaccumulated queries."
501 * Queries are tracked here until all the associated OA reports have
502 * been accumulated via accumulate_oa_reports() after the end
503 * MI_REPORT_PERF_COUNT has landed in query->oa.bo.
506 add_to_unaccumulated_query_list(struct brw_context
*brw
,
507 struct brw_perf_query_object
*obj
)
509 if (brw
->perfquery
.unaccumulated_elements
>=
510 brw
->perfquery
.unaccumulated_array_size
)
512 brw
->perfquery
.unaccumulated_array_size
*= 1.5;
513 brw
->perfquery
.unaccumulated
=
514 reralloc(brw
, brw
->perfquery
.unaccumulated
,
515 struct brw_perf_query_object
*,
516 brw
->perfquery
.unaccumulated_array_size
);
519 brw
->perfquery
.unaccumulated
[brw
->perfquery
.unaccumulated_elements
++] = obj
;
523 * Remove a query from the global list of unaccumulated queries once
524 * after successfully accumulating the OA reports associated with the
525 * query in accumulate_oa_reports() or when discarding unwanted query
529 drop_from_unaccumulated_query_list(struct brw_context
*brw
,
530 struct brw_perf_query_object
*obj
)
532 for (int i
= 0; i
< brw
->perfquery
.unaccumulated_elements
; i
++) {
533 if (brw
->perfquery
.unaccumulated
[i
] == obj
) {
534 int last_elt
= --brw
->perfquery
.unaccumulated_elements
;
537 brw
->perfquery
.unaccumulated
[i
] = NULL
;
539 brw
->perfquery
.unaccumulated
[i
] =
540 brw
->perfquery
.unaccumulated
[last_elt
];
547 /* Drop our samples_head reference so that associated periodic
548 * sample data buffers can potentially be reaped if they aren't
549 * referenced by any other queries...
552 struct brw_oa_sample_buf
*buf
=
553 exec_node_data(struct brw_oa_sample_buf
, obj
->oa
.samples_head
, link
);
555 assert(buf
->refcount
> 0);
558 obj
->oa
.samples_head
= NULL
;
560 reap_old_sample_buffers(brw
);
564 timebase_scale(struct brw_context
*brw
, uint32_t u32_time_delta
)
566 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
567 uint64_t tmp
= ((uint64_t)u32_time_delta
) * 1000000000ull;
569 return tmp
? tmp
/ devinfo
->timestamp_frequency
: 0;
573 accumulate_uint32(const uint32_t *report0
,
574 const uint32_t *report1
,
575 uint64_t *accumulator
)
577 *accumulator
+= (uint32_t)(*report1
- *report0
);
581 accumulate_uint40(int a_index
,
582 const uint32_t *report0
,
583 const uint32_t *report1
,
584 uint64_t *accumulator
)
586 const uint8_t *high_bytes0
= (uint8_t *)(report0
+ 40);
587 const uint8_t *high_bytes1
= (uint8_t *)(report1
+ 40);
588 uint64_t high0
= (uint64_t)(high_bytes0
[a_index
]) << 32;
589 uint64_t high1
= (uint64_t)(high_bytes1
[a_index
]) << 32;
590 uint64_t value0
= report0
[a_index
+ 4] | high0
;
591 uint64_t value1
= report1
[a_index
+ 4] | high1
;
595 delta
= (1ULL << 40) + value1
- value0
;
597 delta
= value1
- value0
;
599 *accumulator
+= delta
;
603 * Given pointers to starting and ending OA snapshots, add the deltas for each
604 * counter to the results.
607 add_deltas(struct brw_context
*brw
,
608 struct brw_perf_query_object
*obj
,
609 const uint32_t *start
,
612 const struct brw_perf_query_info
*query
= obj
->query
;
613 uint64_t *accumulator
= obj
->oa
.accumulator
;
617 switch (query
->oa_format
) {
618 case I915_OA_FORMAT_A32u40_A4u32_B8_C8
:
619 accumulate_uint32(start
+ 1, end
+ 1, accumulator
+ idx
++); /* timestamp */
620 accumulate_uint32(start
+ 3, end
+ 3, accumulator
+ idx
++); /* clock */
622 /* 32x 40bit A counters... */
623 for (i
= 0; i
< 32; i
++)
624 accumulate_uint40(i
, start
, end
, accumulator
+ idx
++);
626 /* 4x 32bit A counters... */
627 for (i
= 0; i
< 4; i
++)
628 accumulate_uint32(start
+ 36 + i
, end
+ 36 + i
, accumulator
+ idx
++);
630 /* 8x 32bit B counters + 8x 32bit C counters... */
631 for (i
= 0; i
< 16; i
++)
632 accumulate_uint32(start
+ 48 + i
, end
+ 48 + i
, accumulator
+ idx
++);
635 case I915_OA_FORMAT_A45_B8_C8
:
636 accumulate_uint32(start
+ 1, end
+ 1, accumulator
); /* timestamp */
638 for (i
= 0; i
< 61; i
++)
639 accumulate_uint32(start
+ 3 + i
, end
+ 3 + i
, accumulator
+ 1 + i
);
643 unreachable("Can't accumulate OA counters in unknown format");
648 inc_n_oa_users(struct brw_context
*brw
)
650 if (brw
->perfquery
.n_oa_users
== 0 &&
651 drmIoctl(brw
->perfquery
.oa_stream_fd
,
652 I915_PERF_IOCTL_ENABLE
, 0) < 0)
656 ++brw
->perfquery
.n_oa_users
;
662 dec_n_oa_users(struct brw_context
*brw
)
664 /* Disabling the i915 perf stream will effectively disable the OA
665 * counters. Note it's important to be sure there are no outstanding
666 * MI_RPC commands at this point since they could stall the CS
667 * indefinitely once OACONTROL is disabled.
669 --brw
->perfquery
.n_oa_users
;
670 if (brw
->perfquery
.n_oa_users
== 0 &&
671 drmIoctl(brw
->perfquery
.oa_stream_fd
, I915_PERF_IOCTL_DISABLE
, 0) < 0)
673 DBG("WARNING: Error disabling i915 perf stream: %m\n");
677 /* In general if we see anything spurious while accumulating results,
678 * we don't try and continue accumulating the current query, hoping
679 * for the best, we scrap anything outstanding, and then hope for the
680 * best with new queries.
683 discard_all_queries(struct brw_context
*brw
)
685 while (brw
->perfquery
.unaccumulated_elements
) {
686 struct brw_perf_query_object
*obj
= brw
->perfquery
.unaccumulated
[0];
688 obj
->oa
.results_accumulated
= true;
689 drop_from_unaccumulated_query_list(brw
, brw
->perfquery
.unaccumulated
[0]);
696 OA_READ_STATUS_ERROR
,
697 OA_READ_STATUS_UNFINISHED
,
698 OA_READ_STATUS_FINISHED
,
701 static enum OaReadStatus
702 read_oa_samples_until(struct brw_context
*brw
,
703 uint32_t start_timestamp
,
704 uint32_t end_timestamp
)
706 struct exec_node
*tail_node
=
707 exec_list_get_tail(&brw
->perfquery
.sample_buffers
);
708 struct brw_oa_sample_buf
*tail_buf
=
709 exec_node_data(struct brw_oa_sample_buf
, tail_node
, link
);
710 uint32_t last_timestamp
= tail_buf
->last_timestamp
;
713 struct brw_oa_sample_buf
*buf
= get_free_sample_buf(brw
);
717 while ((len
= read(brw
->perfquery
.oa_stream_fd
, buf
->buf
,
718 sizeof(buf
->buf
))) < 0 && errno
== EINTR
)
722 exec_list_push_tail(&brw
->perfquery
.free_sample_buffers
, &buf
->link
);
726 return ((last_timestamp
- start_timestamp
) >=
727 (end_timestamp
- start_timestamp
)) ?
728 OA_READ_STATUS_FINISHED
:
729 OA_READ_STATUS_UNFINISHED
;
731 DBG("Error reading i915 perf samples: %m\n");
734 DBG("Spurious EOF reading i915 perf samples\n");
736 return OA_READ_STATUS_ERROR
;
740 exec_list_push_tail(&brw
->perfquery
.sample_buffers
, &buf
->link
);
742 /* Go through the reports and update the last timestamp. */
744 while (offset
< buf
->len
) {
745 const struct drm_i915_perf_record_header
*header
=
746 (const struct drm_i915_perf_record_header
*) &buf
->buf
[offset
];
747 uint32_t *report
= (uint32_t *) (header
+ 1);
749 if (header
->type
== DRM_I915_PERF_RECORD_SAMPLE
)
750 last_timestamp
= report
[1];
752 offset
+= header
->size
;
755 buf
->last_timestamp
= last_timestamp
;
758 unreachable("not reached");
759 return OA_READ_STATUS_ERROR
;
763 * Try to read all the reports until either the delimiting timestamp
764 * or an error arises.
767 read_oa_samples_for_query(struct brw_context
*brw
,
768 struct brw_perf_query_object
*obj
)
774 /* We need the MI_REPORT_PERF_COUNT to land before we can start
776 assert(!brw_batch_references(&brw
->batch
, obj
->oa
.bo
) &&
777 !brw_bo_busy(obj
->oa
.bo
));
779 /* Map the BO once here and let accumulate_oa_reports() unmap
781 if (obj
->oa
.map
== NULL
)
782 obj
->oa
.map
= brw_bo_map(brw
, obj
->oa
.bo
, MAP_READ
);
784 start
= last
= obj
->oa
.map
;
785 end
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
787 if (start
[0] != obj
->oa
.begin_report_id
) {
788 DBG("Spurious start report id=%"PRIu32
"\n", start
[0]);
791 if (end
[0] != (obj
->oa
.begin_report_id
+ 1)) {
792 DBG("Spurious end report id=%"PRIu32
"\n", end
[0]);
796 /* Read the reports until the end timestamp. */
797 switch (read_oa_samples_until(brw
, start
[1], end
[1])) {
798 case OA_READ_STATUS_ERROR
:
799 /* Fallthrough and let accumulate_oa_reports() deal with the
801 case OA_READ_STATUS_FINISHED
:
803 case OA_READ_STATUS_UNFINISHED
:
807 unreachable("invalid read status");
812 * Accumulate raw OA counter values based on deltas between pairs of
815 * Accumulation starts from the first report captured via
816 * MI_REPORT_PERF_COUNT (MI_RPC) by brw_begin_perf_query() until the
817 * last MI_RPC report requested by brw_end_perf_query(). Between these
818 * two reports there may also some number of periodically sampled OA
819 * reports collected via the i915 perf interface - depending on the
820 * duration of the query.
822 * These periodic snapshots help to ensure we handle counter overflow
823 * correctly by being frequent enough to ensure we don't miss multiple
824 * overflows of a counter between snapshots. For Gen8+ the i915 perf
825 * snapshots provide the extra context-switch reports that let us
826 * subtract out the progress of counters associated with other
827 * contexts running on the system.
830 accumulate_oa_reports(struct brw_context
*brw
,
831 struct brw_perf_query_object
*obj
)
833 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
834 struct gl_perf_query_object
*o
= &obj
->base
;
838 struct exec_node
*first_samples_node
;
841 int out_duration
= 0;
844 assert(obj
->oa
.map
!= NULL
);
846 start
= last
= obj
->oa
.map
;
847 end
= obj
->oa
.map
+ MI_RPC_BO_END_OFFSET_BYTES
;
849 if (start
[0] != obj
->oa
.begin_report_id
) {
850 DBG("Spurious start report id=%"PRIu32
"\n", start
[0]);
853 if (end
[0] != (obj
->oa
.begin_report_id
+ 1)) {
854 DBG("Spurious end report id=%"PRIu32
"\n", end
[0]);
860 /* See if we have any periodic reports to accumulate too... */
862 /* N.B. The oa.samples_head was set when the query began and
863 * pointed to the tail of the brw->perfquery.sample_buffers list at
864 * the time the query started. Since the buffer existed before the
865 * first MI_REPORT_PERF_COUNT command was emitted we therefore know
866 * that no data in this particular node's buffer can possibly be
867 * associated with the query - so skip ahead one...
869 first_samples_node
= obj
->oa
.samples_head
->next
;
871 foreach_list_typed_from(struct brw_oa_sample_buf
, buf
, link
,
872 &brw
->perfquery
.sample_buffers
,
877 while (offset
< buf
->len
) {
878 const struct drm_i915_perf_record_header
*header
=
879 (const struct drm_i915_perf_record_header
*)(buf
->buf
+ offset
);
881 assert(header
->size
!= 0);
882 assert(header
->size
<= buf
->len
);
884 offset
+= header
->size
;
886 switch (header
->type
) {
887 case DRM_I915_PERF_RECORD_SAMPLE
: {
888 uint32_t *report
= (uint32_t *)(header
+ 1);
891 /* Ignore reports that come before the start marker.
892 * (Note: takes care to allow overflow of 32bit timestamps)
894 if (timebase_scale(brw
, report
[1] - start
[1]) > 5000000000)
897 /* Ignore reports that come after the end marker.
898 * (Note: takes care to allow overflow of 32bit timestamps)
900 if (timebase_scale(brw
, report
[1] - end
[1]) <= 5000000000)
903 /* For Gen8+ since the counters continue while other
904 * contexts are running we need to discount any unrelated
905 * deltas. The hardware automatically generates a report
906 * on context switch which gives us a new reference point
907 * to continuing adding deltas from.
909 * For Haswell we can rely on the HW to stop the progress
910 * of OA counters while any other context is acctive.
912 if (devinfo
->gen
>= 8) {
913 if (in_ctx
&& report
[2] != ctx_id
) {
914 DBG("i915 perf: Switch AWAY (observed by ID change)\n");
917 } else if (in_ctx
== false && report
[2] == ctx_id
) {
918 DBG("i915 perf: Switch TO\n");
921 /* From experimentation in IGT, we found that the OA unit
922 * might label some report as "idle" (using an invalid
923 * context ID), right after a report for a given context.
924 * Deltas generated by those reports actually belong to the
925 * previous context, even though they're not labelled as
928 * We didn't *really* Switch AWAY in the case that we e.g.
929 * saw a single periodic report while idle...
931 if (out_duration
>= 1)
934 assert(report
[2] == ctx_id
);
935 DBG("i915 perf: Continuation IN\n");
937 assert(report
[2] != ctx_id
);
938 DBG("i915 perf: Continuation OUT\n");
945 add_deltas(brw
, obj
, last
, report
);
952 case DRM_I915_PERF_RECORD_OA_BUFFER_LOST
:
953 DBG("i915 perf: OA error: all reports lost\n");
955 case DRM_I915_PERF_RECORD_OA_REPORT_LOST
:
956 DBG("i915 perf: OA report lost\n");
964 add_deltas(brw
, obj
, last
, end
);
966 DBG("Marking %d accumulated - results gathered\n", o
->Id
);
968 brw_bo_unmap(obj
->oa
.bo
);
970 obj
->oa
.results_accumulated
= true;
971 drop_from_unaccumulated_query_list(brw
, obj
);
978 brw_bo_unmap(obj
->oa
.bo
);
980 discard_all_queries(brw
);
983 /******************************************************************************/
986 open_i915_perf_oa_stream(struct brw_context
*brw
,
993 uint64_t properties
[] = {
994 /* Single context sampling */
995 DRM_I915_PERF_PROP_CTX_HANDLE
, ctx_id
,
997 /* Include OA reports in samples */
998 DRM_I915_PERF_PROP_SAMPLE_OA
, true,
1000 /* OA unit configuration */
1001 DRM_I915_PERF_PROP_OA_METRICS_SET
, metrics_set_id
,
1002 DRM_I915_PERF_PROP_OA_FORMAT
, report_format
,
1003 DRM_I915_PERF_PROP_OA_EXPONENT
, period_exponent
,
1005 struct drm_i915_perf_open_param param
= {
1006 .flags
= I915_PERF_FLAG_FD_CLOEXEC
|
1007 I915_PERF_FLAG_FD_NONBLOCK
|
1008 I915_PERF_FLAG_DISABLED
,
1009 .num_properties
= ARRAY_SIZE(properties
) / 2,
1010 .properties_ptr
= (uintptr_t) properties
,
1012 int fd
= drmIoctl(drm_fd
, DRM_IOCTL_I915_PERF_OPEN
, ¶m
);
1014 DBG("Error opening i915 perf OA stream: %m\n");
1018 brw
->perfquery
.oa_stream_fd
= fd
;
1020 brw
->perfquery
.current_oa_metrics_set_id
= metrics_set_id
;
1021 brw
->perfquery
.current_oa_format
= report_format
;
1027 close_perf(struct brw_context
*brw
)
1029 if (brw
->perfquery
.oa_stream_fd
!= -1) {
1030 close(brw
->perfquery
.oa_stream_fd
);
1031 brw
->perfquery
.oa_stream_fd
= -1;
1036 * Driver hook for glBeginPerfQueryINTEL().
1039 brw_begin_perf_query(struct gl_context
*ctx
,
1040 struct gl_perf_query_object
*o
)
1042 struct brw_context
*brw
= brw_context(ctx
);
1043 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1044 const struct brw_perf_query_info
*query
= obj
->query
;
1046 /* We can assume the frontend hides mistaken attempts to Begin a
1047 * query object multiple times before its End. Similarly if an
1048 * application reuses a query object before results have arrived
1049 * the frontend will wait for prior results so we don't need
1050 * to support abandoning in-flight results.
1053 assert(!o
->Used
|| o
->Ready
); /* no in-flight query to worry about */
1055 DBG("Begin(%d)\n", o
->Id
);
1057 /* XXX: We have to consider that the command parser unit that parses batch
1058 * buffer commands and is used to capture begin/end counter snapshots isn't
1059 * implicitly synchronized with what's currently running across other GPU
1060 * units (such as the EUs running shaders) that the performance counters are
1063 * The intention of performance queries is to measure the work associated
1064 * with commands between the begin/end delimiters and so for that to be the
1065 * case we need to explicitly synchronize the parsing of commands to capture
1066 * Begin/End counter snapshots with what's running across other parts of the
1069 * When the command parser reaches a Begin marker it effectively needs to
1070 * drain everything currently running on the GPU until the hardware is idle
1071 * before capturing the first snapshot of counters - otherwise the results
1072 * would also be measuring the effects of earlier commands.
1074 * When the command parser reaches an End marker it needs to stall until
1075 * everything currently running on the GPU has finished before capturing the
1076 * end snapshot - otherwise the results won't be a complete representation
1079 * Theoretically there could be opportunities to minimize how much of the
1080 * GPU pipeline is drained, or that we stall for, when we know what specific
1081 * units the performance counters being queried relate to but we don't
1082 * currently attempt to be clever here.
1084 * Note: with our current simple approach here then for back-to-back queries
1085 * we will redundantly emit duplicate commands to synchronize the command
1086 * streamer with the rest of the GPU pipeline, but we assume that in HW the
1087 * second synchronization is effectively a NOOP.
1089 * N.B. The final results are based on deltas of counters between (inside)
1090 * Begin/End markers so even though the total wall clock time of the
1091 * workload is stretched by larger pipeline bubbles the bubbles themselves
1092 * are generally invisible to the query results. Whether that's a good or a
1093 * bad thing depends on the use case. For a lower real-time impact while
1094 * capturing metrics then periodic sampling may be a better choice than
1095 * INTEL_performance_query.
1098 * This is our Begin synchronization point to drain current work on the
1099 * GPU before we capture our first counter snapshot...
1101 brw_emit_mi_flush(brw
);
1103 switch (query
->kind
) {
1106 /* Opening an i915 perf stream implies exclusive access to the OA unit
1107 * which will generate counter reports for a specific counter set with a
1108 * specific layout/format so we can't begin any OA based queries that
1109 * require a different counter set or format unless we get an opportunity
1110 * to close the stream and open a new one...
1112 if (brw
->perfquery
.oa_stream_fd
!= -1 &&
1113 brw
->perfquery
.current_oa_metrics_set_id
!=
1114 query
->oa_metrics_set_id
) {
1116 if (brw
->perfquery
.n_oa_users
!= 0)
1122 /* If the OA counters aren't already on, enable them. */
1123 if (brw
->perfquery
.oa_stream_fd
== -1) {
1124 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1125 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1127 /* The period_exponent gives a sampling period as follows:
1128 * sample_period = timestamp_period * 2^(period_exponent + 1)
1130 * The timestamps increments every 80ns (HSW), ~52ns (GEN9LP) or
1133 * The counter overflow period is derived from the EuActive counter
1134 * which reads a counter that increments by the number of clock
1135 * cycles multiplied by the number of EUs. It can be calculated as:
1137 * 2^(number of bits in A counter) / (n_eus * max_gen_freq * 2)
1139 * (E.g. 40 EUs @ 1GHz = ~53ms)
1141 * We select a sampling period inferior to that overflow period to
1142 * ensure we cannot see more than 1 counter overflow, otherwise we
1143 * could loose information.
1146 int a_counter_in_bits
= 32;
1147 if (devinfo
->gen
>= 8)
1148 a_counter_in_bits
= 40;
1150 uint64_t overflow_period
= pow(2, a_counter_in_bits
) /
1151 (brw
->perfquery
.sys_vars
.n_eus
*
1152 /* drop 1GHz freq to have units in nanoseconds */
1155 DBG("A counter overflow period: %"PRIu64
"ns, %"PRIu64
"ms (n_eus=%"PRIu64
")\n",
1156 overflow_period
, overflow_period
/ 1000000ul, brw
->perfquery
.sys_vars
.n_eus
);
1158 int period_exponent
= 0;
1159 uint64_t prev_sample_period
, next_sample_period
;
1160 for (int e
= 0; e
< 30; e
++) {
1161 prev_sample_period
= 1000000000ull * pow(2, e
+ 1) / devinfo
->timestamp_frequency
;
1162 next_sample_period
= 1000000000ull * pow(2, e
+ 2) / devinfo
->timestamp_frequency
;
1164 /* Take the previous sampling period, lower than the overflow
1167 if (prev_sample_period
< overflow_period
&&
1168 next_sample_period
> overflow_period
)
1169 period_exponent
= e
+ 1;
1172 if (period_exponent
== 0) {
1173 DBG("WARNING: enable to find a sampling exponent\n");
1177 DBG("OA sampling exponent: %i ~= %"PRIu64
"ms\n", period_exponent
,
1178 prev_sample_period
/ 1000000ul);
1180 if (!open_i915_perf_oa_stream(brw
,
1181 query
->oa_metrics_set_id
,
1184 screen
->fd
, /* drm fd */
1188 assert(brw
->perfquery
.current_oa_metrics_set_id
==
1189 query
->oa_metrics_set_id
&&
1190 brw
->perfquery
.current_oa_format
==
1194 if (!inc_n_oa_users(brw
)) {
1195 DBG("WARNING: Error enabling i915 perf stream: %m\n");
1200 brw_bo_unreference(obj
->oa
.bo
);
1205 brw_bo_alloc(brw
->bufmgr
, "perf. query OA MI_RPC bo",
1206 MI_RPC_BO_SIZE
, 64);
1208 /* Pre-filling the BO helps debug whether writes landed. */
1209 void *map
= brw_bo_map(brw
, obj
->oa
.bo
, MAP_WRITE
);
1210 memset(map
, 0x80, MI_RPC_BO_SIZE
);
1211 brw_bo_unmap(obj
->oa
.bo
);
1214 obj
->oa
.begin_report_id
= brw
->perfquery
.next_query_start_report_id
;
1215 brw
->perfquery
.next_query_start_report_id
+= 2;
1217 /* We flush the batchbuffer here to minimize the chances that MI_RPC
1218 * delimiting commands end up in different batchbuffers. If that's the
1219 * case, the measurement will include the time it takes for the kernel
1220 * scheduler to load a new request into the hardware. This is manifested in
1221 * tools like frameretrace by spikes in the "GPU Core Clocks" counter.
1223 intel_batchbuffer_flush(brw
);
1225 /* Take a starting OA counter snapshot. */
1226 brw
->vtbl
.emit_mi_report_perf_count(brw
, obj
->oa
.bo
, 0,
1227 obj
->oa
.begin_report_id
);
1228 ++brw
->perfquery
.n_active_oa_queries
;
1230 /* No already-buffered samples can possibly be associated with this query
1231 * so create a marker within the list of sample buffers enabling us to
1232 * easily ignore earlier samples when processing this query after
1235 assert(!exec_list_is_empty(&brw
->perfquery
.sample_buffers
));
1236 obj
->oa
.samples_head
= exec_list_get_tail(&brw
->perfquery
.sample_buffers
);
1238 struct brw_oa_sample_buf
*buf
=
1239 exec_node_data(struct brw_oa_sample_buf
, obj
->oa
.samples_head
, link
);
1241 /* This reference will ensure that future/following sample
1242 * buffers (that may relate to this query) can't be freed until
1243 * this drops to zero.
1247 memset(obj
->oa
.accumulator
, 0, sizeof(obj
->oa
.accumulator
));
1248 obj
->oa
.results_accumulated
= false;
1250 add_to_unaccumulated_query_list(brw
, obj
);
1253 case PIPELINE_STATS
:
1254 if (obj
->pipeline_stats
.bo
) {
1255 brw_bo_unreference(obj
->pipeline_stats
.bo
);
1256 obj
->pipeline_stats
.bo
= NULL
;
1259 obj
->pipeline_stats
.bo
=
1260 brw_bo_alloc(brw
->bufmgr
, "perf. query pipeline stats bo",
1263 /* Take starting snapshots. */
1264 snapshot_statistics_registers(brw
, obj
, 0);
1266 ++brw
->perfquery
.n_active_pipeline_stats_queries
;
1270 if (INTEL_DEBUG
& DEBUG_PERFMON
)
1271 dump_perf_queries(brw
);
1277 * Driver hook for glEndPerfQueryINTEL().
1280 brw_end_perf_query(struct gl_context
*ctx
,
1281 struct gl_perf_query_object
*o
)
1283 struct brw_context
*brw
= brw_context(ctx
);
1284 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1286 DBG("End(%d)\n", o
->Id
);
1288 /* Ensure that the work associated with the queried commands will have
1289 * finished before taking our query end counter readings.
1291 * For more details see comment in brw_begin_perf_query for
1292 * corresponding flush.
1294 brw_emit_mi_flush(brw
);
1296 switch (obj
->query
->kind
) {
1299 /* NB: It's possible that the query will have already been marked
1300 * as 'accumulated' if an error was seen while reading samples
1301 * from perf. In this case we mustn't try and emit a closing
1302 * MI_RPC command in case the OA unit has already been disabled
1304 if (!obj
->oa
.results_accumulated
) {
1305 /* Take an ending OA counter snapshot. */
1306 brw
->vtbl
.emit_mi_report_perf_count(brw
, obj
->oa
.bo
,
1307 MI_RPC_BO_END_OFFSET_BYTES
,
1308 obj
->oa
.begin_report_id
+ 1);
1311 --brw
->perfquery
.n_active_oa_queries
;
1313 /* NB: even though the query has now ended, it can't be accumulated
1314 * until the end MI_REPORT_PERF_COUNT snapshot has been written
1319 case PIPELINE_STATS
:
1320 snapshot_statistics_registers(brw
, obj
,
1321 STATS_BO_END_OFFSET_BYTES
);
1322 --brw
->perfquery
.n_active_pipeline_stats_queries
;
1328 brw_wait_perf_query(struct gl_context
*ctx
, struct gl_perf_query_object
*o
)
1330 struct brw_context
*brw
= brw_context(ctx
);
1331 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1332 struct brw_bo
*bo
= NULL
;
1336 switch (obj
->query
->kind
) {
1341 case PIPELINE_STATS
:
1342 bo
= obj
->pipeline_stats
.bo
;
1349 /* If the current batch references our results bo then we need to
1352 if (brw_batch_references(&brw
->batch
, bo
))
1353 intel_batchbuffer_flush(brw
);
1355 brw_bo_wait_rendering(bo
);
1357 /* Due to a race condition between the OA unit signaling report
1358 * availability and the report actually being written into memory,
1359 * we need to wait for all the reports to come in before we can
1362 if (obj
->query
->kind
== OA_COUNTERS
) {
1363 while (!read_oa_samples_for_query(brw
, obj
))
1369 brw_is_perf_query_ready(struct gl_context
*ctx
,
1370 struct gl_perf_query_object
*o
)
1372 struct brw_context
*brw
= brw_context(ctx
);
1373 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1378 switch (obj
->query
->kind
) {
1380 return (obj
->oa
.results_accumulated
||
1382 !brw_batch_references(&brw
->batch
, obj
->oa
.bo
) &&
1383 !brw_bo_busy(obj
->oa
.bo
) &&
1384 read_oa_samples_for_query(brw
, obj
)));
1385 case PIPELINE_STATS
:
1386 return (obj
->pipeline_stats
.bo
&&
1387 !brw_batch_references(&brw
->batch
, obj
->pipeline_stats
.bo
) &&
1388 !brw_bo_busy(obj
->pipeline_stats
.bo
));
1391 unreachable("missing ready check for unknown query kind");
1396 get_oa_counter_data(struct brw_context
*brw
,
1397 struct brw_perf_query_object
*obj
,
1401 const struct brw_perf_query_info
*query
= obj
->query
;
1402 int n_counters
= query
->n_counters
;
1405 if (!obj
->oa
.results_accumulated
) {
1406 accumulate_oa_reports(brw
, obj
);
1407 assert(obj
->oa
.results_accumulated
);
1410 for (int i
= 0; i
< n_counters
; i
++) {
1411 const struct brw_perf_query_counter
*counter
= &query
->counters
[i
];
1412 uint64_t *out_uint64
;
1415 if (counter
->size
) {
1416 switch (counter
->data_type
) {
1417 case GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL
:
1418 out_uint64
= (uint64_t *)(data
+ counter
->offset
);
1419 *out_uint64
= counter
->oa_counter_read_uint64(brw
, query
,
1420 obj
->oa
.accumulator
);
1422 case GL_PERFQUERY_COUNTER_DATA_FLOAT_INTEL
:
1423 out_float
= (float *)(data
+ counter
->offset
);
1424 *out_float
= counter
->oa_counter_read_float(brw
, query
,
1425 obj
->oa
.accumulator
);
1428 /* So far we aren't using uint32, double or bool32... */
1429 unreachable("unexpected counter data type");
1431 written
= counter
->offset
+ counter
->size
;
1439 get_pipeline_stats_data(struct brw_context
*brw
,
1440 struct brw_perf_query_object
*obj
,
1445 const struct brw_perf_query_info
*query
= obj
->query
;
1446 int n_counters
= obj
->query
->n_counters
;
1449 uint64_t *start
= brw_bo_map(brw
, obj
->pipeline_stats
.bo
, MAP_READ
);
1450 uint64_t *end
= start
+ (STATS_BO_END_OFFSET_BYTES
/ sizeof(uint64_t));
1452 for (int i
= 0; i
< n_counters
; i
++) {
1453 const struct brw_perf_query_counter
*counter
= &query
->counters
[i
];
1454 uint64_t value
= end
[i
] - start
[i
];
1456 if (counter
->pipeline_stat
.numerator
!=
1457 counter
->pipeline_stat
.denominator
) {
1458 value
*= counter
->pipeline_stat
.numerator
;
1459 value
/= counter
->pipeline_stat
.denominator
;
1462 *((uint64_t *)p
) = value
;
1466 brw_bo_unmap(obj
->pipeline_stats
.bo
);
1472 * Driver hook for glGetPerfQueryDataINTEL().
1475 brw_get_perf_query_data(struct gl_context
*ctx
,
1476 struct gl_perf_query_object
*o
,
1479 GLuint
*bytes_written
)
1481 struct brw_context
*brw
= brw_context(ctx
);
1482 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1485 assert(brw_is_perf_query_ready(ctx
, o
));
1487 DBG("GetData(%d)\n", o
->Id
);
1489 if (INTEL_DEBUG
& DEBUG_PERFMON
)
1490 dump_perf_queries(brw
);
1492 /* We expect that the frontend only calls this hook when it knows
1493 * that results are available.
1497 switch (obj
->query
->kind
) {
1499 written
= get_oa_counter_data(brw
, obj
, data_size
, (uint8_t *)data
);
1502 case PIPELINE_STATS
:
1503 written
= get_pipeline_stats_data(brw
, obj
, data_size
, (uint8_t *)data
);
1508 *bytes_written
= written
;
1511 static struct gl_perf_query_object
*
1512 brw_new_perf_query_object(struct gl_context
*ctx
, unsigned query_index
)
1514 struct brw_context
*brw
= brw_context(ctx
);
1515 const struct brw_perf_query_info
*query
=
1516 &brw
->perfquery
.queries
[query_index
];
1517 struct brw_perf_query_object
*obj
=
1518 calloc(1, sizeof(struct brw_perf_query_object
));
1525 brw
->perfquery
.n_query_instances
++;
1531 * Driver hook for glDeletePerfQueryINTEL().
1534 brw_delete_perf_query(struct gl_context
*ctx
,
1535 struct gl_perf_query_object
*o
)
1537 struct brw_context
*brw
= brw_context(ctx
);
1538 struct brw_perf_query_object
*obj
= brw_perf_query(o
);
1540 /* We can assume that the frontend waits for a query to complete
1541 * before ever calling into here, so we don't have to worry about
1542 * deleting an in-flight query object.
1545 assert(!o
->Used
|| o
->Ready
);
1547 DBG("Delete(%d)\n", o
->Id
);
1549 switch (obj
->query
->kind
) {
1552 if (!obj
->oa
.results_accumulated
) {
1553 drop_from_unaccumulated_query_list(brw
, obj
);
1554 dec_n_oa_users(brw
);
1557 brw_bo_unreference(obj
->oa
.bo
);
1561 obj
->oa
.results_accumulated
= false;
1564 case PIPELINE_STATS
:
1565 if (obj
->pipeline_stats
.bo
) {
1566 brw_bo_unreference(obj
->pipeline_stats
.bo
);
1567 obj
->pipeline_stats
.bo
= NULL
;
1574 /* As an indication that the INTEL_performance_query extension is no
1575 * longer in use, it's a good time to free our cache of sample
1576 * buffers and close any current i915-perf stream.
1578 if (--brw
->perfquery
.n_query_instances
== 0) {
1579 free_sample_bufs(brw
);
1584 /******************************************************************************/
1586 static struct brw_perf_query_info
*
1587 append_query_info(struct brw_context
*brw
)
1589 brw
->perfquery
.queries
=
1590 reralloc(brw
, brw
->perfquery
.queries
,
1591 struct brw_perf_query_info
, ++brw
->perfquery
.n_queries
);
1593 return &brw
->perfquery
.queries
[brw
->perfquery
.n_queries
- 1];
1597 add_stat_reg(struct brw_perf_query_info
*query
,
1600 uint32_t denominator
,
1602 const char *description
)
1604 struct brw_perf_query_counter
*counter
;
1606 assert(query
->n_counters
< MAX_STAT_COUNTERS
);
1608 counter
= &query
->counters
[query
->n_counters
];
1609 counter
->name
= name
;
1610 counter
->desc
= description
;
1611 counter
->type
= GL_PERFQUERY_COUNTER_RAW_INTEL
;
1612 counter
->data_type
= GL_PERFQUERY_COUNTER_DATA_UINT64_INTEL
;
1613 counter
->size
= sizeof(uint64_t);
1614 counter
->offset
= sizeof(uint64_t) * query
->n_counters
;
1615 counter
->pipeline_stat
.reg
= reg
;
1616 counter
->pipeline_stat
.numerator
= numerator
;
1617 counter
->pipeline_stat
.denominator
= denominator
;
1619 query
->n_counters
++;
1623 add_basic_stat_reg(struct brw_perf_query_info
*query
,
1624 uint32_t reg
, const char *name
)
1626 add_stat_reg(query
, reg
, 1, 1, name
, name
);
1630 init_pipeline_statistic_query_registers(struct brw_context
*brw
)
1632 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1633 struct brw_perf_query_info
*query
= append_query_info(brw
);
1635 query
->kind
= PIPELINE_STATS
;
1636 query
->name
= "Pipeline Statistics Registers";
1637 query
->n_counters
= 0;
1639 rzalloc_array(brw
, struct brw_perf_query_counter
, MAX_STAT_COUNTERS
);
1641 add_basic_stat_reg(query
, IA_VERTICES_COUNT
,
1642 "N vertices submitted");
1643 add_basic_stat_reg(query
, IA_PRIMITIVES_COUNT
,
1644 "N primitives submitted");
1645 add_basic_stat_reg(query
, VS_INVOCATION_COUNT
,
1646 "N vertex shader invocations");
1648 if (devinfo
->gen
== 6) {
1649 add_stat_reg(query
, GEN6_SO_PRIM_STORAGE_NEEDED
, 1, 1,
1650 "SO_PRIM_STORAGE_NEEDED",
1651 "N geometry shader stream-out primitives (total)");
1652 add_stat_reg(query
, GEN6_SO_NUM_PRIMS_WRITTEN
, 1, 1,
1653 "SO_NUM_PRIMS_WRITTEN",
1654 "N geometry shader stream-out primitives (written)");
1656 add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(0), 1, 1,
1657 "SO_PRIM_STORAGE_NEEDED (Stream 0)",
1658 "N stream-out (stream 0) primitives (total)");
1659 add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(1), 1, 1,
1660 "SO_PRIM_STORAGE_NEEDED (Stream 1)",
1661 "N stream-out (stream 1) primitives (total)");
1662 add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(2), 1, 1,
1663 "SO_PRIM_STORAGE_NEEDED (Stream 2)",
1664 "N stream-out (stream 2) primitives (total)");
1665 add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(3), 1, 1,
1666 "SO_PRIM_STORAGE_NEEDED (Stream 3)",
1667 "N stream-out (stream 3) primitives (total)");
1668 add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(0), 1, 1,
1669 "SO_NUM_PRIMS_WRITTEN (Stream 0)",
1670 "N stream-out (stream 0) primitives (written)");
1671 add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(1), 1, 1,
1672 "SO_NUM_PRIMS_WRITTEN (Stream 1)",
1673 "N stream-out (stream 1) primitives (written)");
1674 add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(2), 1, 1,
1675 "SO_NUM_PRIMS_WRITTEN (Stream 2)",
1676 "N stream-out (stream 2) primitives (written)");
1677 add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(3), 1, 1,
1678 "SO_NUM_PRIMS_WRITTEN (Stream 3)",
1679 "N stream-out (stream 3) primitives (written)");
1682 add_basic_stat_reg(query
, HS_INVOCATION_COUNT
,
1683 "N TCS shader invocations");
1684 add_basic_stat_reg(query
, DS_INVOCATION_COUNT
,
1685 "N TES shader invocations");
1687 add_basic_stat_reg(query
, GS_INVOCATION_COUNT
,
1688 "N geometry shader invocations");
1689 add_basic_stat_reg(query
, GS_PRIMITIVES_COUNT
,
1690 "N geometry shader primitives emitted");
1692 add_basic_stat_reg(query
, CL_INVOCATION_COUNT
,
1693 "N primitives entering clipping");
1694 add_basic_stat_reg(query
, CL_PRIMITIVES_COUNT
,
1695 "N primitives leaving clipping");
1697 if (devinfo
->is_haswell
|| devinfo
->gen
== 8)
1698 add_stat_reg(query
, PS_INVOCATION_COUNT
, 1, 4,
1699 "N fragment shader invocations",
1700 "N fragment shader invocations");
1702 add_basic_stat_reg(query
, PS_INVOCATION_COUNT
,
1703 "N fragment shader invocations");
1705 add_basic_stat_reg(query
, PS_DEPTH_COUNT
, "N z-pass fragments");
1707 if (devinfo
->gen
>= 7)
1708 add_basic_stat_reg(query
, CS_INVOCATION_COUNT
,
1709 "N compute shader invocations");
1711 query
->data_size
= sizeof(uint64_t) * query
->n_counters
;
1715 read_file_uint64(const char *file
, uint64_t *val
)
1723 while ((n
= read(fd
, buf
, sizeof (buf
) - 1)) < 0 &&
1730 *val
= strtoull(buf
, NULL
, 0);
1736 register_oa_config(struct brw_context
*brw
,
1737 const struct brw_perf_query_info
*query
,
1740 struct brw_perf_query_info
*registred_query
= append_query_info(brw
);
1741 *registred_query
= *query
;
1742 registred_query
->oa_metrics_set_id
= config_id
;
1743 DBG("metric set registred: id = %" PRIu64
", guid = %s\n",
1744 registred_query
->oa_metrics_set_id
, query
->guid
);
1748 enumerate_sysfs_metrics(struct brw_context
*brw
, const char *sysfs_dev_dir
)
1751 DIR *metricsdir
= NULL
;
1752 struct dirent
*metric_entry
;
1755 len
= snprintf(buf
, sizeof(buf
), "%s/metrics", sysfs_dev_dir
);
1756 if (len
< 0 || len
>= sizeof(buf
)) {
1757 DBG("Failed to concatenate path to sysfs metrics/ directory\n");
1761 metricsdir
= opendir(buf
);
1763 DBG("Failed to open %s: %m\n", buf
);
1767 while ((metric_entry
= readdir(metricsdir
))) {
1768 struct hash_entry
*entry
;
1770 if ((metric_entry
->d_type
!= DT_DIR
&&
1771 metric_entry
->d_type
!= DT_LNK
) ||
1772 metric_entry
->d_name
[0] == '.')
1775 DBG("metric set: %s\n", metric_entry
->d_name
);
1776 entry
= _mesa_hash_table_search(brw
->perfquery
.oa_metrics_table
,
1777 metric_entry
->d_name
);
1781 len
= snprintf(buf
, sizeof(buf
), "%s/metrics/%s/id",
1782 sysfs_dev_dir
, metric_entry
->d_name
);
1783 if (len
< 0 || len
>= sizeof(buf
)) {
1784 DBG("Failed to concatenate path to sysfs metric id file\n");
1788 if (!read_file_uint64(buf
, &id
)) {
1789 DBG("Failed to read metric set id from %s: %m", buf
);
1793 register_oa_config(brw
, (const struct brw_perf_query_info
*)entry
->data
, id
);
1795 DBG("metric set not known by mesa (skipping)\n");
1798 closedir(metricsdir
);
1802 read_sysfs_drm_device_file_uint64(struct brw_context
*brw
,
1803 const char *sysfs_dev_dir
,
1810 len
= snprintf(buf
, sizeof(buf
), "%s/%s", sysfs_dev_dir
, file
);
1811 if (len
< 0 || len
>= sizeof(buf
)) {
1812 DBG("Failed to concatenate sys filename to read u64 from\n");
1816 return read_file_uint64(buf
, value
);
1820 kernel_has_dynamic_config_support(struct brw_context
*brw
,
1821 const char *sysfs_dev_dir
)
1823 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1824 struct hash_entry
*entry
;
1826 hash_table_foreach(brw
->perfquery
.oa_metrics_table
, entry
) {
1827 struct brw_perf_query_info
*query
= entry
->data
;
1828 char config_path
[256];
1831 snprintf(config_path
, sizeof(config_path
),
1832 "%s/metrics/%s/id", sysfs_dev_dir
, query
->guid
);
1834 /* Look for the test config, which we know we can't replace. */
1835 if (read_file_uint64(config_path
, &config_id
) && config_id
== 1) {
1836 return drmIoctl(screen
->fd
, DRM_IOCTL_I915_PERF_REMOVE_CONFIG
,
1837 &config_id
) < 0 && errno
== ENOENT
;
1845 init_oa_configs(struct brw_context
*brw
, const char *sysfs_dev_dir
)
1847 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1848 struct hash_entry
*entry
;
1850 hash_table_foreach(brw
->perfquery
.oa_metrics_table
, entry
) {
1851 const struct brw_perf_query_info
*query
= entry
->data
;
1852 struct drm_i915_perf_oa_config config
;
1853 char config_path
[256];
1857 snprintf(config_path
, sizeof(config_path
),
1858 "%s/metrics/%s/id", sysfs_dev_dir
, query
->guid
);
1860 /* Don't recreate already loaded configs. */
1861 if (read_file_uint64(config_path
, &config_id
)) {
1862 DBG("metric set: %s (already loaded)\n", query
->guid
);
1863 register_oa_config(brw
, query
, config_id
);
1867 memset(&config
, 0, sizeof(config
));
1869 memcpy(config
.uuid
, query
->guid
, sizeof(config
.uuid
));
1871 config
.n_mux_regs
= query
->n_mux_regs
;
1872 config
.mux_regs_ptr
= (uintptr_t) query
->mux_regs
;
1874 config
.n_boolean_regs
= query
->n_b_counter_regs
;
1875 config
.boolean_regs_ptr
= (uintptr_t) query
->b_counter_regs
;
1877 config
.n_flex_regs
= query
->n_flex_regs
;
1878 config
.flex_regs_ptr
= (uintptr_t) query
->flex_regs
;
1880 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_I915_PERF_ADD_CONFIG
, &config
);
1882 DBG("Failed to load \"%s\" (%s) metrics set in kernel: %s\n",
1883 query
->name
, query
->guid
, strerror(errno
));
1887 register_oa_config(brw
, query
, config_id
);
1888 DBG("metric set: %s (added)\n", query
->guid
);
1893 init_oa_sys_vars(struct brw_context
*brw
, const char *sysfs_dev_dir
)
1895 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
1896 uint64_t min_freq_mhz
= 0, max_freq_mhz
= 0;
1897 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1899 if (!read_sysfs_drm_device_file_uint64(brw
, sysfs_dev_dir
,
1904 if (!read_sysfs_drm_device_file_uint64(brw
, sysfs_dev_dir
,
1909 brw
->perfquery
.sys_vars
.gt_min_freq
= min_freq_mhz
* 1000000;
1910 brw
->perfquery
.sys_vars
.gt_max_freq
= max_freq_mhz
* 1000000;
1911 brw
->perfquery
.sys_vars
.timestamp_frequency
= devinfo
->timestamp_frequency
;
1913 brw
->perfquery
.sys_vars
.revision
= intel_device_get_revision(screen
->fd
);
1914 brw
->perfquery
.sys_vars
.n_eu_slices
= devinfo
->num_slices
;
1915 /* Assuming uniform distribution of subslices per slices. */
1916 brw
->perfquery
.sys_vars
.n_eu_sub_slices
= devinfo
->num_subslices
[0];
1918 if (devinfo
->is_haswell
) {
1919 brw
->perfquery
.sys_vars
.slice_mask
= 0;
1920 brw
->perfquery
.sys_vars
.subslice_mask
= 0;
1922 for (int s
= 0; s
< devinfo
->num_slices
; s
++)
1923 brw
->perfquery
.sys_vars
.slice_mask
|= 1U << s
;
1924 for (int ss
= 0; ss
< devinfo
->num_subslices
[0]; ss
++)
1925 brw
->perfquery
.sys_vars
.subslice_mask
|= 1U << ss
;
1927 if (devinfo
->gt
== 1) {
1928 brw
->perfquery
.sys_vars
.n_eus
= 10;
1929 } else if (devinfo
->gt
== 2) {
1930 brw
->perfquery
.sys_vars
.n_eus
= 20;
1931 } else if (devinfo
->gt
== 3) {
1932 brw
->perfquery
.sys_vars
.n_eus
= 40;
1934 unreachable("not reached");
1936 drm_i915_getparam_t gp
;
1940 /* maximum number of slices */
1941 int s_max
= devinfo
->num_slices
;
1942 /* maximum number of subslices per slice (assuming uniform subslices per
1945 int ss_max
= devinfo
->num_subslices
[0];
1946 uint64_t subslice_mask
= 0;
1949 gp
.param
= I915_PARAM_SLICE_MASK
;
1950 gp
.value
= &slice_mask
;
1951 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
1955 gp
.param
= I915_PARAM_SUBSLICE_MASK
;
1956 gp
.value
= &ss_mask
;
1957 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
1961 brw
->perfquery
.sys_vars
.n_eus
= brw
->screen
->eu_total
;
1962 brw
->perfquery
.sys_vars
.n_eu_slices
= __builtin_popcount(slice_mask
);
1963 brw
->perfquery
.sys_vars
.slice_mask
= slice_mask
;
1965 /* Note: the _SUBSLICE_MASK param only reports a global subslice mask
1966 * which applies to all slices.
1968 * Note: some of the metrics we have (as described in XML) are
1969 * conditional on a $SubsliceMask variable which is expected to also
1970 * reflect the slice mask by packing together subslice masks for each
1971 * slice in one value..
1973 for (s
= 0; s
< s_max
; s
++) {
1974 if (slice_mask
& (1<<s
)) {
1975 subslice_mask
|= ss_mask
<< (ss_max
* s
);
1979 brw
->perfquery
.sys_vars
.subslice_mask
= subslice_mask
;
1980 brw
->perfquery
.sys_vars
.n_eu_sub_slices
=
1981 __builtin_popcount(subslice_mask
);
1984 brw
->perfquery
.sys_vars
.eu_threads_count
=
1985 brw
->perfquery
.sys_vars
.n_eus
* devinfo
->num_thread_per_eu
;
1991 get_sysfs_dev_dir(struct brw_context
*brw
,
1995 __DRIscreen
*screen
= brw
->screen
->driScrnPriv
;
1999 struct dirent
*drm_entry
;
2003 assert(path_buf_len
);
2006 if (fstat(screen
->fd
, &sb
)) {
2007 DBG("Failed to stat DRM fd\n");
2011 maj
= major(sb
.st_rdev
);
2012 min
= minor(sb
.st_rdev
);
2014 if (!S_ISCHR(sb
.st_mode
)) {
2015 DBG("DRM fd is not a character device as expected\n");
2019 len
= snprintf(path_buf
, path_buf_len
,
2020 "/sys/dev/char/%d:%d/device/drm", maj
, min
);
2021 if (len
< 0 || len
>= path_buf_len
) {
2022 DBG("Failed to concatenate sysfs path to drm device\n");
2026 drmdir
= opendir(path_buf
);
2028 DBG("Failed to open %s: %m\n", path_buf
);
2032 while ((drm_entry
= readdir(drmdir
))) {
2033 if ((drm_entry
->d_type
== DT_DIR
||
2034 drm_entry
->d_type
== DT_LNK
) &&
2035 strncmp(drm_entry
->d_name
, "card", 4) == 0)
2037 len
= snprintf(path_buf
, path_buf_len
,
2038 "/sys/dev/char/%d:%d/device/drm/%s",
2039 maj
, min
, drm_entry
->d_name
);
2041 if (len
< 0 || len
>= path_buf_len
)
2050 DBG("Failed to find cardX directory under /sys/dev/char/%d:%d/device/drm\n",
2056 typedef void (*perf_register_oa_queries_t
)(struct brw_context
*);
2058 static perf_register_oa_queries_t
2059 get_register_queries_function(const struct gen_device_info
*devinfo
)
2061 if (devinfo
->is_haswell
)
2062 return brw_oa_register_queries_hsw
;
2063 if (devinfo
->is_cherryview
)
2064 return brw_oa_register_queries_chv
;
2065 if (devinfo
->is_broadwell
)
2066 return brw_oa_register_queries_bdw
;
2067 if (devinfo
->is_broxton
)
2068 return brw_oa_register_queries_bxt
;
2069 if (devinfo
->is_skylake
) {
2070 if (devinfo
->gt
== 2)
2071 return brw_oa_register_queries_sklgt2
;
2072 if (devinfo
->gt
== 3)
2073 return brw_oa_register_queries_sklgt3
;
2074 if (devinfo
->gt
== 4)
2075 return brw_oa_register_queries_sklgt4
;
2077 if (devinfo
->is_kabylake
) {
2078 if (devinfo
->gt
== 2)
2079 return brw_oa_register_queries_kblgt2
;
2080 if (devinfo
->gt
== 3)
2081 return brw_oa_register_queries_kblgt3
;
2083 if (devinfo
->is_geminilake
)
2084 return brw_oa_register_queries_glk
;
2085 if (devinfo
->is_coffeelake
) {
2086 if (devinfo
->gt
== 2)
2087 return brw_oa_register_queries_cflgt2
;
2088 if (devinfo
->gt
== 3)
2089 return brw_oa_register_queries_cflgt3
;
2096 brw_init_perf_query_info(struct gl_context
*ctx
)
2098 struct brw_context
*brw
= brw_context(ctx
);
2099 const struct gen_device_info
*devinfo
= &brw
->screen
->devinfo
;
2100 bool i915_perf_oa_available
= false;
2102 char sysfs_dev_dir
[128];
2103 perf_register_oa_queries_t oa_register
;
2105 if (brw
->perfquery
.n_queries
)
2106 return brw
->perfquery
.n_queries
;
2108 init_pipeline_statistic_query_registers(brw
);
2110 oa_register
= get_register_queries_function(devinfo
);
2112 /* The existence of this sysctl parameter implies the kernel supports
2113 * the i915 perf interface.
2115 if (stat("/proc/sys/dev/i915/perf_stream_paranoid", &sb
) == 0) {
2117 /* If _paranoid == 1 then on Gen8+ we won't be able to access OA
2118 * metrics unless running as root.
2120 if (devinfo
->is_haswell
)
2121 i915_perf_oa_available
= true;
2123 uint64_t paranoid
= 1;
2125 read_file_uint64("/proc/sys/dev/i915/perf_stream_paranoid", ¶noid
);
2127 if (paranoid
== 0 || geteuid() == 0)
2128 i915_perf_oa_available
= true;
2132 if (i915_perf_oa_available
&&
2134 get_sysfs_dev_dir(brw
, sysfs_dev_dir
, sizeof(sysfs_dev_dir
)) &&
2135 init_oa_sys_vars(brw
, sysfs_dev_dir
))
2137 brw
->perfquery
.oa_metrics_table
=
2138 _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
2139 _mesa_key_string_equal
);
2141 /* Index all the metric sets mesa knows about before looking to see what
2142 * the kernel is advertising.
2146 if (likely((INTEL_DEBUG
& DEBUG_NO_OACONFIG
) == 0) &&
2147 kernel_has_dynamic_config_support(brw
, sysfs_dev_dir
))
2148 init_oa_configs(brw
, sysfs_dev_dir
);
2150 enumerate_sysfs_metrics(brw
, sysfs_dev_dir
);
2153 brw
->perfquery
.unaccumulated
=
2154 ralloc_array(brw
, struct brw_perf_query_object
*, 2);
2155 brw
->perfquery
.unaccumulated_elements
= 0;
2156 brw
->perfquery
.unaccumulated_array_size
= 2;
2158 exec_list_make_empty(&brw
->perfquery
.sample_buffers
);
2159 exec_list_make_empty(&brw
->perfquery
.free_sample_buffers
);
2161 /* It's convenient to guarantee that this linked list of sample
2162 * buffers is never empty so we add an empty head so when we
2163 * Begin an OA query we can always take a reference on a buffer
2166 struct brw_oa_sample_buf
*buf
= get_free_sample_buf(brw
);
2167 exec_list_push_head(&brw
->perfquery
.sample_buffers
, &buf
->link
);
2169 brw
->perfquery
.oa_stream_fd
= -1;
2171 brw
->perfquery
.next_query_start_report_id
= 1000;
2173 return brw
->perfquery
.n_queries
;
2177 brw_init_performance_queries(struct brw_context
*brw
)
2179 struct gl_context
*ctx
= &brw
->ctx
;
2181 ctx
->Driver
.InitPerfQueryInfo
= brw_init_perf_query_info
;
2182 ctx
->Driver
.GetPerfQueryInfo
= brw_get_perf_query_info
;
2183 ctx
->Driver
.GetPerfCounterInfo
= brw_get_perf_counter_info
;
2184 ctx
->Driver
.NewPerfQueryObject
= brw_new_perf_query_object
;
2185 ctx
->Driver
.DeletePerfQuery
= brw_delete_perf_query
;
2186 ctx
->Driver
.BeginPerfQuery
= brw_begin_perf_query
;
2187 ctx
->Driver
.EndPerfQuery
= brw_end_perf_query
;
2188 ctx
->Driver
.WaitPerfQuery
= brw_wait_perf_query
;
2189 ctx
->Driver
.IsPerfQueryReady
= brw_is_perf_query_ready
;
2190 ctx
->Driver
.GetPerfQueryData
= brw_get_perf_query_data
;