1#!/bin/bash
2# perf all metrics test
3# SPDX-License-Identifier: GPL-2.0
4
5ParanoidAndNotRoot()
6{
7  [ "$(id -u)" != 0 ] && [ "$(cat /proc/sys/kernel/perf_event_paranoid)" -gt $1 ]
8}
9
10system_wide_flag="-a"
11if ParanoidAndNotRoot 0
12then
13  system_wide_flag=""
14fi
15
16err=0
17for m in $(perf list --raw-dump metrics); do
18  echo "Testing $m"
19  result=$(perf stat -M "$m" $system_wide_flag -- sleep 0.01 2>&1)
20  result_err=$?
21  if [[ $result_err -gt 0 ]]
22  then
23    if [[ "$result" =~ "Cannot resolve IDs for" ]]
24    then
25      echo "Metric contains missing events"
26      echo $result
27      err=1 # Fail
28      continue
29    elif [[ "$result" =~ \
30          "Access to performance monitoring and observability operations is limited" ]]
31    then
32      echo "Permission failure"
33      echo $result
34      if [[ $err -eq 0 ]]
35      then
36        err=2 # Skip
37      fi
38      continue
39    elif [[ "$result" =~ "in per-thread mode, enable system wide" ]]
40    then
41      echo "Permissions - need system wide mode"
42      echo $result
43      if [[ $err -eq 0 ]]
44      then
45        err=2 # Skip
46      fi
47      continue
48    elif [[ "$result" =~ "<not supported>" ]]
49    then
50      echo "Not supported events"
51      echo $result
52      if [[ $err -eq 0 ]]
53      then
54        err=2 # Skip
55      fi
56      continue
57    elif [[ "$result" =~ "FP_ARITH" || "$result" =~ "AMX" ]]
58    then
59      echo "FP issues"
60      echo $result
61      if [[ $err -eq 0 ]]
62      then
63        err=2 # Skip
64      fi
65      continue
66    elif [[ "$result" =~ "PMM" ]]
67    then
68      echo "Optane memory issues"
69      echo $result
70      if [[ $err -eq 0 ]]
71      then
72        err=2 # Skip
73      fi
74      continue
75    fi
76  fi
77
78  if [[ "$result" =~ ${m:0:50} ]]
79  then
80    continue
81  fi
82
83  # Failed, possibly the workload was too small so retry with something longer.
84  result=$(perf stat -M "$m" $system_wide_flag -- perf bench internals synthesize 2>&1)
85  if [[ "$result" =~ ${m:0:50} ]]
86  then
87    continue
88  fi
89  echo "Metric '$m' not printed in:"
90  echo "$result"
91  err=1
92done
93
94exit "$err"
95