• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <stdbool.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <poll.h>
14 #include <sys/ioctl.h>
15 #include <linux/perf_event.h>
16 #include <linux/bpf.h>
17 #include <signal.h>
18 #include <assert.h>
19 #include <errno.h>
20 #include <sys/resource.h>
21 #include "libbpf.h"
22 #include "bpf_load.h"
23 #include "perf-sys.h"
24 
25 #define SAMPLE_FREQ 50
26 
27 static bool sys_read_seen, sys_write_seen;
28 
print_ksym(__u64 addr)29 static void print_ksym(__u64 addr)
30 {
31 	struct ksym *sym;
32 
33 	if (!addr)
34 		return;
35 	sym = ksym_search(addr);
36 	printf("%s;", sym->name);
37 	if (!strstr(sym->name, "sys_read"))
38 		sys_read_seen = true;
39 	else if (!strstr(sym->name, "sys_write"))
40 		sys_write_seen = true;
41 }
42 
print_addr(__u64 addr)43 static void print_addr(__u64 addr)
44 {
45 	if (!addr)
46 		return;
47 	printf("%llx;", addr);
48 }
49 
50 #define TASK_COMM_LEN 16
51 
52 struct key_t {
53 	char comm[TASK_COMM_LEN];
54 	__u32 kernstack;
55 	__u32 userstack;
56 };
57 
print_stack(struct key_t * key,__u64 count)58 static void print_stack(struct key_t *key, __u64 count)
59 {
60 	__u64 ip[PERF_MAX_STACK_DEPTH] = {};
61 	static bool warned;
62 	int i;
63 
64 	printf("%3lld %s;", count, key->comm);
65 	if (bpf_map_lookup_elem(map_fd[1], &key->kernstack, ip) != 0) {
66 		printf("---;");
67 	} else {
68 		for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
69 			print_ksym(ip[i]);
70 	}
71 	printf("-;");
72 	if (bpf_map_lookup_elem(map_fd[1], &key->userstack, ip) != 0) {
73 		printf("---;");
74 	} else {
75 		for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
76 			print_addr(ip[i]);
77 	}
78 	if (count < 6)
79 		printf("\r");
80 	else
81 		printf("\n");
82 
83 	if (key->kernstack == -EEXIST && !warned) {
84 		printf("stackmap collisions seen. Consider increasing size\n");
85 		warned = true;
86 	} else if ((int)key->kernstack < 0 && (int)key->userstack < 0) {
87 		printf("err stackid %d %d\n", key->kernstack, key->userstack);
88 	}
89 }
90 
int_exit(int sig)91 static void int_exit(int sig)
92 {
93 	kill(0, SIGKILL);
94 	exit(0);
95 }
96 
print_stacks(void)97 static void print_stacks(void)
98 {
99 	struct key_t key = {}, next_key;
100 	__u64 value;
101 	__u32 stackid = 0, next_id;
102 	int fd = map_fd[0], stack_map = map_fd[1];
103 
104 	sys_read_seen = sys_write_seen = false;
105 	while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
106 		bpf_map_lookup_elem(fd, &next_key, &value);
107 		print_stack(&next_key, value);
108 		bpf_map_delete_elem(fd, &next_key);
109 		key = next_key;
110 	}
111 	printf("\n");
112 	if (!sys_read_seen || !sys_write_seen) {
113 		printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n");
114 		int_exit(0);
115 	}
116 
117 	/* clear stack map */
118 	while (bpf_map_get_next_key(stack_map, &stackid, &next_id) == 0) {
119 		bpf_map_delete_elem(stack_map, &next_id);
120 		stackid = next_id;
121 	}
122 }
123 
generate_load(void)124 static inline int generate_load(void)
125 {
126 	if (system("dd if=/dev/zero of=/dev/null count=5000k status=none") < 0) {
127 		printf("failed to generate some load with dd: %s\n", strerror(errno));
128 		return -1;
129 	}
130 
131 	return 0;
132 }
133 
test_perf_event_all_cpu(struct perf_event_attr * attr)134 static void test_perf_event_all_cpu(struct perf_event_attr *attr)
135 {
136 	int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
137 	int *pmu_fd = malloc(nr_cpus * sizeof(int));
138 	int i, error = 0;
139 
140 	/* open perf_event on all cpus */
141 	for (i = 0; i < nr_cpus; i++) {
142 		pmu_fd[i] = sys_perf_event_open(attr, -1, i, -1, 0);
143 		if (pmu_fd[i] < 0) {
144 			printf("sys_perf_event_open failed\n");
145 			error = 1;
146 			goto all_cpu_err;
147 		}
148 		assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
149 		assert(ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE) == 0);
150 	}
151 
152 	if (generate_load() < 0) {
153 		error = 1;
154 		goto all_cpu_err;
155 	}
156 	print_stacks();
157 all_cpu_err:
158 	for (i--; i >= 0; i--) {
159 		ioctl(pmu_fd[i], PERF_EVENT_IOC_DISABLE);
160 		close(pmu_fd[i]);
161 	}
162 	free(pmu_fd);
163 	if (error)
164 		int_exit(0);
165 }
166 
test_perf_event_task(struct perf_event_attr * attr)167 static void test_perf_event_task(struct perf_event_attr *attr)
168 {
169 	int pmu_fd, error = 0;
170 
171 	/* open task bound event */
172 	pmu_fd = sys_perf_event_open(attr, 0, -1, -1, 0);
173 	if (pmu_fd < 0) {
174 		printf("sys_perf_event_open failed\n");
175 		int_exit(0);
176 	}
177 	assert(ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
178 	assert(ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE) == 0);
179 
180 	if (generate_load() < 0) {
181 		error = 1;
182 		goto err;
183 	}
184 	print_stacks();
185 err:
186 	ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE);
187 	close(pmu_fd);
188 	if (error)
189 		int_exit(0);
190 }
191 
test_bpf_perf_event(void)192 static void test_bpf_perf_event(void)
193 {
194 	struct perf_event_attr attr_type_hw = {
195 		.sample_freq = SAMPLE_FREQ,
196 		.freq = 1,
197 		.type = PERF_TYPE_HARDWARE,
198 		.config = PERF_COUNT_HW_CPU_CYCLES,
199 		.inherit = 1,
200 	};
201 	struct perf_event_attr attr_type_sw = {
202 		.sample_freq = SAMPLE_FREQ,
203 		.freq = 1,
204 		.type = PERF_TYPE_SOFTWARE,
205 		.config = PERF_COUNT_SW_CPU_CLOCK,
206 		.inherit = 1,
207 	};
208 	struct perf_event_attr attr_hw_cache_l1d = {
209 		.sample_freq = SAMPLE_FREQ,
210 		.freq = 1,
211 		.type = PERF_TYPE_HW_CACHE,
212 		.config =
213 			PERF_COUNT_HW_CACHE_L1D |
214 			(PERF_COUNT_HW_CACHE_OP_READ << 8) |
215 			(PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
216 		.inherit = 1,
217 	};
218 	struct perf_event_attr attr_hw_cache_branch_miss = {
219 		.sample_freq = SAMPLE_FREQ,
220 		.freq = 1,
221 		.type = PERF_TYPE_HW_CACHE,
222 		.config =
223 			PERF_COUNT_HW_CACHE_BPU |
224 			(PERF_COUNT_HW_CACHE_OP_READ << 8) |
225 			(PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
226 		.inherit = 1,
227 	};
228 	struct perf_event_attr attr_type_raw = {
229 		.sample_freq = SAMPLE_FREQ,
230 		.freq = 1,
231 		.type = PERF_TYPE_RAW,
232 		/* Intel Instruction Retired */
233 		.config = 0xc0,
234 		.inherit = 1,
235 	};
236 
237 	printf("Test HW_CPU_CYCLES\n");
238 	test_perf_event_all_cpu(&attr_type_hw);
239 	test_perf_event_task(&attr_type_hw);
240 
241 	printf("Test SW_CPU_CLOCK\n");
242 	test_perf_event_all_cpu(&attr_type_sw);
243 	test_perf_event_task(&attr_type_sw);
244 
245 	printf("Test HW_CACHE_L1D\n");
246 	test_perf_event_all_cpu(&attr_hw_cache_l1d);
247 	test_perf_event_task(&attr_hw_cache_l1d);
248 
249 	printf("Test HW_CACHE_BPU\n");
250 	test_perf_event_all_cpu(&attr_hw_cache_branch_miss);
251 	test_perf_event_task(&attr_hw_cache_branch_miss);
252 
253 	printf("Test Instruction Retired\n");
254 	test_perf_event_all_cpu(&attr_type_raw);
255 	test_perf_event_task(&attr_type_raw);
256 
257 	printf("*** PASS ***\n");
258 }
259 
260 
main(int argc,char ** argv)261 int main(int argc, char **argv)
262 {
263 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
264 	char filename[256];
265 
266 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
267 	setrlimit(RLIMIT_MEMLOCK, &r);
268 
269 	signal(SIGINT, int_exit);
270 	signal(SIGTERM, int_exit);
271 
272 	if (load_kallsyms()) {
273 		printf("failed to process /proc/kallsyms\n");
274 		return 1;
275 	}
276 
277 	if (load_bpf_file(filename)) {
278 		printf("%s", bpf_log_buf);
279 		return 2;
280 	}
281 
282 	if (fork() == 0) {
283 		read_trace_pipe();
284 		return 0;
285 	}
286 	test_bpf_perf_event();
287 	int_exit(0);
288 	return 0;
289 }
290