无法使用 cpufrequtils 限制 Ubuntu 22.04 上的 CPU 速度

无法使用 cpufrequtils 限制 Ubuntu 22.04 上的 CPU 速度

基于此:如何轻松控制 Ubuntu 上的 CPU 速度?我尝试使用(来自:cpufrequtils 008:cpufreq-info (C) Dominik Brodowski 2004-2009)手动强制将 Ubuntu 22.04.1 上的某些 CPU 核心5.15.0-56-generic x86_64 GNU/Linux限制为 800 MHz,cpufreq但似乎不起作用:

$ for i in 1 2 3 4 5 6 7; do sudo cpufreq-set -c $i -d 800 MHz; done

$ watch -n2 'cpufreq-info | grep "current CPU freq"'

Every 2.0s: cpufreq-info | grep "current CPU freq"                                                                                                          
  current CPU frequency is 3.45 GHz.
  current CPU frequency is 1.44 GHz.
  current CPU frequency is 1.85 GHz.
  current CPU frequency is 1.61 GHz.
  current CPU frequency is 2.58 GHz.
  current CPU frequency is 1.97 GHz.
  current CPU frequency is 1.26 GHz.
  current CPU frequency is 3.45 GHz.

为什么它不起作用以及如何使其起作用?

这是/proc/cpuinfo一个核心:

processor   : 7
vendor_id   : GenuineIntel
cpu family  : 6
model       : 140
model name  : 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
stepping    : 1
microcode   : 0xa4
cpu MHz     : 2570.099
cache size  : 12288 KB
physical id : 0
siblings    : 8
core id     : 3
cpu cores   : 4
apicid      : 7
initial apicid  : 7
fpu     : yes
fpu_exception   : yes
cpuid level : 27
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l2 invpcid_single cdp_l2 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves split_lock_detect dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid movdiri movdir64b fsrm avx512_vp2intersect md_clear flush_l1d arch_capabilities
vmx flags   : vnmi preemption_timer posted_intr invvpid ept_x_only ept_ad ept_1gb flexpriority apicv tsc_offset vtpr mtf vapic ept vpid unrestricted_guest vapic_reg vid ple pml ept_mode_based_exec tsc_scaling
bugs        : spectre_v1 spectre_v2 spec_store_bypass swapgs eibrs_pbrsb
bogomips    : 5606.40
clflush size    : 64
cache_alignment : 64
address sizes   : 39 bits physical, 48 bits virtual
power management:

和:

$ sudo lshw -class cpu
  *-cpu                     
       description: CPU
       product: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
       vendor: Intel Corp.
       physical id: 10
       bus info: cpu@0
       version: 6.140.1
       serial: None
       slot: U3E1
       size: 3542MHz
       capacity: 4700MHz
       width: 64 bits
       clock: 100MHz
       capabilities: lm fpu fpu_exception wp vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp x86-64 constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l2 invpcid_single cdp_l2 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves split_lock_detect dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid movdiri movdir64b fsrm avx512_vp2intersect md_clear flush_l1d arch_capabilities cpufreq
       configuration: cores=4 enabledcores=4 microcode=164 threads=8

电脑是联想Thinkpad P15s。

编辑

正如 Doug Smythies 在下面的评论中所建议的,这里还有一些命令输出:

$ grep . /sys/devices/system/cpu/cpu*/cpufreq/scaling_driver
/sys/devices/system/cpu/cpu0/cpufreq/scaling_driver:intel_pstate
/sys/devices/system/cpu/cpu1/cpufreq/scaling_driver:intel_pstate
/sys/devices/system/cpu/cpu2/cpufreq/scaling_driver:intel_pstate
/sys/devices/system/cpu/cpu3/cpufreq/scaling_driver:intel_pstate
/sys/devices/system/cpu/cpu4/cpufreq/scaling_driver:intel_pstate
/sys/devices/system/cpu/cpu5/cpufreq/scaling_driver:intel_pstate
/sys/devices/system/cpu/cpu6/cpufreq/scaling_driver:intel_pstate
/sys/devices/system/cpu/cpu7/cpufreq/scaling_driver:intel_pstate
$ grep . /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor:powersave
/sys/devices/system/cpu/cpu1/cpufreq/scaling_governor:powersave
/sys/devices/system/cpu/cpu2/cpufreq/scaling_governor:powersave
/sys/devices/system/cpu/cpu3/cpufreq/scaling_governor:powersave
/sys/devices/system/cpu/cpu4/cpufreq/scaling_governor:powersave
/sys/devices/system/cpu/cpu5/cpufreq/scaling_governor:powersave
/sys/devices/system/cpu/cpu6/cpufreq/scaling_governor:powersave
/sys/devices/system/cpu/cpu7/cpufreq/scaling_governor:powersave
$ grep . /sys/devices/system/cpu/cpu6/cpufreq/*
/sys/devices/system/cpu/cpu6/cpufreq/affected_cpus:6
/sys/devices/system/cpu/cpu6/cpufreq/base_frequency:1200000
/sys/devices/system/cpu/cpu6/cpufreq/cpuinfo_max_freq:4700000
/sys/devices/system/cpu/cpu6/cpufreq/cpuinfo_min_freq:400000
/sys/devices/system/cpu/cpu6/cpufreq/cpuinfo_transition_latency:0
/sys/devices/system/cpu/cpu6/cpufreq/energy_performance_available_preferences:default performance balance_performance balance_power power 
/sys/devices/system/cpu/cpu6/cpufreq/energy_performance_preference:balance_performance
/sys/devices/system/cpu/cpu6/cpufreq/related_cpus:6
/sys/devices/system/cpu/cpu6/cpufreq/scaling_available_governors:performance powersave
/sys/devices/system/cpu/cpu6/cpufreq/scaling_cur_freq:1006285
/sys/devices/system/cpu/cpu6/cpufreq/scaling_driver:intel_pstate
/sys/devices/system/cpu/cpu6/cpufreq/scaling_governor:powersave
/sys/devices/system/cpu/cpu6/cpufreq/scaling_max_freq:4700000
/sys/devices/system/cpu/cpu6/cpufreq/scaling_min_freq:400000
/sys/devices/system/cpu/cpu6/cpufreq/scaling_setspeed:<unsupported>
$ grep . /sys/devices/system/cpu/intel_pstate/*
/sys/devices/system/cpu/intel_pstate/hwp_dynamic_boost:0
/sys/devices/system/cpu/intel_pstate/max_perf_pct:100
/sys/devices/system/cpu/intel_pstate/min_perf_pct:8
/sys/devices/system/cpu/intel_pstate/no_turbo:0
/sys/devices/system/cpu/intel_pstate/num_pstates:44
/sys/devices/system/cpu/intel_pstate/status:active
/sys/devices/system/cpu/intel_pstate/turbo_pct:44

答案1

对于英特尔处理器,至少到第 10 代为止,问题在于只有一个 CPU 时钟频率 PLL(锁相环)。PLL 的输入(频率请求)来自所有 CPU 以及一些内部贡献者。在启用 HWP(硬件 Pstate 控制)的情况下,CPU 输入仅仅是指导方针,由处理器决定。如果 CPU 处于足够深的空闲状态,则 CPU 的投票将被忽略。PLL 的 CPU 频率输出呈现给所有 CPU。由于您没有将所有 CPU 设置为最高频率 800 MHz,因此看起来剩下的一个要求更高的频率。其他 CPU 列出了 3.45 GHz 以外的频率,因为它们有时空闲,有时不空闲,导致同一时间内的数字不同。

使用 i5-10600K、6 核、12 个 CPU 的示例:

首先,将除 0 之外的所有 CPU 设置为最大频率 800 MHz 并检查它:

doug@s19:~$ for i in `seq 1 11`; do echo 800000 | sudo tee /sys/devices/system/cpu/cpu$i/cpufreq/scaling_max_freq; done
800000
800000
800000
800000
800000
800000
800000
800000
800000
800000
800000
doug@s19:~$ grep . /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq
/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu10/cpufreq/scaling_max_freq:800000
/sys/devices/system/cpu/cpu11/cpufreq/scaling_max_freq:800000
/sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq:800000
/sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq:800000
/sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq:800000
/sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq:800000
/sys/devices/system/cpu/cpu5/cpufreq/scaling_max_freq:800000
/sys/devices/system/cpu/cpu6/cpufreq/scaling_max_freq:800000
/sys/devices/system/cpu/cpu7/cpufreq/scaling_max_freq:800000
/sys/devices/system/cpu/cpu8/cpufreq/scaling_max_freq:800000
/sys/devices/system/cpu/cpu9/cpufreq/scaling_max_freq:800000

现在,看看 CPU 频率:

doug@s19:~$ grep . /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq
/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq:799994
/sys/devices/system/cpu/cpu10/cpufreq/scaling_cur_freq:800458
/sys/devices/system/cpu/cpu11/cpufreq/scaling_cur_freq:799748
/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq:800173
/sys/devices/system/cpu/cpu4/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu5/cpufreq/scaling_cur_freq:800003
/sys/devices/system/cpu/cpu6/cpufreq/scaling_cur_freq:800063
/sys/devices/system/cpu/cpu7/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu8/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu9/cpufreq/scaling_cur_freq:4100000

系统处于空闲状态,因此没有任何东西推动 CPU 0 提高频率,因此时钟频率较低。4.1 GHz 的数字具有误导性,实际上表示由于 CPU 长时间处于深度空闲状态而导致的数据过时(此输出是内核版本的函数,其他用户可能会得到不同的结果)。

现在加载 CPU 0 并再次检查:

doug@s19:~$ taskset -c 0 yes > /dev/null &
[1] 3911
doug@s19:~$ grep . /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq
/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq:4799997
/sys/devices/system/cpu/cpu10/cpufreq/scaling_cur_freq:4800121
/sys/devices/system/cpu/cpu11/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu4/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu5/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu6/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu7/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu8/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu9/cpufreq/scaling_cur_freq:4100000
doug@s19:~$ fg
taskset -c 0 yes > /dev/null
^C
doug@s19:~$

现在,如果我将所有 CPU 的最大频率设置为 800 MHz,那么它就会起作用:

doug@s19:~$ echo 800000 | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
800000
doug@s19:~$ taskset -c 0 yes > /dev/null &
[1] 3920
doug@s19:~$ grep . /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq
/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq:799997
/sys/devices/system/cpu/cpu10/cpufreq/scaling_cur_freq:800322
/sys/devices/system/cpu/cpu11/cpufreq/scaling_cur_freq:800101
/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq:800105
/sys/devices/system/cpu/cpu4/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu5/cpufreq/scaling_cur_freq:800001
/sys/devices/system/cpu/cpu6/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu7/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu8/cpufreq/scaling_cur_freq:4100000
/sys/devices/system/cpu/cpu9/cpufreq/scaling_cur_freq:4100000
doug@s19:~$ fg
taskset -c 0 yes > /dev/null
^C
doug@s19:~$

免责声明:此答案可能存在一些不准确之处。我不确定这款第 11 代处理器是否只有一个 PLL(锁相环)。

编辑:要恢复更改,只需撤销所做的操作即可。如果忘记了原始的最大 CPU 频率设置,请先查询:

doug@s19:~$ grep . /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_max_freq
/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq:4800000
/sys/devices/system/cpu/cpu10/cpufreq/cpuinfo_max_freq:4800000
/sys/devices/system/cpu/cpu11/cpufreq/cpuinfo_max_freq:4800000
/sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_max_freq:4800000
/sys/devices/system/cpu/cpu2/cpufreq/cpuinfo_max_freq:4800000
/sys/devices/system/cpu/cpu3/cpufreq/cpuinfo_max_freq:4800000
/sys/devices/system/cpu/cpu4/cpufreq/cpuinfo_max_freq:4800000
/sys/devices/system/cpu/cpu5/cpufreq/cpuinfo_max_freq:4800000
/sys/devices/system/cpu/cpu6/cpufreq/cpuinfo_max_freq:4800000
/sys/devices/system/cpu/cpu7/cpufreq/cpuinfo_max_freq:4800000
/sys/devices/system/cpu/cpu8/cpufreq/cpuinfo_max_freq:4800000
/sys/devices/system/cpu/cpu9/cpufreq/cpuinfo_max_freq:4800000

将其放回原位并检查:

doug@s19:~$ echo 4800000 | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq
4800000
doug@s19:~$ grep . /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq
/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu10/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu11/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu5/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu6/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu7/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu8/cpufreq/scaling_max_freq:4800000
/sys/devices/system/cpu/cpu9/cpufreq/scaling_max_freq:4800000

相关内容