selftests: amd-pstate: Split basic.sh into run.sh and basic.sh.
authorMeng Li <li.meng@amd.com>
Mon, 31 Oct 2022 08:49:21 +0000 (16:49 +0800)
committerShuah Khan <skhan@linuxfoundation.org>
Tue, 1 Nov 2022 09:20:58 +0000 (03:20 -0600)
Split basic.sh into run.sh and basic.sh.
The modification makes basic.sh more pure, just for test basic kernel
functions. The file of run.sh mainly contains functions such as test
entry, parameter check, prerequisite and log clearing etc.
Then you can specify test case in kselftest/amd-pstate, for example:
sudo ./run.sh -c basic. The detail please run the below script.
./run.sh --help

Signed-off-by: Meng Li <li.meng@amd.com>
Acked-by: Huang Rui <ray.huang@amd.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/testing/selftests/amd-pstate/Makefile
tools/testing/selftests/amd-pstate/basic.sh
tools/testing/selftests/amd-pstate/run.sh [new file with mode: 0755]

index 1d6f962a8f9e2d91f54a7cb1822f7f0f80e1b393..6f4c7b01e3bb2aeffc7ed036339274afad1e7314 100644 (file)
@@ -4,6 +4,7 @@
 # No binaries, but make sure arg-less "make" doesn't trigger "run_tests"
 all:
 
-TEST_PROGS := basic.sh
+TEST_PROGS := run.sh
+TEST_FILES := basic.sh
 
 include ../lib.mk
index f8e82d91ffcf80fed1f638cc237f20debd8545ba..e4c43193e4a3296877381e0459682396bf390947 100755 (executable)
 # (3) We can introduce more functional or performance tests to align
 # the result together, it will benefit power and performance scale optimization.
 
-# Kselftest framework requirement - SKIP code is 4.
-ksft_skip=4
-
-# amd-pstate-ut only run on x86/x86_64 AMD systems.
-ARCH=$(uname -m 2>/dev/null | sed -e 's/i.86/x86/' -e 's/x86_64/x86/')
-VENDOR=$(cat /proc/cpuinfo | grep -m 1 'vendor_id' | awk '{print $NF}')
-
-if ! echo "$ARCH" | grep -q x86; then
-       echo "$0 # Skipped: Test can only run on x86 architectures."
-       exit $ksft_skip
-fi
-
-if ! echo "$VENDOR" | grep -iq amd; then
-       echo "$0 # Skipped: Test can only run on AMD CPU."
-       echo "$0 # Current cpu vendor is $VENDOR."
-       exit $ksft_skip
-fi
-
-scaling_driver=$(cat /sys/devices/system/cpu/cpufreq/policy0/scaling_driver)
-if [ "$scaling_driver" != "amd-pstate" ]; then
-       echo "$0 # Skipped: Test can only run on amd-pstate driver."
-       echo "$0 # Please set X86_AMD_PSTATE enabled."
-       echo "$0 # Current cpufreq scaling drvier is $scaling_driver."
-       exit $ksft_skip
-fi
-
-msg="Skip all tests:"
-if [ ! -w /dev ]; then
-    echo $msg please run this as root >&2
-    exit $ksft_skip
-fi
-
-if ! /sbin/modprobe -q -n amd-pstate-ut; then
-       echo "amd-pstate-ut: module amd-pstate-ut is not found [SKIP]"
-       exit $ksft_skip
-fi
-if /sbin/modprobe -q amd-pstate-ut; then
-       /sbin/modprobe -q -r amd-pstate-ut
-       echo "amd-pstate-ut: ok"
+# protect against multiple inclusion
+if [ $FILE_BASIC ]; then
+       return 0
 else
-       echo "amd-pstate-ut: [FAIL]"
-       exit 1
+       FILE_BASIC=DONE
 fi
+
+amd_pstate_basic()
+{
+       printf "\n---------------------------------------------\n"
+       printf "*** Running AMD P-state ut                ***"
+       printf "\n---------------------------------------------\n"
+
+       if ! /sbin/modprobe -q -n amd-pstate-ut; then
+               echo "amd-pstate-ut: module amd-pstate-ut is not found [SKIP]"
+               exit $ksft_skip
+       fi
+       if /sbin/modprobe -q amd-pstate-ut; then
+               /sbin/modprobe -q -r amd-pstate-ut
+               echo "amd-pstate-basic: ok"
+       else
+               echo "amd-pstate-basic: [FAIL]"
+               exit 1
+       fi
+}
diff --git a/tools/testing/selftests/amd-pstate/run.sh b/tools/testing/selftests/amd-pstate/run.sh
new file mode 100755 (executable)
index 0000000..35c0a0b
--- /dev/null
@@ -0,0 +1,142 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+# protect against multiple inclusion
+if [ $FILE_MAIN ]; then
+       return 0
+else
+       FILE_MAIN=DONE
+fi
+
+source basic.sh
+
+# amd-pstate-ut only run on x86/x86_64 AMD systems.
+ARCH=$(uname -m 2>/dev/null | sed -e 's/i.86/x86/' -e 's/x86_64/x86/')
+VENDOR=$(cat /proc/cpuinfo | grep -m 1 'vendor_id' | awk '{print $NF}')
+
+FUNC=all
+OUTFILE=selftest
+
+# Kselftest framework requirement - SKIP code is 4.
+ksft_skip=4
+
+# All amd-pstate tests
+amd_pstate_all()
+{
+       printf "\n=============================================\n"
+       printf "***** Running AMD P-state Sanity Tests  *****\n"
+       printf "=============================================\n\n"
+
+       # unit test for amd-pstate kernel driver
+       amd_pstate_basic
+}
+
+help()
+{
+       printf "Usage: $0 [OPTION...]
+       [-h <help>]
+       [-o <output-file-for-dump>]
+       [-c <all: All testing,
+            basic: Basic testing.>]
+       \n"
+       exit 2
+}
+
+parse_arguments()
+{
+       while getopts ho:c: arg
+       do
+               case $arg in
+                       h) # --help
+                               help
+                               ;;
+
+                       c) # --func_type (Function to perform: basic (default: all))
+                               FUNC=$OPTARG
+                               ;;
+
+                       o) # --output-file (Output file to store dumps)
+                               OUTFILE=$OPTARG
+                               ;;
+
+                       *)
+                               help
+                               ;;
+               esac
+       done
+}
+
+prerequisite()
+{
+       if ! echo "$ARCH" | grep -q x86; then
+               echo "$0 # Skipped: Test can only run on x86 architectures."
+               exit $ksft_skip
+       fi
+
+       if ! echo "$VENDOR" | grep -iq amd; then
+               echo "$0 # Skipped: Test can only run on AMD CPU."
+               echo "$0 # Current cpu vendor is $VENDOR."
+               exit $ksft_skip
+       fi
+
+       scaling_driver=$(cat /sys/devices/system/cpu/cpufreq/policy0/scaling_driver)
+       if [ "$scaling_driver" != "amd-pstate" ]; then
+               echo "$0 # Skipped: Test can only run on amd-pstate driver."
+               echo "$0 # Please set X86_AMD_PSTATE enabled."
+               echo "$0 # Current cpufreq scaling drvier is $scaling_driver."
+               exit $ksft_skip
+       fi
+
+       msg="Skip all tests:"
+       if [ ! -w /dev ]; then
+               echo $msg please run this as root >&2
+               exit $ksft_skip
+       fi
+}
+
+do_test()
+{
+       case "$FUNC" in
+               "all")
+                       amd_pstate_all
+                       ;;
+
+               "basic")
+                       amd_pstate_basic
+                       ;;
+
+               *)
+                       echo "Invalid [-f] function type"
+                       help
+                       ;;
+       esac
+}
+
+# clear dumps
+pre_clear_dumps()
+{
+       case "$FUNC" in
+               "all")
+                       rm -rf $OUTFILE*
+                       ;;
+
+               *)
+                       ;;
+       esac
+}
+
+post_clear_dumps()
+{
+       rm -rf $OUTFILE.log
+}
+
+# Parse arguments
+parse_arguments $@
+
+# Make sure all requirements are met
+prerequisite
+
+# Run requested functions
+pre_clear_dumps
+do_test | tee -a $OUTFILE.log
+post_clear_dumps