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
9pythonchecker=$(dirname $0)/lib/perf_csv_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 CSV output: no args "
33	perf stat -x, true 2>&1 | $PYTHON $pythonchecker --no-args
34	echo "[Success]"
35}
36
37check_system_wide()
38{
39	echo -n "Checking CSV output: system wide "
40	if ParanoidAndNotRoot 0
41	then
42		echo "[Skip] paranoid and not root"
43		return
44	fi
45	perf stat -x, -a true 2>&1 | $PYTHON $pythonchecker --system-wide
46	echo "[Success]"
47}
48
49check_system_wide_no_aggr()
50{
51	echo -n "Checking CSV output: system wide "
52	if ParanoidAndNotRoot 0
53	then
54		echo "[Skip] paranoid and not root"
55		return
56	fi
57	echo -n "Checking CSV output: system wide no aggregation "
58	perf stat -x, -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 CSV output: interval "
65	perf stat -x, -I 1000 true 2>&1 | $PYTHON $pythonchecker --interval
66	echo "[Success]"
67}
68
69
70check_event()
71{
72	echo -n "Checking CSV output: event "
73	perf stat -x, -e cpu-clock true 2>&1 | $PYTHON $pythonchecker --event
74	echo "[Success]"
75}
76
77check_per_core()
78{
79	echo -n "Checking CSV output: per core "
80	if ParanoidAndNotRoot 0
81	then
82		echo "[Skip] paranoid and not root"
83		return
84	fi
85	perf stat -x, --per-core -a true 2>&1 | $PYTHON $pythonchecker --per-core
86	echo "[Success]"
87}
88
89check_per_thread()
90{
91	echo -n "Checking CSV output: per thread "
92	if ParanoidAndNotRoot 0
93	then
94		echo "[Skip] paranoid and not root"
95		return
96	fi
97	perf stat -x, --per-thread -a true 2>&1 | $PYTHON $pythonchecker --per-thread
98	echo "[Success]"
99}
100
101check_per_die()
102{
103	echo -n "Checking CSV output: per die "
104	if ParanoidAndNotRoot 0
105	then
106		echo "[Skip] paranoid and not root"
107		return
108	fi
109	perf stat -x, --per-die -a true 2>&1 | $PYTHON $pythonchecker --per-die
110	echo "[Success]"
111}
112
113check_per_node()
114{
115	echo -n "Checking CSV output: per node "
116	if ParanoidAndNotRoot 0
117	then
118		echo "[Skip] paranoid and not root"
119		return
120	fi
121	perf stat -x, --per-node -a true 2>&1 | $PYTHON $pythonchecker --per-node
122	echo "[Success]"
123}
124
125check_per_socket()
126{
127	echo -n "Checking CSV output: per socket "
128	if ParanoidAndNotRoot 0
129	then
130		echo "[Skip] paranoid and not root"
131		return
132	fi
133	perf stat -x, --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