0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 usage() {
0011 [ ! -z "$2" ] && echo $2
0012 echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]"
0013 echo " Options:"
0014 echo " -h|--help Show help message"
0015 echo " -k|--keep Keep passed test logs"
0016 echo " -v|--verbose Increase verbosity of test messages"
0017 echo " -vv Alias of -v -v (Show all results in stdout)"
0018 echo " -vvv Alias of -v -v -v (Show all commands immediately)"
0019 echo " --fail-unsupported Treat UNSUPPORTED as a failure"
0020 echo " --fail-unresolved Treat UNRESOLVED as a failure"
0021 echo " -d|--debug Debug mode (trace all shell commands)"
0022 echo " -l|--logdir <dir> Save logs on the <dir>"
0023 echo " If <dir> is -, all logs output in console only"
0024 exit $1
0025 }
0026
0027
0028 err_ret=1
0029
0030
0031 err_skip=4
0032
0033
0034
0035
0036
0037 readonly sched_rt_runtime=/proc/sys/kernel/sched_rt_runtime_us
0038
0039 sched_rt_runtime_orig=$(cat $sched_rt_runtime)
0040
0041 setup() {
0042 echo -1 > $sched_rt_runtime
0043 }
0044
0045 cleanup() {
0046 echo $sched_rt_runtime_orig > $sched_rt_runtime
0047 }
0048
0049 errexit() {
0050 echo "Error: $1" 1>&2
0051 cleanup
0052 exit $err_ret
0053 }
0054
0055
0056 if [ `id -u` -ne 0 ]; then
0057 errexit "this must be run by root user"
0058 fi
0059
0060 setup
0061
0062
0063 absdir() {
0064 (cd `dirname $1`; pwd)
0065 }
0066
0067 abspath() {
0068 echo `absdir $1`/`basename $1`
0069 }
0070
0071 find_testcases() {
0072 echo `find $1 -name \*.tc | sort`
0073 }
0074
0075 parse_opts() {
0076 local OPT_TEST_CASES=
0077 local OPT_TEST_DIR=
0078
0079 while [ ! -z "$1" ]; do
0080 case "$1" in
0081 --help|-h)
0082 usage 0
0083 ;;
0084 --keep|-k)
0085 KEEP_LOG=1
0086 shift 1
0087 ;;
0088 --verbose|-v|-vv|-vvv)
0089 if [ $VERBOSE -eq -1 ]; then
0090 usage "--console can not use with --verbose"
0091 fi
0092 VERBOSE=$((VERBOSE + 1))
0093 [ $1 = '-vv' ] && VERBOSE=$((VERBOSE + 1))
0094 [ $1 = '-vvv' ] && VERBOSE=$((VERBOSE + 2))
0095 shift 1
0096 ;;
0097 --console)
0098 if [ $VERBOSE -ne 0 ]; then
0099 usage "--console can not use with --verbose"
0100 fi
0101 VERBOSE=-1
0102 shift 1
0103 ;;
0104 --debug|-d)
0105 DEBUG=1
0106 shift 1
0107 ;;
0108 --stop-fail)
0109 STOP_FAILURE=1
0110 shift 1
0111 ;;
0112 --fail-unsupported)
0113 UNSUPPORTED_RESULT=1
0114 shift 1
0115 ;;
0116 --fail-unresolved)
0117 UNRESOLVED_RESULT=1
0118 shift 1
0119 ;;
0120 --logdir|-l)
0121 LOG_DIR=$2
0122 shift 2
0123 ;;
0124 *.tc)
0125 if [ -f "$1" ]; then
0126 OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`"
0127 shift 1
0128 else
0129 usage 1 "$1 is not a testcase"
0130 fi
0131 ;;
0132 *)
0133 if [ -d "$1" ]; then
0134 OPT_TEST_DIR=`abspath $1`
0135 OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
0136 shift 1
0137 else
0138 usage 1 "Invalid option ($1)"
0139 fi
0140 ;;
0141 esac
0142 done
0143 if [ ! -z "$OPT_TEST_CASES" ]; then
0144 TEST_CASES=$OPT_TEST_CASES
0145 fi
0146 }
0147
0148
0149 TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
0150 if [ -z "$TRACING_DIR" ]; then
0151 DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
0152 if [ -z "$DEBUGFS_DIR" ]; then
0153
0154 if [ -d "/sys/kernel/tracing" ]; then
0155 mount -t tracefs nodev /sys/kernel/tracing ||
0156 errexit "Failed to mount /sys/kernel/tracing"
0157 TRACING_DIR="/sys/kernel/tracing"
0158
0159 elif [ -d "/sys/kernel/debug" ]; then
0160 mount -t debugfs nodev /sys/kernel/debug ||
0161 errexit "Failed to mount /sys/kernel/debug"
0162 TRACING_DIR="/sys/kernel/debug/tracing"
0163 else
0164 err_ret=$err_skip
0165 errexit "debugfs and tracefs are not configured in this kernel"
0166 fi
0167 else
0168 TRACING_DIR="$DEBUGFS_DIR/tracing"
0169 fi
0170 fi
0171 if [ ! -d "$TRACING_DIR" ]; then
0172 err_ret=$err_skip
0173 errexit "ftrace is not configured in this kernel"
0174 fi
0175
0176 TOP_DIR=`absdir $0`
0177 TEST_DIR=$TOP_DIR/test.d
0178 TEST_CASES=`find_testcases $TEST_DIR`
0179 LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/
0180 KEEP_LOG=0
0181 DEBUG=0
0182 VERBOSE=0
0183 UNSUPPORTED_RESULT=0
0184 UNRESOLVED_RESULT=0
0185 STOP_FAILURE=0
0186
0187 parse_opts $*
0188
0189 [ $DEBUG -ne 0 ] && set -x
0190
0191
0192 if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
0193 errexit "No ftrace directory found"
0194 fi
0195
0196
0197 if [ "x$LOG_DIR" = "x-" ]; then
0198 LOG_FILE=
0199 date
0200 else
0201 LOG_FILE=$LOG_DIR/ftracetest.log
0202 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
0203 date > $LOG_FILE
0204 fi
0205
0206
0207
0208 ncolors=`tput colors 2>/dev/null || echo 0`
0209 color_reset=
0210 color_red=
0211 color_green=
0212 color_blue=
0213
0214 if [ -t 1 -a "$ncolors" -ge 8 ]; then
0215 color_reset="\033[0m"
0216 color_red="\033[31m"
0217 color_green="\033[32m"
0218 color_blue="\033[34m"
0219 fi
0220
0221 strip_esc() {
0222
0223 sed -E "s/[[:cntrl:]]\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
0224 }
0225
0226 prlog() {
0227 newline="\n"
0228 if [ "$1" = "-n" ] ; then
0229 newline=
0230 shift
0231 fi
0232 printf "$*$newline"
0233 [ "$LOG_FILE" ] && printf "$*$newline" | strip_esc >> $LOG_FILE
0234 }
0235 catlog() {
0236 cat $1
0237 [ "$LOG_FILE" ] && cat $1 | strip_esc >> $LOG_FILE
0238 }
0239 prlog "=== Ftrace unit tests ==="
0240
0241
0242
0243
0244 PASS=0
0245 FAIL=1
0246 UNRESOLVED=2
0247 UNTESTED=3
0248 UNSUPPORTED=4
0249 XFAIL=5
0250
0251
0252 PASSED_CASES=
0253 FAILED_CASES=
0254 UNRESOLVED_CASES=
0255 UNTESTED_CASES=
0256 UNSUPPORTED_CASES=
0257 XFAILED_CASES=
0258 UNDEFINED_CASES=
0259 TOTAL_RESULT=0
0260
0261 INSTANCE=
0262 CASENO=0
0263
0264 testcase() {
0265 CASENO=$((CASENO+1))
0266 desc=`grep "^#[ \t]*description:" $1 | cut -f2- -d:`
0267 prlog -n "[$CASENO]$INSTANCE$desc"
0268 }
0269
0270 checkreq() {
0271 requires=`grep "^#[ \t]*requires:" $1 | cut -f2- -d:`
0272
0273 eval check_requires "$requires"
0274 }
0275
0276 test_on_instance() {
0277 grep -q "^#[ \t]*flags:.*instance" $1
0278 }
0279
0280 eval_result() {
0281 case $1 in
0282 $PASS)
0283 prlog " [${color_green}PASS${color_reset}]"
0284 PASSED_CASES="$PASSED_CASES $CASENO"
0285 return 0
0286 ;;
0287 $FAIL)
0288 prlog " [${color_red}FAIL${color_reset}]"
0289 FAILED_CASES="$FAILED_CASES $CASENO"
0290 return 1
0291 ;;
0292 $UNRESOLVED)
0293 prlog " [${color_blue}UNRESOLVED${color_reset}]"
0294 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
0295 return $UNRESOLVED_RESULT
0296 ;;
0297 $UNTESTED)
0298 prlog " [${color_blue}UNTESTED${color_reset}]"
0299 UNTESTED_CASES="$UNTESTED_CASES $CASENO"
0300 return 0
0301 ;;
0302 $UNSUPPORTED)
0303 prlog " [${color_blue}UNSUPPORTED${color_reset}]"
0304 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
0305 return $UNSUPPORTED_RESULT
0306 ;;
0307 $XFAIL)
0308 prlog " [${color_green}XFAIL${color_reset}]"
0309 XFAILED_CASES="$XFAILED_CASES $CASENO"
0310 return 0
0311 ;;
0312 *)
0313 prlog " [${color_blue}UNDEFINED${color_reset}]"
0314 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
0315 return 1
0316 ;;
0317 esac
0318 }
0319
0320
0321 SIG_RESULT=
0322 SIG_BASE=36
0323 SIG_PID=$$
0324
0325 exit_pass () {
0326 exit 0
0327 }
0328
0329 SIG_FAIL=$((SIG_BASE + FAIL))
0330 exit_fail () {
0331 exit 1
0332 }
0333 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
0334
0335 SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
0336 exit_unresolved () {
0337 kill -s $SIG_UNRESOLVED $SIG_PID
0338 exit 0
0339 }
0340 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
0341
0342 SIG_UNTESTED=$((SIG_BASE + UNTESTED))
0343 exit_untested () {
0344 kill -s $SIG_UNTESTED $SIG_PID
0345 exit 0
0346 }
0347 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
0348
0349 SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
0350 exit_unsupported () {
0351 kill -s $SIG_UNSUPPORTED $SIG_PID
0352 exit 0
0353 }
0354 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
0355
0356 SIG_XFAIL=$((SIG_BASE + XFAIL))
0357 exit_xfail () {
0358 kill -s $SIG_XFAIL $SIG_PID
0359 exit 0
0360 }
0361 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
0362
0363 __run_test() {
0364
0365 (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x;
0366 checkreq $1; initialize_ftrace; . $1)
0367 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
0368 }
0369
0370
0371 run_test() {
0372 local testname=`basename $1`
0373 testcase $1
0374 if [ ! -z "$LOG_FILE" ] ; then
0375 local testlog=`mktemp $LOG_DIR/${CASENO}-${testname}-log.XXXXXX`
0376 else
0377 local testlog=/proc/self/fd/1
0378 fi
0379 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
0380 export FTRACETEST_ROOT=$TOP_DIR
0381 echo "execute$INSTANCE: "$1 > $testlog
0382 SIG_RESULT=0
0383 if [ $VERBOSE -eq -1 ]; then
0384 __run_test $1
0385 elif [ -z "$LOG_FILE" ]; then
0386 __run_test $1 2>&1
0387 elif [ $VERBOSE -ge 3 ]; then
0388 __run_test $1 | tee -a $testlog 2>&1
0389 elif [ $VERBOSE -eq 2 ]; then
0390 __run_test $1 2>> $testlog | tee -a $testlog
0391 else
0392 __run_test $1 >> $testlog 2>&1
0393 fi
0394 eval_result $SIG_RESULT
0395 if [ $? -eq 0 ]; then
0396
0397 [ $KEEP_LOG -eq 0 -a ! -z "$LOG_FILE" ] && rm $testlog
0398 else
0399 [ $VERBOSE -eq 1 -o $VERBOSE -eq 2 ] && catlog $testlog
0400 TOTAL_RESULT=1
0401 fi
0402 rm -rf $TMPDIR
0403 }
0404
0405
0406 . $TEST_DIR/functions
0407
0408
0409 for t in $TEST_CASES; do
0410 run_test $t
0411 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
0412 echo "A failure detected. Stop test."
0413 exit 1
0414 fi
0415 done
0416
0417
0418 INSTANCE=" (instance) "
0419 for t in $TEST_CASES; do
0420 test_on_instance $t || continue
0421 SAVED_TRACING_DIR=$TRACING_DIR
0422 export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX`
0423 run_test $t
0424 rmdir $TRACING_DIR
0425 TRACING_DIR=$SAVED_TRACING_DIR
0426 if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
0427 echo "A failure detected. Stop test."
0428 exit 1
0429 fi
0430 done
0431 (cd $TRACING_DIR; finish_ftrace)
0432
0433 prlog ""
0434 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
0435 prlog "# of failed: " `echo $FAILED_CASES | wc -w`
0436 prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
0437 prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
0438 prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
0439 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
0440 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
0441
0442 cleanup
0443
0444
0445 exit $TOTAL_RESULT