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