xref: /linux/tools/perf/tests/shell/top.sh (revision b61104e7a6349bd2c2b3e2fb3260d87f15eda8f4)
1#!/bin/bash
2# perf top tests
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7err=0
8log_file=$(mktemp /tmp/perf.top.log.XXXXX)
9
10cleanup() {
11	rm -f "${log_file}"
12	trap - EXIT TERM INT
13}
14
15trap_cleanup() {
16	echo "Unexpected signal in ${FUNCNAME[1]}"
17	cleanup
18	exit 1
19}
20trap trap_cleanup EXIT TERM INT
21
22test_basic_perf_top() {
23	echo "Basic perf top test"
24
25	# Start a workload that spins to generate samples
26	# thloop runs for the specified number of seconds
27	perf test -w thloop 20 &
28	PID=$!
29
30	# Allow it to start
31	sleep 0.1
32
33	# Run perf top for 5 seconds, monitoring that PID
34	# Use --stdio to avoid TUI and redirect output
35	# Use -d 1 to avoid flooding output
36	# Use -e cpu-clock to ensure we get samples
37	# Use sleep to keep stdin open but silent, preventing EOF loop or interactive spam
38	if ! sleep 10 | timeout 5s perf top --stdio -d 1 -e cpu-clock -p $PID > "${log_file}" 2>&1; then
39		retval=$?
40		if [ $retval -ne 124 ] && [ $retval -ne 0 ]; then
41			echo "Basic perf top test [Failed: perf top failed to start or run (ret=$retval)]"
42			head -n 50 "${log_file}"
43			kill $PID
44			wait $PID 2>/dev/null || true
45			err=1
46			return
47		fi
48	fi
49
50	kill $PID
51	wait $PID 2>/dev/null || true
52
53	# Check for some sample data (percentage)
54	if ! grep -E -q "[0-9]+\.[0-9]+%" "${log_file}"; then
55		echo "Basic perf top test [Failed: no sample percentage found]"
56		head -n 50 "${log_file}"
57		err=1
58		return
59	fi
60
61	# Check for the symbol
62	if ! grep -q "test_loop" "${log_file}"; then
63		echo "Basic perf top test [Failed: test_loop symbol not found]"
64		head -n 50 "${log_file}"
65		err=1
66		return
67	fi
68
69	echo "Basic perf top test [Success]"
70}
71
72test_basic_perf_top
73cleanup
74exit $err
75