1# SPDX-License-Identifier: GPL-2.0
2# Carsten Haitzler <carsten.haitzler@arm.com>, 2021
3
4# This is sourced from a driver script so no need for #!/bin... etc. at the
5# top - the assumption below is that it runs as part of sourcing after the
6# test sets up some basic env vars to say what it is.
7
8# This currently works with ETMv4 / ETF not any other packet types at thi
9# point. This will need changes if that changes.
10
11# perf record options for the perf tests to use
12PERFRECMEM="-m ,16M"
13PERFRECOPT="$PERFRECMEM -e cs_etm//u"
14
15TOOLS=$(dirname $0)
16DIR="$TOOLS/$TEST"
17BIN="$DIR/$TEST"
18# If the test tool/binary does not exist and is executable then skip the test
19if ! test -x "$BIN"; then exit 2; fi
20# If CoreSight is not available, skip the test
21perf list cs_etm | grep -q cs_etm || exit 2
22DATD="."
23# If the data dir env is set then make the data dir use that instead of ./
24if test -n "$PERF_TEST_CORESIGHT_DATADIR"; then
25	DATD="$PERF_TEST_CORESIGHT_DATADIR";
26fi
27# If the stat dir env is set then make the data dir use that instead of ./
28STATD="."
29if test -n "$PERF_TEST_CORESIGHT_STATDIR"; then
30	STATD="$PERF_TEST_CORESIGHT_STATDIR";
31fi
32
33# Called if the test fails - error code 1
34err() {
35	echo "$1"
36	exit 1
37}
38
39# Check that some statistics from our perf
40check_val_min() {
41	STATF="$4"
42	if test "$2" -lt "$3"; then
43		echo ", FAILED" >> "$STATF"
44		err "Sanity check number of $1 is too low ($2 < $3)"
45	fi
46}
47
48perf_dump_aux_verify() {
49	# Some basic checking that the AUX chunk contains some sensible data
50	# to see that we are recording something and at least a minimum
51	# amount of it. We should almost always see Fn packets in just about
52	# anything but certainly we will see some trace info and async
53	# packets
54	DUMP="$DATD/perf-tmp-aux-dump.txt"
55	perf report --stdio --dump -i "$1" | \
56		grep -o -e I_ATOM_F -e I_ASYNC -e I_TRACE_INFO > "$DUMP"
57	# Simply count how many of these packets we find to see that we are
58	# producing a reasonable amount of data - exact checks are not sane
59	# as this is a lossy process where we may lose some blocks and the
60	# compiler may produce different code depending on the compiler and
61	# optimization options, so this is rough just to see if we're
62	# either missing almost all the data or all of it
63	ATOM_FX_NUM=$(grep -c I_ATOM_F "$DUMP")
64	ASYNC_NUM=$(grep -c I_ASYNC "$DUMP")
65	TRACE_INFO_NUM=$(grep -c I_TRACE_INFO "$DUMP")
66	rm -f "$DUMP"
67
68	# Arguments provide minimums for a pass
69	CHECK_FX_MIN="$2"
70	CHECK_ASYNC_MIN="$3"
71	CHECK_TRACE_INFO_MIN="$4"
72
73	# Write out statistics, so over time you can track results to see if
74	# there is a pattern - for example we have less "noisy" results that
75	# produce more consistent amounts of data each run, to see if over
76	# time any techinques to  minimize data loss are having an effect or
77	# not
78	STATF="$STATD/stats-$TEST-$DATV.csv"
79	if ! test -f "$STATF"; then
80		echo "ATOM Fx Count, Minimum, ASYNC Count, Minimum, TRACE INFO Count, Minimum" > "$STATF"
81	fi
82	echo -n "$ATOM_FX_NUM, $CHECK_FX_MIN, $ASYNC_NUM, $CHECK_ASYNC_MIN, $TRACE_INFO_NUM, $CHECK_TRACE_INFO_MIN" >> "$STATF"
83
84	# Actually check to see if we passed or failed.
85	check_val_min "ATOM_FX" "$ATOM_FX_NUM" "$CHECK_FX_MIN" "$STATF"
86	check_val_min "ASYNC" "$ASYNC_NUM" "$CHECK_ASYNC_MIN" "$STATF"
87	check_val_min "TRACE_INFO" "$TRACE_INFO_NUM" "$CHECK_TRACE_INFO_MIN" "$STATF"
88	echo ", Ok" >> "$STATF"
89}
90
91perf_dump_aux_tid_verify() {
92	# Specifically crafted test will produce a list of Tread ID's to
93	# stdout that need to be checked to  see that they have had trace
94	# info collected in AUX blocks in the perf data. This will go
95	# through all the TID's that are listed as CID=0xabcdef and see
96	# that all the Thread IDs the test tool reports are  in the perf
97	# data AUX chunks
98
99	# The TID test tools will print a TID per stdout line that are being
100	# tested
101	TIDS=$(cat "$2")
102	# Scan the perf report to find the TIDs that are actually CID in hex
103	# and build a list of the ones found
104	FOUND_TIDS=$(perf report --stdio --dump -i "$1" | \
105			grep -o "CID=0x[0-9a-z]\+" | sed 's/CID=//g' | \
106			uniq | sort | uniq)
107	# No CID=xxx found - maybe your kernel is reporting these as
108	# VMID=xxx so look there
109	if test -z "$FOUND_TIDS"; then
110		FOUND_TIDS=$(perf report --stdio --dump -i "$1" | \
111				grep -o "VMID=0x[0-9a-z]\+" | sed 's/VMID=//g' | \
112				uniq | sort | uniq)
113	fi
114
115	# Iterate over the list of TIDs that the test says it has and find
116	# them in the TIDs found in the perf report
117	MISSING=""
118	for TID2 in $TIDS; do
119		FOUND=""
120		for TIDHEX in $FOUND_TIDS; do
121			TID=$(printf "%i" $TIDHEX)
122			if test "$TID" -eq "$TID2"; then
123				FOUND="y"
124				break
125			fi
126		done
127		if test -z "$FOUND"; then
128			MISSING="$MISSING $TID"
129		fi
130	done
131	if test -n "$MISSING"; then
132		err "Thread IDs $MISSING not found in perf AUX data"
133	fi
134}
135