在Linux中豪诲,當(dāng)程序出現(xiàn)性能問(wèn)題時(shí),需要定位其性能瓶頸在哪里挂绰,以下是幾個(gè)定位性能問(wèn)題的工具:
可以先用top查看cpu 耗時(shí)屎篱,top -> 1;查看每個(gè)core的耗時(shí)情況葵蒂;us是用戶態(tài)調(diào)用交播,sy是內(nèi)核態(tài)。
1践付、strace: 匯總內(nèi)核態(tài)系統(tǒng)調(diào)用的時(shí)間
strace -f -cp PID
: -c 匯總各個(gè)操作的總耗時(shí)秦士,不加c輸出詳細(xì)信息,-f表示跟蹤其子進(jìn)程。
strace {command}: 跟蹤命令對(duì)文件的操作荔仁,如df -h卡死伍宦,strace df -h
跟蹤命令在哪一步卡死。
strace可以看到系統(tǒng)調(diào)用性能乏梁,但無(wú)法看到調(diào)用棧次洼。可以利用pstack查看進(jìn)程的調(diào)用棧:
pstack {pid}
- top 查看所有進(jìn)程cpu內(nèi)核態(tài)和用戶態(tài)耗時(shí),perf查看cpu總的耗時(shí)
perf top : 查看哪個(gè)系統(tǒng)調(diào)用的時(shí)間最高.
perf top -e {event}; event 是指可追蹤的event事件
對(duì)內(nèi)核參數(shù)的調(diào)用棧進(jìn)行取樣
#perf record -a -g -F 1000 sleep 60
“-g'的意思是按照調(diào)用關(guān)系存儲(chǔ)數(shù)據(jù)遇骑;“-F 1000 sleep 60”表示按照每秒取1000個(gè)樣本的頻率取一分鐘卖毁。
取完樣后,使用perf report -g打開(kāi)取樣的數(shù)據(jù)
event: perf list 列出所有可選擇的event.
perf stat [-e {event}] [{command}] [{options}]: 收集此command運(yùn)行的的一些性能數(shù)據(jù).
perf stat report [-i file]: 生成匯報(bào)數(shù)據(jù).
perf lock {record | report | script | info} : 記錄內(nèi)核鎖的性能分析
perf-kmem: slab分配器的性能分析。
perf kmem {record | stat} [<options>]
perf-probe: 可以自定義探測(cè)點(diǎn)亥啦。
使用例子
-
Display which lines in schedule() can be probed
# perf probe --line schedule #前面有行號(hào)的可以探測(cè)炭剪。
-
Add a probe on schedule() function 12th line.
#perf probe -a schedule:12
在schedule函數(shù)的12處增加一個(gè)探測(cè)點(diǎn)。
2翔脱、ltrace:匯總用戶態(tài)的調(diào)用時(shí)間
ltrace -p PID
3奴拦、trace: strace類(lèi)似
4、lsof:查看文件的占用情況届吁。
Python性能定位工具
由于strace只能進(jìn)行系統(tǒng)調(diào)用的分析错妖。對(duì)于python程序本身一些用戶態(tài)的函數(shù)性能無(wú)法查看【毋澹可以采用pyflame暂氯,生成火焰圖進(jìn)行查看。生產(chǎn)火焰圖的工具FlameGraph亮蛔。
安裝pyflame:
yum install autoconf automake gcc-c++ python-devel python3-devel libtool -y
./autogen.sh
./configure # Plus any options like --prefix.
make
make check # Optional, test the build! Should take < 1 minute.
make install # Optional, install into the configure prefix.
火焰圖工具:
cp flamegraph.pl /usr/bin/
chmod +x /usr/bin/flamegraph.pl
數(shù)據(jù)收集:
#pyflame -p {pid} | flamegraph.pl > myprofile.svg
瀏覽器打開(kāi)myprofile.svg
- CPU負(fù)載查看
# mpstat 3 表示CPU平均負(fù)載痴施,3表示采樣間隔
# mpstat -P 0 3 表示查看具體某個(gè)核的負(fù)載
# top 查看具體哪個(gè)進(jìn)程占用的負(fù)載較高
- IO負(fù)載查看
iostat:
間隔2s,產(chǎn)生6分關(guān)于device的報(bào)告究流。-d/-c: device report/cpu的使用情況; -x: 擴(kuò)展記錄 2: 2s; 6:6個(gè)報(bào)告
# iostat -d -x -p sda 2 6
Linux 3.10.0-862.11.6.1.el7.x86_64 (ceph01) 10/14/2020 _x86_64_ (40 CPU)
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.07 0.00 6.58 0.22 268.69 81.65 0.11 16.22 6.10 16.23 0.39 0.26
sda1 0.00 0.00 0.00 0.00 0.00 0.00 153.13 0.00 3.43 3.42 3.51 2.45 0.00
sda2 0.00 0.07 0.00 6.58 0.21 268.69 81.65 0.11 16.22 6.13 16.23 0.39 0.26
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.00 0.00 9.00 0.00 76.00 16.89 0.00 0.28 0.00 0.28 0.11 0.10
sda1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda2 0.00 0.00 0.00 9.00 0.00 76.00 16.89 0.00 0.28 0.00 0.28 0.11 0.10
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.00 0.00 24.50 0.00 3398.00 277.39 0.34 8.10 0.00 8.10 0.67 1.65
sda1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda2 0.00 0.00 0.00 24.50 0.00 3398.00 277.39 0.37 8.10 0.00 8.10 0.69 1.70
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.00 0.00 46.00 0.00 9076.00 394.61 2.10 48.83 0.00 48.83 1.63 7.50
sda1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda2 0.00 0.00 0.00 46.00 0.00 9076.00 394.61 2.07 48.83 0.00 48.83 1.62 7.45
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 1.50 0.00 22.50 0.00 236.00 20.98 0.05 2.07 0.00 2.07 0.44 1.00
sda1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda2 0.00 1.50 0.00 22.50 0.00 236.00 20.98 0.05 2.07 0.00 2.07 0.44 1.00
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 0.00 0.00 9.50 0.00 62.00 13.05 0.00 0.16 0.00 0.16 0.11 0.10
sda1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sda2 0.00 0.00 0.00 9.50 0.00 62.00 13.05 0.00 0.16 0.00 0.16 0.11 0.10
iotop: 查看IO最高的線程
strace: 跟蹤系統(tǒng)調(diào)用辣吃,查看io時(shí),可以先根據(jù)iotop確定io最高的線程,strace -p {pid}梯嗽,跟蹤此線程的系統(tǒng)調(diào)用
iperf3:
打流測(cè)試:
客戶端:
# iperf3 -c {server_host} -i1 -t60 -p {port}
服務(wù)端:
# iperf3 -s -p {port}
關(guān)注帶寬齿尽,Retr: tcp重傳次數(shù),窗口值