Slackware Linux Tip-of-the-day: Power savings/CPU frequency scaling

607

Some Linux laptop users may have noticed that the battery life under Linux outperforms the performance under windows or Mac. The reason for this is the cpu frequency scaling modules and utilities, some distros enable this by default while others like Slackware leave the choice to the user. 

What this function does is throttle the power used by your processor which slows down the system when little performance is needed and increases the power when full performance is needed. There are multiple settings depending on your needs but for most production servers and laptops setting up the automatic throttling is a good practice because it will save you battery life and power consumption.

Recently when looking in the /etc/rc.d/rc.modules file in Slackware  13.0, I noticed that Pat has slipped an auto-configure script in for frequency scaling, the extract which starts on line 814 is shown below: 

### CPU frequency scaling support

#

# Below, set CPUFREQ to enable CPU frequency scaling to save system power.

#

# To always try to use CPU frequency scaling, set to:  on

# To never use CPU frequency scaling, set to:  off

# To use it only when the battery module is loaded (this will cause it to

# be used by default with most laptops), set to:  battery

#

CPUFREQ=battery


# If CPUFREQ=battery and the battery module is loaded, turn on CPUfreq.

if [ “$CPUFREQ” = “battery” ]; then

  if /sbin/lsmod | grep -wq battery ; then

    # CPUFREQ=battery and a battery was detected, so change CPUFREQ

    # to ‘on’ so that the block of script below will try to enable it.

    CPUFREQ=on

  fi

fi


### Enable CPU frequency scaling if requested:

if [ “$CPUFREQ” = “on” ]; then

  ### CPU frequency scaling modules for the Linux kernel CPUfreq subsystem.

  #

  # Clock scaling allows you to change the clock speed of the CPUs on the fly.

  # This is a nice method to save battery power, because the lower the clock

  # speed is, the less power the CPU consumes.

  # 

  # It should not hurt anything to try to load these modules.

  #

  # generic ACPI P-States based driver:

  /sbin/modprobe acpi-cpufreq 2>/dev/null

  # AMD mobile K6-2/3+ PowerNow!:

  /sbin/modprobe powernow-k6 2>/dev/null

  # AMD mobile Athlon PowerNow!:

  /sbin/modprobe powernow-k7 2>/dev/null

  # AMD Cool&Quiet PowerNow!:

  /sbin/modprobe powernow-k8 2>/dev/null

  # Intel SpeedStep using the SMI BIOS interface:

  /sbin/modprobe speedstep-smi 2>/dev/null

  # Intel SpeedStep on ICH-based chipsets:

  /sbin/modprobe speedstep-ich 2>/dev/null

  # Intel Enhanced SpeedStep :

  /sbin/modprobe speedstep-centrino 2>/dev/null

  # Intel Pentium4/Xeon clock modulation is not enabled by default.

  # The kernel documentation says “This adds the CPUFreq driver for Intel

  # Pentium 4 / XEON processors.  When enabled it will lower CPU temperature

  # by skipping clocks.  This driver should be only used in exceptional

  # circumstances when very low power is needed because it causes severe

  # slowdowns and noticeable latencies.  Normally Speedstep should be used

  # instead.”

  # If you still want to try the Pentium4/Xeon module, uncomment the next line:

  #/sbin/modprobe p4-clockmod 2>/dev/null

  # NatSemi Geode GX / Cyrix MediaGXm:

  /sbin/modprobe gx-suspmod  2>/dev/null

  # Transmeta Crusoe / Efficeon LongRun:

  /sbin/modprobe longrun  2>/dev/null

  # VIA Cyrix Longhaul:

  /sbin/modprobe longhaul  2>/dev/null

  # nForce2 FSB changing cpufreq driver:

  /sbin/modprobe cpufreq-nforce2 2>/dev/null

  # Enhanced PowerSaver driver for VIA C7 CPUs:

  /sbin/modprobe e_powersaver 2>/dev/null


  ### CPU frequency scaling policies:

  #

  # Use the CPUFreq governor ‘powersave’ as default.  This sets the

  # frequency statically to the lowest frequency supported by the CPU.

  #/sbin/modprobe cpufreq_powersave

  #

  # Use the CPUFreq governor ‘performance’ as default. This sets the

  # frequency statically to the highest frequency supported by the CPU.

  #/sbin/modprobe cpufreq_performance

  #

  # Use the CPUFreq governor ‘conservative’ as default.  This allows you

  # to get a full dynamic frequency capable system by simply loading your

  # cpufreq low-level hardware driver.  Be aware that not all cpufreq

  # drivers support the ‘conservative’ governor — the fallback governor

  # will be the ‘performance’ governor.

  #/sbin/modprobe cpufreq_conservative

  #

  # Use the CPUFreq governor ‘ondemand’ as default.  This allows you to

  # get a full dynamic frequency capable system by simply loading your

  # cpufreq low-level hardware driver.  Be aware that not all cpufreq

  # drivers support the ‘ondemand’ governor — the fallback governor will

  # be the performance governor.  This seems to be the most-recommended

  # scaling policy, so rc.modules will try to load this by default.

  /sbin/modprobe cpufreq_ondemand


  ### CPU scaling governor:

  #

  # Set the default scaling_governor to be used (such as userspace or ondemand)

  # if there is a CPUFreq scaling policy module loaded that supports it:

  SCALING_GOVERNOR=ondemand

  #

  # Try to enable the scaling_governor selected above:

  if [ -r /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors ]; then

    if grep -wq “$SCALING_GOVERNOR” /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors ; then

      if [ -r /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; then

        for SYSCPUFILE in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor ; do

          echo “$SCALING_GOVERNOR” > $SYSCPUFILE

        done

      fi

    fi

  fi


fi # End enabling CPU scaling support


Your first option is to choose if you want to enable the frequency scaling,  on line 823 you can choose on, off or battery. on and off are self explanatory, but battery is quiet cool, if you choose battery the script will autocheck if you are running on a battery and if it is true then it will turn the support on.

The next step is to choose the module for your processor type, Pat’s script tried all available modules which will not hurt anything but will start your module if it is available. Once you have determined which module you use you can comment out the unused ones.

The next option is to choose  the policy/governor, explanations are listed above so I won’t give redundant data, but the simplest approach is to comment out the ones you don’t want to use and replace them with the governor you want to use.  For most environments ondemand is the best policy because it quickly recovers back to full performance when needed, however that governor used more power by making a quick jump so for laptops using the conservative policy will slow you down a bit but help to increase the life of your battery.

Now that you have all of the options chosen you only need to build and install the cpufrequtil package from Slackbuilds.org (http://slackbuilds.org/repository/13.0/libraries/cpufrequtils/), once that has been installed and the above listed options have been configured all you need to to is rerun the rc.module script or restart the computer. After the reboot you will notice a difference in your power usage and the heat generated by the processor.To confirm the scailing is enabled and running you you call the program cpufreq-info which will disply a similar output to what is shown below and tell you what is running and what frequency your system is running under at that point in time. 

cpufrequtils 005: cpufreq-info (C) Dominik Brodowski 2004-2006

Report errors and bugs to
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
, please.

analyzing CPU 0:

  driver: powernow-k8

  CPUs which need to switch frequency at the same time: 0 1

  hardware limits: 1000 MHz – 2.40 GHz

  available frequency steps: 2.40 GHz, 2.20 GHz, 2.00 GHz, 1.80 GHz, 1000 MHz

  available cpufreq governors: ondemand, userspace

  current policy: frequency should be within 1000 MHz and 2.40 GHz.

                  The governor “ondemand” may decide which speed to use

                  within this range.

  current CPU frequency is 1000 MHz (asserted by call to hardware).

analyzing CPU 1:

  driver: powernow-k8

  CPUs which need to switch frequency at the same time: 0 1

  hardware limits: 1000 MHz – 2.40 GHz

  available frequency steps: 2.40 GHz, 2.20 GHz, 2.00 GHz, 1.80 GHz, 1000 MHz

  available cpufreq governors: ondemand, userspace

  current policy: frequency should be within 1000 MHz and 2.40 GHz.

                  The governor “ondemand” may decide which speed to use

                  within this range.

  current CPU frequency is 1000 MHz (asserted by call to hardware).

I hope this helps, and I will follow up with more hidden features that I find in the latest release.