Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / cpufreq / governor.sh
blobfe37df79c0871b3906b9ac3be4a52291ec10317a
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 # Test governors
6 # protect against multiple inclusion
7 if [ $FILE_GOVERNOR ]; then
8 return 0
9 else
10 FILE_GOVERNOR=DONE
13 source cpu.sh
14 source cpufreq.sh
16 CUR_GOV=
17 CUR_FREQ=
19 # Find governor's directory path
20 # $1: policy, $2: governor
21 find_gov_directory()
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"
32 # $1: policy
33 find_current_governor()
35 cat $CPUFREQROOT/$1/scaling_governor
38 # $1: policy
39 backup_governor()
41 CUR_GOV=$(find_current_governor $1)
43 printf "Governor backup done for $1: $CUR_GOV\n"
45 if [ $CUR_GOV == "userspace" ]; then
46 CUR_FREQ=$(find_current_freq $1)
47 printf "Governor frequency backup done for $1: $CUR_FREQ\n"
50 printf "\n"
53 # $1: policy
54 restore_governor()
56 __switch_governor $1 $CUR_GOV
58 printf "Governor restored for $1 to $CUR_GOV\n"
60 if [ $CUR_GOV == "userspace" ]; then
61 set_cpu_frequency $1 $CUR_FREQ
62 printf "Governor frequency restored for $1: $CUR_FREQ\n"
65 printf "\n"
68 # param:
69 # $1: policy, $2: governor
70 __switch_governor()
72 echo $2 > $CPUFREQROOT/$1/scaling_governor
75 # param:
76 # $1: cpu, $2: governor
77 __switch_governor_for_cpu()
79 echo $2 > $CPUROOT/$1/cpufreq/scaling_governor
82 # SWITCH GOVERNORS
84 # $1: cpu, $2: governor
85 switch_governor()
87 local filepath=$CPUFREQROOT/$1/scaling_available_governors
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
96 __switch_governor $1 $2
97 echo 0;
100 # $1: policy, $2: governor
101 switch_show_governor()
103 cur_gov=find_current_governor
104 if [ $cur_gov == "userspace" ]; then
105 cur_freq=find_current_freq
108 # switch governor
109 __switch_governor $1 $2
111 printf "\nSwitched governor for $1 to $2\n\n"
113 if [ $2 == "userspace" -o $2 == "powersave" -o $2 == "performance" ]; then
114 printf "No files to read for $2 governor\n\n"
115 return
118 # show governor files
119 local govpath=$(find_gov_directory $1 $2)
120 read_cpufreq_files_in_dir $govpath
123 # $1: function to be called, $2: policy
124 call_for_each_governor()
126 local filepath=$CPUFREQROOT/$2/scaling_available_governors
128 # Exit if cpu isn't managed by cpufreq core
129 if [ ! -f $filepath ]; then
130 return;
133 backup_governor $2
135 local governors=$(cat $filepath)
136 printf "Available governors for $2: $governors\n"
138 for governor in $governors; do
139 $1 $2 $governor
140 done
142 restore_governor $2
145 # $1: loop count
146 shuffle_governors_for_all_cpus()
148 printf "** Test: Running ${FUNCNAME[0]} for $1 loops **\n\n"
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" "------------------------------------------------"