xref: /linux/tools/testing/selftests/cpufreq/main.sh (revision 594ce0b8a998aa4d05827cd7c0d0dcec9a1e3ae2)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4source cpu.sh
5source cpufreq.sh
6source governor.sh
7source module.sh
8source special-tests.sh
9
10DIR="$(dirname $(readlink -f "$0"))"
11source "${DIR}"/../kselftest/ktap_helpers.sh
12
13FUNC=basic	# do basic tests by default
14OUTFILE=cpufreq_selftest
15SYSFS=
16CPUROOT=
17CPUFREQROOT=
18
19helpme()
20{
21	printf "Usage: $0 [-h] [-todg args]
22	[-h <help>]
23	[-o <output-file-for-dump>]
24	[-t <basic: Basic cpufreq testing
25	     suspend: suspend/resume,
26	     hibernate: hibernate/resume,
27	     modtest: test driver or governor modules. Only to be used with -d or -g options,
28	     sptest1: Simple governor switch to produce lockdep.
29	     sptest2: Concurrent governor switch to produce lockdep.
30	     sptest3: Governor races, shuffle between governors quickly.
31	     sptest4: CPU hotplugs with updates to cpufreq files.>]
32	[-d <driver's module name: only with \"-t modtest>\"]
33	[-g <governor's module name: only with \"-t modtest>\"]
34	\n"
35	exit "${KSFT_FAIL}"
36}
37
38prerequisite()
39{
40	msg="skip all tests:"
41
42	if [ $UID != 0 ]; then
43		ktap_skip_all "$msg must be run as root"
44		exit "${KSFT_SKIP}"
45	fi
46
47	taskset -p 01 $$
48
49	SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
50
51	if [ ! -d "$SYSFS" ]; then
52		ktap_skip_all "$msg sysfs is not mounted"
53		exit "${KSFT_SKIP}"
54	fi
55
56	CPUROOT=$SYSFS/devices/system/cpu
57	CPUFREQROOT="$CPUROOT/cpufreq"
58
59	if ! ls $CPUROOT/cpu* > /dev/null 2>&1; then
60		ktap_skip_all "$msg cpus not available in sysfs"
61		exit "${KSFT_SKIP}"
62	fi
63
64	if ! ls $CPUROOT/cpufreq > /dev/null 2>&1; then
65		ktap_skip_all "$msg cpufreq directory not available in sysfs"
66		exit "${KSFT_SKIP}"
67	fi
68}
69
70parse_arguments()
71{
72	while getopts ht:o:d:g: arg
73	do
74		case $arg in
75			h) # --help
76				helpme
77				;;
78
79			t) # --func_type (Function to perform: basic, suspend, hibernate, modtest, sptest1/2/3/4 (default: basic))
80				FUNC=$OPTARG
81				;;
82
83			o) # --output-file (Output file to store dumps)
84				OUTFILE=$OPTARG
85				;;
86
87			d) # --driver-mod-name (Name of the driver module)
88				DRIVER_MOD=$OPTARG
89				;;
90
91			g) # --governor-mod-name (Name of the governor module)
92				GOVERNOR_MOD=$OPTARG
93				;;
94
95			\?)
96				helpme
97				;;
98		esac
99	done
100}
101
102do_test()
103{
104	# Check if CPUs are managed by cpufreq or not
105	count=$(count_cpufreq_managed_cpus)
106
107	if [ $count = 0 -a $FUNC != "modtest" ]; then
108		ktap_exit_fail_msg "No cpu is managed by cpufreq core, exiting"
109	fi
110
111	case "$FUNC" in
112		"basic")
113		cpufreq_basic_tests
114		;;
115
116		"suspend")
117		do_suspend "suspend" 1
118		;;
119
120		"hibernate")
121		do_suspend "hibernate" 1
122		;;
123
124		"modtest")
125		# Do we have modules in place?
126		if [ -z $DRIVER_MOD ] && [ -z $GOVERNOR_MOD ]; then
127			ktap_exit_fail_msg "No driver or governor module passed with -d or -g"
128		fi
129
130		if [ $DRIVER_MOD ]; then
131			if [ $GOVERNOR_MOD ]; then
132				module_test $DRIVER_MOD $GOVERNOR_MOD
133			else
134				module_driver_test $DRIVER_MOD
135			fi
136		else
137			if [ $count = 0 ]; then
138				ktap_exit_fail_msg "No cpu is managed by cpufreq core, exiting"
139			fi
140
141			module_governor_test $GOVERNOR_MOD
142		fi
143		;;
144
145		"sptest1")
146		simple_lockdep
147		;;
148
149		"sptest2")
150		concurrent_lockdep
151		;;
152
153		"sptest3")
154		governor_race
155		;;
156
157		"sptest4")
158		hotplug_with_updates
159		;;
160
161		*)
162		ktap_print_msg "Invalid [-f] function type"
163		helpme
164		;;
165	esac
166}
167
168# clear dumps
169# $1: file name
170clear_dumps()
171{
172	echo "" > $1.txt
173	echo "" > $1.dmesg_cpufreq.txt
174	echo "" > $1.dmesg_full.txt
175}
176
177# $1: output file name
178dmesg_dumps()
179{
180	dmesg | grep cpufreq >> $1.dmesg_cpufreq.txt
181
182	# We may need the full logs as well
183	dmesg >> $1.dmesg_full.txt
184}
185
186ktap_print_header
187
188# Parse arguments
189parse_arguments $@
190
191ktap_set_plan 1
192
193# Make sure all requirements are met
194prerequisite
195
196# Run requested functions
197clear_dumps $OUTFILE
198do_test | tee -a $OUTFILE.txt
199if [ "${PIPESTATUS[0]}" -ne 0 ]; then
200    exit ${PIPESTATUS[0]};
201fi
202dmesg_dumps $OUTFILE
203
204ktap_test_pass "Completed successfully"
205
206ktap_print_totals
207exit "${KSFT_PASS}"
208