From 5aa1824eeff822e4935221b3d663cd290228810b Mon Sep 17 00:00:00 2001 From: Jan Polensky Date: Tue, 5 Aug 2025 14:19:58 +0200 Subject: [PATCH] libcpumf: Move perf_event_open to reduce code duplication MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Relocated the perf_event_open logic into a shared helper to eliminate redundant implementations across multiple files. Enhances consistency, reduces maintenance overhead, and lowers structural complexity. Acked-by: Thomas Richter Reviewed-by: Thomas Richter Signed-off-by: Jan Polensky Signed-off-by: Jan Höppner --- cpacfstats/Makefile | 2 +- cpacfstats/perf_crypto.c | 11 +---------- cpumf/pai.c | 8 -------- include/lib/libcpumf.h | 20 ++++++++++++++++++++ libcpumf/libcpumf_support.c | 6 ++++++ 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/cpacfstats/Makefile b/cpacfstats/Makefile index 774147d8..9bdf2891 100644 --- a/cpacfstats/Makefile +++ b/cpacfstats/Makefile @@ -22,7 +22,7 @@ ALL_CPPFLAGS += -DVERSION=$(VERSION) all: check_dep cpacfstats cpacfstatsd cpacfstatsd: cpacfstatsd.o stats_sock.o perf_crypto.o cpacfstats_common.o \ - $(rootdir)/libutil/libutil.a + $(rootdir)/libcpumf/libcpumf.a $(rootdir)/libutil/libutil.a $(LINK) $(ALL_LDFLAGS) $^ $(LDLIBS) -ludev -lpthread -o $@ cpacfstats: cpacfstats.o stats_sock.o cpacfstats_common.o diff --git a/cpacfstats/perf_crypto.c b/cpacfstats/perf_crypto.c index 02952170..ec816677 100644 --- a/cpacfstats/perf_crypto.c +++ b/cpacfstats/perf_crypto.c @@ -30,6 +30,7 @@ #include #include "cpacfstats.h" +#include "lib/libcpumf.h" #include "../include/lib/zt_common.h" /* correlation between counter and perf counter string */ @@ -123,16 +124,6 @@ static struct percpucounter *findcpu(unsigned int cpunum, int unlinkflag) return NULL; } -static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, - int cpu, int group_fd, unsigned long flags) -{ - int ret; - - ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, - group_fd, flags); - return ret; -} - static int perf_supported(void) { return !access("/proc/sys/kernel/perf_event_paranoid", R_OK); diff --git a/cpumf/pai.c b/cpumf/pai.c index 9e794eab..f872d5a5 100644 --- a/cpumf/pai.c +++ b/cpumf/pai.c @@ -59,14 +59,6 @@ static struct util_list list_pai_event; static struct util_list list_pmu_event; static bool summary; -/* System call to perf_event_open(2) */ -static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, - int cpu, int group_fd, unsigned long flags) -{ - return syscall(__NR_perf_event_open, hw_event, pid, cpu, - group_fd, flags); -} - static void ev_dealloc(void) { struct pai_event *next, *p; diff --git a/include/lib/libcpumf.h b/include/lib/libcpumf.h index 6836435f..f960018d 100644 --- a/include/lib/libcpumf.h +++ b/include/lib/libcpumf.h @@ -9,6 +9,8 @@ #include #include +#include +#include #define S390_CPUMF_CF "devices/cpum_cf/" #define S390_CPUMF_CFDIAG "devices/cpum_cf_diag/" @@ -182,4 +184,22 @@ bool libcpumf_have_pai_ext(void); * @retval false PAI_NNPA counter Facility is not available */ bool libcpumf_have_pai_nnpa(void); + +/** + * Wrapper for the perf_event_open syscall used to configure performance events. + * This function simplifies usage of perf_event_open and provides a consistent + * interface for libcpumf internals. + * + * @param hw_event Pointer to perf_event_attr structure describing the event + * @param pid Target process ID (0 for current process) + * @param cpu Target CPU (-1 for all CPUs) + * @param group_fd File descriptor of event group leader, or -1 if none + * @param flags Additional flags (usually 0) + * + * @return File descriptor for the opened event on success + * @return -1 on failure, errno is set appropriately + */ + +long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, + unsigned long flags); #endif diff --git a/libcpumf/libcpumf_support.c b/libcpumf/libcpumf_support.c index 69630025..a4f672c4 100644 --- a/libcpumf/libcpumf_support.c +++ b/libcpumf/libcpumf_support.c @@ -169,3 +169,9 @@ bool libcpumf_have_pai_nnpa(void) { return libcpumf_have_pai_sysfs(S390_SYSFS_PAI_NNPA); } + +long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, + unsigned long flags) +{ + return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); +}