2 * Copyright © 2018 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 DEALINGS
26 #include <sys/types.h>
32 #ifndef HAVE_DIRENT_D_TYPE
33 #include <limits.h> // PATH_MAX
36 #include <drm-uapi/i915_drm.h>
38 #include "common/gen_gem.h"
40 #include "dev/gen_debug.h"
41 #include "dev/gen_device_info.h"
43 #include "perf/gen_perf.h"
44 #include "perf/gen_perf_regs.h"
45 #include "perf/gen_perf_mdapi.h"
46 #include "perf/gen_perf_metrics.h"
47 #include "perf/gen_perf_private.h"
49 #include "util/bitscan.h"
50 #include "util/macros.h"
51 #include "util/mesa-sha1.h"
52 #include "util/u_math.h"
54 #define FILE_DEBUG_FLAG DEBUG_PERFMON
56 #define OA_REPORT_INVALID_CTX_ID (0xffffffff)
59 is_dir_or_link(const struct dirent
*entry
, const char *parent_dir
)
61 #ifdef HAVE_DIRENT_D_TYPE
62 return entry
->d_type
== DT_DIR
|| entry
->d_type
== DT_LNK
;
65 char path
[PATH_MAX
+ 1];
66 snprintf(path
, sizeof(path
), "%s/%s", parent_dir
, entry
->d_name
);
68 return S_ISDIR(st
.st_mode
) || S_ISLNK(st
.st_mode
);
73 get_sysfs_dev_dir(struct gen_perf_config
*perf
, int fd
)
78 struct dirent
*drm_entry
;
81 perf
->sysfs_dev_dir
[0] = '\0';
83 if (unlikely(INTEL_DEBUG
& DEBUG_NO_OACONFIG
))
87 DBG("Failed to stat DRM fd\n");
91 maj
= major(sb
.st_rdev
);
92 min
= minor(sb
.st_rdev
);
94 if (!S_ISCHR(sb
.st_mode
)) {
95 DBG("DRM fd is not a character device as expected\n");
99 len
= snprintf(perf
->sysfs_dev_dir
,
100 sizeof(perf
->sysfs_dev_dir
),
101 "/sys/dev/char/%d:%d/device/drm", maj
, min
);
102 if (len
< 0 || len
>= sizeof(perf
->sysfs_dev_dir
)) {
103 DBG("Failed to concatenate sysfs path to drm device\n");
107 drmdir
= opendir(perf
->sysfs_dev_dir
);
109 DBG("Failed to open %s: %m\n", perf
->sysfs_dev_dir
);
113 while ((drm_entry
= readdir(drmdir
))) {
114 if (is_dir_or_link(drm_entry
, perf
->sysfs_dev_dir
) &&
115 strncmp(drm_entry
->d_name
, "card", 4) == 0)
117 len
= snprintf(perf
->sysfs_dev_dir
,
118 sizeof(perf
->sysfs_dev_dir
),
119 "/sys/dev/char/%d:%d/device/drm/%s",
120 maj
, min
, drm_entry
->d_name
);
122 if (len
< 0 || len
>= sizeof(perf
->sysfs_dev_dir
))
131 DBG("Failed to find cardX directory under /sys/dev/char/%d:%d/device/drm\n",
138 read_file_uint64(const char *file
, uint64_t *val
)
146 while ((n
= read(fd
, buf
, sizeof (buf
) - 1)) < 0 &&
153 *val
= strtoull(buf
, NULL
, 0);
159 read_sysfs_drm_device_file_uint64(struct gen_perf_config
*perf
,
166 len
= snprintf(buf
, sizeof(buf
), "%s/%s", perf
->sysfs_dev_dir
, file
);
167 if (len
< 0 || len
>= sizeof(buf
)) {
168 DBG("Failed to concatenate sys filename to read u64 from\n");
172 return read_file_uint64(buf
, value
);
176 register_oa_config(struct gen_perf_config
*perf
,
177 const struct gen_device_info
*devinfo
,
178 const struct gen_perf_query_info
*query
,
181 struct gen_perf_query_info
*registered_query
=
182 gen_perf_append_query_info(perf
, 0);
184 *registered_query
= *query
;
185 registered_query
->oa_format
= devinfo
->gen
>= 8 ?
186 I915_OA_FORMAT_A32u40_A4u32_B8_C8
: I915_OA_FORMAT_A45_B8_C8
;
187 registered_query
->oa_metrics_set_id
= config_id
;
188 DBG("metric set registered: id = %" PRIu64
", guid = %s\n",
189 registered_query
->oa_metrics_set_id
, query
->guid
);
193 enumerate_sysfs_metrics(struct gen_perf_config
*perf
,
194 const struct gen_device_info
*devinfo
)
196 DIR *metricsdir
= NULL
;
197 struct dirent
*metric_entry
;
201 len
= snprintf(buf
, sizeof(buf
), "%s/metrics", perf
->sysfs_dev_dir
);
202 if (len
< 0 || len
>= sizeof(buf
)) {
203 DBG("Failed to concatenate path to sysfs metrics/ directory\n");
207 metricsdir
= opendir(buf
);
209 DBG("Failed to open %s: %m\n", buf
);
213 while ((metric_entry
= readdir(metricsdir
))) {
214 struct hash_entry
*entry
;
215 if (!is_dir_or_link(metric_entry
, buf
) ||
216 metric_entry
->d_name
[0] == '.')
219 DBG("metric set: %s\n", metric_entry
->d_name
);
220 entry
= _mesa_hash_table_search(perf
->oa_metrics_table
,
221 metric_entry
->d_name
);
224 if (!gen_perf_load_metric_id(perf
, metric_entry
->d_name
, &id
)) {
225 DBG("Failed to read metric set id from %s: %m", buf
);
229 register_oa_config(perf
, devinfo
,
230 (const struct gen_perf_query_info
*)entry
->data
, id
);
232 DBG("metric set not known by mesa (skipping)\n");
235 closedir(metricsdir
);
239 add_all_metrics(struct gen_perf_config
*perf
,
240 const struct gen_device_info
*devinfo
)
242 hash_table_foreach(perf
->oa_metrics_table
, entry
) {
243 const struct gen_perf_query_info
*query
= entry
->data
;
244 register_oa_config(perf
, devinfo
, query
, 0);
249 kernel_has_dynamic_config_support(struct gen_perf_config
*perf
, int fd
)
251 uint64_t invalid_config_id
= UINT64_MAX
;
253 return gen_ioctl(fd
, DRM_IOCTL_I915_PERF_REMOVE_CONFIG
,
254 &invalid_config_id
) < 0 && errno
== ENOENT
;
258 i915_query_items(struct gen_perf_config
*perf
, int fd
,
259 struct drm_i915_query_item
*items
, uint32_t n_items
)
261 struct drm_i915_query q
= {
262 .num_items
= n_items
,
263 .items_ptr
= to_user_pointer(items
),
265 return gen_ioctl(fd
, DRM_IOCTL_I915_QUERY
, &q
);
269 i915_query_perf_config_supported(struct gen_perf_config
*perf
, int fd
)
271 struct drm_i915_query_item item
= {
272 .query_id
= DRM_I915_QUERY_PERF_CONFIG
,
273 .flags
= DRM_I915_QUERY_PERF_CONFIG_LIST
,
276 return i915_query_items(perf
, fd
, &item
, 1) == 0 && item
.length
> 0;
280 i915_query_perf_config_data(struct gen_perf_config
*perf
,
281 int fd
, const char *guid
,
282 struct drm_i915_perf_oa_config
*config
)
285 struct drm_i915_query_perf_config query
;
286 struct drm_i915_perf_oa_config config
;
288 struct drm_i915_query_item item
= {
289 .query_id
= DRM_I915_QUERY_PERF_CONFIG
,
290 .flags
= DRM_I915_QUERY_PERF_CONFIG_DATA_FOR_UUID
,
291 .data_ptr
= to_user_pointer(&item_data
),
292 .length
= sizeof(item_data
),
295 memset(&item_data
, 0, sizeof(item_data
));
296 memcpy(item_data
.query
.uuid
, guid
, sizeof(item_data
.query
.uuid
));
297 memcpy(&item_data
.config
, config
, sizeof(item_data
.config
));
299 if (!(i915_query_items(perf
, fd
, &item
, 1) == 0 && item
.length
> 0))
302 memcpy(config
, &item_data
.config
, sizeof(item_data
.config
));
308 gen_perf_load_metric_id(struct gen_perf_config
*perf_cfg
,
312 char config_path
[280];
314 snprintf(config_path
, sizeof(config_path
), "%s/metrics/%s/id",
315 perf_cfg
->sysfs_dev_dir
, guid
);
317 /* Don't recreate already loaded configs. */
318 return read_file_uint64(config_path
, metric_id
);
322 i915_add_config(struct gen_perf_config
*perf
, int fd
,
323 const struct gen_perf_registers
*config
,
326 struct drm_i915_perf_oa_config i915_config
= { 0, };
328 memcpy(i915_config
.uuid
, guid
, sizeof(i915_config
.uuid
));
330 i915_config
.n_mux_regs
= config
->n_mux_regs
;
331 i915_config
.mux_regs_ptr
= to_const_user_pointer(config
->mux_regs
);
333 i915_config
.n_boolean_regs
= config
->n_b_counter_regs
;
334 i915_config
.boolean_regs_ptr
= to_const_user_pointer(config
->b_counter_regs
);
336 i915_config
.n_flex_regs
= config
->n_flex_regs
;
337 i915_config
.flex_regs_ptr
= to_const_user_pointer(config
->flex_regs
);
339 int ret
= gen_ioctl(fd
, DRM_IOCTL_I915_PERF_ADD_CONFIG
, &i915_config
);
340 return ret
> 0 ? ret
: 0;
344 init_oa_configs(struct gen_perf_config
*perf
, int fd
,
345 const struct gen_device_info
*devinfo
)
347 hash_table_foreach(perf
->oa_metrics_table
, entry
) {
348 const struct gen_perf_query_info
*query
= entry
->data
;
351 if (gen_perf_load_metric_id(perf
, query
->guid
, &config_id
)) {
352 DBG("metric set: %s (already loaded)\n", query
->guid
);
353 register_oa_config(perf
, devinfo
, query
, config_id
);
357 int ret
= i915_add_config(perf
, fd
, &query
->config
, query
->guid
);
359 DBG("Failed to load \"%s\" (%s) metrics set in kernel: %s\n",
360 query
->name
, query
->guid
, strerror(errno
));
364 register_oa_config(perf
, devinfo
, query
, ret
);
365 DBG("metric set: %s (added)\n", query
->guid
);
370 compute_topology_builtins(struct gen_perf_config
*perf
,
371 const struct gen_device_info
*devinfo
)
373 perf
->sys_vars
.slice_mask
= devinfo
->slice_masks
;
374 perf
->sys_vars
.n_eu_slices
= devinfo
->num_slices
;
376 for (int i
= 0; i
< sizeof(devinfo
->subslice_masks
[i
]); i
++) {
377 perf
->sys_vars
.n_eu_sub_slices
+=
378 __builtin_popcount(devinfo
->subslice_masks
[i
]);
381 for (int i
= 0; i
< sizeof(devinfo
->eu_masks
); i
++)
382 perf
->sys_vars
.n_eus
+= __builtin_popcount(devinfo
->eu_masks
[i
]);
384 perf
->sys_vars
.eu_threads_count
= devinfo
->num_thread_per_eu
;
386 /* The subslice mask builtin contains bits for all slices. Prior to Gen11
387 * it had groups of 3bits for each slice, on Gen11 it's 8bits for each
390 * Ideally equations would be updated to have a slice/subslice query
393 perf
->sys_vars
.subslice_mask
= 0;
395 int bits_per_subslice
= devinfo
->gen
== 11 ? 8 : 3;
397 for (int s
= 0; s
< util_last_bit(devinfo
->slice_masks
); s
++) {
398 for (int ss
= 0; ss
< (devinfo
->subslice_slice_stride
* 8); ss
++) {
399 if (gen_device_info_subslice_available(devinfo
, s
, ss
))
400 perf
->sys_vars
.subslice_mask
|= 1ULL << (s
* bits_per_subslice
+ ss
);
406 init_oa_sys_vars(struct gen_perf_config
*perf
, const struct gen_device_info
*devinfo
)
408 uint64_t min_freq_mhz
= 0, max_freq_mhz
= 0;
410 if (likely(!(INTEL_DEBUG
& DEBUG_NO_OACONFIG
))) {
411 if (!read_sysfs_drm_device_file_uint64(perf
, "gt_min_freq_mhz", &min_freq_mhz
))
414 if (!read_sysfs_drm_device_file_uint64(perf
, "gt_max_freq_mhz", &max_freq_mhz
))
421 memset(&perf
->sys_vars
, 0, sizeof(perf
->sys_vars
));
422 perf
->sys_vars
.gt_min_freq
= min_freq_mhz
* 1000000;
423 perf
->sys_vars
.gt_max_freq
= max_freq_mhz
* 1000000;
424 perf
->sys_vars
.timestamp_frequency
= devinfo
->timestamp_frequency
;
425 perf
->sys_vars
.revision
= devinfo
->revision
;
426 compute_topology_builtins(perf
, devinfo
);
431 typedef void (*perf_register_oa_queries_t
)(struct gen_perf_config
*);
433 static perf_register_oa_queries_t
434 get_register_queries_function(const struct gen_device_info
*devinfo
)
436 if (devinfo
->is_haswell
)
437 return gen_oa_register_queries_hsw
;
438 if (devinfo
->is_cherryview
)
439 return gen_oa_register_queries_chv
;
440 if (devinfo
->is_broadwell
)
441 return gen_oa_register_queries_bdw
;
442 if (devinfo
->is_broxton
)
443 return gen_oa_register_queries_bxt
;
444 if (devinfo
->is_skylake
) {
445 if (devinfo
->gt
== 2)
446 return gen_oa_register_queries_sklgt2
;
447 if (devinfo
->gt
== 3)
448 return gen_oa_register_queries_sklgt3
;
449 if (devinfo
->gt
== 4)
450 return gen_oa_register_queries_sklgt4
;
452 if (devinfo
->is_kabylake
) {
453 if (devinfo
->gt
== 2)
454 return gen_oa_register_queries_kblgt2
;
455 if (devinfo
->gt
== 3)
456 return gen_oa_register_queries_kblgt3
;
458 if (devinfo
->is_geminilake
)
459 return gen_oa_register_queries_glk
;
460 if (devinfo
->is_coffeelake
) {
461 if (devinfo
->gt
== 2)
462 return gen_oa_register_queries_cflgt2
;
463 if (devinfo
->gt
== 3)
464 return gen_oa_register_queries_cflgt3
;
466 if (devinfo
->is_cannonlake
)
467 return gen_oa_register_queries_cnl
;
468 if (devinfo
->gen
== 11) {
469 if (devinfo
->is_elkhartlake
)
470 return gen_oa_register_queries_lkf
;
471 return gen_oa_register_queries_icl
;
473 if (devinfo
->gen
== 12)
474 return gen_oa_register_queries_tgl
;
480 gen_perf_compare_counter_names(const void *v1
, const void *v2
)
482 const struct gen_perf_query_counter
*c1
= v1
;
483 const struct gen_perf_query_counter
*c2
= v2
;
485 return strcmp(c1
->name
, c2
->name
);
489 sort_query(struct gen_perf_query_info
*q
)
491 qsort(q
->counters
, q
->n_counters
, sizeof(q
->counters
[0]),
492 gen_perf_compare_counter_names
);
496 load_pipeline_statistic_metrics(struct gen_perf_config
*perf_cfg
,
497 const struct gen_device_info
*devinfo
)
499 struct gen_perf_query_info
*query
=
500 gen_perf_append_query_info(perf_cfg
, MAX_STAT_COUNTERS
);
502 query
->kind
= GEN_PERF_QUERY_TYPE_PIPELINE
;
503 query
->name
= "Pipeline Statistics Registers";
505 gen_perf_query_add_basic_stat_reg(query
, IA_VERTICES_COUNT
,
506 "N vertices submitted");
507 gen_perf_query_add_basic_stat_reg(query
, IA_PRIMITIVES_COUNT
,
508 "N primitives submitted");
509 gen_perf_query_add_basic_stat_reg(query
, VS_INVOCATION_COUNT
,
510 "N vertex shader invocations");
512 if (devinfo
->gen
== 6) {
513 gen_perf_query_add_stat_reg(query
, GEN6_SO_PRIM_STORAGE_NEEDED
, 1, 1,
514 "SO_PRIM_STORAGE_NEEDED",
515 "N geometry shader stream-out primitives (total)");
516 gen_perf_query_add_stat_reg(query
, GEN6_SO_NUM_PRIMS_WRITTEN
, 1, 1,
517 "SO_NUM_PRIMS_WRITTEN",
518 "N geometry shader stream-out primitives (written)");
520 gen_perf_query_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(0), 1, 1,
521 "SO_PRIM_STORAGE_NEEDED (Stream 0)",
522 "N stream-out (stream 0) primitives (total)");
523 gen_perf_query_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(1), 1, 1,
524 "SO_PRIM_STORAGE_NEEDED (Stream 1)",
525 "N stream-out (stream 1) primitives (total)");
526 gen_perf_query_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(2), 1, 1,
527 "SO_PRIM_STORAGE_NEEDED (Stream 2)",
528 "N stream-out (stream 2) primitives (total)");
529 gen_perf_query_add_stat_reg(query
, GEN7_SO_PRIM_STORAGE_NEEDED(3), 1, 1,
530 "SO_PRIM_STORAGE_NEEDED (Stream 3)",
531 "N stream-out (stream 3) primitives (total)");
532 gen_perf_query_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(0), 1, 1,
533 "SO_NUM_PRIMS_WRITTEN (Stream 0)",
534 "N stream-out (stream 0) primitives (written)");
535 gen_perf_query_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(1), 1, 1,
536 "SO_NUM_PRIMS_WRITTEN (Stream 1)",
537 "N stream-out (stream 1) primitives (written)");
538 gen_perf_query_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(2), 1, 1,
539 "SO_NUM_PRIMS_WRITTEN (Stream 2)",
540 "N stream-out (stream 2) primitives (written)");
541 gen_perf_query_add_stat_reg(query
, GEN7_SO_NUM_PRIMS_WRITTEN(3), 1, 1,
542 "SO_NUM_PRIMS_WRITTEN (Stream 3)",
543 "N stream-out (stream 3) primitives (written)");
546 gen_perf_query_add_basic_stat_reg(query
, HS_INVOCATION_COUNT
,
547 "N TCS shader invocations");
548 gen_perf_query_add_basic_stat_reg(query
, DS_INVOCATION_COUNT
,
549 "N TES shader invocations");
551 gen_perf_query_add_basic_stat_reg(query
, GS_INVOCATION_COUNT
,
552 "N geometry shader invocations");
553 gen_perf_query_add_basic_stat_reg(query
, GS_PRIMITIVES_COUNT
,
554 "N geometry shader primitives emitted");
556 gen_perf_query_add_basic_stat_reg(query
, CL_INVOCATION_COUNT
,
557 "N primitives entering clipping");
558 gen_perf_query_add_basic_stat_reg(query
, CL_PRIMITIVES_COUNT
,
559 "N primitives leaving clipping");
561 if (devinfo
->is_haswell
|| devinfo
->gen
== 8) {
562 gen_perf_query_add_stat_reg(query
, PS_INVOCATION_COUNT
, 1, 4,
563 "N fragment shader invocations",
564 "N fragment shader invocations");
566 gen_perf_query_add_basic_stat_reg(query
, PS_INVOCATION_COUNT
,
567 "N fragment shader invocations");
570 gen_perf_query_add_basic_stat_reg(query
, PS_DEPTH_COUNT
,
571 "N z-pass fragments");
573 if (devinfo
->gen
>= 7) {
574 gen_perf_query_add_basic_stat_reg(query
, CS_INVOCATION_COUNT
,
575 "N compute shader invocations");
578 query
->data_size
= sizeof(uint64_t) * query
->n_counters
;
584 i915_perf_version(int drm_fd
)
587 drm_i915_getparam_t gp
= {
588 .param
= I915_PARAM_PERF_REVISION
,
592 int ret
= gen_ioctl(drm_fd
, DRM_IOCTL_I915_GETPARAM
, &gp
);
594 /* Return 0 if this getparam is not supported, the first version supported
597 return ret
< 0 ? 0 : tmp
;
601 i915_get_sseu(int drm_fd
, struct drm_i915_gem_context_param_sseu
*sseu
)
603 struct drm_i915_gem_context_param arg
= {
604 .param
= I915_CONTEXT_PARAM_SSEU
,
605 .size
= sizeof(*sseu
),
606 .value
= to_user_pointer(sseu
)
609 gen_ioctl(drm_fd
, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM
, &arg
);
613 compare_str_or_null(const char *s1
, const char *s2
)
615 if (s1
== NULL
&& s2
== NULL
)
622 return strcmp(s1
, s2
);
626 compare_counter_categories_and_names(const void *_c1
, const void *_c2
)
628 const struct gen_perf_query_counter_info
*c1
= (const struct gen_perf_query_counter_info
*)_c1
;
629 const struct gen_perf_query_counter_info
*c2
= (const struct gen_perf_query_counter_info
*)_c2
;
631 /* pipeline counters don't have an assigned category */
632 int r
= compare_str_or_null(c1
->counter
->category
, c2
->counter
->category
);
636 return strcmp(c1
->counter
->name
, c2
->counter
->name
);
640 build_unique_counter_list(struct gen_perf_config
*perf
)
642 assert(perf
->n_queries
< 64);
644 size_t max_counters
= 0;
646 for (int q
= 0; q
< perf
->n_queries
; q
++)
647 max_counters
+= perf
->queries
[q
].n_counters
;
650 * Allocate big enough array to hold maximum possible number of counters.
651 * We can't alloc it small and realloc when needed because the hash table
652 * below contains pointers to this array.
654 struct gen_perf_query_counter_info
*counter_infos
=
655 ralloc_array_size(perf
, sizeof(counter_infos
[0]), max_counters
);
657 perf
->n_counters
= 0;
659 struct hash_table
*counters_table
=
660 _mesa_hash_table_create(perf
,
662 _mesa_key_string_equal
);
663 struct hash_entry
*entry
;
664 for (int q
= 0; q
< perf
->n_queries
; q
++) {
665 struct gen_perf_query_info
*query
= &perf
->queries
[q
];
667 for (int c
= 0; c
< query
->n_counters
; c
++) {
668 struct gen_perf_query_counter
*counter
;
669 struct gen_perf_query_counter_info
*counter_info
;
671 counter
= &query
->counters
[c
];
672 entry
= _mesa_hash_table_search(counters_table
, counter
->symbol_name
);
675 counter_info
= entry
->data
;
676 counter_info
->query_mask
|= BITFIELD64_BIT(q
);
679 assert(perf
->n_counters
< max_counters
);
681 counter_info
= &counter_infos
[perf
->n_counters
++];
682 counter_info
->counter
= counter
;
683 counter_info
->query_mask
= BITFIELD64_BIT(q
);
685 counter_info
->location
.group_idx
= q
;
686 counter_info
->location
.counter_idx
= c
;
688 _mesa_hash_table_insert(counters_table
, counter
->symbol_name
, counter_info
);
692 _mesa_hash_table_destroy(counters_table
, NULL
);
694 /* Now we can realloc counter_infos array because hash table doesn't exist. */
695 perf
->counter_infos
= reralloc_array_size(perf
, counter_infos
,
696 sizeof(counter_infos
[0]), perf
->n_counters
);
698 qsort(perf
->counter_infos
, perf
->n_counters
, sizeof(perf
->counter_infos
[0]),
699 compare_counter_categories_and_names
);
703 oa_metrics_available(struct gen_perf_config
*perf
, int fd
,
704 const struct gen_device_info
*devinfo
)
706 perf_register_oa_queries_t oa_register
= get_register_queries_function(devinfo
);
707 bool i915_perf_oa_available
= false;
710 perf
->i915_query_supported
= i915_query_perf_config_supported(perf
, fd
);
711 perf
->i915_perf_version
= i915_perf_version(fd
);
713 /* Record the default SSEU configuration. */
714 i915_get_sseu(fd
, &perf
->sseu
);
716 /* The existence of this sysctl parameter implies the kernel supports
717 * the i915 perf interface.
719 if (stat("/proc/sys/dev/i915/perf_stream_paranoid", &sb
) == 0) {
721 /* If _paranoid == 1 then on Gen8+ we won't be able to access OA
722 * metrics unless running as root.
724 if (devinfo
->is_haswell
)
725 i915_perf_oa_available
= true;
727 uint64_t paranoid
= 1;
729 read_file_uint64("/proc/sys/dev/i915/perf_stream_paranoid", ¶noid
);
731 if (paranoid
== 0 || geteuid() == 0)
732 i915_perf_oa_available
= true;
735 perf
->platform_supported
= oa_register
!= NULL
;
738 return i915_perf_oa_available
&&
740 get_sysfs_dev_dir(perf
, fd
) &&
741 init_oa_sys_vars(perf
, devinfo
);
745 load_oa_metrics(struct gen_perf_config
*perf
, int fd
,
746 const struct gen_device_info
*devinfo
)
748 int existing_queries
= perf
->n_queries
;
750 perf_register_oa_queries_t oa_register
= get_register_queries_function(devinfo
);
752 perf
->oa_metrics_table
=
753 _mesa_hash_table_create(perf
, _mesa_hash_string
,
754 _mesa_key_string_equal
);
756 /* Index all the metric sets mesa knows about before looking to see what
757 * the kernel is advertising.
761 if (likely(!(INTEL_DEBUG
& DEBUG_NO_OACONFIG
))) {
762 if (kernel_has_dynamic_config_support(perf
, fd
))
763 init_oa_configs(perf
, fd
, devinfo
);
765 enumerate_sysfs_metrics(perf
, devinfo
);
767 add_all_metrics(perf
, devinfo
);
770 /* sort counters in each individual group created by this function by name */
771 for (int i
= existing_queries
; i
< perf
->n_queries
; ++i
)
772 sort_query(&perf
->queries
[i
]);
774 /* Select a fallback OA metric. Look for the TestOa metric or use the last
775 * one if no present (on HSW).
777 for (int i
= existing_queries
; i
< perf
->n_queries
; i
++) {
778 if (perf
->queries
[i
].symbol_name
&&
779 strcmp(perf
->queries
[i
].symbol_name
, "TestOa") == 0) {
780 perf
->fallback_raw_oa_metric
= perf
->queries
[i
].oa_metrics_set_id
;
784 if (perf
->fallback_raw_oa_metric
== 0)
785 perf
->fallback_raw_oa_metric
= perf
->queries
[perf
->n_queries
- 1].oa_metrics_set_id
;
788 struct gen_perf_registers
*
789 gen_perf_load_configuration(struct gen_perf_config
*perf_cfg
, int fd
, const char *guid
)
791 if (!perf_cfg
->i915_query_supported
)
794 struct drm_i915_perf_oa_config i915_config
= { 0, };
795 if (!i915_query_perf_config_data(perf_cfg
, fd
, guid
, &i915_config
))
798 struct gen_perf_registers
*config
= rzalloc(NULL
, struct gen_perf_registers
);
799 config
->n_flex_regs
= i915_config
.n_flex_regs
;
800 config
->flex_regs
= rzalloc_array(config
, struct gen_perf_query_register_prog
, config
->n_flex_regs
);
801 config
->n_mux_regs
= i915_config
.n_mux_regs
;
802 config
->mux_regs
= rzalloc_array(config
, struct gen_perf_query_register_prog
, config
->n_mux_regs
);
803 config
->n_b_counter_regs
= i915_config
.n_boolean_regs
;
804 config
->b_counter_regs
= rzalloc_array(config
, struct gen_perf_query_register_prog
, config
->n_b_counter_regs
);
807 * struct gen_perf_query_register_prog maps exactly to the tuple of
808 * (register offset, register value) returned by the i915.
810 i915_config
.flex_regs_ptr
= to_const_user_pointer(config
->flex_regs
);
811 i915_config
.mux_regs_ptr
= to_const_user_pointer(config
->mux_regs
);
812 i915_config
.boolean_regs_ptr
= to_const_user_pointer(config
->b_counter_regs
);
813 if (!i915_query_perf_config_data(perf_cfg
, fd
, guid
, &i915_config
)) {
822 gen_perf_store_configuration(struct gen_perf_config
*perf_cfg
, int fd
,
823 const struct gen_perf_registers
*config
,
827 return i915_add_config(perf_cfg
, fd
, config
, guid
);
829 struct mesa_sha1 sha1_ctx
;
830 _mesa_sha1_init(&sha1_ctx
);
832 if (config
->flex_regs
) {
833 _mesa_sha1_update(&sha1_ctx
, config
->flex_regs
,
834 sizeof(config
->flex_regs
[0]) *
835 config
->n_flex_regs
);
837 if (config
->mux_regs
) {
838 _mesa_sha1_update(&sha1_ctx
, config
->mux_regs
,
839 sizeof(config
->mux_regs
[0]) *
842 if (config
->b_counter_regs
) {
843 _mesa_sha1_update(&sha1_ctx
, config
->b_counter_regs
,
844 sizeof(config
->b_counter_regs
[0]) *
845 config
->n_b_counter_regs
);
849 _mesa_sha1_final(&sha1_ctx
, hash
);
851 char formatted_hash
[41];
852 _mesa_sha1_format(formatted_hash
, hash
);
854 char generated_guid
[37];
855 snprintf(generated_guid
, sizeof(generated_guid
),
856 "%.8s-%.4s-%.4s-%.4s-%.12s",
857 &formatted_hash
[0], &formatted_hash
[8],
858 &formatted_hash
[8 + 4], &formatted_hash
[8 + 4 + 4],
859 &formatted_hash
[8 + 4 + 4 + 4]);
861 /* Check if already present. */
863 if (gen_perf_load_metric_id(perf_cfg
, generated_guid
, &id
))
866 return i915_add_config(perf_cfg
, fd
, config
, generated_guid
);
870 get_passes_mask(struct gen_perf_config
*perf
,
871 const uint32_t *counter_indices
,
872 uint32_t counter_indices_count
)
874 uint64_t queries_mask
= 0;
876 assert(perf
->n_queries
< 64);
878 /* Compute the number of passes by going through all counters N times (with
879 * N the number of queries) to make sure we select the most constraining
880 * counters first and look at the more flexible ones (that could be
881 * obtained from multiple queries) later. That way we minimize the number
882 * of passes required.
884 for (uint32_t q
= 0; q
< perf
->n_queries
; q
++) {
885 for (uint32_t i
= 0; i
< counter_indices_count
; i
++) {
886 assert(counter_indices
[i
] < perf
->n_counters
);
888 uint32_t idx
= counter_indices
[i
];
889 if (__builtin_popcount(perf
->counter_infos
[idx
].query_mask
) != (q
+ 1))
892 if (queries_mask
& perf
->counter_infos
[idx
].query_mask
)
895 queries_mask
|= BITFIELD64_BIT(ffsll(perf
->counter_infos
[idx
].query_mask
) - 1);
903 gen_perf_get_n_passes(struct gen_perf_config
*perf
,
904 const uint32_t *counter_indices
,
905 uint32_t counter_indices_count
,
906 struct gen_perf_query_info
**pass_queries
)
908 uint64_t queries_mask
= get_passes_mask(perf
, counter_indices
, counter_indices_count
);
912 for (uint32_t q
= 0; q
< perf
->n_queries
; q
++) {
913 if ((1ULL << q
) & queries_mask
)
914 pass_queries
[pass
++] = &perf
->queries
[q
];
918 return __builtin_popcount(queries_mask
);
922 gen_perf_get_counters_passes(struct gen_perf_config
*perf
,
923 const uint32_t *counter_indices
,
924 uint32_t counter_indices_count
,
925 struct gen_perf_counter_pass
*counter_pass
)
927 uint64_t queries_mask
= get_passes_mask(perf
, counter_indices
, counter_indices_count
);
928 ASSERTED
uint32_t n_passes
= __builtin_popcount(queries_mask
);
930 for (uint32_t i
= 0; i
< counter_indices_count
; i
++) {
931 assert(counter_indices
[i
] < perf
->n_counters
);
933 uint32_t idx
= counter_indices
[i
];
934 counter_pass
[i
].counter
= perf
->counter_infos
[idx
].counter
;
936 uint32_t query_idx
= ffsll(perf
->counter_infos
[idx
].query_mask
& queries_mask
) - 1;
937 counter_pass
[i
].query
= &perf
->queries
[query_idx
];
939 uint32_t clear_bits
= 63 - query_idx
;
940 counter_pass
[i
].pass
= __builtin_popcount((queries_mask
<< clear_bits
) >> clear_bits
) - 1;
941 assert(counter_pass
[i
].pass
< n_passes
);
945 /* Accumulate 32bits OA counters */
947 accumulate_uint32(const uint32_t *report0
,
948 const uint32_t *report1
,
949 uint64_t *accumulator
)
951 *accumulator
+= (uint32_t)(*report1
- *report0
);
954 /* Accumulate 40bits OA counters */
956 accumulate_uint40(int a_index
,
957 const uint32_t *report0
,
958 const uint32_t *report1
,
959 uint64_t *accumulator
)
961 const uint8_t *high_bytes0
= (uint8_t *)(report0
+ 40);
962 const uint8_t *high_bytes1
= (uint8_t *)(report1
+ 40);
963 uint64_t high0
= (uint64_t)(high_bytes0
[a_index
]) << 32;
964 uint64_t high1
= (uint64_t)(high_bytes1
[a_index
]) << 32;
965 uint64_t value0
= report0
[a_index
+ 4] | high0
;
966 uint64_t value1
= report1
[a_index
+ 4] | high1
;
970 delta
= (1ULL << 40) + value1
- value0
;
972 delta
= value1
- value0
;
974 *accumulator
+= delta
;
978 gen8_read_report_clock_ratios(const uint32_t *report
,
979 uint64_t *slice_freq_hz
,
980 uint64_t *unslice_freq_hz
)
982 /* The lower 16bits of the RPT_ID field of the OA reports contains a
983 * snapshot of the bits coming from the RP_FREQ_NORMAL register and is
986 * RPT_ID[31:25]: RP_FREQ_NORMAL[20:14] (low squashed_slice_clock_frequency)
987 * RPT_ID[10:9]: RP_FREQ_NORMAL[22:21] (high squashed_slice_clock_frequency)
988 * RPT_ID[8:0]: RP_FREQ_NORMAL[31:23] (squashed_unslice_clock_frequency)
990 * RP_FREQ_NORMAL[31:23]: Software Unslice Ratio Request
991 * Multiple of 33.33MHz 2xclk (16 MHz 1xclk)
993 * RP_FREQ_NORMAL[22:14]: Software Slice Ratio Request
994 * Multiple of 33.33MHz 2xclk (16 MHz 1xclk)
997 uint32_t unslice_freq
= report
[0] & 0x1ff;
998 uint32_t slice_freq_low
= (report
[0] >> 25) & 0x7f;
999 uint32_t slice_freq_high
= (report
[0] >> 9) & 0x3;
1000 uint32_t slice_freq
= slice_freq_low
| (slice_freq_high
<< 7);
1002 *slice_freq_hz
= slice_freq
* 16666667ULL;
1003 *unslice_freq_hz
= unslice_freq
* 16666667ULL;
1007 gen_perf_query_result_read_frequencies(struct gen_perf_query_result
*result
,
1008 const struct gen_device_info
*devinfo
,
1009 const uint32_t *start
,
1010 const uint32_t *end
)
1012 /* Slice/Unslice frequency is only available in the OA reports when the
1013 * "Disable OA reports due to clock ratio change" field in
1014 * OA_DEBUG_REGISTER is set to 1. This is how the kernel programs this
1015 * global register (see drivers/gpu/drm/i915/i915_perf.c)
1017 * Documentation says this should be available on Gen9+ but experimentation
1018 * shows that Gen8 reports similar values, so we enable it there too.
1020 if (devinfo
->gen
< 8)
1023 gen8_read_report_clock_ratios(start
,
1024 &result
->slice_frequency
[0],
1025 &result
->unslice_frequency
[0]);
1026 gen8_read_report_clock_ratios(end
,
1027 &result
->slice_frequency
[1],
1028 &result
->unslice_frequency
[1]);
1032 gen_perf_query_result_accumulate(struct gen_perf_query_result
*result
,
1033 const struct gen_perf_query_info
*query
,
1034 const uint32_t *start
,
1035 const uint32_t *end
)
1039 if (result
->hw_id
== OA_REPORT_INVALID_CTX_ID
&&
1040 start
[2] != OA_REPORT_INVALID_CTX_ID
)
1041 result
->hw_id
= start
[2];
1042 if (result
->reports_accumulated
== 0)
1043 result
->begin_timestamp
= start
[1];
1044 result
->reports_accumulated
++;
1046 switch (query
->oa_format
) {
1047 case I915_OA_FORMAT_A32u40_A4u32_B8_C8
:
1048 accumulate_uint32(start
+ 1, end
+ 1,
1049 result
->accumulator
+ query
->gpu_time_offset
); /* timestamp */
1050 accumulate_uint32(start
+ 3, end
+ 3,
1051 result
->accumulator
+ query
->gpu_clock_offset
); /* clock */
1053 /* 32x 40bit A counters... */
1054 for (i
= 0; i
< 32; i
++) {
1055 accumulate_uint40(i
, start
, end
,
1056 result
->accumulator
+ query
->a_offset
+ i
);
1059 /* 4x 32bit A counters... */
1060 for (i
= 0; i
< 4; i
++) {
1061 accumulate_uint32(start
+ 36 + i
, end
+ 36 + i
,
1062 result
->accumulator
+ query
->a_offset
+ 32 + i
);
1065 /* 8x 32bit B counters */
1066 for (i
= 0; i
< 8; i
++) {
1067 accumulate_uint32(start
+ 48 + i
, end
+ 48 + i
,
1068 result
->accumulator
+ query
->b_offset
+ i
);
1071 /* 8x 32bit C counters... */
1072 for (i
= 0; i
< 8; i
++) {
1073 accumulate_uint32(start
+ 56 + i
, end
+ 56 + i
,
1074 result
->accumulator
+ query
->c_offset
+ i
);
1078 case I915_OA_FORMAT_A45_B8_C8
:
1079 accumulate_uint32(start
+ 1, end
+ 1, result
->accumulator
); /* timestamp */
1081 for (i
= 0; i
< 61; i
++) {
1082 accumulate_uint32(start
+ 3 + i
, end
+ 3 + i
,
1083 result
->accumulator
+ query
->a_offset
+ i
);
1088 unreachable("Can't accumulate OA counters in unknown format");
1094 gen_perf_query_result_clear(struct gen_perf_query_result
*result
)
1096 memset(result
, 0, sizeof(*result
));
1097 result
->hw_id
= OA_REPORT_INVALID_CTX_ID
; /* invalid */
1101 gen_perf_compare_query_names(const void *v1
, const void *v2
)
1103 const struct gen_perf_query_info
*q1
= v1
;
1104 const struct gen_perf_query_info
*q2
= v2
;
1106 return strcmp(q1
->name
, q2
->name
);
1110 gen_perf_init_metrics(struct gen_perf_config
*perf_cfg
,
1111 const struct gen_device_info
*devinfo
,
1113 bool include_pipeline_statistics
)
1115 if (include_pipeline_statistics
) {
1116 load_pipeline_statistic_metrics(perf_cfg
, devinfo
);
1117 gen_perf_register_mdapi_statistic_query(perf_cfg
, devinfo
);
1120 bool oa_metrics
= oa_metrics_available(perf_cfg
, drm_fd
, devinfo
);
1122 load_oa_metrics(perf_cfg
, drm_fd
, devinfo
);
1124 /* sort query groups by name */
1125 qsort(perf_cfg
->queries
, perf_cfg
->n_queries
,
1126 sizeof(perf_cfg
->queries
[0]), gen_perf_compare_query_names
);
1128 build_unique_counter_list(perf_cfg
);
1131 gen_perf_register_mdapi_oa_query(perf_cfg
, devinfo
);