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
7skip_if_no_arm_spe_event() {
8	perf list | egrep -q 'arm_spe_[0-9]+//' && return 0
9	return 2
10}
11
12skip_if_no_arm_spe_event || exit 2
13
14# skip if there's no compiler
15if ! [ -x "$(command -v cc)" ]; then
16	echo "failed: no compiler, install gcc"
17	exit 2
18fi
19
20TEST_PROGRAM_SOURCE=$(mktemp /tmp/__perf_test.program.XXXXX.c)
21TEST_PROGRAM=$(mktemp /tmp/__perf_test.program.XXXXX)
22PERF_DATA=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
23PERF_RECORD_LOG=$(mktemp /tmp/__perf_test.log.XXXXX)
24
25cleanup_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
34trap cleanup_files exit term int
35
36# compile test program
37cat << 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
44int workload() {
45  while (1)
46    sqrt(rand());
47  return 0;
48}
49
50int 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}
61EOF
62
63echo "Compiling test program..."
64CFLAGS="-lm"
65cc $TEST_PROGRAM_SOURCE $CFLAGS -o $TEST_PROGRAM || exit 1
66
67echo "Recording workload..."
68perf record -o ${PERF_DATA} -e arm_spe/period=65536/ -vvv -- $TEST_PROGRAM > ${PERF_RECORD_LOG} 2>&1 &
69PERFPID=$!
70
71# Check if perf hangs by checking the perf-record logs.
72sleep 1
73log0=$(wc -l $PERF_RECORD_LOG)
74echo Log lines = $log0
75sleep 1
76log1=$(wc -l $PERF_RECORD_LOG)
77echo Log lines after 1 second = $log1
78
79kill $PERFPID
80wait $PERFPID
81# test program may leave an orphan process running the workload
82killall $(basename $TEST_PROGRAM)
83
84if [ "$log0" = "$log1" ];
85then
86        echo "SPE hang test: FAIL"
87        exit 1
88else
89        echo "SPE hang test: PASS"
90fi
91
92exit 0
93