xref: /linux/samples/bpf/test_overhead_user.c (revision 18f90d372cf35b387663f1567de701e5393f6eb5)
1 /* Copyright (c) 2016 Facebook
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  */
7 #define _GNU_SOURCE
8 #include <sched.h>
9 #include <errno.h>
10 #include <stdio.h>
11 #include <sys/types.h>
12 #include <asm/unistd.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <assert.h>
16 #include <sys/wait.h>
17 #include <stdlib.h>
18 #include <signal.h>
19 #include <linux/bpf.h>
20 #include <string.h>
21 #include <time.h>
22 #include <sys/resource.h>
23 #include <bpf/bpf.h>
24 #include "bpf_load.h"
25 
26 #define MAX_CNT 1000000
27 
28 static __u64 time_get_ns(void)
29 {
30 	struct timespec ts;
31 
32 	clock_gettime(CLOCK_MONOTONIC, &ts);
33 	return ts.tv_sec * 1000000000ull + ts.tv_nsec;
34 }
35 
36 static void test_task_rename(int cpu)
37 {
38 	__u64 start_time;
39 	char buf[] = "test\n";
40 	int i, fd;
41 
42 	fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
43 	if (fd < 0) {
44 		printf("couldn't open /proc\n");
45 		exit(1);
46 	}
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));
51 			close(fd);
52 			return;
53 		}
54 	}
55 	printf("task_rename:%d: %lld events per sec\n",
56 	       cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
57 	close(fd);
58 }
59 
60 static void test_urandom_read(int cpu)
61 {
62 	__u64 start_time;
63 	char buf[4];
64 	int i, fd;
65 
66 	fd = open("/dev/urandom", O_RDONLY);
67 	if (fd < 0) {
68 		printf("couldn't open /dev/urandom\n");
69 		exit(1);
70 	}
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));
75 			close(fd);
76 			return;
77 		}
78 	}
79 	printf("urandom_read:%d: %lld events per sec\n",
80 	       cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
81 	close(fd);
82 }
83 
84 static void loop(int cpu, int flags)
85 {
86 	cpu_set_t cpuset;
87 
88 	CPU_ZERO(&cpuset);
89 	CPU_SET(cpu, &cpuset);
90 	sched_setaffinity(0, sizeof(cpuset), &cpuset);
91 
92 	if (flags & 1)
93 		test_task_rename(cpu);
94 	if (flags & 2)
95 		test_urandom_read(cpu);
96 }
97 
98 static void run_perf_test(int tasks, int flags)
99 {
100 	pid_t pid[tasks];
101 	int i;
102 
103 	for (i = 0; i < tasks; i++) {
104 		pid[i] = fork();
105 		if (pid[i] == 0) {
106 			loop(i, flags);
107 			exit(0);
108 		} else if (pid[i] == -1) {
109 			printf("couldn't spawn #%d process\n", i);
110 			exit(1);
111 		}
112 	}
113 	for (i = 0; i < tasks; i++) {
114 		int status;
115 
116 		assert(waitpid(pid[i], &status, 0) == pid[i]);
117 		assert(status == 0);
118 	}
119 }
120 
121 static void unload_progs(void)
122 {
123 	close(prog_fd[0]);
124 	close(prog_fd[1]);
125 	close(event_fd[0]);
126 	close(event_fd[1]);
127 }
128 
129 int main(int argc, char **argv)
130 {
131 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
132 	char filename[256];
133 	int num_cpu = 8;
134 	int test_flags = ~0;
135 
136 	setrlimit(RLIMIT_MEMLOCK, &r);
137 
138 	if (argc > 1)
139 		test_flags = atoi(argv[1]) ? : test_flags;
140 	if (argc > 2)
141 		num_cpu = atoi(argv[2]) ? : num_cpu;
142 
143 	if (test_flags & 0x3) {
144 		printf("BASE\n");
145 		run_perf_test(num_cpu, test_flags);
146 	}
147 
148 	if (test_flags & 0xC) {
149 		snprintf(filename, sizeof(filename),
150 			 "%s_kprobe_kern.o", argv[0]);
151 		if (load_bpf_file(filename)) {
152 			printf("%s", bpf_log_buf);
153 			return 1;
154 		}
155 		printf("w/KPROBE\n");
156 		run_perf_test(num_cpu, test_flags >> 2);
157 		unload_progs();
158 	}
159 
160 	if (test_flags & 0x30) {
161 		snprintf(filename, sizeof(filename),
162 			 "%s_tp_kern.o", argv[0]);
163 		if (load_bpf_file(filename)) {
164 			printf("%s", bpf_log_buf);
165 			return 1;
166 		}
167 		printf("w/TRACEPOINT\n");
168 		run_perf_test(num_cpu, test_flags >> 4);
169 		unload_progs();
170 	}
171 
172 	if (test_flags & 0xC0) {
173 		snprintf(filename, sizeof(filename),
174 			 "%s_raw_tp_kern.o", argv[0]);
175 		if (load_bpf_file(filename)) {
176 			printf("%s", bpf_log_buf);
177 			return 1;
178 		}
179 		printf("w/RAW_TRACEPOINT\n");
180 		run_perf_test(num_cpu, test_flags >> 6);
181 		unload_progs();
182 	}
183 
184 	return 0;
185 }
186