162306a36Sopenharmony_ci#!/bin/sh
262306a36Sopenharmony_ci# Check branch stack sampling
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
562306a36Sopenharmony_ci# German Gomez <german.gomez@arm.com>, 2022
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci# skip the test if the hardware doesn't support branch stack sampling
862306a36Sopenharmony_ci# and if the architecture doesn't support filter types: any,save_type,u
962306a36Sopenharmony_ciif ! perf record -o- --no-buildid --branch-filter any,save_type,u -- true > /dev/null 2>&1 ; then
1062306a36Sopenharmony_ci	echo "skip: system doesn't support filter types: any,save_type,u"
1162306a36Sopenharmony_ci	exit 2
1262306a36Sopenharmony_cifi
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciTMPDIR=$(mktemp -d /tmp/__perf_test.program.XXXXX)
1562306a36Sopenharmony_ciTESTPROG="perf test -w brstack"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cicleanup() {
1862306a36Sopenharmony_ci	rm -rf $TMPDIR
1962306a36Sopenharmony_ci}
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_citrap cleanup EXIT TERM INT
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_citest_user_branches() {
2462306a36Sopenharmony_ci	echo "Testing user branch stack sampling"
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci	perf record -o $TMPDIR/perf.data --branch-filter any,save_type,u -- ${TESTPROG} > /dev/null 2>&1
2762306a36Sopenharmony_ci	perf script -i $TMPDIR/perf.data --fields brstacksym | xargs -n1 > $TMPDIR/perf.script
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci	# example of branch entries:
3062306a36Sopenharmony_ci	# 	brstack_foo+0x14/brstack_bar+0x40/P/-/-/0/CALL
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	set -x
3362306a36Sopenharmony_ci	grep -E -m1 "^brstack_bench\+[^ ]*/brstack_foo\+[^ ]*/IND_CALL/.*$"	$TMPDIR/perf.script
3462306a36Sopenharmony_ci	grep -E -m1 "^brstack_foo\+[^ ]*/brstack_bar\+[^ ]*/CALL/.*$"	$TMPDIR/perf.script
3562306a36Sopenharmony_ci	grep -E -m1 "^brstack_bench\+[^ ]*/brstack_foo\+[^ ]*/CALL/.*$"	$TMPDIR/perf.script
3662306a36Sopenharmony_ci	grep -E -m1 "^brstack_bench\+[^ ]*/brstack_bar\+[^ ]*/CALL/.*$"	$TMPDIR/perf.script
3762306a36Sopenharmony_ci	grep -E -m1 "^brstack_bar\+[^ ]*/brstack_foo\+[^ ]*/RET/.*$"		$TMPDIR/perf.script
3862306a36Sopenharmony_ci	grep -E -m1 "^brstack_foo\+[^ ]*/brstack_bench\+[^ ]*/RET/.*$"	$TMPDIR/perf.script
3962306a36Sopenharmony_ci	grep -E -m1 "^brstack_bench\+[^ ]*/brstack_bench\+[^ ]*/COND/.*$"	$TMPDIR/perf.script
4062306a36Sopenharmony_ci	grep -E -m1 "^brstack\+[^ ]*/brstack\+[^ ]*/UNCOND/.*$"		$TMPDIR/perf.script
4162306a36Sopenharmony_ci	set +x
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	# some branch types are still not being tested:
4462306a36Sopenharmony_ci	# IND COND_CALL COND_RET SYSCALL SYSRET IRQ SERROR NO_TX
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci# first argument <arg0> is the argument passed to "--branch-stack <arg0>,save_type,u"
4862306a36Sopenharmony_ci# second argument are the expected branch types for the given filter
4962306a36Sopenharmony_citest_filter() {
5062306a36Sopenharmony_ci	test_filter_filter=$1
5162306a36Sopenharmony_ci	test_filter_expect=$2
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	echo "Testing branch stack filtering permutation ($test_filter_filter,$test_filter_expect)"
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	perf record -o $TMPDIR/perf.data --branch-filter $test_filter_filter,save_type,u -- ${TESTPROG} > /dev/null 2>&1
5662306a36Sopenharmony_ci	perf script -i $TMPDIR/perf.data --fields brstack | xargs -n1 > $TMPDIR/perf.script
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	# fail if we find any branch type that doesn't match any of the expected ones
5962306a36Sopenharmony_ci	# also consider UNKNOWN branch types (-)
6062306a36Sopenharmony_ci	if grep -E -vm1 "^[^ ]*/($test_filter_expect|-|( *))/.*$" $TMPDIR/perf.script; then
6162306a36Sopenharmony_ci		return 1
6262306a36Sopenharmony_ci	fi
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciset -e
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_citest_user_branches
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_citest_filter "any_call"	"CALL|IND_CALL|COND_CALL|SYSCALL|IRQ"
7062306a36Sopenharmony_citest_filter "call"	"CALL|SYSCALL"
7162306a36Sopenharmony_citest_filter "cond"	"COND"
7262306a36Sopenharmony_citest_filter "any_ret"	"RET|COND_RET|SYSRET|ERET"
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_citest_filter "call,cond"		"CALL|SYSCALL|COND"
7562306a36Sopenharmony_citest_filter "any_call,cond"		"CALL|IND_CALL|COND_CALL|IRQ|SYSCALL|COND"
7662306a36Sopenharmony_citest_filter "cond,any_call,any_ret"	"COND|CALL|IND_CALL|COND_CALL|SYSCALL|IRQ|RET|COND_RET|SYSRET|ERET"
77