Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <unistd.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdbool.h> |
| 6 | #include <string.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <poll.h> |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 9 | #include <linux/perf_event.h> |
| 10 | #include <linux/bpf.h> |
| 11 | #include <errno.h> |
| 12 | #include <assert.h> |
| 13 | #include <sys/syscall.h> |
| 14 | #include <sys/ioctl.h> |
| 15 | #include <sys/mman.h> |
| 16 | #include <time.h> |
| 17 | #include <signal.h> |
Jakub Kicinski | 2bf3e2e | 2018-05-14 22:35:02 -0700 | [diff] [blame] | 18 | #include <libbpf.h> |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 19 | #include "bpf_load.h" |
Joe Stringer | 205c8ad | 2016-12-08 18:46:19 -0800 | [diff] [blame] | 20 | #include "perf-sys.h" |
Yonghong Song | 28dbf86 | 2018-04-28 22:28:13 -0700 | [diff] [blame] | 21 | #include "trace_helpers.h" |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 22 | |
| 23 | static int pmu_fd; |
| 24 | |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 25 | static __u64 time_get_ns(void) |
| 26 | { |
| 27 | struct timespec ts; |
| 28 | |
| 29 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 30 | return ts.tv_sec * 1000000000ull + ts.tv_nsec; |
| 31 | } |
| 32 | |
| 33 | static __u64 start_time; |
| 34 | |
| 35 | #define MAX_CNT 100000ll |
| 36 | |
Yonghong Song | 28dbf86 | 2018-04-28 22:28:13 -0700 | [diff] [blame] | 37 | static int print_bpf_output(void *data, int size) |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 38 | { |
| 39 | static __u64 cnt; |
| 40 | struct { |
| 41 | __u64 pid; |
| 42 | __u64 cookie; |
| 43 | } *e = data; |
| 44 | |
| 45 | if (e->cookie != 0x12345678) { |
| 46 | printf("BUG pid %llx cookie %llx sized %d\n", |
| 47 | e->pid, e->cookie, size); |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 48 | return LIBBPF_PERF_EVENT_ERROR; |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | cnt++; |
| 52 | |
| 53 | if (cnt == MAX_CNT) { |
| 54 | printf("recv %lld events per sec\n", |
| 55 | MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 56 | return LIBBPF_PERF_EVENT_DONE; |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 57 | } |
Yonghong Song | 28dbf86 | 2018-04-28 22:28:13 -0700 | [diff] [blame] | 58 | |
Jakub Kicinski | d0cabbb | 2018-05-10 10:24:40 -0700 | [diff] [blame] | 59 | return LIBBPF_PERF_EVENT_CONT; |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static void test_bpf_perf_event(void) |
| 63 | { |
| 64 | struct perf_event_attr attr = { |
| 65 | .sample_type = PERF_SAMPLE_RAW, |
| 66 | .type = PERF_TYPE_SOFTWARE, |
| 67 | .config = PERF_COUNT_SW_BPF_OUTPUT, |
| 68 | }; |
| 69 | int key = 0; |
| 70 | |
Joe Stringer | 205c8ad | 2016-12-08 18:46:19 -0800 | [diff] [blame] | 71 | pmu_fd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0); |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 72 | |
| 73 | assert(pmu_fd >= 0); |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 74 | assert(bpf_map_update_elem(map_fd[0], &key, &pmu_fd, BPF_ANY) == 0); |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 75 | ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0); |
| 76 | } |
| 77 | |
| 78 | int main(int argc, char **argv) |
| 79 | { |
| 80 | char filename[256]; |
| 81 | FILE *f; |
Yonghong Song | 28dbf86 | 2018-04-28 22:28:13 -0700 | [diff] [blame] | 82 | int ret; |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 83 | |
| 84 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
| 85 | |
| 86 | if (load_bpf_file(filename)) { |
| 87 | printf("%s", bpf_log_buf); |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | test_bpf_perf_event(); |
| 92 | |
| 93 | if (perf_event_mmap(pmu_fd) < 0) |
| 94 | return 1; |
| 95 | |
| 96 | f = popen("taskset 1 dd if=/dev/zero of=/dev/null", "r"); |
| 97 | (void) f; |
| 98 | |
| 99 | start_time = time_get_ns(); |
Yonghong Song | 28dbf86 | 2018-04-28 22:28:13 -0700 | [diff] [blame] | 100 | ret = perf_event_poller(pmu_fd, print_bpf_output); |
| 101 | kill(0, SIGINT); |
| 102 | return ret; |
Alexei Starovoitov | 3911169 | 2015-10-20 20:02:35 -0700 | [diff] [blame] | 103 | } |