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>
25 #define SAMPLE_FREQ 50
27 static bool sys_read_seen
, sys_write_seen
;
29 static void print_ksym(__u64 addr
)
35 sym
= ksym_search(addr
);
36 printf("%s;", sym
->name
);
37 if (!strcmp(sym
->name
, "sys_read"))
39 else if (!strcmp(sym
->name
, "sys_write"))
40 sys_write_seen
= true;
43 static void print_addr(__u64 addr
)
47 printf("%llx;", addr
);
50 #define TASK_COMM_LEN 16
53 char comm
[TASK_COMM_LEN
];
58 static void print_stack(struct key_t
*key
, __u64 count
)
60 __u64 ip
[PERF_MAX_STACK_DEPTH
] = {};
64 printf("%3lld %s;", count
, key
->comm
);
65 if (bpf_map_lookup_elem(map_fd
[1], &key
->kernstack
, ip
) != 0) {
68 for (i
= PERF_MAX_STACK_DEPTH
- 1; i
>= 0; i
--)
72 if (bpf_map_lookup_elem(map_fd
[1], &key
->userstack
, ip
) != 0) {
75 for (i
= PERF_MAX_STACK_DEPTH
- 1; i
>= 0; i
--)
80 if (key
->kernstack
== -EEXIST
&& !warned
) {
81 printf("stackmap collisions seen. Consider increasing size\n");
83 } else if ((int)key
->kernstack
< 0 && (int)key
->userstack
< 0) {
84 printf("err stackid %d %d\n", key
->kernstack
, key
->userstack
);
88 static void int_exit(int sig
)
94 static void print_stacks(void)
96 struct key_t key
= {}, next_key
;
98 __u32 stackid
= 0, next_id
;
99 int fd
= map_fd
[0], stack_map
= map_fd
[1];
101 sys_read_seen
= sys_write_seen
= false;
102 while (bpf_map_get_next_key(fd
, &key
, &next_key
) == 0) {
103 bpf_map_lookup_elem(fd
, &next_key
, &value
);
104 print_stack(&next_key
, value
);
105 bpf_map_delete_elem(fd
, &next_key
);
109 if (!sys_read_seen
|| !sys_write_seen
) {
110 printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n");
114 /* clear stack map */
115 while (bpf_map_get_next_key(stack_map
, &stackid
, &next_id
) == 0) {
116 bpf_map_delete_elem(stack_map
, &next_id
);
121 static void test_perf_event_all_cpu(struct perf_event_attr
*attr
)
123 int nr_cpus
= sysconf(_SC_NPROCESSORS_CONF
);
124 int *pmu_fd
= malloc(nr_cpus
* sizeof(int));
127 /* open perf_event on all cpus */
128 for (i
= 0; i
< nr_cpus
; i
++) {
129 pmu_fd
[i
] = sys_perf_event_open(attr
, -1, i
, -1, 0);
131 printf("sys_perf_event_open failed\n");
134 assert(ioctl(pmu_fd
[i
], PERF_EVENT_IOC_SET_BPF
, prog_fd
[0]) == 0);
135 assert(ioctl(pmu_fd
[i
], PERF_EVENT_IOC_ENABLE
, 0) == 0);
137 system("dd if=/dev/zero of=/dev/null count=5000k");
140 for (i
--; i
>= 0; i
--)
145 static void test_perf_event_task(struct perf_event_attr
*attr
)
149 /* open task bound event */
150 pmu_fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
152 printf("sys_perf_event_open failed\n");
155 assert(ioctl(pmu_fd
, PERF_EVENT_IOC_SET_BPF
, prog_fd
[0]) == 0);
156 assert(ioctl(pmu_fd
, PERF_EVENT_IOC_ENABLE
, 0) == 0);
157 system("dd if=/dev/zero of=/dev/null count=5000k");
162 static void test_bpf_perf_event(void)
164 struct perf_event_attr attr_type_hw
= {
165 .sample_freq
= SAMPLE_FREQ
,
167 .type
= PERF_TYPE_HARDWARE
,
168 .config
= PERF_COUNT_HW_CPU_CYCLES
,
171 struct perf_event_attr attr_type_sw
= {
172 .sample_freq
= SAMPLE_FREQ
,
174 .type
= PERF_TYPE_SOFTWARE
,
175 .config
= PERF_COUNT_SW_CPU_CLOCK
,
179 test_perf_event_all_cpu(&attr_type_hw
);
180 test_perf_event_task(&attr_type_hw
);
181 test_perf_event_all_cpu(&attr_type_sw
);
182 test_perf_event_task(&attr_type_sw
);
186 int main(int argc
, char **argv
)
188 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
191 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
192 setrlimit(RLIMIT_MEMLOCK
, &r
);
194 signal(SIGINT
, int_exit
);
196 if (load_kallsyms()) {
197 printf("failed to process /proc/kallsyms\n");
201 if (load_bpf_file(filename
)) {
202 printf("%s", bpf_log_buf
);
210 test_bpf_perf_event();