Linux Audio

Check our new training course

In-person Linux kernel drivers training

Jun 16-20, 2025
Register
Loading...
Note: File does not exist in v4.6.
  1#!/bin/sh
  2# perf stat tests
  3# SPDX-License-Identifier: GPL-2.0
  4
  5set -e
  6
  7err=0
  8test_default_stat() {
  9  echo "Basic stat command test"
 10  if ! perf stat true 2>&1 | grep -E -q "Performance counter stats for 'true':"
 11  then
 12    echo "Basic stat command test [Failed]"
 13    err=1
 14    return
 15  fi
 16  echo "Basic stat command test [Success]"
 17}
 18
 19test_stat_record_report() {
 20  echo "stat record and report test"
 21  if ! perf stat record -o - true | perf stat report -i - 2>&1 | \
 22    grep -E -q "Performance counter stats for 'pipe':"
 23  then
 24    echo "stat record and report test [Failed]"
 25    err=1
 26    return
 27  fi
 28  echo "stat record and report test [Success]"
 29}
 30
 31test_stat_record_script() {
 32  echo "stat record and script test"
 33  if ! perf stat record -o - true | perf script -i - 2>&1 | \
 34    grep -E -q "CPU[[:space:]]+THREAD[[:space:]]+VAL[[:space:]]+ENA[[:space:]]+RUN[[:space:]]+TIME[[:space:]]+EVENT"
 35  then
 36    echo "stat record and script test [Failed]"
 37    err=1
 38    return
 39  fi
 40  echo "stat record and script test [Success]"
 41}
 42
 43test_stat_repeat_weak_groups() {
 44  echo "stat repeat weak groups test"
 45  if ! perf stat -e '{cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles}' \
 46     true 2>&1 | grep -q 'seconds time elapsed'
 47  then
 48    echo "stat repeat weak groups test [Skipped event parsing failed]"
 49    return
 50  fi
 51  if ! perf stat -r2 -e '{cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles}:W' \
 52    true > /dev/null 2>&1
 53  then
 54    echo "stat repeat weak groups test [Failed]"
 55    err=1
 56    return
 57  fi
 58  echo "stat repeat weak groups test [Success]"
 59}
 60
 61test_topdown_groups() {
 62  # Topdown events must be grouped with the slots event first. Test that
 63  # parse-events reorders this.
 64  echo "Topdown event group test"
 65  if ! perf stat -e '{slots,topdown-retiring}' true > /dev/null 2>&1
 66  then
 67    echo "Topdown event group test [Skipped event parsing failed]"
 68    return
 69  fi
 70  if perf stat -e '{slots,topdown-retiring}' true 2>&1 | grep -E -q "<not supported>"
 71  then
 72    echo "Topdown event group test [Failed events not supported]"
 73    err=1
 74    return
 75  fi
 76  if perf stat -e 'instructions,topdown-retiring,slots' true 2>&1 | grep -E -q "<not supported>"
 77  then
 78    echo "Topdown event group test [Failed slots not reordered first in no-group case]"
 79    err=1
 80    return
 81  fi
 82  if perf stat -e '{instructions,topdown-retiring,slots}' true 2>&1 | grep -E -q "<not supported>"
 83  then
 84    echo "Topdown event group test [Failed slots not reordered first in single group case]"
 85    err=1
 86    return
 87  fi
 88  if perf stat -e '{instructions,slots},topdown-retiring' true 2>&1 | grep -E -q "<not supported>"
 89  then
 90    echo "Topdown event group test [Failed topdown metrics event not move into slots group]"
 91    err=1
 92    return
 93  fi
 94  if perf stat -e '{instructions,slots},{topdown-retiring}' true 2>&1 | grep -E -q "<not supported>"
 95  then
 96    echo "Topdown event group test [Failed topdown metrics group not merge into slots group]"
 97    err=1
 98    return
 99  fi
100  if perf stat -e '{instructions,r400,r8000}' true 2>&1 | grep -E -q "<not supported>"
101  then
102    echo "Topdown event group test [Failed raw format slots not reordered first]"
103    err=1
104    return
105  fi
106  echo "Topdown event group test [Success]"
107}
108
109test_topdown_weak_groups() {
110  # Weak groups break if the perf_event_open of multiple grouped events
111  # fails. Breaking a topdown group causes the events to fail. Test a very large
112  # grouping to see that the topdown events aren't broken out.
113  echo "Topdown weak groups test"
114  ok_grouping="{slots,topdown-bad-spec,topdown-be-bound,topdown-fe-bound,topdown-retiring},branch-instructions,branch-misses,bus-cycles,cache-misses,cache-references,cpu-cycles,instructions,mem-loads,mem-stores,ref-cycles,cache-misses,cache-references"
115  if ! perf stat --no-merge -e "$ok_grouping" true > /dev/null 2>&1
116  then
117    echo "Topdown weak groups test [Skipped event parsing failed]"
118    return
119  fi
120  group_needs_break="{slots,topdown-bad-spec,topdown-be-bound,topdown-fe-bound,topdown-retiring,branch-instructions,branch-misses,bus-cycles,cache-misses,cache-references,cpu-cycles,instructions,mem-loads,mem-stores,ref-cycles,cache-misses,cache-references}:W"
121  if perf stat --no-merge -e "$group_needs_break" true 2>&1 | grep -E -q "<not supported>"
122  then
123    echo "Topdown weak groups test [Failed events not supported]"
124    err=1
125    return
126  fi
127  echo "Topdown weak groups test [Success]"
128}
129
130test_cputype() {
131  # Test --cputype argument.
132  echo "cputype test"
133
134  # Bogus PMU should fail.
135  if perf stat --cputype="123" -e instructions true > /dev/null 2>&1
136  then
137    echo "cputype test [Bogus PMU didn't fail]"
138    err=1
139    return
140  fi
141
142  # Find a known PMU for cputype.
143  pmu=""
144  devs="/sys/bus/event_source/devices"
145  for i in $devs/cpu $devs/cpu_atom $devs/armv8_pmuv3_0 $devs/armv8_cortex_*
146  do
147    i_base=$(basename "$i")
148    if test -d "$i"
149    then
150      pmu="$i_base"
151      break
152    fi
153    if perf stat -e "$i_base/instructions/" true > /dev/null 2>&1
154    then
155      pmu="$i_base"
156      break
157    fi
158  done
159  if test "x$pmu" = "x"
160  then
161    echo "cputype test [Skipped known PMU not found]"
162    return
163  fi
164
165  # Test running with cputype produces output.
166  if ! perf stat --cputype="$pmu" -e instructions true 2>&1 | grep -E -q "instructions"
167  then
168    echo "cputype test [Failed count missed with given filter]"
169    err=1
170    return
171  fi
172  echo "cputype test [Success]"
173}
174
175test_hybrid() {
176  # Test the default stat command on hybrid devices opens one cycles event for
177  # each CPU type.
178  echo "hybrid test"
179
180  # Count the number of core PMUs, assume minimum of 1
181  pmus=$(ls /sys/bus/event_source/devices/*/cpus 2>/dev/null | wc -l)
182  if [ "$pmus" -lt 1 ]
183  then
184    pmus=1
185  fi
186
187  # Run default Perf stat
188  cycles_events=$(perf stat -- true 2>&1 | grep -E "/cycles/[uH]*|  cycles[:uH]*  " -c)
189
190  # The expectation is that default output will have a cycles events on each
191  # hybrid PMU. In situations with no cycles PMU events, like virtualized, this
192  # can fall back to task-clock and so the end count may be 0. Fail if neither
193  # condition holds.
194  if [ "$pmus" -ne "$cycles_events" ] && [ "0" -ne "$cycles_events" ]
195  then
196    echo "hybrid test [Found $pmus PMUs but $cycles_events cycles events. Failed]"
197    err=1
198    return
199  fi
200  echo "hybrid test [Success]"
201}
202
203test_default_stat
204test_stat_record_report
205test_stat_record_script
206test_stat_repeat_weak_groups
207test_topdown_groups
208test_topdown_weak_groups
209test_cputype
210test_hybrid
211exit $err