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 *fmodret_name = "fmod_ret/__set_task_comm"; 65 const char *kprobe_func = "__set_task_comm"; 66 struct bpf_program *kprobe_prog, *kretprobe_prog, *raw_tp_prog; 67 struct bpf_program *fentry_prog, *fexit_prog, *fmodret_prog; 68 struct bpf_object *obj; 69 struct bpf_link *link; 70 int err, duration = 0; 71 char comm[16] = {}; 72 73 if (CHECK_FAIL(prctl(PR_GET_NAME, comm, 0L, 0L, 0L))) 74 return; 75 76 obj = bpf_object__open_file("./test_overhead.o", NULL); 77 if (CHECK(IS_ERR(obj), "obj_open_file", "err %ld\n", PTR_ERR(obj))) 78 return; 79 80 kprobe_prog = bpf_object__find_program_by_title(obj, kprobe_name); 81 if (CHECK(!kprobe_prog, "find_probe", 82 "prog '%s' not found\n", kprobe_name)) 83 goto cleanup; 84 kretprobe_prog = bpf_object__find_program_by_title(obj, kretprobe_name); 85 if (CHECK(!kretprobe_prog, "find_probe", 86 "prog '%s' not found\n", kretprobe_name)) 87 goto cleanup; 88 raw_tp_prog = bpf_object__find_program_by_title(obj, raw_tp_name); 89 if (CHECK(!raw_tp_prog, "find_probe", 90 "prog '%s' not found\n", raw_tp_name)) 91 goto cleanup; 92 fentry_prog = bpf_object__find_program_by_title(obj, fentry_name); 93 if (CHECK(!fentry_prog, "find_probe", 94 "prog '%s' not found\n", fentry_name)) 95 goto cleanup; 96 fexit_prog = bpf_object__find_program_by_title(obj, fexit_name); 97 if (CHECK(!fexit_prog, "find_probe", 98 "prog '%s' not found\n", fexit_name)) 99 goto cleanup; 100 fmodret_prog = bpf_object__find_program_by_title(obj, fmodret_name); 101 if (CHECK(!fmodret_prog, "find_probe", 102 "prog '%s' not found\n", fmodret_name)) 103 goto cleanup; 104 105 err = bpf_object__load(obj); 106 if (CHECK(err, "obj_load", "err %d\n", err)) 107 goto cleanup; 108 109 setaffinity(); 110 111 /* base line run */ 112 test_run("base"); 113 114 /* attach kprobe */ 115 link = bpf_program__attach_kprobe(kprobe_prog, false /* retprobe */, 116 kprobe_func); 117 if (CHECK(IS_ERR(link), "attach_kprobe", "err %ld\n", PTR_ERR(link))) 118 goto cleanup; 119 test_run("kprobe"); 120 bpf_link__destroy(link); 121 122 /* attach kretprobe */ 123 link = bpf_program__attach_kprobe(kretprobe_prog, true /* retprobe */, 124 kprobe_func); 125 if (CHECK(IS_ERR(link), "attach kretprobe", "err %ld\n", PTR_ERR(link))) 126 goto cleanup; 127 test_run("kretprobe"); 128 bpf_link__destroy(link); 129 130 /* attach raw_tp */ 131 link = bpf_program__attach_raw_tracepoint(raw_tp_prog, "task_rename"); 132 if (CHECK(IS_ERR(link), "attach fentry", "err %ld\n", PTR_ERR(link))) 133 goto cleanup; 134 test_run("raw_tp"); 135 bpf_link__destroy(link); 136 137 /* attach fentry */ 138 link = bpf_program__attach_trace(fentry_prog); 139 if (CHECK(IS_ERR(link), "attach fentry", "err %ld\n", PTR_ERR(link))) 140 goto cleanup; 141 test_run("fentry"); 142 bpf_link__destroy(link); 143 144 /* attach fexit */ 145 link = bpf_program__attach_trace(fexit_prog); 146 if (CHECK(IS_ERR(link), "attach fexit", "err %ld\n", PTR_ERR(link))) 147 goto cleanup; 148 test_run("fexit"); 149 bpf_link__destroy(link); 150 151 /* attach fmod_ret */ 152 link = bpf_program__attach_trace(fmodret_prog); 153 if (CHECK(IS_ERR(link), "attach fmod_ret", "err %ld\n", PTR_ERR(link))) 154 goto cleanup; 155 test_run("fmod_ret"); 156 bpf_link__destroy(link); 157 cleanup: 158 prctl(PR_SET_NAME, comm, 0L, 0L, 0L); 159 bpf_object__close(obj); 160 } 161