xref: /linux/tools/perf/tests/shell/lib/waiting.sh (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
1*1e094f92SAthira Rajeev#!/bin/sh
25ebcdf07SAdrian Hunter# SPDX-License-Identifier: GPL-2.0
35ebcdf07SAdrian Hunter
45ebcdf07SAdrian Huntertenths=date\ +%s%1N
55ebcdf07SAdrian Hunter
65ebcdf07SAdrian Hunter# Wait for PID $1 to have $2 number of threads started
784838712SAdrian Hunter# Time out after $3 tenths of a second or 5 seconds if $3 is ""
85ebcdf07SAdrian Hunterwait_for_threads()
95ebcdf07SAdrian Hunter{
1084838712SAdrian Hunter	tm_out=$3 ; [ -n "${tm_out}" ] || tm_out=50
115ebcdf07SAdrian Hunter	start_time=$($tenths)
125ebcdf07SAdrian Hunter	while [ -e "/proc/$1/task" ] ; do
135ebcdf07SAdrian Hunter		th_cnt=$(find "/proc/$1/task" -mindepth 1 -maxdepth 1 -printf x | wc -c)
145ebcdf07SAdrian Hunter		if [ "${th_cnt}" -ge "$2" ] ; then
155ebcdf07SAdrian Hunter			return 0
165ebcdf07SAdrian Hunter		fi
1784838712SAdrian Hunter		# Wait at most tm_out tenths of a second
1884838712SAdrian Hunter		if [ $(($($tenths) - start_time)) -ge $tm_out ] ; then
195ebcdf07SAdrian Hunter			echo "PID $1 does not have $2 threads"
205ebcdf07SAdrian Hunter			return 1
215ebcdf07SAdrian Hunter		fi
225ebcdf07SAdrian Hunter	done
235ebcdf07SAdrian Hunter	return 1
245ebcdf07SAdrian Hunter}
255ebcdf07SAdrian Hunter
265ebcdf07SAdrian Hunter# Wait for perf record -vvv 2>$2 with PID $1 to start by looking at file $2
275ebcdf07SAdrian Hunter# It depends on capturing perf record debug message "perf record has started"
2884838712SAdrian Hunter# Time out after $3 tenths of a second or 5 seconds if $3 is ""
295ebcdf07SAdrian Hunterwait_for_perf_to_start()
305ebcdf07SAdrian Hunter{
3184838712SAdrian Hunter	tm_out=$3 ; [ -n "${tm_out}" ] || tm_out=50
325ebcdf07SAdrian Hunter	echo "Waiting for \"perf record has started\" message"
335ebcdf07SAdrian Hunter	start_time=$($tenths)
345ebcdf07SAdrian Hunter	while [ -e "/proc/$1" ] ; do
355ebcdf07SAdrian Hunter		if grep -q "perf record has started" "$2" ; then
365ebcdf07SAdrian Hunter			echo OK
375ebcdf07SAdrian Hunter			break
385ebcdf07SAdrian Hunter		fi
3984838712SAdrian Hunter		# Wait at most tm_out tenths of a second
4084838712SAdrian Hunter		if [ $(($($tenths) - start_time)) -ge $tm_out ] ; then
415ebcdf07SAdrian Hunter			echo "perf recording did not start"
425ebcdf07SAdrian Hunter			return 1
435ebcdf07SAdrian Hunter		fi
445ebcdf07SAdrian Hunter	done
455ebcdf07SAdrian Hunter	return 0
465ebcdf07SAdrian Hunter}
475ebcdf07SAdrian Hunter
485ebcdf07SAdrian Hunter# Wait for process PID %1 to exit
4984838712SAdrian Hunter# Time out after $2 tenths of a second or 5 seconds if $2 is ""
505ebcdf07SAdrian Hunterwait_for_process_to_exit()
515ebcdf07SAdrian Hunter{
5284838712SAdrian Hunter	tm_out=$2 ; [ -n "${tm_out}" ] || tm_out=50
535ebcdf07SAdrian Hunter	start_time=$($tenths)
545ebcdf07SAdrian Hunter	while [ -e "/proc/$1" ] ; do
5584838712SAdrian Hunter		# Wait at most tm_out tenths of a second
5684838712SAdrian Hunter		if [ $(($($tenths) - start_time)) -ge $tm_out ] ; then
575ebcdf07SAdrian Hunter			echo "PID $1 did not exit as expected"
585ebcdf07SAdrian Hunter			return 1
595ebcdf07SAdrian Hunter		fi
605ebcdf07SAdrian Hunter	done
615ebcdf07SAdrian Hunter	return 0
625ebcdf07SAdrian Hunter}
635ebcdf07SAdrian Hunter
6484838712SAdrian Hunter# Check if PID $1 is still running after $2 tenths of a second
6584838712SAdrian Hunter# or 0.3 seconds if $2 is ""
665ebcdf07SAdrian Hunteris_running()
675ebcdf07SAdrian Hunter{
6884838712SAdrian Hunter	tm_out=$2 ; [ -n "${tm_out}" ] || tm_out=3
695ebcdf07SAdrian Hunter	start_time=$($tenths)
705ebcdf07SAdrian Hunter	while [ -e "/proc/$1" ] ; do
7184838712SAdrian Hunter		# Check for at least tm_out tenths of a second
7284838712SAdrian Hunter		if [ $(($($tenths) - start_time)) -gt $tm_out ] ; then
735ebcdf07SAdrian Hunter			return 0
745ebcdf07SAdrian Hunter		fi
755ebcdf07SAdrian Hunter	done
765ebcdf07SAdrian Hunter	echo "PID $1 exited prematurely"
775ebcdf07SAdrian Hunter	return 1
785ebcdf07SAdrian Hunter}
79