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