hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/tools/testing/selftests/ftrace/ftracetest
....@@ -1,11 +1,11 @@
11 #!/bin/sh
2
+# SPDX-License-Identifier: GPL-2.0-only
23
34 # ftracetest - Ftrace test shell scripts
45 #
56 # Copyright (C) Hitachi Ltd., 2014
67 # Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
78 #
8
-# Released under the terms of the GPL v2.
99
1010 usage() { # errno [message]
1111 [ ! -z "$2" ] && echo $2
....@@ -17,21 +17,47 @@
1717 echo " -vv Alias of -v -v (Show all results in stdout)"
1818 echo " -vvv Alias of -v -v -v (Show all commands immediately)"
1919 echo " --fail-unsupported Treat UNSUPPORTED as a failure"
20
+echo " --fail-unresolved Treat UNRESOLVED as a failure"
2021 echo " -d|--debug Debug mode (trace all shell commands)"
2122 echo " -l|--logdir <dir> Save logs on the <dir>"
2223 echo " If <dir> is -, all logs output in console only"
2324 exit $1
2425 }
2526
27
+# default error
28
+err_ret=1
29
+
30
+# kselftest skip code is 4
31
+err_skip=4
32
+
33
+# cgroup RT scheduling prevents chrt commands from succeeding, which
34
+# induces failures in test wakeup tests. Disable for the duration of
35
+# the tests.
36
+
37
+readonly sched_rt_runtime=/proc/sys/kernel/sched_rt_runtime_us
38
+
39
+sched_rt_runtime_orig=$(cat $sched_rt_runtime)
40
+
41
+setup() {
42
+ echo -1 > $sched_rt_runtime
43
+}
44
+
45
+cleanup() {
46
+ echo $sched_rt_runtime_orig > $sched_rt_runtime
47
+}
48
+
2649 errexit() { # message
2750 echo "Error: $1" 1>&2
28
- exit 1
51
+ cleanup
52
+ exit $err_ret
2953 }
3054
3155 # Ensuring user privilege
3256 if [ `id -u` -ne 0 ]; then
3357 errexit "this must be run by root user"
3458 fi
59
+
60
+setup
3561
3662 # Utilities
3763 absdir() { # file_path
....@@ -60,17 +86,35 @@
6086 shift 1
6187 ;;
6288 --verbose|-v|-vv|-vvv)
89
+ if [ $VERBOSE -eq -1 ]; then
90
+ usage "--console can not use with --verbose"
91
+ fi
6392 VERBOSE=$((VERBOSE + 1))
6493 [ $1 = '-vv' ] && VERBOSE=$((VERBOSE + 1))
6594 [ $1 = '-vvv' ] && VERBOSE=$((VERBOSE + 2))
95
+ shift 1
96
+ ;;
97
+ --console)
98
+ if [ $VERBOSE -ne 0 ]; then
99
+ usage "--console can not use with --verbose"
100
+ fi
101
+ VERBOSE=-1
66102 shift 1
67103 ;;
68104 --debug|-d)
69105 DEBUG=1
70106 shift 1
71107 ;;
108
+ --stop-fail)
109
+ STOP_FAILURE=1
110
+ shift 1
111
+ ;;
72112 --fail-unsupported)
73113 UNSUPPORTED_RESULT=1
114
+ shift 1
115
+ ;;
116
+ --fail-unresolved)
117
+ UNRESOLVED_RESULT=1
74118 shift 1
75119 ;;
76120 --logdir|-l)
....@@ -102,11 +146,31 @@
102146 }
103147
104148 # Parameters
105
-DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
106
-if [ -z "$DEBUGFS_DIR" ]; then
107
- TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
108
-else
109
- TRACING_DIR=$DEBUGFS_DIR/tracing
149
+TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
150
+if [ -z "$TRACING_DIR" ]; then
151
+ DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
152
+ if [ -z "$DEBUGFS_DIR" ]; then
153
+ # If tracefs exists, then so does /sys/kernel/tracing
154
+ if [ -d "/sys/kernel/tracing" ]; then
155
+ mount -t tracefs nodev /sys/kernel/tracing ||
156
+ errexit "Failed to mount /sys/kernel/tracing"
157
+ TRACING_DIR="/sys/kernel/tracing"
158
+ # If debugfs exists, then so does /sys/kernel/debug
159
+ elif [ -d "/sys/kernel/debug" ]; then
160
+ mount -t debugfs nodev /sys/kernel/debug ||
161
+ errexit "Failed to mount /sys/kernel/debug"
162
+ TRACING_DIR="/sys/kernel/debug/tracing"
163
+ else
164
+ err_ret=$err_skip
165
+ errexit "debugfs and tracefs are not configured in this kernel"
166
+ fi
167
+ else
168
+ TRACING_DIR="$DEBUGFS_DIR/tracing"
169
+ fi
170
+fi
171
+if [ ! -d "$TRACING_DIR" ]; then
172
+ err_ret=$err_skip
173
+ errexit "ftrace is not configured in this kernel"
110174 fi
111175
112176 TOP_DIR=`absdir $0`
....@@ -117,6 +181,8 @@
117181 DEBUG=0
118182 VERBOSE=0
119183 UNSUPPORTED_RESULT=0
184
+UNRESOLVED_RESULT=0
185
+STOP_FAILURE=0
120186 # Parse command-line options
121187 parse_opts $*
122188
....@@ -137,11 +203,38 @@
137203 date > $LOG_FILE
138204 fi
139205
206
+# Define text colors
207
+# Check available colors on the terminal, if any
208
+ncolors=`tput colors 2>/dev/null || echo 0`
209
+color_reset=
210
+color_red=
211
+color_green=
212
+color_blue=
213
+# If stdout exists and number of colors is eight or more, use them
214
+if [ -t 1 -a "$ncolors" -ge 8 ]; then
215
+ color_reset="\033[0m"
216
+ color_red="\033[31m"
217
+ color_green="\033[32m"
218
+ color_blue="\033[34m"
219
+fi
220
+
221
+strip_esc() {
222
+ # busybox sed implementation doesn't accept "\x1B", so use [:cntrl:] instead.
223
+ sed -E "s/[[:cntrl:]]\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
224
+}
225
+
140226 prlog() { # messages
141
- [ -z "$LOG_FILE" ] && echo "$@" || echo "$@" | tee -a $LOG_FILE
227
+ newline="\n"
228
+ if [ "$1" = "-n" ] ; then
229
+ newline=
230
+ shift
231
+ fi
232
+ printf "$*$newline"
233
+ [ "$LOG_FILE" ] && printf "$*$newline" | strip_esc >> $LOG_FILE
142234 }
143235 catlog() { #file
144
- [ -z "$LOG_FILE" ] && cat $1 || cat $1 | tee -a $LOG_FILE
236
+ cat $1
237
+ [ "$LOG_FILE" ] && cat $1 | strip_esc >> $LOG_FILE
145238 }
146239 prlog "=== Ftrace unit tests ==="
147240
....@@ -167,10 +260,17 @@
167260
168261 INSTANCE=
169262 CASENO=0
263
+
170264 testcase() { # testfile
171265 CASENO=$((CASENO+1))
172
- desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
266
+ desc=`grep "^#[ \t]*description:" $1 | cut -f2- -d:`
173267 prlog -n "[$CASENO]$INSTANCE$desc"
268
+}
269
+
270
+checkreq() { # testfile
271
+ requires=`grep "^#[ \t]*requires:" $1 | cut -f2- -d:`
272
+ # Use eval to pass quoted-patterns correctly.
273
+ eval check_requires "$requires"
174274 }
175275
176276 test_on_instance() { # testfile
....@@ -180,37 +280,37 @@
180280 eval_result() { # sigval
181281 case $1 in
182282 $PASS)
183
- prlog " [PASS]"
283
+ prlog " [${color_green}PASS${color_reset}]"
184284 PASSED_CASES="$PASSED_CASES $CASENO"
185285 return 0
186286 ;;
187287 $FAIL)
188
- prlog " [FAIL]"
288
+ prlog " [${color_red}FAIL${color_reset}]"
189289 FAILED_CASES="$FAILED_CASES $CASENO"
190290 return 1 # this is a bug.
191291 ;;
192292 $UNRESOLVED)
193
- prlog " [UNRESOLVED]"
293
+ prlog " [${color_blue}UNRESOLVED${color_reset}]"
194294 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
195
- return 1 # this is a kind of bug.. something happened.
295
+ return $UNRESOLVED_RESULT # depends on use case
196296 ;;
197297 $UNTESTED)
198
- prlog " [UNTESTED]"
298
+ prlog " [${color_blue}UNTESTED${color_reset}]"
199299 UNTESTED_CASES="$UNTESTED_CASES $CASENO"
200300 return 0
201301 ;;
202302 $UNSUPPORTED)
203
- prlog " [UNSUPPORTED]"
303
+ prlog " [${color_blue}UNSUPPORTED${color_reset}]"
204304 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
205305 return $UNSUPPORTED_RESULT # depends on use case
206306 ;;
207307 $XFAIL)
208
- prlog " [XFAIL]"
308
+ prlog " [${color_green}XFAIL${color_reset}]"
209309 XFAILED_CASES="$XFAILED_CASES $CASENO"
210310 return 0
211311 ;;
212312 *)
213
- prlog " [UNDEFINED]"
313
+ prlog " [${color_blue}UNDEFINED${color_reset}]"
214314 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
215315 return 1 # this must be a test bug
216316 ;;
....@@ -262,23 +362,27 @@
262362
263363 __run_test() { # testfile
264364 # setup PID and PPID, $$ is not updated.
265
- (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1)
365
+ (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x;
366
+ checkreq $1; initialize_ftrace; . $1)
266367 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
267368 }
268369
269370 # Run one test case
270371 run_test() { # testfile
271372 local testname=`basename $1`
373
+ testcase $1
272374 if [ ! -z "$LOG_FILE" ] ; then
273
- local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
375
+ local testlog=`mktemp $LOG_DIR/${CASENO}-${testname}-log.XXXXXX`
274376 else
275377 local testlog=/proc/self/fd/1
276378 fi
277379 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
278
- testcase $1
380
+ export FTRACETEST_ROOT=$TOP_DIR
279381 echo "execute$INSTANCE: "$1 > $testlog
280382 SIG_RESULT=0
281
- if [ -z "$LOG_FILE" ]; then
383
+ if [ $VERBOSE -eq -1 ]; then
384
+ __run_test $1
385
+ elif [ -z "$LOG_FILE" ]; then
282386 __run_test $1 2>&1
283387 elif [ $VERBOSE -ge 3 ]; then
284388 __run_test $1 | tee -a $testlog 2>&1
....@@ -304,6 +408,10 @@
304408 # Main loop
305409 for t in $TEST_CASES; do
306410 run_test $t
411
+ if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
412
+ echo "A failure detected. Stop test."
413
+ exit 1
414
+ fi
307415 done
308416
309417 # Test on instance loop
....@@ -315,7 +423,12 @@
315423 run_test $t
316424 rmdir $TRACING_DIR
317425 TRACING_DIR=$SAVED_TRACING_DIR
426
+ if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
427
+ echo "A failure detected. Stop test."
428
+ exit 1
429
+ fi
318430 done
431
+(cd $TRACING_DIR; initialize_ftrace) # for cleanup
319432
320433 prlog ""
321434 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
....@@ -326,5 +439,7 @@
326439 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
327440 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
328441
442
+cleanup
443
+
329444 # if no error, return 0
330445 exit $TOTAL_RESULT