1#!/bin/bash
2# perf stat CSV output linter
3# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
4# Tests various perf stat CSV output commands for the
5# correct number of fields and the CSV separator set to ','.
6
7set -e
8
9skip_test=0
10
11function commachecker()
12{
13	local -i cnt=0
14	local exp=0
15
16	case "$1"
17	in "--no-args")		exp=6
18	;; "--system-wide")	exp=6
19	;; "--event")		exp=6
20	;; "--interval")	exp=7
21	;; "--per-thread")	exp=7
22	;; "--system-wide-no-aggr")	exp=7
23				[ $(uname -m) = "s390x" ] && exp='^[6-7]$'
24	;; "--per-core")	exp=8
25	;; "--per-socket")	exp=8
26	;; "--per-node")	exp=8
27	;; "--per-die")		exp=8
28	esac
29
30	while read line
31	do
32		# Check for lines beginning with Failed
33		x=${line:0:6}
34		[ "$x" = "Failed" ] && continue
35
36		# Count the number of commas
37		x=$(echo $line | tr -d -c ',')
38		cnt="${#x}"
39		# echo $line $cnt
40		[[ ! "$cnt" =~ $exp ]] && {
41			echo "wrong number of fields. expected $exp in $line" 1>&2
42			exit 1;
43		}
44	done
45	return 0
46}
47
48# Return true if perf_event_paranoid is > $1 and not running as root.
49function ParanoidAndNotRoot()
50{
51	 [ $(id -u) != 0 ] && [ $(cat /proc/sys/kernel/perf_event_paranoid) -gt $1 ]
52}
53
54check_no_args()
55{
56	echo -n "Checking CSV output: no args "
57	perf stat -x, true 2>&1 | commachecker --no-args
58	echo "[Success]"
59}
60
61check_system_wide()
62{
63	echo -n "Checking CSV output: system wide "
64	if ParanoidAndNotRoot 0
65	then
66		echo "[Skip] paranoid and not root"
67		return
68	fi
69	perf stat -x, -a true 2>&1 | commachecker --system-wide
70	echo "[Success]"
71}
72
73check_system_wide_no_aggr()
74{
75	echo -n "Checking CSV output: system wide "
76	if ParanoidAndNotRoot 0
77	then
78		echo "[Skip] paranoid and not root"
79		return
80	fi
81	echo -n "Checking CSV output: system wide no aggregation "
82	perf stat -x, -A -a --no-merge true 2>&1 | commachecker --system-wide-no-aggr
83	echo "[Success]"
84}
85
86check_interval()
87{
88	echo -n "Checking CSV output: interval "
89	perf stat -x, -I 1000 true 2>&1 | commachecker --interval
90	echo "[Success]"
91}
92
93
94check_event()
95{
96	echo -n "Checking CSV output: event "
97	perf stat -x, -e cpu-clock true 2>&1 | commachecker --event
98	echo "[Success]"
99}
100
101check_per_core()
102{
103	echo -n "Checking CSV output: per core "
104	if ParanoidAndNotRoot 0
105	then
106		echo "[Skip] paranoid and not root"
107		return
108	fi
109	perf stat -x, --per-core -a true 2>&1 | commachecker --per-core
110	echo "[Success]"
111}
112
113check_per_thread()
114{
115	echo -n "Checking CSV output: per thread "
116	if ParanoidAndNotRoot 0
117	then
118		echo "[Skip] paranoid and not root"
119		return
120	fi
121	perf stat -x, --per-thread -a true 2>&1 | commachecker --per-thread
122	echo "[Success]"
123}
124
125check_per_die()
126{
127	echo -n "Checking CSV output: per die "
128	if ParanoidAndNotRoot 0
129	then
130		echo "[Skip] paranoid and not root"
131		return
132	fi
133	perf stat -x, --per-die -a true 2>&1 | commachecker --per-die
134	echo "[Success]"
135}
136
137check_per_node()
138{
139	echo -n "Checking CSV output: per node "
140	if ParanoidAndNotRoot 0
141	then
142		echo "[Skip] paranoid and not root"
143		return
144	fi
145	perf stat -x, --per-node -a true 2>&1 | commachecker --per-node
146	echo "[Success]"
147}
148
149check_per_socket()
150{
151	echo -n "Checking CSV output: per socket "
152	if ParanoidAndNotRoot 0
153	then
154		echo "[Skip] paranoid and not root"
155		return
156	fi
157	perf stat -x, --per-socket -a true 2>&1 | commachecker --per-socket
158	echo "[Success]"
159}
160
161# The perf stat options for per-socket, per-core, per-die
162# and -A ( no_aggr mode ) uses the info fetched from this
163# directory: "/sys/devices/system/cpu/cpu*/topology". For
164# example, socket value is fetched from "physical_package_id"
165# file in topology directory.
166# Reference: cpu__get_topology_int in util/cpumap.c
167# If the platform doesn't expose topology information, values
168# will be set to -1. For example, incase of pSeries platform
169# of powerpc, value for  "physical_package_id" is restricted
170# and set to -1. Check here validates the socket-id read from
171# topology file before proceeding further
172
173FILE_LOC="/sys/devices/system/cpu/cpu*/topology/"
174FILE_NAME="physical_package_id"
175
176check_for_topology()
177{
178	if ! ParanoidAndNotRoot 0
179	then
180		socket_file=`ls $FILE_LOC/$FILE_NAME | head -n 1`
181		[ -z $socket_file ] && return 0
182		socket_id=`cat $socket_file`
183		[ $socket_id == -1 ] && skip_test=1
184		return 0
185	fi
186}
187
188check_for_topology
189check_no_args
190check_system_wide
191check_interval
192check_event
193check_per_thread
194check_per_node
195if [ $skip_test -ne 1 ]
196then
197	check_system_wide_no_aggr
198	check_per_core
199	check_per_die
200	check_per_socket
201else
202	echo "[Skip] Skipping tests for system_wide_no_aggr, per_core, per_die and per_socket since socket id exposed via topology is invalid"
203fi
204exit 0
205