1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
9 #include <asm/unistd.h>
14 #include <sys/socket.h>
15 #include <arpa/inet.h>
18 #include <linux/bpf.h>
22 #include <bpf/libbpf.h>
24 #define MAX_CNT 1000000
25 #define DUMMY_IP "127.0.0.1"
28 static struct bpf_link *links[2];
29 static struct bpf_object *obj;
32 static __u64 time_get_ns(void)
36 clock_gettime(CLOCK_MONOTONIC, &ts);
37 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
40 static void test_task_rename(int cpu)
42 char buf[] = "test\n";
46 fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
48 printf("couldn't open /proc\n");
51 start_time = time_get_ns();
52 for (i = 0; i < MAX_CNT; i++) {
53 if (write(fd, buf, sizeof(buf)) < 0) {
54 printf("task rename failed: %s\n", strerror(errno));
59 printf("task_rename:%d: %lld events per sec\n",
60 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
64 static void test_fib_table_lookup(int cpu)
66 struct sockaddr_in addr;
67 char buf[] = "test\n";
71 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
73 printf("couldn't open socket\n");
76 memset((char *)&addr, 0, sizeof(addr));
77 addr.sin_addr.s_addr = inet_addr(DUMMY_IP);
78 addr.sin_port = htons(DUMMY_PORT);
79 addr.sin_family = AF_INET;
80 start_time = time_get_ns();
81 for (i = 0; i < MAX_CNT; i++) {
82 if (sendto(fd, buf, strlen(buf), 0,
83 (struct sockaddr *)&addr, sizeof(addr)) < 0) {
84 printf("failed to start ping: %s\n", strerror(errno));
89 printf("fib_table_lookup:%d: %lld events per sec\n",
90 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
94 static void loop(int cpu, int flags)
99 CPU_SET(cpu, &cpuset);
100 sched_setaffinity(0, sizeof(cpuset), &cpuset);
103 test_task_rename(cpu);
105 test_fib_table_lookup(cpu);
108 static void run_perf_test(int tasks, int flags)
113 for (i = 0; i < tasks; i++) {
118 } else if (pid[i] == -1) {
119 printf("couldn't spawn #%d process\n", i);
123 for (i = 0; i < tasks; i++) {
126 assert(waitpid(pid[i], &status, 0) == pid[i]);
131 static int load_progs(char *filename)
133 struct bpf_program *prog;
136 obj = bpf_object__open_file(filename, NULL);
137 err = libbpf_get_error(obj);
139 fprintf(stderr, "ERROR: opening BPF object file failed\n");
143 /* load BPF program */
144 err = bpf_object__load(obj);
146 fprintf(stderr, "ERROR: loading BPF object file failed\n");
150 bpf_object__for_each_program(prog, obj) {
151 links[cnt] = bpf_program__attach(prog);
152 err = libbpf_get_error(links[cnt]);
154 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
164 static void unload_progs(void)
167 bpf_link__destroy(links[--cnt]);
169 bpf_object__close(obj);
172 int main(int argc, char **argv)
174 int num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
181 test_flags = atoi(argv[1]) ? : test_flags;
183 num_cpu = atoi(argv[2]) ? : num_cpu;
185 if (test_flags & 0x3) {
187 run_perf_test(num_cpu, test_flags);
190 if (test_flags & 0xC) {
191 snprintf(filename, sizeof(filename),
192 "%s_kprobe.bpf.o", argv[0]);
194 printf("w/KPROBE\n");
195 err = load_progs(filename);
197 run_perf_test(num_cpu, test_flags >> 2);
202 if (test_flags & 0x30) {
203 snprintf(filename, sizeof(filename),
204 "%s_tp.bpf.o", argv[0]);
205 printf("w/TRACEPOINT\n");
206 err = load_progs(filename);
208 run_perf_test(num_cpu, test_flags >> 4);
213 if (test_flags & 0xC0) {
214 snprintf(filename, sizeof(filename),
215 "%s_raw_tp.bpf.o", argv[0]);
216 printf("w/RAW_TRACEPOINT\n");
217 err = load_progs(filename);
219 run_perf_test(num_cpu, test_flags >> 6);