1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
9 #include <asm/unistd.h>
16 #include <linux/bpf.h>
19 #include <sys/resource.h>
23 #define MAX_CNT 1000000
25 static __u64
time_get_ns(void)
29 clock_gettime(CLOCK_MONOTONIC
, &ts
);
30 return ts
.tv_sec
* 1000000000ull + ts
.tv_nsec
;
33 static void test_task_rename(int cpu
)
36 char buf
[] = "test\n";
39 fd
= open("/proc/self/comm", O_WRONLY
|O_TRUNC
);
41 printf("couldn't open /proc\n");
44 start_time
= time_get_ns();
45 for (i
= 0; i
< MAX_CNT
; i
++) {
46 if (write(fd
, buf
, sizeof(buf
)) < 0) {
47 printf("task rename failed: %s\n", strerror(errno
));
52 printf("task_rename:%d: %lld events per sec\n",
53 cpu
, MAX_CNT
* 1000000000ll / (time_get_ns() - start_time
));
57 static void test_urandom_read(int cpu
)
63 fd
= open("/dev/urandom", O_RDONLY
);
65 printf("couldn't open /dev/urandom\n");
68 start_time
= time_get_ns();
69 for (i
= 0; i
< MAX_CNT
; i
++) {
70 if (read(fd
, buf
, sizeof(buf
)) < 0) {
71 printf("failed to read from /dev/urandom: %s\n", strerror(errno
));
76 printf("urandom_read:%d: %lld events per sec\n",
77 cpu
, MAX_CNT
* 1000000000ll / (time_get_ns() - start_time
));
81 static void loop(int cpu
, int flags
)
86 CPU_SET(cpu
, &cpuset
);
87 sched_setaffinity(0, sizeof(cpuset
), &cpuset
);
90 test_task_rename(cpu
);
92 test_urandom_read(cpu
);
95 static void run_perf_test(int tasks
, int flags
)
100 for (i
= 0; i
< tasks
; i
++) {
105 } else if (pid
[i
] == -1) {
106 printf("couldn't spawn #%d process\n", i
);
110 for (i
= 0; i
< tasks
; i
++) {
113 assert(waitpid(pid
[i
], &status
, 0) == pid
[i
]);
118 static void unload_progs(void)
126 int main(int argc
, char **argv
)
128 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
133 setrlimit(RLIMIT_MEMLOCK
, &r
);
136 test_flags
= atoi(argv
[1]) ? : test_flags
;
138 num_cpu
= atoi(argv
[2]) ? : num_cpu
;
140 if (test_flags
& 0x3) {
142 run_perf_test(num_cpu
, test_flags
);
145 if (test_flags
& 0xC) {
146 snprintf(filename
, sizeof(filename
),
147 "%s_kprobe_kern.o", argv
[0]);
148 if (load_bpf_file(filename
)) {
149 printf("%s", bpf_log_buf
);
152 printf("w/KPROBE\n");
153 run_perf_test(num_cpu
, test_flags
>> 2);
157 if (test_flags
& 0x30) {
158 snprintf(filename
, sizeof(filename
),
159 "%s_tp_kern.o", argv
[0]);
160 if (load_bpf_file(filename
)) {
161 printf("%s", bpf_log_buf
);
164 printf("w/TRACEPOINT\n");
165 run_perf_test(num_cpu
, test_flags
>> 4);
169 if (test_flags
& 0xC0) {
170 snprintf(filename
, sizeof(filename
),
171 "%s_raw_tp_kern.o", argv
[0]);
172 if (load_bpf_file(filename
)) {
173 printf("%s", bpf_log_buf
);
176 printf("w/RAW_TRACEPOINT\n");
177 run_perf_test(num_cpu
, test_flags
>> 6);