525 lines · plain
1#!/bin/sh2# SPDX-License-Identifier: GPL-2.0-only3 4# ftracetest - Ftrace test shell scripts5#6# Copyright (C) Hitachi Ltd., 20147# Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>8#9 10usage() { # errno [message]11[ ! -z "$2" ] && echo $212echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]"13echo " Options:"14echo " -h|--help Show help message"15echo " -k|--keep Keep passed test logs"16echo " -K|--ktap Output in KTAP format"17echo " -v|--verbose Increase verbosity of test messages"18echo " -vv Alias of -v -v (Show all results in stdout)"19echo " -vvv Alias of -v -v -v (Show all commands immediately)"20echo " --fail-unsupported Treat UNSUPPORTED as a failure"21echo " --fail-unresolved Treat UNRESOLVED as a failure"22echo " -d|--debug Debug mode (trace all shell commands)"23echo " -l|--logdir <dir> Save logs on the <dir>"24echo " If <dir> is -, all logs output in console only"25exit $126}27 28# default error29err_ret=130 31# kselftest skip code is 432err_skip=433 34# umount required35UMOUNT_DIR=""36 37# cgroup RT scheduling prevents chrt commands from succeeding, which38# induces failures in test wakeup tests. Disable for the duration of39# the tests.40 41readonly sched_rt_runtime=/proc/sys/kernel/sched_rt_runtime_us42 43sched_rt_runtime_orig=$(cat $sched_rt_runtime)44 45setup() {46 echo -1 > $sched_rt_runtime47}48 49cleanup() {50 echo $sched_rt_runtime_orig > $sched_rt_runtime51 if [ -n "${UMOUNT_DIR}" ]; then52 umount ${UMOUNT_DIR} ||:53 fi54}55 56errexit() { # message57 echo "Error: $1" 1>&258 cleanup59 exit $err_ret60}61 62# Ensuring user privilege63if [ `id -u` -ne 0 ]; then64 errexit "this must be run by root user"65fi66 67setup68 69# Utilities70absdir() { # file_path71 (cd `dirname $1`; pwd)72}73 74abspath() {75 echo `absdir $1`/`basename $1`76}77 78find_testcases() { #directory79 echo `find $1 -name \*.tc | sort`80}81 82parse_opts() { # opts83 local OPT_TEST_CASES=84 local OPT_TEST_DIR=85 86 while [ ! -z "$1" ]; do87 case "$1" in88 --help|-h)89 usage 090 ;;91 --keep|-k)92 KEEP_LOG=193 shift 194 ;;95 --ktap|-K)96 KTAP=197 shift 198 ;;99 --verbose|-v|-vv|-vvv)100 if [ $VERBOSE -eq -1 ]; then101 usage "--console can not use with --verbose"102 fi103 VERBOSE=$((VERBOSE + 1))104 [ $1 = '-vv' ] && VERBOSE=$((VERBOSE + 1))105 [ $1 = '-vvv' ] && VERBOSE=$((VERBOSE + 2))106 shift 1107 ;;108 --console)109 if [ $VERBOSE -ne 0 ]; then110 usage "--console can not use with --verbose"111 fi112 VERBOSE=-1113 shift 1114 ;;115 --debug|-d)116 DEBUG=1117 shift 1118 ;;119 --stop-fail)120 STOP_FAILURE=1121 shift 1122 ;;123 --fail-unsupported)124 UNSUPPORTED_RESULT=1125 shift 1126 ;;127 --fail-unresolved)128 UNRESOLVED_RESULT=1129 shift 1130 ;;131 --logdir|-l)132 LOG_DIR=$2133 LINK_PTR=134 shift 2135 ;;136 *.tc)137 if [ -f "$1" ]; then138 OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`"139 shift 1140 else141 usage 1 "$1 is not a testcase"142 fi143 ;;144 *)145 if [ -d "$1" ]; then146 OPT_TEST_DIR=`abspath $1`147 OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"148 shift 1149 else150 usage 1 "Invalid option ($1)"151 fi152 ;;153 esac154 done155 if [ ! -z "$OPT_TEST_CASES" ]; then156 TEST_CASES=$OPT_TEST_CASES157 fi158}159 160# Parameters161TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`162if [ -z "$TRACING_DIR" ]; then163 DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`164 if [ -z "$DEBUGFS_DIR" ]; then165 # If tracefs exists, then so does /sys/kernel/tracing166 if [ -d "/sys/kernel/tracing" ]; then167 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/debug172 elif [ -d "/sys/kernel/debug" ]; then173 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}177 else178 err_ret=$err_skip179 errexit "debugfs and tracefs are not configured in this kernel"180 fi181 else182 TRACING_DIR="$DEBUGFS_DIR/tracing"183 fi184fi185if [ ! -d "$TRACING_DIR" ]; then186 err_ret=$err_skip187 errexit "ftrace is not configured in this kernel"188fi189 190TOP_DIR=`absdir $0`191TEST_DIR=$TOP_DIR/test.d192TEST_CASES=`find_testcases $TEST_DIR`193LOG_TOP_DIR=$TOP_DIR/logs194LOG_DATE=`date +%Y%m%d-%H%M%S`195LOG_DIR=$LOG_TOP_DIR/$LOG_DATE/196LINK_PTR=$LOG_TOP_DIR/latest197KEEP_LOG=0198KTAP=0199DEBUG=0200VERBOSE=0201UNSUPPORTED_RESULT=0202UNRESOLVED_RESULT=0203STOP_FAILURE=0204# Parse command-line options205parse_opts $*206 207[ $DEBUG -ne 0 ] && set -x208 209# Verify parameters210if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then211 errexit "No ftrace directory found"212fi213 214# Preparing logs215if [ "x$LOG_DIR" = "x-" ]; then216 LOG_FILE=217 date218else219 LOG_FILE=$LOG_DIR/ftracetest.log220 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"221 date > $LOG_FILE222 if [ "x-$LINK_PTR" != "x-" ]; then223 unlink $LINK_PTR224 ln -fs $LOG_DATE $LINK_PTR225 fi226fi227 228# Define text colors229# Check available colors on the terminal, if any230ncolors=`tput colors 2>/dev/null || echo 0`231color_reset=232color_red=233color_green=234color_blue=235# If stdout exists and number of colors is eight or more, use them236if [ -t 1 -a "$ncolors" -ge 8 ]; then237 color_reset="\033[0m"238 color_red="\033[31m"239 color_green="\033[32m"240 color_blue="\033[34m"241fi242 243strip_esc() {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"246}247 248prlog() { # messages249 newline="\n"250 if [ "$1" = "-n" ] ; then251 newline=252 shift253 fi254 [ "$KTAP" != "1" ] && printf "$*$newline"255 [ "$LOG_FILE" ] && printf "$*$newline" | strip_esc >> $LOG_FILE256}257catlog() { #file258 if [ "${KTAP}" = "1" ]; then259 cat $1 | while read line ; do260 echo "# $line"261 done262 else263 cat $1264 fi265 [ "$LOG_FILE" ] && cat $1 | strip_esc >> $LOG_FILE266}267prlog "=== Ftrace unit tests ==="268 269 270# Testcase management271# Test result codes - Dejagnu extended code272PASS=0 # The test succeeded.273FAIL=1 # The test failed, but was expected to succeed.274UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted)275UNTESTED=3 # The test was not run, currently just a placeholder.276UNSUPPORTED=4 # The test failed because of lack of feature.277XFAIL=5 # The test failed, and was expected to fail.278 279# Accumulations280PASSED_CASES=281FAILED_CASES=282UNRESOLVED_CASES=283UNTESTED_CASES=284UNSUPPORTED_CASES=285XFAILED_CASES=286UNDEFINED_CASES=287TOTAL_RESULT=0288 289INSTANCE=290CASENO=0291CASENAME=292 293testcase() { # testfile294 CASENO=$((CASENO+1))295 CASENAME=`grep "^#[ \t]*description:" $1 | cut -f2- -d:`296}297 298checkreq() { # testfile299 requires=`grep "^#[ \t]*requires:" $1 | cut -f2- -d:`300 # Use eval to pass quoted-patterns correctly.301 eval check_requires "$requires"302}303 304test_on_instance() { # testfile305 grep -q "^#[ \t]*flags:.*instance" $1306}307 308ktaptest() { # result comment309 if [ "$KTAP" != "1" ]; then310 return311 fi312 313 local result=314 if [ "$1" = "1" ]; then315 result="ok"316 else317 result="not ok"318 fi319 shift320 321 local comment=$*322 if [ "$comment" != "" ]; then323 comment="# $comment"324 fi325 326 echo $result $CASENO $INSTANCE$CASENAME $comment327}328 329eval_result() { # sigval330 case $1 in331 $PASS)332 prlog " [${color_green}PASS${color_reset}]"333 ktaptest 1334 PASSED_CASES="$PASSED_CASES $CASENO"335 return 0336 ;;337 $FAIL)338 prlog " [${color_red}FAIL${color_reset}]"339 ktaptest 0340 FAILED_CASES="$FAILED_CASES $CASENO"341 return 1 # this is a bug.342 ;;343 $UNRESOLVED)344 prlog " [${color_blue}UNRESOLVED${color_reset}]"345 ktaptest 0 UNRESOLVED346 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"347 return $UNRESOLVED_RESULT # depends on use case348 ;;349 $UNTESTED)350 prlog " [${color_blue}UNTESTED${color_reset}]"351 ktaptest 1 SKIP352 UNTESTED_CASES="$UNTESTED_CASES $CASENO"353 return 0354 ;;355 $UNSUPPORTED)356 prlog " [${color_blue}UNSUPPORTED${color_reset}]"357 ktaptest 1 SKIP358 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"359 return $UNSUPPORTED_RESULT # depends on use case360 ;;361 $XFAIL)362 prlog " [${color_green}XFAIL${color_reset}]"363 ktaptest 1 XFAIL364 XFAILED_CASES="$XFAILED_CASES $CASENO"365 return 0366 ;;367 *)368 prlog " [${color_blue}UNDEFINED${color_reset}]"369 ktaptest 0 error370 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"371 return 1 # this must be a test bug372 ;;373 esac374}375 376# Signal handling for result codes377SIG_RESULT=378SIG_BASE=36 # Use realtime signals379SIG_PID=$$380 381exit_pass () {382 exit 0383}384 385SIG_FAIL=$((SIG_BASE + FAIL))386exit_fail () {387 exit 1388}389trap 'SIG_RESULT=$FAIL' $SIG_FAIL390 391SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))392exit_unresolved () {393 kill -s $SIG_UNRESOLVED $SIG_PID394 exit 0395}396trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED397 398SIG_UNTESTED=$((SIG_BASE + UNTESTED))399exit_untested () {400 kill -s $SIG_UNTESTED $SIG_PID401 exit 0402}403trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED404 405SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))406exit_unsupported () {407 kill -s $SIG_UNSUPPORTED $SIG_PID408 exit 0409}410trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED411 412SIG_XFAIL=$((SIG_BASE + XFAIL))413exit_xfail () {414 kill -s $SIG_XFAIL $SIG_PID415 exit 0416}417trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL418 419__run_test() { # testfile420 # 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_PID424}425 426# Run one test case427run_test() { # testfile428 local testname=`basename $1`429 testcase $1430 prlog -n "[$CASENO]$INSTANCE$CASENAME"431 if [ ! -z "$LOG_FILE" ] ; then432 local testlog=`mktemp $LOG_DIR/${CASENO}-${testname}-log.XXXXXX`433 else434 local testlog=/proc/self/fd/1435 fi436 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`437 export FTRACETEST_ROOT=$TOP_DIR438 echo "execute$INSTANCE: "$1 > $testlog439 SIG_RESULT=0440 if [ $VERBOSE -eq -1 ]; then441 __run_test $1442 elif [ -z "$LOG_FILE" ]; then443 __run_test $1 2>&1444 elif [ $VERBOSE -ge 3 ]; then445 __run_test $1 | tee -a $testlog 2>&1446 elif [ $VERBOSE -eq 2 ]; then447 __run_test $1 2>> $testlog | tee -a $testlog448 else449 __run_test $1 >> $testlog 2>&1450 fi451 eval_result $SIG_RESULT452 if [ $? -eq 0 ]; then453 # Remove test log if the test was done as it was expected.454 [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog455 else456 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog457 TOTAL_RESULT=1458 fi459 rm -rf $TMPDIR460}461 462# load in the helper functions463. $TEST_DIR/functions464 465if [ "$KTAP" = "1" ]; then466 echo "TAP version 13"467 468 casecount=`echo $TEST_CASES | wc -w`469 for t in $TEST_CASES; do470 test_on_instance $t || continue471 casecount=$((casecount+1))472 done473 echo "1..${casecount}"474fi475 476# Main loop477for t in $TEST_CASES; do478 run_test $t479 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then480 echo "A failure detected. Stop test."481 exit 1482 fi483done484 485# Test on instance loop486INSTANCE=" (instance) "487for t in $TEST_CASES; do488 test_on_instance $t || continue489 SAVED_TRACING_DIR=$TRACING_DIR490 export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX`491 run_test $t492 rmdir $TRACING_DIR493 TRACING_DIR=$SAVED_TRACING_DIR494 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then495 echo "A failure detected. Stop test."496 exit 1497 fi498done499(cd $TRACING_DIR; finish_ftrace) # for cleanup500 501prlog ""502prlog "# of passed: " `echo $PASSED_CASES | wc -w`503prlog "# of failed: " `echo $FAILED_CASES | wc -w`504prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`505prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`506prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`507prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`508prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`509 510if [ "$KTAP" = "1" ]; then511 echo -n "# Totals:"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`515 echo -n " xpass:0"516 echo -n " skip:"`echo $UNTESTED_CASES $UNSUPPORTED_CASES | wc -w`517 echo -n " error:"`echo $UNRESOLVED_CASES $UNDEFINED_CASES | wc -w`518 echo519fi520 521cleanup522 523# if no error, return 0524exit $TOTAL_RESULT525