内容大纲
内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
1. CPU性能调优概述
CPU性能调优是通过调整CPU频率、亲和性、优先级等参数,提高系统性能的过程。
# CPU频率调节:动态调整CPU频率
# CPU亲和性:绑定进程到特定CPU
# 进程优先级:调整进程优先级
# CPU调度器:选择合适的调度器
# 中断处理:优化中断处理
# CPU缓存:优化CPU缓存使用
2. CPU频率调节
使用cpufreq工具调节CPU频率。
学习交流加群风哥QQ113257174
# 1. 查看CPU频率信息
[root@localhost ~]# cpupower frequency-info
analyzing CPU 0:
driver: intel_pstate
CPUs which run at the same hardware frequency: 0
CPUs which need to have their frequency coordinated by software: 0
maximum transition latency: Cannot determine or is not supported.
hardware limits: 800 MHz – 3.60 GHz
available frequency steps: 3.60 GHz, 3.50 GHz, 3.40 GHz, 3.30 GHz, 3.20 GHz, 3.10 GHz, 3.00 GHz, 2.90 GHz, 2.80 GHz, 2.70 GHz, 2.60 GHz, 2.50 GHz, 2.40 GHz, 2.30 GHz, 2.20 GHz, 2.10 GHz, 2.00 GHz, 1.90 GHz, 1.80 GHz, 1.70 GHz, 1.60 GHz, 1.50 GHz, 1.40 GHz, 1.30 GHz, 1.20 GHz, 1.10 GHz, 1.00 GHz, 900 MHz, 800 MHz
available cpufreq governors: performance powersave
current policy: frequency should be within 800 MHz and 3.60 GHz.
The governor “powersave” may decide which speed to use
within this range.
current CPU frequency: 800 MHz (asserted by call to hardware)
boost state support:
Supported: yes
Active: yes
Boost States: 0
Total States: 2
Pstate-P0: 3600MHz (max turbo)
# 2. 设置CPU频率调节器
[root@localhost ~]# cpupower frequency-set -g performance
Setting cpu: 0
Setting cpu: 1
Setting cpu: 2
Setting cpu: 3
# 3. 设置CPU频率为powersave模式
[root@localhost ~]# cpupower frequency-set -g powersave
Setting cpu: 0
Setting cpu: 1
Setting cpu: 2
Setting cpu: 3
# 4. 设置CPU频率为特定值
[root@localhost ~]# cpupower frequency-set -f 2.40GHz
Setting cpu: 0
Setting cpu: 1
Setting cpu: 2
Setting cpu: 3
# 5. 查看当前CPU频率
[root@localhost ~]# cpupower frequency-info
analyzing CPU 0:
driver: intel_pstate
CPUs which run at the same hardware frequency: 0
CPUs which need to have their frequency coordinated by software: 0
maximum transition latency: Cannot determine or is not supported.
hardware limits: 800 MHz – 3.60 GHz
available frequency steps: 3.60 GHz, 3.50 GHz, 3.40 GHz, 3.30 GHz, 3.20 GHz, 3.10 GHz, 3.00 GHz, 2.90 GHz, 2.80 GHz, 2.70 GHz, 2.60 GHz, 2.50 GHz, 2.40 GHz, 2.30 GHz, 2.20 GHz, 2.10 GHz, 2.00 GHz, 1.90 GHz, 1.80 GHz, 1.70 GHz, 1.60 GHz, 1.50 GHz, 1.40 GHz, 1.30 GHz, 1.20 GHz, 1.10 GHz, 1.00 GHz, 900 MHz, 800 MHz
available cpufreq governors: performance powersave
current policy: frequency should be within 800 MHz and 3.60 GHz.
The governor “performance” may decide which speed to use
within this range.
current CPU frequency: 2.40 GHz (asserted by call to hardware)
boost state support:
Supported: yes
Active: yes
Boost States: 0
Total States: 2
Pstate-P0: 3600MHz (max turbo)
3. CPU亲和性设置
<
from PG视频:www.itpux.com
p>使用taskset工具设置CPU亲和性。
# 1. 查看进程的CPU亲和性
[root@localhost ~]# taskset -pc 1234
pid 1234’s current affinity list: 0-3
# 2. 设置进程的CPU亲和性
[root@localhost ~]# taskset -pc 0,1 1234
pid 1234’s new affinity list: 0,1
# 3. 启动进程时设置CPU亲和性
[root@localhost ~]# taskset -c 0,1 /usr/bin/nginx
# 4. 查看CPU亲和性掩码
[root@localhost ~]# taskset -p 1234
pid 1234’s current affinity mask: 3
# 5. 使用十六进制掩码设置CPU亲和性
[root@localhost ~]# taskset -p 0x3 1234
pid 1234’s new affinity mask: 3
# 6. 查看所有CPU核心
[root@localhost ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 46 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Vendor ID: GenuineIntel
Model name: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
CPU family: 6
Model: 158
Thread(s) per core: 1
Core(s) per socket: 4
Socket(s): 1
Stepping: 10
CPU max MHz: 4700.0000
CPU min MHz: 800.0000
BogoMIPS: 7400.00
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves arat md_clear arch_capabilities
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 12288K
NUMA node(s): 1
NUMA node0 CPU(s): 0-3
4. 进程优先级调整
使用nice和renice工具调整进程优先级。
学习交流加群风哥微信: itpux-com
# 1. 查看进程优先级
[root@localhost ~]# ps -eo pid,ni,comm | head -10
PID NI COMMAND
1 0 systemd
2 0 kthreadd
3 0 rcu_gp
4 0 rcu_par_gp
5 0 kworker/0:0H
6 0 kworker/0:0
7 0 kworker/u4:0
8 0 mm_percpu_wq
9 0 ksoftirqd/0
# 2. 使用nice启动进程
[root@localhost ~]# nice -n 10 /usr/bin/nginx
# 3. 使用renice调整进程优先级
[root@localhost ~]# renice -n 10 -p 1234
1234 (process ID) old priority 0, new priority 10
# 4. 调整用户所有进程的优先级
[root@localhost ~]# renice -n 10 -u nginx
1234 (process ID) old priority 0, new priority 10
1235 (process ID) old priority 0, new priority 10
1236 (process ID) old priority 0, new priority 10
# 5. 调整进程组所有进程的优先级
[root@localhost ~]# renice -n 10 -g 1234
1234 (process ID) old priority 0, new priority 10
1235 (process ID) old priority 0, new priority 10
1236 (process ID) old priority 0, new priority 10
# 6. 查看nice值范围
[root@localhost ~]# cat /proc/sys/kernel/nice_value
-20 19
5. CPU性能监控
使用top、vmstat、mpstat等工具监控CPU性能。
# 1. 使用top监控CPU
[root@localhost ~]# top -bn1 | head -20
top – 10:00:00 up 1 day, 2:30, 4 users, load average: 0.10, 0.15, 0.12
Tasks: 123 total, 1 running, 122 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.5 us, 1.0 sy, 0.0 ni, 96.0 id, 0.5 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 8192.0 total, 4096.0 free, 2048.0 used, 2048.0 buff/cache
MiB Swap: 4096.0 total, 4096.0 free, 0.0 used. 8192.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 root 20 0 123456 10240 4096 S 2.5 0.1 0:00.05 systemd
2345 root 20 0 65536 5120 2048 S 1.0 0.1 0:00.03 kworker/u4:0
3456 mysql 20 0 2097152 524288 102400 S 0.5 6.3 1:23.45 mysqld
4567 nginx 20 0 262144 51200 20480 S 0.3 0.6 0:12.34 nginx
5678 root 20 0 131072 10240 4096 R 0.2 0.1 0:00.01 top
# 2. 使用vmstat监控CPU
[root@localhost ~]# vmstat 1 3
procs ———–memory———- —swap– —–io—- -system– ——cpu—–
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 4096000 102400 2048000 0 0 0 0 10 20 2 1 96 1 0
0 0 0 4096000 102400 2048000 0 0 0 0 12 25 3 1 95 1 0
1 0 0 4096000 102400 2048000 0 0 0 0 11 22 2 1 96 1 0
# 3. 使用mpstat监控CPU
[root@localhost ~]# mpstat -P ALL 1 3
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)
10:00:00 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:00:01 AM all 2.50 0.00 1.00 0.50 0.00 0.00 0.00 0.00 0.00 96.00
10:00:01 AM 0 2.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 97.00
10:00:01 AM 1 3.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 96.00
10:00:01 AM 2 2.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 97.00
10:00:01 AM 3 3.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 96.00
10:00:02 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:00:03 AM all 3.00 0.00 1.50 0.00 0.00 0.00 0.00 0.00 0.00 95.50
10:00:03 AM 0 3.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 96.00
10:00:03 AM 1 3.00 0.00 2.00 0.00 0.00 0.00 0.00 0.00 0.00 95.00
10:00:03 AM 2 3.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 96.00
10:00:03 AM 3 3.00 0.00 2.00 0.00 0.00 0.00 0.00 0.00 0.00 95.00
10:00:04 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:00:05 AM all 2.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 97.00
10:00:05 AM 0 2.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 97.00
10:00:05 AM 1 2.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 97.00
10:00:05 AM 2 2.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 97.00
10:00:05 AM 3 2.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 97.00
Average: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
Average: all 2.50 0.00 1.17 0.17 0.00 0.00 0.00 0.00 0.00 96.17
Average: 0 2.33 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 96.67
Average: 1 2.67 0.00 1.33 0.00 0.00 0.00 0.00 0.00 0.00 96.00
Average: 2 2.33 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 96.67
Average: 3 2.67 0.00 1.33 0.00 0.00 0.00 0.00 0.00 0.00 96.00
风哥提示:
6. 实战案例
CPU性能调优实战案例。
# 1. 查看CPU信息
[root@localhost ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 46 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Vendor ID: GenuineIntel
Model name: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
CPU family: 6
Model: 158
Thread(s) per core: 1
Core(s) per socket: 4
Socket(s): 1
Stepping: 10
CPU max MHz: 4700.0000
CPU min MHz: 800.0000
BogoMIPS: 7400.00
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves arat md_clear arch_capabilities
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 12288K
NUMA node(s): 1
NUMA node0 CPU(s): 0-3
# 2. 设置CPU频率为performance模式
[root@localhost ~]# cpupower frequency-set -g performance
Setting cpu: 0
Setting cpu: 1
Setting cpu: 2
Setting cpu: 3
# 3. 查看CPU频率
[root@localhost ~]# cpupower frequency-info
analyzing CPU 0:
driver: intel_pstate
CPUs which run at the same hardware frequency: 0
CPUs which need to have their frequency coordinated by software: 0
maximum transition latency: Cannot determine or is not supported.
hardware limits: 800 MHz – 3.60 GHz
available frequency steps: 3.60 GHz, 3.50 GHz, 3.40 GHz, 3.30 GHz, 3.20 GHz, 3.10 GHz, 3.00 GHz, 2.90 GHz, 2.80 GHz, 2.70 GHz, 2.60 GHz, 2.50 GHz, 2.40 GHz, 2.30 GHz, 2.20 GHz, 2.10 GHz, 2.00 GHz, 1.90 GHz, 1.80 GHz, 1.70 GHz, 1.60 GHz, 1.50 GHz, 1.40 GHz, 1.30 GHz, 1.20 GHz, 1.10 GHz, 1.00 GHz, 900 MHz, 800 MHz
available cpufreq governors: performance powersave
current policy: frequency should be within 800 MHz and 3.60 GHz.
The governor “performance” may decide which speed to use
within this range.
current CPU frequency: 3.60 GHz (asserted by call to hardware)
boost state support:
Supported: yes
Active: yes
Boost States: 0
Total States: 2
Pstate-P0: 3600MHz (max turbo)
# 4. 设置nginx进程的CPU亲和性
[root@localhost ~]# taskset -pc 0,1 $(pgrep nginx | head -1)
pid 1234’s new affinity list: 0,1
# 5. 调整nginx进程的优先级
[root@localhost ~]# renice -n -5 -p $(pgrep nginx | head -1)
1234 (process ID) old priority 0, new priority -5
# 6. 创建CPU调优脚本
[root@localhost ~]# cat > /usr/local/bin/cpu-tuning.sh << 'EOF'
#!/bin/bash
# script.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# CPU调优脚本
LOG_FILE="/var/log/cpu-tuning.log"
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# 设置CPU频率
set_cpu_frequency() {
log “Setting CPU frequency to performance mode…”
cpupower frequency-set -g performance
log “CPU frequency set to performance mode.”
}
# 设置nginx进程的CPU亲和性
set_nginx_affinity() {
log “Setting nginx process CPU affinity…”
NGINX_PID=$(pgrep nginx | head -1)
if [ -n “$NGINX_PID” ]; then
taskset -pc 0,1 $NGINX_PID
log “Nginx process $NGINX_PID CPU affinity set to 0,1.”
fi
}
# 调整nginx进程的优先级
set_nginx_priority() {
log “Setting nginx process priority…”
NGINX_PID=$(pgrep nginx | head -1)
if [ -n “$NGINX_PID” ]; then
renice -n -5 -p $NGINX_PID
log “Nginx process $NGINX_PID priority set to -5.”
fi
}
# 主函数
main() {
log “Starting CPU tuning…”
set_cpu_frequency
set_nginx_affinity
set_nginx_priority
log “CPU tuning completed.”
}
# 执行主函数
main
EOF
# 7. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/cpu-tuning.sh
# 8. 配置开机自动执行
[root@localhost ~]# cat > /etc/systemd/system/cpu-tuning.service << 'EOF'
[Unit]
Description=CPU Tuning
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/cpu-tuning.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
# 9. 启用并启动服务
[root@localhost ~]# systemctl enable --now cpu-tuning.service
Created symlink /etc/systemd/system/multi-user.target.wants/cpu-tuning.service → /etc/systemd/system/cpu-tuning.service.
Created symlink /etc/systemd/system/cpu-tuning.service → /etc/systemd/system/cpu-tuning.service.
# 10. 查看服务状态
[root@localhost ~]# systemctl status cpu-tuning.service
● cpu-tuning.service - CPU Tuning
Loaded: loaded (/etc/systemd/system/cpu-tuning.service; enabled; preset: disabled)
Active: active (exited) since Fri 2026-04-03 10:00:00 CST; 5s ago
Process: 12345 ExecStart=/usr/local/bin/cpu-tuning.sh (code=exited, status=0/SUCCESS)
Main PID: 12345 (code=exited, status=0/SUCCESS)
Apr 03 10:00:00 localhost systemd[1]: Starting CPU Tuning...
Apr 03 10:00:00 localhost cpu-tuning.sh[12345]: [2026-04-03 10:00:00] Starting CPU tuning...
Apr 03 10:00:00 localhost cpu-tuning.sh[12345]: [2026-04-03 10:00:00] Setting CPU frequency to performance mode...
Apr 03 10:00:00 localhost cpu-tuning.sh[12345]: [2026-04-03 10:00:00] CPU frequency set to performance mode.
Apr 03 10:00:00 localhost cpu-tuning.sh[12345]: [2026-04-03 10:00:00] Setting nginx process CPU affinity...
Apr 03 10:00:00 localhost cpu-tuning.sh[12345]: [2026-04-03 10:00:00] Nginx process 1234 CPU affinity set to 0,1.
Apr 03 10:00:00 localhost cpu-tuning.sh[12345]: [2026-04-03 10:00:00] Setting nginx process priority...
Apr 03 10:00:00 localhost cpu-tuning.sh[12345]: [2026-04-03 10:00:00] Nginx process 1234 priority set to -5.
Apr 03 10:00:00 localhost cpu-tuning.sh[12345]: [2026-04-03 10:00:00] CPU tuning completed.
Apr 03 10:00:00 localhost systemd[1]: Finished CPU Tuning.
# 11. 查看日志
[root@localhost ~]# tail -f /var/log/cpu-tuning.log
[2026-04-03 10:00:00] Starting CPU tuning...
[2026-04-03 10:00:00] Setting CPU frequency to performance mode...
[2026-04-03 10:00:00] CPU frequency set to performance mode.
[2026-04-03 10:00:00] Setting nginx process CPU affinity...
[2026-04-03 10:00:00] Nginx process 1234 CPU affinity set to 0,1.
[2026-04-03 10:00:00] Setting nginx process priority...
[2026-04-03 10:00:00] Nginx process 1234 priority set to -5.
[2026-04-03 10:00:00] CPU tuning completed.
提示
CPU性能调优需要根据实际应用场景进行调整。对于高性能计算场景,建议使用p
更多学习教程公众号风哥教程itpux_com
erformance模式和CPU亲和性;对于节能场景,建议使用powersave模式。定期监控CPU性能,及时调整调优策略。
