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>
21 #include <bpf/libbpf.h>
23 #define MAX_CNT 1000000
25 static struct bpf_link *links[2];
26 static struct bpf_object *obj;
29 static __u64 time_get_ns(void)
33 clock_gettime(CLOCK_MONOTONIC, &ts);
34 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
37 static void test_task_rename(int cpu)
40 char buf[] = "test\n";
43 fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
45 printf("couldn't open /proc\n");
48 start_time = time_get_ns();
49 for (i = 0; i < MAX_CNT; i++) {
50 if (write(fd, buf, sizeof(buf)) < 0) {
51 printf("task rename failed: %s\n", strerror(errno));
56 printf("task_rename:%d: %lld events per sec\n",
57 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
61 static void test_urandom_read(int cpu)
67 fd = open("/dev/urandom", O_RDONLY);
69 printf("couldn't open /dev/urandom\n");
72 start_time = time_get_ns();
73 for (i = 0; i < MAX_CNT; i++) {
74 if (read(fd, buf, sizeof(buf)) < 0) {
75 printf("failed to read from /dev/urandom: %s\n", strerror(errno));
80 printf("urandom_read:%d: %lld events per sec\n",
81 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
85 static void loop(int cpu, int flags)
90 CPU_SET(cpu, &cpuset);
91 sched_setaffinity(0, sizeof(cpuset), &cpuset);
94 test_task_rename(cpu);
96 test_urandom_read(cpu);
99 static void run_perf_test(int tasks, int flags)
104 for (i = 0; i < tasks; i++) {
109 } else if (pid[i] == -1) {
110 printf("couldn't spawn #%d process\n", i);
114 for (i = 0; i < tasks; i++) {
117 assert(waitpid(pid[i], &status, 0) == pid[i]);
122 static int load_progs(char *filename)
124 struct bpf_program *prog;
127 obj = bpf_object__open_file(filename, NULL);
128 err = libbpf_get_error(obj);
130 fprintf(stderr, "ERROR: opening BPF object file failed\n");
134 /* load BPF program */
135 err = bpf_object__load(obj);
137 fprintf(stderr, "ERROR: loading BPF object file failed\n");
141 bpf_object__for_each_program(prog, obj) {
142 links[cnt] = bpf_program__attach(prog);
143 err = libbpf_get_error(links[cnt]);
145 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
155 static void unload_progs(void)
158 bpf_link__destroy(links[--cnt]);
160 bpf_object__close(obj);
163 int main(int argc, char **argv)
165 int num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
172 test_flags = atoi(argv[1]) ? : test_flags;
174 num_cpu = atoi(argv[2]) ? : num_cpu;
176 if (test_flags & 0x3) {
178 run_perf_test(num_cpu, test_flags);
181 if (test_flags & 0xC) {
182 snprintf(filename, sizeof(filename),
183 "%s_kprobe_kern.o", argv[0]);
185 printf("w/KPROBE\n");
186 err = load_progs(filename);
188 run_perf_test(num_cpu, test_flags >> 2);
193 if (test_flags & 0x30) {
194 snprintf(filename, sizeof(filename),
195 "%s_tp_kern.o", argv[0]);
196 printf("w/TRACEPOINT\n");
197 err = load_progs(filename);
199 run_perf_test(num_cpu, test_flags >> 4);
204 if (test_flags & 0xC0) {
205 snprintf(filename, sizeof(filename),
206 "%s_raw_tp_kern.o", argv[0]);
207 printf("w/RAW_TRACEPOINT\n");
208 err = load_progs(filename);
210 run_perf_test(num_cpu, test_flags >> 6);