3 #include <util/bpf-loader.h>
4 #include <util/evlist.h>
10 #ifdef HAVE_LIBBPF_SUPPORT
12 static int epoll_pwait_loop(void)
16 /* Should fail NR_ITERS times */
17 for (i
= 0; i
< NR_ITERS
; i
++)
18 epoll_pwait(-(i
+ 1), NULL
, 0, 0, NULL
);
23 enum test_llvm__testcase prog_id
;
26 const char *msg_compile_fail
;
27 const char *msg_load_fail
;
28 int (*target_func
)(void);
30 } bpf_testcase_table
[] = {
33 "Test basic BPF filtering",
35 "fix 'perf test LLVM' first",
36 "load bpf object failed",
42 static int do_test(struct bpf_object
*obj
, int (*func
)(void),
45 struct record_opts opts
= {
52 .default_interval
= 1,
56 char sbuf
[STRERR_BUFSIZE
];
57 struct perf_evlist
*evlist
;
58 int i
, ret
= TEST_FAIL
, err
= 0, count
= 0;
60 struct parse_events_evlist parse_evlist
;
61 struct parse_events_error parse_error
;
63 bzero(&parse_error
, sizeof(parse_error
));
64 bzero(&parse_evlist
, sizeof(parse_evlist
));
65 parse_evlist
.error
= &parse_error
;
66 INIT_LIST_HEAD(&parse_evlist
.list
);
68 err
= parse_events_load_bpf_obj(&parse_evlist
, &parse_evlist
.list
, obj
);
69 if (err
|| list_empty(&parse_evlist
.list
)) {
70 pr_debug("Failed to add events selected by BPF\n");
75 snprintf(pid
, sizeof(pid
), "%d", getpid());
76 pid
[sizeof(pid
) - 1] = '\0';
77 opts
.target
.tid
= opts
.target
.pid
= pid
;
79 /* Instead of perf_evlist__new_default, don't add default events */
80 evlist
= perf_evlist__new();
82 pr_debug("No ehough memory to create evlist\n");
86 err
= perf_evlist__create_maps(evlist
, &opts
.target
);
88 pr_debug("Not enough memory to create thread/cpu maps\n");
89 goto out_delete_evlist
;
92 perf_evlist__splice_list_tail(evlist
, &parse_evlist
.list
);
93 evlist
->nr_groups
= parse_evlist
.nr_groups
;
95 perf_evlist__config(evlist
, &opts
);
97 err
= perf_evlist__open(evlist
);
99 pr_debug("perf_evlist__open: %s\n",
100 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
101 goto out_delete_evlist
;
104 err
= perf_evlist__mmap(evlist
, opts
.mmap_pages
, false);
106 pr_debug("perf_evlist__mmap: %s\n",
107 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
108 goto out_delete_evlist
;
111 perf_evlist__enable(evlist
);
113 perf_evlist__disable(evlist
);
115 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
116 union perf_event
*event
;
118 while ((event
= perf_evlist__mmap_read(evlist
, i
)) != NULL
) {
119 const u32 type
= event
->header
.type
;
121 if (type
== PERF_RECORD_SAMPLE
)
127 pr_debug("BPF filter result incorrect\n");
132 perf_evlist__delete(evlist
);
136 static struct bpf_object
*
137 prepare_bpf(void *obj_buf
, size_t obj_buf_sz
, const char *name
)
139 struct bpf_object
*obj
;
141 obj
= bpf__prepare_load_buffer(obj_buf
, obj_buf_sz
, name
);
143 pr_debug("Compile BPF program failed.\n");
149 static int __test__bpf(int idx
)
154 struct bpf_object
*obj
;
156 ret
= test_llvm__fetch_bpf_obj(&obj_buf
, &obj_buf_sz
,
157 bpf_testcase_table
[idx
].prog_id
,
159 if (ret
!= TEST_OK
|| !obj_buf
|| !obj_buf_sz
) {
160 pr_debug("Unable to get BPF object, %s\n",
161 bpf_testcase_table
[idx
].msg_compile_fail
);
168 obj
= prepare_bpf(obj_buf
, obj_buf_sz
,
169 bpf_testcase_table
[idx
].name
);
176 bpf_testcase_table
[idx
].target_func
,
177 bpf_testcase_table
[idx
].expect_result
);
188 if (geteuid() != 0) {
189 pr_debug("Only root can run BPF test\n");
193 for (i
= 0; i
< ARRAY_SIZE(bpf_testcase_table
); i
++) {
194 err
= __test__bpf(i
);
206 pr_debug("Skip BPF test because BPF support is not compiled\n");