2 * Originally done by Vince Weaver <vincent.weaver@maine.edu> for
3 * perf_event_tests (git://github.com/deater/perf_event_tests)
7 * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
8 * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
10 #define __SANE_USERSPACE_TYPES__
16 #include <sys/ioctl.h>
21 #include <linux/compiler.h>
22 #include <linux/hw_breakpoint.h>
30 __attribute__ ((noinline))
31 static int test_function(void)
36 static void sig_handler(int signum __maybe_unused,
37 siginfo_t *oh __maybe_unused,
38 void *uc __maybe_unused)
43 static long long bp_count(int fd)
48 ret = read(fd, &count, sizeof(long long));
49 if (ret != sizeof(long long)) {
50 pr_debug("failed to read: %d\n", ret);
57 #define EXECUTIONS 10000
60 int test__bp_signal_overflow(void)
62 struct perf_event_attr pe;
67 /* setup SIGIO signal handler */
68 memset(&sa, 0, sizeof(struct sigaction));
69 sa.sa_sigaction = (void *) sig_handler;
70 sa.sa_flags = SA_SIGINFO;
72 if (sigaction(SIGIO, &sa, NULL) < 0) {
73 pr_debug("failed setting up signal handler\n");
77 memset(&pe, 0, sizeof(struct perf_event_attr));
78 pe.type = PERF_TYPE_BREAKPOINT;
79 pe.size = sizeof(struct perf_event_attr);
82 pe.bp_type = HW_BREAKPOINT_X;
83 pe.bp_addr = (unsigned long) test_function;
84 pe.bp_len = sizeof(long);
86 pe.sample_period = THRESHOLD;
87 pe.sample_type = PERF_SAMPLE_IP;
91 pe.exclude_kernel = 1;
94 fd = sys_perf_event_open(&pe, 0, -1, -1, 0);
96 pr_debug("failed opening event %llx\n", pe.config);
100 fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
101 fcntl(fd, F_SETSIG, SIGIO);
102 fcntl(fd, F_SETOWN, getpid());
104 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
105 ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
107 for (i = 0; i < EXECUTIONS; i++)
110 ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
112 count = bp_count(fd);
116 pr_debug("count %lld, overflow %d\n",
119 if (count != EXECUTIONS) {
120 pr_debug("\tWrong number of executions %lld != %d\n",
125 if (overflows != EXECUTIONS / THRESHOLD) {
126 pr_debug("\tWrong number of overflows %d != %d\n",
127 overflows, EXECUTIONS / THRESHOLD);
131 return fails ? TEST_FAIL : TEST_OK;