2 # perf stat JSON output linter
3 # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
4 # Checks various perf stat JSON output commands for the
5 # correct number of fields.
11 pythonchecker=$(dirname $0)/lib/perf_json_output_lint.py
12 if [ "x$PYTHON" == "x" ]
14 if which python3 > /dev/null
17 elif which python > /dev/null
21 echo Skipping test, python not detected please set environment variable PYTHON.
26 stat_output=$(mktemp /tmp/__perf_test.stat_output.json.XXXXX)
29 rm -f "${stat_output}"
38 trap trap_cleanup EXIT TERM INT
40 # Return true if perf_event_paranoid is > $1 and not running as root.
41 function ParanoidAndNotRoot()
43 [ "$(id -u)" != 0 ] && [ "$(cat /proc/sys/kernel/perf_event_paranoid)" -gt $1 ]
48 echo -n "Checking json output: no args "
49 perf stat -j -o "${stat_output}" true
50 $PYTHON $pythonchecker --no-args --file "${stat_output}"
56 echo -n "Checking json output: system wide "
57 if ParanoidAndNotRoot 0
59 echo "[Skip] paranoia and not root"
62 perf stat -j -a -o "${stat_output}" true
63 $PYTHON $pythonchecker --system-wide --file "${stat_output}"
67 check_system_wide_no_aggr()
69 echo -n "Checking json output: system wide no aggregation "
70 if ParanoidAndNotRoot 0
72 echo "[Skip] paranoia and not root"
75 perf stat -j -A -a --no-merge -o "${stat_output}" true
76 $PYTHON $pythonchecker --system-wide-no-aggr --file "${stat_output}"
82 echo -n "Checking json output: interval "
83 perf stat -j -I 1000 -o "${stat_output}" true
84 $PYTHON $pythonchecker --interval --file "${stat_output}"
91 echo -n "Checking json output: event "
92 perf stat -j -e cpu-clock -o "${stat_output}" true
93 $PYTHON $pythonchecker --event --file "${stat_output}"
99 echo -n "Checking json output: per core "
100 if ParanoidAndNotRoot 0
102 echo "[Skip] paranoia and not root"
105 perf stat -j --per-core -a -o "${stat_output}" true
106 $PYTHON $pythonchecker --per-core --file "${stat_output}"
112 echo -n "Checking json output: per thread "
113 if ParanoidAndNotRoot 0
115 echo "[Skip] paranoia and not root"
118 perf stat -j --per-thread -a -o "${stat_output}" true
119 $PYTHON $pythonchecker --per-thread --file "${stat_output}"
123 check_per_cache_instance()
125 echo -n "Checking json output: per cache_instance "
126 if ParanoidAndNotRoot 0
128 echo "[Skip] paranoia and not root"
131 perf stat -j --per-cache -a true 2>&1 | $PYTHON $pythonchecker --per-cache
137 echo -n "Checking json output: per die "
138 if ParanoidAndNotRoot 0
140 echo "[Skip] paranoia and not root"
143 perf stat -j --per-die -a -o "${stat_output}" true
144 $PYTHON $pythonchecker --per-die --file "${stat_output}"
150 echo -n "Checking json output: per node "
151 if ParanoidAndNotRoot 0
153 echo "[Skip] paranoia and not root"
156 perf stat -j --per-node -a -o "${stat_output}" true
157 $PYTHON $pythonchecker --per-node --file "${stat_output}"
163 echo -n "Checking json output: per socket "
164 if ParanoidAndNotRoot 0
166 echo "[Skip] paranoia and not root"
169 perf stat -j --per-socket -a -o "${stat_output}" true
170 $PYTHON $pythonchecker --per-socket --file "${stat_output}"
174 # The perf stat options for per-socket, per-core, per-die
175 # and -A ( no_aggr mode ) uses the info fetched from this
176 # directory: "/sys/devices/system/cpu/cpu*/topology". For
177 # example, socket value is fetched from "physical_package_id"
178 # file in topology directory.
179 # Reference: cpu__get_topology_int in util/cpumap.c
180 # If the platform doesn't expose topology information, values
181 # will be set to -1. For example, incase of pSeries platform
182 # of powerpc, value for "physical_package_id" is restricted
183 # and set to -1. Check here validates the socket-id read from
184 # topology file before proceeding further
186 FILE_LOC="/sys/devices/system/cpu/cpu*/topology/"
187 FILE_NAME="physical_package_id"
191 if ! ParanoidAndNotRoot 0
193 socket_file=`ls $FILE_LOC/$FILE_NAME | head -n 1`
194 [ -z $socket_file ] && return 0
195 socket_id=`cat $socket_file`
196 [ $socket_id == -1 ] && skip_test=1
208 if [ $skip_test -ne 1 ]
210 check_system_wide_no_aggr
212 check_per_cache_instance
216 echo "[Skip] Skipping tests for system_wide_no_aggr, per_core, per_die and per_socket since socket id exposed via topology is invalid"