blob: ab73e4a0e944b589d8e0dd7cd64f8c168a764d1c [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 Nguyena06e5bf2015-02-18 13:47:59 -080026. ../Switches/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"
Lisa Nguyen15e40ff2015-02-02 12:30:06 -080038gov_array="governors_backup"
39freq_array="frequencies_backup"
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053040
41test_status_show() {
Lisa Nguyene2877d52014-08-04 12:30:43 -070042 if [ $fail_count -ne 0 ]; then
43 test_script_status="fail"
44 else
45 if [ $skip_count -ne 0 ]; then
46 if [ $pass_count -ne 0 ]; then
47 test_script_status="pass"
48 else
49 test_script_status="skip"
50 fi
51 fi
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053052 fi
Lisa Nguyene2877d52014-08-04 12:30:43 -070053
54 echo " "
Lisa Nguyend1e0be92015-01-27 13:57:02 -080055 if [ "$test_script_status" = "fail" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070056 echo "$TEST_NAME: fail"
Lisa Nguyend1e0be92015-01-27 13:57:02 -080057 elif [ "$test_script_status" = "skip" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070058 echo "$TEST_NAME: skip"
59 else
60 echo "$TEST_NAME: pass"
61 fi
62 echo " "
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053063}
Daniel Lezcanod84ec062011-07-26 14:38:59 +020064
Lisa Nguyend713d632014-08-20 17:26:04 -070065skip_tests() {
66 dir=$1
67
68 test_script_list=$(ls ../$1/*.sh | grep -v 'sanity.sh$' | grep -v '00.sh$')
69
70 for test_script in $test_script_list; do
71 test_case=$(basename $test_script .sh)
72 echo "$test_case: skip"
73 done
74}
75
Daniel Lezcanod84ec062011-07-26 14:38:59 +020076log_begin() {
Daniel Lezcano994f6f12011-08-16 13:28:33 +020077 printf "%-76s" "$TEST_NAME.$INC$CPU: $@... "
Daniel Lezcano1efddc12011-08-09 23:45:30 +020078 INC=$(($INC+1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +020079}
80
81log_end() {
82 printf "$*\n"
Lisa Nguyene2877d52014-08-04 12:30:43 -070083
Lisa Nguyend1e0be92015-01-27 13:57:02 -080084 if [ "$*" = "Err" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070085 fail_count=$((fail_count + 1))
Lisa Nguyend1e0be92015-01-27 13:57:02 -080086 elif [ "$*" = "skip" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070087 skip_count=$((skip_count + 1))
88 else
89 pass_count=$((pass_count + 1))
90 fi
Daniel Lezcanod84ec062011-07-26 14:38:59 +020091}
92
93log_skip() {
94 log_begin "$@"
Daniel Lezcanoff4aa112011-08-16 15:48:38 +020095 log_end "skip"
Daniel Lezcanod84ec062011-07-26 14:38:59 +020096}
97
98for_each_cpu() {
99
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800100 cpu_func=$1
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200101 shift 1
102
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200103 for cpu in $cpus; do
Daniel Lezcano1efddc12011-08-09 23:45:30 +0200104 INC=0
105 CPU=/$cpu
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800106 $cpu_func $cpu $@
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200107 done
108
109 return 0
110}
111
112for_each_governor() {
113
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800114 gov_cpu=$1
115 gov_func=$2
116 cpufreq_dirpath=$CPU_PATH/$gov_cpu/cpufreq
117 governors=$(cat $cpufreq_dirpath/scaling_available_governors)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200118 shift 2
119
120 for governor in $governors; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800121 $gov_func $gov_cpu $governor $@
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200122 done
123
124 return 0
125}
126
127for_each_frequency() {
128
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800129 freq_cpu=$1
130 freq_func=$2
131 cpufreq_dirpath=$CPU_PATH/$freq_cpu/cpufreq
132 frequencies=$(cat $cpufreq_dirpath/scaling_available_frequencies)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200133 shift 2
134
135 for frequency in $frequencies; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800136 $freq_func $freq_cpu $frequency $@
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200137 done
138
139 return 0
140}
141
142set_governor() {
143
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800144 gov_cpu=$1
145 scaling_gov_dirpath=$CPU_PATH/$gov_cpu/cpufreq/scaling_governor
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800146 newgov=$2
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200147
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800148 echo $newgov > $scaling_gov_dirpath
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200149}
150
151get_governor() {
152
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800153 gov_cpu=$1
154 scaling_gov_dirpath=$CPU_PATH/$gov_cpu/cpufreq/scaling_governor
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200155
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800156 cat $scaling_gov_dirpath
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200157}
158
159wait_latency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800160 wait_latency_cpu=$1
161 cpufreq_dirpath=$CPU_PATH/$wait_latency_cpu/cpufreq
162 gov=$(cat $cpufreq_dirpath/scaling_governor)
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530163
164 # consider per-policy governor case
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800165 if [ -e $CPU_PATH/$wait_latency_cpu/cpufreq/$gov ]; then
166 sampling_rate=$(cat $CPU_PATH/$wait_latency_cpu/cpufreq/$gov/sampling_rate)
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530167 else
168 sampling_rate=$(cat $CPU_PATH/cpufreq/$gov/sampling_rate)
169 fi
170 sampling_rate=$((sampling_rate * 1000)) # unit nsec
Daniel Lezcano93382952011-09-19 11:41:39 +0200171
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800172 latency=$(cat $cpufreq_dirpath/cpuinfo_transition_latency)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530173 if [ $? -ne 0 ]; then
Daniel Lezcano0b90d412011-09-20 08:31:04 +0200174 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200175 fi
176
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800177 nrfreq=$(cat $cpufreq_dirpath/scaling_available_frequencies | wc -w)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530178 if [ $? -ne 0 ]; then
Daniel Lezcano0b90d412011-09-20 08:31:04 +0200179 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200180 fi
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200181
182 nrfreq=$((nrfreq + 1))
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530183
184 sleep_time=$(($latency + $sampling_rate))
185
Lisa Nguyenf62f3542015-01-26 17:33:03 -0800186 $NANOSLEEP $(($nrfreq * $sleep_time))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200187}
188
189frequnit() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800190 freq=$1
Lisa Nguyen374c1b52015-10-21 17:57:04 -0700191 ghz=$(echo $freq | awk '{printf "%.1f", ($1 / 1000000)}')
192 mhz=$(echo $freq | awk '{printf "%.1f", ($1 / 1000)}')
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200193
Lisa Nguyen374c1b52015-10-21 17:57:04 -0700194 ghz_value=$(echo $ghz | awk '{printf "%f", ($1 > 1.0)}')
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800195 if [ "$ghz_value" = "1" ]; then
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200196 echo $ghz GHz
197 return 0
198 fi
199
Lisa Nguyen374c1b52015-10-21 17:57:04 -0700200 mhz_value=$(echo $mhz | awk '{printf "%f", ($1 > 1.0)}')
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800201 if [ "$mhz_value" = "1" ];then
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200202 echo $mhz MHz
203 return 0
204 fi
205
206 echo $freq KHz
207}
208
209set_frequency() {
210
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800211 freq_cpu=$1
212 cpufreq_dirpath=$CPU_PATH/$freq_cpu/cpufreq
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800213 newfreq=$2
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800214 setfreqpath=$cpufreq_dirpath/scaling_setspeed
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200215
216 echo $newfreq > $setfreqpath
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800217 wait_latency $freq_cpu
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200218}
219
220get_frequency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800221 freq_cpu=$1
222 scaling_cur_freq=$CPU_PATH/$freq_cpu/cpufreq/scaling_cur_freq
223 cat $scaling_cur_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200224}
225
226get_max_frequency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800227 freq_cpu=$1
228 scaling_max_freq=$CPU_PATH/$freq_cpu/cpufreq/scaling_max_freq
229 cat $scaling_max_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200230}
231
232get_min_frequency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800233 freq_cpu=$1
234 scaling_min_freq=$CPU_PATH/$freq_cpu/cpufreq/scaling_min_freq
235 cat $scaling_min_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200236}
237
Daniel Lezcano805e3352011-10-03 11:07:36 +0200238set_online() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800239 current_cpu=$1
240 current_cpu_path=$CPU_PATH/$current_cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200241
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800242 if [ "$current_cpu" = "cpu0" ]; then
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200243 return 0
244 fi
245
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800246 echo 1 > $current_cpu_path/online
Daniel Lezcano805e3352011-10-03 11:07:36 +0200247}
248
249set_offline() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800250 current_cpu=$1
251 current_cpu_path=$CPU_PATH/$current_cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200252
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800253 if [ "$current_cpu" = "cpu0" ]; then
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200254 return 0
255 fi
256
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800257 echo 0 > $current_cpu_path/online
Daniel Lezcano805e3352011-10-03 11:07:36 +0200258}
259
260get_online() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800261 current_cpu=$1
262 current_cpu_path=$CPU_PATH/$current_cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200263
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800264 cat $current_cpu_path/online
Daniel Lezcano805e3352011-10-03 11:07:36 +0200265}
266
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200267check() {
268
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800269 check_descr=$1
270 check_func=$2
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200271 shift 2;
272
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800273 log_begin "checking $check_descr"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200274
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800275 $check_func $@
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530276 if [ $? -ne 0 ]; then
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530277 log_end "Err"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200278 return 1
279 fi
280
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530281 log_end "Ok"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200282
283 return 0
284}
285
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200286check_file() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800287 file=$1
288 dir=$2
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200289
Sanjay Singh Rawat8b6ad7c2014-07-08 16:35:36 +0200290 check "'$file' exists in '$dir'" "test -f" $dir/$file
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200291}
292
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200293check_cpufreq_files() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800294 cpu_id=$1
295 cpufreq_files_dir=$CPU_PATH/$cpu_id/cpufreq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200296 shift 1
297
298 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800299 check_file $i $cpufreq_files_dir || return 1
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200300 done
301
302 return 0
303}
304
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200305check_sched_mc_files() {
306
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200307 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800308 check_file $i $CPU_PATH || 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() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800315 cpu=$1
316 topology_files_dir=$CPU_PATH/$cpu/topology
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200317 shift 1
318
319 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800320 check_file $i $topology_files_dir || 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 Nguyen2d49a682015-01-26 20:27:48 -0800328 cpuhotplug_files_dir=$CPU_PATH/$1
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200329 shift 1
330
331 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800332 if [ `echo $cpuhotplug_files_dir | grep -c "cpu0"` -eq 1 ]; then
Lisa Nguyene2323182014-07-04 10:24:30 +0530333 if [ $hotplug_allow_cpu0 -eq 0 ]; then
334 continue
335 fi
336 fi
337
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800338 check_file $i $cpuhotplug_files_dir || return 1
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200339 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
Lisa Nguyen15e40ff2015-02-02 12:30:06 -0800349 scaling_gov_value=$(cat $CPU_PATH/$cpu/cpufreq/scaling_governor)
350 eval $gov_array$index=$scaling_gov_value
351 eval export $gov_array$index
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200352 done
353}
354
355restore_governors() {
356
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800357 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200358
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700359 for cpu in $cpus; do
Lisa Nguyen15e40ff2015-02-02 12:30:06 -0800360 oldgov=$(eval echo \$$gov_array$index)
361 echo $oldgov > $CPU_PATH/$cpu/cpufreq/scaling_governor
362 index=$((index + 1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200363 done
364}
365
366save_frequencies() {
367
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800368 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200369
370 for cpu in $cpus; do
Lisa Nguyen15e40ff2015-02-02 12:30:06 -0800371 freq_value=$(cat $CPU_PATH/$cpu/cpufreq/scaling_cur_freq)
372 eval $freq_array$index=$freq_value
373 eval export $freq_array$index
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200374 done
375}
376
377restore_frequencies() {
378
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800379 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200380
381 for cpu in $cpus; do
Lisa Nguyen15e40ff2015-02-02 12:30:06 -0800382 oldfreq=$(eval echo \$$freq_array$index)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200383 echo $oldfreq > $CPU_PATH/$cpu/cpufreq/scaling_setspeed
384 index=$((index + 1))
385 done
386}
387
388sigtrap() {
389 exit 255
Daniel Lezcano18e22b52011-08-06 02:52:02 +0200390}
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530391
392# currently we support ubuntu and android
393get_os() {
394 lsb_release -a 2>&1 | grep -i ubuntu > /dev/null
395 if [ $? -eq 0 ]; then
396 # for ubuntu
397 return 1
398 else
399 # for android (if needed can look for build.prop)
400 return 2
401 fi
402}
403
404is_root() {
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530405 get_os
406 if [ $? -eq 1 ]; then
407 # for ubuntu
408 ret=$(id -u)
409 else
410 # for android
Lisa Nguyen46f76f82015-11-06 19:34:38 -0800411 ret=$(id | awk '{if ($1) print $1}' | sed 's/[^0-9]*//g')
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530412 fi
413 return $ret
414}
Lisa Nguyene2323182014-07-04 10:24:30 +0530415
416is_cpu0_hotplug_allowed() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800417 status=$1
Lisa Nguyene2323182014-07-04 10:24:30 +0530418
419 if [ $status -eq 1 ]; then
420 return 0
421 else
422 return 1
423 fi
424}