1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2019 Facebook */ 3 #define _GNU_SOURCE 4 #include <sched.h> 5 #include <sys/prctl.h> 6 #include <test_progs.h> 7 8 #define MAX_CNT 100000 9 10 static __u64 time_get_ns(void) 11 { 12 struct timespec ts; 13 14 clock_gettime(CLOCK_MONOTONIC, &ts); 15 return ts.tv_sec * 1000000000ull + ts.tv_nsec; 16 } 17 18 static int test_task_rename(const char *prog) 19 { 20 int i, fd, duration = 0, err; 21 char buf[] = "test_overhead"; 22 __u64 start_time; 23 24 fd = open("/proc/self/comm", O_WRONLY|O_TRUNC); 25 if (CHECK(fd < 0, "open /proc", "err %d", errno)) 26 return -1; 27 start_time = time_get_ns(); 28 for (i = 0; i < MAX_CNT; i++) { 29 err = write(fd, buf, sizeof(buf)); 30 if (err < 0) { 31 CHECK(err < 0, "task rename", "err %d", errno); 32 close(fd); 33 return -1; 34 } 35 } 36 printf("task_rename %s\t%lluK events per sec\n", prog, 37 MAX_CNT * 1000000ll / (time_get_ns() - start_time)); 38 close(fd); 39 return 0; 40 } 41 42 static void test_run(const char *prog) 43 { 44 test_task_rename(prog); 45 } 46 47 static void setaffinity(void) 48 { 49 cpu_set_t cpuset; 50 int cpu = 0; 51 52 CPU_ZERO(&cpuset); 53 CPU_SET(cpu, &cpuset); 54 sched_setaffinity(0, sizeof(cpuset), &cpuset); 55 } 56 57 void test_test_overhead(void) 58 { 59 const char *kprobe_name = "kprobe/__set_task_comm"; 60 const char *kretprobe_name = "kretprobe/__set_task_comm"; 61 const char *raw_tp_name = "raw_tp/task_rename"; 62 const char *fentry_name = "fentry/__set_task_comm"; 63 const char *fexit_name = "fexit/__set_task_comm"; 64 const char *kprobe_func = "__set_task_comm"; 65 struct bpf_program *kprobe_prog, *kretprobe_prog, *raw_tp_prog; 66 struct bpf_program *fentry_prog, *fexit_prog; 67 struct bpf_object *obj; 68 struct bpf_link *link; 69 int err, duration = 0; 70 char comm[16] = {}; 71 72 if (CHECK_FAIL(prctl(PR_GET_NAME, comm, 0L, 0L, 0L))) 73 return; 74 75 obj = bpf_object__open_file("./test_overhead.o", NULL); 76 if (CHECK(IS_ERR(obj), "obj_open_file", "err %ld\n", PTR_ERR(obj))) 77 return; 78 79 kprobe_prog = bpf_object__find_program_by_title(obj, kprobe_name); 80 if (CHECK(!kprobe_prog, "find_probe", 81 "prog '%s' not found\n", kprobe_name)) 82 goto cleanup; 83 kretprobe_prog = bpf_object__find_program_by_title(obj, kretprobe_name); 84 if (CHECK(!kretprobe_prog, "find_probe", 85 "prog '%s' not found\n", kretprobe_name)) 86 goto cleanup; 87 raw_tp_prog = bpf_object__find_program_by_title(obj, raw_tp_name); 88 if (CHECK(!raw_tp_prog, "find_probe", 89 "prog '%s' not found\n", raw_tp_name)) 90 goto cleanup; 91 fentry_prog = bpf_object__find_program_by_title(obj, fentry_name); 92 if (CHECK(!fentry_prog, "find_probe", 93 "prog '%s' not found\n", fentry_name)) 94 goto cleanup; 95 fexit_prog = bpf_object__find_program_by_title(obj, fexit_name); 96 if (CHECK(!fexit_prog, "find_probe", 97 "prog '%s' not found\n", fexit_name)) 98 goto cleanup; 99 100 err = bpf_object__load(obj); 101 if (CHECK(err, "obj_load", "err %d\n", err)) 102 goto cleanup; 103 104 setaffinity(); 105 106 /* base line run */ 107 test_run("base"); 108 109 /* attach kprobe */ 110 link = bpf_program__attach_kprobe(kprobe_prog, false /* retprobe */, 111 kprobe_func); 112 if (CHECK(IS_ERR(link), "attach_kprobe", "err %ld\n", PTR_ERR(link))) 113 goto cleanup; 114 test_run("kprobe"); 115 bpf_link__destroy(link); 116 117 /* attach kretprobe */ 118 link = bpf_program__attach_kprobe(kretprobe_prog, true /* retprobe */, 119 kprobe_func); 120 if (CHECK(IS_ERR(link), "attach kretprobe", "err %ld\n", PTR_ERR(link))) 121 goto cleanup; 122 test_run("kretprobe"); 123 bpf_link__destroy(link); 124 125 /* attach raw_tp */ 126 link = bpf_program__attach_raw_tracepoint(raw_tp_prog, "task_rename"); 127 if (CHECK(IS_ERR(link), "attach fentry", "err %ld\n", PTR_ERR(link))) 128 goto cleanup; 129 test_run("raw_tp"); 130 bpf_link__destroy(link); 131 132 /* attach fentry */ 133 link = bpf_program__attach_trace(fentry_prog); 134 if (CHECK(IS_ERR(link), "attach fentry", "err %ld\n", PTR_ERR(link))) 135 goto cleanup; 136 test_run("fentry"); 137 bpf_link__destroy(link); 138 139 /* attach fexit */ 140 link = bpf_program__attach_trace(fexit_prog); 141 if (CHECK(IS_ERR(link), "attach fexit", "err %ld\n", PTR_ERR(link))) 142 goto cleanup; 143 test_run("fexit"); 144 bpf_link__destroy(link); 145 cleanup: 146 prctl(PR_SET_NAME, comm, 0L, 0L, 0L); 147 bpf_object__close(obj); 148 } 149