一、调整CPU频率调节器(Governor)
CPU频率调节器是Linux内核管理CPU频率的核心组件,通过选择不同的调节器可直接控制CPU的功耗与性能平衡。常见调节器及适用场景如下:
echo powersave | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor(批量设置所有核心)。echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor。echo ondemand | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor。二、使用工具固化设置(开机生效)
临时设置(如通过echo命令)重启后会失效,需通过工具固化配置:
sudo apt install cpufrequtils,编辑/etc/default/cpufrequtils文件,设置GOVERNOR="performance"(或powersave),重启服务sudo systemctl restart cpufrequtils。sudo yum install kernel-tools,通过cpupower frequency-set -g performance临时设置,或添加cpupower frequency-set -g performance到/etc/rc.local(需赋予执行权限chmod +x /etc/rc.local)实现开机生效。/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=true
[Install]
WantedBy=multi-user.target
然后执行sudo systemctl daemon-reload、sudo systemctl enable --now cpu-performance.service启用服务。三、优化内核与中断管理
watch --interval=1 cat /proc/interrupts(若timer中断值非1000步进则已启用);启用方法:内核编译时选择Processor type and features -> Tickless System (Dynamic Ticks),或通过内核参数nohz=on开启。sudo apt install irqbalance(Ubuntu/Debian)或sudo yum install irqbalance(CentOS/RHEL),启动服务sudo systemctl start irqbalance并设置开机自启sudo systemctl enable irqbalance。四、调整进程与任务调度
nice(启动时设置)或renice(运行时调整)降低非关键进程的优先级,减少其对CPU资源的占用(如后台备份、日志记录等任务)。示例:nice -n 19 ./backup_script.sh(启动时设置为最低优先级),renice +10 -p 1234(将PID为1234的进程优先级降低10)。taskset将特定任务绑定至固定核心(如将数据库进程绑定至核心0),避免任务在多核心间切换的开销(减少CPU唤醒次数,降低功耗)。示例:taskset -c 0 ./database_server(将进程绑定至核心0)。五、利用监控工具定位高耗电进程
使用powertop工具分析系统中高耗电的进程与设备,针对性优化。安装sudo apt install powertop(Ubuntu/Debian)或sudo yum install powertop(CentOS/RHEL),运行sudo powertop(需root权限),查看“Overview” tab中的“Power est.”列(显示各进程的功耗估计),优先优化功耗高的进程(如关闭不必要的后台服务、优化应用程序的CPU使用率)。
六、BIOS层面设置(可选)
部分服务器/台式机的BIOS支持CPU性能模式设置,可直接在BIOS中开启“High Performance”或“Max Performance”模式(关闭“Intel SpeedStep”“AMD Cool’n’Quiet”等节能功能),强制CPU始终运行在最高频率。需重启系统进入BIOS修改(具体选项因硬件而异)。