blob: 10b8de8293f15206eda5f2c5995922d43aadb907 [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#
Lisa Nguyen3ab9ffd2016-01-25 12:16:18 -08005# Copyright (C) 2008-2009 Canonical Ltd.
6# Copyright (C) 2011-2016, Linaro Limited.
Daniel Lezcanod84ec062011-07-26 14:38:59 +02007#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License
10# as published by the Free Software Foundation; either version 2
11# of the License, or (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21#
22# Contributors:
23# Daniel Lezcano <daniel.lezcano@linaro.org> (IBM Corporation)
24# - initial API and implementation
Lisa Nguyen3ab9ffd2016-01-25 12:16:18 -080025# Michael Frey <michael.frey@canonical.com>
26# - initial suspend/resume functions
27# Andy Whitcroft <apw@canonical.com>
28# - initial suspend/resume functions
29# Hongbo Zhang <hongbo.zhang@linaro.org>
30# - updated suspend/resume functions
Daniel Lezcanod84ec062011-07-26 14:38:59 +020031#
32
Lisa Nguyena06e5bf2015-02-18 13:47:59 -080033. ../Switches/Switches.sh
Hongbo Zhang974802b2013-02-04 19:22:58 +080034
Lisa Nguyen3ab9ffd2016-01-25 12:16:18 -080035LOGDIR='/var/lib/pm-utils'
36LOGFILE="$LOGDIR/stress.log"
Daniel Lezcanod84ec062011-07-26 14:38:59 +020037CPU_PATH="/sys/devices/system/cpu"
38TEST_NAME=$(basename ${0%.sh})
Daniel Lezcano1efddc12011-08-09 23:45:30 +020039PREFIX=$TEST_NAME
40INC=0
Lisa Nguyen523e8d92014-09-04 17:26:03 -070041cpus=$(ls $CPU_PATH | grep "cpu[0-9].*")
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053042pass_count=0
43fail_count=0
Lisa Nguyene2877d52014-08-04 12:30:43 -070044skip_count=0
45test_script_status="pass"
Lisa Nguyenf62f3542015-01-26 17:33:03 -080046NANOSLEEP="../utils/nanosleep"
Lisa Nguyen15e40ff2015-02-02 12:30:06 -080047gov_array="governors_backup"
48freq_array="frequencies_backup"
Lisa Nguyend39d0bb2015-11-16 22:39:25 -080049THERMAL_PATH="/sys/devices/virtual/thermal"
50MAX_ZONE=0-12
51MAX_CDEV=0-50
52scaling_freq_array="scaling_freq"
53mode_array="mode_list"
54thermal_gov_array="thermal_governor_backup"
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053055
Lisa Nguyen3ab9ffd2016-01-25 12:16:18 -080056# config options for suspend/resume
57dry=0
58auto=1
59pm_trace=1
60timer_sleep=20
61
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053062test_status_show() {
Lisa Nguyene2877d52014-08-04 12:30:43 -070063 if [ $fail_count -ne 0 ]; then
64 test_script_status="fail"
65 else
66 if [ $skip_count -ne 0 ]; then
67 if [ $pass_count -ne 0 ]; then
68 test_script_status="pass"
69 else
70 test_script_status="skip"
71 fi
72 fi
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053073 fi
Lisa Nguyene2877d52014-08-04 12:30:43 -070074
75 echo " "
Lisa Nguyend1e0be92015-01-27 13:57:02 -080076 if [ "$test_script_status" = "fail" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070077 echo "$TEST_NAME: fail"
Lisa Nguyend1e0be92015-01-27 13:57:02 -080078 elif [ "$test_script_status" = "skip" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -070079 echo "$TEST_NAME: skip"
80 else
81 echo "$TEST_NAME: pass"
82 fi
83 echo " "
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053084}
Daniel Lezcanod84ec062011-07-26 14:38:59 +020085
Lisa Nguyend713d632014-08-20 17:26:04 -070086skip_tests() {
87 dir=$1
88
89 test_script_list=$(ls ../$1/*.sh | grep -v 'sanity.sh$' | grep -v '00.sh$')
90
91 for test_script in $test_script_list; do
92 test_case=$(basename $test_script .sh)
93 echo "$test_case: skip"
94 done
95}
96
Daniel Lezcanod84ec062011-07-26 14:38:59 +020097log_begin() {
Daniel Lezcano994f6f12011-08-16 13:28:33 +020098 printf "%-76s" "$TEST_NAME.$INC$CPU: $@... "
Daniel Lezcano1efddc12011-08-09 23:45:30 +020099 INC=$(($INC+1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200100}
101
102log_end() {
103 printf "$*\n"
Lisa Nguyene2877d52014-08-04 12:30:43 -0700104
Lisa Nguyend1e0be92015-01-27 13:57:02 -0800105 if [ "$*" = "Err" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -0700106 fail_count=$((fail_count + 1))
Lisa Nguyend1e0be92015-01-27 13:57:02 -0800107 elif [ "$*" = "skip" ]; then
Lisa Nguyene2877d52014-08-04 12:30:43 -0700108 skip_count=$((skip_count + 1))
109 else
110 pass_count=$((pass_count + 1))
111 fi
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200112}
113
114log_skip() {
115 log_begin "$@"
Daniel Lezcanoff4aa112011-08-16 15:48:38 +0200116 log_end "skip"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200117}
118
119for_each_cpu() {
120
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800121 cpu_func=$1
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200122 shift 1
123
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200124 for cpu in $cpus; do
Daniel Lezcano1efddc12011-08-09 23:45:30 +0200125 INC=0
126 CPU=/$cpu
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800127 $cpu_func $cpu $@
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200128 done
129
130 return 0
131}
132
133for_each_governor() {
134
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800135 gov_cpu=$1
136 gov_func=$2
137 cpufreq_dirpath=$CPU_PATH/$gov_cpu/cpufreq
138 governors=$(cat $cpufreq_dirpath/scaling_available_governors)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200139 shift 2
140
141 for governor in $governors; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800142 $gov_func $gov_cpu $governor $@
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200143 done
144
145 return 0
146}
147
148for_each_frequency() {
149
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800150 freq_cpu=$1
151 freq_func=$2
152 cpufreq_dirpath=$CPU_PATH/$freq_cpu/cpufreq
153 frequencies=$(cat $cpufreq_dirpath/scaling_available_frequencies)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200154 shift 2
155
156 for frequency in $frequencies; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800157 $freq_func $freq_cpu $frequency $@
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200158 done
159
160 return 0
161}
162
163set_governor() {
164
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800165 gov_cpu=$1
166 scaling_gov_dirpath=$CPU_PATH/$gov_cpu/cpufreq/scaling_governor
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800167 newgov=$2
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200168
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800169 echo $newgov > $scaling_gov_dirpath
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200170}
171
172get_governor() {
173
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800174 gov_cpu=$1
175 scaling_gov_dirpath=$CPU_PATH/$gov_cpu/cpufreq/scaling_governor
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200176
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800177 cat $scaling_gov_dirpath
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200178}
179
180wait_latency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800181 wait_latency_cpu=$1
182 cpufreq_dirpath=$CPU_PATH/$wait_latency_cpu/cpufreq
183 gov=$(cat $cpufreq_dirpath/scaling_governor)
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530184
185 # consider per-policy governor case
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800186 if [ -e $CPU_PATH/$wait_latency_cpu/cpufreq/$gov ]; then
Lisa Nguyene7145532015-11-17 17:24:28 -0800187 #try one path to see if the sampling_rate can be found
188 if [ -e $CPU_PATH/$wait_latency_cpu/cpufreq/$gov/sampling_rate ]; then
189 sampling_rate=$(cat $CPU_PATH/$wait_latency_cpu/cpufreq/$gov/sampling_rate)
190 else
191 # try another path to get the sampling_rate
192 if [ -e $CPU_PATH/cpufreq/$gov/sampling_rate ]; then
193 sampling_rate=$(cat $CPU_PATH/cpufreq/$gov/sampling_rate)
194 else
195 return 1
196 fi
197 fi
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530198 else
Lisa Nguyene7145532015-11-17 17:24:28 -0800199 return 1
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530200 fi
201 sampling_rate=$((sampling_rate * 1000)) # unit nsec
Daniel Lezcano93382952011-09-19 11:41:39 +0200202
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800203 latency=$(cat $cpufreq_dirpath/cpuinfo_transition_latency)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530204 if [ $? -ne 0 ]; then
Lisa Nguyene7145532015-11-17 17:24:28 -0800205 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200206 fi
207
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800208 nrfreq=$(cat $cpufreq_dirpath/scaling_available_frequencies | wc -w)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530209 if [ $? -ne 0 ]; then
Lisa Nguyene7145532015-11-17 17:24:28 -0800210 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200211 fi
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200212
213 nrfreq=$((nrfreq + 1))
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530214
215 sleep_time=$(($latency + $sampling_rate))
216
Lisa Nguyenf62f3542015-01-26 17:33:03 -0800217 $NANOSLEEP $(($nrfreq * $sleep_time))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200218}
219
220frequnit() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800221 freq=$1
Lisa Nguyen374c1b52015-10-21 17:57:04 -0700222 ghz=$(echo $freq | awk '{printf "%.1f", ($1 / 1000000)}')
223 mhz=$(echo $freq | awk '{printf "%.1f", ($1 / 1000)}')
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200224
Lisa Nguyen374c1b52015-10-21 17:57:04 -0700225 ghz_value=$(echo $ghz | awk '{printf "%f", ($1 > 1.0)}')
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800226 if [ "$ghz_value" = "1" ]; then
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200227 echo $ghz GHz
228 return 0
229 fi
230
Lisa Nguyen374c1b52015-10-21 17:57:04 -0700231 mhz_value=$(echo $mhz | awk '{printf "%f", ($1 > 1.0)}')
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800232 if [ "$mhz_value" = "1" ];then
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200233 echo $mhz MHz
234 return 0
235 fi
236
237 echo $freq KHz
238}
239
240set_frequency() {
241
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800242 freq_cpu=$1
243 cpufreq_dirpath=$CPU_PATH/$freq_cpu/cpufreq
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800244 newfreq=$2
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800245 setfreqpath=$cpufreq_dirpath/scaling_setspeed
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200246
247 echo $newfreq > $setfreqpath
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800248 wait_latency $freq_cpu
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200249}
250
251get_frequency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800252 freq_cpu=$1
253 scaling_cur_freq=$CPU_PATH/$freq_cpu/cpufreq/scaling_cur_freq
254 cat $scaling_cur_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200255}
256
257get_max_frequency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800258 freq_cpu=$1
259 scaling_max_freq=$CPU_PATH/$freq_cpu/cpufreq/scaling_max_freq
260 cat $scaling_max_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200261}
262
263get_min_frequency() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800264 freq_cpu=$1
265 scaling_min_freq=$CPU_PATH/$freq_cpu/cpufreq/scaling_min_freq
266 cat $scaling_min_freq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200267}
268
Daniel Lezcano805e3352011-10-03 11:07:36 +0200269set_online() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800270 current_cpu=$1
271 current_cpu_path=$CPU_PATH/$current_cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200272
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800273 if [ "$current_cpu" = "cpu0" ]; then
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200274 return 0
275 fi
276
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800277 echo 1 > $current_cpu_path/online
Daniel Lezcano805e3352011-10-03 11:07:36 +0200278}
279
280set_offline() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800281 current_cpu=$1
282 current_cpu_path=$CPU_PATH/$current_cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200283
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800284 if [ "$current_cpu" = "cpu0" ]; then
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200285 return 0
286 fi
287
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800288 echo 0 > $current_cpu_path/online
Daniel Lezcano805e3352011-10-03 11:07:36 +0200289}
290
291get_online() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800292 current_cpu=$1
293 current_cpu_path=$CPU_PATH/$current_cpu
Daniel Lezcano805e3352011-10-03 11:07:36 +0200294
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800295 cat $current_cpu_path/online
Daniel Lezcano805e3352011-10-03 11:07:36 +0200296}
297
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200298check() {
299
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800300 check_descr=$1
301 check_func=$2
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200302 shift 2;
303
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800304 log_begin "checking $check_descr"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200305
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800306 $check_func $@
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530307 if [ $? -ne 0 ]; then
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530308 log_end "Err"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200309 return 1
310 fi
311
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530312 log_end "Ok"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200313
314 return 0
315}
316
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200317check_file() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800318 file=$1
319 dir=$2
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200320
Sanjay Singh Rawat8b6ad7c2014-07-08 16:35:36 +0200321 check "'$file' exists in '$dir'" "test -f" $dir/$file
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200322}
323
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200324check_cpufreq_files() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800325 cpu_id=$1
326 cpufreq_files_dir=$CPU_PATH/$cpu_id/cpufreq
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200327 shift 1
328
329 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800330 check_file $i $cpufreq_files_dir || return 1
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200331 done
332
333 return 0
334}
335
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200336check_sched_mc_files() {
337
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200338 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800339 check_file $i $CPU_PATH || return 1
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200340 done
341
342 return 0
343}
344
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200345check_topology_files() {
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800346 cpu=$1
347 topology_files_dir=$CPU_PATH/$cpu/topology
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200348 shift 1
349
350 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800351 check_file $i $topology_files_dir || return 1
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200352 done
353
354 return 0
355}
356
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200357check_cpuhotplug_files() {
358
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800359 cpuhotplug_files_dir=$CPU_PATH/$1
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200360 shift 1
361
362 for i in $@; do
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800363 if [ `echo $cpuhotplug_files_dir | grep -c "cpu0"` -eq 1 ]; then
Lisa Nguyene2323182014-07-04 10:24:30 +0530364 if [ $hotplug_allow_cpu0 -eq 0 ]; then
365 continue
366 fi
367 fi
368
Lisa Nguyen2d49a682015-01-26 20:27:48 -0800369 check_file $i $cpuhotplug_files_dir || return 1
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200370 done
371
372 return 0
373}
374
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200375save_governors() {
376
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800377 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200378
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700379 for cpu in $cpus; do
Lisa Nguyen15e40ff2015-02-02 12:30:06 -0800380 scaling_gov_value=$(cat $CPU_PATH/$cpu/cpufreq/scaling_governor)
381 eval $gov_array$index=$scaling_gov_value
382 eval export $gov_array$index
Lisa Nguyen2af35fa2016-06-10 17:49:21 -0700383 index=$((index + 1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200384 done
385}
386
387restore_governors() {
388
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800389 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200390
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700391 for cpu in $cpus; do
Lisa Nguyen15e40ff2015-02-02 12:30:06 -0800392 oldgov=$(eval echo \$$gov_array$index)
393 echo $oldgov > $CPU_PATH/$cpu/cpufreq/scaling_governor
394 index=$((index + 1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200395 done
396}
397
398save_frequencies() {
399
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800400 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200401
402 for cpu in $cpus; do
Lisa Nguyen15e40ff2015-02-02 12:30:06 -0800403 freq_value=$(cat $CPU_PATH/$cpu/cpufreq/scaling_cur_freq)
404 eval $freq_array$index=$freq_value
405 eval export $freq_array$index
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200406 done
407}
408
409restore_frequencies() {
410
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800411 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200412
413 for cpu in $cpus; do
Lisa Nguyen15e40ff2015-02-02 12:30:06 -0800414 oldfreq=$(eval echo \$$freq_array$index)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200415 echo $oldfreq > $CPU_PATH/$cpu/cpufreq/scaling_setspeed
416 index=$((index + 1))
417 done
418}
419
420sigtrap() {
421 exit 255
Daniel Lezcano18e22b52011-08-06 02:52:02 +0200422}
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530423
424# currently we support ubuntu and android
425get_os() {
Lisa Nguyen43ec86a2016-07-25 19:00:29 -0700426 build_prop_file="\system\build.prop"
427
428 if [ -e "$build_prop_file" ]; then
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530429 # for ubuntu
430 return 1
Lisa Nguyen43ec86a2016-07-25 19:00:29 -0700431 else
432 # for android
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530433 return 2
Lisa Nguyen43ec86a2016-07-25 19:00:29 -0700434 fi
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530435}
436
437is_root() {
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530438 get_os
439 if [ $? -eq 1 ]; then
440 # for ubuntu
441 ret=$(id -u)
442 else
443 # for android
Lisa Nguyen46f76f82015-11-06 19:34:38 -0800444 ret=$(id | awk '{if ($1) print $1}' | sed 's/[^0-9]*//g')
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530445 fi
446 return $ret
447}
Lisa Nguyene2323182014-07-04 10:24:30 +0530448
449is_cpu0_hotplug_allowed() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800450 status=$1
Lisa Nguyene2323182014-07-04 10:24:30 +0530451
452 if [ $status -eq 1 ]; then
453 return 0
454 else
455 return 1
456 fi
457}
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800458
459check_valid_temp() {
460 file=$1
461 zone_name=$2
462 dir=$THERMAL_PATH/$zone_name
463
464 temp_file=$dir/$file
465 shift 2;
466
467 temp_val=$(cat $temp_file)
468 descr="'$zone_name'/'$file' ='$temp_val'"
469 log_begin "checking $descr"
470
471 if [ $temp_val -gt 0 ]; then
472 log_end "Ok"
473 return 0
474 fi
475
476 log_end "Err"
477
478 return 1
479}
480
481for_each_thermal_zone() {
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800482 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']")
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800483 thermal_func=$1
484 shift 1
485
486 for thermal_zone in $thermal_zones; do
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800487 INC=0
488 $thermal_func $thermal_zone $@
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800489 done
490
491 return 0
492}
493
494get_total_trip_point_of_zone() {
495 zone=$1
496 zone_path=$THERMAL_PATH/$zone
497 count=0
498 shift 1
499 trips=$(ls $zone_path | grep "trip_point_['$MAX_ZONE']_temp")
500 for trip in $trips; do
501 count=$((count + 1))
502 done
503 return $count
504}
505
506for_each_trip_point_of_zone() {
507
508 zone_path=$THERMAL_PATH/$1
509 count=0
510 func=$2
511 zone_name=$1
512 shift 2
513 trips=$(ls $zone_path | grep "trip_point_['$MAX_ZONE']_temp")
514 for trip in $trips; do
515 $func $zone_name $count
516 count=$((count + 1))
517 done
518 return 0
519}
520
521for_each_binding_of_zone() {
522
523 zone_path=$THERMAL_PATH/$1
524 count=0
525 func=$2
526 zone_name=$1
527 shift 2
528 trips=$(ls $zone_path | grep "cdev['$MAX_CDEV']_trip_point")
529 for trip in $trips; do
530 $func $zone_name $count
531 count=$((count + 1))
532 done
533
534 return 0
535
536}
537
538check_valid_binding() {
539 trip_point=$1
540 zone_name=$2
541 dirpath=$THERMAL_PATH/$zone_name
542 temp_file=$zone_name/$trip_point
543 trip_point_val=$(cat $dirpath/$trip_point)
544 get_total_trip_point_of_zone $zone_name
545 trip_point_max=$?
546 descr="'$temp_file' valid binding"
547 shift 2
548
549 log_begin "checking $descr"
550 if [ $trip_point_val -ge $trip_point_max ]; then
551 log_end "Err"
552 return 1
553 fi
554
555 log_end "Ok"
556 return 0
557}
558
559validate_trip_bindings() {
560 zone_name=$1
561 bind_no=$2
562 dirpath=$THERMAL_PATH/$zone_name
563 trip_point=cdev"$bind_no"_trip_point
564 shift 2
565
566 check_file $trip_point $dirpath || return 1
567 check_valid_binding $trip_point $zone_name || return 1
568}
569
570validate_trip_level() {
571 zone_name=$1
572 trip_no=$2
573 dirpath=$THERMAL_PATH/$zone_name
574 trip_temp=trip_point_"$trip_no"_temp
575 trip_type=trip_point_"$trip_no"_type
576 shift 2
577
578 check_file $trip_temp $dirpath || return 1
579 check_file $trip_type $dirpath || return 1
580 check_valid_temp $trip_temp $zone_name || return 1
581}
582
583for_each_cooling_device() {
584
585 cdev_func=$1
586 shift 1
587
588 cooling_devices=$(ls $THERMAL_PATH | grep "cooling_device['$MAX_CDEV']")
589 if [ "$cooling_devices" = "" ]; then
590 log_skip "no cooling devices"
591 return 0
592 fi
593
594 for cooling_device in $cooling_devices; do
595 INC=0
596 $cdev_func $cooling_device $@
597 done
598
599 return 0
600}
601check_scaling_freq() {
602
603 before_freq_list=$1
604 after_freq_list=$2
605 shift 2
606 index=0
607
608 flag=0
609 for cpu in $cpus; do
610 after_freq=$(eval echo \$$after_freq_list$index)
611 before_freq=$(eval echo \$$before_freq_list$index)
612
613 if [ $after_freq -ne $before_freq ]; then
614 flag=1
615 fi
616
617 index=$((index + 1))
618 done
619
620 return $flag
621}
622
623store_scaling_maxfreq() {
624 index=0
625
626 for cpu in $cpus; do
627 scaling_freq_max_value=$(cat $CPU_PATH/$cpu/cpufreq/scaling_max_freq)
628 eval $scaling_freq_array$index=$scaling_freq_max_value
Lisa Nguyen002f4812016-07-25 19:06:41 -0700629 eval export $scaling_freq_array$index
630
631 index=$((index + 1))
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800632 done
633
634 return 0
635}
636
637get_trip_id() {
638
639 trip_name=$1
640 shift 1
641
642 id1=$(echo $trip_name|cut -c12)
643 id2=$(echo $trip_name|cut -c13)
644 if [ $id2 != "_" ]; then
645 id1=$(($id2 + 10*$id1))
646 fi
647 return $id1
648}
649
650disable_all_thermal_zones() {
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800651 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']")
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800652 index=0
653
654 for thermal_zone in $thermal_zones; do
655 mode=$(cat $THERMAL_PATH/$thermal_zone/mode)
656 eval $mode_array$index=$mode
657 eval export $mode_array$index
658 index=$((index + 1))
659 echo -n "disabled" > $THERMAL_PATH/$thermal_zone/mode
660 done
661
662 return 0
663}
664
665enable_all_thermal_zones() {
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800666 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']")
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800667 index=0
668
669 for thermal_zone in $thermal_zones; do
670 mode=$(eval echo \$$mode_array$index)
671 echo $mode > $THERMAL_PATH/$thermal_zone/mode
672 index=$((index + 1))
673 done
674
675 return 0
676}
677
678GPU_HEAT_BIN=/usr/bin/glmark2
679gpu_pid=0
680
Lisa Nguyencca3b382016-09-11 21:19:54 -0700681check_for_glmark2() {
682 glmark2_found=$(which glmark2)
683
684 if [ $? -ne 0 ]; then
685 return 1
686 else
687 return 0
688 fi
689}
690
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800691start_glmark2() {
692 if [ -n "$ANDROID" ]; then
693 am start org.linaro.glmark2/.Glmark2Activity
694 return
695 fi
696
697 if [ -x $GPU_HEAT_BIN ]; then
698 $GPU_HEAT_BIN &
699 gpu_pid=$(pidof $GPU_HEAT_BIN)
700 # Starting X application from serial console needs this
701 if [ -z "$gpu_pid" ]; then
702 cp /etc/lightdm/lightdm.conf /etc/lightdm/lightdm.conf.bk
703 echo "autologin-user=root" >> /etc/lightdm/lightdm.conf
704 export DISPLAY=localhost:0.0
705 restart lightdm
706 sleep 5
707 mv /etc/lightdm/lightdm.conf.bk /etc/lightdm/lightdm.conf
708 $GPU_HEAT_BIN &
709 gpu_pid=$(pidof $GPU_HEAT_BIN)
710 fi
711 test -z "$gpu_pid" && cpu_pid=0
712 echo "start gpu heat binary $gpu_pid"
713 else
714 echo "glmark2 not found." 1>&2
715 fi
716}
717
718kill_glmark2() {
719 if [ -n "$ANDROID" ]; then
720 am kill org.linaro.glmark2
721 return
722 fi
723
724 if [ "$gpu_pid" -ne 0 ]; then
725 kill -9 $gpu_pid
726 fi
727}
728
729set_thermal_governors() {
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800730 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']")
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800731 gov=$1
732 index=0
733
734 for thermal_zone in $thermal_zones; do
735 policy=$(cat $THERMAL_PATH/$thermal_zone/policy)
736 eval $thermal_gov_array$index=$policy
737 eval export $thermal_gov_array$index
738 index=$((index + 1))
739 echo $gov > $THERMAL_PATH/$thermal_zone/policy
740 done
741
742 return 0
743}
744
745restore_thermal_governors() {
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800746 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']")
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800747 index=0
748
749 for thermal_zone in $thermal_zones; do
750 old_policy=$(eval echo \$$thermal_gov_array$index)
751 echo $old_policy > $THERMAL_PATH/$thermal_zone/policy
752 index=$((index + 1))
753 done
754
755 return 0
756}
Lisa Nguyenddd07af2015-11-17 23:15:22 -0800757
758check_for_thermal_zones()
759{
760 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']" 2>/dev/null)
761 if [ ! -z "$thermal_zones" ]; then
762 return 0
763 else
764 return 1
765 fi
766}
Lisa Nguyen3ab9ffd2016-01-25 12:16:18 -0800767
768check_logdir()
769{
770 if [ ! -f $LOGDIR ]; then
771 mkdir -p $LOGDIR
772 fi
773}
774
775setup_wakeup_timer ()
776{
777 timeout="$1"
778
779 # Request wakeup from the RTC or ACPI alarm timers. Set the timeout
780 # at 'now' + $timeout seconds.
781 ctl='/sys/class/rtc/rtc0/wakealarm'
782 if [ -f "$ctl" ]; then
783 # Cancel any outstanding timers.
784 echo "0" >"$ctl"
785 # rtcN/wakealarm can use relative time in seconds
786 echo "+$timeout" >"$ctl"
787 return 0
788 fi
789 ctl='/proc/acpi/alarm'
790 if [ -f "$ctl" ]; then
791 echo `date '+%F %H:%M:%S' -d '+ '$timeout' seconds'` >"$ctl"
792 return 0
793 fi
794
795 echo "no method to awaken machine automatically" 1>&2
796 exit 1
797}
798
799suspend_system ()
800{
801 if [ "$dry" -eq 1 ]; then
802 echo "DRY-RUN: suspend machine for $timer_sleep"
803 sleep 1
804 return
805 fi
806
807 setup_wakeup_timer "$timer_sleep"
808
809 dmesg >"$LOGFILE.dmesg.A"
810
811 # Initiate suspend in different ways.
812 case "$1" in
813 dbus)
814 dbus-send --session --type=method_call \
815 --dest=org.freedesktop.PowerManagement \
816 /org/freedesktop/PowerManagement \
817 org.freedesktop.PowerManagement.Suspend \
818 >> "$LOGFILE" || {
819 ECHO "FAILED: dbus suspend failed" 1>&2
820 return 1
821 }
822 ;;
823 pmsuspend)
824 pm-suspend >> "$LOGFILE"
825 ;;
826 mem)
827 `echo "mem" > /sys/power/state` >> "$LOGFILE"
828 ;;
829 esac
830
831 # Wait on the machine coming back up -- pulling the dmesg over.
832 echo "v---" >>"$LOGFILE"
833 retry=30
834 while [ "$retry" -gt 0 ]; do
835 retry=$((retry - 1))
836
837 # Accumulate the dmesg delta.
838 dmesg >"$LOGFILE.dmesg.B"
839 diff "$LOGFILE.dmesg.A" "$LOGFILE.dmesg.B" | \
840 grep '^>' >"$LOGFILE.dmesg"
841 mv "$LOGFILE.dmesg.B" "$LOGFILE.dmesg.A"
842
843 echo "Waiting for suspend to complete $retry to go ..." \
844 >> "$LOGFILE"
845 cat "$LOGFILE.dmesg" >> "$LOGFILE"
846
847 if [ "`grep -c 'Back to C!' $LOGFILE.dmesg`" -ne 0 ]; then
848 break;
849 fi
850 sleep 1
851 done
852 echo "^---" >>"$LOGFILE"
853 rm -f "$LOGFILE.dmesg"*
854 if [ "$retry" -eq 0 ]; then
855 ECHO "SUSPEND FAILED, did not go to sleep" 1>&2
856 return 1
857 fi
858}
859
860ECHO ()
861{
862 echo "$@" | tee -a "$LOGFILE"
863}
864
865enable_trace()
866{
867 if [ -w /sys/power/pm_trace ]; then
868 echo 1 > '/sys/power/pm_trace'
869 fi
870}
871
872disable_trace()
873{
874 if [ -w /sys/power/pm_trace ]; then
875 echo 0 > '/sys/power/pm_trace'
876 fi
877}
878
879trace_state=-1
880
881save_trace()
882{
883 if [ -r /sys/power/pm_trace ]; then
884 trace_state=`cat /sys/power/pm_trace`
885 fi
886}
887
888restore_trace()
889{
890 if [ "$trace_state" -ne -1 -a -w /sys/power/pm_trace ]; then
891 echo "$trace_state" > '/sys/power/pm_trace'
892 fi
893}
894
895battery_count()
896{
897 cat /proc/acpi/battery/*/state 2>/dev/null | \
898 awk '
899 BEGIN { total = 0 }
900 /present:.*yes/ { total += 1 }
901 END { print total }
902 '
903}
904
905battery_capacity()
906{
907 cat /proc/acpi/battery/*/state 2>/dev/null | \
908 awk '
909 BEGIN { total = 0 }
910 /remaining capacity:/ { total += $3 }
911 END { print total }
912 '
913}
914
915ac_needed=-1
916ac_is=-1
917ac_becomes=-1
918
919ac_required()
920{
921 ac_check
922
923 ac_needed="$1"
924 ac_becomes="$1"
925}
926
927ac_transitions()
928{
929 ac_check
930
931 ac_needed="$1"
932 ac_becomes="$2"
933}
934
935ac_online()
936{
937 cat /proc/acpi/ac_adapter/*/state 2>/dev/null | \
938 awk '
939 BEGIN { online = 0; offline = 0 }
940 /on-line/ { online = 1 }
941 /off-line/ { offline = 1 }
942 END {
943 if (online) {
944 print "1"
945 } else if (offline) {
946 print "0"
947 } else {
948 print "-1"
949 }
950 }
951 '
952}
953
954ac_check()
955{
956 ac_current=`ac_online`
957
958 if [ "$ac_becomes" -ne -1 -a "$ac_current" -ne -1 -a \
959 "$ac_current" -ne "$ac_becomes" ]; then
960 ECHO "*** WARNING: AC power not in expected state" \
961 "($ac_becomes) after test"
962 fi
963 ac_is="$ac_becomes"
964}
965
966phase=0
967phase_first=1
968phase_interactive=1
969
970phase()
971{
972 phase=$((phase + 1))
973
974 if [ "$ac_needed" -ne "$ac_is" ]; then
975 case "$ac_needed" in
976 0) echo "*** please ensure your AC cord is detached" ;;
977 1) echo "*** please ensure your AC cord is attached" ;;
978 esac
979 ac_is="$ac_needed"
980 fi
981
982 if [ "$timer_sleep" -gt 60 ]; then
983 sleep="$timer_sleep / 60"
984 sleep="$sleep minutes"
985 else
986 sleep="$timer_sleep seconds"
987 fi
988 echo "*** machine will suspend for $sleep"
989
990 if [ "$auto" -eq 1 ]; then
991 :
992 elif [ "$phase_interactive" -eq 1 ]; then
993 echo "*** press return when ready"
994 read x
995
996 elif [ "$phase_first" -eq 1 ]; then
997 echo "*** NOTE: there will be no further user interaction from this point"
998 echo "*** press return when ready"
999 phase_first=0
1000 read x
1001 fi
1002}
1003
1004save_trace
1005
1006if [ "$pm_trace" -eq 1 ]; then
1007 enable_trace
1008else
1009 disable_trace
1010fi