2014年2月6日

CPU power saving in Linux



Some CPUs support many frequencies. If the usage of CPU is low, we can control the CPU to low power mode for power saving.

Here, the test environment is Ubuntu 10.04 and x200 notebook. We have to install powernowd.

Lenovo Notebook X200, CPU (Pentium M) which supports 2401, 2400, 1600 and 800 MHz
CPU: Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz
OS: Ubuntu 10.04 TLS (Linux kernel: 2.6.32-26-generic SMP)

The powernowd daemon monitors the usage of the CPU periodically.

We can configure the /etc/default/powernowd to set the top bound and bottom bound of CPU usage.

The powernowd adjusts automatically the CPU frequency according to our policy.


[Control the CPU frequency manually]

Script code: (set_cpu.sh)

#!/bin/bash

# Lenovo x200 support: 800 MHz, 1600 MHz, 2400 MHz and 2401 MHz
# Refer to /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_available_frequencies

num=$#
if [ $num -lt 2 ]; then
echo "$0 cpu_id cpu_freq"
exit 1;
fi

cpu="$1"
cpufreq="$2""000"

echo "Set CPU$cpu with $2 MHz"

# Refer to /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_available_governors

echo "userspace" > /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_governor


# Set the bottom bound of the CPU frequency
echo "800000" > /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_min_freq

# Set the top bound of the CPU frequency
echo "2401000" > /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_max_freq

# Set the current CPU frequency
echo "$cpufreq" > /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_setspeed

# Confirm
echo "cat /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_setspeed"
out=`cat /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_setspeed`
echo $out

cat /proc/cpuinfo |grep "cpu MHz"

exit 0;


Usage in a root shell session:
# bash set_cpu.sh cpu_id cpu_freq
( Change the cpu_id to the corresponding processor id in /proc/cpuinfo, and set cpufreq as your frequency, ex. bash set_cpu.sh 0 800 )

Note: If we want to control the CPU frequency manually, we have to disable the powernowd, the shell session is shown as following:

$ sudo apt-get remove powernowd cpudyn cpufreqd powersaved speedfreqd


Further Information:

driver: acpi-cpufreq

/sys/devices/system/cpu/cpu0/cpufreq
/sys/devices/system/cpu/cpufreq/ondemand (decides the speed to use)

cpufreq-info shows the cpufreq stats (records the ratio of every frequency)

/proc/acpi adjusts the light and dark and information of battery.

example: echo "70" > /proc/acpi/video/VID/LCD0/brightness

/proc/acpi/ac_adapter/AC (Check if AC power)

/proc/acpi/ibm: Control IBM devices

2.6.26 (mp)

In the directory: /sys/devices/system/cpu/cpu?

Enable/Disable CPU: online { 1, 0 }

/sys/devices/system/cpu/sched_smt_power_savings

0 - No power saving load balance (default value)
1 - Fill one thread/core/package first for long running threads
2 - Also bias task wakeups to semi-idle cpu package for power savings

/sys/devices/system/cpu/kernel_max

kernel_max: the maximum cpu index allowed by the kernel configuration.

offline: cpus that are not online because they have been HOTPLUGGED off
or exceed the limit of cpus allowed by the kernel configuration
(kernel_max above).


online: cpus that are online and being scheduled.

possible: cpus that have been allocated resources and can be
brought online if they are present.

present: cpus that have been identified as being present in the system.

Reference from the Linux kernel source 2.6.32 and 2.6.36

Documentation/cputopology.txt for more information.
Documentation/cpuidle/.
Documentation/ABI/testing/sysfs-devices-system-cpu.
http://www.pantz.org/software/cpufreq/usingcpufreqonlinux.html


沒有留言:

張貼留言

歡迎留言