Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf test: Add perf record sample filtering test

$ sudo ./perf test 'sample filter' -v
94: perf record sample filtering (by BPF) tests :
--- start ---
test child forked, pid 3817527
Checking BPF-filter privilege
Basic bpf-filter test
Basic bpf-filter test [Success]
Failing bpf-filter test
Error: task-clock event does not have PERF_SAMPLE_CPU
Failing bpf-filter test [Success]
Group bpf-filter test
Error: task-clock event does not have PERF_SAMPLE_CPU
Error: task-clock event does not have PERF_SAMPLE_CODE_PAGE_SIZE
Group bpf-filter test [Success]
test child finished with 0
---- end ----
perf record sample filtering (by BPF) tests: Ok

Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20230811025822.3859771-2-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
9575ecdd dc7f01f1

+128
+128
tools/perf/tests/shell/record_bpf_filter.sh
··· 1 + #!/bin/sh 2 + # perf record sample filtering (by BPF) tests 3 + # SPDX-License-Identifier: GPL-2.0 4 + 5 + set -e 6 + 7 + err=0 8 + perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX) 9 + 10 + cleanup() { 11 + rm -f "${perfdata}" 12 + rm -f "${perfdata}".old 13 + trap - EXIT TERM INT 14 + } 15 + 16 + trap_cleanup() { 17 + cleanup 18 + exit 1 19 + } 20 + trap trap_cleanup EXIT TERM INT 21 + 22 + test_bpf_filter_priv() { 23 + echo "Checking BPF-filter privilege" 24 + 25 + if [ "$(id -u)" != 0 ] 26 + then 27 + echo "bpf-filter test [Skipped permission]" 28 + err=2 29 + return 30 + fi 31 + if ! perf record -e task-clock --filter 'period > 1' \ 32 + -o /dev/null --quiet true 2>&1 33 + then 34 + echo "bpf-filter test [Skipped missing BPF support]" 35 + err=2 36 + return 37 + fi 38 + } 39 + 40 + test_bpf_filter_basic() { 41 + echo "Basic bpf-filter test" 42 + 43 + if ! perf record -e task-clock -c 10000 --filter 'ip < 0xffffffff00000000' \ 44 + -o "${perfdata}" true 2> /dev/null 45 + then 46 + echo "Basic bpf-filter test [Failed record]" 47 + err=1 48 + return 49 + fi 50 + if perf script -i "${perfdata}" -F ip | grep 'ffffffff[0-9a-f]*' 51 + then 52 + echo "Basic bpf-filter test [Failed invalid output]" 53 + err=1 54 + return 55 + fi 56 + echo "Basic bpf-filter test [Success]" 57 + } 58 + 59 + test_bpf_filter_fail() { 60 + echo "Failing bpf-filter test" 61 + 62 + # 'cpu' requires PERF_SAMPLE_CPU flag 63 + if ! perf record -e task-clock --filter 'cpu > 0' \ 64 + -o /dev/null true 2>&1 | grep PERF_SAMPLE_CPU 65 + then 66 + echo "Failing bpf-filter test [Failed forbidden CPU]" 67 + err=1 68 + return 69 + fi 70 + 71 + if ! perf record --sample-cpu -e task-clock --filter 'cpu > 0' \ 72 + -o /dev/null true 2>/dev/null 73 + then 74 + echo "Failing bpf-filter test [Failed should succeed]" 75 + err=1 76 + return 77 + fi 78 + 79 + echo "Failing bpf-filter test [Success]" 80 + } 81 + 82 + test_bpf_filter_group() { 83 + echo "Group bpf-filter test" 84 + 85 + if ! perf record -e task-clock --filter 'period > 1000 || ip > 0' \ 86 + -o /dev/null true 2>/dev/null 87 + then 88 + echo "Group bpf-filter test [Failed should succeed]" 89 + err=1 90 + return 91 + fi 92 + 93 + if ! perf record -e task-clock --filter 'cpu > 0 || ip > 0' \ 94 + -o /dev/null true 2>&1 | grep PERF_SAMPLE_CPU 95 + then 96 + echo "Group bpf-filter test [Failed forbidden CPU]" 97 + err=1 98 + return 99 + fi 100 + 101 + if ! perf record -e task-clock --filter 'period > 0 || code_pgsz > 4096' \ 102 + -o /dev/null true 2>&1 | grep PERF_SAMPLE_CODE_PAGE_SIZE 103 + then 104 + echo "Group bpf-filter test [Failed forbidden CODE_PAGE_SIZE]" 105 + err=1 106 + return 107 + fi 108 + 109 + echo "Group bpf-filter test [Success]" 110 + } 111 + 112 + 113 + test_bpf_filter_priv 114 + 115 + if [ $err = 0 ]; then 116 + test_bpf_filter_basic 117 + fi 118 + 119 + if [ $err = 0 ]; then 120 + test_bpf_filter_fail 121 + fi 122 + 123 + if [ $err = 0 ]; then 124 + test_bpf_filter_group 125 + fi 126 + 127 + cleanup 128 + exit $err