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.
12 #include <linux/perf_event.h>
13 #include <linux/bpf.h>
16 #include <sys/syscall.h>
17 #include <sys/ioctl.h>
24 #include "trace_helpers.h"
28 static __u64
time_get_ns(void)
32 clock_gettime(CLOCK_MONOTONIC
, &ts
);
33 return ts
.tv_sec
* 1000000000ull + ts
.tv_nsec
;
36 static __u64 start_time
;
38 #define MAX_CNT 100000ll
40 static int print_bpf_output(void *data
, int size
)
48 if (e
->cookie
!= 0x12345678) {
49 printf("BUG pid %llx cookie %llx sized %d\n",
50 e
->pid
, e
->cookie
, size
);
51 return LIBBPF_PERF_EVENT_ERROR
;
57 printf("recv %lld events per sec\n",
58 MAX_CNT
* 1000000000ll / (time_get_ns() - start_time
));
59 return LIBBPF_PERF_EVENT_DONE
;
62 return LIBBPF_PERF_EVENT_CONT
;
65 static void test_bpf_perf_event(void)
67 struct perf_event_attr attr
= {
68 .sample_type
= PERF_SAMPLE_RAW
,
69 .type
= PERF_TYPE_SOFTWARE
,
70 .config
= PERF_COUNT_SW_BPF_OUTPUT
,
74 pmu_fd
= sys_perf_event_open(&attr
, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
77 assert(bpf_map_update_elem(map_fd
[0], &key
, &pmu_fd
, BPF_ANY
) == 0);
78 ioctl(pmu_fd
, PERF_EVENT_IOC_ENABLE
, 0);
81 int main(int argc
, char **argv
)
87 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
89 if (load_bpf_file(filename
)) {
90 printf("%s", bpf_log_buf
);
94 test_bpf_perf_event();
96 if (perf_event_mmap(pmu_fd
) < 0)
99 f
= popen("taskset 1 dd if=/dev/zero of=/dev/null", "r");
102 start_time
= time_get_ns();
103 ret
= perf_event_poller(pmu_fd
, print_bpf_output
);