性能问题
性能问题排查的第一步是确定瓶颈所在:CPU、内存、磁盘 I/O 还是网络。本文介绍常用的性能分析工具和排查方法。
快速概览:系统整体状态
Section titled “快速概览:系统整体状态”在深入分析之前,先获取系统整体状态的快照。
$ uptime输出示例:10:32:05 up 45 days, load average: 2.50, 1.80, 0.95
负载平均值(load average)分别是 1 分钟、5 分钟、15 分钟的平均值。作为参考,负载值不应长期超过 CPU 核心数。
$ nproc$ free -h # 内存$ df -h # 磁盘空间$ uptime # 负载$ sudo ss -s # 网络连接统计CPU 性能排查
Section titled “CPU 性能排查”使用 top
Section titled “使用 top”$ toptop 界面中的关键指标:
- %us — 用户空间 CPU 使用率
- %sy — 内核空间 CPU 使用率
- %wa — 等待 I/O 的 CPU 时间(高值说明磁盘是瓶颈)
- %id — 空闲 CPU 时间
常用操作:
- 按
1— 显示每个 CPU 核心的使用情况 - 按
P— 按 CPU 使用率排序 - 按
M— 按内存使用率排序 - 按
c— 显示完整命令行
$ top -bn1 | head -30使用 htop
Section titled “使用 htop”htop 提供更直观的界面,需要从 EPEL 安装:
$ sudo dnf install -y epel-release$ sudo dnf install -y htop$ htophtop 中按 F6 选择排序方式,按 F5 切换树状视图。
使用 mpstat 查看每核心 CPU 状态
Section titled “使用 mpstat 查看每核心 CPU 状态”$ sudo dnf install -y sysstat$ mpstat -P ALL 1 5定位高 CPU 进程
Section titled “定位高 CPU 进程”$ ps aux --sort=-%cpu | head -11$ top -H -p <PID>常见 CPU 瓶颈原因
Section titled “常见 CPU 瓶颈原因”- 应用程序 bug — 死循环、低效算法
- 过多进程/线程 — 上下文切换开销大
- 中断风暴 — 查看
/proc/interrupts - 编译或压缩任务 — CPU 密集型操作
$ vmstat 1 5cs 列为上下文切换次数。每秒数万次以上可能影响性能。
内存性能排查
Section titled “内存性能排查”使用 free 查看内存
Section titled “使用 free 查看内存”$ free -h输出解读:
- total — 物理内存总量
- used — 已使用的内存
- free — 完全空闲的内存
- buff/cache — 被用作缓冲/缓存的内存(可回收)
- available — 实际可用内存(free + 可回收的缓存)
重要: Linux 会积极使用空闲内存作为磁盘缓存,这是正常行为。判断内存是否不足应看
available列,而不是free列。
使用 vmstat 监控
Section titled “使用 vmstat 监控”$ vmstat 1 10关键列:
- si / so — 换入/换出页面数(swap)。持续大于 0 说明内存不足
- free — 空闲内存(KB)
- buff / cache — 缓冲和缓存内存
定位内存占用大户
Section titled “定位内存占用大户”$ ps aux --sort=-%mem | head -11$ cat /proc/<PID>/status | grep -E '(VmRSS|VmSize|VmSwap)'检查 Swap 使用
Section titled “检查 Swap 使用”$ swapon --show$ for pid in /proc/[0-9]*; do name=$(cat $pid/comm 2>/dev/null) swap=$(grep VmSwap $pid/status 2>/dev/null | awk '{print $2}') [ -n "$swap" ] && [ "$swap" -gt 0 ] && echo "$swap kB - $name (PID: $(basename $pid))" done | sort -rn | head -10常见内存瓶颈原因
Section titled “常见内存瓶颈原因”- 内存泄漏 — 应用程序不断消耗更多内存
- 缓存未释放 — 某些应用占用大量缓存
- OOM Killer — 系统内存耗尽时自动杀死进程
$ dmesg | grep -i "out of memory"$ journalctl --since "7 days ago" | grep -i "oom-kill"磁盘 I/O 性能排查
Section titled “磁盘 I/O 性能排查”使用 iostat
Section titled “使用 iostat”$ iostat -x 1 5关键指标:
- %util — 磁盘利用率。持续接近 100% 说明磁盘已饱和
- await — 平均 I/O 等待时间(毫秒)。HDD 正常在 5-20ms,SSD 在 0.5-2ms
- r/s, w/s — 每秒读/写操作次数(IOPS)
- rkB/s, wkB/s — 每秒读/写吞吐量
$ iostat -x /dev/sda 1 5使用 iotop 定位 I/O 大户
Section titled “使用 iotop 定位 I/O 大户”$ sudo dnf install -y iotop$ sudo iotop -o-o 参数仅显示有 I/O 活动的进程。
检查磁盘空间
Section titled “检查磁盘空间”$ df -h$ df -i注意: 即使磁盘空间看起来未满,inode 耗尽同样会导致无法创建新文件。
$ sudo du -sh /var/log/* | sort -rh | head -10$ sudo find / -xdev -type f -size +100M -exec ls -lh {} \; 2>/dev/null | sort -k5 -rh | head -10常见磁盘瓶颈原因
Section titled “常见磁盘瓶颈原因”- 日志文件过大 —
/var/log写满 - 数据库 I/O 密集 — 查询未优化
- Swap 频繁读写 — 实际是内存不足导致
- RAID 降级 — 磁盘阵列性能下降
网络性能排查
Section titled “网络性能排查”使用 sar 查看网络吞吐量
Section titled “使用 sar 查看网络吞吐量”$ sar -n DEV 1 5关键指标:
- rxpck/s, txpck/s — 每秒收/发包数
- rxkB/s, txkB/s — 每秒收/发字节数
- %ifutil — 网络接口利用率
使用 ss 查看连接状态
Section titled “使用 ss 查看连接状态”$ ss -s$ ss -ant | awk '{print $1}' | sort | uniq -c | sort -rn大量 TIME_WAIT 可能说明短连接过多。大量 CLOSE_WAIT 可能说明应用有 bug。
$ sudo dnf install -y iperf3$ iperf3 -s$ iperf3 -c <服务端IP>常见网络瓶颈原因
Section titled “常见网络瓶颈原因”- 带宽饱和 — 流量超过网卡或链路容量
- 丢包 — 网络质量差或缓冲区溢出
- 连接数过多 — 超过系统或应用限制
- DNS 延迟 — 每次请求都做 DNS 查询
综合分析工具 sar
Section titled “综合分析工具 sar”sar(System Activity Reporter)可以回溯历史性能数据。
$ sudo dnf install -y sysstat$ sudo systemctl enable --now sysstat$ sar -u$ sar -r$ sar -d$ sar -u -f /var/log/sa/sa20$ sar -u -s 08:00:00 -e 12:00:00性能问题快速定位流程
Section titled “性能问题快速定位流程”按照以下顺序可以快速定位大多数性能问题:
1. 查看系统负载
Section titled “1. 查看系统负载”$ echo "负载: $(cat /proc/loadavg | awk '{print $1, $2, $3}'), CPU 核心: $(nproc)"2. 判断瓶颈类型
Section titled “2. 判断瓶颈类型”$ vmstat 1 5us + sy高 → CPU 瓶颈wa高 → 磁盘 I/O 瓶颈si/so持续大于 0 → 内存不足- 以上都正常但系统慢 → 可能是网络或应用层问题
3. 深入分析
Section titled “3. 深入分析”根据第 2 步的结果,使用对应章节的工具进行深入分析。
常见快速修复
Section titled “常见快速修复”CPU 高:限制进程 CPU 使用
Section titled “CPU 高:限制进程 CPU 使用”$ nice -n 19 <命令>$ sudo dnf install -y cpulimit$ sudo cpulimit -p <PID> -l 50 # 限制到 50% CPU内存不足:调整 Swap
Section titled “内存不足:调整 Swap”$ sudo fallocate -l 2G /swapfile$ sudo chmod 600 /swapfile$ sudo mkswap /swapfile$ sudo swapon /swapfile$ sudo sync && sudo sysctl vm.drop_caches=3磁盘满:快速释放空间
Section titled “磁盘满:快速释放空间”$ sudo dnf clean all$ sudo journalctl --vacuum-size=200M$ sudo dnf remove --oldinstallonly --setopt installonly_limit=2 kernel网络连接数过多:调整内核参数
Section titled “网络连接数过多:调整内核参数”$ sysctl net.core.somaxconn$ cat /proc/sys/net/ipv4/tcp_max_tw_buckets$ sudo sysctl -w net.core.somaxconn=65535$ sudo sysctl -w net.ipv4.tcp_max_tw_buckets=65535