223 lines · bash
1#!/bin/bash2# perf stat JSON output linter3# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)4# Checks various perf stat JSON output commands for the5# correct number of fields.6 7set -e8 9skip_test=010 11shelldir=$(dirname "$0")12# shellcheck source=lib/setup_python.sh13. "${shelldir}"/lib/setup_python.sh14pythonchecker=$(dirname $0)/lib/perf_json_output_lint.py15 16stat_output=$(mktemp /tmp/__perf_test.stat_output.json.XXXXX)17 18cleanup() {19 rm -f "${stat_output}"20 21 trap - EXIT TERM INT22}23 24trap_cleanup() {25 cleanup26 exit 127}28trap trap_cleanup EXIT TERM INT29 30# Return true if perf_event_paranoid is > $1 and not running as root.31function ParanoidAndNotRoot()32{33 [ "$(id -u)" != 0 ] && [ "$(cat /proc/sys/kernel/perf_event_paranoid)" -gt $1 ]34}35 36check_no_args()37{38 echo -n "Checking json output: no args "39 perf stat -j -o "${stat_output}" true40 $PYTHON $pythonchecker --no-args --file "${stat_output}"41 echo "[Success]"42}43 44check_system_wide()45{46 echo -n "Checking json output: system wide "47 if ParanoidAndNotRoot 048 then49 echo "[Skip] paranoia and not root"50 return51 fi52 perf stat -j -a -o "${stat_output}" true53 $PYTHON $pythonchecker --system-wide --file "${stat_output}"54 echo "[Success]"55}56 57check_system_wide_no_aggr()58{59 echo -n "Checking json output: system wide no aggregation "60 if ParanoidAndNotRoot 061 then62 echo "[Skip] paranoia and not root"63 return64 fi65 perf stat -j -A -a --no-merge -o "${stat_output}" true66 $PYTHON $pythonchecker --system-wide-no-aggr --file "${stat_output}"67 echo "[Success]"68}69 70check_interval()71{72 echo -n "Checking json output: interval "73 perf stat -j -I 1000 -o "${stat_output}" true74 $PYTHON $pythonchecker --interval --file "${stat_output}"75 echo "[Success]"76}77 78 79check_event()80{81 echo -n "Checking json output: event "82 perf stat -j -e cpu-clock -o "${stat_output}" true83 $PYTHON $pythonchecker --event --file "${stat_output}"84 echo "[Success]"85}86 87check_per_core()88{89 echo -n "Checking json output: per core "90 if ParanoidAndNotRoot 091 then92 echo "[Skip] paranoia and not root"93 return94 fi95 perf stat -j --per-core -a -o "${stat_output}" true96 $PYTHON $pythonchecker --per-core --file "${stat_output}"97 echo "[Success]"98}99 100check_per_thread()101{102 echo -n "Checking json output: per thread "103 if ParanoidAndNotRoot 0104 then105 echo "[Skip] paranoia and not root"106 return107 fi108 perf stat -j --per-thread -p $$ -o "${stat_output}" true109 $PYTHON $pythonchecker --per-thread --file "${stat_output}"110 echo "[Success]"111}112 113check_per_cache_instance()114{115 echo -n "Checking json output: per cache_instance "116 if ParanoidAndNotRoot 0117 then118 echo "[Skip] paranoia and not root"119 return120 fi121 perf stat -j --per-cache -a true 2>&1 | $PYTHON $pythonchecker --per-cache122 echo "[Success]"123}124 125check_per_cluster()126{127 echo -n "Checking json output: per cluster "128 if ParanoidAndNotRoot 0129 then130 echo "[Skip] paranoia and not root"131 return132 fi133 perf stat -j --per-cluster -a true 2>&1 | $PYTHON $pythonchecker --per-cluster134 echo "[Success]"135}136 137check_per_die()138{139 echo -n "Checking json output: per die "140 if ParanoidAndNotRoot 0141 then142 echo "[Skip] paranoia and not root"143 return144 fi145 perf stat -j --per-die -a -o "${stat_output}" true146 $PYTHON $pythonchecker --per-die --file "${stat_output}"147 echo "[Success]"148}149 150check_per_node()151{152 echo -n "Checking json output: per node "153 if ParanoidAndNotRoot 0154 then155 echo "[Skip] paranoia and not root"156 return157 fi158 perf stat -j --per-node -a -o "${stat_output}" true159 $PYTHON $pythonchecker --per-node --file "${stat_output}"160 echo "[Success]"161}162 163check_per_socket()164{165 echo -n "Checking json output: per socket "166 if ParanoidAndNotRoot 0167 then168 echo "[Skip] paranoia and not root"169 return170 fi171 perf stat -j --per-socket -a -o "${stat_output}" true172 $PYTHON $pythonchecker --per-socket --file "${stat_output}"173 echo "[Success]"174}175 176# The perf stat options for per-socket, per-core, per-die177# and -A ( no_aggr mode ) uses the info fetched from this178# directory: "/sys/devices/system/cpu/cpu*/topology". For179# example, socket value is fetched from "physical_package_id"180# file in topology directory.181# Reference: cpu__get_topology_int in util/cpumap.c182# If the platform doesn't expose topology information, values183# will be set to -1. For example, incase of pSeries platform184# of powerpc, value for "physical_package_id" is restricted185# and set to -1. Check here validates the socket-id read from186# topology file before proceeding further187 188FILE_LOC="/sys/devices/system/cpu/cpu*/topology/"189FILE_NAME="physical_package_id"190 191check_for_topology()192{193 if ! ParanoidAndNotRoot 0194 then195 socket_file=`ls $FILE_LOC/$FILE_NAME | head -n 1`196 [ -z $socket_file ] && return 0197 socket_id=`cat $socket_file`198 [ $socket_id == -1 ] && skip_test=1199 return 0200 fi201}202 203check_for_topology204check_no_args205check_system_wide206check_interval207check_event208check_per_thread209check_per_node210if [ $skip_test -ne 1 ]211then212 check_system_wide_no_aggr213 check_per_core214 check_per_cache_instance215 check_per_cluster216 check_per_die217 check_per_socket218else219 echo "[Skip] Skipping tests for system_wide_no_aggr, per_core, per_die and per_socket since socket id exposed via topology is invalid"220fi221cleanup222exit 0223