Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # perf stat CSV output linter
0003 # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
0004 # Tests various perf stat CSV output commands for the
0005 # correct number of fields and the CSV separator set to ','.
0006 
0007 set -e
0008 
0009 function commachecker()
0010 {
0011         local -i cnt=0
0012         local exp=0
0013 
0014         case "$1"
0015         in "--no-args")         exp=6
0016         ;; "--system-wide")     exp=6
0017         ;; "--event")           exp=6
0018         ;; "--interval")        exp=7
0019         ;; "--per-thread")      exp=7
0020         ;; "--system-wide-no-aggr")     exp=7
0021                                 [ $(uname -m) = "s390x" ] && exp='^[6-7]$'
0022         ;; "--per-core")        exp=8
0023         ;; "--per-socket")      exp=8
0024         ;; "--per-node")        exp=8
0025         ;; "--per-die")         exp=8
0026         esac
0027 
0028         while read line
0029         do
0030                 # Check for lines beginning with Failed
0031                 x=${line:0:6}
0032                 [ "$x" = "Failed" ] && continue
0033 
0034                 # Count the number of commas
0035                 x=$(echo $line | tr -d -c ',')
0036                 cnt="${#x}"
0037                 # echo $line $cnt
0038                 [[ ! "$cnt" =~ $exp ]] && {
0039                         echo "wrong number of fields. expected $exp in $line" 1>&2
0040                         exit 1;
0041                 }
0042         done
0043         return 0
0044 }
0045 
0046 # Return true if perf_event_paranoid is > $1 and not running as root.
0047 function ParanoidAndNotRoot()
0048 {
0049          [ $(id -u) != 0 ] && [ $(cat /proc/sys/kernel/perf_event_paranoid) -gt $1 ]
0050 }
0051 
0052 check_no_args()
0053 {
0054         echo -n "Checking CSV output: no args "
0055         perf stat -x, true 2>&1 | commachecker --no-args
0056         echo "[Success]"
0057 }
0058 
0059 check_system_wide()
0060 {
0061         echo -n "Checking CSV output: system wide "
0062         if ParanoidAndNotRoot 0
0063         then
0064                 echo "[Skip] paranoid and not root"
0065                 return
0066         fi
0067         perf stat -x, -a true 2>&1 | commachecker --system-wide
0068         echo "[Success]"
0069 }
0070 
0071 check_system_wide_no_aggr()
0072 {
0073         echo -n "Checking CSV output: system wide "
0074         if ParanoidAndNotRoot 0
0075         then
0076                 echo "[Skip] paranoid and not root"
0077                 return
0078         fi
0079         echo -n "Checking CSV output: system wide no aggregation "
0080         perf stat -x, -A -a --no-merge true 2>&1 | commachecker --system-wide-no-aggr
0081         echo "[Success]"
0082 }
0083 
0084 check_interval()
0085 {
0086         echo -n "Checking CSV output: interval "
0087         perf stat -x, -I 1000 true 2>&1 | commachecker --interval
0088         echo "[Success]"
0089 }
0090 
0091 
0092 check_event()
0093 {
0094         echo -n "Checking CSV output: event "
0095         perf stat -x, -e cpu-clock true 2>&1 | commachecker --event
0096         echo "[Success]"
0097 }
0098 
0099 check_per_core()
0100 {
0101         echo -n "Checking CSV output: per core "
0102         if ParanoidAndNotRoot 0
0103         then
0104                 echo "[Skip] paranoid and not root"
0105                 return
0106         fi
0107         perf stat -x, --per-core -a true 2>&1 | commachecker --per-core
0108         echo "[Success]"
0109 }
0110 
0111 check_per_thread()
0112 {
0113         echo -n "Checking CSV output: per thread "
0114         if ParanoidAndNotRoot 0
0115         then
0116                 echo "[Skip] paranoid and not root"
0117                 return
0118         fi
0119         perf stat -x, --per-thread -a true 2>&1 | commachecker --per-thread
0120         echo "[Success]"
0121 }
0122 
0123 check_per_die()
0124 {
0125         echo -n "Checking CSV output: per die "
0126         if ParanoidAndNotRoot 0
0127         then
0128                 echo "[Skip] paranoid and not root"
0129                 return
0130         fi
0131         perf stat -x, --per-die -a true 2>&1 | commachecker --per-die
0132         echo "[Success]"
0133 }
0134 
0135 check_per_node()
0136 {
0137         echo -n "Checking CSV output: per node "
0138         if ParanoidAndNotRoot 0
0139         then
0140                 echo "[Skip] paranoid and not root"
0141                 return
0142         fi
0143         perf stat -x, --per-node -a true 2>&1 | commachecker --per-node
0144         echo "[Success]"
0145 }
0146 
0147 check_per_socket()
0148 {
0149         echo -n "Checking CSV output: per socket "
0150         if ParanoidAndNotRoot 0
0151         then
0152                 echo "[Skip] paranoid and not root"
0153                 return
0154         fi
0155         perf stat -x, --per-socket -a true 2>&1 | commachecker --per-socket
0156         echo "[Success]"
0157 }
0158 
0159 check_no_args
0160 check_system_wide
0161 check_system_wide_no_aggr
0162 check_interval
0163 check_event
0164 check_per_core
0165 check_per_thread
0166 check_per_die
0167 check_per_node
0168 check_per_socket
0169 exit 0