1 // SPDX-License-Identifier: GPL-2.0-only
9 #include <linux/perf_event.h>
10 #include <linux/bpf.h>
13 #include <sys/syscall.h>
14 #include <sys/ioctl.h>
22 static __u64 time_get_ns(void)
26 clock_gettime(CLOCK_MONOTONIC, &ts);
27 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
30 static __u64 start_time;
33 #define MAX_CNT 100000ll
35 static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
42 if (e->cookie != 0x12345678) {
43 printf("BUG pid %llx cookie %llx sized %d\n",
44 e->pid, e->cookie, size);
51 printf("recv %lld events per sec\n",
52 MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
57 int main(int argc, char **argv)
59 struct perf_buffer_opts pb_opts = {};
60 struct perf_buffer *pb;
65 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
67 if (load_bpf_file(filename)) {
68 printf("%s", bpf_log_buf);
72 pb_opts.sample_cb = print_bpf_output;
73 pb = perf_buffer__new(map_fd[0], 8, &pb_opts);
74 ret = libbpf_get_error(pb);
76 printf("failed to setup perf_buffer: %d\n", ret);
80 f = popen("taskset 1 dd if=/dev/zero of=/dev/null", "r");
83 start_time = time_get_ns();
84 while ((ret = perf_buffer__poll(pb, 1000)) >= 0 && cnt < MAX_CNT) {