engine.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. test_begin() {
  4. # Count tests to allow the test harness to double-check if all were
  5. # included correctly.
  6. ctr=0
  7. [ -z "$RTLA" ] && RTLA="./rtla"
  8. [ -n "$TEST_COUNT" ] && echo "1..$TEST_COUNT"
  9. }
  10. reset_osnoise() {
  11. # Reset osnoise options to default and remove any dangling instances created
  12. # by improperly exited rtla runs.
  13. pushd /sys/kernel/tracing >/dev/null || return 1
  14. # Remove dangling instances created by previous rtla run
  15. echo 0 > tracing_thresh
  16. cd instances
  17. for i in osnoise_top osnoise_hist timerlat_top timerlat_hist
  18. do
  19. [ ! -d "$i" ] && continue
  20. rmdir "$i"
  21. done
  22. # Reset options to default
  23. # Note: those are copied from the default values of osnoise_data
  24. # in kernel/trace/trace_osnoise.c
  25. cd ../osnoise
  26. echo all > cpus
  27. echo DEFAULTS > options
  28. echo 1000000 > period_us
  29. echo 0 > print_stack
  30. echo 1000000 > runtime_us
  31. echo 0 > stop_tracing_total_us
  32. echo 0 > stop_tracing_us
  33. echo 1000 > timerlat_period_us
  34. popd >/dev/null
  35. }
  36. check() {
  37. test_name=$0
  38. tested_command=$1
  39. expected_exitcode=${3:-0}
  40. expected_output=$4
  41. unexpected_output=$5
  42. # Simple check: run rtla with given arguments and test exit code.
  43. # If TEST_COUNT is set, run the test. Otherwise, just count.
  44. ctr=$(($ctr + 1))
  45. if [ -n "$TEST_COUNT" ]
  46. then
  47. # Reset osnoise options before running test.
  48. [ "$NO_RESET_OSNOISE" == 1 ] || reset_osnoise
  49. # Run rtla; in case of failure, include its output as comment
  50. # in the test results.
  51. result=$(eval stdbuf -oL $TIMEOUT "$RTLA" $2 2>&1); exitcode=$?
  52. failbuf=''
  53. fail=0
  54. # Test if the results matches if requested
  55. if [ -n "$expected_output" ] && ! grep -qE "$expected_output" <<< "$result"
  56. then
  57. fail=1
  58. failbuf+=$(printf "# Output match failed: \"%s\"" "$expected_output")
  59. failbuf+=$'\n'
  60. fi
  61. if [ -n "$unexpected_output" ] && grep -qE "$unexpected_output" <<< "$result"
  62. then
  63. fail=1
  64. failbuf+=$(printf "# Output non-match failed: \"%s\"" "$unexpected_output")
  65. failbuf+=$'\n'
  66. fi
  67. if [ $exitcode -eq $expected_exitcode ] && [ $fail -eq 0 ]
  68. then
  69. echo "ok $ctr - $1"
  70. else
  71. # Add rtla output and exit code as comments in case of failure
  72. echo "not ok $ctr - $1"
  73. echo -n "$failbuf"
  74. echo "$result" | col -b | while read line; do echo "# $line"; done
  75. printf "#\n# exit code %s\n" $exitcode
  76. fi
  77. fi
  78. }
  79. check_with_osnoise_options() {
  80. # Do the same as "check", but with pre-set osnoise options.
  81. # Note: rtla should reset the osnoise options, this is used to test
  82. # if it indeed does so.
  83. # Save original arguments
  84. arg1=$1
  85. arg2=$2
  86. arg3=$3
  87. # Apply osnoise options (if not dry run)
  88. if [ -n "$TEST_COUNT" ]
  89. then
  90. [ "$NO_RESET_OSNOISE" == 1 ] || reset_osnoise
  91. shift
  92. shift
  93. while shift
  94. do
  95. [ "$1" == "" ] && continue
  96. option=$(echo $1 | cut -d '=' -f 1)
  97. value=$(echo $1 | cut -d '=' -f 2)
  98. echo "$value" > "/sys/kernel/tracing/osnoise/$option" || return 1
  99. done
  100. fi
  101. NO_RESET_OSNOISE=1 check "$arg1" "$arg2" "$arg3"
  102. }
  103. set_timeout() {
  104. TIMEOUT="timeout -v -k 15s $1"
  105. }
  106. unset_timeout() {
  107. unset TIMEOUT
  108. }
  109. set_no_reset_osnoise() {
  110. NO_RESET_OSNOISE=1
  111. }
  112. unset_no_reset_osnoise() {
  113. unset NO_RESET_OSNOISE
  114. }
  115. test_end() {
  116. # If running without TEST_COUNT, tests are not actually run, just
  117. # counted. In that case, re-run the test with the correct count.
  118. [ -z "$TEST_COUNT" ] && TEST_COUNT=$ctr exec bash $0 || true
  119. }
  120. # Avoid any environmental discrepancies
  121. export LC_ALL=C
  122. unset_timeout