Skip to content

Commit edf2cad

Browse files
blakej11namhyung
authored andcommitted
perf test: add test for BPF metadata collection
This is an end-to-end test for the PERF_RECORD_BPF_METADATA support. It adds a new "bpf_metadata_perf_version" variable to perf's BPF programs, so that when they are loaded, there will be at least one BPF program with some metadata to parse. The test invokes "perf record" in a way that loads one of those BPF programs, and then sifts through the output to find its BPF metadata. Signed-off-by: Blake Jones <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent f19860e commit edf2cad

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

tools/perf/Makefile.perf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,8 +1250,9 @@ else
12501250
$(Q)cp "$(VMLINUX_H)" $@
12511251
endif
12521252

1253-
$(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(LIBBPF) $(SKEL_OUT)/vmlinux.h | $(SKEL_TMP_OUT)
1253+
$(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(OUTPUT)PERF-VERSION-FILE util/bpf_skel/perf_version.h $(LIBBPF) $(SKEL_OUT)/vmlinux.h | $(SKEL_TMP_OUT)
12541254
$(QUIET_CLANG)$(CLANG) -g -O2 --target=bpf $(CLANG_OPTIONS) $(BPF_INCLUDE) $(TOOLS_UAPI_INCLUDE) \
1255+
-include $(OUTPUT)PERF-VERSION-FILE -include util/bpf_skel/perf_version.h \
12551256
-c $(filter util/bpf_skel/%.bpf.c,$^) -o $@
12561257

12571258
$(SKEL_OUT)/%.skel.h: $(SKEL_TMP_OUT)/%.bpf.o | $(BPFTOOL)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# BPF metadata collection test.
5+
6+
set -e
7+
8+
err=0
9+
perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
10+
11+
cleanup() {
12+
rm -f "${perfdata}"
13+
rm -f "${perfdata}".old
14+
trap - EXIT TERM INT
15+
}
16+
17+
trap_cleanup() {
18+
cleanup
19+
exit 1
20+
}
21+
trap trap_cleanup EXIT TERM INT
22+
23+
test_bpf_metadata() {
24+
echo "Checking BPF metadata collection"
25+
26+
if ! perf check -q feature libbpf-strings ; then
27+
echo "Basic BPF metadata test [skipping - not supported]"
28+
err=0
29+
return
30+
fi
31+
32+
# This is a basic invocation of perf record
33+
# that invokes the perf_sample_filter BPF program.
34+
if ! perf record -e task-clock --filter 'ip > 0' \
35+
-o "${perfdata}" sleep 1 2> /dev/null
36+
then
37+
echo "Basic BPF metadata test [Failed record]"
38+
err=1
39+
return
40+
fi
41+
42+
# The BPF programs that ship with "perf" all have the following
43+
# variable defined at compile time:
44+
#
45+
# const char bpf_metadata_perf_version[] SEC(".rodata") = <...>;
46+
#
47+
# This invocation looks for a PERF_RECORD_BPF_METADATA event,
48+
# and checks that its content contains the string given by
49+
# "perf version".
50+
VERS=$(perf version | awk '{print $NF}')
51+
if ! perf script --show-bpf-events -i "${perfdata}" | awk '
52+
/PERF_RECORD_BPF_METADATA.*perf_sample_filter/ {
53+
header = 1;
54+
}
55+
/^ *entry/ {
56+
if (header) { header = 0; entry = 1; }
57+
}
58+
$0 !~ /^ *entry/ {
59+
entry = 0;
60+
}
61+
/perf_version/ {
62+
if (entry) print $NF;
63+
}
64+
' | egrep "$VERS" > /dev/null
65+
then
66+
echo "Basic BPF metadata test [Failed invalid output]"
67+
err=1
68+
return
69+
fi
70+
echo "Basic BPF metadata test [Success]"
71+
}
72+
73+
test_bpf_metadata
74+
75+
cleanup
76+
exit $err
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2+
3+
#ifndef __PERF_VERSION_H__
4+
#define __PERF_VERSION_H__
5+
6+
#include "vmlinux.h"
7+
#include <bpf/bpf_helpers.h>
8+
9+
/*
10+
* This is used by tests/shell/record_bpf_metadata.sh
11+
* to verify that BPF metadata generation works.
12+
*
13+
* PERF_VERSION is defined by a build rule at compile time.
14+
*/
15+
const char bpf_metadata_perf_version[] SEC(".rodata") = PERF_VERSION;
16+
17+
#endif /* __PERF_VERSION_H__ */

0 commit comments

Comments
 (0)