[PATCH] tools/perf/test: Check for perf stat return code in perf all PMU test

Athira Rajeev
2026-03-15T10:58:13Z

Currently in "perf all PMU test", for "perf stat -e <event> true",
below checks are done:
- if return code is zero, look for "not supported" to decide pass
scenario
- check for "not supported" to ignore the event
- looks for "No permission to enable" to skip the event.
- If output has "Bad event name", fail the test.
- Use "Access to performance monitoring and observability operations is
limited." to ignore fail due to access limitations

If we failed to see event and it is supported, retries with longer
workload "perf bench internals synthesize".
- Here if output has <event>, the test is a pass.

Snippet of code check:
```
output=$(perf stat -e "$p" perf bench internals synthesize 2>&1)
if echo "$output" | grep -q "$p"
```
- if output doesn't have event printed in logs, considers it fail.

But this results in false pass for events in some cases.
Example, if perf stat fails as below:

# ./perf stat -e pmu/event/ true
event syntax error: 'pmu/event/'
\___ Bad event or PMU

Unable to find PMU or event on a PMU of 'pmu'
Run 'perf list' for a list of valid events

Usage: perf stat [<options>] [<command>]

-e, --event <event> event selector. use 'perf list' to list available events
# echo $?
129

Since this has non-zero return code and doesn't have the
fail strings being checked in the test, it will enter check using
longer workload. and since the output fail log has event, it
declares test as "supported".

Since all the fail strings can't be added in the check, update
the testcase to check return code before proceeding to longer
workload run.

Another missing scenario is when system wide monitoring is supported
example:
# ./perf stat -e pmu/event/ true
Error:
No supported events found.
Unsupported event (pmu/event/H) in per-thread mode, enable system wide with '-a'.

Update testcase to check with "perf stat -a -e $p" as well

Signed-off-by: Athira Rajeev <atrajeev@linux.ibm.com>
---
tools/perf/tests/shell/stat_all_pmu.sh | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)

diff --git a/tools/perf/tests/shell/stat_all_pmu.sh b/tools/perf/tests/shell/stat_all_pmu.sh
index 9c466c0efa85..6c4d59cbfa5f 100755
--- a/tools/perf/tests/shell/stat_all_pmu.sh
+++ b/tools/perf/tests/shell/stat_all_pmu.sh
@@ -53,6 +53,26 @@ do
continue
fi

+ # check with system wide if it is supported.
+ output=$(perf stat -a -e "$p" true 2>&1)
+ stat_result=$?
+ if echo "$output" | grep -q "not supported"
+ then
+ # Event not supported, so ignore.
+ echo "not supported"
+ continue
+ fi
+
+ # checked through possible access limitations and permissions.
+ # At this step, non-zero return code from "perf stat" needs to
+ # reported as fail for the user to investigate
+ if [ $stat_result -ne 0 ]
+ then
+ echo "perf stat failed with non-zero return code"
+ err=1
+ continue
+ fi
+
# We failed to see the event and it is supported. Possibly the workload was
# too small so retry with something longer.
output=$(perf stat -e "$p" perf bench internals synthesize 2>&1)
--
2.47.3