前言
程序跑得慢,但不知道慢在哪。CPU不高、内存够用、磁盘IO也正常,可就是响应慢。这时候需要看系统调用(syscall):程序到底在做什么?是频繁读写文件、网络IO阻塞,还是系统调用本身开销太大?
Linux提供了strace、perf、ftrace等工具,可以追踪系统调用、分析性能瓶颈。这篇文章从实战出发,先讲怎么用工具定位问题,再讲怎么优化。
一、strace基础:追踪系统调用
1.1 基本用法
# 追踪命令的系统调用
strace ls -l
# 追踪已运行进程
strace -p <PID>
# 只追踪特定系统调用
strace -e trace=open,read,write ls
# 统计系统调用次数和时间
strace -c ls -l
1.2 常用选项
# -f 追踪子进程
strace -f ./script.sh
# -e trace=file 只追踪文件相关
strace -e trace=file ls
# -e trace=network 只追踪网络相关
strace -e trace=network curl http://example.com
# -e trace=process 只追踪进程相关
strace -e trace=process ps aux
# -o 输出到文件
strace -o trace.log ls
# -s 显示完整字符串(默认只显示32字节)
strace -s 200 ls
# -tt 显示时间戳
strace -tt ls
# -T 显示系统调用耗时
strace -T ls
1.3 实战案例:定位文件读取慢
# 追踪文件读取
strace -e trace=open,openat,read -T ls -R /large/directory
# 输出示例
# openat(AT_FDCWD, "/large/directory", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3 <0.000012>
# openat(3, "file1.txt", O_RDONLY|O_CLOEXEC) = 4 <0.000008>
# read(4, "content...", 4096) = 4096 <0.000015>
# read(4, "", 4096) = 0 <0.000003>
# +++ exited with 0 +++
分析要点:
<0.000015>:系统调用耗时,如果某个调用耗时很长,就是瓶颈
1.4 实战案例:定位网络连接慢
# 追踪网络连接
strace -e trace=network -T curl http://slow-api.com
# 输出示例
# socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) = 3 <0.000010>
# connect(3, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("1.2.3.4")}, 16) = 0 <2.500000>
# sendto(3, "GET / HTTP/1.1\r\nHost: slow-api.com\r\n\r\n", 45, MSG_NOSIGNAL, NULL, 0) = 45 <0.000020>
# recvfrom(3, "HTTP/1.1 200 OK\r\n...", 4096, 0, NULL, NULL) = 1024 <3.200000>
分析要点:
connect耗时2.5秒:网络连接慢,可能是DNS解析慢或网络延迟- 如果
connect返回-1:连接失败,检查网络或防火墙
二、strace高级用法:过滤和分析
2.1 过滤特定文件或路径
# 只追踪访问特定文件
strace -e trace=open,openat -e trace=file -P /etc/passwd ls
# 追踪访问特定目录
strace -e trace=open,openat 2>&1 | grep "/var/log"
2.2 统计系统调用
# 统计系统调用次数和时间
strace -c ls -R /large/directory
# 输出示例
# % time seconds usecs/call calls errors syscall
# ------ ----------- ----------- --------- --------- ----------------
# 45.23 0.123456 123 1000 openat
# 30.12 0.082345 82 1000 read
# 15.45 0.042123 42 1000 close
# 9.20 0.025123 25 1000 fstat
分析要点:
如果openat占用45%时间且调用1000次,说明文件打开太频繁,可能需要批量处理或缓存。
2.3 追踪系统调用参数
# 显示系统调用参数
strace -e trace=openat -v ls
# 输出示例
# openat(AT_FDCWD, "/etc/passwd", O_RDONLY|O_CLOEXEC) = 3
2.4 实战案例:分析Nginx慢请求
# 追踪Nginx worker进程
strace -p $(pgrep -f "nginx: worker") -e trace=network,epoll_wait -T
# 如果发现epoll_wait耗时很长,说明没有事件,可能是上游服务慢
# 如果发现connect/recvfrom耗时很长,说明网络IO慢
三、perf性能分析:更深入的性能追踪
3.1 perf基础
strace看系统调用,perf看CPU性能。perf可以采样CPU、追踪函数调用、分析热点。
# 安装perf(CentOS/RHEL)
yum install perf
# 安装perf(Ubuntu/Debian)
apt-get install linux-perf
# 查看perf版本
perf --version
3.2 perf top:实时查看CPU热点
# 实时查看CPU占用最高的函数
perf top
# 只看特定进程
perf top -p <PID>
# 只看特定命令
perf top -g -p $(pgrep nginx)
输出示例:
Samples: 1M of event 'cpu-clock', Event count (approx.): 25000000000
45.23% nginx [kernel] [k] __d_lookup
12.34% nginx libc-2.31.so [.] __GI___libc_malloc
8.90% nginx nginx [.] ngx_http_core_run_phases
分析要点:
- 如果内核函数占用高,可能是系统调用开销大或IO等待
3.3 perf record和perf report:记录和分析
# 记录性能数据(默认采样10秒)
perf record -g ./your_program
# 记录特定进程
perf record -g -p <PID>
# 记录特定时间
perf record -g sleep 30
# 查看报告
perf report
3.4 perf stat:统计性能事件
# 统计命令的性能事件
perf stat ls -R /large/directory
# 输出示例
# Performance counter stats for 'ls -R /large/directory':
#
# 1234.56 msec task-clock # 0.123 CPUs utilized
# 1000 context-switches # 0.810 K/sec
# 500 cpu-migrations # 0.405 K/sec
# 2000 page-faults # 0.001 M/sec
# 5000000000 cycles # 4.050 GHz
# 2000000000 instructions # 0.40 insn per cycle
# 500000000 branches # 405.000 M/sec
# 10000000 branch-misses # 2.00% of all branches
关键指标:
context-switches:上下文切换次数,如果很高说明调度频繁page-faults:缺页中断,如果很高说明内存访问模式不好branch-misses:分支预测失败率,如果>5%可能影响性能
3.5 perf trace:追踪系统调用(类似strace)
# 追踪系统调用
perf trace ls
# 追踪特定进程
perf trace -p <PID>
# 只追踪特定系统调用
perf trace -e syscalls:sys_enter_openat ls
四、实战案例:定位程序慢的原因
4.1 案例1:文件查找慢
现象:find /large -name "*.log"很慢
排查步骤:
# 1. 用strace看系统调用
strace -c find /large -name "*.log"
# 发现openat调用10000次,耗时5秒
# 2. 用perf看CPU热点
perf record -g find /large -name "*.log"
perf report
# 发现大部分时间在__d_lookup(目录查找)
# 3. 优化方案
# - 使用locate数据库(updatedb + locate)
# - 限制搜索深度(-maxdepth)
# - 使用更快的文件系统索引工具
4.2 案例2:网络请求慢
现象:HTTP请求响应慢
排查步骤:
# 1. 用strace追踪网络调用
strace -e trace=network -T curl http://api.example.com
# 发现connect耗时2秒,recvfrom耗时3秒
# 2. 用perf看网络栈
perf record -g -e syscalls:sys_enter_connect curl http://api.example.com
perf report
# 3. 进一步排查
# - DNS解析:time nslookup api.example.com
# - 网络延迟:ping api.example.com
# - 服务器性能:检查服务器CPU/IO
4.3 案例3:程序启动慢
现象:程序启动需要10秒
排查步骤:
# 1. 用strace看启动过程
strace -tt -T ./your_program 2>&1 | head -100
# 发现加载了很多动态库,每个库加载耗时0.1秒
# 2. 优化方案
# - 减少动态库依赖
# - 使用静态链接
# - 预加载常用库(LD_PRELOAD)
五、系统调用优化实践
5.1 减少系统调用次数
问题:频繁的文件操作
# 不好的做法:每次读一个字节
for i in {1..1000}; do
echo$i >> file.txt
done
# 好的做法:批量写入
echo {1..1000} | tr ' ''\n' >> file.txt
问题:频繁的网络连接
# 不好的做法:每次请求都新建连接
for url in$urls; do
curl $url
done
# 好的做法:复用连接(HTTP/1.1 keep-alive或连接池)
5.2 使用更高效的系统调用
问题:读取大文件
# read():每次最多读一个页面(4KB)
# readahead():预读,减少IO等待
# mmap():内存映射,减少拷贝
问题:文件查找
# opendir() + readdir():遍历目录
# 如果频繁查找,用inotify监控文件变化,维护索引
5.3 异步IO:减少阻塞
# 同步IO:read()阻塞直到数据就绪
# 异步IO:aio_read(),不阻塞,用回调处理
# 网络IO:epoll/kqueue,事件驱动
# 文件IO:io_uring(Linux 5.1+),异步文件IO
六、跨网络调试:批量追踪系统调用
6.1 场景:多台服务器性能分析
如果需要在多台服务器上分析性能,逐个SSH登录很麻烦。
解决方案:
- 组网工具:WireGuard、ZeroTier、星空组网等,把机器组成虚拟局域网
用组网工具后,不管机器实际在哪,都可以用虚拟内网IP直接访问。然后用Ansible批量执行strace/perf:
# Ansible批量执行strace
ansible all -m shell -a "strace -c -p \$(pgrep nginx)"
# 或者用脚本批量收集
for host in 10.0.0.{1..10}; do
ssh $host"strace -c -p \$(pgrep nginx)" > trace_${host}.log
done
6.2 场景:分布式系统性能分析
分布式系统涉及多台机器,需要同时追踪多个进程。
方案:
# 批量收集perf数据
for host in web{1..5} db{1..3}; do
ssh $host"perf record -g -p \$(pgrep app) sleep 30" &
done
wait
# 汇总分析
for host in web{1..5} db{1..3}; do
scp $host:perf.data perf_${host}.data
done
perf report -i perf_web1.data
七、常见问题与注意事项
7.1 strace性能开销
strace会显著影响程序性能(可能慢10-100倍),生产环境谨慎使用。
建议:
7.2 权限问题
# strace追踪其他用户进程需要root
sudo strace -p <PID>
# perf也需要root或调整perf_event_paranoid
echo -1 | sudo tee /proc/sys/kernel/perf_event_paranoid
7.3 输出太多怎么办
# 限制输出行数
strace -e trace=open 2>&1 | head -100
# 输出到文件
strace -o trace.log ls
# 只追踪错误
strace -e trace=open -e status=failed ls
7.4 内核函数追踪
# perf可以追踪内核函数
perf record -g -e syscalls:sys_enter_openat ls
perf report
# 需要调试符号
# CentOS: debuginfo-install kernel
# Ubuntu: apt-get install linux-image-$(uname -r)-dbg
八、工具对比与选择
| | | |
|---|
strace | | | |
perf | | | |
ftrace | | | |
ltrace | | | |
tcpdump | | | |
选择建议:
总结
| | | |
|---|
| strace -e trace=file | openat | |
| strace -e trace=network | connect | |
| perf top | | |
| perf stat | context-switches | |
| perf stat | page-faults | |
核心思路:
- 再用
strace -e trace=特定调用或perf top定位热点 - 最后针对性优化(减少调用次数、使用更高效调用、异步IO)
注意事项: