waiting.sh 2.0 KB

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