1#!/bin/sh
2# Check Arm SPE trace data recording and synthesized samples
3
4# Uses the 'perf record' to record trace data of Arm SPE events;
5# then verify if any SPE event samples are generated by SPE with
6# 'perf script' and 'perf report' commands.
7
8# SPDX-License-Identifier: GPL-2.0
9# German Gomez <german.gomez@arm.com>, 2021
10
11skip_if_no_arm_spe_event() {
12	perf list | egrep -q 'arm_spe_[0-9]+//' && return 0
13
14	# arm_spe event doesn't exist
15	return 2
16}
17
18skip_if_no_arm_spe_event || exit 2
19
20perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
21glb_err=0
22
23cleanup_files()
24{
25	rm -f ${perfdata}
26	exit $glb_err
27}
28
29trap cleanup_files exit term int
30
31arm_spe_report() {
32	if [ $2 != 0 ]; then
33		echo "$1: FAIL"
34		glb_err=$2
35	else
36		echo "$1: PASS"
37	fi
38}
39
40perf_script_samples() {
41	echo "Looking at perf.data file for dumping samples:"
42
43	# from arm-spe.c/arm_spe_synth_events()
44	events="(ld1-miss|ld1-access|llc-miss|lld-access|tlb-miss|tlb-access|branch-miss|remote-access|memory)"
45
46	# Below is an example of the samples dumping:
47	#	dd  3048 [002]          1    l1d-access:      ffffaa64999c __GI___libc_write+0x3c (/lib/aarch64-linux-gnu/libc-2.27.so)
48	#	dd  3048 [002]          1    tlb-access:      ffffaa64999c __GI___libc_write+0x3c (/lib/aarch64-linux-gnu/libc-2.27.so)
49	#	dd  3048 [002]          1        memory:      ffffaa64999c __GI___libc_write+0x3c (/lib/aarch64-linux-gnu/libc-2.27.so)
50	perf script -F,-time -i ${perfdata} 2>&1 | \
51		egrep " +$1 +[0-9]+ .* +${events}:(.*:)? +" > /dev/null 2>&1
52}
53
54perf_report_samples() {
55	echo "Looking at perf.data file for reporting samples:"
56
57	# Below is an example of the samples reporting:
58	#   73.04%    73.04%  dd    libc-2.27.so      [.] _dl_addr
59	#    7.71%     7.71%  dd    libc-2.27.so      [.] getenv
60	#    2.59%     2.59%  dd    ld-2.27.so        [.] strcmp
61	perf report --stdio -i ${perfdata} 2>&1 | \
62		egrep " +[0-9]+\.[0-9]+% +[0-9]+\.[0-9]+% +$1 " > /dev/null 2>&1
63}
64
65arm_spe_snapshot_test() {
66	echo "Recording trace with snapshot mode $perfdata"
67	perf record -o ${perfdata} -e arm_spe// -S \
68		-- dd if=/dev/zero of=/dev/null > /dev/null 2>&1 &
69	PERFPID=$!
70
71	# Wait for perf program
72	sleep 1
73
74	# Send signal to snapshot trace data
75	kill -USR2 $PERFPID
76
77	# Stop perf program
78	kill $PERFPID
79	wait $PERFPID
80
81	perf_script_samples dd &&
82	perf_report_samples dd
83
84	err=$?
85	arm_spe_report "SPE snapshot testing" $err
86}
87
88arm_spe_snapshot_test
89exit $glb_err
90