blob: 2ffdb361b706fb992cba4be6f1bbfe65b677a6c3 [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"
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053037
38test_status_show() {
Lisa Nguyene2877d52014-08-04 12:30:43 -070039 if [ $fail_count -ne 0 ]; then
40 test_script_status="fail"
41 else
42 if [ $skip_count -ne 0 ]; then
43 if [ $pass_count -ne 0 ]; then
44 test_script_status="pass"
45 else
46 test_script_status="skip"
47 fi
48 fi
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053049 fi
Lisa Nguyene2877d52014-08-04 12:30:43 -070050
51 echo " "
Lisa Nguyend1e0be92015-01-27 13:57:02 -080052 if [ "$test_script_status" = "fail" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070053 echo "$TEST_NAME: fail"
Lisa Nguyend1e0be92015-01-27 13:57:02 -080054 elif [ "$test_script_status" = "skip" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070055 echo "$TEST_NAME: skip"
56 else
57 echo "$TEST_NAME: pass"
58 fi
59 echo " "
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053060}
Daniel Lezcanod84ec062011-07-26 14:38:59 +020061
Lisa Nguyend713d632014-08-20 17:26:04 -070062skip_tests() {
63 dir=$1
64
65 test_script_list=$(ls ../$1/*.sh | grep -v 'sanity.sh$' | grep -v '00.sh$')
66
67 for test_script in $test_script_list; do
68 test_case=$(basename $test_script .sh)
69 echo "$test_case: skip"
70 done
71}
72
Daniel Lezcanod84ec062011-07-26 14:38:59 +020073log_begin() {
Daniel Lezcano994f6f12011-08-16 13:28:33 +020074 printf "%-76s" "$TEST_NAME.$INC$CPU: $@... "
Daniel Lezcano1efddc12011-08-09 23:45:30 +020075 INC=$(($INC+1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +020076}
77
78log_end() {
79 printf "$*\n"
Lisa Nguyene2877d52014-08-04 12:30:43 -070080
Lisa Nguyend1e0be92015-01-27 13:57:02 -080081 if [ "$*" = "Err" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070082 fail_count=$((fail_count + 1))
Lisa Nguyend1e0be92015-01-27 13:57:02 -080083 elif [ "$*" = "skip" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070084 skip_count=$((skip_count + 1))
85 else
86 pass_count=$((pass_count + 1))
87 fi
Daniel Lezcanod84ec062011-07-26 14:38:59 +020088}
89
90log_skip() {
91 log_begin "$@"
Daniel Lezcanoff4aa112011-08-16 15:48:38 +020092 log_end "skip"
Daniel Lezcanod84ec062011-07-26 14:38:59 +020093}
94
95for_each_cpu() {
96
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -080097 func=$1
Daniel Lezcanod84ec062011-07-26 14:38:59 +020098 shift 1
99
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200100 for cpu in $cpus; do
Daniel Lezcano1efddc12011-08-09 23:45:30 +0200101 INC=0
102 CPU=/$cpu
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200103 $func $cpu $@
104 done
105
106 return 0
107}
108
109for_each_governor() {
110
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800111 cpu=$1
112 func=$2
113 dirpath=$CPU_PATH/$cpu/cpufreq
114 governors=$(cat $dirpath/scaling_available_governors)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200115 shift 2
116
117 for governor in $governors; do
118 $func $cpu $governor $@
119 done
120
121 return 0
122}
123
124for_each_frequency() {
125
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800126 cpu=$1
127 func=$2
128 dirpath=$CPU_PATH/$cpu/cpufreq
129 frequencies=$(cat $dirpath/scaling_available_frequencies)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200130 shift 2
131
132 for frequency in $frequencies; do
133 $func $cpu $frequency $@
134 done
135
136 return 0
137}
138
139set_governor() {
140
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800141 cpu=$1
142 dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor
143 newgov=$2
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200144
145 echo $newgov > $dirpath
146}
147
148get_governor() {
149
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800150 cpu=$1
151 dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200152
153 cat $dirpath
154}
155
156wait_latency() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800157 cpu=$1
158 dirpath=$CPU_PATH/$cpu/cpufreq
159 gov=$(cat $dirpath/scaling_governor)
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530160
161 # consider per-policy governor case
162 if [ -e $CPU_PATH/$cpu/cpufreq/$gov ]; then
163 sampling_rate=$(cat $CPU_PATH/$cpu/cpufreq/$gov/sampling_rate)
164 else
165 sampling_rate=$(cat $CPU_PATH/cpufreq/$gov/sampling_rate)
166 fi
167 sampling_rate=$((sampling_rate * 1000)) # unit nsec
Daniel Lezcano93382952011-09-19 11:41:39 +0200168
169 latency=$(cat $dirpath/cpuinfo_transition_latency)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530170 if [ $? -ne 0 ]; then
Daniel Lezcano0b90d412011-09-20 08:31:04 +0200171 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200172 fi
173
174 nrfreq=$(cat $dirpath/scaling_available_frequencies | wc -w)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530175 if [ $? -ne 0 ]; then
Daniel Lezcano0b90d412011-09-20 08:31:04 +0200176 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200177 fi
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200178
179 nrfreq=$((nrfreq + 1))
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530180
181 sleep_time=$(($latency + $sampling_rate))
182
183 ../utils/nanosleep $(($nrfreq * $sleep_time))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200184}
185
186frequnit() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800187 freq=$1
188 ghz=$(echo "scale=1;($freq / 1000000)" | bc -l)
189 mhz=$(echo "scale=1;($freq / 1000)" | bc -l)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200190
191 res=$(echo "($ghz > 1.0)" | bc -l)
192 if [ "$res" = "1" ]; then
193 echo $ghz GHz
194 return 0
195 fi
196
197 res=$(echo "($mhz > 1.0)" | bc -l)
198 if [ "$res" = "1" ];then
199 echo $mhz MHz
200 return 0
201 fi
202
203 echo $freq KHz
204}
205
206set_frequency() {
207
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800208 cpu=$1
209 dirpath=$CPU_PATH/$cpu/cpufreq
210 newfreq=$2
211 setfreqpath=$dirpath/scaling_setspeed
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200212
213 echo $newfreq > $setfreqpath
214 wait_latency $cpu
215}
216
217get_frequency() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800218 cpu=$1
219 dirpath=$CPU_PATH/$cpu/cpufreq/scaling_cur_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200220 cat $dirpath
221}
222
223get_max_frequency() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800224 cpu=$1
225 dirpath=$CPU_PATH/$cpu/cpufreq/scaling_max_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200226 cat $dirpath
227}
228
229get_min_frequency() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800230 cpu=$1
231 dirpath=$CPU_PATH/$cpu/cpufreq/scaling_min_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200232 cat $dirpath
233}
234
Daniel Lezcano805e3352011-10-03 11:07:36 +0200235set_online() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800236 cpu=$1
237 dirpath=$CPU_PATH/$cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200238
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200239 if [ "$cpu" = "cpu0" ]; then
240 return 0
241 fi
242
Daniel Lezcano805e3352011-10-03 11:07:36 +0200243 echo 1 > $dirpath/online
244}
245
246set_offline() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800247 cpu=$1
248 dirpath=$CPU_PATH/$cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200249
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200250 if [ "$cpu" = "cpu0" ]; then
251 return 0
252 fi
253
Daniel Lezcano805e3352011-10-03 11:07:36 +0200254 echo 0 > $dirpath/online
255}
256
257get_online() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800258 cpu=$1
259 dirpath=$CPU_PATH/$cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200260
261 cat $dirpath/online
262}
263
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200264check() {
265
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800266 descr=$1
267 func=$2
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200268 shift 2;
269
270 log_begin "checking $descr"
271
272 $func $@
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530273 if [ $? -ne 0 ]; then
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530274 log_end "Err"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200275 return 1
276 fi
277
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530278 log_end "Ok"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200279
280 return 0
281}
282
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200283check_file() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800284 file=$1
285 dir=$2
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200286
Sanjay Singh Rawat8b6ad7c2014-07-08 16:35:36 +0200287 check "'$file' exists in '$dir'" "test -f" $dir/$file
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200288}
289
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200290check_cpufreq_files() {
291
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800292 dirpath=$CPU_PATH/$1/cpufreq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200293 shift 1
294
295 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200296 check_file $i $dirpath || return 1
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200297 done
298
299 return 0
300}
301
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200302check_sched_mc_files() {
303
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800304 dirpath=$CPU_PATH
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200305
306 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200307 check_file $i $dirpath || return 1
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200308 done
309
310 return 0
311}
312
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200313check_topology_files() {
314
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800315 dirpath=$CPU_PATH/$1/topology
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200316 shift 1
317
318 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200319 check_file $i $dirpath || return 1
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200320 done
321
322 return 0
323}
324
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200325check_cpuhotplug_files() {
326
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800327 dirpath=$CPU_PATH/$1
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200328 shift 1
329
330 for i in $@; do
Lisa Nguyene2323182014-07-04 10:24:30 +0530331 if [ `echo $dirpath | grep -c "cpu0"` -eq 1 ]; then
332 if [ $hotplug_allow_cpu0 -eq 0 ]; then
333 continue
334 fi
335 fi
336
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200337 check_file $i $dirpath || return 1
338 done
339
340 return 0
341}
342
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200343save_governors() {
344
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800345 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200346
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700347 for cpu in $cpus; do
348 governors_backup[$index]=$(cat $CPU_PATH/$cpu/cpufreq/scaling_governor)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200349 index=$((index + 1))
350 done
351}
352
353restore_governors() {
354
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800355 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200356
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700357 for cpu in $cpus; do
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200358 oldgov=${governors_backup[$index]}
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700359 echo $oldgov > $CPU_PATH/$cpu/cpufreq/scaling_governor
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200360 index=$((index + 1))
361 done
362}
363
364save_frequencies() {
365
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800366 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200367
368 for cpu in $cpus; do
369 frequencies_backup[$index]=$(cat $CPU_PATH/$cpu/cpufreq/scaling_cur_freq)
370 index=$((index + 1))
371 done
372}
373
374restore_frequencies() {
375
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800376 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200377
378 for cpu in $cpus; do
379 oldfreq=${frequencies_backup[$index]}
380 echo $oldfreq > $CPU_PATH/$cpu/cpufreq/scaling_setspeed
381 index=$((index + 1))
382 done
383}
384
385sigtrap() {
386 exit 255
Daniel Lezcano18e22b52011-08-06 02:52:02 +0200387}
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530388
389# currently we support ubuntu and android
390get_os() {
391 lsb_release -a 2>&1 | grep -i ubuntu > /dev/null
392 if [ $? -eq 0 ]; then
393 # for ubuntu
394 return 1
395 else
396 # for android (if needed can look for build.prop)
397 return 2
398 fi
399}
400
401is_root() {
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530402 get_os
403 if [ $? -eq 1 ]; then
404 # for ubuntu
405 ret=$(id -u)
406 else
407 # for android
408 ret=$(id | sed -n 's/uid=//p' | sed -n 's/(root) [a-z]*=[0-9]*(log)//p')
409 fi
410 return $ret
411}
Lisa Nguyene2323182014-07-04 10:24:30 +0530412
413is_cpu0_hotplug_allowed() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800414 status=$1
Lisa Nguyene2323182014-07-04 10:24:30 +0530415
416 if [ $status -eq 1 ]; then
417 return 0
418 else
419 return 1
420 fi
421}