Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
Daniel Lezcano | 35fb172 | 2011-10-03 13:56:11 +0200 | [diff] [blame] | 3 | # PM-QA validation test suite for the power management on Linux |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 4 | # |
| 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 Rawat | b579c10 | 2013-06-19 18:59:36 +0530 | [diff] [blame] | 26 | source ../Switches.sh |
Hongbo Zhang | 974802b | 2013-02-04 19:22:58 +0800 | [diff] [blame] | 27 | |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 28 | CPU_PATH="/sys/devices/system/cpu" |
| 29 | TEST_NAME=$(basename ${0%.sh}) |
Daniel Lezcano | 1efddc1 | 2011-08-09 23:45:30 +0200 | [diff] [blame] | 30 | PREFIX=$TEST_NAME |
| 31 | INC=0 |
| 32 | CPU= |
Lisa Nguyen | 523e8d9 | 2014-09-04 17:26:03 -0700 | [diff] [blame^] | 33 | cpus=$(ls $CPU_PATH | grep "cpu[0-9].*") |
Sanjay Singh Rawat | 3bf6194 | 2013-04-10 14:06:14 +0530 | [diff] [blame] | 34 | pass_count=0 |
| 35 | fail_count=0 |
Lisa Nguyen | e2877d5 | 2014-08-04 12:30:43 -0700 | [diff] [blame] | 36 | skip_count=0 |
| 37 | test_script_status="pass" |
Sanjay Singh Rawat | 3bf6194 | 2013-04-10 14:06:14 +0530 | [diff] [blame] | 38 | |
| 39 | test_status_show() { |
Lisa Nguyen | e2877d5 | 2014-08-04 12:30:43 -0700 | [diff] [blame] | 40 | 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 Rawat | 3bf6194 | 2013-04-10 14:06:14 +0530 | [diff] [blame] | 50 | fi |
Lisa Nguyen | e2877d5 | 2014-08-04 12:30:43 -0700 | [diff] [blame] | 51 | |
| 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 Rawat | 3bf6194 | 2013-04-10 14:06:14 +0530 | [diff] [blame] | 61 | } |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 62 | |
| 63 | log_begin() { |
Daniel Lezcano | 994f6f1 | 2011-08-16 13:28:33 +0200 | [diff] [blame] | 64 | printf "%-76s" "$TEST_NAME.$INC$CPU: $@... " |
Daniel Lezcano | 1efddc1 | 2011-08-09 23:45:30 +0200 | [diff] [blame] | 65 | INC=$(($INC+1)) |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | log_end() { |
| 69 | printf "$*\n" |
Lisa Nguyen | e2877d5 | 2014-08-04 12:30:43 -0700 | [diff] [blame] | 70 | |
| 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 Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | log_skip() { |
| 81 | log_begin "$@" |
Daniel Lezcano | ff4aa11 | 2011-08-16 15:48:38 +0200 | [diff] [blame] | 82 | log_end "skip" |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | for_each_cpu() { |
| 86 | |
| 87 | local func=$1 |
| 88 | shift 1 |
| 89 | |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 90 | for cpu in $cpus; do |
Daniel Lezcano | 1efddc1 | 2011-08-09 23:45:30 +0200 | [diff] [blame] | 91 | INC=0 |
| 92 | CPU=/$cpu |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 93 | $func $cpu $@ |
| 94 | done |
| 95 | |
| 96 | return 0 |
| 97 | } |
| 98 | |
| 99 | for_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 | |
| 114 | for_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 | |
| 129 | set_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 | |
| 138 | get_governor() { |
| 139 | |
| 140 | local cpu=$1 |
| 141 | local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor |
| 142 | |
| 143 | cat $dirpath |
| 144 | } |
| 145 | |
| 146 | wait_latency() { |
| 147 | local cpu=$1 |
| 148 | local dirpath=$CPU_PATH/$cpu/cpufreq |
Daniel Lezcano | 9338295 | 2011-09-19 11:41:39 +0200 | [diff] [blame] | 149 | local latency= |
| 150 | local nrfreq= |
Sanjay Singh Rawat | e5726c1 | 2014-08-05 13:39:06 +0530 | [diff] [blame] | 151 | 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 Lezcano | 9338295 | 2011-09-19 11:41:39 +0200 | [diff] [blame] | 162 | |
| 163 | latency=$(cat $dirpath/cpuinfo_transition_latency) |
Sanjay Singh Rawat | c7af87b | 2013-12-15 13:30:37 +0530 | [diff] [blame] | 164 | if [ $? -ne 0 ]; then |
Daniel Lezcano | 0b90d41 | 2011-09-20 08:31:04 +0200 | [diff] [blame] | 165 | return 1 |
Daniel Lezcano | 9338295 | 2011-09-19 11:41:39 +0200 | [diff] [blame] | 166 | fi |
| 167 | |
| 168 | nrfreq=$(cat $dirpath/scaling_available_frequencies | wc -w) |
Sanjay Singh Rawat | c7af87b | 2013-12-15 13:30:37 +0530 | [diff] [blame] | 169 | if [ $? -ne 0 ]; then |
Daniel Lezcano | 0b90d41 | 2011-09-20 08:31:04 +0200 | [diff] [blame] | 170 | return 1 |
Daniel Lezcano | 9338295 | 2011-09-19 11:41:39 +0200 | [diff] [blame] | 171 | fi |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 172 | |
| 173 | nrfreq=$((nrfreq + 1)) |
Sanjay Singh Rawat | e5726c1 | 2014-08-05 13:39:06 +0530 | [diff] [blame] | 174 | |
| 175 | sleep_time=$(($latency + $sampling_rate)) |
| 176 | |
| 177 | ../utils/nanosleep $(($nrfreq * $sleep_time)) |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | frequnit() { |
| 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 | |
| 200 | set_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 | |
| 211 | get_frequency() { |
| 212 | local cpu=$1 |
| 213 | local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_cur_freq |
| 214 | cat $dirpath |
| 215 | } |
| 216 | |
| 217 | get_max_frequency() { |
| 218 | local cpu=$1 |
| 219 | local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_max_freq |
| 220 | cat $dirpath |
| 221 | } |
| 222 | |
| 223 | get_min_frequency() { |
| 224 | local cpu=$1 |
| 225 | local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_min_freq |
| 226 | cat $dirpath |
| 227 | } |
| 228 | |
Daniel Lezcano | 805e335 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 229 | set_online() { |
| 230 | local cpu=$1 |
| 231 | local dirpath=$CPU_PATH/$cpu |
| 232 | |
Daniel Lezcano | 3dd53ee | 2012-04-05 14:03:08 +0200 | [diff] [blame] | 233 | if [ "$cpu" = "cpu0" ]; then |
| 234 | return 0 |
| 235 | fi |
| 236 | |
Daniel Lezcano | 805e335 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 237 | echo 1 > $dirpath/online |
| 238 | } |
| 239 | |
| 240 | set_offline() { |
| 241 | local cpu=$1 |
| 242 | local dirpath=$CPU_PATH/$cpu |
| 243 | |
Daniel Lezcano | 3dd53ee | 2012-04-05 14:03:08 +0200 | [diff] [blame] | 244 | if [ "$cpu" = "cpu0" ]; then |
| 245 | return 0 |
| 246 | fi |
| 247 | |
Daniel Lezcano | 805e335 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 248 | echo 0 > $dirpath/online |
| 249 | } |
| 250 | |
| 251 | get_online() { |
| 252 | local cpu=$1 |
| 253 | local dirpath=$CPU_PATH/$cpu |
| 254 | |
| 255 | cat $dirpath/online |
| 256 | } |
| 257 | |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 258 | check() { |
| 259 | |
| 260 | local descr=$1 |
| 261 | local func=$2 |
| 262 | shift 2; |
| 263 | |
| 264 | log_begin "checking $descr" |
| 265 | |
| 266 | $func $@ |
Sanjay Singh Rawat | c7af87b | 2013-12-15 13:30:37 +0530 | [diff] [blame] | 267 | if [ $? -ne 0 ]; then |
Sanjay Singh Rawat | 3bf6194 | 2013-04-10 14:06:14 +0530 | [diff] [blame] | 268 | log_end "Err" |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 269 | return 1 |
| 270 | fi |
| 271 | |
Sanjay Singh Rawat | 3bf6194 | 2013-04-10 14:06:14 +0530 | [diff] [blame] | 272 | log_end "Ok" |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 273 | |
| 274 | return 0 |
| 275 | } |
| 276 | |
Daniel Lezcano | b6a3119 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 277 | check_file() { |
| 278 | local file=$1 |
| 279 | local dir=$2 |
| 280 | |
Sanjay Singh Rawat | 8b6ad7c | 2014-07-08 16:35:36 +0200 | [diff] [blame] | 281 | check "'$file' exists in '$dir'" "test -f" $dir/$file |
Daniel Lezcano | b6a3119 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 284 | check_cpufreq_files() { |
| 285 | |
| 286 | local dirpath=$CPU_PATH/$1/cpufreq |
| 287 | shift 1 |
| 288 | |
| 289 | for i in $@; do |
Daniel Lezcano | b6a3119 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 290 | check_file $i $dirpath || return 1 |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 291 | done |
| 292 | |
| 293 | return 0 |
| 294 | } |
| 295 | |
Daniel Lezcano | a7da520 | 2011-08-05 15:01:42 +0200 | [diff] [blame] | 296 | check_sched_mc_files() { |
| 297 | |
| 298 | local dirpath=$CPU_PATH |
| 299 | |
| 300 | for i in $@; do |
Daniel Lezcano | b6a3119 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 301 | check_file $i $dirpath || return 1 |
Daniel Lezcano | a7da520 | 2011-08-05 15:01:42 +0200 | [diff] [blame] | 302 | done |
| 303 | |
| 304 | return 0 |
| 305 | } |
| 306 | |
Daniel Lezcano | bc043cb | 2011-08-05 15:01:42 +0200 | [diff] [blame] | 307 | check_topology_files() { |
| 308 | |
| 309 | local dirpath=$CPU_PATH/$1/topology |
| 310 | shift 1 |
| 311 | |
| 312 | for i in $@; do |
Daniel Lezcano | b6a3119 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 313 | check_file $i $dirpath || return 1 |
Daniel Lezcano | bc043cb | 2011-08-05 15:01:42 +0200 | [diff] [blame] | 314 | done |
| 315 | |
| 316 | return 0 |
| 317 | } |
| 318 | |
Daniel Lezcano | b10475e | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 319 | check_cpuhotplug_files() { |
| 320 | |
| 321 | local dirpath=$CPU_PATH/$1 |
| 322 | shift 1 |
| 323 | |
| 324 | for i in $@; do |
Lisa Nguyen | e232318 | 2014-07-04 10:24:30 +0530 | [diff] [blame] | 325 | 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 Lezcano | b10475e | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 331 | check_file $i $dirpath || return 1 |
| 332 | done |
| 333 | |
| 334 | return 0 |
| 335 | } |
| 336 | |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 337 | save_governors() { |
| 338 | |
| 339 | governors_backup= |
| 340 | local index=0 |
| 341 | |
Lisa Nguyen | 188ca9a | 2014-08-11 12:21:08 -0700 | [diff] [blame] | 342 | for cpu in $cpus; do |
| 343 | governors_backup[$index]=$(cat $CPU_PATH/$cpu/cpufreq/scaling_governor) |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 344 | index=$((index + 1)) |
| 345 | done |
| 346 | } |
| 347 | |
| 348 | restore_governors() { |
| 349 | |
| 350 | local index=0 |
| 351 | local oldgov= |
| 352 | |
Lisa Nguyen | 188ca9a | 2014-08-11 12:21:08 -0700 | [diff] [blame] | 353 | for cpu in $cpus; do |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 354 | oldgov=${governors_backup[$index]} |
Lisa Nguyen | 188ca9a | 2014-08-11 12:21:08 -0700 | [diff] [blame] | 355 | echo $oldgov > $CPU_PATH/$cpu/cpufreq/scaling_governor |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 356 | index=$((index + 1)) |
| 357 | done |
| 358 | } |
| 359 | |
| 360 | save_frequencies() { |
| 361 | |
| 362 | frequencies_backup= |
| 363 | local index=0 |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 364 | |
| 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 | |
| 371 | restore_frequencies() { |
| 372 | |
| 373 | local index=0 |
| 374 | local oldfreq= |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 375 | |
| 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 | |
| 383 | sigtrap() { |
| 384 | exit 255 |
Daniel Lezcano | 18e22b5 | 2011-08-06 02:52:02 +0200 | [diff] [blame] | 385 | } |
Sanjay Singh Rawat | d5963e5 | 2014-02-05 18:12:17 +0530 | [diff] [blame] | 386 | |
| 387 | # currently we support ubuntu and android |
| 388 | get_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 | |
| 399 | is_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 Nguyen | e232318 | 2014-07-04 10:24:30 +0530 | [diff] [blame] | 411 | |
| 412 | is_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 | } |