libcpumf: Relocate ctr_in_list() for shared use

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 <tmricht@linux.ibm.com>
Signed-off-by: Jan Polensky <japo@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Jan Polensky
2025-09-29 12:58:52 +02:00
committed by Jan Höppner
parent 20a4ebd83c
commit 3a0c394fa2
3 changed files with 40 additions and 26 deletions

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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;
}