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