Linux CPU主频优化指南
一 核心思路与前提检查
lsmod | grep acpi_cpufreqcat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors 与 cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governorcat /proc/cpuinfo | grep -i "cpu mhz";更建议使用 cpupower frequency-info 获取详细与当前频率。二 快速优化步骤
sudo cpupower -c all frequency-set -g performanceecho performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governorsudo yum install -y kernel-tools,在 /etc/sysconfig/cpupower 中设置 CPUPOWER_START_OPTS="frequency-set -g performance",并启用服务 sudo systemctl enable --now cpupower.service。sudo apt install -y cpufrequtils,编辑 /etc/default/cpufrequtils 设置 GOVERNOR="performance",重启服务 sudo systemctl restart cpufrequtils。/etc/systemd/system/cpu-performance.service,内容示例:[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=yes
[Install]
WantedBy=multi-user.target
启用:sudo systemctl daemon-reload && sudo systemctl enable --now cpu-performance.service。cpupower frequency-info 或 cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 应显示为 performance。三 进阶优化与稳定性
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequenciesecho <频率值kHz> | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freqecho <频率值kHz> | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freqecho userspace | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governorecho <频率值kHz> | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeedsudo systemctl stop cpuspeed(或 service cpuspeed stop)。四 监控与 BIOS 层优化
lscputurbostatperf stat(关注 CPI、指令吞吐等)五 常见问题与排查
lsmod | grep acpi_cpufreq),现代 Intel 平台可能使用 intel_pstate(仅 performance/powersave)。如需使用其他 governor,可在 /etc/default/grub 的 GRUB_CMDLINE_LINUX_DEFAULT 添加 intel_pstate=disable 后更新 grub 并重启。turbostat 观察实际频率与 Tau(睿频时间)变化。scaling_governor。