HOWTO: NUT & CPU Throttling/Frequency Scaling

After a couple of power cuts lasting long enough for my UPS to shutdown my PC, I’ve been meaning to get NUTs to set my CPU to lowest speed possible when running on battery power, this week I finally got round to it. Please note these instructions are based on Debian 4.0 (Etch) and may require extra steps to work on different distro’s.

For this to work you have to:

  1. Have a CPU that support Frequency Scaling
  2. Have CPU Frequency scaling working
  3. Use NUT (Network UPS Tools)

Here what you need to do to get thing working

  • Check you what ‘Governors’ you have available
    You need to make sure the correct modules are loaded so that you can scale to the correct settings. To see what you do have loaded run:
    cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
    You’ll need at least powersave and one other. Powersave should set the CPU Frequency to the lowest setting. If you don’t have powersave you’ll need to load the module. As root run:
    modprobe cpufreq_powersave and add cpufreq_powersave to the file
    /etc/modules.

    To test that it is working, ideally run something that force you CPU to 100 for a while (say compress a large folder) and then run
    cpufreq-selector --governor powersave
    sleep 1
    cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
    cpufreq-selector --governor userspace
    sleep 1
    cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq

    The first number given should be the lowest CPU frequency and the last should be the highest (assuming the process is using a lot of CPU time still)

  • Add a few new items to upssched.conf
    Using you’re favorite editor open up /etc/nut/upssched.conf and add the following line:
    AT ONBATT * START-TIMER powersave 30
    AT ONLINE * CANCEL-TIMER powersave
    AT ONLINE * START-TIMER userspace 30
    AT ONBATT * CANCEL-TIMER userspace

    I’ve used a timer (30 seconds) to smooth out jumpy power from triggering off any actions.
  • Add the command to the CMDSCRIPT file to change the CPU frequency govenor
  • Edit the cmdscript file (the path is given in upssched.conf on the line starting CMDSCRIPT. This file probably doesn’t exist – so just create a new one. Assuming it doesn’t exist add the following lines:
    #! /bin/sh
    case $1 in
    powersave)
    /usr/bin/cpufreq-selector --governor powersave && logger -t upssched-cmd "Setting CPU Governor to powersave"
    ;;
    userspace)
    /usr/bin/cpufreq-selector --governor userspace && logger -t upssched-cmd "Setting CPU Governor to userspace"
    ;;
    *)
    logger -t upssched-cmd "Unrecognized command: $1"
    ;;
    esac

  • Test Unplug you’re UPS for 1 min, plug back in and look at /var/log/syslog – you should see that NUTs has throttled you’re CPU.

I haven’t had a power cut since I’ve implemented this, but from looking at the stats from the UPS I should gain another 7 min or so of battery time – an extra 21%. An improvement on this may be stopping some process and task when on battery and starting them again when back on mains. But for now, at least untill the next power cut this will do for me.

May 21, 2007 · robert · Comments Closed
Posted in: Debian, Hardware, Howtos, Linux