为了一个研究项目,我需要为我的 CPU 设置一个固定频率。
我正在使用Ubuntu 20.04.5 LTS (GNU/Linux 5.4.0-155-generic x86_64)
英特尔 CPU Xeon W-2155 3.3GHz
。
我正在尝试使用该cpufrequtils
软件包来实现这一点。州长是powersave
。
$ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
据我所知,我可以使用-d
(min. freq.) 和-u
(max. freq.)cpufreq-set
参数设置频率。我运行:
sudo cpufreq-set -c 0 -d 1.8GHz -u 1.8GHz
将CPU 0频率设置为1.8GHz。
然后如果我跑cpufreq-info
,我会看到已设定限制,但未生效。
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: 4294.55 ms.
hardware limits: 1.20 GHz - 4.50 GHz
available cpufreq governors: performance, powersave
current policy: frequency should be within 1.80 GHz and 1.80 GHz.
The governor "powersave" may decide which speed to use
within this range.
current CPU frequency is 1.31 GHz.
为什么当前 CPU 频率超出了调节器限制?我在另一台服务器/CPU(i7-7700K 4.2GHz)上使用了类似的方法,并修复了所需的频率。
(类似问题这里,但并未得到解决。)
答案1
对于此问题的英特尔处理器的各代产品,所有 CPU 都只有一个 CPU 时钟发生器,由锁相环 (PLL) 驱动。PLL 需要一定的时间来提高或降低到它决定生成的 CPU 频率。处于深度空闲状态的 CPU 会放弃对所需 CPU 频率的投票。请求更高 CPU 频率的其他 CPU 可以覆盖另一个 CPU 的最大频率。
要始终生成 1.8GHz CPU 频率,需要将所有 CPU 的最小和最大频率设置为该值,并且需要为至少一个 CPU 禁用深度空闲状态(以功耗为代价),以便始终至少有一个 CPU 向 PLL 提交 pstate 18 的投票请求(每个 pstate 100MHz)。使用 CPU 5 作为我的测试 CPU 的示例:
doug@s19:~$ echo 1800000 | sudo tee /sys/devices/system/cpu/cpufreq/policy*/scaling_max_freq
1800000
doug@s19:~$ grep . /sys/devices/system/cpu/cpufreq/policy*/scaling_max_freq
/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq:1800000
/sys/devices/system/cpu/cpufreq/policy10/scaling_max_freq:1800000
/sys/devices/system/cpu/cpufreq/policy11/scaling_max_freq:1800000
/sys/devices/system/cpu/cpufreq/policy1/scaling_max_freq:1800000
/sys/devices/system/cpu/cpufreq/policy2/scaling_max_freq:1800000
/sys/devices/system/cpu/cpufreq/policy3/scaling_max_freq:1800000
/sys/devices/system/cpu/cpufreq/policy4/scaling_max_freq:1800000
/sys/devices/system/cpu/cpufreq/policy5/scaling_max_freq:1800000
/sys/devices/system/cpu/cpufreq/policy6/scaling_max_freq:1800000
/sys/devices/system/cpu/cpufreq/policy7/scaling_max_freq:1800000
/sys/devices/system/cpu/cpufreq/policy8/scaling_max_freq:1800000
/sys/devices/system/cpu/cpufreq/policy9/scaling_max_freq:1800000
doug@s19:~$ echo 1800000 | sudo tee /sys/devices/system/cpu/cpufreq/policy*/scaling_min_freq
1800000
doug@s19:~$ grep . /sys/devices/system/cpu/cpufreq/policy*/scaling_min_freq
/sys/devices/system/cpu/cpufreq/policy0/scaling_min_freq:1800000
/sys/devices/system/cpu/cpufreq/policy10/scaling_min_freq:1800000
/sys/devices/system/cpu/cpufreq/policy11/scaling_min_freq:1800000
/sys/devices/system/cpu/cpufreq/policy1/scaling_min_freq:1800000
/sys/devices/system/cpu/cpufreq/policy2/scaling_min_freq:1800000
/sys/devices/system/cpu/cpufreq/policy3/scaling_min_freq:1800000
/sys/devices/system/cpu/cpufreq/policy4/scaling_min_freq:1800000
/sys/devices/system/cpu/cpufreq/policy5/scaling_min_freq:1800000
/sys/devices/system/cpu/cpufreq/policy6/scaling_min_freq:1800000
/sys/devices/system/cpu/cpufreq/policy7/scaling_min_freq:1800000
/sys/devices/system/cpu/cpufreq/policy8/scaling_min_freq:1800000
/sys/devices/system/cpu/cpufreq/policy9/scaling_min_freq:1800000
doug@s19:~/idle$ grep . /sys/devices/system/cpu/cpu5/cpuidle/state*/name
/sys/devices/system/cpu/cpu5/cpuidle/state0/name:POLL
/sys/devices/system/cpu/cpu5/cpuidle/state1/name:C1
/sys/devices/system/cpu/cpu5/cpuidle/state2/name:C2
/sys/devices/system/cpu/cpu5/cpuidle/state3/name:C3
doug@s19:~/idle$ grep . /sys/devices/system/cpu/cpu5/cpuidle/state*/disable
/sys/devices/system/cpu/cpu5/cpuidle/state0/disable:0
/sys/devices/system/cpu/cpu5/cpuidle/state1/disable:0
/sys/devices/system/cpu/cpu5/cpuidle/state2/disable:0
/sys/devices/system/cpu/cpu5/cpuidle/state3/disable:0
doug@s19:~/idle$ echo 1 | sudo tee /sys/devices/system/cpu/cpu5/cpuidle/state1/disable
1
doug@s19:~/idle$ echo 1 | sudo tee /sys/devices/system/cpu/cpu5/cpuidle/state2/disable
1
doug@s19:~/idle$ echo 1 | sudo tee /sys/devices/system/cpu/cpu5/cpuidle/state3/disable
1
doug@s19:~/idle$ grep . /sys/devices/system/cpu/cpu5/cpuidle/state*/disable
/sys/devices/system/cpu/cpu5/cpuidle/state0/disable:0
/sys/devices/system/cpu/cpu5/cpuidle/state1/disable:1
/sys/devices/system/cpu/cpu5/cpuidle/state2/disable:1
/sys/devices/system/cpu/cpu5/cpuidle/state3/disable:1
注意:您的空闲状态数可能不同。只需禁用除状态 0 之外的所有状态即可。现在观察 CPU 频率:
doug@s19:~/idle$ grep . /sys/devices/system/cpu/cpufreq/policy*/scaling_cur_freq
/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq:1801307
/sys/devices/system/cpu/cpufreq/policy10/scaling_cur_freq:1800484
/sys/devices/system/cpu/cpufreq/policy11/scaling_cur_freq:1801825
/sys/devices/system/cpu/cpufreq/policy1/scaling_cur_freq:1800432
/sys/devices/system/cpu/cpufreq/policy2/scaling_cur_freq:1801306
/sys/devices/system/cpu/cpufreq/policy3/scaling_cur_freq:1803848
/sys/devices/system/cpu/cpufreq/policy4/scaling_cur_freq:1800934
/sys/devices/system/cpu/cpufreq/policy5/scaling_cur_freq:1800000
/sys/devices/system/cpu/cpufreq/policy6/scaling_cur_freq:1801484
/sys/devices/system/cpu/cpufreq/policy7/scaling_cur_freq:1799961
/sys/devices/system/cpu/cpufreq/policy8/scaling_cur_freq:1800669
/sys/devices/system/cpu/cpufreq/policy9/scaling_cur_freq:1799992
使用 turbostat(我认为是 linux_tools_common 包)来监控:
$ sudo turbostat --Summary --quiet --show Busy%,Bzy_MHz,IRQ,PkgWatt,PkgTmp,RAMWatt,GFXWatt,CorWatt --interval 10
Busy% Bzy_MHz IRQ PkgTmp PkgWatt CorWatt GFXWatt RAMWatt
8.32 1800 2910 38 3.27 2.62 0.00 1.33
8.32 1800 2757 38 3.19 2.53 0.00 1.33
8.32 1800 2750 39 2.99 2.33 0.00 1.33
注意:此答案是使用内核 5.4.0-156-generic 编写的,与问题使用的内核类似。使用较新内核的读者将获得不同的实际频率结果,因为情况已发生变化,空闲 CPU 不再被唤醒以读取频率。在这种情况下将返回默认值。使用内核 6.5 的示例:
doug@s19:~$ grep . /sys/devices/system/cpu/cpufreq/policy*/scaling_cur_freq
/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpufreq/policy10/scaling_cur_freq:1800006
/sys/devices/system/cpu/cpufreq/policy11/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpufreq/policy1/scaling_cur_freq:1799751
/sys/devices/system/cpu/cpufreq/policy2/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpufreq/policy3/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpufreq/policy4/scaling_cur_freq:1799571
/sys/devices/system/cpu/cpufreq/policy5/scaling_cur_freq:1799995
/sys/devices/system/cpu/cpufreq/policy6/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpufreq/policy7/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpufreq/policy8/scaling_cur_freq:1800304
/sys/devices/system/cpu/cpufreq/policy9/scaling_cur_freq:1800337
其中 4.1 GHz 是空闲 CPU 的默认值。请注意,该值高于我们设置的最大值 1.8GHz。将来会变为当前最小值。