xref: /openbmc/linux/tools/perf/tests/shell/lib/waiting.sh (revision f5c27da4)
1# SPDX-License-Identifier: GPL-2.0
2
3tenths=date\ +%s%1N
4
5# Wait for PID $1 to have $2 number of threads started
6# Time out after $3 tenths of a second or 5 seconds if $3 is ""
7wait_for_threads()
8{
9	tm_out=$3 ; [ -n "${tm_out}" ] || tm_out=50
10	start_time=$($tenths)
11	while [ -e "/proc/$1/task" ] ; do
12		th_cnt=$(find "/proc/$1/task" -mindepth 1 -maxdepth 1 -printf x | wc -c)
13		if [ "${th_cnt}" -ge "$2" ] ; then
14			return 0
15		fi
16		# Wait at most tm_out tenths of a second
17		if [ $(($($tenths) - start_time)) -ge $tm_out ] ; then
18			echo "PID $1 does not have $2 threads"
19			return 1
20		fi
21	done
22	return 1
23}
24
25# Wait for perf record -vvv 2>$2 with PID $1 to start by looking at file $2
26# It depends on capturing perf record debug message "perf record has started"
27# Time out after $3 tenths of a second or 5 seconds if $3 is ""
28wait_for_perf_to_start()
29{
30	tm_out=$3 ; [ -n "${tm_out}" ] || tm_out=50
31	echo "Waiting for \"perf record has started\" message"
32	start_time=$($tenths)
33	while [ -e "/proc/$1" ] ; do
34		if grep -q "perf record has started" "$2" ; then
35			echo OK
36			break
37		fi
38		# Wait at most tm_out tenths of a second
39		if [ $(($($tenths) - start_time)) -ge $tm_out ] ; then
40			echo "perf recording did not start"
41			return 1
42		fi
43	done
44	return 0
45}
46
47# Wait for process PID %1 to exit
48# Time out after $2 tenths of a second or 5 seconds if $2 is ""
49wait_for_process_to_exit()
50{
51	tm_out=$2 ; [ -n "${tm_out}" ] || tm_out=50
52	start_time=$($tenths)
53	while [ -e "/proc/$1" ] ; do
54		# Wait at most tm_out tenths of a second
55		if [ $(($($tenths) - start_time)) -ge $tm_out ] ; then
56			echo "PID $1 did not exit as expected"
57			return 1
58		fi
59	done
60	return 0
61}
62
63# Check if PID $1 is still running after $2 tenths of a second
64# or 0.3 seconds if $2 is ""
65is_running()
66{
67	tm_out=$2 ; [ -n "${tm_out}" ] || tm_out=3
68	start_time=$($tenths)
69	while [ -e "/proc/$1" ] ; do
70		# Check for at least tm_out tenths of a second
71		if [ $(($($tenths) - start_time)) -gt $tm_out ] ; then
72			return 0
73		fi
74	done
75	echo "PID $1 exited prematurely"
76	return 1
77}
78