top.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash
  2. # perf top tests (exclusive)
  3. # SPDX-License-Identifier: GPL-2.0
  4. set -e
  5. err=0
  6. log_file=$(mktemp /tmp/perf.top.log.XXXXX)
  7. cleanup() {
  8. rm -f "${log_file}"
  9. trap - EXIT TERM INT
  10. }
  11. trap_cleanup() {
  12. echo "Unexpected signal in ${FUNCNAME[1]}"
  13. cleanup
  14. exit 1
  15. }
  16. trap trap_cleanup EXIT TERM INT
  17. test_basic_perf_top() {
  18. echo "Basic perf top test"
  19. # Start a workload that spins to generate samples
  20. # thloop runs for the specified number of seconds
  21. perf test -w thloop 20 &
  22. PID=$!
  23. # Allow it to start
  24. sleep 0.1
  25. # Run perf top for 5 seconds, monitoring that PID
  26. # Use --stdio to avoid TUI and redirect output
  27. # Use -d 1 to avoid flooding output
  28. # Use -e cpu-clock to ensure we get samples
  29. # Use sleep to keep stdin open but silent, preventing EOF loop or interactive spam
  30. if ! sleep 10 | timeout 5s perf top --stdio -d 1 -e cpu-clock -p $PID > "${log_file}" 2>&1; then
  31. retval=$?
  32. if [ $retval -ne 124 ] && [ $retval -ne 0 ]; then
  33. echo "Basic perf top test [Failed: perf top failed to start or run (ret=$retval)]"
  34. head -n 50 "${log_file}"
  35. kill $PID
  36. wait $PID 2>/dev/null || true
  37. err=1
  38. return
  39. fi
  40. fi
  41. kill $PID
  42. wait $PID 2>/dev/null || true
  43. # Check for some sample data (percentage)
  44. if ! grep -E -q "[0-9]+\.[0-9]+%" "${log_file}"; then
  45. echo "Basic perf top test [Failed: no sample percentage found]"
  46. head -n 50 "${log_file}"
  47. err=1
  48. return
  49. fi
  50. # Check for the symbol
  51. if ! grep -q "test_loop" "${log_file}"; then
  52. echo "Basic perf top test [Failed: test_loop symbol not found]"
  53. head -n 50 "${log_file}"
  54. err=1
  55. return
  56. fi
  57. echo "Basic perf top test [Success]"
  58. }
  59. test_basic_perf_top
  60. cleanup
  61. exit $err