1#!/bin/bash
2# perf stat JSON output linter
3# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
4# Checks various perf stat JSON output commands for the
5# correct number of fields.
6
7set -e
8
9pythonchecker=$(dirname $0)/lib/perf_json_output_lint.py
10if [ "x$PYTHON" == "x" ]
11then
12	if which python3 > /dev/null
13	then
14		PYTHON=python3
15	elif which python > /dev/null
16	then
17		PYTHON=python
18	else
19		echo Skipping test, python not detected please set environment variable PYTHON.
20		exit 2
21	fi
22fi
23
24# Return true if perf_event_paranoid is > $1 and not running as root.
25function ParanoidAndNotRoot()
26{
27	 [ $(id -u) != 0 ] && [ $(cat /proc/sys/kernel/perf_event_paranoid) -gt $1 ]
28}
29
30check_no_args()
31{
32	echo -n "Checking json output: no args "
33	perf stat -j true 2>&1 | $PYTHON $pythonchecker --no-args
34	echo "[Success]"
35}
36
37check_system_wide()
38{
39	echo -n "Checking json output: system wide "
40	if ParanoidAndNotRoot 0
41	then
42		echo "[Skip] paranoia and not root"
43		return
44	fi
45	perf stat -j -a true 2>&1 | $PYTHON $pythonchecker --system-wide
46	echo "[Success]"
47}
48
49check_system_wide_no_aggr()
50{
51	echo -n "Checking json output: system wide "
52	if ParanoidAndNotRoot 0
53	then
54		echo "[Skip] paranoia and not root"
55		return
56	fi
57	echo -n "Checking json output: system wide no aggregation "
58	perf stat -j -A -a --no-merge true 2>&1 | $PYTHON $pythonchecker --system-wide-no-aggr
59	echo "[Success]"
60}
61
62check_interval()
63{
64	echo -n "Checking json output: interval "
65	perf stat -j -I 1000 true 2>&1 | $PYTHON $pythonchecker --interval
66	echo "[Success]"
67}
68
69
70check_event()
71{
72	echo -n "Checking json output: event "
73	perf stat -j -e cpu-clock true 2>&1 | $PYTHON $pythonchecker --event
74	echo "[Success]"
75}
76
77check_per_core()
78{
79	echo -n "Checking json output: per core "
80	if ParanoidAndNotRoot 0
81	then
82		echo "[Skip] paranoia and not root"
83		return
84	fi
85	perf stat -j --per-core -a true 2>&1 | $PYTHON $pythonchecker --per-core
86	echo "[Success]"
87}
88
89check_per_thread()
90{
91	echo -n "Checking json output: per thread "
92	if ParanoidAndNotRoot 0
93	then
94		echo "[Skip] paranoia and not root"
95		return
96	fi
97	perf stat -j --per-thread -a true 2>&1 | $PYTHON $pythonchecker --per-thread
98	echo "[Success]"
99}
100
101check_per_die()
102{
103	echo -n "Checking json output: per die "
104	if ParanoidAndNotRoot 0
105	then
106		echo "[Skip] paranoia and not root"
107		return
108	fi
109	perf stat -j --per-die -a true 2>&1 | $PYTHON $pythonchecker --per-die
110	echo "[Success]"
111}
112
113check_per_node()
114{
115	echo -n "Checking json output: per node "
116	if ParanoidAndNotRoot 0
117	then
118		echo "[Skip] paranoia and not root"
119		return
120	fi
121	perf stat -j --per-node -a true 2>&1 | $PYTHON $pythonchecker --per-node
122	echo "[Success]"
123}
124
125check_per_socket()
126{
127	echo -n "Checking json output: per socket "
128	if ParanoidAndNotRoot 0
129	then
130		echo "[Skip] paranoia and not root"
131		return
132	fi
133	perf stat -j --per-socket -a true 2>&1 | $PYTHON $pythonchecker --per-socket
134	echo "[Success]"
135}
136
137check_no_args
138check_system_wide
139check_system_wide_no_aggr
140check_interval
141check_event
142check_per_core
143check_per_thread
144check_per_die
145check_per_node
146check_per_socket
147exit 0
148