1 // SPDX-License-Identifier: GPL-2.0
3 * Originally done by Vince Weaver <vincent.weaver@maine.edu> for
4 * perf_event_tests (git://github.com/deater/perf_event_tests)
8 * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
9 * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
11 #define __SANE_USERSPACE_TYPES__
17 #include <sys/ioctl.h>
22 #include <linux/compiler.h>
23 #include <linux/hw_breakpoint.h>
28 #include "parse-events.h"
29 #include "../perf-sys.h"
34 static noinline
int test_function(void)
39 static void sig_handler(int signum __maybe_unused
,
40 siginfo_t
*oh __maybe_unused
,
41 void *uc __maybe_unused
)
46 static long long bp_count(int fd
)
51 ret
= read(fd
, &count
, sizeof(long long));
52 if (ret
!= sizeof(long long)) {
53 pr_debug("failed to read: %d\n", ret
);
60 #define EXECUTIONS 10000
63 static int test__bp_signal_overflow(struct test_suite
*test __maybe_unused
, int subtest __maybe_unused
)
65 struct perf_event_attr pe
;
70 if (!BP_SIGNAL_IS_SUPPORTED
) {
71 pr_debug("Test not supported on this architecture");
75 /* setup SIGIO signal handler */
76 memset(&sa
, 0, sizeof(struct sigaction
));
77 sa
.sa_sigaction
= (void *) sig_handler
;
78 sa
.sa_flags
= SA_SIGINFO
;
80 if (sigaction(SIGIO
, &sa
, NULL
) < 0) {
81 pr_debug("failed setting up signal handler\n");
85 memset(&pe
, 0, sizeof(struct perf_event_attr
));
86 pe
.type
= PERF_TYPE_BREAKPOINT
;
87 pe
.size
= sizeof(struct perf_event_attr
);
90 pe
.bp_type
= HW_BREAKPOINT_X
;
91 pe
.bp_addr
= (unsigned long) test_function
;
92 pe
.bp_len
= default_breakpoint_len();
94 pe
.sample_period
= THRESHOLD
;
95 pe
.sample_type
= PERF_SAMPLE_IP
;
99 pe
.exclude_kernel
= 1;
102 fd
= sys_perf_event_open(&pe
, 0, -1, -1,
103 perf_event_open_cloexec_flag());
105 pr_debug("failed opening event %llx\n", pe
.config
);
109 fcntl(fd
, F_SETFL
, O_RDWR
|O_NONBLOCK
|O_ASYNC
);
110 fcntl(fd
, F_SETSIG
, SIGIO
);
111 fcntl(fd
, F_SETOWN
, getpid());
113 ioctl(fd
, PERF_EVENT_IOC_RESET
, 0);
114 ioctl(fd
, PERF_EVENT_IOC_ENABLE
, 0);
116 for (i
= 0; i
< EXECUTIONS
; i
++)
119 ioctl(fd
, PERF_EVENT_IOC_DISABLE
, 0);
121 count
= bp_count(fd
);
125 pr_debug("count %lld, overflow %d\n",
128 if (count
!= EXECUTIONS
) {
129 pr_debug("\tWrong number of executions %lld != %d\n",
134 if (overflows
!= EXECUTIONS
/ THRESHOLD
) {
135 pr_debug("\tWrong number of overflows %d != %d\n",
136 overflows
, EXECUTIONS
/ THRESHOLD
);
140 return fails
? TEST_FAIL
: TEST_OK
;
143 DEFINE_SUITE("Breakpoint overflow sampling", bp_signal_overflow
);