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= |
Sanjay Singh Rawat | 3bf6194 | 2013-04-10 14:06:14 +0530 | [diff] [blame] | 33 | pass_count=0 |
| 34 | fail_count=0 |
| 35 | |
| 36 | test_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 Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 44 | |
| 45 | log_begin() { |
Daniel Lezcano | 994f6f1 | 2011-08-16 13:28:33 +0200 | [diff] [blame] | 46 | printf "%-76s" "$TEST_NAME.$INC$CPU: $@... " |
Daniel Lezcano | 1efddc1 | 2011-08-09 23:45:30 +0200 | [diff] [blame] | 47 | INC=$(($INC+1)) |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | log_end() { |
| 51 | printf "$*\n" |
| 52 | } |
| 53 | |
| 54 | log_skip() { |
| 55 | log_begin "$@" |
Daniel Lezcano | ff4aa11 | 2011-08-16 15:48:38 +0200 | [diff] [blame] | 56 | log_end "skip" |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | for_each_cpu() { |
| 60 | |
| 61 | local func=$1 |
| 62 | shift 1 |
| 63 | |
| 64 | cpus=$(ls $CPU_PATH | grep "cpu[0-9].*") |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 65 | for cpu in $cpus; do |
Daniel Lezcano | 1efddc1 | 2011-08-09 23:45:30 +0200 | [diff] [blame] | 66 | INC=0 |
| 67 | CPU=/$cpu |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 68 | $func $cpu $@ |
| 69 | done |
| 70 | |
| 71 | return 0 |
| 72 | } |
| 73 | |
| 74 | for_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 | |
| 89 | for_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 | |
| 104 | set_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 | |
| 113 | get_governor() { |
| 114 | |
| 115 | local cpu=$1 |
| 116 | local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor |
| 117 | |
| 118 | cat $dirpath |
| 119 | } |
| 120 | |
| 121 | wait_latency() { |
| 122 | local cpu=$1 |
| 123 | local dirpath=$CPU_PATH/$cpu/cpufreq |
Daniel Lezcano | 9338295 | 2011-09-19 11:41:39 +0200 | [diff] [blame] | 124 | local latency= |
| 125 | local nrfreq= |
Sanjay Singh Rawat | e5726c1 | 2014-08-05 13:39:06 +0530 | [diff] [blame^] | 126 | 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 Lezcano | 9338295 | 2011-09-19 11:41:39 +0200 | [diff] [blame] | 137 | |
| 138 | latency=$(cat $dirpath/cpuinfo_transition_latency) |
Sanjay Singh Rawat | c7af87b | 2013-12-15 13:30:37 +0530 | [diff] [blame] | 139 | if [ $? -ne 0 ]; then |
Daniel Lezcano | 0b90d41 | 2011-09-20 08:31:04 +0200 | [diff] [blame] | 140 | return 1 |
Daniel Lezcano | 9338295 | 2011-09-19 11:41:39 +0200 | [diff] [blame] | 141 | fi |
| 142 | |
| 143 | nrfreq=$(cat $dirpath/scaling_available_frequencies | wc -w) |
Sanjay Singh Rawat | c7af87b | 2013-12-15 13:30:37 +0530 | [diff] [blame] | 144 | if [ $? -ne 0 ]; then |
Daniel Lezcano | 0b90d41 | 2011-09-20 08:31:04 +0200 | [diff] [blame] | 145 | return 1 |
Daniel Lezcano | 9338295 | 2011-09-19 11:41:39 +0200 | [diff] [blame] | 146 | fi |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 147 | |
| 148 | nrfreq=$((nrfreq + 1)) |
Sanjay Singh Rawat | e5726c1 | 2014-08-05 13:39:06 +0530 | [diff] [blame^] | 149 | |
| 150 | sleep_time=$(($latency + $sampling_rate)) |
| 151 | |
| 152 | ../utils/nanosleep $(($nrfreq * $sleep_time)) |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | frequnit() { |
| 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 | |
| 175 | set_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 | |
| 186 | get_frequency() { |
| 187 | local cpu=$1 |
| 188 | local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_cur_freq |
| 189 | cat $dirpath |
| 190 | } |
| 191 | |
| 192 | get_max_frequency() { |
| 193 | local cpu=$1 |
| 194 | local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_max_freq |
| 195 | cat $dirpath |
| 196 | } |
| 197 | |
| 198 | get_min_frequency() { |
| 199 | local cpu=$1 |
| 200 | local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_min_freq |
| 201 | cat $dirpath |
| 202 | } |
| 203 | |
Daniel Lezcano | 805e335 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 204 | set_online() { |
| 205 | local cpu=$1 |
| 206 | local dirpath=$CPU_PATH/$cpu |
| 207 | |
Daniel Lezcano | 3dd53ee | 2012-04-05 14:03:08 +0200 | [diff] [blame] | 208 | if [ "$cpu" = "cpu0" ]; then |
| 209 | return 0 |
| 210 | fi |
| 211 | |
Daniel Lezcano | 805e335 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 212 | echo 1 > $dirpath/online |
| 213 | } |
| 214 | |
| 215 | set_offline() { |
| 216 | local cpu=$1 |
| 217 | local dirpath=$CPU_PATH/$cpu |
| 218 | |
Daniel Lezcano | 3dd53ee | 2012-04-05 14:03:08 +0200 | [diff] [blame] | 219 | if [ "$cpu" = "cpu0" ]; then |
| 220 | return 0 |
| 221 | fi |
| 222 | |
Daniel Lezcano | 805e335 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 223 | echo 0 > $dirpath/online |
| 224 | } |
| 225 | |
| 226 | get_online() { |
| 227 | local cpu=$1 |
| 228 | local dirpath=$CPU_PATH/$cpu |
| 229 | |
| 230 | cat $dirpath/online |
| 231 | } |
| 232 | |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 233 | check() { |
| 234 | |
| 235 | local descr=$1 |
| 236 | local func=$2 |
| 237 | shift 2; |
| 238 | |
| 239 | log_begin "checking $descr" |
| 240 | |
| 241 | $func $@ |
Sanjay Singh Rawat | c7af87b | 2013-12-15 13:30:37 +0530 | [diff] [blame] | 242 | if [ $? -ne 0 ]; then |
Sanjay Singh Rawat | 3bf6194 | 2013-04-10 14:06:14 +0530 | [diff] [blame] | 243 | log_end "Err" |
| 244 | fail_count=$(($fail_count + 1)) |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 245 | return 1 |
| 246 | fi |
| 247 | |
Sanjay Singh Rawat | 3bf6194 | 2013-04-10 14:06:14 +0530 | [diff] [blame] | 248 | log_end "Ok" |
| 249 | pass_count=$(($pass_count + 1)) |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 250 | |
| 251 | return 0 |
| 252 | } |
| 253 | |
Daniel Lezcano | b6a3119 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 254 | check_file() { |
| 255 | local file=$1 |
| 256 | local dir=$2 |
| 257 | |
Sanjay Singh Rawat | 8b6ad7c | 2014-07-08 16:35:36 +0200 | [diff] [blame] | 258 | check "'$file' exists in '$dir'" "test -f" $dir/$file |
Daniel Lezcano | b6a3119 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 261 | check_cpufreq_files() { |
| 262 | |
| 263 | local dirpath=$CPU_PATH/$1/cpufreq |
| 264 | shift 1 |
| 265 | |
| 266 | for i in $@; do |
Daniel Lezcano | b6a3119 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 267 | check_file $i $dirpath || return 1 |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 268 | done |
| 269 | |
| 270 | return 0 |
| 271 | } |
| 272 | |
Daniel Lezcano | a7da520 | 2011-08-05 15:01:42 +0200 | [diff] [blame] | 273 | check_sched_mc_files() { |
| 274 | |
| 275 | local dirpath=$CPU_PATH |
| 276 | |
| 277 | for i in $@; do |
Daniel Lezcano | b6a3119 | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 278 | check_file $i $dirpath || return 1 |
Daniel Lezcano | a7da520 | 2011-08-05 15:01:42 +0200 | [diff] [blame] | 279 | done |
| 280 | |
| 281 | return 0 |
| 282 | } |
| 283 | |
Daniel Lezcano | bc043cb | 2011-08-05 15:01:42 +0200 | [diff] [blame] | 284 | check_topology_files() { |
| 285 | |
| 286 | local dirpath=$CPU_PATH/$1/topology |
| 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 | bc043cb | 2011-08-05 15:01:42 +0200 | [diff] [blame] | 291 | done |
| 292 | |
| 293 | return 0 |
| 294 | } |
| 295 | |
Daniel Lezcano | b10475e | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 296 | check_cpuhotplug_files() { |
| 297 | |
| 298 | local dirpath=$CPU_PATH/$1 |
| 299 | shift 1 |
| 300 | |
| 301 | for i in $@; do |
Lisa Nguyen | e232318 | 2014-07-04 10:24:30 +0530 | [diff] [blame] | 302 | 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 Lezcano | b10475e | 2011-10-03 11:07:36 +0200 | [diff] [blame] | 308 | check_file $i $dirpath || return 1 |
| 309 | done |
| 310 | |
| 311 | return 0 |
| 312 | } |
| 313 | |
Daniel Lezcano | d84ec06 | 2011-07-26 14:38:59 +0200 | [diff] [blame] | 314 | save_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 | |
| 325 | restore_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 | |
| 337 | save_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 | |
| 350 | restore_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 | |
| 363 | sigtrap() { |
| 364 | exit 255 |
Daniel Lezcano | 18e22b5 | 2011-08-06 02:52:02 +0200 | [diff] [blame] | 365 | } |
Sanjay Singh Rawat | d5963e5 | 2014-02-05 18:12:17 +0530 | [diff] [blame] | 366 | |
| 367 | # currently we support ubuntu and android |
| 368 | get_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 | |
| 379 | is_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 Nguyen | e232318 | 2014-07-04 10:24:30 +0530 | [diff] [blame] | 391 | |
| 392 | is_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 | } |