perf test: add test for BPF metadata collection
authorBlake Jones <blakejones@google.com>
Thu, 12 Jun 2025 19:49:39 +0000 (12:49 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Fri, 20 Jun 2025 21:55:24 +0000 (14:55 -0700)
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 <blakejones@google.com>
Link: https://lore.kernel.org/r/20250612194939.162730-6-blakejones@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/Makefile.perf
tools/perf/tests/shell/test_bpf_metadata.sh [new file with mode: 0755]
tools/perf/util/bpf_skel/perf_version.h [new file with mode: 0644]

index d4c7031b01a77f4a42326e4c9a88d8a352143178..4f292edeca5a8f1eafb2702094846b49adcf871d 100644 (file)
@@ -1250,8 +1250,9 @@ else
        $(Q)cp "$(VMLINUX_H)" $@
 endif
 
-$(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(LIBBPF) $(SKEL_OUT)/vmlinux.h | $(SKEL_TMP_OUT)
+$(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)
        $(QUIET_CLANG)$(CLANG) -g -O2 --target=bpf $(CLANG_OPTIONS) $(BPF_INCLUDE) $(TOOLS_UAPI_INCLUDE) \
+         -include $(OUTPUT)PERF-VERSION-FILE -include util/bpf_skel/perf_version.h \
          -c $(filter util/bpf_skel/%.bpf.c,$^) -o $@
 
 $(SKEL_OUT)/%.skel.h: $(SKEL_TMP_OUT)/%.bpf.o | $(BPFTOOL)
diff --git a/tools/perf/tests/shell/test_bpf_metadata.sh b/tools/perf/tests/shell/test_bpf_metadata.sh
new file mode 100755 (executable)
index 0000000..11df592
--- /dev/null
@@ -0,0 +1,76 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# BPF metadata collection test.
+
+set -e
+
+err=0
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+
+cleanup() {
+       rm -f "${perfdata}"
+       rm -f "${perfdata}".old
+       trap - EXIT TERM INT
+}
+
+trap_cleanup() {
+       cleanup
+       exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+test_bpf_metadata() {
+       echo "Checking BPF metadata collection"
+
+       if ! perf check -q feature libbpf-strings ; then
+               echo "Basic BPF metadata test [skipping - not supported]"
+               err=0
+               return
+       fi
+
+       # This is a basic invocation of perf record
+       # that invokes the perf_sample_filter BPF program.
+       if ! perf record -e task-clock --filter 'ip > 0' \
+                        -o "${perfdata}" sleep 1 2> /dev/null
+       then
+               echo "Basic BPF metadata test [Failed record]"
+               err=1
+               return
+       fi
+
+       # The BPF programs that ship with "perf" all have the following
+       # variable defined at compile time:
+       #
+       #   const char bpf_metadata_perf_version[] SEC(".rodata") = <...>;
+       #
+       # This invocation looks for a PERF_RECORD_BPF_METADATA event,
+       # and checks that its content contains the string given by
+       # "perf version".
+       VERS=$(perf version | awk '{print $NF}')
+       if ! perf script --show-bpf-events -i "${perfdata}" | awk '
+               /PERF_RECORD_BPF_METADATA.*perf_sample_filter/ {
+                       header = 1;
+               }
+               /^ *entry/ {
+                       if (header) { header = 0; entry = 1; }
+               }
+               $0 !~ /^ *entry/ {
+                       entry = 0;
+               }
+               /perf_version/ {
+                       if (entry) print $NF;
+               }
+       ' | egrep "$VERS" > /dev/null
+       then
+               echo "Basic BPF metadata test [Failed invalid output]"
+               err=1
+               return
+       fi
+       echo "Basic BPF metadata test [Success]"
+}
+
+test_bpf_metadata
+
+cleanup
+exit $err
diff --git a/tools/perf/util/bpf_skel/perf_version.h b/tools/perf/util/bpf_skel/perf_version.h
new file mode 100644 (file)
index 0000000..1ed5b2e
--- /dev/null
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+
+#ifndef __PERF_VERSION_H__
+#define __PERF_VERSION_H__
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+/*
+ * This is used by tests/shell/record_bpf_metadata.sh
+ * to verify that BPF metadata generation works.
+ *
+ * PERF_VERSION is defined by a build rule at compile time.
+ */
+const char bpf_metadata_perf_version[] SEC(".rodata") = PERF_VERSION;
+
+#endif /* __PERF_VERSION_H__ */