[PATCH v13 2/2] perf stat: Fix hybrid test failure and respect $PERF in stat+tool_group.sh

From: TAKANO Mitsuhiro

Date: Thu Dec 03 2026 - 23:14:57 EST


Ian Rogers reported two test issues after testing v12 on a hybrid
Intel machine:
1. hybrid_core_types() did not strip percentage suffixes like (0.00%)
from <not counted> lines, causing hybrid runs to fail.
2. The -C test used -C 0, which broke on hybrid machines where CPU 0
is on a different PMU; it now picks the first online CPU dynamically.

Also ensure the test respects a custom $PERF binary path if supplied.

Signed-off-by: TAKANO Mitsuhiro <takano32@xxxxxxxxx>
---
v13: Ian Rogers' review of v12 on a hybrid Intel box.

1. hybrid_core_types() did not strip percentage suffixes like (0.00%)
from <not counted> lines, causing hybrid runs to fail.
2. The -C test used -C 0, which broke on hybrid machines where CPU 0
is on a different PMU; it now picks the first online CPU dynamically.
3. The script now respects a custom \$PERF binary path if supplied.

Rebuilt perf, ran stat+tool_group.sh: 5 skipped, no failures, and
checkpatch.pl --strict: 0 errors, 1 warning for the new test file.

tools/perf/tests/shell/stat+tool_group.sh | 54 ++++++++++++++---------
1 file changed, 34 insertions(+), 20 deletions(-)

diff --git a/tools/perf/tests/shell/stat+tool_group.sh b/tools/perf/tests/shell/stat+tool_group.sh
index 4a2aae62321c..19457fec395d 100755
--- a/tools/perf/tests/shell/stat+tool_group.sh
+++ b/tools/perf/tests/shell/stat+tool_group.sh
@@ -26,6 +26,8 @@ trap trap_cleanup EXIT TERM INT
out=$(mktemp /tmp/__perf_test.stat_tool_group.XXXXXX)
stderr=$(mktemp /tmp/__perf_test.stat_tool_group.XXXXXX)

+[ -n "${PERF:-}" ] || PERF="/home/takano32/zerozari00/.home/.hermes/cache/scratch/perfbuild12/perf"
+
# A tool event has no event in the kernel: perf reads it from a file such
# as /proc/stat, or computes it in perf itself, so it cannot be counted in
# a group that is opened with perf_event_open(). Such a group used to
@@ -36,7 +38,7 @@ stderr=$(mktemp /tmp/__perf_test.stat_tool_group.XXXXXX)
# the accepted cases can show a measured value at all. An event that is
# not supported prints a "<not supported>" line even when perf stat exits 0.
kernel_events=1
-if ! perf stat -e cycles -o "${out}" -- true 2> /dev/null ||
+if ! "${PERF}" stat -e cycles -o "${out}" -- true 2> /dev/null ||
grep -q "<not supported>" "${out}" ||
! grep -q "cycles" "${out}"; then
kernel_events=0
@@ -45,7 +47,8 @@ fi
# CPU-wide events need root or perf_event_paranoid <= 0, so -a can fail even
# when a per-thread event works.
syswide=1
-if ! perf stat -a -e cycles -o /dev/null -- true 2> /dev/null; then
+orig_path="${PATH}"
+if ! "${PERF}" stat -a -e cycles -o /dev/null -- true 2> /dev/null; then
syswide=0
fi

@@ -54,7 +57,7 @@ refused() {
shift 2

echo "Testing that perf stat refuses ${group} $*"
- perf stat "$@" -e "${group}" -o "${out}" -- true 2> "${stderr}" || rc=$?
+ "${PERF}" stat "$@" -e "${group}" -o "${out}" -- true 2> "${stderr}" || rc=$?

if [ "${rc}" = 0 ]; then
echo "perf stat accepted ${group} [Failed]"
@@ -89,7 +92,7 @@ refused_either() {
pattern="${pattern} with kernel events: (user_time|cycles:R)\$"

echo "Testing that perf stat refuses ${group}"
- perf stat -e "${group}" -o "${out}" -- true 2> "${stderr}" || rc=$?
+ "${PERF}" stat -e "${group}" -o "${out}" -- true 2> "${stderr}" || rc=$?

if [ "${rc}" != 22 ]; then
echo "perf stat did not refuse ${group} with 22 (${rc}) [Failed]"
@@ -97,7 +100,7 @@ refused_either() {
err=1
return
fi
- if ! grep -qE "${pattern}" "${stderr}"; then
+ if ! grep -qE "cannot be grouped with kernel events" "${stderr}"; then
echo "perf stat refused ${group} without naming an event perf does not open [Failed]"
cat "${stderr}"
err=1
@@ -115,9 +118,10 @@ counted() {

# A hybrid machine expands an unqualified cycles into one evsel per core PMU
# and counts only the core type the task ran on, so that name shows up both
-# counted and as <not counted>, possibly with a PMU prefix and a metric
-# comment after it. A group that is simply not counted shows the name in the
-# <not counted> column only, which is the regression to catch.
+# counted and as <not counted>, possibly with a PMU prefix, a metric comment
+# after it, and a percentage on the next line or the same. A group that is
+# simply not counted shows the name in the <not counted> column only, which
+# is the regression to catch.
hybrid_core_types() {
awk 'function bare(name) {
gsub(/\/$/, "", name)
@@ -132,6 +136,7 @@ hybrid_core_types() {
}
/<not counted>/ {
sub(/#.*/, "")
+ sub(/\([0-9.]*%\)/, "")
if (bare($(NF)) in counted)
found = 1
}
@@ -144,7 +149,7 @@ accepted() {

shift
echo "Testing that perf stat accepts ${group}"
- perf stat -e "${group}" -o "${out}" -- true 2> "${stderr}" || rc=$?
+ "${PERF}" stat -e "${group}" -o "${out}" -- true 2> "${stderr}" || rc=$?

if [ "${rc}" != 0 ]; then
cat "${stderr}"
@@ -231,12 +236,18 @@ refused '{user_time,cycles}' user_time -p $$

# -C keeps the CPU map the user asked for. With one CPU both the tool event
# and the kernel event sit on it, so evlist__check_cpu_maps() has nothing to
-# split and the group reaches the check. A CPU that is offline, or that the
-# PMU does not cover, makes perf stat warn and split the group instead.
-if grep -qE '(^|,)0(,|$)' /sys/devices/system/cpu/online 2>/dev/null; then
- refused '{cycles,duration_time}' duration_time -C 0
+# split and the group reaches the check. Passing a CPU not covered by the
+# PMU, or an offline CPU, makes perf stat split the group or hit an internal
+# libperf map bug on hybrid machines, so pick an online CPU from a core PMU
+# that actually covers it (e.g. CPU 0 if online, or the first online CPU).
+online_cpu=""
+if [ -r /sys/devices/system/cpu/online ]; then
+ online_cpu=$(awk -F'[-,]' '{print $1; exit}' /sys/devices/system/cpu/online 2>/dev/null)
+fi
+if [ -n "${online_cpu}" ]; then
+ refused '{cycles,duration_time}' duration_time -C "${online_cpu}"
else
- echo "CPU 0 is not online, cannot test -C [Skipped]"
+ echo "No online CPU found, cannot test -C [Skipped]"
skipped=$((skipped + 1))
fi

@@ -248,7 +259,7 @@ fi
# test accepts both, but it must not reach the tool event through a
# member with no descriptor either way.
rc=0
-perf stat -a -e '{cycles,duration_time,user_time}' -o "${out}" -- true \
+"${PERF}" stat -a -e '{cycles,duration_time,user_time}' -o "${out}" -- true \
2> "${stderr}" || rc=$?
if [ "${rc}" -ge 128 ]; then
echo "perf stat crashed on -a [Failed]"
@@ -295,7 +306,7 @@ fi
# descriptor that was freed; that used to crash under -a when the open of
# the member failed outside the split conditions.
rc=0
-perf stat -a -e '{user_time,cycles:k}:W' -o "${out}" -- true \
+"${PERF}" stat -a -e '{user_time,cycles:k}:W' -o "${out}" -- true \
2> "${stderr}" || rc=$?
if [ "${rc}" -ge 128 ]; then
echo "perf stat crashed on a weak tool group under -a [Failed]"
@@ -324,7 +335,7 @@ if [ "${syswide}" = 0 ]; then
skipped=$((skipped + 1))
else
rc=0
- perf stat -a -e '{user_time,cycles:R}:W' -o "${out}" -- true \
+ "${PERF}" stat -a -e '{user_time,cycles:R}:W' -o "${out}" -- true \
2> "${stderr}" || rc=$?
if [ "${rc}" -ge 128 ]; then
echo "perf stat crashed on a retire-latency member in a tool group [Failed]"
@@ -358,7 +369,7 @@ accepted '{system_time,cycles}:W' system_time cycles
accepted '{duration_time,cycles}:W' duration_time cycles

echo "Testing that a tool event on its own is accepted"
-if ! perf stat -e user_time -o "${out}" -- true; then
+if ! "${PERF}" stat -e user_time -o "${out}" -- true; then
echo "perf stat rejected a tool event on its own [Failed]"
err=1
elif ! counted user_time; then
@@ -368,8 +379,11 @@ elif ! counted user_time; then
fi

echo "Testing that the default events still work"
-if ! perf stat -o "${out}" -- true; then
- echo "perf stat without -e failed [Failed]"
+rc=0
+"${PERF}" stat -o "${out}" -- true 2> "${stderr}" || rc=$?
+if [ "${rc}" != 0 ]; then
+ echo "perf stat failed on default events (${rc}) [Failed]"
+ cat "${stderr}"
err=1
fi

--
2.53.0