CPU频率调节器
cpupower
在 Debian 平台通过以下命令安装
cpupower
工具包:
cpufreq governor
检查 cpufreq governor :
cpupower frequency-info
备注
Ubuntu系统需要安装 linux-tools-common
软件包,然后执行 cpupower
命令会提示你按照内核版本安装对应工具包,例如我当前运行内核 5.4.0-121-generic
,提示安装 linux-tools-5.4.0-121-generic
在我的 HPE ProLiant DL360 Gen9服务器 服务器上,输出案例:
analyzing CPU 0:
driver: intel_pstate
CPUs which run at the same hardware frequency: 0
CPUs which need to have their frequency coordinated by software: 0
maximum transition latency: Cannot determine or is not supported.
hardware limits: 1.20 GHz - 3.10 GHz
available cpufreq governors: performance powersave
current policy: frequency should be within 1.20 GHz and 3.10 GHz.
The governor "powersave" may decide which speed to use
within this range.
current CPU frequency: Unable to call hardware
current CPU frequency: 1.20 GHz (asserted by call to kernel)
boost state support:
Supported: yes
Active: yes
Linux内核支持的cpufreq调节器
cpufreq调节器,说明
Performance,在scaling_min_freq和scaling_max_freq范围内固定使用最高的主频
Powersave,在scaling_min_freq和scaling_max_freq范围内尽量使用最低频率
Userspace,允许用户或任何使用root账号的用户空间程序通过scaling_setspeed文件设置CPU主频
Ondemand,按当前使用情况设置CPU,这个调机器设置需要相应配套参数(比较复杂)
Conservative,"保守"调节器很像Ondemand调节器,但是更为平滑增加或降低CPU主频而不是CPU有任何负载都跳到最大速度,更适合电池供电环境
要人工调整处理器主频,则需要采用 userspace
governor,否则设置时候会报错
当使用 userspace
governor时,可以通过如下命令设置某个处理器的主频(案例是cpu 0):
cpufreq-set -c 0 -f 3100000
注意, cpufreq驱动可能只支持部分cpufreq governor。例如 Intel CPU p-state 就只支持 performance
和 powersave
调节器。所以,此时只能切换这两者之一
修改cpufreq governor
直接通过
sys
文件系统可以单独对每个CPU核心调整不同的 cpufreq governor ,例如以下命令只调整 cpu 0 的 cpufreq governor:echo performance > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor
此时检查:
cpupower monitor -m Mperf
会看到(我的CPU是 Intel(R) Xeon(R) CPU E5-2670 v3 @ 2.30GHz
):
| Mperf
PKG|CORE| CPU| C0 | Cx | Freq
0| 0| 0| 3.11| 96.89| 2268
0| 0| 24| 11.91| 88.09| 2234
0| 1| 1| 11.67| 88.33| 1250
0| 1| 25| 6.88| 93.12| 1240
0| 2| 2| 0.95| 99.05| 1201
0| 2| 26| 6.48| 93.52| 1209
...
注意看,这里只有 cpu core 0 (启用超线程时,对应core 0的是CPU 0和24)显示主频提升到 2.2GHz
,而其他CPU核心的主频依然处于 powersave
的cpufreq governor下,所以主频只有 1.2GHz
现在我们使用 压力模拟工具
stress
来测试CPU:stress -c 48 -v -t 120s
这里将48个CPU都压满
然后检查 cpupower monitor -m Mperf
输出如下:
| Mperf
PKG|CORE| CPU| C0 | Cx | Freq
0| 0| 0| 99.83| 0.17| 2596
0| 0| 24| 99.86| 0.14| 2596
0| 1| 1| 99.83| 0.17| 2596
0| 1| 25| 99.86| 0.14| 2596
0| 2| 2| 99.83| 0.17| 2596
0| 2| 26| 99.86| 0.14| 2596
...
这里看到所有CPU都运行在 2.6GHz
频率,这说明不管是 performance
还是 powersave
的cpufreq governor,在压力下都会达到处理器的最高频率。区别在于 performance
策略下,默认空闲状态就是较高频率,所以在压力下会更快运行在最高频率。这样对于应用看来,就是响应更快(更快运行在全速状态)
但是,在这里你看到的 2.6GHz
却不是处理器物理限制的最高频率: 该处理器物理限制频率是 3.1GHz
。也就是说,我们还没有释放出处理器的最高性能。不过,这里的 2.6GHz
已经是开启了 turbo
模式,可以从:
cat /sys/devices/system/cpu/intel_pstate/no_turbo
看到默认值是 0
,也就是关闭 no_turbo
,相当于启用 turbo
。
如果 echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo
关闭 turbo
,则压力下处理器主频会降低,对于我的 Intel(R) Xeon(R) CPU E5-2670 v3 @ 2.30GHz
在没有启用Trubo时候,最高主频只有 2.3GHz
。
性能设置
启用全面的Performance策略:
performance
governorcpupower frequency-set -g performance
此时会看到所有cpu都滚动一遍设置:
Setting cpu: 0
Setting cpu: 1
Setting cpu: 2
Setting cpu: 3
...
然后检查 cpupower monitor -m Mperf
输出如下:
| Mperf
PKG|CORE| CPU| C0 | Cx | Freq
0| 0| 0| 2.25| 97.75| 2345
0| 0| 24| 2.83| 97.17| 2495
0| 1| 1| 0.37| 99.63| 2271
0| 1| 25| 2.47| 97.53| 2509
0| 2| 2| 0.29| 99.71| 2258
0| 2| 26| 5.23| 94.77| 2585
...
可以看到即使系统压力不大,所有运行程序的处理器核心频率都在 2.3GHz ~ 2.6GHz
之间,基本上就是最高主频了
节电设置
如果对性能没有强要求,但是需要节约电能,则可以设置 powersave
governor:
powersave
governorcpupower frequency-set -g powersave
冲击最高主频
你会看到不管是 Performance
和 Powersave
的cpufreq governor,都没有出现在运行压力的情况下出现 hardware limits
的极限主频 3.1 GHz
,而只达到 2.6 GHz
。这似乎没有达到预期...
永久性cpufreq governor设置
系统安装了 cpufrequtils
软件包之后,就可以通过脚本配置在启动时自动设置好对应的 cpufreq governer
:
/etc/init.d/cpufrequtils
脚本在启动时执行,这个脚本(简单看一下就明白)非常简单,其中关于默认cpufreq governer
有如下脚本命令:
/etc/init.d/cpufrequtils
脚本ENABLE="true"
GOVERNOR="ondemand"
MAX_SPEED="0"
MIN_SPEED="0"
...
if [ -f /etc/default/cpufrequtils ] ; then
. /etc/default/cpufrequtils
fi
...
可以看到参数变量可以在 /etc/default/cpufrequtils
中设置,所以执行以下命令将默认 cpufreq governer
调整为 powersave
:
powersave
的 cpufreq governer
echo 'GOVERNOR="powersave"' | sudo tee /etc/default/cpufrequtils
当然,当前运行状态也要设置(不用重启):
cpufreq governer
sudo cpupower frequency-set -g powersave
Debian governor
在 Debian 系(例如 Raspberry Pi OS
),通过配置 /etc/default/cpu_governor
就能够设置系统默认的 CPU governor