perf test arm-spe: Check if perf-record hangs when recording workload with forks
[platform/kernel/linux-starfive.git] / tools / perf / tests / shell / test_arm_spe_fork.sh
1 #!/bin/sh
2 # Check Arm SPE doesn't hang when there are forks
3
4 # SPDX-License-Identifier: GPL-2.0
5 # German Gomez <german.gomez@arm.com>, 2022
6
7 skip_if_no_arm_spe_event() {
8         perf list | egrep -q 'arm_spe_[0-9]+//' && return 0
9         return 2
10 }
11
12 skip_if_no_arm_spe_event || exit 2
13
14 # skip if there's no compiler
15 if ! [ -x "$(command -v cc)" ]; then
16         echo "failed: no compiler, install gcc"
17         exit 2
18 fi
19
20 TEST_PROGRAM_SOURCE=$(mktemp /tmp/__perf_test.program.XXXXX.c)
21 TEST_PROGRAM=$(mktemp /tmp/__perf_test.program.XXXXX)
22 PERF_DATA=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
23 PERF_RECORD_LOG=$(mktemp /tmp/__perf_test.log.XXXXX)
24
25 cleanup_files()
26 {
27         echo "Cleaning up files..."
28         rm -f ${PERF_RECORD_LOG}
29         rm -f ${PERF_DATA}
30         rm -f ${TEST_PROGRAM_SOURCE}
31         rm -f ${TEST_PROGRAM}
32 }
33
34 trap cleanup_files exit term int
35
36 # compile test program
37 cat << EOF > $TEST_PROGRAM_SOURCE
38 #include <math.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <sys/wait.h>
43
44 int workload() {
45   while (1)
46     sqrt(rand());
47   return 0;
48 }
49
50 int main() {
51   switch (fork()) {
52     case 0:
53       return workload();
54     case -1:
55       return 1;
56     default:
57       wait(NULL);
58   }
59   return 0;
60 }
61 EOF
62
63 echo "Compiling test program..."
64 CFLAGS="-lm"
65 cc $TEST_PROGRAM_SOURCE $CFLAGS -o $TEST_PROGRAM || exit 1
66
67 echo "Recording workload..."
68 perf record -o ${PERF_DATA} -e arm_spe/period=65536/ -vvv -- $TEST_PROGRAM > ${PERF_RECORD_LOG} 2>&1 &
69 PERFPID=$!
70
71 # Check if perf hangs by checking the perf-record logs.
72 sleep 1
73 log0=$(wc -l $PERF_RECORD_LOG)
74 echo Log lines = $log0
75 sleep 1
76 log1=$(wc -l $PERF_RECORD_LOG)
77 echo Log lines after 1 second = $log1
78
79 kill $PERFPID
80 wait $PERFPID
81 # test program may leave an orphan process running the workload
82 killall $(basename $TEST_PROGRAM)
83
84 if [ "$log0" = "$log1" ];
85 then
86         echo "SPE hang test: FAIL"
87         exit 1
88 else
89         echo "SPE hang test: PASS"
90 fi
91
92 exit 0