summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/cpufreq/governor.sh
blob: fe37df79c0871b3906b9ac3be4a52291ec10317a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# Test governors

# protect against multiple inclusion
if [ $FILE_GOVERNOR ]; then
	return 0
else
	FILE_GOVERNOR=DONE
fi

source cpu.sh
source cpufreq.sh

CUR_GOV=
CUR_FREQ=

# Find governor's directory path
# $1: policy, $2: governor
find_gov_directory()
{
	if [ -d $CPUFREQROOT/$2 ]; then
		printf "$CPUFREQROOT/$2\n"
	elif [ -d $CPUFREQROOT/$1/$2 ]; then
		printf "$CPUFREQROOT/$1/$2\n"
	else
		printf "INVALID\n"
	fi
}

# $1: policy
find_current_governor()
{
	cat $CPUFREQROOT/$1/scaling_governor
}

# $1: policy
backup_governor()
{
	CUR_GOV=$(find_current_governor $1)

	printf "Governor backup done for $1: $CUR_GOV\n"

	if [ $CUR_GOV == "userspace" ]; then
		CUR_FREQ=$(find_current_freq $1)
		printf "Governor frequency backup done for $1: $CUR_FREQ\n"
	fi

	printf "\n"
}

# $1: policy
restore_governor()
{
	__switch_governor $1 $CUR_GOV

	printf "Governor restored for $1 to $CUR_GOV\n"

	if [ $CUR_GOV == "userspace" ]; then
		set_cpu_frequency $1 $CUR_FREQ
		printf "Governor frequency restored for $1: $CUR_FREQ\n"
	fi

	printf "\n"
}

# param:
# $1: policy, $2: governor
__switch_governor()
{
	echo $2 > $CPUFREQROOT/$1/scaling_governor
}

# param:
# $1: cpu, $2: governor
__switch_governor_for_cpu()
{
	echo $2 > $CPUROOT/$1/cpufreq/scaling_governor
}

# SWITCH GOVERNORS

# $1: cpu, $2: governor
switch_governor()
{
	local filepath=$CPUFREQROOT/$1/scaling_available_governors

	# check if governor is available
	local found=$(cat $filepath | grep $2 | wc -l)
	if [ $found = 0 ]; then
		echo 1;
		return
	fi

	__switch_governor $1 $2
	echo 0;
}

# $1: policy, $2: governor
switch_show_governor()
{
	cur_gov=find_current_governor
	if [ $cur_gov == "userspace" ]; then
		cur_freq=find_current_freq
	fi

	# switch governor
	__switch_governor $1 $2

	printf "\nSwitched governor for $1 to $2\n\n"

	if [ $2 == "userspace" -o $2 == "powersave" -o $2 == "performance" ]; then
		printf "No files to read for $2 governor\n\n"
		return
	fi

	# show governor files
	local govpath=$(find_gov_directory $1 $2)
	read_cpufreq_files_in_dir $govpath
}

# $1: function to be called, $2: policy
call_for_each_governor()
{
	local filepath=$CPUFREQROOT/$2/scaling_available_governors

	# Exit if cpu isn't managed by cpufreq core
	if [ ! -f $filepath ]; then
		return;
	fi

	backup_governor $2

	local governors=$(cat $filepath)
	printf "Available governors for $2: $governors\n"

	for governor in $governors; do
		$1 $2 $governor
	done

	restore_governor $2
}

# $1: loop count
shuffle_governors_for_all_cpus()
{
	printf "** Test: Running ${FUNCNAME[0]} for $1 loops **\n\n"

	for i in `seq 1 $1`; do
		for_each_policy call_for_each_governor switch_show_governor
	done
	printf "%s\n\n" "------------------------------------------------"
}