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