cpuhotplug_testsuite.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/bin/sh
  2. ############################################################
  3. ## Convenience functions for reporting, asserting, etc. ##
  4. ############################################################
  5. # warn(TEXT)
  6. #
  7. # Issues a warning message to stderr
  8. #
  9. warn()
  10. {
  11. echo $1 1>&2
  12. }
  13. # assert()
  14. #
  15. # Basic assertion support. Use it like this:
  16. #
  17. # a=5
  18. # b=4
  19. # condition="$a -lt $b" # Error message and exit from script.
  20. # # Try setting "condition" to something else,
  21. # #+ and see what happens.
  22. #
  23. # assert "$condition" $LINENO
  24. #
  25. # Note that $LINENO is a built-in
  26. #
  27. assert () # If condition false,
  28. { #+ exit from script with error message.
  29. E_PARAM_ERR=98
  30. E_ASSERT_FAILED=99
  31. if [ -z "$2" ] # Not enough parameters passed.
  32. then
  33. return $E_PARAM_ERR # No damage done.
  34. fi
  35. lineno=$2
  36. if [ ! $1 ]
  37. then
  38. echo "Assertion failed: \"$1\""
  39. echo "File \"$0\", line $lineno"
  40. exit $E_ASSERT_FAILED
  41. # else
  42. # return
  43. # and continue executing script.
  44. fi
  45. }
  46. ############################################################
  47. ## Process management ##
  48. ############################################################
  49. # pid_is_valid(PID)
  50. #
  51. # Checks if the given $PID is still running. Returns a true value if
  52. # it is, false otherwise.
  53. #
  54. pid_is_valid()
  55. {
  56. PID=$1
  57. ps --pid ${PID} --no-header | grep ${PID}
  58. return $?
  59. }
  60. # kill_pid(PID)
  61. #
  62. # Forcibly kills the process ID and prevents it from
  63. # displaying any messages (to stdout, stderr, or otherwise)
  64. #
  65. kill_pid()
  66. {
  67. PID=$1
  68. kill -9 $PID > /dev/null 2>&1
  69. }
  70. ############################################################
  71. ## Timing ##
  72. ############################################################
  73. # Routines in this library are set up to allow timing to be done
  74. # by defining $TIME to a timing command. You can define your
  75. # own handler by defining $TIME before or after including this
  76. # library.
  77. TIME=${TIME:-""}
  78. # Allows overriding the filename to use for storing time
  79. # measurements. Required in order to
  80. TIME_TMP_FILE=${TIME_TMP_FILE:-"${TMP:-/tmp}/cpu_$$"}
  81. # perform_timings()
  82. #
  83. # This turns on timings for operations that support timing
  84. # via the $TIME variable. It does this by setting $TIME to
  85. # a general purpose time command.
  86. set_timing_on()
  87. {
  88. TIME="/usr/bin/time -o $TIME_TMP_FILE -f \"%e\""
  89. }
  90. report_timing()
  91. {
  92. MSG=${1:-"perform operation"}
  93. if [ ! -z "${TIME}" ]; then
  94. TM=`cat $TIME_TMP_FILE`
  95. echo "Time to ${MSG} : $TM"
  96. fi
  97. }
  98. ############################################################
  99. ## Interrupt handling and cleanup ##
  100. ############################################################
  101. # do_clean()
  102. #
  103. # Virtual function called by do_intr(). Override this to
  104. # provide custom cleanup handling.
  105. #
  106. do_clean()
  107. {
  108. return 0
  109. }
  110. # do_testsuite_clean()
  111. #
  112. # Internal routine to do cleanup specific to other routines
  113. # in this testsuite. You may override this routine if you
  114. # do not want this behavior.
  115. #
  116. do_testsuite_clean()
  117. {
  118. /bin/rm -rf $TIME_TMP_FILE
  119. }
  120. # exit_clean(EXIT_CODE)
  121. #
  122. # Replacement for exit command. Prints the date, then calls do_clean
  123. # and exits with the given $EXIT_CODE, or 0 if none specified.
  124. #
  125. exit_clean()
  126. {
  127. EXIT_CODE=${1:-0}
  128. date
  129. do_clean
  130. exit $EXIT_CODE
  131. }
  132. # do_intr()
  133. #
  134. # Handler for trapped interrupts (i.e., signals 1 2 15).
  135. #
  136. # This will result in a call do do_clean() when the user
  137. # interrupts the test, allowing you to do whatever final
  138. # cleanup work is needed (removing tmp files, restoring
  139. # resources to initial states, etc.) This routine will
  140. # exit with error code 1 when done.
  141. #
  142. do_intr()
  143. {
  144. echo "## Cleaning up... user interrupt"
  145. do_testsuite_clean
  146. do_clean
  147. exit 1
  148. }
  149. trap "do_intr" 1 2 15