1# SPDX-License-Identifier: GPL-2.0
2
3# Return true if perf_event_paranoid is > $1 and not running as root.
4function ParanoidAndNotRoot()
5{
6	 [ "$(id -u)" != 0 ] && [ "$(cat /proc/sys/kernel/perf_event_paranoid)" -gt $1 ]
7}
8
9# $1 name $2 extra_opt
10check_no_args()
11{
12        echo -n "Checking $1 output: no args "
13        perf stat $2 true
14        commachecker --no-args
15        echo "[Success]"
16}
17
18check_system_wide()
19{
20	echo -n "Checking $1 output: system wide "
21	if ParanoidAndNotRoot 0
22	then
23		echo "[Skip] paranoid and not root"
24		return
25	fi
26	perf stat -a $2 true
27	commachecker --system-wide
28	echo "[Success]"
29}
30
31check_system_wide_no_aggr()
32{
33	echo -n "Checking $1 output: system wide no aggregation "
34	if ParanoidAndNotRoot 0
35	then
36		echo "[Skip] paranoid and not root"
37		return
38	fi
39	perf stat -A -a --no-merge $2 true
40	commachecker --system-wide-no-aggr
41	echo "[Success]"
42}
43
44check_interval()
45{
46	echo -n "Checking $1 output: interval "
47	perf stat -I 1000 $2 true
48	commachecker --interval
49	echo "[Success]"
50}
51
52check_event()
53{
54	echo -n "Checking $1 output: event "
55	perf stat -e cpu-clock $2 true
56	commachecker --event
57	echo "[Success]"
58}
59
60check_per_core()
61{
62	echo -n "Checking $1 output: per core "
63	if ParanoidAndNotRoot 0
64	then
65		echo "[Skip] paranoid and not root"
66		return
67	fi
68	perf stat --per-core -a $2 true
69	commachecker --per-core
70	echo "[Success]"
71}
72
73check_per_thread()
74{
75	echo -n "Checking $1 output: per thread "
76	if ParanoidAndNotRoot 0
77	then
78		echo "[Skip] paranoid and not root"
79		return
80	fi
81	perf stat --per-thread -a $2 true
82	commachecker --per-thread
83	echo "[Success]"
84}
85
86check_per_cache_instance()
87{
88	echo -n "Checking $1 output: per cache instance "
89	if ParanoidAndNotRoot 0
90	then
91		echo "[Skip] paranoid and not root"
92		return
93	fi
94	perf stat --per-cache -a $2 true
95	commachecker --per-cache
96	echo "[Success]"
97}
98
99check_per_die()
100{
101	echo -n "Checking $1 output: per die "
102	if ParanoidAndNotRoot 0
103	then
104		echo "[Skip] paranoid and not root"
105		return
106	fi
107	perf stat --per-die -a $2 true
108	commachecker --per-die
109	echo "[Success]"
110}
111
112check_per_node()
113{
114	echo -n "Checking $1 output: per node "
115	if ParanoidAndNotRoot 0
116	then
117		echo "[Skip] paranoid and not root"
118		return
119	fi
120	perf stat --per-node -a $2 true
121	commachecker --per-node
122	echo "[Success]"
123}
124
125check_per_socket()
126{
127	echo -n "Checking $1 output: per socket "
128	if ParanoidAndNotRoot 0
129	then
130		echo "[Skip] paranoid and not root"
131		return
132	fi
133	perf stat --per-socket -a $2 true
134	commachecker --per-socket
135	echo "[Success]"
136}
137
138# The perf stat options for per-socket, per-core, per-die
139# and -A ( no_aggr mode ) uses the info fetched from this
140# directory: "/sys/devices/system/cpu/cpu*/topology". For
141# example, socket value is fetched from "physical_package_id"
142# file in topology directory.
143# Reference: cpu__get_topology_int in util/cpumap.c
144# If the platform doesn't expose topology information, values
145# will be set to -1. For example, incase of pSeries platform
146# of powerpc, value for  "physical_package_id" is restricted
147# and set to -1. Check here validates the socket-id read from
148# topology file before proceeding further
149
150FILE_LOC="/sys/devices/system/cpu/cpu*/topology/"
151FILE_NAME="physical_package_id"
152
153function check_for_topology()
154{
155	if ! ParanoidAndNotRoot 0
156	then
157		socket_file=`ls $FILE_LOC/$FILE_NAME | head -n 1`
158		[ -z $socket_file ] && {
159			echo 0
160			return
161		}
162		socket_id=`cat $socket_file`
163		[ $socket_id == -1 ] && {
164			echo 1
165			return
166		}
167	fi
168	echo 0
169}
170