79 lines · bash
1#!/bin/sh2# SPDX-License-Identifier: GPL-2.03 4tenths=date\ +%s%1N5 6# Wait for PID $1 to have $2 number of threads started7# Time out after $3 tenths of a second or 5 seconds if $3 is ""8wait_for_threads()9{10 tm_out=$3 ; [ -n "${tm_out}" ] || tm_out=5011 start_time=$($tenths)12 while [ -e "/proc/$1/task" ] ; do13 th_cnt=$(find "/proc/$1/task" -mindepth 1 -maxdepth 1 -printf x | wc -c)14 if [ "${th_cnt}" -ge "$2" ] ; then15 return 016 fi17 # Wait at most tm_out tenths of a second18 if [ $(($($tenths) - start_time)) -ge $tm_out ] ; then19 echo "PID $1 does not have $2 threads"20 return 121 fi22 done23 return 124}25 26# Wait for perf record -vvv 2>$2 with PID $1 to start by looking at file $227# It depends on capturing perf record debug message "perf record has started"28# Time out after $3 tenths of a second or 5 seconds if $3 is ""29wait_for_perf_to_start()30{31 tm_out=$3 ; [ -n "${tm_out}" ] || tm_out=5032 echo "Waiting for \"perf record has started\" message"33 start_time=$($tenths)34 while [ -e "/proc/$1" ] ; do35 if grep -q "perf record has started" "$2" ; then36 echo OK37 break38 fi39 # Wait at most tm_out tenths of a second40 if [ $(($($tenths) - start_time)) -ge $tm_out ] ; then41 echo "perf recording did not start"42 return 143 fi44 done45 return 046}47 48# Wait for process PID %1 to exit49# Time out after $2 tenths of a second or 5 seconds if $2 is ""50wait_for_process_to_exit()51{52 tm_out=$2 ; [ -n "${tm_out}" ] || tm_out=5053 start_time=$($tenths)54 while [ -e "/proc/$1" ] ; do55 # Wait at most tm_out tenths of a second56 if [ $(($($tenths) - start_time)) -ge $tm_out ] ; then57 echo "PID $1 did not exit as expected"58 return 159 fi60 done61 return 062}63 64# Check if PID $1 is still running after $2 tenths of a second65# or 0.3 seconds if $2 is ""66is_running()67{68 tm_out=$2 ; [ -n "${tm_out}" ] || tm_out=369 start_time=$($tenths)70 while [ -e "/proc/$1" ] ; do71 # Check for at least tm_out tenths of a second72 if [ $(($($tenths) - start_time)) -gt $tm_out ] ; then73 return 074 fi75 done76 echo "PID $1 exited prematurely"77 return 178}79