Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / tools / perf / tests / bp_signal_overflow.c
blobeb4dbbddf4ffcfcbe42ee513d0dac6520dad00e7
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Originally done by Vince Weaver <vincent.weaver@maine.edu> for
4 * perf_event_tests (git://github.com/deater/perf_event_tests)
5 */
7 /*
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__
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <sys/ioctl.h>
18 #include <time.h>
19 #include <fcntl.h>
20 #include <signal.h>
21 #include <sys/mman.h>
22 #include <linux/compiler.h>
23 #include <linux/hw_breakpoint.h>
25 #include "tests.h"
26 #include "debug.h"
27 #include "event.h"
28 #include "../perf-sys.h"
29 #include "cloexec.h"
31 static int overflows;
33 static noinline int test_function(void)
35 return time(NULL);
38 static void sig_handler(int signum __maybe_unused,
39 siginfo_t *oh __maybe_unused,
40 void *uc __maybe_unused)
42 overflows++;
45 static long long bp_count(int fd)
47 long long count;
48 int ret;
50 ret = read(fd, &count, sizeof(long long));
51 if (ret != sizeof(long long)) {
52 pr_debug("failed to read: %d\n", ret);
53 return TEST_FAIL;
56 return count;
59 #define EXECUTIONS 10000
60 #define THRESHOLD 100
62 int test__bp_signal_overflow(struct test *test __maybe_unused, int subtest __maybe_unused)
64 struct perf_event_attr pe;
65 struct sigaction sa;
66 long long count;
67 int fd, i, fails = 0;
69 /* setup SIGIO signal handler */
70 memset(&sa, 0, sizeof(struct sigaction));
71 sa.sa_sigaction = (void *) sig_handler;
72 sa.sa_flags = SA_SIGINFO;
74 if (sigaction(SIGIO, &sa, NULL) < 0) {
75 pr_debug("failed setting up signal handler\n");
76 return TEST_FAIL;
79 memset(&pe, 0, sizeof(struct perf_event_attr));
80 pe.type = PERF_TYPE_BREAKPOINT;
81 pe.size = sizeof(struct perf_event_attr);
83 pe.config = 0;
84 pe.bp_type = HW_BREAKPOINT_X;
85 pe.bp_addr = (unsigned long) test_function;
86 pe.bp_len = sizeof(long);
88 pe.sample_period = THRESHOLD;
89 pe.sample_type = PERF_SAMPLE_IP;
90 pe.wakeup_events = 1;
92 pe.disabled = 1;
93 pe.exclude_kernel = 1;
94 pe.exclude_hv = 1;
96 fd = sys_perf_event_open(&pe, 0, -1, -1,
97 perf_event_open_cloexec_flag());
98 if (fd < 0) {
99 pr_debug("failed opening event %llx\n", pe.config);
100 return TEST_FAIL;
103 fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
104 fcntl(fd, F_SETSIG, SIGIO);
105 fcntl(fd, F_SETOWN, getpid());
107 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
108 ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
110 for (i = 0; i < EXECUTIONS; i++)
111 test_function();
113 ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
115 count = bp_count(fd);
117 close(fd);
119 pr_debug("count %lld, overflow %d\n",
120 count, overflows);
122 if (count != EXECUTIONS) {
123 pr_debug("\tWrong number of executions %lld != %d\n",
124 count, EXECUTIONS);
125 fails++;
128 if (overflows != EXECUTIONS / THRESHOLD) {
129 pr_debug("\tWrong number of overflows %d != %d\n",
130 overflows, EXECUTIONS / THRESHOLD);
131 fails++;
134 return fails ? TEST_FAIL : TEST_OK;