1#!/bin/bash 2 3# protect against multiple inclusion 4if [ $FILE_CPUFREQ ]; then 5 return 0 6else 7 FILE_CPUFREQ=DONE 8fi 9 10source cpu.sh 11 12 13# $1: cpu 14cpu_should_have_cpufreq_directory() 15{ 16 if [ ! -d $CPUROOT/$1/cpufreq ]; then 17 printf "Warning: No cpufreq directory present for $1\n" 18 fi 19} 20 21cpu_should_not_have_cpufreq_directory() 22{ 23 if [ -d $CPUROOT/$1/cpufreq ]; then 24 printf "Warning: cpufreq directory present for $1\n" 25 fi 26} 27 28for_each_policy() 29{ 30 policies=$(ls $CPUFREQROOT| grep "policy[0-9].*") 31 for policy in $policies; do 32 $@ $policy 33 done 34} 35 36for_each_policy_concurrent() 37{ 38 policies=$(ls $CPUFREQROOT| grep "policy[0-9].*") 39 for policy in $policies; do 40 $@ $policy & 41 done 42} 43 44# $1: Path 45read_cpufreq_files_in_dir() 46{ 47 local files=`ls $1` 48 49 printf "Printing directory: $1\n\n" 50 51 for file in $files; do 52 if [ -f $1/$file ]; then 53 printf "$file:" 54 cat $1/$file 55 else 56 printf "\n" 57 read_cpufreq_files_in_dir "$1/$file" 58 fi 59 done 60 printf "\n" 61} 62 63 64read_all_cpufreq_files() 65{ 66 printf "** Test: Running ${FUNCNAME[0]} **\n\n" 67 68 read_cpufreq_files_in_dir $CPUFREQROOT 69 70 printf "%s\n\n" "------------------------------------------------" 71} 72 73 74# UPDATE CPUFREQ FILES 75 76# $1: directory path 77update_cpufreq_files_in_dir() 78{ 79 local files=`ls $1` 80 81 printf "Updating directory: $1\n\n" 82 83 for file in $files; do 84 if [ -f $1/$file ]; then 85 # file is writable ? 86 local wfile=$(ls -l $1/$file | awk '$1 ~ /^.*w.*/ { print $NF; }') 87 88 if [ ! -z $wfile ]; then 89 # scaling_setspeed is a special file and we 90 # should skip updating it 91 if [ $file != "scaling_setspeed" ]; then 92 local val=$(cat $1/$file) 93 printf "Writing $val to: $file\n" 94 echo $val > $1/$file 95 fi 96 fi 97 else 98 printf "\n" 99 update_cpufreq_files_in_dir "$1/$file" 100 fi 101 done 102 103 printf "\n" 104} 105 106# Update all writable files with their existing values 107update_all_cpufreq_files() 108{ 109 printf "** Test: Running ${FUNCNAME[0]} **\n\n" 110 111 update_cpufreq_files_in_dir $CPUFREQROOT 112 113 printf "%s\n\n" "------------------------------------------------" 114} 115 116 117# CHANGE CPU FREQUENCIES 118 119# $1: policy 120find_current_freq() 121{ 122 cat $CPUFREQROOT/$1/scaling_cur_freq 123} 124 125# $1: policy 126# $2: frequency 127set_cpu_frequency() 128{ 129 printf "Change frequency for $1 to $2\n" 130 echo $2 > $CPUFREQROOT/$1/scaling_setspeed 131} 132 133# $1: policy 134test_all_frequencies() 135{ 136 local filepath="$CPUFREQROOT/$1" 137 138 backup_governor $1 139 140 local found=$(switch_governor $1 "userspace") 141 if [ $found = 1 ]; then 142 printf "${FUNCNAME[0]}: userspace governor not available for: $1\n" 143 return; 144 fi 145 146 printf "Switched governor for $1 to userspace\n\n" 147 148 local freqs=$(cat $filepath/scaling_available_frequencies) 149 printf "Available frequencies for $1: $freqs\n\n" 150 151 # Set all frequencies one-by-one 152 for freq in $freqs; do 153 set_cpu_frequency $1 $freq 154 done 155 156 printf "\n" 157 158 restore_governor $1 159} 160 161# $1: loop count 162shuffle_frequency_for_all_cpus() 163{ 164 printf "** Test: Running ${FUNCNAME[0]} for $1 loops **\n\n" 165 166 for i in `seq 1 $1`; do 167 for_each_policy test_all_frequencies 168 done 169 printf "\n%s\n\n" "------------------------------------------------" 170} 171 172# Basic cpufreq tests 173cpufreq_basic_tests() 174{ 175 printf "*** RUNNING CPUFREQ SANITY TESTS ***\n" 176 printf "====================================\n\n" 177 178 count=$(count_cpufreq_managed_cpus) 179 if [ $count = 0 ]; then 180 printf "No cpu is managed by cpufreq core, exiting\n" 181 exit; 182 else 183 printf "CPUFreq manages: $count CPUs\n\n" 184 fi 185 186 # Detect & print which CPUs are not managed by cpufreq 187 print_unmanaged_cpus 188 189 # read/update all cpufreq files 190 read_all_cpufreq_files 191 update_all_cpufreq_files 192 193 # hotplug cpus 194 reboot_cpus 5 195 196 # Test all frequencies 197 shuffle_frequency_for_all_cpus 2 198 199 # Test all governors 200 shuffle_governors_for_all_cpus 1 201} 202 203# Suspend/resume 204# $1: "suspend" or "hibernate", $2: loop count 205do_suspend() 206{ 207 printf "** Test: Running ${FUNCNAME[0]}: Trying $1 for $2 loops **\n\n" 208 209 # Is the directory available 210 if [ ! -d $SYSFS/power/ -o ! -f $SYSFS/power/state ]; then 211 printf "$SYSFS/power/state not available\n" 212 return 1 213 fi 214 215 if [ $1 = "suspend" ]; then 216 filename="mem" 217 elif [ $1 = "hibernate" ]; then 218 filename="disk" 219 else 220 printf "$1 is not a valid option\n" 221 return 1 222 fi 223 224 if [ -n $filename ]; then 225 present=$(cat $SYSFS/power/state | grep $filename) 226 227 if [ -z "$present" ]; then 228 printf "Tried to $1 but $filename isn't present in $SYSFS/power/state\n" 229 return 1; 230 fi 231 232 for i in `seq 1 $2`; do 233 printf "Starting $1\n" 234 echo $filename > $SYSFS/power/state 235 printf "Came out of $1\n" 236 237 printf "Do basic tests after finishing $1 to verify cpufreq state\n\n" 238 cpufreq_basic_tests 239 done 240 fi 241} 242