[PATCH v2 8/8] libperf test: Add test_stat_overflow_event()

Charlie Jenkins
2024-07-29T16:46:20Z

Add a test to check overflowed events.

Committer testing:

$ sudo make tests -C ./tools/lib/perf V=1
make: Entering directory '/home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/lib/perf'
make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=. obj=libperf
make -C /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/lib/api/ O= libapi.a
make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=./fd obj=libapi
make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=./fs obj=libapi
make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=. obj=tests
make -f /home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/build/Makefile.build dir=./tests obj=tests
running static:
- running tests/test-cpumap.c...OK
- running tests/test-threadmap.c...OK
- running tests/test-evlist.c...

<SNIP>

Event 0 -- overflow flag = 0x1, POLL_HUP = 1, other signal event = 0
Event 1 -- overflow flag = 0x2, POLL_HUP = 1, other signal event = 0
Event 2 -- overflow flag = 0x4, POLL_HUP = 1, other signal event = 0
Event 3 -- overflow flag = 0x8, POLL_HUP = 1, other signal event = 0
OK
- running tests/test-evsel.c...

<SNIP>

OK
running dynamic:
- running tests/test-cpumap.c...OK
- running tests/test-threadmap.c...OK
- running tests/test-evlist.c...

<SNIP>

Event 0 -- overflow flag = 0x1, POLL_HUP = 1, other signal event = 0
Event 1 -- overflow flag = 0x2, POLL_HUP = 1, other signal event = 0
Event 2 -- overflow flag = 0x4, POLL_HUP = 1, other signal event = 0
Event 3 -- overflow flag = 0x8, POLL_HUP = 1, other signal event = 0
OK
- running tests/test-evsel.c...

<SNIP>

OK
make: Leaving directory '/home/nakamura/build_work/build_kernel/linux-kernel/linux/tools/lib/perf'

Signed-off-by: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
Signed-off-by: Charlie Jenkins <charlie@rivosinc.com>
---
tools/lib/perf/tests/test-evlist.c | 111 +++++++++++++++++++++++++++++++++++++
tools/lib/perf/tests/test-evsel.c | 20 +++----
2 files changed, 121 insertions(+), 10 deletions(-)

diff --git a/tools/lib/perf/tests/test-evlist.c b/tools/lib/perf/tests/test-evlist.c
index 3a833f0349d3..7bfceb8e0c66 100644
--- a/tools/lib/perf/tests/test-evlist.c
+++ b/tools/lib/perf/tests/test-evlist.c
@@ -5,6 +5,8 @@
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
#include <linux/perf_event.h>
#include <linux/limits.h>
#include <sys/types.h>
@@ -24,6 +26,13 @@
#define EVENT_NUM 15
#define WAIT_COUNT 100000000UL

+static unsigned int overflow_flag;
+static struct signal_counts {
+ int hup;
+ int others;
+} sig_count;
+static struct perf_evlist *s_evlist;
+
static int libperf_print(enum libperf_print_level level,
const char *fmt, va_list ap)
{
@@ -570,6 +579,107 @@ static int test_stat_multiplexing(void)
return 0;
}

+static void sig_handler(int signo, siginfo_t *info, void *uc)
+{
+ struct perf_evsel *evsel;
+ int i = 0;
+
+ perf_evlist__for_each_evsel(s_evlist, evsel) {
+ if (perf_evsel__has_fd(evsel, info->si_fd)) {
+ if (info->si_code == POLL_HUP)
+ sig_count.hup++;
+ else
+ sig_count.others++;
+
+ overflow_flag = (1U << i);
+ return;
+ }
+ i++;
+ }
+
+ __T_VERBOSE("Failed to get fd of overflowed event");
+}
+
+static int test_stat_overflow_event(void)
+{
+ static struct sigaction sigact;
+
+ struct perf_thread_map *threads;
+ struct perf_evsel *evsel;
+ struct perf_event_attr attr = {
+ .type = PERF_TYPE_SOFTWARE,
+ .config = PERF_COUNT_SW_CPU_CLOCK,
+ .sample_type = PERF_SAMPLE_PERIOD,
+ .sample_period = 100000,
+ .disabled = 1,
+ };
+ int err, i, event_num = 4;
+
+ LIBPERF_OPTS(perf_evsel_open_opts, opts,
+ .open_flags = PERF_FLAG_FD_CLOEXEC,
+ .fcntl_flags = (O_RDWR | O_NONBLOCK | O_ASYNC),
+ .signal = SIGRTMIN + 1,
+ .owner_type = F_OWNER_PID,
+ .sigact = &sigact);
+
+ /* setup signal handler */
+ memset(&sigact, 0, sizeof(struct sigaction));
+ sigact.sa_sigaction = (void *)sig_handler;
+ sigact.sa_flags = SA_SIGINFO;
+
+ threads = perf_thread_map__new_dummy();
+ __T("failed to create threads", threads);
+
+ perf_thread_map__set_pid(threads, 0, 0);
+
+ s_evlist = perf_evlist__new();
+ __T("failed to create evlist", s_evlist);
+
+ for (i = 0; i < event_num; i++) {
+ evsel = perf_evsel__new(&attr);
+ __T("failed to create evsel", evsel);
+
+ perf_evlist__add(s_evlist, evsel);
+ }
+
+ perf_evlist__set_maps(s_evlist, NULL, threads);
+
+ err = perf_evlist__open_opts(s_evlist, &opts);
+ __T("failed to open evlist", err == 0);
+
+ i = 0;
+ perf_evlist__for_each_evsel(s_evlist, evsel) {
+ volatile unsigned int wait_count = WAIT_COUNT;
+
+ overflow_flag = 0;
+ sig_count.hup = 0;
+ sig_count.others = 0;
+
+ err = perf_evsel__refresh(evsel, 1);
+ __T("failed to refresh evsel", err == 0);
+
+ while (wait_count--)
+ ;
+
+ __T_VERBOSE("Event %2d -- overflow flag = %#x, ",
+ i, overflow_flag);
+ __T_VERBOSE("POLL_HUP = %d, other signal event = %d\n",
+ sig_count.hup, sig_count.others);
+
+ __T("unexpected event overflow detected", overflow_flag && (1U << i));
+ __T("unexpected signal event detected",
+ sig_count.hup == 1 && sig_count.others == 0);
+
+ i++;
+ }
+
+ perf_evlist__close(s_evlist);
+ perf_evlist__delete(s_evlist);
+ perf_thread_map__put(threads);
+
+ return 0;
+}
+
int test_evlist(int argc, char **argv)
{
__T_START;
@@ -582,6 +692,7 @@ int test_evlist(int argc, char **argv)
test_mmap_thread();
test_mmap_cpus();
test_stat_multiplexing();
+ test_stat_overflow_event();

__T_END;
return tests_failed == 0 ? 0 : -1;
diff --git a/tools/lib/perf/tests/test-evsel.c b/tools/lib/perf/tests/test-evsel.c
index b27dd65f2ec9..56f4ae20e922 100644
--- a/tools/lib/perf/tests/test-evsel.c
+++ b/tools/lib/perf/tests/test-evsel.c
@@ -15,7 +15,7 @@
#include <internal/tests.h>
#include "tests.h"

-#define WAIT_COUNT 10000000UL
+#define WAIT_COUNT 100000000UL
static struct signal_counts {
int in;
int hup;
@@ -380,7 +380,7 @@ static void sig_handler(int signo, siginfo_t *info, void *uc)

static int test_stat_overflow(int owner)
{
- static struct sigaction sig;
+ static struct sigaction sigact;
u64 period = 1000000;
int overflow_limit = 3;

@@ -396,16 +396,16 @@ static int test_stat_overflow(int owner)
int err = 0, i;

LIBPERF_OPTS(perf_evsel_open_opts, opts,
- .open_flags = PERF_FLAG_FD_CLOEXEC,
- .flags = (O_RDWR | O_NONBLOCK | O_ASYNC),
- .signal = SIGRTMIN + 1,
- .owner_type = owner,
- .sig = &sig);
+ .open_flags = PERF_FLAG_FD_CLOEXEC,
+ .fcntl_flags = (O_RDWR | O_NONBLOCK | O_ASYNC),
+ .signal = SIGRTMIN + 1,
+ .owner_type = owner,
+ .sigact = &sigact);

/* setup signal handler */
- memset(&sig, 0, sizeof(struct sigaction));
- sig.sa_sigaction = (void *)sig_handler;
- sig.sa_flags = SA_SIGINFO;
+ memset(&sigact, 0, sizeof(struct sigaction));
+ sigact.sa_sigaction = (void *)sig_handler;
+ sigact.sa_flags = SA_SIGINFO;

threads = perf_thread_map__new_dummy();
__T("failed to create threads", threads);

--
2.44.0