Rev 4 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/bin/sh# the following is the LSB init header see# http://www.linux-foundation.org/spec//booksets/LSB-Core-generic/LSB-Core-generic.html#INITSCRCOMCONV#### BEGIN INIT INFO# Provides: cpuspeed# Should-Start:# Default-Start: 1 2 3 4 5# Short-Description: processor frequency scaling support# Description: This script enables/disables processor frequency# scaling support, either using the cpuspeed daemon# or in-kernel frequency scaling support### END INIT INFO# the following is the chkconfig init header## cpuspeed: processor frequency scaling support## chkconfig: 12345 13 99# description: Run dynamic CPU speed daemon and/or load appropriate# cpu frequency scaling kernel modules and/or governors## Source function library.. /etc/rc.d/init.d/functionsprog="cpuspeed"[ -f /usr/sbin/$prog ] || exit 5# Get config.if [ -f /etc/sysconfig/$prog ]; then. /etc/sysconfig/$progficpu0freqd=/sys/devices/system/cpu/cpu0/cpufreqglobfreq='/sys/devices/system/cpu'cpufreq="${globfreq}/cpufreq"cpus="${globfreq}/cpu[0-9]*"testpat="${cpus}/cpufreq/scaling_driver"lockfile="/var/lock/subsys/$prog"xendir="/proc/xen"logger="/usr/bin/logger -p info -t $prog"IGNORE_NICE=${IGNORE_NICE:-0}module_loaded=falsesome_file_exist() {while [ "$1" ] ; do[ -f "$1" ] && return 0shiftdonereturn 1}governor_is_module() {# Check to see if the requested cpufreq governor# is provided as a kernel module or notmodule_info=`/sbin/modinfo cpufreq-${governor} > /dev/null 2>&1`return $?}is_p4_clockmod() {if [ `/sbin/lsmod | grep -c -w "p4.clockmod"` -ge 1 -a -d "/sys/devices/system/cpu/cpu0/thermal_throttle" ]; thenreturn 0fireturn 1}governor_module_loaded() {# Check to see if we have a module loaded for# the current cpufreq governorif [ -e ${cpu0freqd}/scaling_governor ]; thengovernor=`cat ${cpu0freqd}/scaling_governor`elsegovernor="none"fiif [ "${governor}" != "none" -a `/sbin/lsmod | grep -c -w "cpufreq.${governor}"` -ge 1 ]; thenreturn 0fireturn 1}adjust_cpufreq() {# First arg is a param under $cpu/cpufreq/# Second arg is the value you want to set that param toif $(echo ${1} | grep -qE '(threshold|ignore_nice_load)'); then[ -f $cpufreq/$1 ] && echo $2 > $cpufreq/$1elsefor cpu in ${cpus}; do[ -f $cpu/cpufreq/$1 ] && echo $2 > $cpu/cpufreq/$1donefi}start_cpuspeed() {echo -n $"Starting $prog: "# cpuspeed daemon thresholds are specified as idle percentages,# cpufreq modules as busy percentages, so we need to do some# math here for use of unified config...# DOWN_THRESHOLD doesn't mean exactly the same thing for# cpuspeed as it does for the cpufreq governors, but close# enough, and if not specified, we use same defaults as governors.if [ -n "$UP_THRESHOLD" ]; thenlet UP_THRESHOLD=100-$UP_THRESHOLDelseUP_THRESHOLD=20fiif [ -n "$DOWN_THRESHOLD" ]; thenlet DOWN_THRESHOLD=100-$DOWN_THRESHOLDelseDOWN_THRESHOLD=80fiOPTS="$OPTS -r -p $UP_THRESHOLD $DOWN_THRESHOLD"if [ -n "$MIN_SPEED" ]; thenOPTS="$OPTS -m $MIN_SPEED"fiif [ -n "$MAX_SPEED" ]; thenOPTS="$OPTS -M $MAX_SPEED"fiif [ "$IGNORE_NICE" -eq 0 ]; thenOPTS="$OPTS -n"fidaemon $prog -d $OPTSRETVAL=$?return $RETVAL}stop_cpuspeed() {if [ -n "`pidof $prog`" ]; thenecho -n $"Stopping $prog: "killproc $prog -USR1killproc $prog -INTfiif [ -n "`pidof $prog`" ]; thenkillproc $progfiRETVAL=$?return $RETVAL}start() {if [ $(id -u) -ne 0 ]; thenecho -n "Insufficient privileges to start cpuspeed service: "failure; echoreturn 4fiif [ ! -f $lockfile ] && [ ! -d "$xendir" ]; thencpu_vendor=$(awk '/vendor_id/{print $3}' /proc/cpuinfo | tail -n 1)cpu_family=$(awk '/cpu family/{print $4}' /proc/cpuinfo | tail -n 1)if ! some_file_exist $testpat ; then# Attempt to load scaling_driver if not loaded# but it is configuredif [ -n "$DRIVER" ]; then/sbin/modprobe "$DRIVER"elif [ -d /proc/acpi ]; then/sbin/modprobe pcc-cpufreq 2> /dev/nullif [ ! -d ${cpu0freqd} ]; then/sbin/modprobe -r pcc-cpufreq 2> /dev/nullif [ "$cpu_vendor" == AuthenticAMD ] && [ "$cpu_family" -ge 7 ]; then# Try loading powernow-k8 if this is an AMD processor,# family 7 or greater (Athlon XP/MP was family 6)pk8m=$(/sbin/modinfo powernow-k8 > /dev/null 2>&1)if [ "$?" -eq 0 ]; then/sbin/modprobe powernow-k8 2> /dev/null[ -d ${cpu0freqd} ] || /sbin/modprobe -r powernow-k8 2> /dev/nullfielse# use ACPI as a fallback/sbin/modprobe acpi-cpufreq 2> /dev/null# if even ACPI didn't work, remove it# and then next test will bail out.[ -d ${cpu0freqd} ] || /sbin/modprobe -r acpi-cpufreq 2> /dev/nullfififiif [ ! -d ${cpu0freqd} -a "$cpu_vendor" == GenuineIntel ]; then# last-ditch effort for Intel proc boxes, try our neutered p4-clockmod# to get at least passive cooling support (no clock changes)/sbin/modprobe p4-clockmod 2> /dev/null[ -d ${cpu0freqd} ] || /sbin/modprobe -r p4-clockmod 2> /dev/nullfifi# If we get this far with no driver, we must have no scaling.# We're doomed.[ ! -f ${cpu0freqd}/scaling_driver ] && return 6# Okay, we have a driver, carry on...drv=`cat ${cpu0freqd}/scaling_driver`# Figure out default governor to usecase "$drv" incentrino|powernow-k8|pcc-cpufreq|acpi-cpufreq|e_powersaver)default_governor=ondemand;;p4-clockmod)# not actually a governor, we want to bail without doing either# in-kernel scaling or starting up the cpuspeed daemon in this casedefault_governor=p4passive;;intel_pstate)default_governor=powersaveecho "CPU states managed by intel_pstate; use cpupower for configuration."$logger "CPU states managed by intel_pstate; use cpupower for configuration.";;*)default_governor=userspace;;esacgovernor=${GOVERNOR:-${default_governor}}if [ "${governor}" == "p4passive" ]; thenecho -n "Enabling p4-clockmod driver (passive cooling only): "success; echoreturn 0fi# Load governor module, if need be, and validategovernor_is_module && /sbin/modprobe cpufreq-${governor}if [ `grep -c -w ${governor} ${cpu0freqd}/scaling_available_governors` -ge 1 ]; then$logger "Enabling ${governor} cpu frequency scaling governor"else$logger "Invalid governor \"${governor}\" specified, falling back to ${default_governor}"governor_is_module && /sbin/modprobe -r cpufreq-${governor}governor=${default_governor}governor_is_module && /sbin/modprobe cpufreq-${governor}fi# Set governoradjust_cpufreq scaling_governor ${governor}# Run cpuspeed daemon for userspace gov, kernel ones otherwiseif [ "${governor}" == "userspace" ]; thenstart_cpuspeedRETVAL=$?elseif [ -n "$MIN_SPEED" ]; thenadjust_cpufreq scaling_min_freq $MIN_SPEEDelseadjust_cpufreq scaling_min_freq 0fiif [ -n "$MAX_SPEED" ]; thenadjust_cpufreq scaling_max_freq $MAX_SPEEDelseadjust_cpufreq scaling_max_freq `cat $cpu0freqd/cpuinfo_max_freq`fiif [ -n "$UP_THRESHOLD" -a ${governor} == "ondemand" ]; thenadjust_cpufreq ondemand/up_threshold $UP_THRESHOLDfiif [ -n "$DOWN_THRESHOLD" -a ${governor} == "conservative" ]; thenadjust_cpufreq conservative/down_threshold $DOWN_THRESHOLDfiif [ "$IGNORE_NICE" -eq 1 -a ${governor} == "ondemand" -o ${governor} == "conservative" ]; thenadjust_cpufreq ${governor}/ignore_nice_load $IGNORE_NICEfiecho -n "Enabling ${governor} cpu frequency scaling: "successRETVAL=0fiecho# Technically, not quite right in non-cpuspeed daemon# cases, but close enough to indicate that we're# doing some sort of cpu frequency scaling.[ $RETVAL = 0 ] && touch $lockfileelseif [ -d "$xendir" ]; then$logger "CPU Frequency scaling is currently not supported on xen kernels"fireturn 0fireturn $RETVAL}stop() {if [ $(id -u) -ne 0 ]; thenecho -n "Insufficient privileges to stop cpuspeed service: "failure; echoreturn 4fiis_p4_clockmod && p4status="true"if [ "$p4status" == "true" -a "x${GOVERNOR}" == "x" ]; thenecho "p4-clockmod passive cooling support cannot be truly stopped"fi[ ! -f ${cpu0freqd}/scaling_driver ] && return 0drv=`cat ${cpu0freqd}/scaling_driver`governor_module_loaded && module_loaded=trueif [ "${drv}" != "intel_pstate" -a "${governor}" != "userspace" ] ||[ "${drv}" == "intel_pstate" -a "${governor}" != "powersave" ]; thenecho -n "Disabling ${governor} cpu frequency scaling: "$logger "Disabling ${governor} cpu frequency scaling governor"for cpu in ${cpus}; doif [ "${drv}" == "intel_pstate" ]; thenecho powersave > $cpu/cpufreq/scaling_governorelseecho userspace > $cpu/cpufreq/scaling_governorcat $cpu/cpufreq/cpuinfo_max_freq > $cpu/cpufreq/scaling_setspeedfidoneif [ $module_loaded == true ]; then/sbin/modprobe -r cpufreq-${governor}fisuccessRETVAL=0elsestop_cpuspeedRETVAL=$?fiecho[ -n "$DRIVER" ] && /sbin/modprobe -r $DRIVER[ $RETVAL = 0 ] && RETVAL=$?[ $RETVAL = 0 ] && rm -f $lockfilereturn $RETVAL}reload() {if [ $(id -u) -ne 0 ]; thenecho -n "Insufficient privileges to stop cpuspeed service: "failure; echoreturn 4figovernor_module_loaded && module_loaded=trueif [ "${governor}" == "userspace" ]; thenfailure; echoreturn 3elseif [ -n "$MIN_SPEED" ]; thenadjust_cpufreq scaling_min_freq $MIN_SPEEDfiif [ -n "$MAX_SPEED" ]; thenadjust_cpufreq scaling_max_freq $MAX_SPEEDfiif [ -n "$UP_THRESHOLD" -a ${governor} == "ondemand" ]; thenadjust_cpufreq ondemand/up_threshold $UP_THRESHOLDfiif [ -n "$DOWN_THRESHOLD" -a ${governor} == "conservative" ]; thenadjust_cpufreq conservative/down_threshold $DOWN_THRESHOLDfiif [ "$IGNORE_NICE" -eq 1 -a ${governor} == "ondemand" -o ${governor} == "conservative" ]; thenadjust_cpufreq ${governor}/ignore_nice_load $IGNORE_NICEfiecho -n "Reloading configuration for ${governor}: "success; echoreturn 0fi}case "$1" instart)start;;stop)stop;;status)is_p4_clockmod && p4status="true"if [ "$p4status" == "true" -a "x${GOVERNOR}" == "x" ]; thenecho "p4-clockmod passive cooling is enabled"exit 0figovernor_module_loaded && module_loaded=trueif [ -d "$xendir" ]; thenecho "Frequency scaling not supported under xen kernels"RETVAL=0elif [ $module_loaded == true -o ${governor} == "performance" ]; thenecho "Frequency scaling enabled using ${governor} governor"RETVAL=0elsestatus $progRETVAL="$?"fi;;restart|force-reload)stopstart;;reload)reload;;condrestart|try-restart)governor_module_loaded && module_loaded=trueif [ $module_loaded == true -o -n "`pidof $prog`" -o ${governor} == "performance" ]; thenstopstartfi;;*)echo $"Usage: $0 {start|stop|restart|condrestart|status}"exit 2;;esacexit $RETVAL