Linux CPU Performance 模式全攻略:从原理到实践,Ubuntu/CentOS/RHEL 一键锁频指南

一、为什么要把 CPU 调到 performance 模式  

1. 去掉“省电/变频”带来的抖动:  

   默认的 ondemand、conservative 等 governor 会根据负载升降频,某些实时业务(交易撮合、音视频、游戏服务器、高并发网关)可能出现几十微秒到毫秒级的延迟抖动。  

2. 把 CPU 锁在最高频率:

可带来 1 %–15 %不等的延迟降低和吞吐提升(Intel/AMD 官方 SPEC 及大量线上业务实测)。  

3. 在虚拟化、容器云场景里,宿主机统一跑在 performance 可避免“邻居”负载变化对同台 VM/容器的频率干扰。  

4. 代价:

整机功耗上升 5 %–30 %(视硬件与业务负载而定),散热/电费需重新评估。

二、各发行版设置方法

1. Ubuntu / Debian 系  

   临时(立即生效,重启失效)  

   sudo apt update && sudo apt install cpufrequtils

   sudo cpufreq-set -g performance          # 对所有逻辑 CPU 生效

   # 如要指定核心:sudo cpufreq-set -c 0 -g performance

   永久(开机自动 performance)  

   方法 A(systemd 服务,官方推荐):  

   sudo tee /etc/systemd/system/cpu-performance.service > /dev/null <<'EOF'

   [Unit]

   Description=Set CPU governor to performance

   After=multi-user.target

   [Service]

   Type=oneshot

  ExecStart=/bin/bash -c 'for f in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo performance > $f; done'

   RemainAfterExit=true

   [Install]

   WantedBy=multi-user.target

   EOF

   sudo systemctl daemon-reload

   sudo systemctl enable --now cpu-performance.service

   方法 B(GRUB 内核参数,彻底关闭 intel_pstate):  

  sudo sed -i 's/GRUB_CMDLINE_LINUX="/GRUB_CMDLINE_LINUX="intel_pstate=disable /' /etc/default/grub

   sudo update-grub && sudo reboot

   # 重启后再用上面的 systemd 服务或 cpufrequtils 设置 performance。

2. CentOS 7 / RHEL 7  

   临时  

   sudo yum install -y cpufreq-utils

   sudo cpupower frequency-set -g performance

   永久  

   sudo cpupower frequency-set -g performance          # 先立即生效

   echo 'cpupower frequency-set -g performance' | sudo tee /etc/rc.d/rc.local

   sudo chmod +x /etc/rc.d/rc.local                    # 确保 rc.local 可执行

3. CentOS 8 / RHEL 8+(已默认使用 cpupower)  

   临时  

   sudo dnf install -y kernel-tools

   sudo cpupower frequency-set -g performance

   永久(systemd 服务,同上思路):  

   sudo tee /etc/systemd/system/cpu-performance.service > /dev/null <<'EOF'

   [Unit]

   Description=Set CPU governor to performance

   After=multi-user.target

   [Service]

   Type=oneshot

   ExecStart=/usr/bin/cpupower frequency-set -g performance

   RemainAfterExit=true

   [Install]

   WantedBy=multi-user.target

   EOF

   sudo systemctl daemon-reload

   sudo systemctl enable --now cpu-performance.service

4. 验证  

   任意发行版均可执行:  

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

   # 输出应为:performance

小结  

临时测试→ `cpufreq-set -g performance`(Ubuntu)或 `cpupower frequency-set -g performance`(CentOS/RHEL)。  

生产长期运行 → 推荐用 systemd 服务开机自启,或在 BIOS 里直接锁定 “High Performance / Max Performance”。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值