blob: e7df3f77c6204250652a74572569a1d43e5582f5 [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
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200383 done
384}
385
386restore_governors() {
387
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800388 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200389
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700390 for cpu in $cpus; do
Lisa Nguyen15e40ff2015-02-02 12:30:06 -0800391 oldgov=$(eval echo \$$gov_array$index)
392 echo $oldgov > $CPU_PATH/$cpu/cpufreq/scaling_governor
393 index=$((index + 1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200394 done
395}
396
397save_frequencies() {
398
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800399 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200400
401 for cpu in $cpus; do
Lisa Nguyen15e40ff2015-02-02 12:30:06 -0800402 freq_value=$(cat $CPU_PATH/$cpu/cpufreq/scaling_cur_freq)
403 eval $freq_array$index=$freq_value
404 eval export $freq_array$index
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200405 done
406}
407
408restore_frequencies() {
409
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800410 index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200411
412 for cpu in $cpus; do
Lisa Nguyen15e40ff2015-02-02 12:30:06 -0800413 oldfreq=$(eval echo \$$freq_array$index)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200414 echo $oldfreq > $CPU_PATH/$cpu/cpufreq/scaling_setspeed
415 index=$((index + 1))
416 done
417}
418
419sigtrap() {
420 exit 255
Daniel Lezcano18e22b52011-08-06 02:52:02 +0200421}
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530422
423# currently we support ubuntu and android
424get_os() {
425 lsb_release -a 2>&1 | grep -i ubuntu > /dev/null
426 if [ $? -eq 0 ]; then
427 # for ubuntu
428 return 1
429 else
430 # for android (if needed can look for build.prop)
431 return 2
432 fi
433}
434
435is_root() {
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530436 get_os
437 if [ $? -eq 1 ]; then
438 # for ubuntu
439 ret=$(id -u)
440 else
441 # for android
Lisa Nguyen46f76f82015-11-06 19:34:38 -0800442 ret=$(id | awk '{if ($1) print $1}' | sed 's/[^0-9]*//g')
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530443 fi
444 return $ret
445}
Lisa Nguyene2323182014-07-04 10:24:30 +0530446
447is_cpu0_hotplug_allowed() {
Lisa Nguyen4e0b59a2015-01-25 17:46:40 -0800448 status=$1
Lisa Nguyene2323182014-07-04 10:24:30 +0530449
450 if [ $status -eq 1 ]; then
451 return 0
452 else
453 return 1
454 fi
455}
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800456
457check_valid_temp() {
458 file=$1
459 zone_name=$2
460 dir=$THERMAL_PATH/$zone_name
461
462 temp_file=$dir/$file
463 shift 2;
464
465 temp_val=$(cat $temp_file)
466 descr="'$zone_name'/'$file' ='$temp_val'"
467 log_begin "checking $descr"
468
469 if [ $temp_val -gt 0 ]; then
470 log_end "Ok"
471 return 0
472 fi
473
474 log_end "Err"
475
476 return 1
477}
478
479for_each_thermal_zone() {
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800480 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']")
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800481 thermal_func=$1
482 shift 1
483
484 for thermal_zone in $thermal_zones; do
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800485 INC=0
486 $thermal_func $thermal_zone $@
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800487 done
488
489 return 0
490}
491
492get_total_trip_point_of_zone() {
493 zone=$1
494 zone_path=$THERMAL_PATH/$zone
495 count=0
496 shift 1
497 trips=$(ls $zone_path | grep "trip_point_['$MAX_ZONE']_temp")
498 for trip in $trips; do
499 count=$((count + 1))
500 done
501 return $count
502}
503
504for_each_trip_point_of_zone() {
505
506 zone_path=$THERMAL_PATH/$1
507 count=0
508 func=$2
509 zone_name=$1
510 shift 2
511 trips=$(ls $zone_path | grep "trip_point_['$MAX_ZONE']_temp")
512 for trip in $trips; do
513 $func $zone_name $count
514 count=$((count + 1))
515 done
516 return 0
517}
518
519for_each_binding_of_zone() {
520
521 zone_path=$THERMAL_PATH/$1
522 count=0
523 func=$2
524 zone_name=$1
525 shift 2
526 trips=$(ls $zone_path | grep "cdev['$MAX_CDEV']_trip_point")
527 for trip in $trips; do
528 $func $zone_name $count
529 count=$((count + 1))
530 done
531
532 return 0
533
534}
535
536check_valid_binding() {
537 trip_point=$1
538 zone_name=$2
539 dirpath=$THERMAL_PATH/$zone_name
540 temp_file=$zone_name/$trip_point
541 trip_point_val=$(cat $dirpath/$trip_point)
542 get_total_trip_point_of_zone $zone_name
543 trip_point_max=$?
544 descr="'$temp_file' valid binding"
545 shift 2
546
547 log_begin "checking $descr"
548 if [ $trip_point_val -ge $trip_point_max ]; then
549 log_end "Err"
550 return 1
551 fi
552
553 log_end "Ok"
554 return 0
555}
556
557validate_trip_bindings() {
558 zone_name=$1
559 bind_no=$2
560 dirpath=$THERMAL_PATH/$zone_name
561 trip_point=cdev"$bind_no"_trip_point
562 shift 2
563
564 check_file $trip_point $dirpath || return 1
565 check_valid_binding $trip_point $zone_name || return 1
566}
567
568validate_trip_level() {
569 zone_name=$1
570 trip_no=$2
571 dirpath=$THERMAL_PATH/$zone_name
572 trip_temp=trip_point_"$trip_no"_temp
573 trip_type=trip_point_"$trip_no"_type
574 shift 2
575
576 check_file $trip_temp $dirpath || return 1
577 check_file $trip_type $dirpath || return 1
578 check_valid_temp $trip_temp $zone_name || return 1
579}
580
581for_each_cooling_device() {
582
583 cdev_func=$1
584 shift 1
585
586 cooling_devices=$(ls $THERMAL_PATH | grep "cooling_device['$MAX_CDEV']")
587 if [ "$cooling_devices" = "" ]; then
588 log_skip "no cooling devices"
589 return 0
590 fi
591
592 for cooling_device in $cooling_devices; do
593 INC=0
594 $cdev_func $cooling_device $@
595 done
596
597 return 0
598}
599check_scaling_freq() {
600
601 before_freq_list=$1
602 after_freq_list=$2
603 shift 2
604 index=0
605
606 flag=0
607 for cpu in $cpus; do
608 after_freq=$(eval echo \$$after_freq_list$index)
609 before_freq=$(eval echo \$$before_freq_list$index)
610
611 if [ $after_freq -ne $before_freq ]; then
612 flag=1
613 fi
614
615 index=$((index + 1))
616 done
617
618 return $flag
619}
620
621store_scaling_maxfreq() {
622 index=0
623
624 for cpu in $cpus; do
625 scaling_freq_max_value=$(cat $CPU_PATH/$cpu/cpufreq/scaling_max_freq)
626 eval $scaling_freq_array$index=$scaling_freq_max_value
627 eval echo $scaling_freq_array$index
628 done
629
630 return 0
631}
632
633get_trip_id() {
634
635 trip_name=$1
636 shift 1
637
638 id1=$(echo $trip_name|cut -c12)
639 id2=$(echo $trip_name|cut -c13)
640 if [ $id2 != "_" ]; then
641 id1=$(($id2 + 10*$id1))
642 fi
643 return $id1
644}
645
646disable_all_thermal_zones() {
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800647 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']")
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800648 index=0
649
650 for thermal_zone in $thermal_zones; do
651 mode=$(cat $THERMAL_PATH/$thermal_zone/mode)
652 eval $mode_array$index=$mode
653 eval export $mode_array$index
654 index=$((index + 1))
655 echo -n "disabled" > $THERMAL_PATH/$thermal_zone/mode
656 done
657
658 return 0
659}
660
661enable_all_thermal_zones() {
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800662 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']")
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800663 index=0
664
665 for thermal_zone in $thermal_zones; do
666 mode=$(eval echo \$$mode_array$index)
667 echo $mode > $THERMAL_PATH/$thermal_zone/mode
668 index=$((index + 1))
669 done
670
671 return 0
672}
673
674GPU_HEAT_BIN=/usr/bin/glmark2
675gpu_pid=0
676
677start_glmark2() {
678 if [ -n "$ANDROID" ]; then
679 am start org.linaro.glmark2/.Glmark2Activity
680 return
681 fi
682
683 if [ -x $GPU_HEAT_BIN ]; then
684 $GPU_HEAT_BIN &
685 gpu_pid=$(pidof $GPU_HEAT_BIN)
686 # Starting X application from serial console needs this
687 if [ -z "$gpu_pid" ]; then
688 cp /etc/lightdm/lightdm.conf /etc/lightdm/lightdm.conf.bk
689 echo "autologin-user=root" >> /etc/lightdm/lightdm.conf
690 export DISPLAY=localhost:0.0
691 restart lightdm
692 sleep 5
693 mv /etc/lightdm/lightdm.conf.bk /etc/lightdm/lightdm.conf
694 $GPU_HEAT_BIN &
695 gpu_pid=$(pidof $GPU_HEAT_BIN)
696 fi
697 test -z "$gpu_pid" && cpu_pid=0
698 echo "start gpu heat binary $gpu_pid"
699 else
700 echo "glmark2 not found." 1>&2
701 fi
702}
703
704kill_glmark2() {
705 if [ -n "$ANDROID" ]; then
706 am kill org.linaro.glmark2
707 return
708 fi
709
710 if [ "$gpu_pid" -ne 0 ]; then
711 kill -9 $gpu_pid
712 fi
713}
714
715set_thermal_governors() {
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800716 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']")
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800717 gov=$1
718 index=0
719
720 for thermal_zone in $thermal_zones; do
721 policy=$(cat $THERMAL_PATH/$thermal_zone/policy)
722 eval $thermal_gov_array$index=$policy
723 eval export $thermal_gov_array$index
724 index=$((index + 1))
725 echo $gov > $THERMAL_PATH/$thermal_zone/policy
726 done
727
728 return 0
729}
730
731restore_thermal_governors() {
Lisa Nguyenbab54c02015-11-17 17:03:02 -0800732 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']")
Lisa Nguyend39d0bb2015-11-16 22:39:25 -0800733 index=0
734
735 for thermal_zone in $thermal_zones; do
736 old_policy=$(eval echo \$$thermal_gov_array$index)
737 echo $old_policy > $THERMAL_PATH/$thermal_zone/policy
738 index=$((index + 1))
739 done
740
741 return 0
742}
Lisa Nguyenddd07af2015-11-17 23:15:22 -0800743
744check_for_thermal_zones()
745{
746 thermal_zones=$(ls $THERMAL_PATH | grep "thermal_zone['$MAX_ZONE']" 2>/dev/null)
747 if [ ! -z "$thermal_zones" ]; then
748 return 0
749 else
750 return 1
751 fi
752}
Lisa Nguyen3ab9ffd2016-01-25 12:16:18 -0800753
754check_logdir()
755{
756 if [ ! -f $LOGDIR ]; then
757 mkdir -p $LOGDIR
758 fi
759}
760
761setup_wakeup_timer ()
762{
763 timeout="$1"
764
765 # Request wakeup from the RTC or ACPI alarm timers. Set the timeout
766 # at 'now' + $timeout seconds.
767 ctl='/sys/class/rtc/rtc0/wakealarm'
768 if [ -f "$ctl" ]; then
769 # Cancel any outstanding timers.
770 echo "0" >"$ctl"
771 # rtcN/wakealarm can use relative time in seconds
772 echo "+$timeout" >"$ctl"
773 return 0
774 fi
775 ctl='/proc/acpi/alarm'
776 if [ -f "$ctl" ]; then
777 echo `date '+%F %H:%M:%S' -d '+ '$timeout' seconds'` >"$ctl"
778 return 0
779 fi
780
781 echo "no method to awaken machine automatically" 1>&2
782 exit 1
783}
784
785suspend_system ()
786{
787 if [ "$dry" -eq 1 ]; then
788 echo "DRY-RUN: suspend machine for $timer_sleep"
789 sleep 1
790 return
791 fi
792
793 setup_wakeup_timer "$timer_sleep"
794
795 dmesg >"$LOGFILE.dmesg.A"
796
797 # Initiate suspend in different ways.
798 case "$1" in
799 dbus)
800 dbus-send --session --type=method_call \
801 --dest=org.freedesktop.PowerManagement \
802 /org/freedesktop/PowerManagement \
803 org.freedesktop.PowerManagement.Suspend \
804 >> "$LOGFILE" || {
805 ECHO "FAILED: dbus suspend failed" 1>&2
806 return 1
807 }
808 ;;
809 pmsuspend)
810 pm-suspend >> "$LOGFILE"
811 ;;
812 mem)
813 `echo "mem" > /sys/power/state` >> "$LOGFILE"
814 ;;
815 esac
816
817 # Wait on the machine coming back up -- pulling the dmesg over.
818 echo "v---" >>"$LOGFILE"
819 retry=30
820 while [ "$retry" -gt 0 ]; do
821 retry=$((retry - 1))
822
823 # Accumulate the dmesg delta.
824 dmesg >"$LOGFILE.dmesg.B"
825 diff "$LOGFILE.dmesg.A" "$LOGFILE.dmesg.B" | \
826 grep '^>' >"$LOGFILE.dmesg"
827 mv "$LOGFILE.dmesg.B" "$LOGFILE.dmesg.A"
828
829 echo "Waiting for suspend to complete $retry to go ..." \
830 >> "$LOGFILE"
831 cat "$LOGFILE.dmesg" >> "$LOGFILE"
832
833 if [ "`grep -c 'Back to C!' $LOGFILE.dmesg`" -ne 0 ]; then
834 break;
835 fi
836 sleep 1
837 done
838 echo "^---" >>"$LOGFILE"
839 rm -f "$LOGFILE.dmesg"*
840 if [ "$retry" -eq 0 ]; then
841 ECHO "SUSPEND FAILED, did not go to sleep" 1>&2
842 return 1
843 fi
844}
845
846ECHO ()
847{
848 echo "$@" | tee -a "$LOGFILE"
849}
850
851enable_trace()
852{
853 if [ -w /sys/power/pm_trace ]; then
854 echo 1 > '/sys/power/pm_trace'
855 fi
856}
857
858disable_trace()
859{
860 if [ -w /sys/power/pm_trace ]; then
861 echo 0 > '/sys/power/pm_trace'
862 fi
863}
864
865trace_state=-1
866
867save_trace()
868{
869 if [ -r /sys/power/pm_trace ]; then
870 trace_state=`cat /sys/power/pm_trace`
871 fi
872}
873
874restore_trace()
875{
876 if [ "$trace_state" -ne -1 -a -w /sys/power/pm_trace ]; then
877 echo "$trace_state" > '/sys/power/pm_trace'
878 fi
879}
880
881battery_count()
882{
883 cat /proc/acpi/battery/*/state 2>/dev/null | \
884 awk '
885 BEGIN { total = 0 }
886 /present:.*yes/ { total += 1 }
887 END { print total }
888 '
889}
890
891battery_capacity()
892{
893 cat /proc/acpi/battery/*/state 2>/dev/null | \
894 awk '
895 BEGIN { total = 0 }
896 /remaining capacity:/ { total += $3 }
897 END { print total }
898 '
899}
900
901ac_needed=-1
902ac_is=-1
903ac_becomes=-1
904
905ac_required()
906{
907 ac_check
908
909 ac_needed="$1"
910 ac_becomes="$1"
911}
912
913ac_transitions()
914{
915 ac_check
916
917 ac_needed="$1"
918 ac_becomes="$2"
919}
920
921ac_online()
922{
923 cat /proc/acpi/ac_adapter/*/state 2>/dev/null | \
924 awk '
925 BEGIN { online = 0; offline = 0 }
926 /on-line/ { online = 1 }
927 /off-line/ { offline = 1 }
928 END {
929 if (online) {
930 print "1"
931 } else if (offline) {
932 print "0"
933 } else {
934 print "-1"
935 }
936 }
937 '
938}
939
940ac_check()
941{
942 ac_current=`ac_online`
943
944 if [ "$ac_becomes" -ne -1 -a "$ac_current" -ne -1 -a \
945 "$ac_current" -ne "$ac_becomes" ]; then
946 ECHO "*** WARNING: AC power not in expected state" \
947 "($ac_becomes) after test"
948 fi
949 ac_is="$ac_becomes"
950}
951
952phase=0
953phase_first=1
954phase_interactive=1
955
956phase()
957{
958 phase=$((phase + 1))
959
960 if [ "$ac_needed" -ne "$ac_is" ]; then
961 case "$ac_needed" in
962 0) echo "*** please ensure your AC cord is detached" ;;
963 1) echo "*** please ensure your AC cord is attached" ;;
964 esac
965 ac_is="$ac_needed"
966 fi
967
968 if [ "$timer_sleep" -gt 60 ]; then
969 sleep="$timer_sleep / 60"
970 sleep="$sleep minutes"
971 else
972 sleep="$timer_sleep seconds"
973 fi
974 echo "*** machine will suspend for $sleep"
975
976 if [ "$auto" -eq 1 ]; then
977 :
978 elif [ "$phase_interactive" -eq 1 ]; then
979 echo "*** press return when ready"
980 read x
981
982 elif [ "$phase_first" -eq 1 ]; then
983 echo "*** NOTE: there will be no further user interaction from this point"
984 echo "*** press return when ready"
985 phase_first=0
986 read x
987 fi
988}
989
990save_trace
991
992if [ "$pm_trace" -eq 1 ]; then
993 enable_trace
994else
995 disable_trace
996fi