home > linux > optimisation > cpu-frequency-scaling-via-ac-adapter

CPU frequency scaling via AC Adapter

19 | 21 Nov 2016

Scaling the CPU frequency enables the machine to either increase or decrease power consumption to increase or decrease cpu performance. Based off the AC Adapter status the CPU frequency can be auto scaled to either provide performance or conserve battery.

Detecting AC Status

The AC adapter and or battery status can be derived from ACPI. The events triggered in the firmware trigger ACPI events which notify the Kernel. When the kernel detects events they are made availible to userspace via the acpi module (CONFIG_ACPI).

In userspace you can choose which acpi events to listen to / action on by placing a configuration file in /etc/acpi/events. The configuration file will be processed by acpid.

mike@mike-laptop4:~$ sudo apt-get install acpid

ACPI event configuration file

cat /etc/acpi/events/ac_adapter-acpi-profile

# ac on/off
event=ac_adapter ACPI0003:00.*
action=/etc/acpi/ac-adapter-profile.sh %e

It's worth noting changes to the configuration file do require a restart of the acpi daemon.

ACPI Event Script

/etc/acpi/ac-adapter-profile.sh

#!/bin/bash

# ac off
# ac_adapter ACPI0003:00 00000080 00000000
# battery PNP0C0A:00 00000080 00000001

# ac on
# ac_adapter ACPI0003:00 00000080 00000001
# battery PNP0C0A:00 00000080 00000001
#  PNP0C14:00 00000080 00000000
#  battery PNP0C0A:00 00000081 00000001
#  battery PNP0C0A:00 00000080 00000001

if [ "$4" == "00000000" ]; then
    # Reduce Brightness on Battery
    echo 9 > /sys/class/backlight/acpi_video0/brightness

    # Reduce CPU frequency profile
    for core in /sys/devices/system/cpu/cpu[0-9]*; do
        echo powersave > $core/cpufreq/scaling_governor
    done

    # Disable CPU BOOST, (max 1.8Ghz)
    echo 0 > /sys/devices/system/cpu/cpufreq/boost
elif [ "$4" == "00000001" ]; then
    # Increase Brightness on AC Power
    echo 22 > /sys/class/backlight/acpi_video0/brightness

    # Set CPU to ondemand (default cpu freq profile)
        for core in /sys/devices/system/cpu/cpu[0-9]*; do
        echo ondemand > $core/cpufreq/scaling_governor
    done    

    # Enable CPU Boost, (max 3.2Ghz)
    echo 1 > /sys/devices/system/cpu/cpufreq/boost
fi

All in place, when the power cable is unplugged the screen will dim, and a power saving cpu frequency profile will be enabled.

Comments

Posted by syscad on 22/12/2016 08:09 nice stuff thanks

Post a Comment