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

perf test: Extra verbosity and hypervisor skip for tpebs test

When not running as root and with higher perf event paranoia values
the perf record forked by TPEBS can fail to attach to the process. Skip
the test in these scenarios.

Intel TPEBS test skips on non-Intel CPUs. On Intel CPUs under a
hypervisor the cache-misses event may not be present or precise. Skip
the test under this condition.

Refactor the output code to be placed in a file so that on a signal
the file can be dumped. This was necessary to catch the issue above as
the failing perf record command would fail without output.

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Thomas Falcon <thomas.falcon@intel.com>
Cc: Weilin Wang <weilin.wang@intel.com>
Cc: James Clark <james.clark@linaro.org>
Link: https://lore.kernel.org/r/20250130170135.5817-1-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Ian Rogers and committed by
Namhyung Kim
bb4b8f96 4c4c0724

+76 -13
+76 -13
tools/perf/tests/shell/test_stat_intel_tpebs.sh
··· 3 3 # SPDX-License-Identifier: GPL-2.0 4 4 5 5 set -e 6 - grep -q GenuineIntel /proc/cpuinfo || { echo Skipping non-Intel; exit 2; } 7 6 8 - # Use this event for testing because it should exist in all platforms 9 - event=cache-misses:R 7 + ParanoidAndNotRoot() { 8 + [ "$(id -u)" != 0 ] && [ "$(cat /proc/sys/kernel/perf_event_paranoid)" -gt $1 ] 9 + } 10 10 11 - # Hybrid platforms output like "cpu_atom/cache-misses/R", rather than as above 12 - alt_name=/cache-misses/R 11 + if ! grep -q GenuineIntel /proc/cpuinfo 12 + then 13 + echo "Skipping non-Intel" 14 + exit 2 15 + fi 13 16 14 - # Without this cmd option, default value or zero is returned 15 - #echo "Testing without --record-tpebs" 16 - #result=$(perf stat -e "$event" true 2>&1) 17 - #[[ "$result" =~ $event || "$result" =~ $alt_name ]] || exit 1 17 + if ParanoidAndNotRoot 0 18 + then 19 + echo "Skipping paranoid >0 and not root" 20 + exit 2 21 + fi 18 22 19 - # In platforms that do not support TPEBS, it should execute without error. 20 - echo "Testing with --record-tpebs" 21 - result=$(perf stat -e "$event" --record-tpebs -a sleep 0.01 2>&1) 22 - [[ "$result" =~ "perf record" && "$result" =~ $event || "$result" =~ $alt_name ]] || exit 1 23 + stat_output=$(mktemp /tmp/__perf_stat_tpebs_output.XXXXX) 24 + 25 + cleanup() { 26 + rm -rf "${stat_output}" 27 + trap - EXIT TERM INT 28 + } 29 + 30 + trap_cleanup() { 31 + echo "Unexpected signal in ${FUNCNAME[1]}" 32 + cat "${stat_output}" 33 + cleanup 34 + exit 1 35 + } 36 + trap trap_cleanup EXIT TERM INT 37 + 38 + # Event to be used in tests 39 + event=cache-misses 40 + 41 + if ! perf record -e "${event}:p" -a -o /dev/null sleep 0.01 > "${stat_output}" 2>&1 42 + then 43 + echo "Missing ${event} support" 44 + cleanup 45 + exit 2 46 + fi 47 + 48 + test_with_record_tpebs() { 49 + echo "Testing with --record-tpebs" 50 + if ! perf stat -e "${event}:R" --record-tpebs -a sleep 0.01 > "${stat_output}" 2>&1 51 + then 52 + echo "Testing with --record-tpebs [Failed perf stat]" 53 + cat "${stat_output}" 54 + exit 1 55 + fi 56 + 57 + # Expected output: 58 + # $ perf stat --record-tpebs -e cache-misses:R -a sleep 0.01 59 + # Events enabled 60 + # [ perf record: Woken up 2 times to write data ] 61 + # [ perf record: Captured and wrote 0.056 MB - ] 62 + # 63 + # Performance counter stats for 'system wide': 64 + # 65 + # 0 cache-misses:R 66 + # 67 + # 0.013963299 seconds time elapsed 68 + if ! grep "perf record" "${stat_output}" 69 + then 70 + echo "Testing with --record-tpebs [Failed missing perf record]" 71 + cat "${stat_output}" 72 + exit 1 73 + fi 74 + if ! grep "${event}:R" "${stat_output}" && ! grep "/${event}/R" "${stat_output}" 75 + then 76 + echo "Testing with --record-tpebs [Failed missing event name]" 77 + cat "${stat_output}" 78 + exit 1 79 + fi 80 + echo "Testing with --record-tpebs [Success]" 81 + } 82 + 83 + test_with_record_tpebs 84 + cleanup 85 + exit 0