Difference between revisions of "CPU frequency scaling (简体中文)"

From ArchWiki
Jump to: navigation, search
Line 21: Line 21:
 
{{Article summary end}}
 
{{Article summary end}}
  
cpufreq 涉及到内核中实现CPU频率调节的基础功能。 这项技术让操作系统可以调节CPU频率的高低以达到省电的目的。 CPU频率可以根据系统负载动态调节(通过响应 ACPI 事件),或者由用户空间的程序手动调节。
+
cpufreq引用了内核架构中的CPU频率调整部分相关功能。这项技术使得操作系统能够提高或降低CPU速度来达到省电目的。CPU频率可以根据系统负荷自动调整,或响应ACPI事件而调整,或通过用户空间程序手工调整。
  
使用该功能至少要加载所需的内核模块,然后设置一个调速器。 更高级的设置包括使[[#cpufrequtils]], [[acpid]], [[laptop-mode-tools]], 以及你桌面环境提供的 GUI 工具协调工作。
+
从内核3.4开始,必要的模块都会自动加载,而且推荐的调速器[[CPU_Frequency_Scaling#Scaling_governors|ondemand governor]]默认启动。但是,用户空间程序,例如[[#cpupower|cpupower]],[[acpid]][[laptop-mode-tools]],或你的桌面系统对应的图形化工具,在进行高级特性配置时可能仍然需要被用到。
  
 
==cpupower==
 
==cpupower==
 +
{{Pkg|cpupower}}是一组设计为辅助''CPU frequency scaling''的一组用户空间工具。为使用频率调整的功能,这个软件包并不是必须的,但强烈建议安装,因为它提供了方便的命令行工具和在启动时改变调速器的服务(参考下文[[#守护进程]])。
  
{{Pkg|cpupower}}是一个辅助 CPU 频率调节功能的一系列用户空间工具。没有此软件包也能进行 CPU 内存调节,但是建议安装此软件包,因为它提供了命令行工具,可以在启动时修改调节方法。
+
{{Tip|在你启动cpupower守护进程之前,请确保已经正确编辑过{{ic|/etc/conf.d/cpupower}},并且设置了期望的调速器/频率。}}
  
{{Tip|确保你在启动cpupower daemon之前编辑了 {{ic|/etc/conf.d/cpupower}} 文件并设置了你想要的模式/频率}}
+
{{注意|使用3.5或更新的内核后cpupower的附加功能不再工作,因为对它的支持已经从内核中移除。不要设置配置文件中的smp_scheduler或mc_scheduler值,否则守护进程将会启动失败。}}
  
{{注意|内核 3.5 之后移除了一些 cpupower 附加功能。不要设置 smp_scheduler 或 mc_scheduler,否则启动时会出错。}}
+
==配置==
 +
配置CPU频率调整有3个步骤:
 +
# 加载适合的CPU频率驱动
 +
# 加载期望的频率控制器
 +
# 选择一个管理切换和调整控制器的方法:
 +
#* 通过{{ic|/sys}}接口手工调整或通过软件包{{Pkg|cpupower}}
 +
#* 通过cpupower [[#守护进程]],
 +
#* 通过[[Laptop Mode Tools]],
 +
#* 通过[[acpid]]事件,
 +
#* 通过桌面工具和桌面小程序,
 +
#* 或以上方式的组合。
  
== 设置 ==
+
=== CPU频率驱动 ===
设置 CPU 频率调节需要三步:
+
{{注意|从内核3.4以后CPU频率驱动会根据你的CPU自动加载,因此此节中描述的手工加载部分理论上不再必要。}}
# 加载合适的 CPU 频率调节驱动程序
+
为使CPU频率调整正常工作,操作系统必须先知道CPU的频率限制。为达到这个目的,一个能够读取和管理CPU规格的内核模块将被加载。注意这些模块可能需要BIOS中的相关功能被打开。这些功能在BIOS中可能被描述为:''Speedstep'',''Cool and Quiet'',''PowerNow!''或''ACPI''。
# 加载需要的调速器
+
# 选择一个方法来管理开关和调节调速器
+
#* 手动通过编辑 {{ic|/sys}} 界面或 {{ic|cpufreq-set}} (该命令来自于{{Pkg|cpufrequtils}}工具集),
+
#* the cpufrequtils [[#Daemon]],
+
#* [[Laptop Mode Tools]],
+
#* [[acpid]] 事件,
+
#* 桌面环境工具或插件,
+
#* 同时使用以上的一种或几种方法.
+
  
=== CPU 频率调节驱动程序 ===
+
如果你使用64位处理器,你很可能会需要'''{{ic|acpi_cpufreq}}'''(Intel处理器)或'''{{ic|powernow-k8}}'''(AMD K8/K10处理器,如Athlon 64, Opteron, and Phenom等)。这些模块同时支持32位和64位内核,所以即使你在64位硬件下运行32位内核,它们很可能仍然是你所需要的模块。
要使频率调节正常工作,操作系统必须知道您的 CPU 所具有的一些限制,为此,可以加载一个能读取、管理您的 CPU 规格的内核驱动。注意这些模块可能需要你在BIOS里启用一些功能,如:Speedstep, Cool and Quiet(凉又静), PowerNow!,或者ACPI
+
  
如果你使用64位处理器,你可以简单地使用 '''{{ic|acpi-cpufreq}}''' 驱动(对于Intel CPU),或者 '''{{ic|powernow-k8}}''' (对于 AMD K8/K10 处理器,包括Athlon 64, Opteron, and Phenom)。这些驱动针对32位和64位内核都有,即使你在64位CPU上运行32位内核它们也很合适。
+
查看支持驱动的完整列表,请运行以下命令:
 
+
如果需要查看所有可用的(频率调节)驱动,运行以下代码:
+
  
 
  $ ls /lib/modules/$(uname -r)/kernel/drivers/cpufreq/
 
  $ ls /lib/modules/$(uname -r)/kernel/drivers/cpufreq/
  
以下表格列出了部分可用的频率调节驱动(Adapted from 内核源代码{{ic|<kernel source>/arch/x86/kernel/cpu/cpufreq/Kconfig}}).
+
以下列表是部分可用的频率驱动(来自内核源代码{{ic|<kernel source>/arch/x86/kernel/cpu/cpufreq/Kconfig}})。
 
{| border="1"
 
{| border="1"
!Module!!Description
+
!模块!!描述
 
|-
 
|-
| acpi-cpufreq || 该CPU频率调节驱动利用了硬件的ACPI Processor Performance States功能. 这个驱动也支持Intel Enhanced Speedstep (这个特性以前是由deprecated speedstep-centrino模块支持的).
+
| acpi_cpufreq || 支持ACPI Processor Performance States的CPUFreq驱动。该驱动也支持Intel Enhanced SpeedStep(之前由已过期的模块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-ich || 支持部分移动Intel奔腾III (Coppermine)的CPUFreq驱动。该驱动支持所有具备Intel ICH2, ICH3 或 ICH4 南桥芯片的系统中的移动Intel奔腾III-M (Tualatin)和移动Intel奔腾4 P4-M。
 
|-
 
|-
| 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.
+
| speedstep-smi || 支持部分移动Intel奔腾III (Coppermine)的CPUFreq驱动。该驱动支持所有具备Intel 440BX/ZX/MX南桥芯片系统中的移动Intel奔腾III-M (Tualatin)
 
|-
 
|-
| powernow-k8  || 这是K8/K10 Athlon64/Opteron/Phenom处理器的CPU频率调节驱动.
+
| powernow-k8  || 支持K8/K10 Athlon64/Opteron/Phenom处理器的CPUFreq驱动。
 
|-
 
|-
| powernow-k7  || CPUFreq driver for mobile AMD K7 mobile processors.
+
| powernow-k7  || 支持移动AMD K7处理器的CPUFreq驱动。
 
|-
 
|-
| cpufreq-nforce2 || CPUFreq driver for FSB changing on nVidia nForce2 platforms. (AMD K7, Socket A)
+
| cpufreq-nforce2 || 支持nForce2平台下外频调整的CPUFreq驱动(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.
+
| pcc-cpufreq  || 支持HP和微软提出的“处理器时钟频率控制”接口,这在一些Proliant服务器上很有用。
 
|-
 
|-
| p4_clockmod  || CPUFreq driver for Intel Pentium 4 / Xeon / Celeron processors.  When enabled it will lower CPU temperature by skipping clocks.<br>You probably want to use a Speedstep driver instead.
+
| p4_clockmod  || 支持Intel奔腾4 / 至强 / 赛扬 处理器的CPUFreq驱动。生效时会通过skipping clocks技术降低CPU发热量。<br>你可能更希望使用Speedstep驱动代替它。
 
|}
 
|}
 
<br>
 
<br>
手动加载 CPU 频率调节驱动:
+
手工加载CPU频率驱动:
  # modprobe acpi-cpufreq
+
  # modprobe acpi_cpufreq
 
+
注意如果你尝试加载错误的模块,你会得到类似这样的错误消息:
若要自动加载驱动,将合适的驱动添加到 {{ic|/etc/rc.conf}} 的 MODULES 列,如:
+
MODULES=( '''''acpi-cpufreq''''' vboxdrv fuse fglrx iwl3945 ... )
+
 
+
如果你尝试加载一个错误的模块,会得到一条错误信息,比如:
+
 
  FATAL: Error inserting acpi_cpufreq ([...]/acpi-cpufreq.ko): No such device
 
  FATAL: Error inserting acpi_cpufreq ([...]/acpi-cpufreq.ko): No such device
  
加载合适的 cpufreq 驱动后,您可以用以下命令来查看您的 CPU 的详细信息:
+
一旦合适的cpufreq驱动加载完成,CPU的详细信息就可以通过以下命令得到:
 
+
{{hc
$ cpufreq-info
+
|$ cpupower frequency-info
 
+
|<nowiki>
样板输出 {{ic|cpufreq-info}} (Intel Duo Core T2500):
+
analyzing CPU 0:
analyzing CPU 0:
+
 
   driver: acpi-cpufreq
 
   driver: acpi-cpufreq
   CPUs which need to switch frequency at the same time: 0 1
+
   CPUs which run at the same hardware frequency: 0 1 2 3
   hardware limits: 1000 MHz - 2.00 GHz
+
  CPUs which need to have their frequency coordinated by software: 0
   available frequency steps: 2.00 GHz, 1.67 GHz, 1.33 GHz, 1000 MHz
+
  maximum transition latency: 10.0 us.
 +
   hardware limits: 1.20 GHz - 2.40 GHz
 +
   available frequency steps: 2.40 GHz, 2.40 GHz, 2.27 GHz, 2.13 GHz, 2.00 GHz, 1.87 GHz, 1.73 GHz, 1.60 GHz, 1.47 GHz, 1.33 GHz, 1.20 GHz
 
   available cpufreq governors: ondemand, performance
 
   available cpufreq governors: ondemand, performance
   current policy: frequency should be within 1000 MHz and 2.00 GHz.
+
   current policy: frequency should be within 1.20 GHz and 2.40 GHz.
                   The governor "performance" may decide which speed to use
+
                   The governor "ondemand" may decide which speed to use
 
                   within this range.
 
                   within this range.
   current CPU frequency is 2.00 GHz.
+
   current CPU frequency is 1.20 GHz.
analyzing CPU 1:
+
  boost state support:
  driver: acpi-cpufreq
+
    Supported: yes
  CPUs which need to switch frequency at the same time: 0 1
+
    Active: yes
  hardware limits: 1000 MHz - 2.00 GHz
+
    25500 MHz max turbo 4 active cores
  available frequency steps: 2.00 GHz, 1.67 GHz, 1.33 GHz, 1000 MHz
+
    25500 MHz max turbo 3 active cores
  available cpufreq governors: ondemand, performance
+
    25500 MHz max turbo 2 active cores
  current policy: frequency should be within 1000 MHz and 2.00 GHz.
+
    25500 MHz max turbo 1 active cores
                  The governor "performance" may decide which speed to use
+
</nowiki>}}
                  within this range.
+
  current CPU frequency is 2.00 GHz.
+
  
=== 调速器 (CPU 电源方案) ===
+
如果要在系统启动时自动加载驱动,请在{{ic|/etc/modules-load.d/}}目录中添加一个文件指明需要加载的驱动。例如:
调速器可以被看作是预先设置好的 CPU 电源方案,它们必须被作为内核模块加载才能够被 kpowersave 和 gnome-power-manager 之类的程序识别。您可以加载多个调速器,但是在同一时间您只能激活一个。
+
  
可用的调速器:
+
{{hc|/etc/modules-load.d/cpufreq.conf|<nowiki>
*{{ic|performance}} '''(默认)''' -- Performance 调速器使 CPU 运行在最高频率,它已经被编译进内核。
+
# 加载cpufreq驱动
*{{ic|cpufreq_ondemand}} '''(推荐使用)''' -- 根据系统负载动态调节 CPU 频率。
+
acpi_cpufreq</nowiki>}}
*{{ic|cpufreq_conservative}} -- 与 ondemand 相似但更为保守 (clock speed changes are more graceful)
+
*{{ic|cpufreq_powersave}} (节能模式) -- 使 CPU 运行在最低频率。
+
*{{ic|cpufreq_userspace}} (手动模式)-- 由用户手动选择 CPU 频率。
+
  
对于桌面应用和大部分系统来说, ondemand 方案可以在散热能力、电源消耗、机器性能和易用性方面提供较好的平衡。对于笔记本和手机系统,conservative 方案可以显著地降低电量消耗。
+
=== 调速器 ===
  
用{{ic|modprobe}}加载你想要的方案(调速器)。比如:
+
调速器可以想象成预设的CPU电源方案。一些调速器必须作为内核模块加载以便能够被用户空间程序识别。可以加载你所期望的尽可能多的调速器(但在同一时刻在一个CPU上只有一个会被激活)。
# modprobe cpufreq_ondemand
+
# modprobe cpufreq_userspace
+
  
或者,将需要的调速器加入 {{ic|/etc/rc.conf}} 的 MODULES 列然后重新启动。
+
可用的调速器:
MODULES=(acpi-cpufreq '''cpufreq_ondemand''' '''cpufreq_powersave''')
+
  
要查看已经加载的方案(调速器),运行:
+
;{{ic|cpufreq_ondemand}} ''(默认,推荐)'': 根据系统负荷,在支持的CPU频率之间自动调整。
# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
+
;{{ic|cpufreq_performance}}: 高性能调速器,使CPU始终运行在最大频率。
 +
;{{ic|cpufreq_conservative}}: 类似于{{ic|ondemand}},但CPU频率根据系统负荷在可用频率中逐渐变化。
 +
;{{ic|cpufreq_powersave}}: 使CPU始终运行在最低频率。
 +
;{{ic|cpufreq_userspace}}: 用户手动调整CPU频率。
  
要查看可以设置哪些频率,运行:
+
For Desktops and most systems, the ondemand governor can provide the best compromise between heat emission, power consumption, performance, and manageability. Since it is the default and built into the kernel, loading the CPU frequency driver should be sufficient to activate it.
  # cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
+
For Laptops or other mobile systems, the conservative governor can possibly provide significant savings in power consumption.
 +
 
 +
The governors {{ic|ondemand}} and {{ic|performance}} are built into the kernel and do not need to be loaded as modules. If you want to use one of the other governors, you have to load them with {{ic|modprobe}}. For example:
 +
  # modprobe cpufreq_powersave
 +
# modprobe cpufreq_userspace
  
作为选择,您可以以 root 执行 {{ic|cpufreq-set}} 命令来手动设置调速器,但这在关机或重启后不会保存。如:
+
Or, add a file specifying the desired governor(s) to {{ic|/etc/modules-load.d/}} and reboot.
  
# cpufreq-set -c 0 -g ondemand
+
{{hc|/etc/modules-load.d/cpufreq_gov.conf|<nowiki>
 +
# Load cpufreq governors
 +
cpufreq_powersave
 +
cpufreq_userspace</nowiki>}}
  
注意上面这条命令仅设置了“第一个”处理器的CPU方案!  多核或多处理器系统需要{{ic|-c}}选项来指定某个核心的CPU方案(调速器),指定多核处理器需要重复运行该命令。CPU的编号从0开始而不是1!
+
{{Note|Remember that if you are using a kernel older than 3.4 (e.g. {{pkg|linux-lts}}) the needed [[#CPU frequency driver]] should be added to the top of the list to ensure it is loaded at boot.}}
  
对于四核芯片:
+
To see which governors have been loaded and which frequencies are supported:
  # for i in 0 1 2 3; do cpufreq-set -c $i -g ondemand; done
+
  # cpupower frequency-info
  
对于双核芯片:
+
Manually set the governor by running the {{ic|cpupower}} command (as root). For example:
# for i in 0 1; do cpufreq-set -c $i -g ondemand; done
+
  # cpupower frequency-set -g performance
+
或使用{{ic|-r}}选项给所有核心指定CPU方案:
+
  # cpufreq-set -r -g ondemand
+
  
{{Note|这些设置在计算机关闭或重启后不会保留. 查看下面的[[#Daemon]]小节在开机时设置cpufreq调速器.}}
+
{{Note|These settings will not be preserved after a reboot/shutdown. See the [[#Daemon]] section below for configuring cpufreq governors at boot.}}
  
其他选项比如设置CPU调速器工作的最高(低)频率限制可以通过{{ic|-u}}{{ic|-d}}选项来设定。处理器可用的CPU频率可以通过运行cpufreq-info来查看。比如要设置第二个核心的最高频率为2.00GHz最低频率为1.00 GHz,运行:
+
Additional options such as upper and lower frequency bounds used by the governor can optionally be set by using the {{ic|-u}} and {{ic|-d}} options. For example, to set the second core's upper frequency bound as 2.00GHz and its lower bound as 1.00 GHz:
  # cpufreq-set -c 1 -g ondemand -u 2.00Ghz -d 1.00Ghz
+
  # cpupower -c 1 frequency-set -g ondemand -u 2.00Ghz -d 1.00Ghz
  
要手动设置处理器使用由用户空间运行的调速器指定的频率。比如把核心0设为2.50Ghz核心1设为800Mhz:
+
To manually set a processor to a specific frequency the userspace governor is used. For example, to set core 0 to 2.50Ghz and core 1 to 800Mhz:
  # cpufreq-set -c 0 -f 2.50Ghz
+
  # cpupower -c 0 frequency-set -f 2.50Ghz
  # cpufreq-set -c 1 -f 800Mhz
+
  # cpupower -c 1 frequency-set -f 800Mhz
  
运行 '''{{ic|cpufreq-set --help}}''' 或者 '''{{ic|man cpufreq-set}}''' 来获得更多信息。
+
Run {{ic|cpupower --help}} or {{ic|man cpupower}} for more information.
  
如果想使用一个GUI程序来设置调速器或频率,可以用[[trayfreq]], 一个基于GTK+的应用程序,在系统托盘里可以找到。
+
For those who would like a GUI for setting governors or frequency there is [[trayfreq]], a GTK+ application that sits in the system tray.
  
CPU时钟的实时显示是由以下代码完成的:
+
The monitoring of CPU clock in real-time is accomplished by running:
 
  $ watch grep \"cpu MHz\" /proc/cpuinfo
 
  $ 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
 
or
 
# watch -n.5 cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
 
  
 
====Improving on-demand performance====
 
====Improving on-demand performance====
Line 198: Line 189:
 
Set the value:
 
Set the value:
 
  # echo -n 10 > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
 
  # echo -n 10 > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
 +
 +
{{Note|When using systemd with cpupower, the above commands can be added to {{ic|/usr/lib/systemd/scripts/cpupower}}, at the end of the script, right before {{ic|exit $fail}}.}}
  
 
====Tuning conservative governor====
 
====Tuning conservative governor====
Line 255: Line 248:
 
</nowiki>}}
 
</nowiki>}}
  
=== 守护进程 ===
+
=== Daemon ===
{{pkg|cpufrequtils}} 也安装了一个守护进程,可以在启动系统的时候就设置调速器和 CPU 的最高、最低频率,而不需要额外的如 kpowersave 之类的程序。这对于运行轻量级桌面,如 Openbox 的人来说是一个完善的解决方案。
+
{{Pkg|cpupower}} includes a daemon which allows users to set the desired scaling governor and min/max clock speeds for all processor cores at boot-time.
  
在启动守护进程之前,以 root 编辑 {{ic|/etc/conf.d/cpufreq}},选择需要的调速器并且设置 CPU 的最高、最低频率,例如:
+
Before starting the daemon, edit {{ic|/etc/conf.d/cpupower}} as root, selecting the desired governor and setting the min/max speed for your CPU(s).
  
{{hc
+
{{Note|The exact min/max values of the CPU(s) can be determined by running {{ic|cpupower frequency-info}}. However, these values are ''optional''. Users may omit them entirely by deleting or commenting out the min/max_freq lines; scaling will work automatically.}}
|/etc/conf.d/cpufreq
+
|<nowiki>
+
#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"
+
</nowiki>}}
+
 
+
{{注意|精确的 min/max 数值可以由运行 '''cpufreq-info''' 查看 (在加载 CPU 驱动之后,如 ''modprobe acpi-cpufreq'')。不过,这是''可选''的,您可以将它们完全忽略,例如删除或者注释这两行,这样它将会自动工作。}}
+
  
设置好配置文件后,可以运行以下命令来启动守护进程:
+
With the appropriate configuration, start the daemon with the following command:
  # rc.d start cpufreq
+
  # systemctl start cpupower
  
若要在启动系统时自动加载守护进程,将 {{ic|cpufreq}} 加到 {{ic|/etc/rc.conf}} 的 DAEMONS 列,如:
+
To start the daemon automatically at startup:
  DAEMONS=(syslog-ng networkmanager @alsa @crond @cupsd '''@cpufreq''')
+
  # systemctl enable cpupower
  
 
=== Privilege Granting Under GNOME ===
 
=== Privilege Granting Under GNOME ===
Line 299: Line 277:
  
 
===Laptop Mode Tools===
 
===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 {{ic|MODULES}} array in {{ic|/etc/rc.conf}} (see [[#CPU frequency driver]] above) and then go through the {{ic|/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.
+
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 {{ic|/etc/modules.d/}} directory. (see [[#CPU frequency driver]] above) and then go through the {{ic|/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.
  
 
== Troubleshooting ==
 
== Troubleshooting ==
Line 308: Line 286:
  
 
* 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.
 
* 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 {{ic|/sys/module/processor/parameters/ignore_ppc}} from 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.
 
* 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.
 +
 +
=== BIOS frequency limitation ===
 +
Some CPU/BIOS configurations may have difficulties to scale to the maximum frequency or scale to higher frequencies at all. This is most likely caused by BIOS events telling the OS to limit the frequency resulting in {{ic|/sys/devices/system/cpu/cpu0/cpufreq/bios_limit}} set to a lower value.
 +
 +
Either you just made a specific Setting in the BIOS Setup Utility, (Frequency, Thermal Management, etc.) you can blame a buggy/outdated BIOS or the BIOS might have a serious reason for throttling the CPU on it's own.
 +
 +
Reasons like that can be (assuming your machine's a notebook) that the battery is removed (or near death) so you're on AC-power only. In this case a weak AC-source might not supply enough electricity to fulfill extreme peak demands by the overall system and as there is no battery to assist this could lead to data loss, data corruption or in worst case even hardware damage!
 +
 +
Not all BIOS'es limit the CPU-Frequency in this case, but for example most IBM/Lenove Thinkpads do. Refer to thinkwiki for more [http://www.thinkwiki.org/wiki/Problem_with_CPU_frequency_scaling thinkpad related info on this topic].
 +
 +
If you checked there's not just an odd BIOS setting and you know what you're doing you can make the Kernel ignore these BIOS-limitations.
 +
 +
{{Warning|Make sure you read and understood the section above. CPU frequency limitation is a safety feature of your BIOS and you should not need to work around it.}}
 +
 +
A special parameter has to be passed to the processor module.
 +
 +
For trying this temporarily change the value in {{ic|/sys/module/processor/parameters/ignore_ppc}} from {{ic|0}} to {{ic|1}}.
 +
 +
For setting it permanent refer to [[Kernel_modules#Configuration|Kernel modules]] or just read on.
 +
Add {{ic|1=processor.ignore_ppc=1}} to your kernel boot line or create
 +
{{hc|/etc/modprobe.d/ignore_ppc.conf|2=# If the frequency of your machine gets wrongly limited by BIOS, this should help
 +
options processor ignore_ppc=1}}

Revision as of 04:46, 11 December 2012

Tango-preferences-desktop-locale.png本页面需要更新翻译,内容可能已经与英文脱节。要贡献翻译,请访问简体中文翻译组Tango-preferences-desktop-locale.png

附注: please use the first argument of the template to provide more detailed indications.

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end

cpufreq引用了内核架构中的CPU频率调整部分相关功能。这项技术使得操作系统能够提高或降低CPU速度来达到省电目的。CPU频率可以根据系统负荷自动调整,或响应ACPI事件而调整,或通过用户空间程序手工调整。

从内核3.4开始,必要的模块都会自动加载,而且推荐的调速器ondemand governor默认启动。但是,用户空间程序,例如cpupoweracpidlaptop-mode-tools,或你的桌面系统对应的图形化工具,在进行高级特性配置时可能仍然需要被用到。

cpupower

cpupower是一组设计为辅助CPU frequency scaling的一组用户空间工具。为使用频率调整的功能,这个软件包并不是必须的,但强烈建议安装,因为它提供了方便的命令行工具和在启动时改变调速器的服务(参考下文#守护进程)。

Tip: 在你启动cpupower守护进程之前,请确保已经正确编辑过/etc/conf.d/cpupower,并且设置了期望的调速器/频率。
注意: 使用3.5或更新的内核后cpupower的附加功能不再工作,因为对它的支持已经从内核中移除。不要设置配置文件中的smp_scheduler或mc_scheduler值,否则守护进程将会启动失败。

配置

配置CPU频率调整有3个步骤:

  1. 加载适合的CPU频率驱动
  2. 加载期望的频率控制器
  3. 选择一个管理切换和调整控制器的方法:

CPU频率驱动

注意: 从内核3.4以后CPU频率驱动会根据你的CPU自动加载,因此此节中描述的手工加载部分理论上不再必要。

为使CPU频率调整正常工作,操作系统必须先知道CPU的频率限制。为达到这个目的,一个能够读取和管理CPU规格的内核模块将被加载。注意这些模块可能需要BIOS中的相关功能被打开。这些功能在BIOS中可能被描述为:SpeedstepCool and QuietPowerNow!ACPI

如果你使用64位处理器,你很可能会需要acpi_cpufreq(Intel处理器)或powernow-k8(AMD K8/K10处理器,如Athlon 64, Opteron, and Phenom等)。这些模块同时支持32位和64位内核,所以即使你在64位硬件下运行32位内核,它们很可能仍然是你所需要的模块。

查看支持驱动的完整列表,请运行以下命令:

$ ls /lib/modules/$(uname -r)/kernel/drivers/cpufreq/

以下列表是部分可用的频率驱动(来自内核源代码<kernel source>/arch/x86/kernel/cpu/cpufreq/Kconfig)。

模块 描述
acpi_cpufreq 支持ACPI Processor Performance States的CPUFreq驱动。该驱动也支持Intel Enhanced SpeedStep(之前由已过期的模块speedstep-centrino支持)。
speedstep-ich 支持部分移动Intel奔腾III (Coppermine)的CPUFreq驱动。该驱动支持所有具备Intel ICH2, ICH3 或 ICH4 南桥芯片的系统中的移动Intel奔腾III-M (Tualatin)和移动Intel奔腾4 P4-M。
speedstep-smi 支持部分移动Intel奔腾III (Coppermine)的CPUFreq驱动。该驱动支持所有具备Intel 440BX/ZX/MX南桥芯片系统中的移动Intel奔腾III-M (Tualatin) 。
powernow-k8 支持K8/K10 Athlon64/Opteron/Phenom处理器的CPUFreq驱动。
powernow-k7 支持移动AMD K7处理器的CPUFreq驱动。
cpufreq-nforce2 支持nForce2平台下外频调整的CPUFreq驱动(AMD K7, Socket A)
pcc-cpufreq 支持HP和微软提出的“处理器时钟频率控制”接口,这在一些Proliant服务器上很有用。
p4_clockmod 支持Intel奔腾4 / 至强 / 赛扬 处理器的CPUFreq驱动。生效时会通过skipping clocks技术降低CPU发热量。
你可能更希望使用Speedstep驱动代替它。


手工加载CPU频率驱动:

# modprobe acpi_cpufreq

注意如果你尝试加载错误的模块,你会得到类似这样的错误消息:

FATAL: Error inserting acpi_cpufreq ([...]/acpi-cpufreq.ko): No such device

一旦合适的cpufreq驱动加载完成,CPU的详细信息就可以通过以下命令得到:

$ cpupower frequency-info
analyzing CPU 0:
  driver: acpi-cpufreq
  CPUs which run at the same hardware frequency: 0 1 2 3
  CPUs which need to have their frequency coordinated by software: 0
  maximum transition latency: 10.0 us.
  hardware limits: 1.20 GHz - 2.40 GHz
  available frequency steps: 2.40 GHz, 2.40 GHz, 2.27 GHz, 2.13 GHz, 2.00 GHz, 1.87 GHz, 1.73 GHz, 1.60 GHz, 1.47 GHz, 1.33 GHz, 1.20 GHz
  available cpufreq governors: ondemand, performance
  current policy: frequency should be within 1.20 GHz and 2.40 GHz.
                  The governor "ondemand" may decide which speed to use
                  within this range.
  current CPU frequency is 1.20 GHz.
  boost state support:
    Supported: yes
    Active: yes
    25500 MHz max turbo 4 active cores
    25500 MHz max turbo 3 active cores
    25500 MHz max turbo 2 active cores
    25500 MHz max turbo 1 active cores

如果要在系统启动时自动加载驱动,请在/etc/modules-load.d/目录中添加一个文件指明需要加载的驱动。例如:

/etc/modules-load.d/cpufreq.conf
# 加载cpufreq驱动
acpi_cpufreq

调速器

调速器可以想象成预设的CPU电源方案。一些调速器必须作为内核模块加载以便能够被用户空间程序识别。可以加载你所期望的尽可能多的调速器(但在同一时刻在一个CPU上只有一个会被激活)。

可用的调速器:

cpufreq_ondemand (默认,推荐)
根据系统负荷,在支持的CPU频率之间自动调整。
cpufreq_performance
高性能调速器,使CPU始终运行在最大频率。
cpufreq_conservative
类似于ondemand,但CPU频率根据系统负荷在可用频率中逐渐变化。
cpufreq_powersave
使CPU始终运行在最低频率。
cpufreq_userspace
用户手动调整CPU频率。

For Desktops and most systems, the ondemand governor can provide the best compromise between heat emission, power consumption, performance, and manageability. Since it is the default and built into the kernel, loading the CPU frequency driver should be sufficient to activate it. For Laptops or other mobile systems, the conservative governor can possibly provide significant savings in power consumption.

The governors ondemand and performance are built into the kernel and do not need to be loaded as modules. If you want to use one of the other governors, you have to load them with modprobe. For example:

# modprobe cpufreq_powersave
# modprobe cpufreq_userspace

Or, add a file specifying the desired governor(s) to /etc/modules-load.d/ and reboot.

/etc/modules-load.d/cpufreq_gov.conf
# Load cpufreq governors
cpufreq_powersave
cpufreq_userspace
Note: Remember that if you are using a kernel older than 3.4 (e.g. linux-lts) the needed #CPU frequency driver should be added to the top of the list to ensure it is loaded at boot.

To see which governors have been loaded and which frequencies are supported:

# cpupower frequency-info

Manually set the governor by running the cpupower command (as root). For example:

# cpupower frequency-set -g performance
Note: These settings will not be preserved after a reboot/shutdown. See the #Daemon section below for configuring cpufreq governors at boot.

Additional options such as upper and lower frequency bounds used by the governor can optionally be set by using the -u and -d options. For example, to set the second core's upper frequency bound as 2.00GHz and its lower bound as 1.00 GHz:

# cpupower -c 1 frequency-set -g ondemand -u 2.00Ghz -d 1.00Ghz

To manually set a processor to a specific frequency the userspace governor is used. For example, to set core 0 to 2.50Ghz and core 1 to 800Mhz:

# cpupower -c 0 frequency-set -f 2.50Ghz
# cpupower -c 1 frequency-set -f 800Mhz

Run cpupower --help or man cpupower for more information.

For those who would like a GUI for setting governors or frequency there is trayfreq, a GTK+ application that sits in the system tray.

The monitoring of CPU clock in real-time is accomplished by running:

$ watch grep \"cpu MHz\" /proc/cpuinfo

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:

/etc/tmpfiles.d/ondemand_threshold.conf
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.

Having 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
Note: When using systemd with cpupower, the above commands can be added to /usr/lib/systemd/scripts/cpupower, at the end of the script, right before exit $fail.

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
Note: The minimum value one can enter must be above the one in down_threshold; entering a value under that results in the error, "bash: echo: write error: Invalid argument"

Similarly the 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 ondemand governor.

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 down_threshold or 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 acpid package is installed, the file should already exist and be executable. For example, to change the scaling governor from performance to conservative when the AC adapter is disconnected and change it back if reconnected:

/etc/acpi/handler.sh
[...]

 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
 ;;

[...]

Daemon

cpupower includes a daemon which allows users to set the desired scaling governor and min/max clock speeds for all processor cores at boot-time.

Before starting the daemon, edit /etc/conf.d/cpupower as root, selecting the desired governor and setting the min/max speed for your CPU(s).

Note: The exact min/max values of the CPU(s) can be determined by running cpupower frequency-info. However, these values are optional. Users may omit them entirely by deleting or commenting out the min/max_freq lines; scaling will work automatically.

With the appropriate configuration, start the daemon with the following command:

# systemctl start cpupower

To start the daemon automatically at startup:

# systemctl enable cpupower

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=yes
0

Where the word USER is replaced with the username of interest.

The desktop-privilegesAUR package in the AUR contains a similar .pkla file for authorizing all users of the power group to change the governor.

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 /etc/modules.d/ directory. (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.

Troubleshooting

Tango-inaccurate.pngThe factual accuracy of this article or section is disputed.Tango-inaccurate.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in Talk:CPU frequency scaling (简体中文)#)
  • 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-demand governor 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-demand governor (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 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.

BIOS frequency limitation

Some CPU/BIOS configurations may have difficulties to scale to the maximum frequency or scale to higher frequencies at all. This is most likely caused by BIOS events telling the OS to limit the frequency resulting in /sys/devices/system/cpu/cpu0/cpufreq/bios_limit set to a lower value.

Either you just made a specific Setting in the BIOS Setup Utility, (Frequency, Thermal Management, etc.) you can blame a buggy/outdated BIOS or the BIOS might have a serious reason for throttling the CPU on it's own.

Reasons like that can be (assuming your machine's a notebook) that the battery is removed (or near death) so you're on AC-power only. In this case a weak AC-source might not supply enough electricity to fulfill extreme peak demands by the overall system and as there is no battery to assist this could lead to data loss, data corruption or in worst case even hardware damage!

Not all BIOS'es limit the CPU-Frequency in this case, but for example most IBM/Lenove Thinkpads do. Refer to thinkwiki for more thinkpad related info on this topic.

If you checked there's not just an odd BIOS setting and you know what you're doing you can make the Kernel ignore these BIOS-limitations.

Warning: Make sure you read and understood the section above. CPU frequency limitation is a safety feature of your BIOS and you should not need to work around it.

A special parameter has to be passed to the processor module.

For trying this temporarily change the value in /sys/module/processor/parameters/ignore_ppc from 0 to 1.

For setting it permanent refer to Kernel modules or just read on. Add processor.ignore_ppc=1 to your kernel boot line or create

/etc/modprobe.d/ignore_ppc.conf
# If the frequency of your machine gets wrongly limited by BIOS, this should help
options processor ignore_ppc=1