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