cpupower
是一个用于管理和监控 Linux 系统上 CPU 性能和电源管理设置的命令行工具。它是一个前端工具,简化了对内核中 CPU 频率调节器(CPUFreq)子系统的访问。cpupower
工具包包含多个实用程序,其中最常用的是 cpupower frequency-info
和 cpupower frequency-set
。
1. 命令概述
名称:cpupower
作用:
- 用于查询和调整 CPU 的电源管理参数,包括频率、功耗策略、空闲状态等。
- 属于
cpufrequtils
工具包,与 Linux 内核的cpufreq
子系统协同工作,优化 CPU 性能与功耗平衡。
核心功能:
- 查询 CPU 频率信息:如当前频率、支持的频率范围、电源管理策略(governor)。
- 设置电源管理策略:如
performance
(性能模式)、powersave
(节能模式)、ondemand
(动态调整)。 - 调整 CPU 频率上下限:手动指定 CPU 的最小/最大频率。
- 管理 CPU 空闲状态:查看或设置 CPU 的空闲状态(idle states)。
2. 基本语法
cpupower [ -c cpulist ] <command> [ARGS]
-c cpulist
:指定要操作的 CPU 核心(如0-3
表示核心 0 到 3)。<command>
:具体子命令(如frequency-info
,frequency-set
,idle-info
)。ARGS
:子命令的参数(如-g performance
设置 governor)。
3. 主要子命令及用法
子命令 | 功能描述 | 常用参数 |
---|---|---|
frequency-info | 显示CPU频率信息 | -e , -m , -w |
frequency-set | 调整CPU频率策略 | -d , -u , -g |
idle-info | 显示C-states空闲状态 | -J |
idle-set | 配置空闲状态 | -D , -E |
monitor | 实时监控CPU状态 | -l , -m |
set | 全局电源设置 | -b (perf-bias) |
(1) frequency-info
- 用途:显示当前 CPU 的频率信息和电源管理策略。
- 语法:
cpupower frequency-info [ -c cpulist ]
- 示例:
输出示例:cpupower frequency-info -c 0
analysis of CPU 0: --------------- hardware limits: 800 MHz - 4.2 GHz current CPU frequency: 4.2 GHz (scaling_cur_freq) current policy: frequency should be within 800 MHz and 4.2 GHz (governor "performance")
- 关键信息:
current CPU frequency
:当前运行频率。current policy
:当前 governor 和频率范围。
(2) frequency-set
- 用途:设置 CPU 的频率策略或上下限。
- 语法:
cpupower frequency-set [ -g <governor> ] [ -d <min_freq> ] [ -u <max_freq> ]
- 参数:
-g <governor>
:设置 governor(如performance
,powersave
)。-d <min_freq>
:设置最小频率(单位:kHz 或后缀GHz
/MHz
)。-u <max_freq>
:设置最大频率。
- 示例:
# 设置所有 CPU 为性能模式 sudo cpupower -c all frequency-set -g performance # 设置 CPU 0 的最大频率为 3.5 GHz sudo cpupower -c 0 frequency-set -u 3500000kHz
(3) idle-info
- 用途:显示 CPU 的空闲状态(C-states)信息。
- 语法:
cpupower idle-info [ -c cpulist ]
- 示例:
输出示例:cpupower idle-info
CPUidle driver: intel_idle CPUidle governor: menu Number of idle states: 4 State 0: POLLING STATE, 0.000 ms, 0.000 uW State 1: C1, 0.000 ms, 0.000 uW State 2: C3, 1.000 ms, 100.000 uW State 3: C6, 2.000 ms, 200.000 uW
(4) help
- 用途:显示支持的子命令和用法。
- 语法:
cpupower help
- 输出示例:
Available commands: frequency-info: Show CPU frequency related info frequency-set: Set CPU frequency policy idle-info: Show CPU idle states info help: Show this help message
4. 重要选项
选项 | 作用 | 示例 |
---|---|---|
-c cpulist | 指定操作的 CPU 核心列表(如 0-3 或 all )。 | cpupower -c 0-3 frequency-set -g ondemand → 设置核心 0-3 的 governor。 |
-v, --version | 显示 cpupower 版本信息。 | cpupower -v → 输出版本号。 |
-h, --help | 显示命令用法和子命令列表。 | cpupower help → 列出所有支持的子命令。 |
5. 常用 Governor 策略
Governor | 描述 | 适用场景 |
---|---|---|
performance | 始终运行在最高频率,提供最大性能。 | 服务器、高性能计算、游戏等对延迟敏感的场景。 |
powersave | 始终运行在最低频率,最小化功耗。 | 笔记本电脑低电量时、对性能要求不高的场景。 |
ondemand | 根据 CPU 负载动态调整频率(负载高时升频,空闲时降频)。 | 平衡性能与功耗的通用场景。 |
conservative | 类似 ondemand ,但频率变化更平缓,避免剧烈波动。 | 对频率稳定性要求较高的场景。 |
userspace | 允许用户空间程序直接设置频率(需手动控制)。 | 需要精细控制的特殊场景。 |
6. 示例
CPU列表语法
0-3 # CPU 0,1,2,3
0-7:2 # CPU 0,2,4,6
1,3,5-7 # CPU 1,3,5,6,7
all # 所有核心
1. 查看CPU频率信息
# 显示所有核心的调频器信息
cpupower -c all frequency-info
# 检查可用调控器
cpupower frequency-info -g
2. 调整CPU策略
# 设置为性能模式(所有核心)
cpupower -c all frequency-set -g performance
# 限制CPU0的最高频率
cpupower -c 0 frequency-set -u 2.5GHz
3. 监控与调试
# 实时监控所有核心状态
cpupower -c all monitor -m "Mperf"
# 查看空闲状态统计
cpupower idle-info
典型应用场景
服务器性能优化
# 禁用所有节能功能
for cpu in $(seq 0 $(nproc --all)); do
cpupower -c $cpu frequency-set -g performance
done
笔记本省电配置
# 启用ondemand调控器
cpupower frequency-set -g ondemand
# 设置能效偏好
cpupower set -b 15
7. 常见问题及解决方案
Q1:设置 governor 失败,提示权限不足?
A:
sudo cpupower frequency-set -g performance
Q2:指定的频率超出支持范围?
A:
# 查看支持的频率范围
cpupower frequency-info | grep "hardware limits"
# 设置在范围内
sudo cpupower frequency-set -u 3500000kHz
Q3:如何让设置在系统启动时生效?
A:
# 方法1:创建 systemd 服务
sudo nano /etc/systemd/system/cpufreq.service
# 内容:
[Unit]
Description=Set CPU frequency to performance
[Service]
Type=oneshot
ExecStart=/usr/bin/cpupower frequency-set -g performance
[Install]
WantedBy=multi-user.target
sudo systemctl enable --now cpufreq.service
8. 注意事项
- 权限要求:
- 大多数操作需要
root
权限(如sudo
)。
- 大多数操作需要
- 频率范围限制:
- 使用
cpupower frequency-info
确认支持的频率范围,避免设置超出范围的值。
- 使用
- 多核系统:
- 使用
-c all
或-c 0-3
等指定核心列表,确保对所有目标 CPU 生效。
- 使用
- 持久化设置:
- 修改后的设置重启后失效,可通过以下方式持久化:
# 编辑 /etc/default/cpufrequtils GOVERNOR="performance"
- 修改后的设置重启后失效,可通过以下方式持久化: