1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
9 #include <asm/unistd.h>
16 #include <linux/bpf.h>
20 #include <bpf/libbpf.h>
22 #define MAX_CNT 1000000
24 static struct bpf_link *links[2];
25 static struct bpf_object *obj;
28 static __u64 time_get_ns(void)
32 clock_gettime(CLOCK_MONOTONIC, &ts);
33 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
36 static void test_task_rename(int cpu)
39 char buf[] = "test\n";
42 fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
44 printf("couldn't open /proc\n");
47 start_time = time_get_ns();
48 for (i = 0; i < MAX_CNT; i++) {
49 if (write(fd, buf, sizeof(buf)) < 0) {
50 printf("task rename failed: %s\n", strerror(errno));
55 printf("task_rename:%d: %lld events per sec\n",
56 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
60 static void test_urandom_read(int cpu)
66 fd = open("/dev/urandom", O_RDONLY);
68 printf("couldn't open /dev/urandom\n");
71 start_time = time_get_ns();
72 for (i = 0; i < MAX_CNT; i++) {
73 if (read(fd, buf, sizeof(buf)) < 0) {
74 printf("failed to read from /dev/urandom: %s\n", strerror(errno));
79 printf("urandom_read:%d: %lld events per sec\n",
80 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
84 static void loop(int cpu, int flags)
89 CPU_SET(cpu, &cpuset);
90 sched_setaffinity(0, sizeof(cpuset), &cpuset);
93 test_task_rename(cpu);
95 test_urandom_read(cpu);
98 static void run_perf_test(int tasks, int flags)
103 for (i = 0; i < tasks; i++) {
108 } else if (pid[i] == -1) {
109 printf("couldn't spawn #%d process\n", i);
113 for (i = 0; i < tasks; i++) {
116 assert(waitpid(pid[i], &status, 0) == pid[i]);
121 static int load_progs(char *filename)
123 struct bpf_program *prog;
126 obj = bpf_object__open_file(filename, NULL);
127 err = libbpf_get_error(obj);
129 fprintf(stderr, "ERROR: opening BPF object file failed\n");
133 /* load BPF program */
134 err = bpf_object__load(obj);
136 fprintf(stderr, "ERROR: loading BPF object file failed\n");
140 bpf_object__for_each_program(prog, obj) {
141 links[cnt] = bpf_program__attach(prog);
142 err = libbpf_get_error(links[cnt]);
144 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
154 static void unload_progs(void)
157 bpf_link__destroy(links[--cnt]);
159 bpf_object__close(obj);
162 int main(int argc, char **argv)
164 int num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
171 test_flags = atoi(argv[1]) ? : test_flags;
173 num_cpu = atoi(argv[2]) ? : num_cpu;
175 if (test_flags & 0x3) {
177 run_perf_test(num_cpu, test_flags);
180 if (test_flags & 0xC) {
181 snprintf(filename, sizeof(filename),
182 "%s_kprobe_kern.o", argv[0]);
184 printf("w/KPROBE\n");
185 err = load_progs(filename);
187 run_perf_test(num_cpu, test_flags >> 2);
192 if (test_flags & 0x30) {
193 snprintf(filename, sizeof(filename),
194 "%s_tp_kern.o", argv[0]);
195 printf("w/TRACEPOINT\n");
196 err = load_progs(filename);
198 run_perf_test(num_cpu, test_flags >> 4);
203 if (test_flags & 0xC0) {
204 snprintf(filename, sizeof(filename),
205 "%s_raw_tp_kern.o", argv[0]);
206 printf("w/RAW_TRACEPOINT\n");
207 err = load_progs(filename);
209 run_perf_test(num_cpu, test_flags >> 6);