forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/tools/testing/selftests/ftrace/ftracetest
....@@ -1,11 +1,11 @@
11 #!/bin/sh
2
+# SPDX-License-Identifier: GPL-2.0-only
23
34 # ftracetest - Ftrace test shell scripts
45 #
56 # Copyright (C) Hitachi Ltd., 2014
67 # Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
78 #
8
-# Released under the terms of the GPL v2.
99
1010 usage() { # errno [message]
1111 [ ! -z "$2" ] && echo $2
....@@ -17,21 +17,53 @@
1717 echo " -vv Alias of -v -v (Show all results in stdout)"
1818 echo " -vvv Alias of -v -v -v (Show all commands immediately)"
1919 echo " --fail-unsupported Treat UNSUPPORTED as a failure"
20
+echo " --fail-unresolved Treat UNRESOLVED as a failure"
2021 echo " -d|--debug Debug mode (trace all shell commands)"
2122 echo " -l|--logdir <dir> Save logs on the <dir>"
2223 echo " If <dir> is -, all logs output in console only"
2324 exit $1
2425 }
2526
27
+# default error
28
+err_ret=1
29
+
30
+# kselftest skip code is 4
31
+err_skip=4
32
+
33
+# umount required
34
+UMOUNT_DIR=""
35
+
36
+# cgroup RT scheduling prevents chrt commands from succeeding, which
37
+# induces failures in test wakeup tests. Disable for the duration of
38
+# the tests.
39
+
40
+readonly sched_rt_runtime=/proc/sys/kernel/sched_rt_runtime_us
41
+
42
+sched_rt_runtime_orig=$(cat $sched_rt_runtime)
43
+
44
+setup() {
45
+ echo -1 > $sched_rt_runtime
46
+}
47
+
48
+cleanup() {
49
+ echo $sched_rt_runtime_orig > $sched_rt_runtime
50
+ if [ -n "${UMOUNT_DIR}" ]; then
51
+ umount ${UMOUNT_DIR} ||:
52
+ fi
53
+}
54
+
2655 errexit() { # message
2756 echo "Error: $1" 1>&2
28
- exit 1
57
+ cleanup
58
+ exit $err_ret
2959 }
3060
3161 # Ensuring user privilege
3262 if [ `id -u` -ne 0 ]; then
3363 errexit "this must be run by root user"
3464 fi
65
+
66
+setup
3567
3668 # Utilities
3769 absdir() { # file_path
....@@ -60,17 +92,35 @@
6092 shift 1
6193 ;;
6294 --verbose|-v|-vv|-vvv)
95
+ if [ $VERBOSE -eq -1 ]; then
96
+ usage "--console can not use with --verbose"
97
+ fi
6398 VERBOSE=$((VERBOSE + 1))
6499 [ $1 = '-vv' ] && VERBOSE=$((VERBOSE + 1))
65100 [ $1 = '-vvv' ] && VERBOSE=$((VERBOSE + 2))
101
+ shift 1
102
+ ;;
103
+ --console)
104
+ if [ $VERBOSE -ne 0 ]; then
105
+ usage "--console can not use with --verbose"
106
+ fi
107
+ VERBOSE=-1
66108 shift 1
67109 ;;
68110 --debug|-d)
69111 DEBUG=1
70112 shift 1
71113 ;;
114
+ --stop-fail)
115
+ STOP_FAILURE=1
116
+ shift 1
117
+ ;;
72118 --fail-unsupported)
73119 UNSUPPORTED_RESULT=1
120
+ shift 1
121
+ ;;
122
+ --fail-unresolved)
123
+ UNRESOLVED_RESULT=1
74124 shift 1
75125 ;;
76126 --logdir|-l)
....@@ -102,11 +152,33 @@
102152 }
103153
104154 # Parameters
105
-DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
106
-if [ -z "$DEBUGFS_DIR" ]; then
107
- TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
108
-else
109
- TRACING_DIR=$DEBUGFS_DIR/tracing
155
+TRACING_DIR=`grep tracefs /proc/mounts | cut -f2 -d' ' | head -1`
156
+if [ -z "$TRACING_DIR" ]; then
157
+ DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' ' | head -1`
158
+ if [ -z "$DEBUGFS_DIR" ]; then
159
+ # If tracefs exists, then so does /sys/kernel/tracing
160
+ if [ -d "/sys/kernel/tracing" ]; then
161
+ mount -t tracefs nodev /sys/kernel/tracing ||
162
+ errexit "Failed to mount /sys/kernel/tracing"
163
+ TRACING_DIR="/sys/kernel/tracing"
164
+ UMOUNT_DIR=${TRACING_DIR}
165
+ # If debugfs exists, then so does /sys/kernel/debug
166
+ elif [ -d "/sys/kernel/debug" ]; then
167
+ mount -t debugfs nodev /sys/kernel/debug ||
168
+ errexit "Failed to mount /sys/kernel/debug"
169
+ TRACING_DIR="/sys/kernel/debug/tracing"
170
+ UMOUNT_DIR=${TRACING_DIR}
171
+ else
172
+ err_ret=$err_skip
173
+ errexit "debugfs and tracefs are not configured in this kernel"
174
+ fi
175
+ else
176
+ TRACING_DIR="$DEBUGFS_DIR/tracing"
177
+ fi
178
+fi
179
+if [ ! -d "$TRACING_DIR" ]; then
180
+ err_ret=$err_skip
181
+ errexit "ftrace is not configured in this kernel"
110182 fi
111183
112184 TOP_DIR=`absdir $0`
....@@ -117,6 +189,8 @@
117189 DEBUG=0
118190 VERBOSE=0
119191 UNSUPPORTED_RESULT=0
192
+UNRESOLVED_RESULT=0
193
+STOP_FAILURE=0
120194 # Parse command-line options
121195 parse_opts $*
122196
....@@ -137,11 +211,38 @@
137211 date > $LOG_FILE
138212 fi
139213
214
+# Define text colors
215
+# Check available colors on the terminal, if any
216
+ncolors=`tput colors 2>/dev/null || echo 0`
217
+color_reset=
218
+color_red=
219
+color_green=
220
+color_blue=
221
+# If stdout exists and number of colors is eight or more, use them
222
+if [ -t 1 -a "$ncolors" -ge 8 ]; then
223
+ color_reset="\033[0m"
224
+ color_red="\033[31m"
225
+ color_green="\033[32m"
226
+ color_blue="\033[34m"
227
+fi
228
+
229
+strip_esc() {
230
+ # busybox sed implementation doesn't accept "\x1B", so use [:cntrl:] instead.
231
+ sed -E "s/[[:cntrl:]]\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
232
+}
233
+
140234 prlog() { # messages
141
- [ -z "$LOG_FILE" ] && echo "$@" || echo "$@" | tee -a $LOG_FILE
235
+ newline="\n"
236
+ if [ "$1" = "-n" ] ; then
237
+ newline=
238
+ shift
239
+ fi
240
+ printf "$*$newline"
241
+ [ "$LOG_FILE" ] && printf "$*$newline" | strip_esc >> $LOG_FILE
142242 }
143243 catlog() { #file
144
- [ -z "$LOG_FILE" ] && cat $1 || cat $1 | tee -a $LOG_FILE
244
+ cat $1
245
+ [ "$LOG_FILE" ] && cat $1 | strip_esc >> $LOG_FILE
145246 }
146247 prlog "=== Ftrace unit tests ==="
147248
....@@ -167,10 +268,17 @@
167268
168269 INSTANCE=
169270 CASENO=0
271
+
170272 testcase() { # testfile
171273 CASENO=$((CASENO+1))
172
- desc=`grep "^#[ \t]*description:" $1 | cut -f2 -d:`
274
+ desc=`grep "^#[ \t]*description:" $1 | cut -f2- -d:`
173275 prlog -n "[$CASENO]$INSTANCE$desc"
276
+}
277
+
278
+checkreq() { # testfile
279
+ requires=`grep "^#[ \t]*requires:" $1 | cut -f2- -d:`
280
+ # Use eval to pass quoted-patterns correctly.
281
+ eval check_requires "$requires"
174282 }
175283
176284 test_on_instance() { # testfile
....@@ -180,37 +288,37 @@
180288 eval_result() { # sigval
181289 case $1 in
182290 $PASS)
183
- prlog " [PASS]"
291
+ prlog " [${color_green}PASS${color_reset}]"
184292 PASSED_CASES="$PASSED_CASES $CASENO"
185293 return 0
186294 ;;
187295 $FAIL)
188
- prlog " [FAIL]"
296
+ prlog " [${color_red}FAIL${color_reset}]"
189297 FAILED_CASES="$FAILED_CASES $CASENO"
190298 return 1 # this is a bug.
191299 ;;
192300 $UNRESOLVED)
193
- prlog " [UNRESOLVED]"
301
+ prlog " [${color_blue}UNRESOLVED${color_reset}]"
194302 UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO"
195
- return 1 # this is a kind of bug.. something happened.
303
+ return $UNRESOLVED_RESULT # depends on use case
196304 ;;
197305 $UNTESTED)
198
- prlog " [UNTESTED]"
306
+ prlog " [${color_blue}UNTESTED${color_reset}]"
199307 UNTESTED_CASES="$UNTESTED_CASES $CASENO"
200308 return 0
201309 ;;
202310 $UNSUPPORTED)
203
- prlog " [UNSUPPORTED]"
311
+ prlog " [${color_blue}UNSUPPORTED${color_reset}]"
204312 UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO"
205313 return $UNSUPPORTED_RESULT # depends on use case
206314 ;;
207315 $XFAIL)
208
- prlog " [XFAIL]"
316
+ prlog " [${color_green}XFAIL${color_reset}]"
209317 XFAILED_CASES="$XFAILED_CASES $CASENO"
210318 return 0
211319 ;;
212320 *)
213
- prlog " [UNDEFINED]"
321
+ prlog " [${color_blue}UNDEFINED${color_reset}]"
214322 UNDEFINED_CASES="$UNDEFINED_CASES $CASENO"
215323 return 1 # this must be a test bug
216324 ;;
....@@ -262,23 +370,27 @@
262370
263371 __run_test() { # testfile
264372 # setup PID and PPID, $$ is not updated.
265
- (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x; initialize_ftrace; . $1)
373
+ (cd $TRACING_DIR; read PID _ < /proc/self/stat; set -e; set -x;
374
+ checkreq $1; initialize_ftrace; . $1)
266375 [ $? -ne 0 ] && kill -s $SIG_FAIL $SIG_PID
267376 }
268377
269378 # Run one test case
270379 run_test() { # testfile
271380 local testname=`basename $1`
381
+ testcase $1
272382 if [ ! -z "$LOG_FILE" ] ; then
273
- local testlog=`mktemp $LOG_DIR/${testname}-log.XXXXXX`
383
+ local testlog=`mktemp $LOG_DIR/${CASENO}-${testname}-log.XXXXXX`
274384 else
275385 local testlog=/proc/self/fd/1
276386 fi
277387 export TMPDIR=`mktemp -d /tmp/ftracetest-dir.XXXXXX`
278
- testcase $1
388
+ export FTRACETEST_ROOT=$TOP_DIR
279389 echo "execute$INSTANCE: "$1 > $testlog
280390 SIG_RESULT=0
281
- if [ -z "$LOG_FILE" ]; then
391
+ if [ $VERBOSE -eq -1 ]; then
392
+ __run_test $1
393
+ elif [ -z "$LOG_FILE" ]; then
282394 __run_test $1 2>&1
283395 elif [ $VERBOSE -ge 3 ]; then
284396 __run_test $1 | tee -a $testlog 2>&1
....@@ -304,6 +416,10 @@
304416 # Main loop
305417 for t in $TEST_CASES; do
306418 run_test $t
419
+ if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
420
+ echo "A failure detected. Stop test."
421
+ exit 1
422
+ fi
307423 done
308424
309425 # Test on instance loop
....@@ -315,7 +431,12 @@
315431 run_test $t
316432 rmdir $TRACING_DIR
317433 TRACING_DIR=$SAVED_TRACING_DIR
434
+ if [ $STOP_FAILURE -ne 0 -a $TOTAL_RESULT -ne 0 ]; then
435
+ echo "A failure detected. Stop test."
436
+ exit 1
437
+ fi
318438 done
439
+(cd $TRACING_DIR; initialize_ftrace) # for cleanup
319440
320441 prlog ""
321442 prlog "# of passed: " `echo $PASSED_CASES | wc -w`
....@@ -326,5 +447,7 @@
326447 prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w`
327448 prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w`
328449
450
+cleanup
451
+
329452 # if no error, return 0
330453 exit $TOTAL_RESULT