blob: 5823a81812dbe77ef3fe34185eefa2bdb41b59bf [file] [log] [blame]
Daniel Lezcanod84ec062011-07-26 14:38:59 +02001#!/bin/bash
2#
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
Sanjay Singh Rawatb579c102013-06-19 18:59:36 +053026source ../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
32CPU=
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053033pass_count=0
34fail_count=0
35
36test_status_show() {
37 echo "-------- total = $(($pass_count + $fail_count))"
38 echo "-------- pass = $pass_count"
39 # report failure only if it is there
40 if [ $fail_count -ne 0 ] ; then
41 echo "-------- fail = $fail_count"
42 fi
43}
Daniel Lezcanod84ec062011-07-26 14:38:59 +020044
45log_begin() {
Daniel Lezcano994f6f12011-08-16 13:28:33 +020046 printf "%-76s" "$TEST_NAME.$INC$CPU: $@... "
Daniel Lezcano1efddc12011-08-09 23:45:30 +020047 INC=$(($INC+1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +020048}
49
50log_end() {
51 printf "$*\n"
52}
53
54log_skip() {
55 log_begin "$@"
Daniel Lezcanoff4aa112011-08-16 15:48:38 +020056 log_end "skip"
Daniel Lezcanod84ec062011-07-26 14:38:59 +020057}
58
59for_each_cpu() {
60
61 local func=$1
62 shift 1
63
64 cpus=$(ls $CPU_PATH | grep "cpu[0-9].*")
Daniel Lezcanod84ec062011-07-26 14:38:59 +020065 for cpu in $cpus; do
Daniel Lezcano1efddc12011-08-09 23:45:30 +020066 INC=0
67 CPU=/$cpu
Daniel Lezcanod84ec062011-07-26 14:38:59 +020068 $func $cpu $@
69 done
70
71 return 0
72}
73
74for_each_governor() {
75
76 local cpu=$1
77 local func=$2
78 local dirpath=$CPU_PATH/$cpu/cpufreq
79 local governors=$(cat $dirpath/scaling_available_governors)
80 shift 2
81
82 for governor in $governors; do
83 $func $cpu $governor $@
84 done
85
86 return 0
87}
88
89for_each_frequency() {
90
91 local cpu=$1
92 local func=$2
93 local dirpath=$CPU_PATH/$cpu/cpufreq
94 local frequencies=$(cat $dirpath/scaling_available_frequencies)
95 shift 2
96
97 for frequency in $frequencies; do
98 $func $cpu $frequency $@
99 done
100
101 return 0
102}
103
104set_governor() {
105
106 local cpu=$1
107 local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor
108 local newgov=$2
109
110 echo $newgov > $dirpath
111}
112
113get_governor() {
114
115 local cpu=$1
116 local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor
117
118 cat $dirpath
119}
120
121wait_latency() {
122 local cpu=$1
123 local dirpath=$CPU_PATH/$cpu/cpufreq
Daniel Lezcano93382952011-09-19 11:41:39 +0200124 local latency=
125 local nrfreq=
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530126 local sampling_rate=
127 local sleep_time=
128 local gov=$(cat $dirpath/scaling_governor)
129
130 # consider per-policy governor case
131 if [ -e $CPU_PATH/$cpu/cpufreq/$gov ]; then
132 sampling_rate=$(cat $CPU_PATH/$cpu/cpufreq/$gov/sampling_rate)
133 else
134 sampling_rate=$(cat $CPU_PATH/cpufreq/$gov/sampling_rate)
135 fi
136 sampling_rate=$((sampling_rate * 1000)) # unit nsec
Daniel Lezcano93382952011-09-19 11:41:39 +0200137
138 latency=$(cat $dirpath/cpuinfo_transition_latency)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530139 if [ $? -ne 0 ]; then
Daniel Lezcano0b90d412011-09-20 08:31:04 +0200140 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200141 fi
142
143 nrfreq=$(cat $dirpath/scaling_available_frequencies | wc -w)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530144 if [ $? -ne 0 ]; then
Daniel Lezcano0b90d412011-09-20 08:31:04 +0200145 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200146 fi
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200147
148 nrfreq=$((nrfreq + 1))
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530149
150 sleep_time=$(($latency + $sampling_rate))
151
152 ../utils/nanosleep $(($nrfreq * $sleep_time))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200153}
154
155frequnit() {
156 local freq=$1
157 local ghz=$(echo "scale=1;($freq / 1000000)" | bc -l)
158 local mhz=$(echo "scale=1;($freq / 1000)" | bc -l)
159
160 res=$(echo "($ghz > 1.0)" | bc -l)
161 if [ "$res" = "1" ]; then
162 echo $ghz GHz
163 return 0
164 fi
165
166 res=$(echo "($mhz > 1.0)" | bc -l)
167 if [ "$res" = "1" ];then
168 echo $mhz MHz
169 return 0
170 fi
171
172 echo $freq KHz
173}
174
175set_frequency() {
176
177 local cpu=$1
178 local dirpath=$CPU_PATH/$cpu/cpufreq
179 local newfreq=$2
180 local setfreqpath=$dirpath/scaling_setspeed
181
182 echo $newfreq > $setfreqpath
183 wait_latency $cpu
184}
185
186get_frequency() {
187 local cpu=$1
188 local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_cur_freq
189 cat $dirpath
190}
191
192get_max_frequency() {
193 local cpu=$1
194 local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_max_freq
195 cat $dirpath
196}
197
198get_min_frequency() {
199 local cpu=$1
200 local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_min_freq
201 cat $dirpath
202}
203
Daniel Lezcano805e3352011-10-03 11:07:36 +0200204set_online() {
205 local cpu=$1
206 local dirpath=$CPU_PATH/$cpu
207
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200208 if [ "$cpu" = "cpu0" ]; then
209 return 0
210 fi
211
Daniel Lezcano805e3352011-10-03 11:07:36 +0200212 echo 1 > $dirpath/online
213}
214
215set_offline() {
216 local cpu=$1
217 local dirpath=$CPU_PATH/$cpu
218
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200219 if [ "$cpu" = "cpu0" ]; then
220 return 0
221 fi
222
Daniel Lezcano805e3352011-10-03 11:07:36 +0200223 echo 0 > $dirpath/online
224}
225
226get_online() {
227 local cpu=$1
228 local dirpath=$CPU_PATH/$cpu
229
230 cat $dirpath/online
231}
232
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200233check() {
234
235 local descr=$1
236 local func=$2
237 shift 2;
238
239 log_begin "checking $descr"
240
241 $func $@
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530242 if [ $? -ne 0 ]; then
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530243 log_end "Err"
244 fail_count=$(($fail_count + 1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200245 return 1
246 fi
247
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530248 log_end "Ok"
249 pass_count=$(($pass_count + 1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200250
251 return 0
252}
253
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200254check_file() {
255 local file=$1
256 local dir=$2
257
Sanjay Singh Rawat8b6ad7c2014-07-08 16:35:36 +0200258 check "'$file' exists in '$dir'" "test -f" $dir/$file
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200259}
260
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200261check_cpufreq_files() {
262
263 local dirpath=$CPU_PATH/$1/cpufreq
264 shift 1
265
266 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200267 check_file $i $dirpath || return 1
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200268 done
269
270 return 0
271}
272
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200273check_sched_mc_files() {
274
275 local dirpath=$CPU_PATH
276
277 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200278 check_file $i $dirpath || return 1
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200279 done
280
281 return 0
282}
283
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200284check_topology_files() {
285
286 local dirpath=$CPU_PATH/$1/topology
287 shift 1
288
289 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200290 check_file $i $dirpath || return 1
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200291 done
292
293 return 0
294}
295
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200296check_cpuhotplug_files() {
297
298 local dirpath=$CPU_PATH/$1
299 shift 1
300
301 for i in $@; do
Lisa Nguyene2323182014-07-04 10:24:30 +0530302 if [ `echo $dirpath | grep -c "cpu0"` -eq 1 ]; then
303 if [ $hotplug_allow_cpu0 -eq 0 ]; then
304 continue
305 fi
306 fi
307
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200308 check_file $i $dirpath || return 1
309 done
310
311 return 0
312}
313
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200314save_governors() {
315
316 governors_backup=
317 local index=0
318
319 for i in $(ls $CPU_PATH | grep "cpu[0-9].*"); do
320 governors_backup[$index]=$(cat $CPU_PATH/$i/cpufreq/scaling_governor)
321 index=$((index + 1))
322 done
323}
324
325restore_governors() {
326
327 local index=0
328 local oldgov=
329
330 for i in $(ls $CPU_PATH | grep "cpu[0-9].*"); do
331 oldgov=${governors_backup[$index]}
332 echo $oldgov > $CPU_PATH/$i/cpufreq/scaling_governor
333 index=$((index + 1))
334 done
335}
336
337save_frequencies() {
338
339 frequencies_backup=
340 local index=0
341 local cpus=$(ls $CPU_PATH | grep "cpu[0-9].*")
342 local cpu=
343
344 for cpu in $cpus; do
345 frequencies_backup[$index]=$(cat $CPU_PATH/$cpu/cpufreq/scaling_cur_freq)
346 index=$((index + 1))
347 done
348}
349
350restore_frequencies() {
351
352 local index=0
353 local oldfreq=
354 local cpus=$(ls $CPU_PATH | grep "cpu[0-9].*")
355
356 for cpu in $cpus; do
357 oldfreq=${frequencies_backup[$index]}
358 echo $oldfreq > $CPU_PATH/$cpu/cpufreq/scaling_setspeed
359 index=$((index + 1))
360 done
361}
362
363sigtrap() {
364 exit 255
Daniel Lezcano18e22b52011-08-06 02:52:02 +0200365}
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530366
367# currently we support ubuntu and android
368get_os() {
369 lsb_release -a 2>&1 | grep -i ubuntu > /dev/null
370 if [ $? -eq 0 ]; then
371 # for ubuntu
372 return 1
373 else
374 # for android (if needed can look for build.prop)
375 return 2
376 fi
377}
378
379is_root() {
380 local ret
381 get_os
382 if [ $? -eq 1 ]; then
383 # for ubuntu
384 ret=$(id -u)
385 else
386 # for android
387 ret=$(id | sed -n 's/uid=//p' | sed -n 's/(root) [a-z]*=[0-9]*(log)//p')
388 fi
389 return $ret
390}
Lisa Nguyene2323182014-07-04 10:24:30 +0530391
392is_cpu0_hotplug_allowed() {
393 local status=$1
394
395 if [ $status -eq 1 ]; then
396 return 0
397 else
398 return 1
399 fi
400}