1#!/bin/bash
2# perf stat STD output linter
3# SPDX-License-Identifier: GPL-2.0
4# Tests various perf stat STD output commands for
5# default event and metricgroup
6
7set -e
8
9. $(dirname $0)/lib/stat_output.sh
10
11stat_output=$(mktemp /tmp/__perf_test.stat_output.std.XXXXX)
12
13event_name=(cpu-clock task-clock context-switches cpu-migrations page-faults stalled-cycles-frontend stalled-cycles-backend cycles instructions branches branch-misses)
14event_metric=("CPUs utilized" "CPUs utilized" "/sec" "/sec" "/sec" "frontend cycles idle" "backend cycles idle" "GHz" "insn per cycle" "/sec" "of all branches")
15skip_metric=("stalled cycles per insn" "tma_")
16
17cleanup() {
18  rm -f "${stat_output}"
19
20  trap - EXIT TERM INT
21}
22
23trap_cleanup() {
24  cleanup
25  exit 1
26}
27trap trap_cleanup EXIT TERM INT
28
29function commachecker()
30{
31	local -i cnt=0
32	local prefix=1
33
34	case "$1"
35	in "--interval")	prefix=2
36	;; "--per-thread")	prefix=2
37	;; "--system-wide-no-aggr")	prefix=2
38	;; "--per-core")	prefix=3
39	;; "--per-socket")	prefix=3
40	;; "--per-node")	prefix=3
41	;; "--per-die")		prefix=3
42	;; "--per-cache")	prefix=3
43	esac
44
45	while read line
46	do
47		# Ignore initial "started on" comment.
48		x=${line:0:1}
49		[ "$x" = "#" ] && continue
50		# Ignore initial blank line.
51		[ "$line" = "" ] && continue
52		# Ignore "Performance counter stats"
53		x=${line:0:25}
54		[ "$x" = "Performance counter stats" ] && continue
55		# Ignore "seconds time elapsed" and break
56		[[ "$line" == *"time elapsed"* ]] && break
57
58		main_body=$(echo $line | cut -d' ' -f$prefix-)
59		x=${main_body%#*}
60		[ "$x" = "" ] && continue
61
62		# Skip metrics without event name
63		y=${main_body#*#}
64		for i in "${!skip_metric[@]}"; do
65			[[ "$y" == *"${skip_metric[$i]}"* ]] && break
66		done
67		[[ "$y" == *"${skip_metric[$i]}"* ]] && continue
68
69		# Check default event
70		for i in "${!event_name[@]}"; do
71			[[ "$x" == *"${event_name[$i]}"* ]] && break
72		done
73
74		[[ ! "$x" == *"${event_name[$i]}"* ]] && {
75			echo "Unknown event name in $line" 1>&2
76			exit 1;
77		}
78
79		# Check event metric if it exists
80		[[ ! "$main_body" == *"#"* ]] && continue
81		[[ ! "$main_body" == *"${event_metric[$i]}"* ]] && {
82			echo "wrong event metric. expected ${event_metric[$i]} in $line" 1>&2
83			exit 1;
84		}
85	done < "${stat_output}"
86	return 0
87}
88
89perf_cmd="-o ${stat_output}"
90
91skip_test=$(check_for_topology)
92check_no_args "STD" "$perf_cmd"
93check_system_wide "STD" "$perf_cmd"
94check_interval "STD" "$perf_cmd"
95check_per_thread "STD" "$perf_cmd"
96check_per_node "STD" "$perf_cmd"
97if [ $skip_test -ne 1 ]
98then
99	check_system_wide_no_aggr "STD" "$perf_cmd"
100	check_per_core "STD" "$perf_cmd"
101	check_per_cache_instance "STD" "$perf_cmd"
102	check_per_die "STD" "$perf_cmd"
103	check_per_socket "STD" "$perf_cmd"
104else
105	echo "[Skip] Skipping tests for system_wide_no_aggr, per_core, per_die and per_socket since socket id exposed via topology is invalid"
106fi
107cleanup
108exit 0
109