2022-02-25 11:10:25

by [email protected]

[permalink] [raw]
Subject: [RFC PATCH 5/7] libperf: Add perf_evsel__check_fd() functions

From: Shunsuke Nakamura <[email protected]>

Add the following functions:

perf_evsel__check_fd()
perf_evsel__check_fd_cpu()

to check for perf events with the file descriptor specified in the
argument.
These functions can be used in signal handlers to detect overflow.

Signed-off-by: Shunsuke Nakamura <[email protected]>
---
tools/lib/perf/Documentation/libperf.txt | 2 ++
tools/lib/perf/evsel.c | 27 ++++++++++++++++++++++++
tools/lib/perf/include/perf/evsel.h | 2 ++
tools/lib/perf/libperf.map | 2 ++
4 files changed, 33 insertions(+)

diff --git a/tools/lib/perf/Documentation/libperf.txt b/tools/lib/perf/Documentation/libperf.txt
index d0aa57c6ca97..d3a1a9e77262 100644
--- a/tools/lib/perf/Documentation/libperf.txt
+++ b/tools/lib/perf/Documentation/libperf.txt
@@ -155,6 +155,8 @@ SYNOPSIS
struct sigaction *sig);
int perf_evsel__set_signal_cpu(struct perf_evsel *evsel, int owner_type, unsigned int signal,
struct sigaction *sig, int cpu_map_idx);
+ int perf_evsel__check_fd(struct perf_evsel *evsel, int sig_fd);
+ int perf_evsel__check_fd_cpu(struct perf_evsel *evsel, int cpu_map_idx, int sig_fd);
struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel);
struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel);
struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel);
diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c
index 991ab04112b1..0c626f846980 100644
--- a/tools/lib/perf/evsel.c
+++ b/tools/lib/perf/evsel.c
@@ -542,6 +542,33 @@ int perf_evsel__set_signal(struct perf_evsel *evsel, int owner_type, unsigned in
return err;
}

+int perf_evsel__check_fd_cpu(struct perf_evsel *evsel, int cpu_map_idx, int sig_fd)
+{
+ int thread;
+ int *fd;
+
+ for (thread = 0; thread < xyarray__max_y(evsel->fd); ++thread) {
+ fd = FD(evsel, cpu_map_idx, thread);
+ if (sig_fd <= 0 || !fd || *fd < 0)
+ return -1;
+
+ if (sig_fd == *fd)
+ return 0;
+ }
+
+ return -1;
+}
+
+int perf_evsel__check_fd(struct perf_evsel *evsel, int sig_fd)
+{
+ int i;
+ int err = 0;
+
+ for (i = 0; i < xyarray__max_x(evsel->fd); i++)
+ err = perf_evsel__check_fd_cpu(evsel, i, sig_fd);
+ return err;
+}
+
int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter)
{
int err = 0, i;
diff --git a/tools/lib/perf/include/perf/evsel.h b/tools/lib/perf/include/perf/evsel.h
index 15768fc91910..5d4b4912fec9 100644
--- a/tools/lib/perf/include/perf/evsel.h
+++ b/tools/lib/perf/include/perf/evsel.h
@@ -49,6 +49,8 @@ LIBPERF_API int perf_evsel__set_signal(struct perf_evsel *evsel, int owner_type,
LIBPERF_API int perf_evsel__set_signal_cpu(struct perf_evsel *evsel, int owner_type,
unsigned int signal, struct sigaction *sig,
int cpu_map_idx);
+LIBPERF_API int perf_evsel__check_fd(struct perf_evsel *evsel, int sig_fd);
+LIBPERF_API int perf_evsel__check_fd_cpu(struct perf_evsel *evsel, int cpu_map_idx, int sig_fd);
LIBPERF_API struct perf_cpu_map *perf_evsel__cpus(struct perf_evsel *evsel);
LIBPERF_API struct perf_thread_map *perf_evsel__threads(struct perf_evsel *evsel);
LIBPERF_API struct perf_event_attr *perf_evsel__attr(struct perf_evsel *evsel);
diff --git a/tools/lib/perf/libperf.map b/tools/lib/perf/libperf.map
index 5e04fe2b611c..edd60bc69822 100644
--- a/tools/lib/perf/libperf.map
+++ b/tools/lib/perf/libperf.map
@@ -34,6 +34,8 @@ LIBPERF_0.0.1 {
perf_evsel__period_cpu;
perf_evsel__set_signal;
perf_evsel__set_signal_cpu;
+ perf_evsel__check_fd;
+ perf_evsel__check_fd_cpu;
perf_evsel__cpus;
perf_evsel__threads;
perf_evsel__attr;
--
2.31.1