xref: /linux/tools/testing/selftests/cpufreq/main.sh (revision 783711f0d2458ca1bd716000e767c478cef27144)
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
10FUNC=basic	# do basic tests by default
11OUTFILE=cpufreq_selftest
12SYSFS=
13CPUROOT=
14CPUFREQROOT=
15
16helpme()
17{
18	printf "Usage: $0 [-h] [-todg args]
19	[-h <help>]
20	[-o <output-file-for-dump>]
21	[-t <basic: Basic cpufreq testing
22	     suspend: suspend/resume,
23	     hibernate: hibernate/resume,
24	     modtest: test driver or governor modules. Only to be used with -d or -g options,
25	     sptest1: Simple governor switch to produce lockdep.
26	     sptest2: Concurrent governor switch to produce lockdep.
27	     sptest3: Governor races, shuffle between governors quickly.
28	     sptest4: CPU hotplugs with updates to cpufreq files.>]
29	[-d <driver's module name: only with \"-t modtest>\"]
30	[-g <governor's module name: only with \"-t modtest>\"]
31	\n"
32	exit 2
33}
34
35prerequisite()
36{
37	msg="skip all tests:"
38
39	if [ $UID != 0 ]; then
40		echo $msg must be run as root >&2
41		exit 2
42	fi
43
44	taskset -p 01 $$
45
46	SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
47
48	if [ ! -d "$SYSFS" ]; then
49		echo $msg sysfs is not mounted >&2
50		exit 2
51	fi
52
53	CPUROOT=$SYSFS/devices/system/cpu
54	CPUFREQROOT="$CPUROOT/cpufreq"
55
56	if ! ls $CPUROOT/cpu* > /dev/null 2>&1; then
57		echo $msg cpus not available in sysfs >&2
58		exit 2
59	fi
60
61	if ! ls $CPUROOT/cpufreq > /dev/null 2>&1; then
62		echo $msg cpufreq directory not available in sysfs >&2
63		exit 2
64	fi
65}
66
67parse_arguments()
68{
69	while getopts ht:o:d:g: arg
70	do
71		case $arg in
72			h) # --help
73				helpme
74				;;
75
76			t) # --func_type (Function to perform: basic, suspend, hibernate, modtest, sptest1/2/3/4 (default: basic))
77				FUNC=$OPTARG
78				;;
79
80			o) # --output-file (Output file to store dumps)
81				OUTFILE=$OPTARG
82				;;
83
84			d) # --driver-mod-name (Name of the driver module)
85				DRIVER_MOD=$OPTARG
86				;;
87
88			g) # --governor-mod-name (Name of the governor module)
89				GOVERNOR_MOD=$OPTARG
90				;;
91
92			\?)
93				helpme
94				;;
95		esac
96	done
97}
98
99do_test()
100{
101	# Check if CPUs are managed by cpufreq or not
102	count=$(count_cpufreq_managed_cpus)
103
104	if [ $count = 0 -a $FUNC != "modtest" ]; then
105		echo "No cpu is managed by cpufreq core, exiting"
106		exit 2;
107	fi
108
109	case "$FUNC" in
110		"basic")
111		cpufreq_basic_tests
112		;;
113
114		"suspend")
115		do_suspend "suspend" 1
116		;;
117
118		"hibernate")
119		do_suspend "hibernate" 1
120		;;
121
122		"modtest")
123		# Do we have modules in place?
124		if [ -z $DRIVER_MOD ] && [ -z $GOVERNOR_MOD ]; then
125			echo "No driver or governor module passed with -d or -g"
126			exit 2;
127		fi
128
129		if [ $DRIVER_MOD ]; then
130			if [ $GOVERNOR_MOD ]; then
131				module_test $DRIVER_MOD $GOVERNOR_MOD
132			else
133				module_driver_test $DRIVER_MOD
134			fi
135		else
136			if [ $count = 0 ]; then
137				echo "No cpu is managed by cpufreq core, exiting"
138				exit 2;
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		echo "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
186# Parse arguments
187parse_arguments $@
188
189# Make sure all requirements are met
190prerequisite
191
192# Run requested functions
193clear_dumps $OUTFILE
194do_test >> $OUTFILE.txt
195dmesg_dumps $OUTFILE
196