树莓派5 CPU主频

我在 Raspberry Pi OS 上运行ZFS 中编译ZFS 动态内核模块支持(DKMS) 模块,发现如果没有使用官方27W电源的时候,一旦CPU全力运行会导致电压不稳而死机。由于目前正在旅途中无法获取匹配电源,所以我想到这种不稳定情况只有全速运行是才出现电压不足,那么何不将 CPU频率调节器 调整为最低频率( powersave )来实现稳定运行呢?

Intel CPU架构CPU频率 性能伸缩(Performance Scaling) 类似, ARM 架构也实现了类似的控制CPU主频管理机制。从 树莓派Raspberry Pi 5 的操作系统 sysfs 中检查目录 /sys/devices/system/cpu/cpu0/cpufreq/ 可以看到

cpufreq 访问接口文件

sysfs cpufreq接口

说明

cpuinfo_min_freq

处理器能够运行的最低操作频率(Hz)

cpuinfo_max_freq

处理器能够运行的最高操作频率(Hz)

scaling_driver

用于设置该CPU频率的cpufreq驱动(例如intel_pstate)

scaling_available_governors

当前内核可以使用的CPUfreq调机器: 通过cat该文件内容可以看到所支持的调节器 如performance 或 powersave

scaling_min_freq和scaling_max_freq

当前cpufreq调机器的策略限制主频范围,通过echo值到这两个文件中能够修订策略限制的范围。注意需要先修改scaling_max_freq再修改scaling_min_freq

scaling_setspeed

通过 echo 值到该文件能够调节CPU速率,但是调节值必须位于scaling_min_freq和scaling_max_freq范围内

  • 安装 cpupower 工具:

Debian 中安装 cpupower
apt install linux-cpupower
  • 动态设置 powersave governor:

设置 powersave governor
cpupower frequency-set -g powersave

然后就可以开始 Raspberry Pi OS 上运行ZFS 编译工作

  • 为了能够持久化配置 powersave governor,则修改 /etc/default/cpu_governor :

Debian 系列设置启动的 CPU governor
# Set default governor on all CPU cores.
# More info https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
#
# CPU_DEFAULT_GOVERNOR="ondemand"
#
CPU_DEFAULT_GOVERNOR="powersave"
  • 检查调整后的CPU governor:

cpupower frequency-info 命令检查CPU主频伸缩策略
cpupower frequency-info

输出显示可以看到当前governor是 powersave :

当前CPU governor信息
analyzing CPU 1:
  driver: cpufreq-dt
  CPUs which run at the same hardware frequency: 0 1 2 3
  CPUs which need to have their frequency coordinated by software: 0 1 2 3
  maximum transition latency:  Cannot determine or is not supported.
  hardware limits: 1.50 GHz - 2.40 GHz
  available frequency steps:  1.50 GHz, 1.60 GHz, 1.70 GHz, 1.80 GHz, 1.90 GHz, 2.00 GHz, 2.10 GHz, 2.20 GHz, 2.30 GHz, 2.40 GHz
  available cpufreq governors: conservative ondemand userspace powersave performance schedutil
  current policy: frequency should be within 1.50 GHz and 2.40 GHz.
                  The governor "powersave" may decide which speed to use
                  within this range.
  current CPU frequency: 1.50 GHz (asserted by call to hardware)

参考