Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1#!/bin/bash
2# BPF metadata collection test
3#
4# SPDX-License-Identifier: GPL-2.0
5
6set -e
7
8err=0
9perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
10
11cleanup() {
12 rm -f "${perfdata}"
13 rm -f "${perfdata}".old
14 trap - EXIT TERM INT
15}
16
17trap_cleanup() {
18 cleanup
19 exit 1
20}
21trap trap_cleanup EXIT TERM INT
22
23test_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 ' | grep -qF "$VERS"
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
73test_bpf_metadata
74
75cleanup
76exit $err