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
9function commachecker()
10{
11	local -i cnt=0
12	local exp=0
13
14	case "$1"
15	in "--no-args")		exp=6
16	;; "--system-wide")	exp=6
17	;; "--event")		exp=6
18	;; "--interval")	exp=7
19	;; "--per-thread")	exp=7
20	;; "--system-wide-no-aggr")	exp=7
21				[ $(uname -m) = "s390x" ] && exp='^[6-7]$'
22	;; "--per-core")	exp=8
23	;; "--per-socket")	exp=8
24	;; "--per-node")	exp=8
25	;; "--per-die")		exp=8
26	esac
27
28	while read line
29	do
30		# Check for lines beginning with Failed
31		x=${line:0:6}
32		[ "$x" = "Failed" ] && continue
33
34		# Count the number of commas
35		x=$(echo $line | tr -d -c ',')
36		cnt="${#x}"
37		# echo $line $cnt
38		[[ ! "$cnt" =~ $exp ]] && {
39			echo "wrong number of fields. expected $exp in $line" 1>&2
40			exit 1;
41		}
42	done
43	return 0
44}
45
46# Return true if perf_event_paranoid is > $1 and not running as root.
47function ParanoidAndNotRoot()
48{
49	 [ $(id -u) != 0 ] && [ $(cat /proc/sys/kernel/perf_event_paranoid) -gt $1 ]
50}
51
52check_no_args()
53{
54	echo -n "Checking CSV output: no args "
55	perf stat -x, true 2>&1 | commachecker --no-args
56	echo "[Success]"
57}
58
59check_system_wide()
60{
61	echo -n "Checking CSV output: system wide "
62	if ParanoidAndNotRoot 0
63	then
64		echo "[Skip] paranoid and not root"
65		return
66	fi
67	perf stat -x, -a true 2>&1 | commachecker --system-wide
68	echo "[Success]"
69}
70
71check_system_wide_no_aggr()
72{
73	echo -n "Checking CSV output: system wide "
74	if ParanoidAndNotRoot 0
75	then
76		echo "[Skip] paranoid and not root"
77		return
78	fi
79	echo -n "Checking CSV output: system wide no aggregation "
80	perf stat -x, -A -a --no-merge true 2>&1 | commachecker --system-wide-no-aggr
81	echo "[Success]"
82}
83
84check_interval()
85{
86	echo -n "Checking CSV output: interval "
87	perf stat -x, -I 1000 true 2>&1 | commachecker --interval
88	echo "[Success]"
89}
90
91
92check_event()
93{
94	echo -n "Checking CSV output: event "
95	perf stat -x, -e cpu-clock true 2>&1 | commachecker --event
96	echo "[Success]"
97}
98
99check_per_core()
100{
101	echo -n "Checking CSV output: per core "
102	if ParanoidAndNotRoot 0
103	then
104		echo "[Skip] paranoid and not root"
105		return
106	fi
107	perf stat -x, --per-core -a true 2>&1 | commachecker --per-core
108	echo "[Success]"
109}
110
111check_per_thread()
112{
113	echo -n "Checking CSV output: per thread "
114	if ParanoidAndNotRoot 0
115	then
116		echo "[Skip] paranoid and not root"
117		return
118	fi
119	perf stat -x, --per-thread -a true 2>&1 | commachecker --per-thread
120	echo "[Success]"
121}
122
123check_per_die()
124{
125	echo -n "Checking CSV output: per die "
126	if ParanoidAndNotRoot 0
127	then
128		echo "[Skip] paranoid and not root"
129		return
130	fi
131	perf stat -x, --per-die -a true 2>&1 | commachecker --per-die
132	echo "[Success]"
133}
134
135check_per_node()
136{
137	echo -n "Checking CSV output: per node "
138	if ParanoidAndNotRoot 0
139	then
140		echo "[Skip] paranoid and not root"
141		return
142	fi
143	perf stat -x, --per-node -a true 2>&1 | commachecker --per-node
144	echo "[Success]"
145}
146
147check_per_socket()
148{
149	echo -n "Checking CSV output: per socket "
150	if ParanoidAndNotRoot 0
151	then
152		echo "[Skip] paranoid and not root"
153		return
154	fi
155	perf stat -x, --per-socket -a true 2>&1 | commachecker --per-socket
156	echo "[Success]"
157}
158
159check_no_args
160check_system_wide
161check_system_wide_no_aggr
162check_interval
163check_event
164check_per_core
165check_per_thread
166check_per_die
167check_per_node
168check_per_socket
169exit 0
170