blob: 31634563781be8e794bfc2a613637bbabfbbb3c5 [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=
Lisa Nguyen523e8d92014-09-04 17:26:03 -070033cpus=$(ls $CPU_PATH | grep "cpu[0-9].*")
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +053034pass_count=0
35fail_count=0
Lisa Nguyene2877d52014-08-04 12:30:43 -070036skip_count=0
37test_script_status="pass"
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 " "
53 if [[ "$test_script_status" == "fail" ]]; then
54 echo "$TEST_NAME: fail"
55 elif [[ "$test_script_status" == "skip" ]]; then
56 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
63log_begin() {
Daniel Lezcano994f6f12011-08-16 13:28:33 +020064 printf "%-76s" "$TEST_NAME.$INC$CPU: $@... "
Daniel Lezcano1efddc12011-08-09 23:45:30 +020065 INC=$(($INC+1))
Daniel Lezcanod84ec062011-07-26 14:38:59 +020066}
67
68log_end() {
69 printf "$*\n"
Lisa Nguyene2877d52014-08-04 12:30:43 -070070
71 if [[ "$*" == "Err" ]]; then
72 fail_count=$((fail_count + 1))
73 elif [[ "$*" == "skip" ]]; then
74 skip_count=$((skip_count + 1))
75 else
76 pass_count=$((pass_count + 1))
77 fi
Daniel Lezcanod84ec062011-07-26 14:38:59 +020078}
79
80log_skip() {
81 log_begin "$@"
Daniel Lezcanoff4aa112011-08-16 15:48:38 +020082 log_end "skip"
Daniel Lezcanod84ec062011-07-26 14:38:59 +020083}
84
85for_each_cpu() {
86
87 local func=$1
88 shift 1
89
Daniel Lezcanod84ec062011-07-26 14:38:59 +020090 for cpu in $cpus; do
Daniel Lezcano1efddc12011-08-09 23:45:30 +020091 INC=0
92 CPU=/$cpu
Daniel Lezcanod84ec062011-07-26 14:38:59 +020093 $func $cpu $@
94 done
95
96 return 0
97}
98
99for_each_governor() {
100
101 local cpu=$1
102 local func=$2
103 local dirpath=$CPU_PATH/$cpu/cpufreq
104 local governors=$(cat $dirpath/scaling_available_governors)
105 shift 2
106
107 for governor in $governors; do
108 $func $cpu $governor $@
109 done
110
111 return 0
112}
113
114for_each_frequency() {
115
116 local cpu=$1
117 local func=$2
118 local dirpath=$CPU_PATH/$cpu/cpufreq
119 local frequencies=$(cat $dirpath/scaling_available_frequencies)
120 shift 2
121
122 for frequency in $frequencies; do
123 $func $cpu $frequency $@
124 done
125
126 return 0
127}
128
129set_governor() {
130
131 local cpu=$1
132 local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor
133 local newgov=$2
134
135 echo $newgov > $dirpath
136}
137
138get_governor() {
139
140 local cpu=$1
141 local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor
142
143 cat $dirpath
144}
145
146wait_latency() {
147 local cpu=$1
148 local dirpath=$CPU_PATH/$cpu/cpufreq
Daniel Lezcano93382952011-09-19 11:41:39 +0200149 local latency=
150 local nrfreq=
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530151 local sampling_rate=
152 local sleep_time=
153 local gov=$(cat $dirpath/scaling_governor)
154
155 # consider per-policy governor case
156 if [ -e $CPU_PATH/$cpu/cpufreq/$gov ]; then
157 sampling_rate=$(cat $CPU_PATH/$cpu/cpufreq/$gov/sampling_rate)
158 else
159 sampling_rate=$(cat $CPU_PATH/cpufreq/$gov/sampling_rate)
160 fi
161 sampling_rate=$((sampling_rate * 1000)) # unit nsec
Daniel Lezcano93382952011-09-19 11:41:39 +0200162
163 latency=$(cat $dirpath/cpuinfo_transition_latency)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530164 if [ $? -ne 0 ]; then
Daniel Lezcano0b90d412011-09-20 08:31:04 +0200165 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200166 fi
167
168 nrfreq=$(cat $dirpath/scaling_available_frequencies | wc -w)
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530169 if [ $? -ne 0 ]; then
Daniel Lezcano0b90d412011-09-20 08:31:04 +0200170 return 1
Daniel Lezcano93382952011-09-19 11:41:39 +0200171 fi
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200172
173 nrfreq=$((nrfreq + 1))
Sanjay Singh Rawate5726c12014-08-05 13:39:06 +0530174
175 sleep_time=$(($latency + $sampling_rate))
176
177 ../utils/nanosleep $(($nrfreq * $sleep_time))
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200178}
179
180frequnit() {
181 local freq=$1
182 local ghz=$(echo "scale=1;($freq / 1000000)" | bc -l)
183 local mhz=$(echo "scale=1;($freq / 1000)" | bc -l)
184
185 res=$(echo "($ghz > 1.0)" | bc -l)
186 if [ "$res" = "1" ]; then
187 echo $ghz GHz
188 return 0
189 fi
190
191 res=$(echo "($mhz > 1.0)" | bc -l)
192 if [ "$res" = "1" ];then
193 echo $mhz MHz
194 return 0
195 fi
196
197 echo $freq KHz
198}
199
200set_frequency() {
201
202 local cpu=$1
203 local dirpath=$CPU_PATH/$cpu/cpufreq
204 local newfreq=$2
205 local setfreqpath=$dirpath/scaling_setspeed
206
207 echo $newfreq > $setfreqpath
208 wait_latency $cpu
209}
210
211get_frequency() {
212 local cpu=$1
213 local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_cur_freq
214 cat $dirpath
215}
216
217get_max_frequency() {
218 local cpu=$1
219 local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_max_freq
220 cat $dirpath
221}
222
223get_min_frequency() {
224 local cpu=$1
225 local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_min_freq
226 cat $dirpath
227}
228
Daniel Lezcano805e3352011-10-03 11:07:36 +0200229set_online() {
230 local cpu=$1
231 local dirpath=$CPU_PATH/$cpu
232
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200233 if [ "$cpu" = "cpu0" ]; then
234 return 0
235 fi
236
Daniel Lezcano805e3352011-10-03 11:07:36 +0200237 echo 1 > $dirpath/online
238}
239
240set_offline() {
241 local cpu=$1
242 local dirpath=$CPU_PATH/$cpu
243
Daniel Lezcano3dd53ee2012-04-05 14:03:08 +0200244 if [ "$cpu" = "cpu0" ]; then
245 return 0
246 fi
247
Daniel Lezcano805e3352011-10-03 11:07:36 +0200248 echo 0 > $dirpath/online
249}
250
251get_online() {
252 local cpu=$1
253 local dirpath=$CPU_PATH/$cpu
254
255 cat $dirpath/online
256}
257
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200258check() {
259
260 local descr=$1
261 local func=$2
262 shift 2;
263
264 log_begin "checking $descr"
265
266 $func $@
Sanjay Singh Rawatc7af87b2013-12-15 13:30:37 +0530267 if [ $? -ne 0 ]; then
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530268 log_end "Err"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200269 return 1
270 fi
271
Sanjay Singh Rawat3bf61942013-04-10 14:06:14 +0530272 log_end "Ok"
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200273
274 return 0
275}
276
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200277check_file() {
278 local file=$1
279 local dir=$2
280
Sanjay Singh Rawat8b6ad7c2014-07-08 16:35:36 +0200281 check "'$file' exists in '$dir'" "test -f" $dir/$file
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200282}
283
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200284check_cpufreq_files() {
285
286 local dirpath=$CPU_PATH/$1/cpufreq
287 shift 1
288
289 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200290 check_file $i $dirpath || return 1
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200291 done
292
293 return 0
294}
295
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200296check_sched_mc_files() {
297
298 local dirpath=$CPU_PATH
299
300 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200301 check_file $i $dirpath || return 1
Daniel Lezcanoa7da5202011-08-05 15:01:42 +0200302 done
303
304 return 0
305}
306
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200307check_topology_files() {
308
309 local dirpath=$CPU_PATH/$1/topology
310 shift 1
311
312 for i in $@; do
Daniel Lezcanob6a31192011-10-03 11:07:36 +0200313 check_file $i $dirpath || return 1
Daniel Lezcanobc043cb2011-08-05 15:01:42 +0200314 done
315
316 return 0
317}
318
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200319check_cpuhotplug_files() {
320
321 local dirpath=$CPU_PATH/$1
322 shift 1
323
324 for i in $@; do
Lisa Nguyene2323182014-07-04 10:24:30 +0530325 if [ `echo $dirpath | grep -c "cpu0"` -eq 1 ]; then
326 if [ $hotplug_allow_cpu0 -eq 0 ]; then
327 continue
328 fi
329 fi
330
Daniel Lezcanob10475e2011-10-03 11:07:36 +0200331 check_file $i $dirpath || return 1
332 done
333
334 return 0
335}
336
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200337save_governors() {
338
339 governors_backup=
340 local index=0
341
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700342 for cpu in $cpus; do
343 governors_backup[$index]=$(cat $CPU_PATH/$cpu/cpufreq/scaling_governor)
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200344 index=$((index + 1))
345 done
346}
347
348restore_governors() {
349
350 local index=0
351 local oldgov=
352
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700353 for cpu in $cpus; do
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200354 oldgov=${governors_backup[$index]}
Lisa Nguyen188ca9a2014-08-11 12:21:08 -0700355 echo $oldgov > $CPU_PATH/$cpu/cpufreq/scaling_governor
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200356 index=$((index + 1))
357 done
358}
359
360save_frequencies() {
361
362 frequencies_backup=
363 local index=0
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200364
365 for cpu in $cpus; do
366 frequencies_backup[$index]=$(cat $CPU_PATH/$cpu/cpufreq/scaling_cur_freq)
367 index=$((index + 1))
368 done
369}
370
371restore_frequencies() {
372
373 local index=0
374 local oldfreq=
Daniel Lezcanod84ec062011-07-26 14:38:59 +0200375
376 for cpu in $cpus; do
377 oldfreq=${frequencies_backup[$index]}
378 echo $oldfreq > $CPU_PATH/$cpu/cpufreq/scaling_setspeed
379 index=$((index + 1))
380 done
381}
382
383sigtrap() {
384 exit 255
Daniel Lezcano18e22b52011-08-06 02:52:02 +0200385}
Sanjay Singh Rawatd5963e52014-02-05 18:12:17 +0530386
387# currently we support ubuntu and android
388get_os() {
389 lsb_release -a 2>&1 | grep -i ubuntu > /dev/null
390 if [ $? -eq 0 ]; then
391 # for ubuntu
392 return 1
393 else
394 # for android (if needed can look for build.prop)
395 return 2
396 fi
397}
398
399is_root() {
400 local ret
401 get_os
402 if [ $? -eq 1 ]; then
403 # for ubuntu
404 ret=$(id -u)
405 else
406 # for android
407 ret=$(id | sed -n 's/uid=//p' | sed -n 's/(root) [a-z]*=[0-9]*(log)//p')
408 fi
409 return $ret
410}
Lisa Nguyene2323182014-07-04 10:24:30 +0530411
412is_cpu0_hotplug_allowed() {
413 local status=$1
414
415 if [ $status -eq 1 ]; then
416 return 0
417 else
418 return 1
419 fi
420}