ftrace.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. # perf ftrace tests
  3. # SPDX-License-Identifier: GPL-2.0
  4. set -e
  5. # perf ftrace commands only works for root
  6. if [ "$(id -u)" != 0 ]; then
  7. echo "perf ftrace test [Skipped: no permission]"
  8. exit 2
  9. fi
  10. output=$(mktemp /tmp/__perf_test.ftrace.XXXXXX)
  11. cleanup() {
  12. rm -f "${output}"
  13. trap - EXIT TERM INT
  14. }
  15. trap_cleanup() {
  16. cleanup
  17. exit 1
  18. }
  19. trap trap_cleanup EXIT TERM INT
  20. # this will be set in test_ftrace_trace()
  21. target_function=
  22. test_ftrace_list() {
  23. echo "perf ftrace list test"
  24. perf ftrace -F > "${output}"
  25. # this will be used in test_ftrace_trace()
  26. sleep_functions=$(grep 'sys_.*sleep$' "${output}")
  27. echo "syscalls for sleep:"
  28. echo "${sleep_functions}"
  29. echo "perf ftrace list test [Success]"
  30. }
  31. test_ftrace_trace() {
  32. echo "perf ftrace trace test"
  33. perf ftrace trace --graph-opts depth=5 sleep 0.1 > "${output}"
  34. # it should have some function name contains 'sleep'
  35. grep "^#" "${output}"
  36. grep -F 'sleep()' "${output}"
  37. # find actual syscall function name
  38. for FN in ${sleep_functions}; do
  39. if grep -q "${FN}" "${output}"; then
  40. target_function="${FN}"
  41. echo "perf ftrace trace test [Success]"
  42. return
  43. fi
  44. done
  45. echo "perf ftrace trace test [Failure: sleep syscall not found]"
  46. exit 1
  47. }
  48. test_ftrace_latency() {
  49. echo "perf ftrace latency test"
  50. echo "target function: ${target_function}"
  51. perf ftrace latency -T "${target_function}" sleep 0.1 > "${output}"
  52. grep "^#" "${output}"
  53. grep "###" "${output}"
  54. echo "perf ftrace latency test [Success]"
  55. }
  56. test_ftrace_profile() {
  57. echo "perf ftrace profile test"
  58. perf ftrace profile --graph-opts depth=5 sleep 0.1 > "${output}"
  59. grep ^# "${output}"
  60. time_re="[[:space:]]+1[[:digit:]]{5}\.[[:digit:]]{3}"
  61. # 100283.000 100283.000 100283.000 1 __x64_sys_clock_nanosleep
  62. # Check for one *clock_nanosleep line with a Count of just 1 that takes a bit more than 0.1 seconds
  63. # Strip the _x64_sys part to work with other architectures
  64. grep -E "^${time_re}${time_re}${time_re}[[:space:]]+1[[:space:]]+.*clock_nanosleep" "${output}"
  65. echo "perf ftrace profile test [Success]"
  66. }
  67. test_ftrace_list
  68. test_ftrace_trace
  69. test_ftrace_latency
  70. test_ftrace_profile
  71. cleanup
  72. exit 0