blob: 0c155bcec7cd83f5fc53cc35fc5a68ca024e3379 [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 Nguyen4e0b59a2015-01-25 17:46:40 -080098 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
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200104 $func $cpu $@
105 done
106
107 return 0
108}
109
110for_each_governor() {
111
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800112 cpu=$1
113 func=$2
114 dirpath=$CPU_PATH/$cpu/cpufreq
115 governors=$(cat $dirpath/scaling_available_governors)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200116 shift 2
117
118 for governor in $governors; do
119 $func $cpu $governor $@
120 done
121
122 return 0
123}
124
125for_each_frequency() {
126
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800127 cpu=$1
128 func=$2
129 dirpath=$CPU_PATH/$cpu/cpufreq
130 frequencies=$(cat $dirpath/scaling_available_frequencies)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200131 shift 2
132
133 for frequency in $frequencies; do
134 $func $cpu $frequency $@
135 done
136
137 return 0
138}
139
140set_governor() {
141
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800142 cpu=$1
143 dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor
144 newgov=$2
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200145
146 echo $newgov > $dirpath
147}
148
149get_governor() {
150
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800151 cpu=$1
152 dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200153
154 cat $dirpath
155}
156
157wait_latency() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800158 cpu=$1
159 dirpath=$CPU_PATH/$cpu/cpufreq
160 gov=$(cat $dirpath/scaling_governor)
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530161
162 # consider per-policy governor case
163 if [ -e $CPU_PATH/$cpu/cpufreq/$gov ]; then
164 sampling_rate=$(cat $CPU_PATH/$cpu/cpufreq/$gov/sampling_rate)
165 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
170 latency=$(cat $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
175 nrfreq=$(cat $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
192 res=$(echo "($ghz > 1.0)" | bc -l)
193 if [ "$res" = "1" ]; then
194 echo $ghz GHz
195 return 0
196 fi
197
198 res=$(echo "($mhz > 1.0)" | bc -l)
199 if [ "$res" = "1" ];then
200 echo $mhz MHz
201 return 0
202 fi
203
204 echo $freq KHz
205}
206
207set_frequency() {
208
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800209 cpu=$1
210 dirpath=$CPU_PATH/$cpu/cpufreq
211 newfreq=$2
212 setfreqpath=$dirpath/scaling_setspeed
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200213
214 echo $newfreq > $setfreqpath
215 wait_latency $cpu
216}
217
218get_frequency() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800219 cpu=$1
220 dirpath=$CPU_PATH/$cpu/cpufreq/scaling_cur_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200221 cat $dirpath
222}
223
224get_max_frequency() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800225 cpu=$1
226 dirpath=$CPU_PATH/$cpu/cpufreq/scaling_max_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200227 cat $dirpath
228}
229
230get_min_frequency() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800231 cpu=$1
232 dirpath=$CPU_PATH/$cpu/cpufreq/scaling_min_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200233 cat $dirpath
234}
235
Daniel Lezcano805e3352011-10-03 11:07:36 +0200236set_online() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800237 cpu=$1
238 dirpath=$CPU_PATH/$cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200239
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200240 if [ "$cpu" = "cpu0" ]; then
241 return 0
242 fi
243
Daniel Lezcano805e3352011-10-03 11:07:36 +0200244 echo 1 > $dirpath/online
245}
246
247set_offline() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800248 cpu=$1
249 dirpath=$CPU_PATH/$cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200250
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200251 if [ "$cpu" = "cpu0" ]; then
252 return 0
253 fi
254
Daniel Lezcano805e3352011-10-03 11:07:36 +0200255 echo 0 > $dirpath/online
256}
257
258get_online() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800259 cpu=$1
260 dirpath=$CPU_PATH/$cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200261
262 cat $dirpath/online
263}
264
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200265check() {
266
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800267 descr=$1
268 func=$2
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200269 shift 2;
270
271 log_begin "checking $descr"
272
273 $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() {
292
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800293 dirpath=$CPU_PATH/$1/cpufreq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200294 shift 1
295
296 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200297 check_file $i $dirpath || 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
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800305 dirpath=$CPU_PATH
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200306
307 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200308 check_file $i $dirpath || return 1
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200309 done
310
311 return 0
312}
313
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200314check_topology_files() {
315
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800316 dirpath=$CPU_PATH/$1/topology
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200317 shift 1
318
319 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200320 check_file $i $dirpath || return 1
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200321 done
322
323 return 0
324}
325
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200326check_cpuhotplug_files() {
327
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800328 dirpath=$CPU_PATH/$1
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200329 shift 1
330
331 for i in $@; do
Lisa Nguyene2323182014-07-04 10:24:30 +0530332 if [ `echo $dirpath | grep -c "cpu0"` -eq 1 ]; then
333 if [ $hotplug_allow_cpu0 -eq 0 ]; then
334 continue
335 fi
336 fi
337
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200338 check_file $i $dirpath || return 1
339 done
340
341 return 0
342}
343
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200344save_governors() {
345
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800346 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200347
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700348 for cpu in $cpus; do
349 governors_backup[$index]=$(cat $CPU_PATH/$cpu/cpufreq/scaling_governor)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200350 index=$((index + 1))
351 done
352}
353
354restore_governors() {
355
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800356 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200357
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700358 for cpu in $cpus; do
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200359 oldgov=${governors_backup[$index]}
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700360 echo $oldgov > $CPU_PATH/$cpu/cpufreq/scaling_governor
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200361 index=$((index + 1))
362 done
363}
364
365save_frequencies() {
366
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800367 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200368
369 for cpu in $cpus; do
370 frequencies_backup[$index]=$(cat $CPU_PATH/$cpu/cpufreq/scaling_cur_freq)
371 index=$((index + 1))
372 done
373}
374
375restore_frequencies() {
376
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800377 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200378
379 for cpu in $cpus; do
380 oldfreq=${frequencies_backup[$index]}
381 echo $oldfreq > $CPU_PATH/$cpu/cpufreq/scaling_setspeed
382 index=$((index + 1))
383 done
384}
385
386sigtrap() {
387 exit 255
Daniel Lezcano18e22b52011-08-06 02:52:02 +0200388}
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530389
390# currently we support ubuntu and android
391get_os() {
392 lsb_release -a 2>&1 | grep -i ubuntu > /dev/null
393 if [ $? -eq 0 ]; then
394 # for ubuntu
395 return 1
396 else
397 # for android (if needed can look for build.prop)
398 return 2
399 fi
400}
401
402is_root() {
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530403 get_os
404 if [ $? -eq 1 ]; then
405 # for ubuntu
406 ret=$(id -u)
407 else
408 # for android
409 ret=$(id | sed -n 's/uid=//p' | sed -n 's/(root) [a-z]*=[0-9]*(log)//p')
410 fi
411 return $ret
412}
Lisa Nguyene2323182014-07-04 10:24:30 +0530413
414is_cpu0_hotplug_allowed() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800415 status=$1
Lisa Nguyene2323182014-07-04 10:24:30 +0530416
417 if [ $status -eq 1 ]; then
418 return 0
419 else
420 return 1
421 fi
422}