Merge remote-tracking branch 'asoc/fix/rt286' into asoc-linus
[sfrench/cifs-2.6.git] / tools / testing / selftests / ftrace / ftracetest
1 #!/bin/sh
2
3 # ftracetest - Ftrace test shell scripts
4 #
5 # Copyright (C) Hitachi Ltd., 2014
6 #  Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
7 #
8 # Released under the terms of the GPL v2.
9
10 usage() { # errno [message]
11 [ "$2" ] && echo $2
12 echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]"
13 echo " Options:"
14 echo "          -h|--help  Show help message"
15 echo "          -k|--keep  Keep passed test logs"
16 echo "          -v|--verbose Increase verbosity of test messages"
17 echo "          -vv        Alias of -v -v (Show all results in stdout)"
18 echo "          -d|--debug Debug mode (trace all shell commands)"
19 echo "          -l|--logdir <dir> Save logs on the <dir>"
20 exit $1
21 }
22
23 errexit() { # message
24   echo "Error: $1" 1>&2
25   exit 1
26 }
27
28 # Ensuring user privilege
29 if [ `id -u` -ne 0 ]; then
30   errexit "this must be run by root user"
31 fi
32
33 # Utilities
34 absdir() { # file_path
35   (cd `dirname $1`; pwd)
36 }
37
38 abspath() {
39   echo `absdir $1`/`basename $1`
40 }
41
42 find_testcases() { #directory
43   echo `find $1 -name \*.tc | sort`
44 }
45
46 parse_opts() { # opts
47   local OPT_TEST_CASES=
48   local OPT_TEST_DIR=
49
50   while [ "$1" ]; do
51     case "$1" in
52     --help|-h)
53       usage 0
54     ;;
55     --keep|-k)
56       KEEP_LOG=1
57       shift 1
58     ;;
59     --verbose|-v|-vv)
60       VERBOSE=$((VERBOSE + 1))
61       [ $1 = '-vv' ] && VERBOSE=$((VERBOSE + 1))
62       shift 1
63     ;;
64     --debug|-d)
65       DEBUG=1
66       shift 1
67     ;;
68     --logdir|-l)
69       LOG_DIR=$2
70       shift 2
71     ;;
72     *.tc)
73       if [ -f "$1" ]; then
74         OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`"
75         shift 1
76       else
77         usage 1 "$1 is not a testcase"
78       fi
79       ;;
80     *)
81       if [ -d "$1" ]; then
82         OPT_TEST_DIR=`abspath $1`
83         OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`"
84         shift 1
85       else
86         usage 1 "Invalid option ($1)"
87       fi
88     ;;
89     esac
90   done
91   if [ "$OPT_TEST_CASES" ]; then
92     TEST_CASES=$OPT_TEST_CASES
93   fi
94 }
95
96 # Parameters
97 DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
98 if [ -z "$DEBUGFS_DIR" ]; then
99     TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
100 else
101     TRACING_DIR=$DEBUGFS_DIR/tracing
102 fi
103
104 TOP_DIR=`absdir $0`
105 TEST_DIR=$TOP_DIR/test.d
106 TEST_CASES=`find_testcases $TEST_DIR`
107 LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/
108 KEEP_LOG=0
109 DEBUG=0
110 VERBOSE=0
111 # Parse command-line options
112 parse_opts $*
113
114 [ $DEBUG -ne 0 ] && set -x
115
116 # Verify parameters
117 if [ -z "$TRACING_DIR" -o ! -d "$TRACING_DIR" ]; then
118   errexit "No ftrace directory found"
119 fi
120
121 # Preparing logs
122 LOG_FILE=$LOG_DIR/ftracetest.log
123 mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR"
124 date > $LOG_FILE
125 prlog() { # messages
126   echo "$@" | tee -a $LOG_FILE
127 }
128 catlog() { #file
129   cat $1 | tee -a $LOG_FILE
130 }
131 prlog "=== Ftrace unit tests ==="
132
133
134 # Testcase management
135 # Test result codes - Dejagnu extended code
136 PASS=0  # The test succeeded.
137 FAIL=1  # The test failed, but was expected to succeed.
138 UNRESOLVED=2  # The test produced indeterminate results. (e.g. interrupted)
139 UNTESTED=3    # The test was not run, currently just a placeholder.
140 UNSUPPORTED=4 # The test failed because of lack of feature.
141 XFAIL=5 # The test failed, and was expected to fail.
142
143 # Accumulations
144 PASSED_CASES=
145 FAILED_CASES=
146 UNRESOLVED_CASES=
147 UNTESTED_CASES=
148 UNSUPPORTED_CASES=
149 XFAILED_CASES=
150 UNDEFINED_CASES=
151 TOTAL_RESULT=0
152
153 INSTANCE=
154 CASENO=0
155 testcase() { # testfile
156   CASENO=$((CASENO+1))
157   desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
158   prlog -n "[$CASENO]$INSTANCE$desc"
159 }
160
161 test_on_instance() { # testfile
162   grep -q "^#[ \t]*flags:.*instance" $1
163 }
164
165 eval_result() { # sigval
166   case $1 in
167     $PASS)
168       prlog "   [PASS]"
169       PASSED_CASES="$PASSED_CASES $CASENO"
170       return 0
171     ;;
172     $FAIL)
173       prlog "   [FAIL]"
174       FAILED_CASES="$FAILED_CASES $CASENO"
175       return 1 # this is a bug.
176     ;;
177     $UNRESOLVED)
178       prlog "   [UNRESOLVED]"
179       UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
180       return 1 # this is a kind of bug.. something happened.
181     ;;
182     $UNTESTED)
183       prlog "   [UNTESTED]"
184       UNTESTED_CASES="$UNTESTED_CASES $CASENO"
185       return 0
186     ;;
187     $UNSUPPORTED)
188       prlog "   [UNSUPPORTED]"
189       UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
190       return 1 # this is not a bug, but the result should be reported.
191     ;;
192     $XFAIL)
193       prlog "   [XFAIL]"
194       XFAILED_CASES="$XFAILED_CASES $CASENO"
195       return 0
196     ;;
197     *)
198       prlog "   [UNDEFINED]"
199       UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
200       return 1 # this must be a test bug
201     ;;
202   esac
203 }
204
205 # Signal handling for result codes
206 SIG_RESULT=
207 SIG_BASE=36     # Use realtime signals
208 SIG_PID=$$
209
210 SIG_FAIL=$((SIG_BASE + FAIL))
211 trap 'SIG_RESULT=$FAIL' $SIG_FAIL
212
213 SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED))
214 exit_unresolved () {
215   kill -s $SIG_UNRESOLVED $SIG_PID
216   exit 0
217 }
218 trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED
219
220 SIG_UNTESTED=$((SIG_BASE + UNTESTED))
221 exit_untested () {
222   kill -s $SIG_UNTESTED $SIG_PID
223   exit 0
224 }
225 trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED
226
227 SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED))
228 exit_unsupported () {
229   kill -s $SIG_UNSUPPORTED $SIG_PID
230   exit 0
231 }
232 trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED
233
234 SIG_XFAIL=$((SIG_BASE + XFAIL))
235 exit_xfail () {
236   kill -s $SIG_XFAIL $SIG_PID
237   exit 0
238 }
239 trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL
240
241 __run_test() { # testfile
242   # setup PID and PPID, $$ is not updated.
243   (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1)
244   [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
245 }
246
247 # Run one test case
248 run_test() { # testfile
249   local testname=`basename $1`
250   local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
251   export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
252   testcase $1
253   echo "execute: "$1 > $testlog
254   SIG_RESULT=0
255   if [ $VERBOSE -ge 2 ]; then
256     __run_test $1 2>> $testlog | tee -a $testlog
257   else
258     __run_test $1 >> $testlog 2>&1
259   fi
260   eval_result $SIG_RESULT
261   if [ $? -eq 0 ]; then
262     # Remove test log if the test was done as it was expected.
263     [ $KEEP_LOG -eq 0 ] && rm $testlog
264   else
265     [ $VERBOSE -ge 1 ] && catlog $testlog
266     TOTAL_RESULT=1
267   fi
268   rm -rf $TMPDIR
269 }
270
271 # load in the helper functions
272 . $TEST_DIR/functions
273
274 # Main loop
275 for t in $TEST_CASES; do
276   run_test $t
277 done
278
279 # Test on instance loop
280 INSTANCE=" (instance) "
281 for t in $TEST_CASES; do
282   test_on_instance $t || continue
283   SAVED_TRACING_DIR=$TRACING_DIR
284   export TRACING_DIR=`mktemp -d $TRACING_DIR/instances/ftracetest.XXXXXX`
285   run_test $t
286   rmdir $TRACING_DIR
287   TRACING_DIR=$SAVED_TRACING_DIR
288 done
289
290 prlog ""
291 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
292 prlog "# of failed: " `echo $FAILED_CASES | wc -w`
293 prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w`
294 prlog "# of untested: " `echo $UNTESTED_CASES | wc -w`
295 prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w`
296 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
297 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
298
299 # if no error, return 0
300 exit $TOTAL_RESULT