[5507] | 1 | #!/bin/sh |
---|
| 2 | # Startup script for cpuspeed |
---|
| 3 | # |
---|
| 4 | # chkconfig: 12345 06 99 |
---|
| 5 | # description: Run dynamic CPU speed daemon and/or load appropriate |
---|
| 6 | # cpu frequency scaling kernel modules and/or governors |
---|
| 7 | |
---|
| 8 | # Source function library. |
---|
| 9 | . /etc/rc.d/init.d/functions |
---|
| 10 | |
---|
| 11 | [ -r /proc/cmdline ] && cmdline=$(cat /proc/cmdline) |
---|
| 12 | if (strstr "$cmdline" nocpufreq) ; then exit 0 ; fi |
---|
| 13 | |
---|
| 14 | prog="cpuspeed" |
---|
| 15 | |
---|
| 16 | [ -f /usr/sbin/$prog ] || exit 5 |
---|
| 17 | |
---|
| 18 | # Get config. |
---|
| 19 | if [ -f /etc/$prog.conf ]; then |
---|
| 20 | . /etc/$prog.conf |
---|
| 21 | fi |
---|
| 22 | |
---|
| 23 | cpufreqd=/sys/devices/system/cpu/cpufreq |
---|
| 24 | cpu0freqd=/sys/devices/system/cpu/cpu0/cpufreq |
---|
| 25 | cpus='/sys/devices/system/cpu/cpu[0-9]*' |
---|
| 26 | testpat="${cpus}/cpufreq/scaling_driver" |
---|
| 27 | lockfile="/var/lock/subsys/$prog" |
---|
| 28 | xendir="/proc/xen" |
---|
| 29 | logger="/usr/bin/logger -p info -t $prog" |
---|
| 30 | IGNORE_NICE=${IGNORE_NICE:-0} |
---|
| 31 | module_loaded=false |
---|
| 32 | |
---|
| 33 | some_file_exist() { |
---|
| 34 | while [ "$1" ] ; do |
---|
| 35 | [ -f "$1" ] && return 0 |
---|
| 36 | shift |
---|
| 37 | done |
---|
| 38 | return 1 |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | governor_is_module() { |
---|
| 42 | # Check to see if the requested cpufreq governor |
---|
| 43 | # is provided as a kernel module or not |
---|
| 44 | module_info=`/sbin/modinfo cpufreq-${governor}` |
---|
| 45 | return $? |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | is_p4_clockmod() { |
---|
| 49 | if [ `/sbin/lsmod | grep -c -w "p4.clockmod"` -ge 1 -a -d "/sys/devices/system/cpu/cpu0/thermal_throttle" ]; then |
---|
| 50 | return 0 |
---|
| 51 | fi |
---|
| 52 | return 1 |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | governor_module_loaded() { |
---|
| 56 | # Check to see if we have a module loaded for |
---|
| 57 | # the current cpufreq governor |
---|
| 58 | if [ -e ${cpu0freqd}/scaling_governor ]; then |
---|
| 59 | governor=`cat ${cpu0freqd}/scaling_governor` |
---|
| 60 | else |
---|
| 61 | governor="none" |
---|
| 62 | fi |
---|
| 63 | if [ "${governor}" != "none" -a `/sbin/lsmod | grep -c -w "cpufreq.${governor}"` -ge 1 ]; then |
---|
| 64 | return 0 |
---|
| 65 | fi |
---|
| 66 | return 1 |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | adjust_cpufreq() { |
---|
| 70 | # First arg is a param under $cpufreqd |
---|
| 71 | # Second arg is the value you want to set that param to |
---|
| 72 | echo $2 > $cpufreqd/$1 |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | adjust_governor() { |
---|
| 76 | # First arg is a param under $cpu/cpufreq/ |
---|
| 77 | # Second arg is the value you want to set that param to |
---|
| 78 | for cpu in ${cpus}; do |
---|
| 79 | echo $2 > $cpu/cpufreq/$1 |
---|
| 80 | done |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | start_cpuspeed() { |
---|
| 84 | echo -n $"Starting $prog: " |
---|
| 85 | # cpuspeed daemon thresholds are specified as idle percentages, |
---|
| 86 | # cpufreq modules as busy percentages, so we need to do some |
---|
| 87 | # math here for use of unified config... |
---|
| 88 | # DOWN_THRESHOLD doesn't mean exactly the same thing for |
---|
| 89 | # cpuspeed as it does for the cpufreq governors, but close |
---|
| 90 | # enough, and if not specified, we use same defaults as governors. |
---|
| 91 | if [ -n "$UP_THRESHOLD" ]; then |
---|
| 92 | let UP_THRESHOLD=100-$UP_THRESHOLD |
---|
| 93 | else |
---|
| 94 | UP_THRESHOLD=20 |
---|
| 95 | fi |
---|
| 96 | if [ -n "$DOWN_THRESHOLD" ]; then |
---|
| 97 | let DOWN_THRESHOLD=100-$DOWN_THRESHOLD |
---|
| 98 | else |
---|
| 99 | DOWN_THRESHOLD=80 |
---|
| 100 | fi |
---|
| 101 | OPTS="$OPTS -p $UP_THRESHOLD $DOWN_THRESHOLD" |
---|
| 102 | if [ -n "$MIN_SPEED" ]; then |
---|
| 103 | OPTS="$OPTS -m $MIN_SPEED" |
---|
| 104 | fi |
---|
| 105 | if [ -n "$MAX_SPEED" ]; then |
---|
| 106 | OPTS="$OPTS -M $MAX_SPEED" |
---|
| 107 | fi |
---|
| 108 | if [ "$IGNORE_NICE" -eq 1 ]; then |
---|
| 109 | OPTS="$OPTS -n" |
---|
| 110 | fi |
---|
| 111 | daemon $prog -d $OPTS |
---|
| 112 | RETVAL=$? |
---|
| 113 | return $RETVAL |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | stop_cpuspeed() { |
---|
| 117 | if [ -n "`pidof $prog`" ]; then |
---|
| 118 | echo -n $"Stopping $prog: " |
---|
| 119 | killproc $prog -USR1 |
---|
| 120 | killproc $prog -INT |
---|
| 121 | fi |
---|
| 122 | if [ -n "`pidof $prog`" ]; then |
---|
| 123 | killproc $prog |
---|
| 124 | fi |
---|
| 125 | RETVAL=$? |
---|
| 126 | return $RETVAL |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | start() { |
---|
| 130 | if [ ! -f $lockfile ] && [ ! -d "$xendir" ]; then |
---|
| 131 | cpu_vendor=$(awk '/vendor_id/{print $3}' /proc/cpuinfo | tail -n 1) |
---|
| 132 | cpu_family=$(awk '/cpu family/{print $4}' /proc/cpuinfo | tail -n 1) |
---|
| 133 | if ! some_file_exist $testpat ; then |
---|
| 134 | # Attempt to load scaling_driver if not loaded |
---|
| 135 | # but it is configured |
---|
| 136 | if [ -n "$DRIVER" ]; then |
---|
| 137 | /sbin/modprobe "$DRIVER" |
---|
| 138 | elif [ "$cpu_vendor" == AuthenticAMD ] && [ "$cpu_family" -ge 7 ]; then |
---|
| 139 | # Try loading powernow-k8 if this is an AMD processor, |
---|
| 140 | # family 7 or greater (Athlon XP/MP was family 6) |
---|
| 141 | pk8m=$(/sbin/modinfo powernow-k8 > /dev/null 2>&1) |
---|
| 142 | if [ "$?" -eq 0 ]; then |
---|
| 143 | /sbin/modprobe powernow-k8 2> /dev/null |
---|
| 144 | [ -d ${cpu0freqd} ] || /sbin/modprobe -r powernow-k8 2> /dev/null |
---|
| 145 | fi |
---|
| 146 | elif [ -d /proc/acpi ]; then |
---|
| 147 | # use ACPI as a fallback |
---|
| 148 | /sbin/modprobe acpi-cpufreq 2> /dev/null |
---|
| 149 | # if even ACPI didn't work, remove it |
---|
| 150 | # and then next test will bail out. |
---|
| 151 | [ -d ${cpu0freqd} ] || /sbin/modprobe -r acpi-cpufreq 2> /dev/null |
---|
| 152 | fi |
---|
| 153 | if [ ! -d ${cpu0freqd} -a "$cpu_vendor" == GenuineIntel ]; then |
---|
| 154 | # last-ditch effort for Intel proc boxes, try our neutered p4-clockmod |
---|
| 155 | # to get at least passive cooling support (no clock changes) |
---|
| 156 | /sbin/modprobe p4-clockmod 2> /dev/null |
---|
| 157 | if [ -d ${cpu0freqd} ]; then |
---|
| 158 | echo -n "Enabling p4-clockmod driver (passive cooling only): " |
---|
| 159 | success; echo |
---|
| 160 | return 0 |
---|
| 161 | else |
---|
| 162 | /sbin/modprobe -r p4-clockmod 2> /dev/null |
---|
| 163 | fi |
---|
| 164 | fi |
---|
| 165 | fi |
---|
| 166 | |
---|
| 167 | # If we get this far with no driver, we must have no scaling. |
---|
| 168 | # We're doomed. |
---|
| 169 | [ ! -f ${cpu0freqd}/scaling_driver ] && return 6 |
---|
| 170 | |
---|
| 171 | # Okay, we have a driver, carry on... |
---|
| 172 | drv=`cat ${cpu0freqd}/scaling_driver` |
---|
| 173 | |
---|
| 174 | # Figure out default governor to use |
---|
| 175 | case "$drv" in |
---|
| 176 | centrino|powernow-k8|acpi-cpufreq|e_powersaver) |
---|
| 177 | default_governor=ondemand |
---|
| 178 | ;; |
---|
| 179 | *) |
---|
| 180 | default_governor=userspace |
---|
| 181 | ;; |
---|
| 182 | esac |
---|
| 183 | governor=${GOVERNOR:-${default_governor}} |
---|
| 184 | |
---|
| 185 | # Load governor module, if need be, and validate |
---|
| 186 | governor_is_module && /sbin/modprobe cpufreq-${governor} |
---|
| 187 | if [ `grep -c -w ${governor} ${cpu0freqd}/scaling_available_governors` -ge 1 ]; then |
---|
| 188 | $logger "Enabling ${governor} cpu frequency scaling governor" |
---|
| 189 | else |
---|
| 190 | $logger "Invalid governor \"${governor}\" specified, falling back to ${default_governor}" |
---|
| 191 | governor_is_module && /sbin/modprobe -r cpufreq-${governor} |
---|
| 192 | governor=${default_governor} |
---|
| 193 | governor_is_module && /sbin/modprobe cpufreq-${governor} |
---|
| 194 | fi |
---|
| 195 | |
---|
| 196 | # Set governor |
---|
| 197 | adjust_governor scaling_governor ${governor} |
---|
| 198 | |
---|
| 199 | # Run cpuspeed daemon for userspace gov, kernel ones otherwise |
---|
| 200 | if [ "${governor}" == "userspace" ]; then |
---|
| 201 | start_cpuspeed |
---|
| 202 | RETVAL=$? |
---|
| 203 | else |
---|
| 204 | if [ -n "$MIN_SPEED" ]; then |
---|
| 205 | adjust_cpufreq scaling_min_freq $MIN_SPEED |
---|
| 206 | fi |
---|
| 207 | if [ -n "$MAX_SPEED" ]; then |
---|
| 208 | adjust_cpufreq scaling_max_freq $MAX_SPEED |
---|
| 209 | fi |
---|
| 210 | if [ -n "$UP_THRESHOLD" -a ${governor} == "ondemand" ]; then |
---|
| 211 | adjust_cpufreq ondemand/up_threshold $UP_THRESHOLD |
---|
| 212 | fi |
---|
| 213 | if [ -n "$DOWN_THRESHOLD" -a ${governor} == "conservative" ]; then |
---|
| 214 | adjust_cpufreq conservative/down_threshold $DOWN_THRESHOLD |
---|
| 215 | fi |
---|
| 216 | if [ "$IGNORE_NICE" -eq 1 -a ${governor} == "ondemand" -o ${governor} == "conservative" ]; then |
---|
| 217 | adjust_cpufreq ${governor}/ignore_nice_load $IGNORE_NICE |
---|
| 218 | fi |
---|
| 219 | echo -n "Enabling ${governor} cpu frequency scaling: " |
---|
| 220 | success |
---|
| 221 | RETVAL=0 |
---|
| 222 | fi |
---|
| 223 | echo |
---|
| 224 | # Technically, not quite right in non-cpuspeed daemon |
---|
| 225 | # cases, but close enough to indicate that we're |
---|
| 226 | # doing some sort of cpu frequency scaling. |
---|
| 227 | [ $RETVAL = 0 ] && touch $lockfile |
---|
| 228 | else |
---|
| 229 | if [ -d "$xendir" ]; then |
---|
| 230 | $logger "CPU Frequency scaling is currently not supported on xen kernels" |
---|
| 231 | fi |
---|
| 232 | return 0 |
---|
| 233 | fi |
---|
| 234 | return $RETVAL |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | stop() { |
---|
| 238 | is_p4_clockmod && p4status="true" |
---|
| 239 | if [ "$p4status" == "true" ]; then |
---|
| 240 | echo "p4-clockmod passive cooling support cannot be stopped" |
---|
| 241 | return 0 |
---|
| 242 | fi |
---|
| 243 | [ ! -f ${cpu0freqd}/scaling_driver ] && return 0 |
---|
| 244 | drv=`cat ${cpu0freqd}/scaling_driver` |
---|
| 245 | governor_module_loaded && module_loaded=true |
---|
| 246 | |
---|
| 247 | if [ "${governor}" != "userspace" ]; then |
---|
| 248 | echo -n "Disabling ${governor} cpu frequency scaling: " |
---|
| 249 | $logger "Disabling ${governor} cpu frequency scaling governor" |
---|
| 250 | for cpu in ${cpus}; do |
---|
| 251 | echo userspace > $cpu/cpufreq/scaling_governor |
---|
| 252 | cat $cpu/cpufreq/cpuinfo_max_freq > $cpu/cpufreq/scaling_setspeed |
---|
| 253 | done |
---|
| 254 | if [ $module_loaded == true ]; then |
---|
| 255 | /sbin/modprobe -r cpufreq-${governor} |
---|
| 256 | fi |
---|
| 257 | success |
---|
| 258 | RETVAL=0 |
---|
| 259 | else |
---|
| 260 | stop_cpuspeed |
---|
| 261 | RETVAL=$? |
---|
| 262 | fi |
---|
| 263 | echo |
---|
| 264 | [ -n "$DRIVER" ] && /sbin/modprobe -r $DRIVER |
---|
| 265 | [ $RETVAL = 0 ] && RETVAL=$? |
---|
| 266 | [ $RETVAL = 0 ] && rm -f $lockfile |
---|
| 267 | return $RETVAL |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | case "$1" in |
---|
| 271 | start) |
---|
| 272 | start |
---|
| 273 | ;; |
---|
| 274 | |
---|
| 275 | stop) |
---|
| 276 | stop |
---|
| 277 | ;; |
---|
| 278 | |
---|
| 279 | status) |
---|
| 280 | is_p4_clockmod && p4status="true" |
---|
| 281 | if [ "$p4status" == "true" ]; then |
---|
| 282 | echo "p4-clockmod passive cooling is enabled" |
---|
| 283 | exit 0 |
---|
| 284 | fi |
---|
| 285 | governor_module_loaded && module_loaded=true |
---|
| 286 | if [ -d "$xendir" ]; then |
---|
| 287 | echo "Frequency scaling not supported under xen kernels" |
---|
| 288 | RETVAL=0 |
---|
| 289 | elif [ $module_loaded == true -o ${governor} == "performance" ]; then |
---|
| 290 | echo "Frequency scaling enabled using ${governor} governor" |
---|
| 291 | RETVAL=0 |
---|
| 292 | else |
---|
| 293 | status $prog |
---|
| 294 | RETVAL="$?" |
---|
| 295 | fi |
---|
| 296 | ;; |
---|
| 297 | |
---|
| 298 | restart) |
---|
| 299 | stop |
---|
| 300 | start |
---|
| 301 | ;; |
---|
| 302 | |
---|
| 303 | condrestart) |
---|
| 304 | governor_module_loaded && module_loaded=true |
---|
| 305 | if [ $module_loaded == true -o -n "`pidof $prog`" -o ${governor} == "performance" ]; then |
---|
| 306 | stop |
---|
| 307 | start |
---|
| 308 | fi |
---|
| 309 | ;; |
---|
| 310 | |
---|
| 311 | *) |
---|
| 312 | echo $"Usage: $0 {start|stop|restart|condrestart|status}" |
---|
| 313 | exit 3 |
---|
| 314 | ;; |
---|
| 315 | esac |
---|
| 316 | |
---|
| 317 | exit $RETVAL |
---|