xref: /linux/tools/perf/tests/shell/stat+event_uniquifying.sh (revision 7c7f9dd1ea3fc6b175b7227bb473dc883b925548)
1#!/bin/bash
2# perf stat events uniquifying
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7stat_output=$(mktemp /tmp/__perf_test.stat_output.XXXXX)
8perf_tool=perf
9err=0
10
11test_event_uniquifying() {
12  # We use `clockticks` in `uncore_imc` to verify the uniquify behavior.
13  pmu="uncore_imc"
14  event="clockticks"
15
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
72  done
73}
74
75test_event_uniquifying
76rm -f "${stat_output}"
77exit $err
78