xref: /linux/tools/perf/tests/shell/stat+event_uniquifying.sh (revision e3966940559d52aa1800a008dcfeec218dd31f88)
1#!/bin/bash
2# perf stat events uniquifying
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7err=0
8stat_output=$(mktemp /tmp/__perf_test.stat_output.XXXXX)
9
10cleanup() {
11  rm -f "${stat_output}"
12
13  trap - EXIT TERM INT
14}
15
16trap_cleanup() {
17  echo "Unexpected signal in ${FUNCNAME[1]}"
18  cleanup
19  exit 1
20}
21trap trap_cleanup EXIT TERM INT
22
23test_event_uniquifying() {
24  echo "Uniquification of PMU sysfs events test"
25
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
61  done
62}
63
64test_event_uniquifying
65cleanup
66exit $err
67