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
259 [ "$LOG_FILE" ] && cat $1 | strip_esc >> $LOG_FILE
261 prlog "=== Ftrace unit tests ==="
264 # Testcase management
265 # Test result codes - Dejagnu extended code
266 PASS=0 # The test succeeded.
267 FAIL=1 # The test failed, but was expected to succeed.
268 UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted)
269 UNTESTED=3 # The test was not run, currently just a placeholder.
270 UNSUPPORTED=4 # The test failed because of lack of feature.
271 XFAIL=5 # The test failed, and was expected to fail.
287 testcase() { # testfile
289 CASENAME=`grep "^#[ \t]*description:" $1 | cut -f2- -d:`
292 checkreq() { # testfile
293 requires=`grep "^#[ \t]*requires:" $1 | cut -f2- -d:`
294 # Use eval to pass quoted-patterns correctly.
295 eval check_requires "$requires"
298 test_on_instance() { # testfile
299 grep -q "^#[ \t]*flags:.*instance" $1
302 ktaptest() { # result comment
303 if [ "$KTAP" != "1" ]; then
308 if [ "$1" = "1" ]; then
316 if [ "$comment" != "" ]; then
320 echo $result $CASENO $INSTANCE$CASENAME $comment
323 eval_result() { # sigval
326 prlog " [${color_green}PASS${color_reset}]"
328 PASSED_CASES="$PASSED_CASES $CASENO"
332 prlog " [${color_red}FAIL${color_reset}]"
334 FAILED_CASES="$FAILED_CASES $CASENO"
335 return 1 # this is a bug.
338 prlog " [${color_blue}UNRESOLVED${color_reset}]"
339 ktaptest 0 UNRESOLVED
340 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
341 return $UNRESOLVED_RESULT # depends on use case
344 prlog " [${color_blue}UNTESTED${color_reset}]"
346 UNTESTED_CASES="$UNTESTED_CASES $CASENO"
350 prlog " [${color_blue}UNSUPPORTED${color_reset}]"
352 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
353 return $UNSUPPORTED_RESULT # depends on use case
356 prlog " [${color_green}XFAIL${color_reset}]"
358 XFAILED_CASES="$XFAILED_CASES $CASENO"
362 prlog " [${color_blue}UNDEFINED${color_reset}]"
364 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
365 return 1 # this must be a test bug
370 # Signal handling for result codes
372 SIG_BASE=36 # Use realtime signals
379 SIG_FAIL=$((SIG_BASE + FAIL))
383 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
385 SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
387 kill -s $SIG_UNRESOLVED $SIG_PID
390 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
392 SIG_UNTESTED=$((SIG_BASE + UNTESTED))
394 kill -s $SIG_UNTESTED $SIG_PID
397 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
399 SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
400 exit_unsupported () {
401 kill -s $SIG_UNSUPPORTED $SIG_PID
404 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
406 SIG_XFAIL=$((SIG_BASE + XFAIL))
408 kill -s $SIG_XFAIL $SIG_PID
411 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
413 __run_test() { # testfile
414 # setup PID and PPID, $$ is not updated.
415 (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x;
416 checkreq $1; initialize_ftrace; . $1)
417 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
421 run_test() { # testfile
422 local testname=`basename $1`
424 prlog -n "[$CASENO]$INSTANCE$CASENAME"
425 if [ ! -z "$LOG_FILE" ] ; then
426 local testlog=`mktemp $LOG_DIR/${CASENO}-${testname}-log.XXXXXX`
428 local testlog=/proc/self/fd/1
430 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
431 export FTRACETEST_ROOT=$TOP_DIR
432 echo "execute$INSTANCE: "$1 > $testlog
434 if [ $VERBOSE -eq -1 ]; then
436 elif [ -z "$LOG_FILE" ]; then
438 elif [ $VERBOSE -ge 3 ]; then
439 __run_test $1 | tee -a $testlog 2>&1
440 elif [ $VERBOSE -eq 2 ]; then
441 __run_test $1 2>> $testlog | tee -a $testlog
443 __run_test $1 >> $testlog 2>&1
445 eval_result $SIG_RESULT
446 if [ $? -eq 0 ]; then
447 # Remove test log if the test was done as it was expected.
448 [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog
450 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog
456 # load in the helper functions
457 . $TEST_DIR/functions
459 if [ "$KTAP" = "1" ]; then
460 echo "TAP version 13"
462 casecount=`echo $TEST_CASES | wc -w`
463 for t in $TEST_CASES; do
464 test_on_instance $t || continue
465 casecount=$((casecount+1))
467 echo "1..${casecount}"
471 for t in $TEST_CASES; do
473 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
474 echo "A failure detected. Stop test."
479 # Test on instance loop
480 INSTANCE=" (instance) "
481 for t in $TEST_CASES; do
482 test_on_instance $t || continue
483 SAVED_TRACING_DIR=$TRACING_DIR
484 export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX`
487 TRACING_DIR=$SAVED_TRACING_DIR
488 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
489 echo "A failure detected. Stop test."
493 (cd $TRACING_DIR; finish_ftrace) # for cleanup
496 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
497 prlog "# of failed: " `echo $FAILED_CASES | wc -w`
498 prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
499 prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
500 prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
501 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
502 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
504 if [ "$KTAP" = "1" ]; then
506 echo -n " pass:"`echo $PASSED_CASES | wc -w`
507 echo -n " faii:"`echo $FAILED_CASES | wc -w`
508 echo -n " xfail:"`echo $XFAILED_CASES | wc -w`
510 echo -n " skip:"`echo $UNTESTED_CASES $UNSUPPORTED_CASES | wc -w`
511 echo -n " error:"`echo $UNRESOLVED_CASES $UNDEFINED_CASES | wc -w`
517 # if no error, return 0