Difference between revisions of "CPU frequency scaling (简体中文)"
m (Remove from Daemon category. See Talk:Table of Contents#Remove Category:Daemons and system services.)
|Line 1:||Line 1:|
[[Category:Power management (简体中文)]]
[[Category:Power management (简体中文)]]
[[cs:CPU Frequency Scaling]]
[[cs:CPU Frequency Scaling]]
Revision as of 02:44, 28 November 2012
|Summary help replacing me|
|An overview of the popular userspace tools for the kernel CPUfreq subsystem.|
|Laptop Mode Tools|
cpufreq 涉及到内核中实现CPU频率调节的基础功能。 这项技术让操作系统可以调节CPU频率的高低以达到省电的目的。 CPU频率可以根据系统负载动态调节（通过响应 ACPI 事件）,或者由用户空间的程序手动调节。
- 1 cpupower
- 2 设置
- 2.1 CPU 频率调节驱动程序
- 2.2 调速器 (CPU 电源方案)
- 2.3 守护进程
- 2.4 Privilege Granting Under GNOME
- 2.5 Laptop Mode Tools
- 3 Troubleshooting
是一个辅助 CPU 频率调节功能的一系列用户空间工具。没有此软件包也能进行 CPU 内存调节，但是建议安装此软件包，因为它提供了命令行工具，可以在启动时修改调节方法。
设置 CPU 频率调节需要三步：
- 加载合适的 CPU 频率调节驱动程序
要使频率调节正常工作，操作系统必须知道您的 CPU 所具有的一些限制，为此，可以加载一个能读取、管理您的 CPU 规格的内核驱动。注意这些模块可能需要你在BIOS里启用一些功能，如：Speedstep, Cool and Quiet（凉又静）, PowerNow!,或者ACPI
acpi-cpufreq 驱动(对于Intel CPU)，或者
powernow-k8 (对于 AMD K8/K10 处理器，包括Athlon 64, Opteron, and Phenom)。这些驱动针对32位和64位内核都有，即使你在64位CPU上运行32位内核它们也很合适。
$ ls /lib/modules/$(uname -r)/kernel/drivers/cpufreq/
以下表格列出了部分可用的频率调节驱动(Adapted from 内核源代码
|acpi-cpufreq||该CPU频率调节驱动利用了硬件的ACPI Processor Performance States功能. 这个驱动也支持Intel Enhanced Speedstep (这个特性以前是由deprecated speedstep-centrino模块支持的).|
|speedstep-ich||CPUFreq driver for certain mobile Intel Pentium III (Coppermine), all mobile Intel Pentium III-M (Tualatin) and all mobile Intel Pentium 4 P4-M on systems which have an Intel ICH2, ICH3 or ICH4 southbridge.|
|speedstep-smi||CPUFreq driver for certain mobile Intel Pentium III (Coppermine), all mobile Intel Pentium III-M (Tualatin) on systems which have an Intel 440BX/ZX/MX southbridge.|
|powernow-k7||CPUFreq driver for mobile AMD K7 mobile processors.|
|cpufreq-nforce2||CPUFreq driver for FSB changing on nVidia nForce2 platforms. (AMD K7, Socket A)|
|pcc-cpufreq||This driver supports Processor Clocking Control interface by Hewlett-Packard and Microsoft Corporation which is useful on some Proliant servers.|
|p4_clockmod|| CPUFreq driver for Intel Pentium 4 / Xeon / Celeron processors. When enabled it will lower CPU temperature by skipping clocks.|
You probably want to use a Speedstep driver instead.
手动加载 CPU 频率调节驱动:
# modprobe acpi-cpufreq
/etc/rc.conf 的 MODULES 列，如：
MODULES=( acpi-cpufreq vboxdrv fuse fglrx iwl3945 ... )
FATAL: Error inserting acpi_cpufreq ([...]/acpi-cpufreq.ko): No such device
加载合适的 cpufreq 驱动后，您可以用以下命令来查看您的 CPU 的详细信息：
cpufreq-info (Intel Duo Core T2500):
analyzing CPU 0: driver: acpi-cpufreq CPUs which need to switch frequency at the same time: 0 1 hardware limits: 1000 MHz - 2.00 GHz available frequency steps: 2.00 GHz, 1.67 GHz, 1.33 GHz, 1000 MHz available cpufreq governors: ondemand, performance current policy: frequency should be within 1000 MHz and 2.00 GHz. The governor "performance" may decide which speed to use within this range. current CPU frequency is 2.00 GHz. analyzing CPU 1: driver: acpi-cpufreq CPUs which need to switch frequency at the same time: 0 1 hardware limits: 1000 MHz - 2.00 GHz available frequency steps: 2.00 GHz, 1.67 GHz, 1.33 GHz, 1000 MHz available cpufreq governors: ondemand, performance current policy: frequency should be within 1000 MHz and 2.00 GHz. The governor "performance" may decide which speed to use within this range. current CPU frequency is 2.00 GHz.
调速器 (CPU 电源方案)
调速器可以被看作是预先设置好的 CPU 电源方案，它们必须被作为内核模块加载才能够被 kpowersave 和 gnome-power-manager 之类的程序识别。您可以加载多个调速器，但是在同一时间您只能激活一个。
performance(默认) -- Performance 调速器使 CPU 运行在最高频率，它已经被编译进内核。
cpufreq_ondemand(推荐使用) -- 根据系统负载动态调节 CPU 频率。
cpufreq_conservative-- 与 ondemand 相似但更为保守 (clock speed changes are more graceful)
cpufreq_powersave（节能模式） -- 使 CPU 运行在最低频率。
cpufreq_userspace（手动模式）-- 由用户手动选择 CPU 频率。
对于桌面应用和大部分系统来说, ondemand 方案可以在散热能力、电源消耗、机器性能和易用性方面提供较好的平衡。对于笔记本和手机系统，conservative 方案可以显著地降低电量消耗。
# modprobe cpufreq_ondemand # modprobe cpufreq_userspace
/etc/rc.conf 的 MODULES 列然后重新启动。
MODULES=(acpi-cpufreq cpufreq_ondemand cpufreq_powersave)
# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
作为选择，您可以以 root 执行
# cpufreq-set -c 0 -g ondemand
# for i in 0 1 2 3; do cpufreq-set -c $i -g ondemand; done
# for i in 0 1; do cpufreq-set -c $i -g ondemand; done
# cpufreq-set -r -g ondemand
# cpufreq-set -c 1 -g ondemand -u 2.00Ghz -d 1.00Ghz
# cpufreq-set -c 0 -f 2.50Ghz # cpufreq-set -c 1 -f 800Mhz
cpufreq-set --help 或者
man cpufreq-set 来获得更多信息。
$ watch grep \"cpu MHz\" /proc/cpuinfo
or (as root, so not nearly as useful)
# watch -n.5 cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
# watch -n.5 cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
Improving on-demand performance
With the out-of-the-box configuration, the ondemand governor will result in a slight but measurable and noticeable loss of performance. It will not clock up the CPU when it is at lower than 95% usage, and will sample the usage at the fastest possible frequency when at full clock speeds in order to clock back down as soon as possible.
Tuning the governor for your needs can reduce the performance loss to the point where it is negligible/non-existent if you are willing to lose a lot of the power savings. If you just want to save power while idle, lowering the up_threshold to 11% and raising the sample_down_factor by an order of magnitude can accomplish this.
Tunables are available in
/sys/devices/system/cpu/cpufreq/ondemand/ once the governor is loaded and selected and can be preserved at reboot using
/etc/tmpfiles.d. For example:
w /sys/devices/system/cpu/cpufreq/ondemand/up_threshold - - - - 25
Changing the on-demand governor's threshold
To change when the
ondemand governor switches to a higher multiplier, one can manipulate
/sys/devices/system/cpu/cpufreq/ondemand/up_threshold. Determine the current setting by issuing the following command as root:
# cat /sys/devices/system/cpu/cpufreq/ondemand/up_threshold
The value returned should be
95, the default setting as of kernel version 3.0. This means that the
ondemand governor currently increases the clock rate if a core reaches 95% utilization. This can be changed, for example:
# echo -n 15 > /sys/devices/system/cpu/cpufreq/ondemand/up_threshold
On-demand governor sampling_down_factor
sampling_down_factor is another global ondemand global tunable.
sampling_down_factor set to
1 makes no changes from existing behavior, but having
sampling_down_factor set to a value greater than 1 (e.g. 100) causes it to act as a multiplier for the scheduling interval for re-evaluating load when the CPU is at its highest clock frequency due to high load. This improves performance by reducing the overhead of load evaluation and helping the CPU stay at its highest clock frequency when it is truly busy, rather than shifting back and forth in speed. This tunable has no effect on behavior at lower frequencies/lower CPU loads.
Read the value (default: 1):
# cat /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
Set the value:
# echo -n 10 > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
Tuning conservative governor
While the conservative governor switches smoothly and utilizes all of the available frequencies, users may want to tune it further. Out-of-the-box it will clock the CPU up when load reaches 75% and clock down when load drops to 20%. As with the on-demand governor, decreasing the
up_threshold may improve performance and responsiveness.
Tunables are available in
/sys/devices/system/cpu/cpufreq/conservative/. Refer to the #Improving ondemand performance section for important advice regarding making this changes permanent, and potential daemon loading issues.
Changing the conservative governor's thresholds
Determine the current
up_threshold setting by issuing the following command as root:
# cat /sys/devices/system/cpu/cpufreq/conservative/up_threshold
The value returned should be
75, the default setting as of kernel version 3.0. This means that the
conservative governor currently increases the clock rate to the next highest speed if a core reaches 75% utilization. The can be changed, for example:
# echo -n 40 > /sys/devices/system/cpu/cpufreq/conservative/up_threshold
down_threshold value can be read and modified via
/sys/devices/system/cpu/cpufreq/conservative/down_threshold. The default value should be
20 as of kernel version 3.0. This means that the
conservative governor decreases the clock rate to the next lowest speed if a core falls to 20% utilization, which is a sensible default.
While the down sampling rate can also be adjusted for the
conservative governor, increasing it may only help with occasional low usage CPU spikes during high usage applications, as the
down_threshold is a much more direct control for down scaling which does not exist on the
Be aware that setting
down_threshold too close to
up_threshold may result in constant CPU switching, which might be something desirable for certain users and not for others. Setting
up_threshold too low may result in the CPU being clocked higher than it needs sacrificing power saving for performance, and setting
up_threshold too high may result in decreased performance, but reduced power consumption. Experiment to find the optimal values for your system and your needs.
Interaction with ACPI events
Users may configure scaling governors to switch automatically based on different ACPI events such as connecting the AC adapter or closing a laptop lid. A quick example is given below, however it may be worth reading full article on acpid.
Events are defined in
/etc/acpi/handler.sh. If the package is installed, the file should already exist and be executable. For example, to change the scaling governor from
conservative when the AC adapter is disconnected and change it back if reconnected:
[...] ac_adapter) case "$2" in AC*) case "$4" in 00000000) echo "conservative" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor echo -n $minspeed >$setspeed #/etc/laptop-mode/laptop-mode start ;; 00000001) echo "performance" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor echo -n $maxspeed >$setspeed #/etc/laptop-mode/laptop-mode stop ;; esac ;; *) logger "ACPI action undefined: $2" ;; esac ;; [...]
也安装了一个守护进程，可以在启动系统的时候就设置调速器和 CPU 的最高、最低频率，而不需要额外的如 kpowersave 之类的程序。这对于运行轻量级桌面，如 Openbox 的人来说是一个完善的解决方案。
在启动守护进程之前，以 root 编辑
/etc/conf.d/cpufreq，选择需要的调速器并且设置 CPU 的最高、最低频率，例如：
#configuration for cpufreq control # valid governors: # ondemand, performance, powersave, # conservative, userspace governor="ondemand" # valid suffixes: Hz, kHz (default), MHz, GHz, THz min_freq="1GHz" max_freq="2GHz"
# rc.d start cpufreq
/etc/rc.conf 的 DAEMONS 列，如：
DAEMONS=(syslog-ng networkmanager @alsa @crond @cupsd @cpufreq)
Privilege Granting Under GNOME
GNOME has a nice applet to change the governor on the fly. To use it without the need to enter the root password, simply create
/var/lib/polkit-1/localauthority/50-local.d/org.gnome.cpufreqselector.pkla and populate it with the following:
[org.gnome.cpufreqselector] Identity=unix-user:USER Action=org.gnome.cpufreqselector ResultAny=no ResultInactive=no ResultActive=yes0
Where the word
USER is replaced with the username of interest.
Laptop Mode Tools
If you are already using or plan to use Laptop Mode Tools for other power saving solutions, then you may want to let it also manage CPU frequency scaling. To do so, you just have to insert the appropriate frequency driver to the
MODULES array in
/etc/rc.conf (see #CPU frequency driver above) and then go through the
/etc/laptop-mode/conf.d/cpufreq.conf file to define governors, frequencies and policies. You will not need to load other modules and daemons or to set up scaling governors and interaction with ACPI events. Please refer to Laptop Mode Tools for more details.
- Some applications, like ntop, do not respond well to automatic frequency scaling. In the case of ntop it can result in segmentation faults and lots of lost information as even the
on-demandgovernor cannot change the frequency quickly enough when a lot of packets suddenly arrive at the monitored network interface that cannot be handled by the current processor speed.
- Some CPU's may suffer from poor performance with the default settings of the
on-demandgovernor (e.g. flash videos not playing smoothly or stuttering window animations). Instead of completely disabling frequency scaling to resolve these issues, the aggressiveness of frequency scaling can be increased by lowering the up_threshold sysctl variable for each CPU. See #Changing the on-demand governor's threshold.
- Sometimes the on-demand governor may not throttle to the maximum frequency but one step below. This can be solved by setting max_freq value slightly higher than the real maximum. For example, if frequency range of the CPU is from 2.00 GHz to 3.00 GHz, setting max_freq to 3.01 GHz can be a good idea.
- Some CPU/BIOS configurations may have difficulties to scale to the maximum frequencies or scale to higher frequencies at all. Sadly there is only a workaround right now. Add "processor.ignore_ppc=1" to your kernel boot line and/or edit the value in
/sys/module/processor/parameters/ignore_ppcfrom 0 to 1.
- It seems to be fixed at least since kernel 3.0, for Toshiba NB-100.
- But this still seems not to be true for my fresh Thinkpad X60 Setup. The 32bit Intel Core Duo T2400 is still usually fixed at 1GHz with kernel 3.2.6-2 while only with the ignore_ppc option set (as described above) it's able to scale up to 1.83GHz like expected.
- Some combinations of ALSA drivers and sound chips may cause audio skipping as the governor changes between frequencies, switching back to a non-changing governor seems to stop the audio skipping.