Linux CPUInfo:CPU主频调整方法
导读:Linux CPU主频调整方法 一 核心概念与查看 关键术语 Governor(调速器):决定频率如何变化,常见有 performance、powersave、ondemand、conservative、userspace。 P-sta...
Linux CPU主频调整方法
一 核心概念与查看
- 关键术语
- Governor(调速器):决定频率如何变化,常见有 performance、powersave、ondemand、conservative、userspace。
- P-state / C-state:P-state 是运行频率状态(如 P0 最高),C-state 是空闲状态(如 C0 工作,C1–C6 逐步更深更省电)。
- 查看命令
- 查看支持的调速器与当前调速器:
- cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
- cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
- 查看频率与驱动:
- cpupower frequency-info
- cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
- 查看空闲状态与驱动:
- cpupower idle-info
- cat /sys/devices/system/cpu/cpuidle/current_driver
- cat /sys/devices/system/cpu/cpuidle/current_governor_ro
- 说明:频率单位在 sysfs 中为 kHz;设置频率或上下限时,取值必须是硬件/驱动支持的步进,且不能超过 cpuinfo_min_freq / cpuinfo_max_freq。
- 查看支持的调速器与当前调速器:
二 常用调整方法
- 使用 cpupower(推荐)
- 安装:Debian/Ubuntu 系可用 apt 安装 cpufrequtils;RHEL/CentOS 系可用 yum 安装 cpupowerutils。
- 设置全核为高性能模式:
- cpupower -c all frequency-set -g performance
- 固定到某一频率(需先切到 userspace 调速器):
- cpupower -c all frequency-set -g userspace
- cpupower -c all frequency-set -f 2400000(单位 kHz,示例为 2.4 GHz)
- 仅调整上下限(不改变调速器):
- cpupower -c all frequency-set -d 1800000 -u 2400000
- 直接写入 sysfs(无需额外工具)
- 切换调速器(示例为 performance):
- echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
- 设置上下限(单位 kHz):
- echo 1800000 | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
- echo 2400000 | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
- 在 userspace 模式下设定固定频率:
- echo 2400000 | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed
- 提示:某些平台需要先设置 scaling_max_freq 再设置 scaling_min_freq;若提示权限不足,使用 sudo 或提升权限后再执行。
- 切换调速器(示例为 performance):
三 进阶场景
- 禁用 Intel P-state 并使用 acpi-cpufreq(仅在必要时)
- 编辑 /etc/default/grub,在 GRUB_CMDLINE_LINUX_DEFAULT 中添加 intel_pstate=disable,然后执行 update-grub 并重启。
- 重启后可用 cpupower 或 sysfs 按上文方法设置调速器与频率。
- 禁用深度空闲以降低时延(慎用)
- 查看并选择性禁用空闲状态(示例禁用 C1/C2):
- cpupower idle-info
- cpupower idle-set -d 1…2
- 注意:禁用空闲会提高功耗与温度,仅在低时延场景评估后使用。
- 查看并选择性禁用空闲状态(示例禁用 C1/C2):
- 使用 tuned 服务统一性能策略
- 安装并启用 tuned,选择 performance 或 power-save 预设:
- sudo yum install tuned
- sudo systemctl start tuned & & sudo systemctl enable tuned
- sudo tuned-adm profile performance
- 安装并启用 tuned,选择 performance 或 power-save 预设:
四 持久化与注意事项
- 持久化方式
- systemd 服务示例(设置高性能模式):
- /etc/systemd/system/cpufreq.service
- [Unit]
- Description=Set CPU frequency to performance mode
- [Service]
- Type=oneshot
- ExecStart=/usr/bin/cpupower frequency-set -g performance
- [Install]
- WantedBy=multi-user.target
- [Unit]
- 启用:sudo systemctl enable --now cpufreq.service
- /etc/systemd/system/cpufreq.service
- 发行版配置:部分系统支持在 /etc/default/cpufrequtils 中写入 GOVERNOR=“performance” 实现开机生效(视发行版而定)。
- systemd 服务示例(设置高性能模式):
- 注意事项
- 设置频率必须是驱动提供的 可用步进,且受 bios_limit / cpuinfo_min_freq / cpuinfo_max_freq 约束;越界会被自动钳制或报错。
- 某些老旧驱动(如 speedstep-smi)在 ondemand 下可能有较长切换延时,按需选择 performance 或 userspace。
- 固定高频或禁用空闲会提升 功耗与温度,请确保散热与供电充足;在笔记本上可能影响 电池续航。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Linux CPUInfo:CPU主频调整方法
本文地址: https://pptw.com/jishu/758119.html
