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

perf test: Avoid uncore_imc/clockticks in uniquification test

The detection of uncore_imc may happen for free running PMUs and the
clockticks event may be present on uncore_clock. Rewrite the test to
detect duplicated/deduplicated events from perf list, not hardcoded to
uncore_imc.

If perf stat fails then assume it is permissions and skip the test.

Committer testing:

Before:

root@x1:~# perf test -vv uniquifyi
96: perf stat events uniquifying:
--- start ---
test child forked, pid 220851
stat event uniquifying test
grep: Unmatched [, [^, [:, [., or [=
Event is not uniquified [Failed]
perf stat -e clockticks -A -o /tmp/__perf_test.stat_output.X7ChD -- true
# started on Fri Sep 19 16:48:38 2025

Performance counter stats for 'system wide':

CPU0 2,310,956 uncore_clock/clockticks/

0.001746771 seconds time elapsed

---- end(-1) ----
96: perf stat events uniquifying : FAILED!
root@x1:~#

After:

root@x1:~# perf test -vv uniquifyi
96: perf stat events uniquifying:
--- start ---
test child forked, pid 222366
Uniquification of PMU sysfs events test
Testing event uncore_imc_free_running/data_read/ is uniquified to uncore_imc_free_running_0/data_read/
Testing event uncore_imc_free_running/data_total/ is uniquified to uncore_imc_free_running_0/data_total/
Testing event uncore_imc_free_running/data_write/ is uniquified to uncore_imc_free_running_0/data_write/
Testing event uncore_imc_free_running/data_read/ is uniquified to uncore_imc_free_running_1/data_read/
Testing event uncore_imc_free_running/data_total/ is uniquified to uncore_imc_free_running_1/data_total/
Testing event uncore_imc_free_running/data_write/ is uniquified to uncore_imc_free_running_1/data_write/
---- end(0) ----
96: perf stat events uniquifying : Ok
root@x1:~#

Fixes: 070b315333ee942f ("perf test: Restrict uniquifying test to machines with 'uncore_imc'")
Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.ibm.com>
Cc: Chun-Tse Shao <ctshao@google.com>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
edaeb4bc 69310179

+51 -62
+51 -62
tools/perf/tests/shell/stat+event_uniquifying.sh
··· 4 4 5 5 set -e 6 6 7 - stat_output=$(mktemp /tmp/__perf_test.stat_output.XXXXX) 8 - perf_tool=perf 9 7 err=0 8 + stat_output=$(mktemp /tmp/__perf_test.stat_output.XXXXX) 9 + 10 + cleanup() { 11 + rm -f "${stat_output}" 12 + 13 + trap - EXIT TERM INT 14 + } 15 + 16 + trap_cleanup() { 17 + echo "Unexpected signal in ${FUNCNAME[1]}" 18 + cleanup 19 + exit 1 20 + } 21 + trap trap_cleanup EXIT TERM INT 10 22 11 23 test_event_uniquifying() { 12 - # We use `clockticks` in `uncore_imc` to verify the uniquify behavior. 13 - pmu="uncore_imc" 14 - event="clockticks" 24 + echo "Uniquification of PMU sysfs events test" 15 25 16 - # If the `-A` option is added, the event should be uniquified. 17 - # 18 - # $perf list -v clockticks 19 - # 20 - # List of pre-defined events (to be used in -e or -M): 21 - # 22 - # uncore_imc_0/clockticks/ [Kernel PMU event] 23 - # uncore_imc_1/clockticks/ [Kernel PMU event] 24 - # uncore_imc_2/clockticks/ [Kernel PMU event] 25 - # uncore_imc_3/clockticks/ [Kernel PMU event] 26 - # uncore_imc_4/clockticks/ [Kernel PMU event] 27 - # uncore_imc_5/clockticks/ [Kernel PMU event] 28 - # 29 - # ... 30 - # 31 - # $perf stat -e clockticks -A -- true 32 - # 33 - # Performance counter stats for 'system wide': 34 - # 35 - # CPU0 3,773,018 uncore_imc_0/clockticks/ 36 - # CPU0 3,609,025 uncore_imc_1/clockticks/ 37 - # CPU0 0 uncore_imc_2/clockticks/ 38 - # CPU0 3,230,009 uncore_imc_3/clockticks/ 39 - # CPU0 3,049,897 uncore_imc_4/clockticks/ 40 - # CPU0 0 uncore_imc_5/clockticks/ 41 - # 42 - # 0.002029828 seconds time elapsed 43 - 44 - echo "stat event uniquifying test" 45 - uniquified_event_array=() 46 - 47 - # Skip if the machine does not have `uncore_imc` device. 48 - if ! ${perf_tool} list pmu | grep -q ${pmu}; then 49 - echo "Target does not support PMU ${pmu} [Skipped]" 50 - err=2 51 - return 52 - fi 53 - 54 - # Check how many uniquified events. 55 - while IFS= read -r line; do 56 - uniquified_event=$(echo "$line" | awk '{print $1}') 57 - uniquified_event_array+=("${uniquified_event}") 58 - done < <(${perf_tool} list -v ${event} | grep ${pmu}) 59 - 60 - perf_command="${perf_tool} stat -e $event -A -o ${stat_output} -- true" 61 - $perf_command 62 - 63 - # Check the output contains all uniquified events. 64 - for uniquified_event in "${uniquified_event_array[@]}"; do 65 - if ! cat "${stat_output}" | grep -q "${uniquified_event}"; then 66 - echo "Event is not uniquified [Failed]" 67 - echo "${perf_command}" 68 - cat "${stat_output}" 69 - err=1 70 - break 71 - fi 26 + # Read events from perf list with and without -v. With -v the duplicate PMUs 27 + # aren't deduplicated. Note, json events are listed by perf list without a 28 + # PMU. 29 + read -ra pmu_events <<< "$(perf list --raw pmu)" 30 + read -ra pmu_v_events <<< "$(perf list -v --raw pmu)" 31 + # For all non-deduplicated events. 32 + for pmu_v_event in "${pmu_v_events[@]}"; do 33 + # If the event matches an event in the deduplicated events then it musn't 34 + # be an event with duplicate PMUs, continue the outer loop. 35 + for pmu_event in "${pmu_events[@]}"; do 36 + if [[ "$pmu_v_event" == "$pmu_event" ]]; then 37 + continue 2 38 + fi 39 + done 40 + # Strip the suffix from the non-deduplicated event's PMU. 41 + event=$(echo "$pmu_v_event" | sed -E 's/_[0-9]+//') 42 + for pmu_event in "${pmu_events[@]}"; do 43 + if [[ "$event" == "$pmu_event" ]]; then 44 + echo "Testing event ${event} is uniquified to ${pmu_v_event}" 45 + if ! perf stat -e "$event" -A -o ${stat_output} -- true; then 46 + echo "Error running perf stat for event '$event' [Skip]" 47 + if [ $err = 0 ]; then 48 + err=2 49 + fi 50 + continue 51 + fi 52 + # Ensure the non-deduplicated event appears in the output. 53 + if ! grep -q "${pmu_v_event}" "${stat_output}"; then 54 + echo "Uniquification of PMU sysfs events test [Failed]" 55 + cat "${stat_output}" 56 + err=1 57 + fi 58 + break 59 + fi 60 + done 72 61 done 73 62 } 74 63 75 64 test_event_uniquifying 76 - rm -f "${stat_output}" 65 + cleanup 77 66 exit $err