1 /* Copyright (c) 2016 Facebook
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.
14 #include <sys/ioctl.h>
15 #include <linux/perf_event.h>
16 #include <linux/bpf.h>
20 #include <sys/resource.h>
24 #include "trace_helpers.h"
26 #define SAMPLE_FREQ 50
28 static bool sys_read_seen, sys_write_seen;
30 static void print_ksym(__u64 addr)
36 sym = ksym_search(addr);
38 printf("ksym not found. Is kallsyms loaded?\n");
42 printf("%s;", sym->name);
43 if (!strcmp(sym->name, "sys_read"))
45 else if (!strcmp(sym->name, "sys_write"))
46 sys_write_seen = true;
49 static void print_addr(__u64 addr)
53 printf("%llx;", addr);
56 #define TASK_COMM_LEN 16
59 char comm[TASK_COMM_LEN];
64 static void print_stack(struct key_t *key, __u64 count)
66 __u64 ip[PERF_MAX_STACK_DEPTH] = {};
70 printf("%3lld %s;", count, key->comm);
71 if (bpf_map_lookup_elem(map_fd[1], &key->kernstack, ip) != 0) {
74 for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
78 if (bpf_map_lookup_elem(map_fd[1], &key->userstack, ip) != 0) {
81 for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
89 if (key->kernstack == -EEXIST && !warned) {
90 printf("stackmap collisions seen. Consider increasing size\n");
92 } else if ((int)key->kernstack < 0 && (int)key->userstack < 0) {
93 printf("err stackid %d %d\n", key->kernstack, key->userstack);
97 static void int_exit(int sig)
103 static void print_stacks(void)
105 struct key_t key = {}, next_key;
107 __u32 stackid = 0, next_id;
108 int fd = map_fd[0], stack_map = map_fd[1];
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);
118 if (!sys_read_seen || !sys_write_seen) {
119 printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n");
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);
130 static inline int generate_load(void)
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));
140 static void test_perf_event_all_cpu(struct perf_event_attr *attr)
142 int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
143 int *pmu_fd = malloc(nr_cpus * sizeof(int));
146 /* system wide perf event, no need to inherit */
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);
153 printf("sys_perf_event_open failed\n");
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);
161 if (generate_load() < 0) {
167 for (i--; i >= 0; i--) {
168 ioctl(pmu_fd[i], PERF_EVENT_IOC_DISABLE);
176 static void test_perf_event_task(struct perf_event_attr *attr)
178 int pmu_fd, error = 0;
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.
185 /* open task bound event */
186 pmu_fd = sys_perf_event_open(attr, 0, -1, -1, 0);
188 printf("sys_perf_event_open failed\n");
191 assert(ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd[0]) == 0);
192 assert(ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE) == 0);
194 if (generate_load() < 0) {
200 ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE);
206 static void test_bpf_perf_event(void)
208 struct perf_event_attr attr_type_hw = {
209 .sample_freq = SAMPLE_FREQ,
211 .type = PERF_TYPE_HARDWARE,
212 .config = PERF_COUNT_HW_CPU_CYCLES,
214 struct perf_event_attr attr_type_sw = {
215 .sample_freq = SAMPLE_FREQ,
217 .type = PERF_TYPE_SOFTWARE,
218 .config = PERF_COUNT_SW_CPU_CLOCK,
220 struct perf_event_attr attr_hw_cache_l1d = {
221 .sample_freq = SAMPLE_FREQ,
223 .type = PERF_TYPE_HW_CACHE,
225 PERF_COUNT_HW_CACHE_L1D |
226 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
227 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
229 struct perf_event_attr attr_hw_cache_branch_miss = {
230 .sample_freq = SAMPLE_FREQ,
232 .type = PERF_TYPE_HW_CACHE,
234 PERF_COUNT_HW_CACHE_BPU |
235 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
236 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
238 struct perf_event_attr attr_type_raw = {
239 .sample_freq = SAMPLE_FREQ,
241 .type = PERF_TYPE_RAW,
242 /* Intel Instruction Retired */
245 struct perf_event_attr attr_type_raw_lock_load = {
246 .sample_freq = SAMPLE_FREQ,
248 .type = PERF_TYPE_RAW,
249 /* Intel MEM_UOPS_RETIRED.LOCK_LOADS */
251 /* Request to record lock address from PEBS */
252 .sample_type = PERF_SAMPLE_ADDR,
253 /* Record address value requires precise event */
257 printf("Test HW_CPU_CYCLES\n");
258 test_perf_event_all_cpu(&attr_type_hw);
259 test_perf_event_task(&attr_type_hw);
261 printf("Test SW_CPU_CLOCK\n");
262 test_perf_event_all_cpu(&attr_type_sw);
263 test_perf_event_task(&attr_type_sw);
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);
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);
273 printf("Test Instruction Retired\n");
274 test_perf_event_all_cpu(&attr_type_raw);
275 test_perf_event_task(&attr_type_raw);
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);
281 printf("*** PASS ***\n");
285 int main(int argc, char **argv)
287 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
290 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
291 setrlimit(RLIMIT_MEMLOCK, &r);
293 signal(SIGINT, int_exit);
294 signal(SIGTERM, int_exit);
296 if (load_kallsyms()) {
297 printf("failed to process /proc/kallsyms\n");
301 if (load_bpf_file(filename)) {
302 printf("%s", bpf_log_buf);
310 test_bpf_perf_event();