17473ee56SClaire Jensen#!/bin/bash
27473ee56SClaire Jensen# perf stat CSV output linter
37473ee56SClaire Jensen# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
47473ee56SClaire Jensen# Tests various perf stat CSV output commands for the
57473ee56SClaire Jensen# correct number of fields and the CSV separator set to ','.
67473ee56SClaire Jensen
77473ee56SClaire Jensenset -e
87473ee56SClaire Jensen
9*8439b44aSAthira Rajeev. "$(dirname $0)"/lib/stat_output.sh
10fc51fc87SKan Liang
11d3e104bbSIan Rogerscsv_sep=@
12cd400f6fSAthira Rajeev
1322036829SIan Rogersstat_output=$(mktemp /tmp/__perf_test.stat_output.csv.XXXXX)
1422036829SIan Rogers
1522036829SIan Rogerscleanup() {
1622036829SIan Rogers  rm -f "${stat_output}"
1722036829SIan Rogers
1822036829SIan Rogers  trap - EXIT TERM INT
1922036829SIan Rogers}
2022036829SIan Rogers
2122036829SIan Rogerstrap_cleanup() {
2222036829SIan Rogers  cleanup
2322036829SIan Rogers  exit 1
2422036829SIan Rogers}
2522036829SIan Rogerstrap trap_cleanup EXIT TERM INT
2622036829SIan Rogers
27ec906102SThomas Richterfunction commachecker()
28ec906102SThomas Richter{
2987abe344SThomas Richter	local -i cnt=0
3087abe344SThomas Richter	local exp=0
31ec906102SThomas Richter
32ec906102SThomas Richter	case "$1"
33ec906102SThomas Richter	in "--no-args")		exp=6
34ec906102SThomas Richter	;; "--system-wide")	exp=6
35ec906102SThomas Richter	;; "--event")		exp=6
36ec906102SThomas Richter	;; "--interval")	exp=7
37ec906102SThomas Richter	;; "--per-thread")	exp=7
38ec906102SThomas Richter	;; "--system-wide-no-aggr")	exp=7
399e9d07a7SKorrapati Likhitha				[ "$(uname -m)" = "s390x" ] && exp='^[6-7]$'
40ec906102SThomas Richter	;; "--per-core")	exp=8
41ec906102SThomas Richter	;; "--per-socket")	exp=8
42ec906102SThomas Richter	;; "--per-node")	exp=8
43ec906102SThomas Richter	;; "--per-die")		exp=8
44bfce728dSK Prateek Nayak	;; "--per-cache")	exp=8
45ec906102SThomas Richter	esac
46ec906102SThomas Richter
47ec906102SThomas Richter	while read line
48ec906102SThomas Richter	do
4922036829SIan Rogers		# Ignore initial "started on" comment.
5022036829SIan Rogers		x=${line:0:1}
5122036829SIan Rogers		[ "$x" = "#" ] && continue
5222036829SIan Rogers		# Ignore initial blank line.
5322036829SIan Rogers		[ "$line" = "" ] && continue
54ec906102SThomas Richter
55ec906102SThomas Richter		# Count the number of commas
56d3e104bbSIan Rogers		x=$(echo $line | tr -d -c $csv_sep)
57ec906102SThomas Richter		cnt="${#x}"
58ec906102SThomas Richter		# echo $line $cnt
5987abe344SThomas Richter		[[ ! "$cnt" =~ $exp ]] && {
60ec906102SThomas Richter			echo "wrong number of fields. expected $exp in $line" 1>&2
61ec906102SThomas Richter			exit 1;
62ec906102SThomas Richter		}
6322036829SIan Rogers	done < "${stat_output}"
64ec906102SThomas Richter	return 0
65ec906102SThomas Richter}
667473ee56SClaire Jensen
67fc51fc87SKan Liangperf_cmd="-x$csv_sep -o ${stat_output}"
687473ee56SClaire Jensen
69fc51fc87SKan Liangskip_test=$(check_for_topology)
70fc51fc87SKan Liangcheck_no_args "CSV" "$perf_cmd"
71fc51fc87SKan Liangcheck_system_wide "CSV" "$perf_cmd"
72fc51fc87SKan Liangcheck_interval "CSV" "$perf_cmd"
73fc51fc87SKan Liangcheck_event "CSV" "$perf_cmd"
74fc51fc87SKan Liangcheck_per_thread "CSV" "$perf_cmd"
75fc51fc87SKan Liangcheck_per_node "CSV" "$perf_cmd"
76cd400f6fSAthira Rajeevif [ $skip_test -ne 1 ]
77cd400f6fSAthira Rajeevthen
78fc51fc87SKan Liang	check_system_wide_no_aggr "CSV" "$perf_cmd"
79fc51fc87SKan Liang	check_per_core "CSV" "$perf_cmd"
80fc51fc87SKan Liang	check_per_cache_instance "CSV" "$perf_cmd"
81fc51fc87SKan Liang	check_per_die "CSV" "$perf_cmd"
82fc51fc87SKan Liang	check_per_socket "CSV" "$perf_cmd"
83cd400f6fSAthira Rajeevelse
84cd400f6fSAthira Rajeev	echo "[Skip] Skipping tests for system_wide_no_aggr, per_core, per_die and per_socket since socket id exposed via topology is invalid"
85cd400f6fSAthira Rajeevfi
8622036829SIan Rogerscleanup
877473ee56SClaire Jensenexit 0
88