blob: b05ded0bea11a3e9b83cb2eee2af0125ccdb27f1 [file] [log] [blame]
Lisa Nguyenadf9df92015-01-25 18:16:45 -08001#!/bin/sh
Daniel Lezcanod84ec062011-07-26 14:38:59 +02002#
Daniel Lezcano35fb1722011-10-03 13:56:11 +02003# PM-QA validation test suite for the power management on Linux
Daniel Lezcanod84ec062011-07-26 14:38:59 +02004#
5# Copyright (C) 2011, Linaro Limited.
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License
9# as published by the Free Software Foundation; either version 2
10# of the License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21# Contributors:
22# Daniel Lezcano <daniel.lezcano@linaro.org> (IBM Corporation)
23# - initial API and implementation
24#
25
Lisa Nguyenadf9df92015-01-25 18:16:45 -080026. ../Switches.sh
Hongbo Zhang974802b2013-02-04 19:22:58 +080027
Daniel Lezcanod84ec062011-07-26 14:38:59 +020028CPU_PATH="/sys/devices/system/cpu"
29TEST_NAME=$(basename ${0%.sh})
Daniel Lezcano1efddc12011-08-09 23:45:30 +020030PREFIX=$TEST_NAME
31INC=0
Lisa Nguyen523e8d92014-09-04 17:26:03 -070032cpus=$(ls $CPU_PATH | grep "cpu[0-9].*")
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053033pass_count=0
34fail_count=0
Lisa Nguyene2877d52014-08-04 12:30:43 -070035skip_count=0
36test_script_status="pass"
Lisa Nguyenf62f3542015-01-26 17:33:03 -080037NANOSLEEP="../utils/nanosleep"
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053038
39test_status_show() {
Lisa Nguyene2877d52014-08-04 12:30:43 -070040 if [ $fail_count -ne 0 ]; then
41 test_script_status="fail"
42 else
43 if [ $skip_count -ne 0 ]; then
44 if [ $pass_count -ne 0 ]; then
45 test_script_status="pass"
46 else
47 test_script_status="skip"
48 fi
49 fi
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053050 fi
Lisa Nguyene2877d52014-08-04 12:30:43 -070051
52 echo " "
Lisa Nguyend1e0be92015-01-27 13:57:02 -080053 if [ "$test_script_status" = "fail" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070054 echo "$TEST_NAME: fail"
Lisa Nguyend1e0be92015-01-27 13:57:02 -080055 elif [ "$test_script_status" = "skip" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070056 echo "$TEST_NAME: skip"
57 else
58 echo "$TEST_NAME: pass"
59 fi
60 echo " "
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053061}
Daniel Lezcanod84ec062011-07-26 14:38:59 +020062
Lisa Nguyend713d632014-08-20 17:26:04 -070063skip_tests() {
64 dir=$1
65
66 test_script_list=$(ls ../$1/*.sh | grep -v 'sanity.sh$' | grep -v '00.sh$')
67
68 for test_script in $test_script_list; do
69 test_case=$(basename $test_script .sh)
70 echo "$test_case: skip"
71 done
72}
73
Daniel Lezcanod84ec062011-07-26 14:38:59 +020074log_begin() {
Daniel Lezcano994f6f12011-08-16 13:28:33 +020075 printf "%-76s" "$TEST_NAME.$INC$CPU: $@... "
Daniel Lezcano1efddc12011-08-09 23:45:30 +020076 INC=$(($INC+1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +020077}
78
79log_end() {
80 printf "$*\n"
Lisa Nguyene2877d52014-08-04 12:30:43 -070081
Lisa Nguyend1e0be92015-01-27 13:57:02 -080082 if [ "$*" = "Err" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070083 fail_count=$((fail_count + 1))
Lisa Nguyend1e0be92015-01-27 13:57:02 -080084 elif [ "$*" = "skip" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070085 skip_count=$((skip_count + 1))
86 else
87 pass_count=$((pass_count + 1))
88 fi
Daniel Lezcanod84ec062011-07-26 14:38:59 +020089}
90
91log_skip() {
92 log_begin "$@"
Daniel Lezcanoff4aa112011-08-16 15:48:38 +020093 log_end "skip"
Daniel Lezcanod84ec062011-07-26 14:38:59 +020094}
95
96for_each_cpu() {
97
Lisa Nguyen2d49a682015-01-26 20:27:48 -080098 cpu_func=$1
Daniel Lezcanod84ec062011-07-26 14:38:59 +020099 shift 1
100
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200101 for cpu in $cpus; do
Daniel Lezcano1efddc12011-08-09 23:45:30 +0200102 INC=0
103 CPU=/$cpu
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800104 $cpu_func $cpu $@
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200105 done
106
107 return 0
108}
109
110for_each_governor() {
111
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800112 gov_cpu=$1
113 gov_func=$2
114 cpufreq_dirpath=$CPU_PATH/$gov_cpu/cpufreq
115 governors=$(cat $cpufreq_dirpath/scaling_available_governors)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200116 shift 2
117
118 for governor in $governors; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800119 $gov_func $gov_cpu $governor $@
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200120 done
121
122 return 0
123}
124
125for_each_frequency() {
126
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800127 freq_cpu=$1
128 freq_func=$2
129 cpufreq_dirpath=$CPU_PATH/$freq_cpu/cpufreq
130 frequencies=$(cat $cpufreq_dirpath/scaling_available_frequencies)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200131 shift 2
132
133 for frequency in $frequencies; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800134 $freq_func $freq_cpu $frequency $@
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200135 done
136
137 return 0
138}
139
140set_governor() {
141
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800142 gov_cpu=$1
143 scaling_gov_dirpath=$CPU_PATH/$gov_cpu/cpufreq/scaling_governor
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800144 newgov=$2
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200145
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800146 echo $newgov > $scaling_gov_dirpath
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200147}
148
149get_governor() {
150
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800151 gov_cpu=$1
152 scaling_gov_dirpath=$CPU_PATH/$gov_cpu/cpufreq/scaling_governor
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200153
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800154 cat $scaling_gov_dirpath
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200155}
156
157wait_latency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800158 wait_latency_cpu=$1
159 cpufreq_dirpath=$CPU_PATH/$wait_latency_cpu/cpufreq
160 gov=$(cat $cpufreq_dirpath/scaling_governor)
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530161
162 # consider per-policy governor case
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800163 if [ -e $CPU_PATH/$wait_latency_cpu/cpufreq/$gov ]; then
164 sampling_rate=$(cat $CPU_PATH/$wait_latency_cpu/cpufreq/$gov/sampling_rate)
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530165 else
166 sampling_rate=$(cat $CPU_PATH/cpufreq/$gov/sampling_rate)
167 fi
168 sampling_rate=$((sampling_rate * 1000)) # unit nsec
Daniel Lezcano93382952011-09-19 11:41:39 +0200169
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800170 latency=$(cat $cpufreq_dirpath/cpuinfo_transition_latency)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530171 if [ $? -ne 0 ]; then
Daniel Lezcano0b90d412011-09-20 08:31:04 +0200172 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200173 fi
174
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800175 nrfreq=$(cat $cpufreq_dirpath/scaling_available_frequencies | wc -w)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530176 if [ $? -ne 0 ]; then
Daniel Lezcano0b90d412011-09-20 08:31:04 +0200177 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200178 fi
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200179
180 nrfreq=$((nrfreq + 1))
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530181
182 sleep_time=$(($latency + $sampling_rate))
183
Lisa Nguyenf62f3542015-01-26 17:33:03 -0800184 $NANOSLEEP $(($nrfreq * $sleep_time))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200185}
186
187frequnit() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800188 freq=$1
189 ghz=$(echo "scale=1;($freq / 1000000)" | bc -l)
190 mhz=$(echo "scale=1;($freq / 1000)" | bc -l)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200191
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800192 ghz_value=$(echo "($ghz > 1.0)" | bc -l)
193 if [ "$ghz_value" = "1" ]; then
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200194 echo $ghz GHz
195 return 0
196 fi
197
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800198 mhz_value=$(echo "($mhz > 1.0)" | bc -l)
199 if [ "$mhz_value" = "1" ];then
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200200 echo $mhz MHz
201 return 0
202 fi
203
204 echo $freq KHz
205}
206
207set_frequency() {
208
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800209 freq_cpu=$1
210 cpufreq_dirpath=$CPU_PATH/$freq_cpu/cpufreq
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800211 newfreq=$2
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800212 setfreqpath=$cpufreq_dirpath/scaling_setspeed
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200213
214 echo $newfreq > $setfreqpath
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800215 wait_latency $freq_cpu
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200216}
217
218get_frequency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800219 freq_cpu=$1
220 scaling_cur_freq=$CPU_PATH/$freq_cpu/cpufreq/scaling_cur_freq
221 cat $scaling_cur_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200222}
223
224get_max_frequency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800225 freq_cpu=$1
226 scaling_max_freq=$CPU_PATH/$freq_cpu/cpufreq/scaling_max_freq
227 cat $scaling_max_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200228}
229
230get_min_frequency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800231 freq_cpu=$1
232 scaling_min_freq=$CPU_PATH/$freq_cpu/cpufreq/scaling_min_freq
233 cat $scaling_min_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200234}
235
Daniel Lezcano805e3352011-10-03 11:07:36 +0200236set_online() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800237 current_cpu=$1
238 current_cpu_path=$CPU_PATH/$current_cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200239
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800240 if [ "$current_cpu" = "cpu0" ]; then
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200241 return 0
242 fi
243
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800244 echo 1 > $current_cpu_path/online
Daniel Lezcano805e3352011-10-03 11:07:36 +0200245}
246
247set_offline() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800248 current_cpu=$1
249 current_cpu_path=$CPU_PATH/$current_cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200250
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800251 if [ "$current_cpu" = "cpu0" ]; then
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200252 return 0
253 fi
254
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800255 echo 0 > $current_cpu_path/online
Daniel Lezcano805e3352011-10-03 11:07:36 +0200256}
257
258get_online() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800259 current_cpu=$1
260 current_cpu_path=$CPU_PATH/$current_cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200261
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800262 cat $current_cpu_path/online
Daniel Lezcano805e3352011-10-03 11:07:36 +0200263}
264
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200265check() {
266
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800267 check_descr=$1
268 check_func=$2
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200269 shift 2;
270
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800271 log_begin "checking $check_descr"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200272
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800273 $check_func $@
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530274 if [ $? -ne 0 ]; then
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530275 log_end "Err"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200276 return 1
277 fi
278
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530279 log_end "Ok"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200280
281 return 0
282}
283
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200284check_file() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800285 file=$1
286 dir=$2
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200287
Sanjay Singh Rawat8b6ad7c2014-07-08 16:35:36 +0200288 check "'$file' exists in '$dir'" "test -f" $dir/$file
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200289}
290
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200291check_cpufreq_files() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800292 cpu_id=$1
293 cpufreq_files_dir=$CPU_PATH/$cpu_id/cpufreq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200294 shift 1
295
296 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800297 check_file $i $cpufreq_files_dir || return 1
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200298 done
299
300 return 0
301}
302
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200303check_sched_mc_files() {
304
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200305 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800306 check_file $i $CPU_PATH || return 1
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200307 done
308
309 return 0
310}
311
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200312check_topology_files() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800313 cpu=$1
314 topology_files_dir=$CPU_PATH/$cpu/topology
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200315 shift 1
316
317 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800318 check_file $i $topology_files_dir || return 1
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200319 done
320
321 return 0
322}
323
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200324check_cpuhotplug_files() {
325
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800326 cpuhotplug_files_dir=$CPU_PATH/$1
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200327 shift 1
328
329 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800330 if [ `echo $cpuhotplug_files_dir | grep -c "cpu0"` -eq 1 ]; then
Lisa Nguyene2323182014-07-04 10:24:30 +0530331 if [ $hotplug_allow_cpu0 -eq 0 ]; then
332 continue
333 fi
334 fi
335
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800336 check_file $i $cpuhotplug_files_dir || return 1
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200337 done
338
339 return 0
340}
341
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200342save_governors() {
343
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800344 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200345
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700346 for cpu in $cpus; do
347 governors_backup[$index]=$(cat $CPU_PATH/$cpu/cpufreq/scaling_governor)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200348 index=$((index + 1))
349 done
350}
351
352restore_governors() {
353
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800354 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200355
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700356 for cpu in $cpus; do
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200357 oldgov=${governors_backup[$index]}
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700358 echo $oldgov > $CPU_PATH/$cpu/cpufreq/scaling_governor
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200359 index=$((index + 1))
360 done
361}
362
363save_frequencies() {
364
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800365 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200366
367 for cpu in $cpus; do
368 frequencies_backup[$index]=$(cat $CPU_PATH/$cpu/cpufreq/scaling_cur_freq)
369 index=$((index + 1))
370 done
371}
372
373restore_frequencies() {
374
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800375 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200376
377 for cpu in $cpus; do
378 oldfreq=${frequencies_backup[$index]}
379 echo $oldfreq > $CPU_PATH/$cpu/cpufreq/scaling_setspeed
380 index=$((index + 1))
381 done
382}
383
384sigtrap() {
385 exit 255
Daniel Lezcano18e22b52011-08-06 02:52:02 +0200386}
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530387
388# currently we support ubuntu and android
389get_os() {
390 lsb_release -a 2>&1 | grep -i ubuntu > /dev/null
391 if [ $? -eq 0 ]; then
392 # for ubuntu
393 return 1
394 else
395 # for android (if needed can look for build.prop)
396 return 2
397 fi
398}
399
400is_root() {
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530401 get_os
402 if [ $? -eq 1 ]; then
403 # for ubuntu
404 ret=$(id -u)
405 else
406 # for android
407 ret=$(id | sed -n 's/uid=//p' | sed -n 's/(root) [a-z]*=[0-9]*(log)//p')
408 fi
409 return $ret
410}
Lisa Nguyene2323182014-07-04 10:24:30 +0530411
412is_cpu0_hotplug_allowed() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800413 status=$1
Lisa Nguyene2323182014-07-04 10:24:30 +0530414
415 if [ $status -eq 1 ]; then
416 return 0
417 else
418 return 1
419 fi
420}