xref: /linux/tools/testing/selftests/cpufreq/governor.sh (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1#!/bin/bash
2#
3# Test governors
4
5# protect against multiple inclusion
6if [ $FILE_GOVERNOR ]; then
7	return 0
8else
9	FILE_GOVERNOR=DONE
10fi
11
12source cpu.sh
13source cpufreq.sh
14
15CUR_GOV=
16CUR_FREQ=
17
18# Find governor's directory path
19# $1: policy, $2: governor
20find_gov_directory()
21{
22	if [ -d $CPUFREQROOT/$2 ]; then
23		printf "$CPUFREQROOT/$2\n"
24	elif [ -d $CPUFREQROOT/$1/$2 ]; then
25		printf "$CPUFREQROOT/$1/$2\n"
26	else
27		printf "INVALID\n"
28	fi
29}
30
31# $1: policy
32find_current_governor()
33{
34	cat $CPUFREQROOT/$1/scaling_governor
35}
36
37# $1: policy
38backup_governor()
39{
40	CUR_GOV=$(find_current_governor $1)
41
42	printf "Governor backup done for $1: $CUR_GOV\n"
43
44	if [ $CUR_GOV == "userspace" ]; then
45		CUR_FREQ=$(find_current_freq $1)
46		printf "Governor frequency backup done for $1: $CUR_FREQ\n"
47	fi
48
49	printf "\n"
50}
51
52# $1: policy
53restore_governor()
54{
55	__switch_governor $1 $CUR_GOV
56
57	printf "Governor restored for $1 to $CUR_GOV\n"
58
59	if [ $CUR_GOV == "userspace" ]; then
60		set_cpu_frequency $1 $CUR_FREQ
61		printf "Governor frequency restored for $1: $CUR_FREQ\n"
62	fi
63
64	printf "\n"
65}
66
67# param:
68# $1: policy, $2: governor
69__switch_governor()
70{
71	echo $2 > $CPUFREQROOT/$1/scaling_governor
72}
73
74# param:
75# $1: cpu, $2: governor
76__switch_governor_for_cpu()
77{
78	echo $2 > $CPUROOT/$1/cpufreq/scaling_governor
79}
80
81# SWITCH GOVERNORS
82
83# $1: cpu, $2: governor
84switch_governor()
85{
86	local filepath=$CPUFREQROOT/$1/scaling_available_governors
87
88	# check if governor is available
89	local found=$(cat $filepath | grep $2 | wc -l)
90	if [ $found = 0 ]; then
91		echo 1;
92		return
93	fi
94
95	__switch_governor $1 $2
96	echo 0;
97}
98
99# $1: policy, $2: governor
100switch_show_governor()
101{
102	cur_gov=find_current_governor
103	if [ $cur_gov == "userspace" ]; then
104		cur_freq=find_current_freq
105	fi
106
107	# switch governor
108	__switch_governor $1 $2
109
110	printf "\nSwitched governor for $1 to $2\n\n"
111
112	if [ $2 == "userspace" -o $2 == "powersave" -o $2 == "performance" ]; then
113		printf "No files to read for $2 governor\n\n"
114		return
115	fi
116
117	# show governor files
118	local govpath=$(find_gov_directory $1 $2)
119	read_cpufreq_files_in_dir $govpath
120}
121
122# $1: function to be called, $2: policy
123call_for_each_governor()
124{
125	local filepath=$CPUFREQROOT/$2/scaling_available_governors
126
127	# Exit if cpu isn't managed by cpufreq core
128	if [ ! -f $filepath ]; then
129		return;
130	fi
131
132	backup_governor $2
133
134	local governors=$(cat $filepath)
135	printf "Available governors for $2: $governors\n"
136
137	for governor in $governors; do
138		$1 $2 $governor
139	done
140
141	restore_governor $2
142}
143
144# $1: loop count
145shuffle_governors_for_all_cpus()
146{
147	printf "** Test: Running ${FUNCNAME[0]} for $1 loops **\n\n"
148
149	for i in `seq 1 $1`; do
150		for_each_policy call_for_each_governor switch_show_governor
151	done
152	printf "%s\n\n" "------------------------------------------------"
153}
154