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 (!ASSERT_OK_PTR(obj, "obj_open_file"))
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 	err = bpf_object__load(obj);
100 	if (CHECK(err, "obj_load", "err %d\n", err))
101 		goto cleanup;
102 
103 	setaffinity();
104 
105 	/* base line run */
106 	test_run("base");
107 
108 	/* attach kprobe */
109 	link = bpf_program__attach_kprobe(kprobe_prog, false /* retprobe */,
110 					  kprobe_func);
111 	if (!ASSERT_OK_PTR(link, "attach_kprobe"))
112 		goto cleanup;
113 	test_run("kprobe");
114 	bpf_link__destroy(link);
115 
116 	/* attach kretprobe */
117 	link = bpf_program__attach_kprobe(kretprobe_prog, true /* retprobe */,
118 					  kprobe_func);
119 	if (!ASSERT_OK_PTR(link, "attach_kretprobe"))
120 		goto cleanup;
121 	test_run("kretprobe");
122 	bpf_link__destroy(link);
123 
124 	/* attach raw_tp */
125 	link = bpf_program__attach_raw_tracepoint(raw_tp_prog, "task_rename");
126 	if (!ASSERT_OK_PTR(link, "attach_raw_tp"))
127 		goto cleanup;
128 	test_run("raw_tp");
129 	bpf_link__destroy(link);
130 
131 	/* attach fentry */
132 	link = bpf_program__attach_trace(fentry_prog);
133 	if (!ASSERT_OK_PTR(link, "attach_fentry"))
134 		goto cleanup;
135 	test_run("fentry");
136 	bpf_link__destroy(link);
137 
138 	/* attach fexit */
139 	link = bpf_program__attach_trace(fexit_prog);
140 	if (!ASSERT_OK_PTR(link, "attach_fexit"))
141 		goto cleanup;
142 	test_run("fexit");
143 	bpf_link__destroy(link);
144 
145 cleanup:
146 	prctl(PR_SET_NAME, comm, 0L, 0L, 0L);
147 	bpf_object__close(obj);
148 }
149