Published on

Fedora Linux CPU Frequency Scaling using cpupower

Authors
  • avatar
    Name
    ttyS3
    Twitter

命令行方式1 -- 不依赖外部工具

查看当前生效的 scaling_driver

cat /sys/devices/system/cpu/cpufreq/policy*/scaling_driver

查看当前生效的 scaling_governor

cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

查看可用的 scaling_governor

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

设置为高性能模式

对于台式机来说,我觉得没有什么理由不设置为 performance 模式

echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

设置为节能模式

echo powersave | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

查看其它信息:

❯ /bin/ls -1 /sys/devices/system/cpu/cpu0/cpufreq/
affected_cpus
base_frequency
cpuinfo_max_freq
cpuinfo_min_freq
cpuinfo_transition_latency
energy_performance_available_preferences
energy_performance_preference
related_cpus
scaling_available_governors
scaling_cur_freq
scaling_driver
scaling_governor
scaling_max_freq
scaling_min_freq
scaling_setspeed

比如查看当前频率我们读取 scaling_cur_freq 这个文件即可:

watch cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq

Scaling governors 的具体含义

Scaling governors are power schemes determining the desired frequency for the CPU. Some request a constant frequency, while others implement algorithms to dynamically adjust according to the system load. The governors included in the kernel are:

Note: Each governor is compatible with any scaling driver, except for intel_pstate and amd_pstate in active mode. These provide pseudo-governors in the form of powersave and performance. See Autonomous frequency scaling.

GovernorDescription
performanceRun the CPU at the maximum frequency, obtained from /sys/devices/system/cpu/cpuX/cpufreq/scaling_max_freq.
powersaveRun the CPU at the minimum frequency, obtained from /sys/devices/system/cpu/cpuX/cpufreq/scaling_min_freq.
userspaceRun the CPU at user-specified frequencies, configurable via /sys/devices/system/cpu/cpuX/cpufreq/scaling_setspeed.
ondemandScales the frequency dynamically according to current load. Jumps to the highest frequency and then possibly backs off as the idle time increases.
conservativeScales the frequency dynamically according to current load. It does so more gradually than ondemand.
schedutilScheduler-driven CPU frequency selection source, source.

命令行方式2 -- cpupower

cpupower 命令在 Fedora 里面由 kernel-tools 包提供,如果没有你需要安装一下:

sudo dnf install -y kernel-tools

查看当前状态:

sudo cpupower frequency-info

直接设置 CPU 工作在特定频率:

cpupower frequency-set -f clock_freq

设置 为 performance 模式

cpupower frequency-set -g performance

目前 Fedora 不再提供 cpupower.service 了, 这里从 Arch 抄一个:

[Unit]
Description=Apply cpupower configuration
ConditionVirtualization=!container

[Service]
Type=oneshot
EnvironmentFile=/etc/default/cpupower
ExecStart=/usr/bin/cpupower $CPUPOWER_ARGS
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

/etc/default/cpupower 内容如下:

CPUPOWER_ARGS="frequency-set -g performance"
sudo systemctl enable --now cpupower
sudo systemctl status cpupower
sudo cpupower frequency-info

命令行方式3 -- powerprofilesctl

powerprofilesctl 命令由 power-profiles-daemon 包提供。

https://gitlab.freedesktop.org/upower/power-profiles-daemon/-/blob/main/README.md

https://fedoraproject.org/wiki/Changes/Power_Profiles_Daemon

它主要是通过 DBus 与 power-profiles-daemon.service 起来的 power-profiles-daemon 进程通信.

❯ powerprofilesctl                 
* performance:
    CpuDriver:	intel_pstate
    Degraded:   no

  balanced:
    CpuDriver:	intel_pstate
    PlatformDriver:	placeholder

  power-saver:
    CpuDriver:	intel_pstate
    PlatformDriver:	placeholder
❯ powerprofilesctl get   
performance

misc

kernel-tools 里的其它命令

kernel-tools 这个包里除了cpupower 命令, 还有一些其它实用工具:

rpm -qlv kernel-tools | rg /usr/bin
-rwxr-xr-x    1 root     root     14696 Mar  6 08:00 /usr/bin/centrino-decode
-rwxr-xr-x    1 root     root     78592 Mar  6 08:00 /usr/bin/cpupower
-rwxr-xr-x    1 root     root     18792 Mar  6 08:00 /usr/bin/gpio-event-mon
-rwxr-xr-x    1 root     root     14696 Mar  6 08:00 /usr/bin/gpio-hammer
-rwxr-xr-x    1 root     root     14696 Mar  6 08:00 /usr/bin/gpio-watch
-rwxr-xr-x    1 root     root     31096 Mar  6 08:00 /usr/bin/iio_event_monitor
-rwxr-xr-x    1 root     root     35192 Mar  6 08:00 /usr/bin/iio_generic_buffer
-rwxr-xr-x    1 root     root    123792 Mar  6 08:00 /usr/bin/intel-speed-select
-rwxr-xr-x    1 root     root     64461 Mar  6 08:00 /usr/bin/kvm_stat
-rwxr-xr-x    1 root     root     14880 Mar  6 08:00 /usr/bin/lsgpio
-rwxr-xr-x    1 root     root     22904 Mar  6 08:00 /usr/bin/lsiio
-rwxr-xr-x    1 root     root     26984 Mar  6 08:00 /usr/bin/page_owner_sort
-rwxr-xr-x    1 root     root     14704 Mar  6 08:00 /usr/bin/powernow-k8-decode
-rwxr-xr-x    1 root     root     44224 Mar  6 08:00 /usr/bin/slabinfo
-rwxr-xr-x    1 root     root     43736 Mar  6 08:00 /usr/bin/tmon
-rwxr-xr-x    1 root     root    132920 Mar  6 08:00 /usr/bin/turbostat
-rwxr-xr-x    1 root     root     39896 Mar  6 08:00 /usr/bin/x86_energy_perf_policy

Fedora 这个包里并没有包含 cpupower.service

不过它带了一个 /usr/lib/systemd/system/kvm_stat.service

Service that logs KVM kernel module trace events

/usr/bin/kvm_stat -dtcz -s 10 -L /var/log/kvm_stat.csv

看上去是把 kvm 内核模块的状态信息输出到 csv 文件。 并且还带了相应的 logrotate 配置文件 /etc/logrotate.d/kvm_stat

intel_pstate

The newer kernel version is using the intel_pstate as the default driver. which is only supported governor performance and powersave.

In order to use the governor ondemand, you need to disable the intel_pstate driver and using the acpi-cpufreq driver as default.

Edit the grub configuration.

sudo nvim /etc/default/grub

Add the following configuration.

GRUB_CMDLINE_LINUX_DEFAULT="intel_pstate=disable"

Regenerate your grub.

sudo grub2-mkconfig -o /boot/grub2/grub.cfg

Now the intel_pstate driver will be disabled, and the acpi_cpufreq will be enabled by default.

内核文档: https://docs.kernel.org/admin-guide/pm/intel_pstate.html

intel_pstate is not modular, so it cannot be unloaded, which means that the only way to pass early-configuration-time parameters to it is via the kernel command line. However, its configuration can be adjusted via sysfs to a great extent. In some configurations it even is possible to unregister it via sysfs which allows another CPUFreq scaling driver to be loaded and registered (see below).

不过,我应该大概率用不到 ondemand 模式。

refs

How to make a CPU governor change permanent?

How-to Increasing performance by changing CPU governor and reducing swappiness

ArchLinux WIKI: CPU frequency scaling

Note: Fedora CPU Frequency Scaling using cpupower

https://github.com/torvalds/linux/blob/master/Documentation/admin-guide/pm/intel_pstate.rst

https://www.kernel.org/doc/Documentation/cpu-freq/intel-pstate.txt

https://www.kernel.org/doc/html/v6.8/admin-guide/pm/intel_pstate.html

https://events.static.linuxfound.org/sites/events/files/slides/LinuxConEurope_2015.pdf