183 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03 4# Return true if perf_event_paranoid is > $1 and not running as root.5function ParanoidAndNotRoot()6{7 [ "$(id -u)" != 0 ] && [ "$(cat /proc/sys/kernel/perf_event_paranoid)" -gt $1 ]8}9 10# $1 name $2 extra_opt11check_no_args()12{13 echo -n "Checking $1 output: no args "14 perf stat $2 true15 commachecker --no-args16 echo "[Success]"17}18 19check_system_wide()20{21 echo -n "Checking $1 output: system wide "22 if ParanoidAndNotRoot 023 then24 echo "[Skip] paranoid and not root"25 return26 fi27 perf stat -a $2 true28 commachecker --system-wide29 echo "[Success]"30}31 32check_system_wide_no_aggr()33{34 echo -n "Checking $1 output: system wide no aggregation "35 if ParanoidAndNotRoot 036 then37 echo "[Skip] paranoid and not root"38 return39 fi40 perf stat -A -a --no-merge $2 true41 commachecker --system-wide-no-aggr42 echo "[Success]"43}44 45check_interval()46{47 echo -n "Checking $1 output: interval "48 perf stat -I 1000 $2 true49 commachecker --interval50 echo "[Success]"51}52 53check_event()54{55 echo -n "Checking $1 output: event "56 perf stat -e cpu-clock $2 true57 commachecker --event58 echo "[Success]"59}60 61check_per_core()62{63 echo -n "Checking $1 output: per core "64 if ParanoidAndNotRoot 065 then66 echo "[Skip] paranoid and not root"67 return68 fi69 perf stat --per-core -a $2 true70 commachecker --per-core71 echo "[Success]"72}73 74check_per_thread()75{76 echo -n "Checking $1 output: per thread "77 if ParanoidAndNotRoot 078 then79 echo "[Skip] paranoid and not root"80 return81 fi82 perf stat --per-thread -p $$ $2 true83 commachecker --per-thread84 echo "[Success]"85}86 87check_per_cache_instance()88{89 echo -n "Checking $1 output: per cache instance "90 if ParanoidAndNotRoot 091 then92 echo "[Skip] paranoid and not root"93 return94 fi95 perf stat --per-cache -a $2 true96 commachecker --per-cache97 echo "[Success]"98}99 100check_per_cluster()101{102 echo -n "Checking $1 output: per cluster "103 if ParanoidAndNotRoot 0104 then105 echo "[Skip] paranoid and not root"106 return107 fi108 perf stat --per-cluster -a $2 true109 echo "[Success]"110}111 112check_per_die()113{114 echo -n "Checking $1 output: per die "115 if ParanoidAndNotRoot 0116 then117 echo "[Skip] paranoid and not root"118 return119 fi120 perf stat --per-die -a $2 true121 commachecker --per-die122 echo "[Success]"123}124 125check_per_node()126{127 echo -n "Checking $1 output: per node "128 if ParanoidAndNotRoot 0129 then130 echo "[Skip] paranoid and not root"131 return132 fi133 perf stat --per-node -a $2 true134 commachecker --per-node135 echo "[Success]"136}137 138check_per_socket()139{140 echo -n "Checking $1 output: per socket "141 if ParanoidAndNotRoot 0142 then143 echo "[Skip] paranoid and not root"144 return145 fi146 perf stat --per-socket -a $2 true147 commachecker --per-socket148 echo "[Success]"149}150 151# The perf stat options for per-socket, per-core, per-die152# and -A ( no_aggr mode ) uses the info fetched from this153# directory: "/sys/devices/system/cpu/cpu*/topology". For154# example, socket value is fetched from "physical_package_id"155# file in topology directory.156# Reference: cpu__get_topology_int in util/cpumap.c157# If the platform doesn't expose topology information, values158# will be set to -1. For example, incase of pSeries platform159# of powerpc, value for "physical_package_id" is restricted160# and set to -1. Check here validates the socket-id read from161# topology file before proceeding further162 163FILE_LOC="/sys/devices/system/cpu/cpu*/topology/"164FILE_NAME="physical_package_id"165 166function check_for_topology()167{168 if ! ParanoidAndNotRoot 0169 then170 socket_file=`ls $FILE_LOC/$FILE_NAME | head -n 1`171 [ -z $socket_file ] && {172 echo 0173 return174 }175 socket_id=`cat $socket_file`176 [ $socket_id == -1 ] && {177 echo 1178 return179 }180 fi181 echo 0182}183