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
);
37 printf("%s;", sym
->name
);
38 if (!strcmp(sym
->name
, "sys_read"))
40 else if (!strcmp(sym
->name
, "sys_write"))
41 sys_write_seen
= true;
44 static void print_addr(__u64 addr
)
48 printf("%llx;", addr
);
51 #define TASK_COMM_LEN 16
54 char comm
[TASK_COMM_LEN
];
59 static void print_stack(struct key_t
*key
, __u64 count
)
61 __u64 ip
[PERF_MAX_STACK_DEPTH
] = {};
65 printf("%3lld %s;", count
, key
->comm
);
66 if (bpf_map_lookup_elem(map_fd
[1], &key
->kernstack
, ip
) != 0) {
69 for (i
= PERF_MAX_STACK_DEPTH
- 1; i
>= 0; i
--)
73 if (bpf_map_lookup_elem(map_fd
[1], &key
->userstack
, ip
) != 0) {
76 for (i
= PERF_MAX_STACK_DEPTH
- 1; i
>= 0; i
--)
84 if (key
->kernstack
== -EEXIST
&& !warned
) {
85 printf("stackmap collisions seen. Consider increasing size\n");
87 } else if ((int)key
->kernstack
< 0 && (int)key
->userstack
< 0) {
88 printf("err stackid %d %d\n", key
->kernstack
, key
->userstack
);
92 static void int_exit(int sig
)
98 static void print_stacks(void)
100 struct key_t key
= {}, next_key
;
102 __u32 stackid
= 0, next_id
;
103 int fd
= map_fd
[0], stack_map
= map_fd
[1];
105 sys_read_seen
= sys_write_seen
= false;
106 while (bpf_map_get_next_key(fd
, &key
, &next_key
) == 0) {
107 bpf_map_lookup_elem(fd
, &next_key
, &value
);
108 print_stack(&next_key
, value
);
109 bpf_map_delete_elem(fd
, &next_key
);
113 if (!sys_read_seen
|| !sys_write_seen
) {
114 printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n");
118 /* clear stack map */
119 while (bpf_map_get_next_key(stack_map
, &stackid
, &next_id
) == 0) {
120 bpf_map_delete_elem(stack_map
, &next_id
);
125 static inline int generate_load(void)
127 if (system("dd if=/dev/zero of=/dev/null count=5000k status=none") < 0) {
128 printf("failed to generate some load with dd: %s\n", strerror(errno
));
135 static void test_perf_event_all_cpu(struct perf_event_attr
*attr
)
137 int nr_cpus
= sysconf(_SC_NPROCESSORS_CONF
);
138 int *pmu_fd
= malloc(nr_cpus
* sizeof(int));
141 /* system wide perf event, no need to inherit */
144 /* open perf_event on all cpus */
145 for (i
= 0; i
< nr_cpus
; i
++) {
146 pmu_fd
[i
] = sys_perf_event_open(attr
, -1, i
, -1, 0);
148 printf("sys_perf_event_open failed\n");
152 assert(ioctl(pmu_fd
[i
], PERF_EVENT_IOC_SET_BPF
, prog_fd
[0]) == 0);
153 assert(ioctl(pmu_fd
[i
], PERF_EVENT_IOC_ENABLE
) == 0);
156 if (generate_load() < 0) {
162 for (i
--; i
>= 0; i
--) {
163 ioctl(pmu_fd
[i
], PERF_EVENT_IOC_DISABLE
);
171 static void test_perf_event_task(struct perf_event_attr
*attr
)
173 int pmu_fd
, error
= 0;
175 /* per task perf event, enable inherit so the "dd ..." command can be traced properly.
176 * Enabling inherit will cause bpf_perf_prog_read_time helper failure.
180 /* open task bound event */
181 pmu_fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
183 printf("sys_perf_event_open failed\n");
186 assert(ioctl(pmu_fd
, PERF_EVENT_IOC_SET_BPF
, prog_fd
[0]) == 0);
187 assert(ioctl(pmu_fd
, PERF_EVENT_IOC_ENABLE
) == 0);
189 if (generate_load() < 0) {
195 ioctl(pmu_fd
, PERF_EVENT_IOC_DISABLE
);
201 static void test_bpf_perf_event(void)
203 struct perf_event_attr attr_type_hw
= {
204 .sample_freq
= SAMPLE_FREQ
,
206 .type
= PERF_TYPE_HARDWARE
,
207 .config
= PERF_COUNT_HW_CPU_CYCLES
,
209 struct perf_event_attr attr_type_sw
= {
210 .sample_freq
= SAMPLE_FREQ
,
212 .type
= PERF_TYPE_SOFTWARE
,
213 .config
= PERF_COUNT_SW_CPU_CLOCK
,
215 struct perf_event_attr attr_hw_cache_l1d
= {
216 .sample_freq
= SAMPLE_FREQ
,
218 .type
= PERF_TYPE_HW_CACHE
,
220 PERF_COUNT_HW_CACHE_L1D
|
221 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
222 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16),
224 struct perf_event_attr attr_hw_cache_branch_miss
= {
225 .sample_freq
= SAMPLE_FREQ
,
227 .type
= PERF_TYPE_HW_CACHE
,
229 PERF_COUNT_HW_CACHE_BPU
|
230 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
231 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16),
233 struct perf_event_attr attr_type_raw
= {
234 .sample_freq
= SAMPLE_FREQ
,
236 .type
= PERF_TYPE_RAW
,
237 /* Intel Instruction Retired */
240 struct perf_event_attr attr_type_raw_lock_load
= {
241 .sample_freq
= SAMPLE_FREQ
,
243 .type
= PERF_TYPE_RAW
,
244 /* Intel MEM_UOPS_RETIRED.LOCK_LOADS */
246 /* Request to record lock address from PEBS */
247 .sample_type
= PERF_SAMPLE_ADDR
,
248 /* Record address value requires precise event */
252 printf("Test HW_CPU_CYCLES\n");
253 test_perf_event_all_cpu(&attr_type_hw
);
254 test_perf_event_task(&attr_type_hw
);
256 printf("Test SW_CPU_CLOCK\n");
257 test_perf_event_all_cpu(&attr_type_sw
);
258 test_perf_event_task(&attr_type_sw
);
260 printf("Test HW_CACHE_L1D\n");
261 test_perf_event_all_cpu(&attr_hw_cache_l1d
);
262 test_perf_event_task(&attr_hw_cache_l1d
);
264 printf("Test HW_CACHE_BPU\n");
265 test_perf_event_all_cpu(&attr_hw_cache_branch_miss
);
266 test_perf_event_task(&attr_hw_cache_branch_miss
);
268 printf("Test Instruction Retired\n");
269 test_perf_event_all_cpu(&attr_type_raw
);
270 test_perf_event_task(&attr_type_raw
);
272 printf("Test Lock Load\n");
273 test_perf_event_all_cpu(&attr_type_raw_lock_load
);
274 test_perf_event_task(&attr_type_raw_lock_load
);
276 printf("*** PASS ***\n");
280 int main(int argc
, char **argv
)
282 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
285 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
286 setrlimit(RLIMIT_MEMLOCK
, &r
);
288 signal(SIGINT
, int_exit
);
289 signal(SIGTERM
, int_exit
);
291 if (load_kallsyms()) {
292 printf("failed to process /proc/kallsyms\n");
296 if (load_bpf_file(filename
)) {
297 printf("%s", bpf_log_buf
);
305 test_bpf_perf_event();