2 # SPDX-License-Identifier: GPL-2.0-only
4 # ftracetest - Ftrace test shell scripts
6 # Copyright (C) Hitachi Ltd., 2014
7 # Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
10 usage
() { # errno [message]
11 [ ! -z "$2" ] && echo $2
12 echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]"
14 echo " -h|--help Show help message"
15 echo " -k|--keep Keep passed test logs"
16 echo " -K|--ktap Output in KTAP format"
17 echo " -v|--verbose Increase verbosity of test messages"
18 echo " -vv Alias of -v -v (Show all results in stdout)"
19 echo " -vvv Alias of -v -v -v (Show all commands immediately)"
20 echo " --fail-unsupported Treat UNSUPPORTED as a failure"
21 echo " --fail-unresolved Treat UNRESOLVED as a failure"
22 echo " -d|--debug Debug mode (trace all shell commands)"
23 echo " -l|--logdir <dir> Save logs on the <dir>"
24 echo " If <dir> is -, all logs output in console only"
31 # kselftest skip code is 4
37 # cgroup RT scheduling prevents chrt commands from succeeding, which
38 # induces failures in test wakeup tests. Disable for the duration of
41 readonly sched_rt_runtime
=/proc
/sys
/kernel
/sched_rt_runtime_us
43 sched_rt_runtime_orig
=$
(cat $sched_rt_runtime)
46 echo -1 > $sched_rt_runtime
50 echo $sched_rt_runtime_orig > $sched_rt_runtime
51 if [ -n "${UMOUNT_DIR}" ]; then
52 umount
${UMOUNT_DIR} ||
:
62 # Ensuring user privilege
63 if [ `id -u` -ne 0 ]; then
64 errexit
"this must be run by root user"
70 absdir
() { # file_path
71 (cd `dirname $1`; pwd)
75 echo `absdir $1`/`basename $1`
78 find_testcases
() { #directory
79 echo `find $1 -name \*.tc | sort`
86 while [ ! -z "$1" ]; do
99 --verbose|
-v|
-vv|
-vvv)
100 if [ $VERBOSE -eq -1 ]; then
101 usage
"--console can not use with --verbose"
103 VERBOSE
=$
((VERBOSE
+ 1))
104 [ $1 = '-vv' ] && VERBOSE
=$
((VERBOSE
+ 1))
105 [ $1 = '-vvv' ] && VERBOSE
=$
((VERBOSE
+ 2))
109 if [ $VERBOSE -ne 0 ]; then
110 usage
"--console can not use with --verbose"
138 OPT_TEST_CASES
="$OPT_TEST_CASES `abspath $1`"
141 usage
1 "$1 is not a testcase"
146 OPT_TEST_DIR
=`abspath $1`
147 OPT_TEST_CASES
="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
150 usage
1 "Invalid option ($1)"
155 if [ ! -z "$OPT_TEST_CASES" ]; then
156 TEST_CASES
=$OPT_TEST_CASES
161 TRACING_DIR
=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
162 if [ -z "$TRACING_DIR" ]; then
163 DEBUGFS_DIR
=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
164 if [ -z "$DEBUGFS_DIR" ]; then
165 # If tracefs exists, then so does /sys/kernel/tracing
166 if [ -d "/sys/kernel/tracing" ]; then
167 mount
-t tracefs nodev
/sys
/kernel
/tracing ||
168 errexit
"Failed to mount /sys/kernel/tracing"
169 TRACING_DIR
="/sys/kernel/tracing"
170 UMOUNT_DIR
=${TRACING_DIR}
171 # If debugfs exists, then so does /sys/kernel/debug
172 elif [ -d "/sys/kernel/debug" ]; then
173 mount
-t debugfs nodev
/sys
/kernel
/debug ||
174 errexit
"Failed to mount /sys/kernel/debug"
175 TRACING_DIR
="/sys/kernel/debug/tracing"
176 UMOUNT_DIR
=${TRACING_DIR}
179 errexit
"debugfs and tracefs are not configured in this kernel"
182 TRACING_DIR
="$DEBUGFS_DIR/tracing"
185 if [ ! -d "$TRACING_DIR" ]; then
187 errexit
"ftrace is not configured in this kernel"
191 TEST_DIR
=$TOP_DIR/test.d
192 TEST_CASES
=`find_testcases $TEST_DIR`
193 LOG_TOP_DIR
=$TOP_DIR/logs
194 LOG_DATE
=`date +%Y%m%d-%H%M%S`
195 LOG_DIR
=$LOG_TOP_DIR/$LOG_DATE/
196 LINK_PTR
=$LOG_TOP_DIR/latest
204 # Parse command-line options
207 [ $DEBUG -ne 0 ] && set -x
210 if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
211 errexit
"No ftrace directory found"
215 if [ "x$LOG_DIR" = "x-" ]; then
219 LOG_FILE
=$LOG_DIR/ftracetest.log
220 mkdir
-p $LOG_DIR || errexit
"Failed to make a log directory: $LOG_DIR"
222 if [ "x-$LINK_PTR" != "x-" ]; then
224 ln -fs $LOG_DATE $LINK_PTR
229 # Check available colors on the terminal, if any
230 ncolors
=`tput colors 2>/dev/null || echo 0`
235 # If stdout exists and number of colors is eight or more, use them
236 if [ -t 1 -a "$ncolors" -ge 8 ]; then
237 color_reset
="\033[0m"
239 color_green
="\033[32m"
240 color_blue
="\033[34m"
244 # busybox sed implementation doesn't accept "\x1B", so use [:cntrl:] instead.
245 sed -E "s/[[:cntrl:]]\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
250 if [ "$1" = "-n" ] ; then
254 [ "$KTAP" != "1" ] && printf "$*$newline"
255 [ "$LOG_FILE" ] && printf "$*$newline" | strip_esc
>> $LOG_FILE
258 if [ "${KTAP}" = "1" ]; then
259 cat $1 |
while read line
; do
265 [ "$LOG_FILE" ] && cat $1 | strip_esc
>> $LOG_FILE
267 prlog
"=== Ftrace unit tests ==="
270 # Testcase management
271 # Test result codes - Dejagnu extended code
272 PASS
=0 # The test succeeded.
273 FAIL
=1 # The test failed, but was expected to succeed.
274 UNRESOLVED
=2 # The test produced indeterminate results. (e.g. interrupted)
275 UNTESTED
=3 # The test was not run, currently just a placeholder.
276 UNSUPPORTED
=4 # The test failed because of lack of feature.
277 XFAIL
=5 # The test failed, and was expected to fail.
293 testcase
() { # testfile
295 CASENAME
=`grep "^#[ \t]*description:" $1 | cut -f2- -d:`
298 checkreq
() { # testfile
299 requires
=`grep "^#[ \t]*requires:" $1 | cut -f2- -d:`
300 # Use eval to pass quoted-patterns correctly.
301 eval check_requires
"$requires"
304 test_on_instance
() { # testfile
305 grep -q "^#[ \t]*flags:.*instance" $1
308 ktaptest
() { # result comment
309 if [ "$KTAP" != "1" ]; then
314 if [ "$1" = "1" ]; then
322 if [ "$comment" != "" ]; then
326 echo $result $CASENO $INSTANCE$CASENAME $comment
329 eval_result
() { # sigval
332 prlog
" [${color_green}PASS${color_reset}]"
334 PASSED_CASES
="$PASSED_CASES $CASENO"
338 prlog
" [${color_red}FAIL${color_reset}]"
340 FAILED_CASES
="$FAILED_CASES $CASENO"
341 return 1 # this is a bug.
344 prlog
" [${color_blue}UNRESOLVED${color_reset}]"
345 ktaptest
0 UNRESOLVED
346 UNRESOLVED_CASES
="$UNRESOLVED_CASES $CASENO"
347 return $UNRESOLVED_RESULT # depends on use case
350 prlog
" [${color_blue}UNTESTED${color_reset}]"
352 UNTESTED_CASES
="$UNTESTED_CASES $CASENO"
356 prlog
" [${color_blue}UNSUPPORTED${color_reset}]"
358 UNSUPPORTED_CASES
="$UNSUPPORTED_CASES $CASENO"
359 return $UNSUPPORTED_RESULT # depends on use case
362 prlog
" [${color_green}XFAIL${color_reset}]"
364 XFAILED_CASES
="$XFAILED_CASES $CASENO"
368 prlog
" [${color_blue}UNDEFINED${color_reset}]"
370 UNDEFINED_CASES
="$UNDEFINED_CASES $CASENO"
371 return 1 # this must be a test bug
376 # Signal handling for result codes
378 SIG_BASE
=36 # Use realtime signals
385 SIG_FAIL
=$
((SIG_BASE
+ FAIL
))
389 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
391 SIG_UNRESOLVED
=$
((SIG_BASE
+ UNRESOLVED
))
393 kill -s $SIG_UNRESOLVED $SIG_PID
396 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
398 SIG_UNTESTED
=$
((SIG_BASE
+ UNTESTED
))
400 kill -s $SIG_UNTESTED $SIG_PID
403 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
405 SIG_UNSUPPORTED
=$
((SIG_BASE
+ UNSUPPORTED
))
406 exit_unsupported
() {
407 kill -s $SIG_UNSUPPORTED $SIG_PID
410 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
412 SIG_XFAIL
=$
((SIG_BASE
+ XFAIL
))
414 kill -s $SIG_XFAIL $SIG_PID
417 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
419 __run_test
() { # testfile
420 # setup PID and PPID, $$ is not updated.
421 (cd $TRACING_DIR; read PID _
< /proc
/self
/stat
; set -e; set -x;
422 checkreq
$1; initialize_ftrace
; .
$1)
423 [ $?
-ne 0 ] && kill -s $SIG_FAIL $SIG_PID
427 run_test
() { # testfile
428 local testname
=`basename $1`
430 prlog
-n "[$CASENO]$INSTANCE$CASENAME"
431 if [ ! -z "$LOG_FILE" ] ; then
432 local testlog
=`mktemp $LOG_DIR/${CASENO}-${testname}-log.XXXXXX`
434 local testlog
=/proc
/self
/fd
/1
436 export TMPDIR
=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
437 export FTRACETEST_ROOT
=$TOP_DIR
438 echo "execute$INSTANCE: "$1 > $testlog
440 if [ $VERBOSE -eq -1 ]; then
442 elif [ -z "$LOG_FILE" ]; then
444 elif [ $VERBOSE -ge 3 ]; then
445 __run_test
$1 |
tee -a $testlog 2>&1
446 elif [ $VERBOSE -eq 2 ]; then
447 __run_test
$1 2>> $testlog |
tee -a $testlog
449 __run_test
$1 >> $testlog 2>&1
451 eval_result
$SIG_RESULT
452 if [ $?
-eq 0 ]; then
453 # Remove test log if the test was done as it was expected.
454 [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog
456 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog
$testlog
462 # load in the helper functions
463 .
$TEST_DIR/functions
465 if [ "$KTAP" = "1" ]; then
466 echo "TAP version 13"
468 casecount
=`echo $TEST_CASES | wc -w`
469 for t
in $TEST_CASES; do
470 test_on_instance
$t ||
continue
471 casecount
=$
((casecount
+1))
473 echo "1..${casecount}"
477 for t
in $TEST_CASES; do
479 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
480 echo "A failure detected. Stop test."
485 # Test on instance loop
486 INSTANCE
=" (instance) "
487 for t
in $TEST_CASES; do
488 test_on_instance
$t ||
continue
489 SAVED_TRACING_DIR
=$TRACING_DIR
490 export TRACING_DIR
=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX`
493 TRACING_DIR
=$SAVED_TRACING_DIR
494 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
495 echo "A failure detected. Stop test."
499 (cd $TRACING_DIR; finish_ftrace
) # for cleanup
502 prlog
"# of passed: " `echo $PASSED_CASES | wc -w`
503 prlog
"# of failed: " `echo $FAILED_CASES | wc -w`
504 prlog
"# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
505 prlog
"# of untested: " `echo $UNTESTED_CASES | wc -w`
506 prlog
"# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
507 prlog
"# of xfailed: " `echo $XFAILED_CASES | wc -w`
508 prlog
"# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
510 if [ "$KTAP" = "1" ]; then
512 echo -n " pass:"`echo $PASSED_CASES | wc -w`
513 echo -n " fail:"`echo $FAILED_CASES | wc -w`
514 echo -n " xfail:"`echo $XFAILED_CASES | wc -w`
516 echo -n " skip:"`echo $UNTESTED_CASES $UNSUPPORTED_CASES | wc -w`
517 echo -n " error:"`echo $UNRESOLVED_CASES $UNDEFINED_CASES | wc -w`
523 # if no error, return 0