From 97a0a4d29d6f95930fa43127b5cc23b765ca4016 Mon Sep 17 00:00:00 2001 From: Michael Shigorin Date: Fri, 4 Nov 2011 18:17:25 +0200 Subject: [PATCH 1/2] reworked scripts and defautls a bit Added: - "unknown" AC state (defaulting to ondemand governor) - attempt to load the missing governor module - libshell use for error reporting Changed: - service is on by default: it's pretty safe, remove the package if unneeded - default AC_ON state CPU governor changed to ondemand: it is vastly preferred to performance governor in either notebook, desktop, or server case regarding power, heat, and noise --- cpufreq-simple/cpufreq-simple | 38 +++++++++++++++++++----------- cpufreq-simple/cpufreq-simple.init | 11 ++++----- cpufreq-simple/cpufreq-simple.sysconfig | 5 +++- 3 files changed, 33 insertions(+), 21 deletions(-) mode change 100644 => 100755 cpufreq-simple/cpufreq-simple mode change 100644 => 100755 cpufreq-simple/cpufreq-simple.sysconfig diff --git a/cpufreq-simple/cpufreq-simple b/cpufreq-simple/cpufreq-simple old mode 100644 new mode 100755 index 3bef32d..50931e7 --- a/cpufreq-simple/cpufreq-simple +++ b/cpufreq-simple/cpufreq-simple @@ -4,18 +4,25 @@ CPU= GOVERNOR_AC_ON= GOVERNOR_AC_OFF= +. shell-error + [ -f /etc/sysconfig/cpufreq-simple ] && . /etc/sysconfig/cpufreq-simple CPUFREQ=/sys/devices/system/cpu/cpu0/cpufreq +GOVERNORS="$CPUFREQ/scaling_available_governors" cmd="${1-}" get_ac_state() { - [ -d /proc/acpi/ac_adapter/ ] || return 1 - - local state_file="$(find /proc/acpi/ac_adapter/ -name state | head -1)" - - [ -n "$state_file" -a -r "$state_file" ] && sed 's;^state:[[:blank:]]*;;' "$state_file" | head -1 + local state_file + if [ -d /proc/acpi/ac_adapter/ ]; then + state_file="$(find /proc/acpi/ac_adapter/ -name state | head -1)" + if [ -n "$state_file" -a -r "$state_file" ]; then + sed 's;^state:[[:blank:]]*;;' "$state_file" | head -1 + fi + else + echo "unknown" + fi } init_command() @@ -26,27 +33,28 @@ init_command() set_cpufreq() { + # getopt seems overkill right now + if [ "$1" = "-g" -a -n "$2" ]; then + grep -Fq -- "$2" "$GOVERNORS" || modprobe "cpufreq_$2" + fi for i in $CPU; do cpufreq-set -c "$i" "$@" done } -if ! [ -f "$CPUFREQ/scaling_governor" -a -f "$CPUFREQ/scaling_available_governors" ]; then - echo "Your system is not configured correctly to support cpu frequency scaling" >&2 - exit 1 +if ! [ -f "$CPUFREQ/scaling_governor" -a -f "$GOVERNORS" ]; then + fatal "system not configured correctly for CPU frequency scaling" fi [ -n "$cmd" ] || cmd="$(init_command)" if [ -z "$cmd" ]; then - echo "Couldn't to set initial settings" >&2 - exit 1 + fatal "couldn't apply initial settings" fi [ -n "$CPU" ] || CPU="$(grep '^processor' /proc/cpuinfo | sed 's/^.*: //;' | tr '\n' ' ')" if [ -z "$CPU" ]; then - echo "Couldn't to detect CPUs number" >&2 - exit 1 + fatal "couldn't detect the number of CPUs" fi case "$cmd" in @@ -56,8 +64,10 @@ case "$cmd" in ac-off-line) [ -z "$GOVERNOR_AC_OFF" ] || set_cpufreq -g "$GOVERNOR_AC_OFF" ;; + ac-unknown) + [ -z "$GOVERNOR_AC_UNKNOWN" ] || set_cpufreq -g "$GOVERNOR_AC_UNKNOWN" + ;; *) - echo "Unknown command: '$cmd'" >&2 - exit 1 + fatal "unknown command: '$cmd'" ;; esac diff --git a/cpufreq-simple/cpufreq-simple.init b/cpufreq-simple/cpufreq-simple.init index 5c765e2..e836533 100644 --- a/cpufreq-simple/cpufreq-simple.init +++ b/cpufreq-simple/cpufreq-simple.init @@ -2,9 +2,8 @@ # # Load kernel modules needed to enable cpufreq scaling # -# chkconfig: - 12 90 -# description: Make it possible to save power by reducing -# the CPU speed when there is little to do. +# chkconfig: 345 12 90 +# description: Save power when idling # Do not load RH compatibility interface. WITHOUT_RC_COMPAT=1 @@ -48,7 +47,7 @@ start() echo if [ $RETVAL -eq 0 ]; then - action "Set cpufreq settings: " cpufreq-simple + action "Tune up cpufreq: " cpufreq-simple RETVAL="$?" fi @@ -79,8 +78,8 @@ case "$1" in # Nothing to do ;; status) - is_loaded && echo "Cpufreq is enabled" || - echo "Cpufreq is disabled" + is_loaded && echo "cpufreq is enabled" || + echo "cpufreq is disabled" ;; *) msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}" diff --git a/cpufreq-simple/cpufreq-simple.sysconfig b/cpufreq-simple/cpufreq-simple.sysconfig old mode 100644 new mode 100755 index db6057f..abe63a1 --- a/cpufreq-simple/cpufreq-simple.sysconfig +++ b/cpufreq-simple/cpufreq-simple.sysconfig @@ -3,11 +3,14 @@ #CPU= # Scalling governor used when AC is on. -GOVERNOR_AC_ON=performance +GOVERNOR_AC_ON=ondemand # Scalling governor used when AC is off. GOVERNOR_AC_OFF=ondemand +# Scalling governor used when AC state is unknown. +GOVERNOR_AC_UNKNOWN=ondemand + # Cpufreq module. If not set then will be detected # automatically by detect-cpufreq-module script. #MODULE= -- 1.7.7.1