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