Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Originally done by Vince Weaver <vincent.weaver@maine.edu> for
0004  * perf_event_tests (git://github.com/deater/perf_event_tests)
0005  */
0006 
0007 /*
0008  * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
0009  * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
0010  */
0011 #define __SANE_USERSPACE_TYPES__
0012 
0013 #include <stdlib.h>
0014 #include <stdio.h>
0015 #include <unistd.h>
0016 #include <string.h>
0017 #include <sys/ioctl.h>
0018 #include <time.h>
0019 #include <fcntl.h>
0020 #include <signal.h>
0021 #include <sys/mman.h>
0022 #include <linux/compiler.h>
0023 #include <linux/hw_breakpoint.h>
0024 
0025 #include "tests.h"
0026 #include "debug.h"
0027 #include "event.h"
0028 #include "../perf-sys.h"
0029 #include "cloexec.h"
0030 
0031 static int overflows;
0032 
0033 static noinline int test_function(void)
0034 {
0035     return time(NULL);
0036 }
0037 
0038 static void sig_handler(int signum __maybe_unused,
0039             siginfo_t *oh __maybe_unused,
0040             void *uc __maybe_unused)
0041 {
0042     overflows++;
0043 }
0044 
0045 static long long bp_count(int fd)
0046 {
0047     long long count;
0048     int ret;
0049 
0050     ret = read(fd, &count, sizeof(long long));
0051     if (ret != sizeof(long long)) {
0052         pr_debug("failed to read: %d\n", ret);
0053         return TEST_FAIL;
0054     }
0055 
0056     return count;
0057 }
0058 
0059 #define EXECUTIONS 10000
0060 #define THRESHOLD  100
0061 
0062 static int test__bp_signal_overflow(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0063 {
0064     struct perf_event_attr pe;
0065     struct sigaction sa;
0066     long long count;
0067     int fd, i, fails = 0;
0068 
0069     if (!BP_SIGNAL_IS_SUPPORTED) {
0070         pr_debug("Test not supported on this architecture");
0071         return TEST_SKIP;
0072     }
0073 
0074     /* setup SIGIO signal handler */
0075     memset(&sa, 0, sizeof(struct sigaction));
0076     sa.sa_sigaction = (void *) sig_handler;
0077     sa.sa_flags = SA_SIGINFO;
0078 
0079     if (sigaction(SIGIO, &sa, NULL) < 0) {
0080         pr_debug("failed setting up signal handler\n");
0081         return TEST_FAIL;
0082     }
0083 
0084     memset(&pe, 0, sizeof(struct perf_event_attr));
0085     pe.type = PERF_TYPE_BREAKPOINT;
0086     pe.size = sizeof(struct perf_event_attr);
0087 
0088     pe.config = 0;
0089     pe.bp_type = HW_BREAKPOINT_X;
0090     pe.bp_addr = (unsigned long) test_function;
0091     pe.bp_len = sizeof(long);
0092 
0093     pe.sample_period = THRESHOLD;
0094     pe.sample_type = PERF_SAMPLE_IP;
0095     pe.wakeup_events = 1;
0096 
0097     pe.disabled = 1;
0098     pe.exclude_kernel = 1;
0099     pe.exclude_hv = 1;
0100 
0101     fd = sys_perf_event_open(&pe, 0, -1, -1,
0102                  perf_event_open_cloexec_flag());
0103     if (fd < 0) {
0104         pr_debug("failed opening event %llx\n", pe.config);
0105         return TEST_FAIL;
0106     }
0107 
0108     fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
0109     fcntl(fd, F_SETSIG, SIGIO);
0110     fcntl(fd, F_SETOWN, getpid());
0111 
0112     ioctl(fd, PERF_EVENT_IOC_RESET, 0);
0113     ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
0114 
0115     for (i = 0; i < EXECUTIONS; i++)
0116         test_function();
0117 
0118     ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
0119 
0120     count = bp_count(fd);
0121 
0122     close(fd);
0123 
0124     pr_debug("count %lld, overflow %d\n",
0125          count, overflows);
0126 
0127     if (count != EXECUTIONS) {
0128         pr_debug("\tWrong number of executions %lld != %d\n",
0129         count, EXECUTIONS);
0130         fails++;
0131     }
0132 
0133     if (overflows != EXECUTIONS / THRESHOLD) {
0134         pr_debug("\tWrong number of overflows %d != %d\n",
0135         overflows, EXECUTIONS / THRESHOLD);
0136         fails++;
0137     }
0138 
0139     return fails ? TEST_FAIL : TEST_OK;
0140 }
0141 
0142 DEFINE_SUITE("Breakpoint overflow sampling", bp_signal_overflow);