Linux性能调优实战指南
前言
系统性能问题是运维工作中最常见的挑战之一。一个慢查询、一次内存泄漏、一个I/O瓶颈都可能导致服务不可用。本文将系统讲解Linux性能分析和调优的方法论,帮助你快速定位和解决性能问题。
一、性能分析方法论
1.1 USE方法
Utilization(使用率)、Saturation(饱和度)、Errors(错误)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| top mpstat -P ALL 1
free -m vmstat 1
iostat -xz 1 df -h
sar -n DEV 1 netstat -s
|
1.2 性能分析工具图谱
1 2 3 4 5 6 7 8
| 观测层次: 应用层 → 系统调用 → 内核 → 硬件
工具分类: • 监控工具:top, htop, vmstat, iostat • 追踪工具:strace, perf, ftrace • 剖析工具:perf, gprof • 基准测试:sysbench, fio, iperf
|
二、CPU性能优化
2.1 CPU性能指标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| lscpu cat /proc/cpuinfo
top
mpstat -P ALL 1 5
uptime
|
2.2 CPU瓶颈排查
高CPU使用率:
1 2 3 4 5 6 7 8 9 10 11
| top -o %CPU
top -H -p <PID>
ps aux --sort=-%cpu | head -10
pidstat -u 1 5 -p <PID>
|
上下文切换过多:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| vmstat 1
pidstat -w 1 5
perf record -e sched:sched_switch -a sleep 10 perf report
|
CPU缓存未命中:
1 2 3 4 5 6
| perf stat -e cache-references,cache-misses ./your_program
|
2.3 CPU调优技巧
CPU亲和性:
1 2 3 4 5 6 7 8
| taskset -c 0,1 ./your_program
taskset -p <PID>
taskset -cp 0,1 <PID>
|
进程优先级:
1 2 3 4 5 6 7 8
| nice -n -10 ./your_program
renice -10 -p <PID>
chrt -f 99 ./realtime_program
|
中断绑定:
1 2 3 4 5
| cat /proc/interrupts
echo 2 > /proc/irq/50/smp_affinity_list
|
三、内存性能优化
3.1 内存性能指标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| free -h
vmstat 1
ps aux --sort=-%mem | head -10
cat /proc/<PID>/status
|
3.2 内存泄漏排查
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| while true; do ps -o pid,vsz,rss,comm -p <PID> sleep 5 done
valgrind --leak-check=full ./your_program
pmap -x <PID>
smem -p | grep your_process
|
3.3 内存调优
swappiness调整:
1 2 3 4 5 6 7 8 9 10
| cat /proc/sys/vm/swappiness
sysctl vm.swappiness=10
echo "vm.swappiness=10" >> /etc/sysctl.conf sysctl -p
|
透明大页:
1 2 3 4 5 6 7 8 9
| cat /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag
|
Page Cache优化:
1 2 3 4 5 6 7 8
| sync; echo 1 > /proc/sys/vm/drop_caches sync; echo 2 > /proc/sys/vm/drop_caches sync; echo 3 > /proc/sys/vm/drop_caches
sysctl -w vm.dirty_background_ratio=5 sysctl -w vm.dirty_ratio=10
|
四、磁盘I/O性能优化
4.1 I/O性能指标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| iostat -xz 1
iotop
pidstat -d 1 5
cat /sys/block/sda/queue/nr_requests
|
4.2 I/O瓶颈排查
找出I/O密集型进程:
1 2 3 4 5 6 7 8 9 10
| iotop -o
lsof -p <PID>
cat /proc/<PID>/io
|
分析I/O模式:
1 2 3 4 5 6 7
| blktrace -d /dev/sda -o trace blkparse trace.blktrace.0 > trace.txt
perf record -e block:block_rq_issue -a sleep 10 perf script
|
4.3 I/O优化
I/O调度器:
1 2 3 4 5 6 7 8 9 10 11 12
| cat /sys/block/sda/queue/scheduler
echo bfq > /sys/block/sda/queue/scheduler
|
文件系统调优:
1 2 3 4 5 6 7 8
| mount -o noatime,nodiratime,data=writeback /dev/sda1 /mnt
mount -o noatime,nodiratime,logbufs=8,logbsize=256k /dev/sda1 /mnt
/dev/sda1 /mnt ext4 defaults,noatime,nodiratime 0 2
|
预读优化:
1 2 3 4 5
| blockdev --getra /dev/sda
blockdev --setra 8192 /dev/sda
|
使用SSD优化:
1 2 3 4 5 6 7 8
| fstrim -v /
0 3 * * 0 /sbin/fstrim -av
lsblk --discard
|
五、网络性能优化
5.1 网络性能指标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| ip -s link
sar -n DEV 1 5
netstat -s netstat -antp
ss -s ss -tanp
|
5.2 网络瓶颈排查
找出网络流量大的进程:
1 2 3 4 5 6 7 8
| nethogs
iftop -i eth0
ss -tp
|
丢包排查:
1 2 3 4 5 6 7 8 9
| netstat -s | grep -i drop netstat -s | grep -i error
ip -s link show eth0
ethtool -S eth0 | grep drop
|
5.3 网络调优
TCP参数优化:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.somaxconn = 32768 net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_congestion_control = bbr
sysctl -p
|
网卡优化:
1 2 3 4 5 6 7 8 9 10 11 12 13
| ethtool eth0
ethtool -G eth0 rx 4096 tx 4096
ethtool -K eth0 tso on ethtool -K eth0 gso on ethtool -K eth0 lro on
ethtool -C eth0 rx-usecs 50
|
多队列优化:
1 2 3 4 5 6 7 8 9 10
| ethtool -l eth0
ethtool -L eth0 combined 4
for i in {0..3}; do echo $i > /sys/class/net/eth0/queues/rx-$i/rps_cpus done
|
六、系统调优实战案例
6.1 高并发Web服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| sysctl -w fs.file-max=1000000 sysctl -w net.core.somaxconn=65535 sysctl -w net.ipv4.tcp_max_syn_backlog=8192 sysctl -w net.ipv4.ip_local_port_range="10000 65535"
worker_processes auto; worker_rlimit_nofile 65535;
events { use epoll; worker_connections 65535; multi_accept on; }
http { keepalive_timeout 65; keepalive_requests 100; sendfile on; tcp_nopush on; tcp_nodelay on; }
|
6.2 数据库服务器
1 2 3 4 5 6 7 8 9 10 11 12 13
| vm.swappiness = 1 vm.dirty_ratio = 15 vm.dirty_background_ratio = 5
echo deadline > /sys/block/sda/queue/scheduler
mount -o noatime,nodiratime,data=writeback
echo never > /sys/kernel/mm/transparent_hugepage/enabled
|
七、性能监控和告警
7.1 监控脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #!/bin/bash
LOG_FILE="/var/log/performance.log"
while true; do timestamp=$(date '+%Y-%m-%d %H:%M:%S') cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}') mem_free=$(free -m | awk 'NR==2{print $4}') disk_util=$(iostat -x 1 2 | awk '/^sda/ {print $14}' | tail -1) net_rx=$(sar -n DEV 1 1 | awk '/eth0/ {print $5}' | tail -1) echo "$timestamp CPU:$cpu_usage% MEM_FREE:${mem_free}MB DISK:$disk_util% NET_RX:$net_rx" >> $LOG_FILE sleep 60 done
|
7.2 性能基准测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| sysbench cpu --cpu-max-prime=20000 run
sysbench memory --memory-block-size=1K --memory-total-size=10G run
fio --name=randwrite --ioengine=libaio --iodepth=16 \ --rw=randwrite --bs=4k --direct=1 \ --size=1G --numjobs=4 --runtime=60 --group_reporting
iperf3 -s
iperf3 -c server_ip -t 60 -P 4
|
八、总结
性能优化原则
- 测量优先:先测量,再优化
- 找瓶颈:优化瓶颈才有效果
- 权衡取舍:没有银弹,根据场景选择
- 持续监控:建立监控体系
常用工具清单
- CPU: top, htop, mpstat, perf
- 内存: free, vmstat, smem
- I/O: iostat, iotop, fio
- 网络: ss, netstat, iftop, iperf
- 综合: sar, sysstat, glances
掌握这些技能,你就能成为Linux性能优化专家!
参考资料