^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) static __u64 time_get_ns(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) struct timespec ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) clock_gettime(CLOCK_MONOTONIC, &ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) return ts.tv_sec * 1000000000ull + ts.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static __u64 start_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static __u64 cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define MAX_CNT 100000ll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) __u64 pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) __u64 cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) } *e = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (e->cookie != 0x12345678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) printf("BUG pid %llx cookie %llx sized %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) e->pid, e->cookie, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (cnt == MAX_CNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) printf("recv %lld events per sec\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct perf_buffer_opts pb_opts = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct bpf_link *link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct bpf_program *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct perf_buffer *pb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int map_fd, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) char filename[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) obj = bpf_object__open_file(filename, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (libbpf_get_error(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) fprintf(stderr, "ERROR: opening BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* load BPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (bpf_object__load(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) fprintf(stderr, "ERROR: loading BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (map_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) fprintf(stderr, "ERROR: finding a map in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (libbpf_get_error(prog)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) link = bpf_program__attach(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (libbpf_get_error(link)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) fprintf(stderr, "ERROR: bpf_program__attach failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) pb_opts.sample_cb = print_bpf_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) pb = perf_buffer__new(map_fd, 8, &pb_opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ret = libbpf_get_error(pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) printf("failed to setup perf_buffer: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) f = popen("taskset 1 dd if=/dev/zero of=/dev/null", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) (void) f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) start_time = time_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) while ((ret = perf_buffer__poll(pb, 1000)) >= 0 && cnt < MAX_CNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) kill(0, SIGINT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) bpf_link__destroy(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) bpf_object__close(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }