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 prefix=1
32
33	case "$1"
34	in "--interval")	prefix=2
35	;; "--per-thread")	prefix=2
36	;; "--system-wide-no-aggr")	prefix=2
37	;; "--per-core")	prefix=3
38	;; "--per-socket")	prefix=3
39	;; "--per-node")	prefix=3
40	;; "--per-die")		prefix=3
41	;; "--per-cache")	prefix=3
42	esac
43
44	while read line
45	do
46		# Ignore initial "started on" comment.
47		x=${line:0:1}
48		[ "$x" = "#" ] && continue
49		# Ignore initial blank line.
50		[ "$line" = "" ] && continue
51		# Ignore "Performance counter stats"
52		x=${line:0:25}
53		[ "$x" = "Performance counter stats" ] && continue
54		# Ignore "seconds time elapsed" and break
55		[[ "$line" == *"time elapsed"* ]] && break
56
57		main_body=$(echo $line | cut -d' ' -f$prefix-)
58		x=${main_body%#*}
59		[ "$x" = "" ] && continue
60
61		# Skip metrics without event name
62		y=${main_body#*#}
63		for i in "${!skip_metric[@]}"; do
64			[[ "$y" == *"${skip_metric[$i]}"* ]] && break
65		done
66		[[ "$y" == *"${skip_metric[$i]}"* ]] && continue
67
68		# Check default event
69		for i in "${!event_name[@]}"; do
70			[[ "$x" == *"${event_name[$i]}"* ]] && break
71		done
72
73		[[ ! "$x" == *"${event_name[$i]}"* ]] && {
74			echo "Unknown event name in $line" 1>&2
75			exit 1;
76		}
77
78		# Check event metric if it exists
79		[[ ! "$main_body" == *"#"* ]] && continue
80		[[ ! "$main_body" == *"${event_metric[$i]}"* ]] && {
81			echo "wrong event metric. expected ${event_metric[$i]} in $line" 1>&2
82			exit 1;
83		}
84	done < "${stat_output}"
85	return 0
86}
87
88perf_cmd="-o ${stat_output}"
89
90skip_test=$(check_for_topology)
91check_no_args "STD" "$perf_cmd"
92check_system_wide "STD" "$perf_cmd"
93check_interval "STD" "$perf_cmd"
94check_per_thread "STD" "$perf_cmd"
95check_per_node "STD" "$perf_cmd"
96if [ $skip_test -ne 1 ]
97then
98	check_system_wide_no_aggr "STD" "$perf_cmd"
99	check_per_core "STD" "$perf_cmd"
100	check_per_cache_instance "STD" "$perf_cmd"
101	check_per_die "STD" "$perf_cmd"
102	check_per_socket "STD" "$perf_cmd"
103else
104	echo "[Skip] Skipping tests for system_wide_no_aggr, per_core, per_die and per_socket since socket id exposed via topology is invalid"
105fi
106cleanup
107exit 0
108