1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2019 Facebook */
6 #include <test_progs.h>
10 static __u64
time_get_ns(void)
14 clock_gettime(CLOCK_MONOTONIC
, &ts
);
15 return ts
.tv_sec
* 1000000000ull + ts
.tv_nsec
;
18 static int test_task_rename(const char *prog
)
20 int i
, fd
, duration
= 0, err
;
21 char buf
[] = "test_overhead";
24 fd
= open("/proc/self/comm", O_WRONLY
|O_TRUNC
);
25 if (CHECK(fd
< 0, "open /proc", "err %d", errno
))
27 start_time
= time_get_ns();
28 for (i
= 0; i
< MAX_CNT
; i
++) {
29 err
= write(fd
, buf
, sizeof(buf
));
31 CHECK(err
< 0, "task rename", "err %d", errno
);
36 printf("task_rename %s\t%lluK events per sec\n", prog
,
37 MAX_CNT
* 1000000ll / (time_get_ns() - start_time
));
42 static void test_run(const char *prog
)
44 test_task_rename(prog
);
47 static void setaffinity(void)
53 CPU_SET(cpu
, &cpuset
);
54 sched_setaffinity(0, sizeof(cpuset
), &cpuset
);
57 void test_test_overhead(void)
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;
72 if (CHECK_FAIL(prctl(PR_GET_NAME
, comm
, 0L, 0L, 0L)))
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
)))
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
))
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
))
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
))
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
))
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
))
99 err
= bpf_object__load(obj
);
100 if (CHECK(err
, "obj_load", "err %d\n", err
))
109 link
= bpf_program__attach_kprobe(kprobe_prog
, false /* retprobe */,
111 if (CHECK(IS_ERR(link
), "attach_kprobe", "err %ld\n", PTR_ERR(link
)))
114 bpf_link__destroy(link
);
116 /* attach kretprobe */
117 link
= bpf_program__attach_kprobe(kretprobe_prog
, true /* retprobe */,
119 if (CHECK(IS_ERR(link
), "attach kretprobe", "err %ld\n", PTR_ERR(link
)))
121 test_run("kretprobe");
122 bpf_link__destroy(link
);
125 link
= bpf_program__attach_raw_tracepoint(raw_tp_prog
, "task_rename");
126 if (CHECK(IS_ERR(link
), "attach fentry", "err %ld\n", PTR_ERR(link
)))
129 bpf_link__destroy(link
);
132 link
= bpf_program__attach_trace(fentry_prog
);
133 if (CHECK(IS_ERR(link
), "attach fentry", "err %ld\n", PTR_ERR(link
)))
136 bpf_link__destroy(link
);
139 link
= bpf_program__attach_trace(fexit_prog
);
140 if (CHECK(IS_ERR(link
), "attach fexit", "err %ld\n", PTR_ERR(link
)))
143 bpf_link__destroy(link
);
146 prctl(PR_SET_NAME
, comm
, 0L, 0L, 0L);
147 bpf_object__close(obj
);