From cc743feb525c2e8ae1f39c5c40358d932bd853bd Mon Sep 17 00:00:00 2001 From: Rob Clark Date: Fri, 1 May 2026 11:07:05 -0700 Subject: [PATCH] freedreno/perfcntrs: Add helpers to resolve group and countable We were duplicating this in a few places. Add helpers instead. Signed-off-by: Rob Clark --- src/freedreno/computerator/main.cc | 9 +++---- src/freedreno/ds/fd_pps_driver.cc | 7 +++-- src/freedreno/perfcntrs/freedreno_perfcntr.h | 27 ++++++++++++++++++++ src/freedreno/vulkan/tu_autotune.cc | 24 +++-------------- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/freedreno/computerator/main.cc b/src/freedreno/computerator/main.cc index 31c839af1df..f33d136f2ee 100644 --- a/src/freedreno/computerator/main.cc +++ b/src/freedreno/computerator/main.cc @@ -134,13 +134,10 @@ setup_counter(const char *name, struct perfcntr *c) { for (int i = 0; i < num_groups; i++) { const struct fd_perfcntr_group *group = &groups[i]; + const struct fd_perfcntr_countable *countable = + fd_perfcntrs_countable(group, name); - for (int j = 0; j < group->num_countables; j++) { - const struct fd_perfcntr_countable *countable = &group->countables[j]; - - if (strcmp(name, countable->name) != 0) - continue; - + if (countable) { /* * Allocate a counter to use to monitor the requested countable: */ diff --git a/src/freedreno/ds/fd_pps_driver.cc b/src/freedreno/ds/fd_pps_driver.cc index 2da6073fb31..8d73813ae41 100644 --- a/src/freedreno/ds/fd_pps_driver.cc +++ b/src/freedreno/ds/fd_pps_driver.cc @@ -539,11 +539,10 @@ FreedrenoDriver::Countable::resolve() const if (group != g->name) continue; - for (unsigned j = 0; j < g->num_countables; j++) { - const struct fd_perfcntr_countable *c = &g->countables[j]; - if (name != c->name) - continue; + const struct fd_perfcntr_countable *c = + fd_perfcntrs_countable(g, name.c_str()); + if (c) { d->state[id].countable = c; /* Assign counters from high to low to reduce conflicts with UMD-owned diff --git a/src/freedreno/perfcntrs/freedreno_perfcntr.h b/src/freedreno/perfcntrs/freedreno_perfcntr.h index a0b6e99acde..5ab0630abf5 100644 --- a/src/freedreno/perfcntrs/freedreno_perfcntr.h +++ b/src/freedreno/perfcntrs/freedreno_perfcntr.h @@ -89,6 +89,33 @@ const struct fd_perfcntr_group *fd_perfcntrs(const struct fd_dev_id *id, unsigne .countables = _countables, \ } +static inline const struct fd_perfcntr_group * +fd_perfcntrs_group(const struct fd_dev_id *id, const char *name) +{ + const struct fd_perfcntr_group *groups; + unsigned count; + + groups = fd_perfcntrs(id, &count); + if (!groups) + return NULL; + + for (unsigned i = 0; i < count; i++) + if (!strcmp(groups[i].name, name)) + return &groups[i]; + + return NULL; +} + +static inline const struct fd_perfcntr_countable * +fd_perfcntrs_countable(const struct fd_perfcntr_group *group, const char *name) +{ + for (unsigned i = 0; i < group->num_countables; i++) + if (!strcmp(group->countables[i].name, name)) + return &group->countables[i]; + + return NULL; +} + #define FD_DERIVED_COUNTER_MAX_PERFCNTRS 8 struct fd_derivation_context { diff --git a/src/freedreno/vulkan/tu_autotune.cc b/src/freedreno/vulkan/tu_autotune.cc index aa6c7f816dd..421cd26c3d1 100644 --- a/src/freedreno/vulkan/tu_autotune.cc +++ b/src/freedreno/vulkan/tu_autotune.cc @@ -1633,31 +1633,13 @@ tu_autotune::tu_autotune(struct tu_device *device, VkResult &result) tu_bo_suballocator_init(&suballoc, device, 128 * 1024, TU_BO_ALLOC_INTERNAL_RESOURCE, "autotune_suballoc"); if (supports_preempt_latency_tracking()) { - uint32_t group_count; - const struct fd_perfcntr_group *groups = fd_perfcntrs(&device->physical_device->dev_id, &group_count); const char *fail_reason = nullptr; - const fd_perfcntr_group *cp_group = nullptr; - for (uint32_t i = 0; i < group_count; i++) { - if (strcmp(groups[i].name, "CP") == 0) { - cp_group = &groups[i]; - break; - } - } + const fd_perfcntr_group *cp_group = fd_perfcntrs_group(&device->physical_device->dev_id, "CP"); if (cp_group) { - auto get_perfcntr_countable = [](const struct fd_perfcntr_group *group, - const char *name) -> const struct fd_perfcntr_countable * { - for (uint32_t i = 0; i < group->num_countables; i++) { - if (strcmp(group->countables[i].name, name) == 0) - return &group->countables[i]; - } - - return nullptr; - }; - - auto preemption_latency_countable = get_perfcntr_countable(cp_group, "PERF_CP_PREEMPTION_REACTION_DELAY"); - auto always_count_countable = get_perfcntr_countable(cp_group, "PERF_CP_ALWAYS_COUNT"); + auto preemption_latency_countable = fd_perfcntrs_countable(cp_group, "PERF_CP_PREEMPTION_REACTION_DELAY"); + auto always_count_countable = fd_perfcntrs_countable(cp_group, "PERF_CP_ALWAYS_COUNT"); if (preemption_latency_countable && always_count_countable) { if (cp_group->num_counters >= 2) { preemption_latency_selector_reg = cp_group->counters[0].select_reg;