1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
9 #include <linux/perf_event.h>
10 #include <linux/bpf.h>
13 #include <sys/resource.h>
15 #include <bpf/libbpf.h>
17 #include "trace_helpers.h"
19 #define SAMPLE_FREQ 50
22 /* counts, stackmap */
24 struct bpf_program
*prog
;
25 static bool sys_read_seen
, sys_write_seen
;
27 static void print_ksym(__u64 addr
)
33 sym
= ksym_search(addr
);
35 printf("ksym not found. Is kallsyms loaded?\n");
39 printf("%s;", sym
->name
);
40 if (!strstr(sym
->name
, "sys_read"))
42 else if (!strstr(sym
->name
, "sys_write"))
43 sys_write_seen
= true;
46 static void print_addr(__u64 addr
)
50 printf("%llx;", addr
);
53 #define TASK_COMM_LEN 16
56 char comm
[TASK_COMM_LEN
];
61 static void print_stack(struct key_t
*key
, __u64 count
)
63 __u64 ip
[PERF_MAX_STACK_DEPTH
] = {};
67 printf("%3lld %s;", count
, key
->comm
);
68 if (bpf_map_lookup_elem(map_fd
[1], &key
->kernstack
, ip
) != 0) {
71 for (i
= PERF_MAX_STACK_DEPTH
- 1; i
>= 0; i
--)
75 if (bpf_map_lookup_elem(map_fd
[1], &key
->userstack
, ip
) != 0) {
78 for (i
= PERF_MAX_STACK_DEPTH
- 1; i
>= 0; i
--)
86 if (key
->kernstack
== -EEXIST
&& !warned
) {
87 printf("stackmap collisions seen. Consider increasing size\n");
89 } else if ((int)key
->kernstack
< 0 && (int)key
->userstack
< 0) {
90 printf("err stackid %d %d\n", key
->kernstack
, key
->userstack
);
94 static void err_exit(int err
)
100 static void print_stacks(void)
102 struct key_t key
= {}, next_key
;
104 __u32 stackid
= 0, next_id
;
105 int error
= 1, fd
= map_fd
[0], stack_map
= map_fd
[1];
107 sys_read_seen
= sys_write_seen
= false;
108 while (bpf_map_get_next_key(fd
, &key
, &next_key
) == 0) {
109 bpf_map_lookup_elem(fd
, &next_key
, &value
);
110 print_stack(&next_key
, value
);
111 bpf_map_delete_elem(fd
, &next_key
);
115 if (!sys_read_seen
|| !sys_write_seen
) {
116 printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n");
120 /* clear stack map */
121 while (bpf_map_get_next_key(stack_map
, &stackid
, &next_id
) == 0) {
122 bpf_map_delete_elem(stack_map
, &next_id
);
127 static inline int generate_load(void)
129 if (system("dd if=/dev/zero of=/dev/null count=5000k status=none") < 0) {
130 printf("failed to generate some load with dd: %s\n", strerror(errno
));
137 static void test_perf_event_all_cpu(struct perf_event_attr
*attr
)
139 int nr_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
140 struct bpf_link
**links
= calloc(nr_cpus
, sizeof(struct bpf_link
*));
141 int i
, pmu_fd
, error
= 1;
144 printf("malloc of links failed\n");
148 /* system wide perf event, no need to inherit */
151 /* open perf_event on all cpus */
152 for (i
= 0; i
< nr_cpus
; i
++) {
153 pmu_fd
= sys_perf_event_open(attr
, -1, i
, -1, 0);
155 printf("sys_perf_event_open failed\n");
158 links
[i
] = bpf_program__attach_perf_event(prog
, pmu_fd
);
159 if (libbpf_get_error(links
[i
])) {
160 printf("bpf_program__attach_perf_event failed\n");
167 if (generate_load() < 0)
173 for (i
--; i
>= 0; i
--)
174 bpf_link__destroy(links
[i
]);
181 static void test_perf_event_task(struct perf_event_attr
*attr
)
183 struct bpf_link
*link
= NULL
;
184 int pmu_fd
, error
= 1;
186 /* per task perf event, enable inherit so the "dd ..." command can be traced properly.
187 * Enabling inherit will cause bpf_perf_prog_read_time helper failure.
191 /* open task bound event */
192 pmu_fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
194 printf("sys_perf_event_open failed\n");
197 link
= bpf_program__attach_perf_event(prog
, pmu_fd
);
198 if (libbpf_get_error(link
)) {
199 printf("bpf_program__attach_perf_event failed\n");
205 if (generate_load() < 0)
211 bpf_link__destroy(link
);
216 static void test_bpf_perf_event(void)
218 struct perf_event_attr attr_type_hw
= {
219 .sample_freq
= SAMPLE_FREQ
,
221 .type
= PERF_TYPE_HARDWARE
,
222 .config
= PERF_COUNT_HW_CPU_CYCLES
,
224 struct perf_event_attr attr_type_sw
= {
225 .sample_freq
= SAMPLE_FREQ
,
227 .type
= PERF_TYPE_SOFTWARE
,
228 .config
= PERF_COUNT_SW_CPU_CLOCK
,
230 struct perf_event_attr attr_hw_cache_l1d
= {
231 .sample_freq
= SAMPLE_FREQ
,
233 .type
= PERF_TYPE_HW_CACHE
,
235 PERF_COUNT_HW_CACHE_L1D
|
236 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
237 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16),
239 struct perf_event_attr attr_hw_cache_branch_miss
= {
240 .sample_freq
= SAMPLE_FREQ
,
242 .type
= PERF_TYPE_HW_CACHE
,
244 PERF_COUNT_HW_CACHE_BPU
|
245 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
246 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16),
248 struct perf_event_attr attr_type_raw
= {
249 .sample_freq
= SAMPLE_FREQ
,
251 .type
= PERF_TYPE_RAW
,
252 /* Intel Instruction Retired */
255 struct perf_event_attr attr_type_raw_lock_load
= {
256 .sample_freq
= SAMPLE_FREQ
,
258 .type
= PERF_TYPE_RAW
,
259 /* Intel MEM_UOPS_RETIRED.LOCK_LOADS */
261 /* Request to record lock address from PEBS */
262 .sample_type
= PERF_SAMPLE_ADDR
,
263 /* Record address value requires precise event */
267 printf("Test HW_CPU_CYCLES\n");
268 test_perf_event_all_cpu(&attr_type_hw
);
269 test_perf_event_task(&attr_type_hw
);
271 printf("Test SW_CPU_CLOCK\n");
272 test_perf_event_all_cpu(&attr_type_sw
);
273 test_perf_event_task(&attr_type_sw
);
275 printf("Test HW_CACHE_L1D\n");
276 test_perf_event_all_cpu(&attr_hw_cache_l1d
);
277 test_perf_event_task(&attr_hw_cache_l1d
);
279 printf("Test HW_CACHE_BPU\n");
280 test_perf_event_all_cpu(&attr_hw_cache_branch_miss
);
281 test_perf_event_task(&attr_hw_cache_branch_miss
);
283 printf("Test Instruction Retired\n");
284 test_perf_event_all_cpu(&attr_type_raw
);
285 test_perf_event_task(&attr_type_raw
);
287 printf("Test Lock Load\n");
288 test_perf_event_all_cpu(&attr_type_raw_lock_load
);
289 test_perf_event_task(&attr_type_raw_lock_load
);
291 printf("*** PASS ***\n");
295 int main(int argc
, char **argv
)
297 struct bpf_object
*obj
= NULL
;
301 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
303 signal(SIGINT
, err_exit
);
304 signal(SIGTERM
, err_exit
);
306 if (load_kallsyms()) {
307 printf("failed to process /proc/kallsyms\n");
311 obj
= bpf_object__open_file(filename
, NULL
);
312 if (libbpf_get_error(obj
)) {
313 printf("opening BPF object file failed\n");
318 prog
= bpf_object__find_program_by_name(obj
, "bpf_prog1");
320 printf("finding a prog in obj file failed\n");
324 /* load BPF program */
325 if (bpf_object__load(obj
)) {
326 printf("loading BPF object file failed\n");
330 map_fd
[0] = bpf_object__find_map_fd_by_name(obj
, "counts");
331 map_fd
[1] = bpf_object__find_map_fd_by_name(obj
, "stackmap");
332 if (map_fd
[0] < 0 || map_fd
[1] < 0) {
333 printf("finding a counts/stackmap map in obj file failed\n");
341 } else if (pid
== -1) {
342 printf("couldn't spawn process\n");
346 test_bpf_perf_event();
350 bpf_object__close(obj
);