2022-03-25 15:05:49

by [email protected]

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

Add the following functions:

perf_evsel__check_overflow
perf_evsel__check_overflow_cpu

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

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

diff --git a/tools/lib/perf/Documentation/libperf.txt b/tools/lib/perf/Documentation/libperf.txt
index 700c1ce15159..4ae8d738b948 100644
--- a/tools/lib/perf/Documentation/libperf.txt
+++ b/tools/lib/perf/Documentation/libperf.txt
@@ -165,6 +165,9 @@ SYNOPSIS
int cpu_map_idx);
int perf_evsel__period(struct perf_evsel *evsel, int period);
int perf_evsel__period_cpu(struct perf_evsel *evsel, int period, int cpu_map_idx);
+ int perf_evsel__check_overflow(struct perf_evsel *evsel, int sig_fd, bool *overflow);
+ int perf_evsel__check_overflow_cpu(struct perf_evsel *evsel, int cpu_map_idx,
+ int sig_fd, bool *overflow);
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 db9b7274feb5..6ff3cf692df3 100644
--- a/tools/lib/perf/evsel.c
+++ b/tools/lib/perf/evsel.c
@@ -616,3 +616,37 @@ int perf_evsel__open_opts(struct perf_evsel *evsel, struct perf_cpu_map *cpus,

return err;
}
+
+int perf_evsel__check_overflow_cpu(struct perf_evsel *evsel, int cpu_map_idx,
+ int sig_fd, bool *overflow)
+{
+ int thread;
+ int *fd;
+ int err = 0;
+
+ if (!overflow)
+ return -EINVAL;
+
+ *overflow = false;
+
+ for (thread = 0; thread < xyarray__max_y(evsel->fd) && !err; ++thread) {
+ fd = FD(evsel, cpu_map_idx, thread);
+ if (sig_fd <= 0 || !fd || *fd < 0)
+ err = -EINVAL;
+
+ if (sig_fd == *fd)
+ *overflow = true;
+ }
+
+ return err;
+}
+
+int perf_evsel__check_overflow(struct perf_evsel *evsel, int sig_fd, bool *overflow)
+{
+ int cpu_map_idx;
+ int err = 0;
+
+ for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->fd) && !err; cpu_map_idx++)
+ err = perf_evsel__check_overflow_cpu(evsel, cpu_map_idx, sig_fd, overflow);
+ return err;
+}
diff --git a/tools/lib/perf/include/perf/evsel.h b/tools/lib/perf/include/perf/evsel.h
index ecf30bc6303f..d686cfbd88a6 100644
--- a/tools/lib/perf/include/perf/evsel.h
+++ b/tools/lib/perf/include/perf/evsel.h
@@ -74,5 +74,9 @@ LIBPERF_API int perf_evsel__open_opts(struct perf_evsel *evsel,
struct perf_cpu_map *cpus,
struct perf_thread_map *threads,
struct perf_evsel_open_opts *opts);
+LIBPERF_API int perf_evsel__check_overflow(struct perf_evsel *evsel, int sig_fd,
+ bool *overflow);
+LIBPERF_API int perf_evsel__check_overflow_cpu(struct perf_evsel *evsel,
+ int cpu_map_idx, int sig_fd, bool *overflow);

#endif /* __LIBPERF_EVSEL_H */
diff --git a/tools/lib/perf/libperf.map b/tools/lib/perf/libperf.map
index 534614fbbb26..981eade34237 100644
--- a/tools/lib/perf/libperf.map
+++ b/tools/lib/perf/libperf.map
@@ -37,6 +37,8 @@ LIBPERF_0.0.1 {
perf_evsel__cpus;
perf_evsel__threads;
perf_evsel__attr;
+ perf_evsel__check_overflow;
+ perf_evsel__check_overflow_cpu;
perf_evlist__new;
perf_evlist__delete;
perf_evlist__open;
--
2.25.1


2022-03-31 04:46:45

by Jiri Olsa

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

On Fri, Mar 25, 2022 at 01:38:27PM +0900, Shunsuke Nakamura wrote:
> Add the following functions:
>
> perf_evsel__check_overflow
> perf_evsel__check_overflow_cpu
>
> to check for perf events with the file descriptor specified in the
> argument.
> These functions can be used in signal handlers to check overflow.
>
> Signed-off-by: Shunsuke Nakamura <[email protected]>
> ---
> tools/lib/perf/Documentation/libperf.txt | 3 +++
> tools/lib/perf/evsel.c | 34 ++++++++++++++++++++++++
> tools/lib/perf/include/perf/evsel.h | 4 +++
> tools/lib/perf/libperf.map | 2 ++
> 4 files changed, 43 insertions(+)
>
> diff --git a/tools/lib/perf/Documentation/libperf.txt b/tools/lib/perf/Documentation/libperf.txt
> index 700c1ce15159..4ae8d738b948 100644
> --- a/tools/lib/perf/Documentation/libperf.txt
> +++ b/tools/lib/perf/Documentation/libperf.txt
> @@ -165,6 +165,9 @@ SYNOPSIS
> int cpu_map_idx);
> int perf_evsel__period(struct perf_evsel *evsel, int period);
> int perf_evsel__period_cpu(struct perf_evsel *evsel, int period, int cpu_map_idx);
> + int perf_evsel__check_overflow(struct perf_evsel *evsel, int sig_fd, bool *overflow);
> + int perf_evsel__check_overflow_cpu(struct perf_evsel *evsel, int cpu_map_idx,
> + int sig_fd, bool *overflow);

should this be more like:

perf_evsel__has_fd(struct perf_evsel *evsel, int fd)

also why do we need to export *_cpu version for this?
I don't see it used in the test

> 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 db9b7274feb5..6ff3cf692df3 100644
> --- a/tools/lib/perf/evsel.c
> +++ b/tools/lib/perf/evsel.c
> @@ -616,3 +616,37 @@ int perf_evsel__open_opts(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
>
> return err;
> }
> +
> +int perf_evsel__check_overflow_cpu(struct perf_evsel *evsel, int cpu_map_idx,
> + int sig_fd, bool *overflow)
> +{
> + int thread;
> + int *fd;
> + int err = 0;
> +
> + if (!overflow)
> + return -EINVAL;
> +
> + *overflow = false;
> +
> + for (thread = 0; thread < xyarray__max_y(evsel->fd) && !err; ++thread) {
> + fd = FD(evsel, cpu_map_idx, thread);
> + if (sig_fd <= 0 || !fd || *fd < 0)
> + err = -EINVAL;

sig_fd check should be before the loop

jirka

2022-04-12 23:43:51

by [email protected]

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

Hi jirka

> On Fri, Mar 25, 2022 at 01:38:27PM +0900, Shunsuke Nakamura wrote:
> > Add the following functions:
> >
> > perf_evsel__check_overflow
> > perf_evsel__check_overflow_cpu
> >
> > to check for perf events with the file descriptor specified in the
> > argument.
> > These functions can be used in signal handlers to check overflow.
> >
> > Signed-off-by: Shunsuke Nakamura <[email protected]>
> > ---
> > tools/lib/perf/Documentation/libperf.txt | 3 +++
> > tools/lib/perf/evsel.c | 34
> ++++++++++++++++++++++++
> > tools/lib/perf/include/perf/evsel.h | 4 +++
> > tools/lib/perf/libperf.map | 2 ++
> > 4 files changed, 43 insertions(+)
> >
> > diff --git a/tools/lib/perf/Documentation/libperf.txt
> > b/tools/lib/perf/Documentation/libperf.txt
> > index 700c1ce15159..4ae8d738b948 100644
> > --- a/tools/lib/perf/Documentation/libperf.txt
> > +++ b/tools/lib/perf/Documentation/libperf.txt
> > @@ -165,6 +165,9 @@ SYNOPSIS
> > int cpu_map_idx);
> > int perf_evsel__period(struct perf_evsel *evsel, int period);
> > int perf_evsel__period_cpu(struct perf_evsel *evsel, int period,
> > int cpu_map_idx);
> > + int perf_evsel__check_overflow(struct perf_evsel *evsel, int
> > + sig_fd, bool *overflow); int perf_evsel__check_overflow_cpu(struct
> perf_evsel *evsel, int cpu_map_idx,
> > + int sig_fd, bool *overflow);
>
> should this be more like:
>
> perf_evsel__has_fd(struct perf_evsel *evsel, int fd)
>
I'll fix it.


> also why do we need to export *_cpu version for this?
> I don't see it used in the test
>
Sorry, not necessary.
I'll fix it.

> > 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
> > db9b7274feb5..6ff3cf692df3 100644
> > --- a/tools/lib/perf/evsel.c
> > +++ b/tools/lib/perf/evsel.c
> > @@ -616,3 +616,37 @@ int perf_evsel__open_opts(struct perf_evsel
> > *evsel, struct perf_cpu_map *cpus,
> >
> > return err;
> > }
> > +
> > +int perf_evsel__check_overflow_cpu(struct perf_evsel *evsel, int
> cpu_map_idx,
> > + int sig_fd, bool *overflow)
> > +{
> > + int thread;
> > + int *fd;
> > + int err = 0;
> > +
> > + if (!overflow)
> > + return -EINVAL;
> > +
> > + *overflow = false;
> > +
> > + for (thread = 0; thread < xyarray__max_y(evsel->fd) && !err; ++thread)
> {
> > + fd = FD(evsel, cpu_map_idx, thread);
> > + if (sig_fd <= 0 || !fd || *fd < 0)
> > + err = -EINVAL;
>
> sig_fd check should be before the loop
>
I'll fix it.


Best Regards
Shunsuke