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
--)
83 if (key
->kernstack
== -EEXIST
&& !warned
) {
84 printf("stackmap collisions seen. Consider increasing size\n");
86 } else if ((int)key
->kernstack
< 0 && (int)key
->userstack
< 0) {
87 printf("err stackid %d %d\n", key
->kernstack
, key
->userstack
);
91 static void int_exit(int sig
)
97 static void print_stacks(void)
99 struct key_t key
= {}, next_key
;
101 __u32 stackid
= 0, next_id
;
102 int fd
= map_fd
[0], stack_map
= map_fd
[1];
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
);
112 if (!sys_read_seen
|| !sys_write_seen
) {
113 printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n");
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
);
124 static void test_perf_event_all_cpu(struct perf_event_attr
*attr
)
126 int nr_cpus
= sysconf(_SC_NPROCESSORS_CONF
);
127 int *pmu_fd
= malloc(nr_cpus
* sizeof(int));
130 /* open perf_event on all cpus */
131 for (i
= 0; i
< nr_cpus
; i
++) {
132 pmu_fd
[i
] = sys_perf_event_open(attr
, -1, i
, -1, 0);
134 printf("sys_perf_event_open failed\n");
138 assert(ioctl(pmu_fd
[i
], PERF_EVENT_IOC_SET_BPF
, prog_fd
[0]) == 0);
139 assert(ioctl(pmu_fd
[i
], PERF_EVENT_IOC_ENABLE
) == 0);
141 system("dd if=/dev/zero of=/dev/null count=5000k status=none");
144 for (i
--; i
>= 0; i
--) {
145 ioctl(pmu_fd
[i
], PERF_EVENT_IOC_DISABLE
);
153 static void test_perf_event_task(struct perf_event_attr
*attr
)
157 /* open task bound event */
158 pmu_fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
160 printf("sys_perf_event_open failed\n");
163 assert(ioctl(pmu_fd
, PERF_EVENT_IOC_SET_BPF
, prog_fd
[0]) == 0);
164 assert(ioctl(pmu_fd
, PERF_EVENT_IOC_ENABLE
) == 0);
165 system("dd if=/dev/zero of=/dev/null count=5000k status=none");
167 ioctl(pmu_fd
, PERF_EVENT_IOC_DISABLE
);
171 static void test_bpf_perf_event(void)
173 struct perf_event_attr attr_type_hw
= {
174 .sample_freq
= SAMPLE_FREQ
,
176 .type
= PERF_TYPE_HARDWARE
,
177 .config
= PERF_COUNT_HW_CPU_CYCLES
,
180 struct perf_event_attr attr_type_sw
= {
181 .sample_freq
= SAMPLE_FREQ
,
183 .type
= PERF_TYPE_SOFTWARE
,
184 .config
= PERF_COUNT_SW_CPU_CLOCK
,
187 struct perf_event_attr attr_hw_cache_l1d
= {
188 .sample_freq
= SAMPLE_FREQ
,
190 .type
= PERF_TYPE_HW_CACHE
,
192 PERF_COUNT_HW_CACHE_L1D
|
193 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
194 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16),
197 struct perf_event_attr attr_hw_cache_branch_miss
= {
198 .sample_freq
= SAMPLE_FREQ
,
200 .type
= PERF_TYPE_HW_CACHE
,
202 PERF_COUNT_HW_CACHE_BPU
|
203 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
204 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16),
207 struct perf_event_attr attr_type_raw
= {
208 .sample_freq
= SAMPLE_FREQ
,
210 .type
= PERF_TYPE_RAW
,
211 /* Intel Instruction Retired */
216 printf("Test HW_CPU_CYCLES\n");
217 test_perf_event_all_cpu(&attr_type_hw
);
218 test_perf_event_task(&attr_type_hw
);
220 printf("Test SW_CPU_CLOCK\n");
221 test_perf_event_all_cpu(&attr_type_sw
);
222 test_perf_event_task(&attr_type_sw
);
224 printf("Test HW_CACHE_L1D\n");
225 test_perf_event_all_cpu(&attr_hw_cache_l1d
);
226 test_perf_event_task(&attr_hw_cache_l1d
);
228 printf("Test HW_CACHE_BPU\n");
229 test_perf_event_all_cpu(&attr_hw_cache_branch_miss
);
230 test_perf_event_task(&attr_hw_cache_branch_miss
);
232 printf("Test Instruction Retired\n");
233 test_perf_event_all_cpu(&attr_type_raw
);
234 test_perf_event_task(&attr_type_raw
);
236 printf("*** PASS ***\n");
240 int main(int argc
, char **argv
)
242 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
245 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
246 setrlimit(RLIMIT_MEMLOCK
, &r
);
248 signal(SIGINT
, int_exit
);
249 signal(SIGTERM
, int_exit
);
251 if (load_kallsyms()) {
252 printf("failed to process /proc/kallsyms\n");
256 if (load_bpf_file(filename
)) {
257 printf("%s", bpf_log_buf
);
265 test_bpf_perf_event();