1#!/bin/sh
2# Check Arm64 callgraphs are complete in fp mode
3# SPDX-License-Identifier: GPL-2.0
4
5lscpu | grep -q "aarch64" || exit 2
6
7if ! [ -x "$(command -v cc)" ]; then
8	echo "failed: no compiler, install gcc"
9	exit 2
10fi
11
12PERF_DATA=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
13TEST_PROGRAM_SOURCE=$(mktemp /tmp/test_program.XXXXX.c)
14TEST_PROGRAM=$(mktemp /tmp/test_program.XXXXX)
15
16cleanup_files()
17{
18	rm -f $PERF_DATA
19	rm -f $TEST_PROGRAM_SOURCE
20	rm -f $TEST_PROGRAM
21}
22
23trap cleanup_files exit term int
24
25cat << EOF > $TEST_PROGRAM_SOURCE
26int a = 0;
27void leaf(void) {
28  for (;;)
29    a += a;
30}
31void parent(void) {
32  leaf();
33}
34int main(void) {
35  parent();
36  return 0;
37}
38EOF
39
40echo " + Compiling test program ($TEST_PROGRAM)..."
41
42CFLAGS="-g -O0 -fno-inline -fno-omit-frame-pointer"
43cc $CFLAGS $TEST_PROGRAM_SOURCE -o $TEST_PROGRAM || exit 1
44
45# Add a 1 second delay to skip samples that are not in the leaf() function
46perf record -o $PERF_DATA --call-graph fp -e cycles//u -D 1000 -- $TEST_PROGRAM 2> /dev/null &
47PID=$!
48
49echo " + Recording (PID=$PID)..."
50sleep 2
51echo " + Stopping perf-record..."
52
53kill $PID
54wait $PID
55
56# expected perf-script output:
57#
58# program
59# 	728 leaf
60# 	753 parent
61# 	76c main
62# ...
63
64perf script -i $PERF_DATA -F comm,ip,sym | head -n4
65perf script -i $PERF_DATA -F comm,ip,sym | head -n4 | \
66	awk '{ if ($2 != "") sym[i++] = $2 } END { if (sym[0] != "leaf" ||
67						       sym[1] != "parent" ||
68						       sym[2] != "main") exit 1 }'
69