| name | Linux 性能诊断工具箱 |
| description | CPU、内存、磁盘、网络性能排查的系统化流程,覆盖 top/vmstat/iostat/ss/strace 等工具 |
| version | 1.0.0 |
Linux 性能诊断工具箱
遵循 USE 方法论(Utilization 使用率 / Saturation 饱和度 / Errors 错误)系统化排查。
快速总览
uptime
dmesg -T | tail -20
vmstat 1 5
mpstat -P ALL 1 3
iostat -xz 1 3
free -h
df -h
ss -s
top -bn1 | head -20
CPU 诊断
uptime
nproc
top
htop
mpstat -P ALL 1 5
pidstat 1 5
perf top
perf record -g -p <pid> -- sleep 30 && perf report
常见原因:死循环、正则回溯、GC 风暴、锁竞争。
内存诊断
free -h
ps aux --sort=-%mem | head -20
pidstat -r 1 5
pmap -x <pid>
cat /proc/<pid>/smaps_rollup
dmesg -T | grep -i 'oom\|kill'
journalctl -k | grep -i 'oom'
常见原因:内存泄漏、缓存未限制、JVM 堆配置不合理。
磁盘 I/O 诊断
df -h
df -i
du -sh /var/* | sort -rh | head
iostat -xz 1 5
iotop -oP
pidstat -d 1 5
find / -xdev -type f -size +100M 2>/dev/null | head -20
lsof +L1
常见原因:日志撑满磁盘、大量小文件、swap 抖动。
网络诊断
ss -s
ss -tlnp
ss -tnp state established
iftop -i eth0
nethogs
ping -c 5 <host>
traceroute <host>
mtr <host>
curl -o /dev/null -w "time_total: %{time_total}s\n" http://<url>
tcpdump -i eth0 -nn port 80 -c 100
tcpdump -i eth0 host <ip> -w capture.pcap
常见原因:连接数耗尽(TIME_WAIT)、DNS 解析慢、带宽饱和。
进程级深入诊断
lsof -p <pid>
ls -la /proc/<pid>/fd | wc -l
strace -p <pid> -c
strace -p <pid> -e trace=network -f
pstree -p <pid>
排查流程建议
- 先看全局:
uptime + vmstat + free + df 定位瓶颈类型
- 锁定资源:是 CPU / 内存 / 磁盘 / 网络 中的哪一个
- 找到进程:
top / iotop / ss 找到占用最高的进程
- 深入分析:对目标进程用
strace / perf / lsof 分析根因
- 确认修复:修改后持续观察指标是否恢复正常