From 3a0c394fa2eafdb3be3da59920556071b2a20d0a Mon Sep 17 00:00:00 2001 From: Jan Polensky Date: Mon, 29 Sep 2025 12:58:52 +0200 Subject: [PATCH] libcpumf: Relocate ctr_in_list() for shared use MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move ctr_in_list() from cpumf/lspai.c to a shared location to enable reuse in other binaries that require counter list filtering. Reviewed-by: Thomas Richter Signed-off-by: Jan Polensky Signed-off-by: Jan Höppner --- cpumf/lspai.c | 28 ++-------------------------- include/lib/libcpumf.h | 15 +++++++++++++++ libcpumf/libcpumf_support.c | 23 +++++++++++++++++++++++ 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/cpumf/lspai.c b/cpumf/lspai.c index 20a57fd4..8df91387 100644 --- a/cpumf/lspai.c +++ b/cpumf/lspai.c @@ -233,31 +233,6 @@ static int pai_ctrcmp(const void *p1, const void *p2) return l->nr > r->nr ? 1 : -1; } -/* Return true if the counter is in the ctrlist. An empty ctrlist - * disables all counters. - */ -static bool ctr_in_list(char *name) -{ - char *token; - char *list; - - if (!ctrlist) /* No --counters means all counters */ - return true; - - list = util_strdup(ctrlist); - token = strtok(list, ","); - while (token) { - if (strcmp(token, name) == 0) { - free(list); - return true; - } - token = strtok(NULL, ","); - } - - free(list); - return false; -} - /* Read counter names and assigned event number from sysfs file tree. * Exit when sysfs directory can not be scanned. */ @@ -280,7 +255,8 @@ static void read_counternames(struct pai_node *node) if (util_file_read_va(ctrpath, "event=%x", &ctr) == 1) { snprintf(sname, sizeof(sname), "%c%ld", pai_type_char(node->type), ctr - node->base); - if (!ctr_in_list(sname) && !ctr_in_list(namelist[i]->d_name)) { + if (!ctr_in_list(sname, ctrlist) && + !ctr_in_list(namelist[i]->d_name, ctrlist)) { /* Counter not listed in --counters option */ continue; } diff --git a/include/lib/libcpumf.h b/include/lib/libcpumf.h index f960018d..faef0589 100644 --- a/include/lib/libcpumf.h +++ b/include/lib/libcpumf.h @@ -202,4 +202,19 @@ bool libcpumf_have_pai_nnpa(void); long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags); + +/** + * Check if a counter name is present in a comma-separated list of counters. + * + * This function checks whether the given counter name appears in the + * provided list. If the list is NULL, the function assumes all counters + * are allowed and returns true. + * + * @param name Name of the counter to search for + * @param ctrlist Comma-separated list of counter names, or NULL + * + * @return true if the counter is in the list or list is NULL + * @return false otherwise + */ +bool ctr_in_list(char *name, char *ctrlist); #endif diff --git a/libcpumf/libcpumf_support.c b/libcpumf/libcpumf_support.c index a4f672c4..76e7065c 100644 --- a/libcpumf/libcpumf_support.c +++ b/libcpumf/libcpumf_support.c @@ -14,6 +14,7 @@ #include "lib/libcpumf.h" #include "lib/util_path.h" +#include "lib/util_libc.h" #define SERVICELEVEL "/proc/service_levels" @@ -175,3 +176,25 @@ long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int g { return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); } + +bool ctr_in_list(char *name, char *ctrlist) +{ + char *token; + char *list; + + if (!ctrlist) /* No --counters means all counters */ + return true; + + list = util_strdup(ctrlist); + token = strtok(list, ","); + while (token) { + if (strcmp(token, name) == 0) { + free(list); + return true; + } + token = strtok(NULL, ","); + } + + free(list); + return false; +}