16b1b208bSGerman Gomez#!/bin/sh
26b1b208bSGerman Gomez# Check Arm SPE trace data recording and synthesized samples
36b1b208bSGerman Gomez
46b1b208bSGerman Gomez# Uses the 'perf record' to record trace data of Arm SPE events;
56b1b208bSGerman Gomez# then verify if any SPE event samples are generated by SPE with
66b1b208bSGerman Gomez# 'perf script' and 'perf report' commands.
76b1b208bSGerman Gomez
86b1b208bSGerman Gomez# SPDX-License-Identifier: GPL-2.0
96b1b208bSGerman Gomez# German Gomez <german.gomez@arm.com>, 2021
106b1b208bSGerman Gomez
116b1b208bSGerman Gomezskip_if_no_arm_spe_event() {
12818448e9STiezhu Yang	perf list | grep -E -q 'arm_spe_[0-9]+//' && return 0
136b1b208bSGerman Gomez
146b1b208bSGerman Gomez	# arm_spe event doesn't exist
156b1b208bSGerman Gomez	return 2
166b1b208bSGerman Gomez}
176b1b208bSGerman Gomez
186b1b208bSGerman Gomezskip_if_no_arm_spe_event || exit 2
196b1b208bSGerman Gomez
206b1b208bSGerman Gomezperfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
216b1b208bSGerman Gomezglb_err=0
226b1b208bSGerman Gomez
236b1b208bSGerman Gomezcleanup_files()
246b1b208bSGerman Gomez{
256b1b208bSGerman Gomez	rm -f ${perfdata}
2600b32625SNamhyung Kim	rm -f ${perfdata}.old
276b1b208bSGerman Gomez	exit $glb_err
286b1b208bSGerman Gomez}
296b1b208bSGerman Gomez
30*e0da03c7SAbhirup Debtrap cleanup_files EXIT TERM INT
316b1b208bSGerman Gomez
326b1b208bSGerman Gomezarm_spe_report() {
3300b32625SNamhyung Kim	if [ $2 = 0 ]; then
3400b32625SNamhyung Kim		echo "$1: PASS"
3500b32625SNamhyung Kim	elif [ $2 = 2 ]; then
3600b32625SNamhyung Kim		echo "$1: SKIPPED"
3700b32625SNamhyung Kim	else
386b1b208bSGerman Gomez		echo "$1: FAIL"
396b1b208bSGerman Gomez		glb_err=$2
406b1b208bSGerman Gomez	fi
416b1b208bSGerman Gomez}
426b1b208bSGerman Gomez
436b1b208bSGerman Gomezperf_script_samples() {
446b1b208bSGerman Gomez	echo "Looking at perf.data file for dumping samples:"
456b1b208bSGerman Gomez
466b1b208bSGerman Gomez	# from arm-spe.c/arm_spe_synth_events()
476b1b208bSGerman Gomez	events="(ld1-miss|ld1-access|llc-miss|lld-access|tlb-miss|tlb-access|branch-miss|remote-access|memory)"
486b1b208bSGerman Gomez
496b1b208bSGerman Gomez	# Below is an example of the samples dumping:
506b1b208bSGerman Gomez	#	dd  3048 [002]          1    l1d-access:      ffffaa64999c __GI___libc_write+0x3c (/lib/aarch64-linux-gnu/libc-2.27.so)
516b1b208bSGerman Gomez	#	dd  3048 [002]          1    tlb-access:      ffffaa64999c __GI___libc_write+0x3c (/lib/aarch64-linux-gnu/libc-2.27.so)
526b1b208bSGerman Gomez	#	dd  3048 [002]          1        memory:      ffffaa64999c __GI___libc_write+0x3c (/lib/aarch64-linux-gnu/libc-2.27.so)
536b1b208bSGerman Gomez	perf script -F,-time -i ${perfdata} 2>&1 | \
54818448e9STiezhu Yang		grep -E " +$1 +[0-9]+ .* +${events}:(.*:)? +" > /dev/null 2>&1
556b1b208bSGerman Gomez}
566b1b208bSGerman Gomez
576b1b208bSGerman Gomezperf_report_samples() {
586b1b208bSGerman Gomez	echo "Looking at perf.data file for reporting samples:"
596b1b208bSGerman Gomez
606b1b208bSGerman Gomez	# Below is an example of the samples reporting:
616b1b208bSGerman Gomez	#   73.04%    73.04%  dd    libc-2.27.so      [.] _dl_addr
626b1b208bSGerman Gomez	#    7.71%     7.71%  dd    libc-2.27.so      [.] getenv
636b1b208bSGerman Gomez	#    2.59%     2.59%  dd    ld-2.27.so        [.] strcmp
646b1b208bSGerman Gomez	perf report --stdio -i ${perfdata} 2>&1 | \
65818448e9STiezhu Yang		grep -E " +[0-9]+\.[0-9]+% +[0-9]+\.[0-9]+% +$1 " > /dev/null 2>&1
666b1b208bSGerman Gomez}
676b1b208bSGerman Gomez
686b1b208bSGerman Gomezarm_spe_snapshot_test() {
696b1b208bSGerman Gomez	echo "Recording trace with snapshot mode $perfdata"
706b1b208bSGerman Gomez	perf record -o ${perfdata} -e arm_spe// -S \
716b1b208bSGerman Gomez		-- dd if=/dev/zero of=/dev/null > /dev/null 2>&1 &
726b1b208bSGerman Gomez	PERFPID=$!
736b1b208bSGerman Gomez
746b1b208bSGerman Gomez	# Wait for perf program
756b1b208bSGerman Gomez	sleep 1
766b1b208bSGerman Gomez
776b1b208bSGerman Gomez	# Send signal to snapshot trace data
786b1b208bSGerman Gomez	kill -USR2 $PERFPID
796b1b208bSGerman Gomez
806b1b208bSGerman Gomez	# Stop perf program
816b1b208bSGerman Gomez	kill $PERFPID
826b1b208bSGerman Gomez	wait $PERFPID
836b1b208bSGerman Gomez
846b1b208bSGerman Gomez	perf_script_samples dd &&
856b1b208bSGerman Gomez	perf_report_samples dd
866b1b208bSGerman Gomez
876b1b208bSGerman Gomez	err=$?
886b1b208bSGerman Gomez	arm_spe_report "SPE snapshot testing" $err
896b1b208bSGerman Gomez}
906b1b208bSGerman Gomez
9100b32625SNamhyung Kimarm_spe_system_wide_test() {
9200b32625SNamhyung Kim	echo "Recording trace with system-wide mode $perfdata"
9300b32625SNamhyung Kim
9400b32625SNamhyung Kim	perf record -o - -e dummy -a -B true > /dev/null 2>&1
9500b32625SNamhyung Kim	if [ $? != 0 ]; then
9600b32625SNamhyung Kim		arm_spe_report "SPE system-wide testing" 2
9700b32625SNamhyung Kim		return
9800b32625SNamhyung Kim	fi
9900b32625SNamhyung Kim
10000b32625SNamhyung Kim	perf record -o ${perfdata} -e arm_spe// -a --no-bpf-event \
10100b32625SNamhyung Kim		-- dd if=/dev/zero of=/dev/null count=100000 > /dev/null 2>&1
10200b32625SNamhyung Kim
10300b32625SNamhyung Kim	perf_script_samples dd &&
10400b32625SNamhyung Kim	perf_report_samples dd
10500b32625SNamhyung Kim
10600b32625SNamhyung Kim	err=$?
10700b32625SNamhyung Kim	arm_spe_report "SPE system-wide testing" $err
10800b32625SNamhyung Kim}
10900b32625SNamhyung Kim
1106b1b208bSGerman Gomezarm_spe_snapshot_test
11100b32625SNamhyung Kimarm_spe_system_wide_test
11200b32625SNamhyung Kim
1136b1b208bSGerman Gomezexit $glb_err
114