pstack說(shuō)明
- 在程序運(yùn)行過(guò)程中,如果發(fā)生阻塞或者work狀態(tài)罗心,可以使用該命令顯示進(jìn)程的棧跟蹤拄踪。
- 該命令必須由進(jìn)程的屬主或root運(yùn)行。
- 使用該命令來(lái)確定進(jìn)程掛起的位置。
- 該命令允許使用的唯一選項(xiàng)是要檢查的進(jìn)程的 PID乘客,即運(yùn)行格式為:
pstack pid
狐血。
pstack應(yīng)用
安裝pstree工具
pstree以樹(shù)結(jié)構(gòu)顯示進(jìn)程,centos7上默認(rèn)沒(méi)有安裝psmisc包易核。
# 在 Mac OS 上安裝
brew install pstree
# 在 Fedora/Red Hat/CentOS 安裝
yum -y install psmisc
# 在 Ubuntu/Debian 上安裝
apt-get install psmisc
pstree使用
- 查看某個(gè)服務(wù)的線(xiàn)程信息
pstree -p work | grep redis
work為工作用戶(hù)匈织,-p為顯示進(jìn)程識(shí)別碼,如下圖所示:redis共啟動(dòng)了3個(gè)子線(xiàn)程牡直,加上主線(xiàn)程共4個(gè)線(xiàn)程缀匕。
pstree -p redis | grep redis
redis-server(1190)-+-{redis-server}(1191)
|-{redis-server}(1192)
`-{redis-server}(1193)
- 查看某個(gè)進(jìn)程的所有線(xiàn)程
[root@jolay ~]# ps -Lf 1190
UID PID PPID LWP C NLWP STIME TTY STAT TIME CMD
redis 1190 1 1190 1 4 May20 ? Ssl 174:44 /home/q/redis/redis10010/bin/redis-server *:10010
redis 1190 1 1191 0 4 May20 ? Ssl 0:00 /home/q/redis/redis10010/bin/redis-server *:10010
redis 1190 1 1192 0 4 May20 ? Ssl 0:00 /home/q/redis/redis10010/bin/redis-server *:10010
redis 1190 1 1193 0 4 May20 ? Ssl 0:00 /home/q/redis/redis10010/bin/redis-server *:10010
pstack使用
- 查看某個(gè)進(jìn)程的pstack信息
[root@jolay ~]# pstack 1190
Thread 4 (Thread 0x7f836dfff700 (LWP 1191)):
#0 0x00007f836ed85965 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x0000000000485b26 in bioProcessBackgroundJobs (arg=0x0) at bio.c:176
#2 0x00007f836ed81dd5 in start_thread () from /lib64/libpthread.so.0
#3 0x00007f836eaaaead in clone () from /lib64/libc.so.6
Thread 3 (Thread 0x7f836d7fe700 (LWP 1192)):
#0 0x00007f836ed85965 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x0000000000485b26 in bioProcessBackgroundJobs (arg=0x1) at bio.c:176
#2 0x00007f836ed81dd5 in start_thread () from /lib64/libpthread.so.0
#3 0x00007f836eaaaead in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f836cffd700 (LWP 1193)):
#0 0x00007f836ed85965 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x0000000000485b26 in bioProcessBackgroundJobs (arg=0x2) at bio.c:176
#2 0x00007f836ed81dd5 in start_thread () from /lib64/libpthread.so.0
#3 0x00007f836eaaaead in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f836f8b6f80 (LWP 1190)):
#0 0x00007f836eaab483 in epoll_wait () from /lib64/libc.so.6
#1 0x000000000042908e in aeApiPoll (tvp=<optimized out>, eventLoop=0x7f836e44c0a0) at ae_epoll.c:112
#2 aeProcessEvents (eventLoop=0x7f836e44c0a0, flags=11) at ae.c:411
#3 0x000000000042952b in aeMain (eventLoop=0x7f836e44c0a0) at ae.c:501
#4 0x000000000043184f in main (argc=<optimized out>, argv=<optimized out>) at server.c:4603
- 格式化查看pstack信息
- 生成腳本
(
cat <<EOF
#!/usr/bin/python3
import sys
import subprocess
import re
if len(sys.argv) != 2:
print('Please input a PID.')
exit(1)
status, out = subprocess.getstatusoutput('pstack {pid}'.format(pid=sys.argv[1]))
if status != 0:
print('Please install pstack.')
exit(1)
lst = re.split('Thread \d+.*:(.*)', out)
result = {}
for item in lst:
item = item.strip()
if item != '':
if item in result:
result[item] += 1
else:
result[item] = 1
result = sorted(result.items(), key=lambda e:e[1], reverse=True)
for item in result:
print('Count: ', item[1])
print(item[0])
print('-----------------------------------------------')
EOF
) > ./pstack.py
chmod +x pstack.py
- 執(zhí)行腳本
[root@jolay ~]# ./pstack.py 1190
Count: 1
#0 0x00007f836ed85965 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x0000000000485b26 in bioProcessBackgroundJobs (arg=0x1) at bio.c:176
#2 0x00007f836ed81dd5 in start_thread () from /lib64/libpthread.so.0
#3 0x00007f836eaaaead in clone () from /lib64/libc.so.6
-----------------------------------------------
Count: 1
#0 0x00007f836eaab483 in epoll_wait () from /lib64/libc.so.6
#1 0x000000000042908e in aeApiPoll (tvp=<optimized out>, eventLoop=0x7f836e44c0a0) at ae_epoll.c:112
#2 aeProcessEvents (eventLoop=0x7f836e44c0a0, flags=11) at ae.c:411
#3 0x000000000042952b in aeMain (eventLoop=0x7f836e44c0a0) at ae.c:501
#4 0x000000000043184f in main (argc=<optimized out>, argv=<optimized out>) at server.c:4603
-----------------------------------------------
Count: 1
#0 0x00007f836ed85965 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x0000000000485b26 in bioProcessBackgroundJobs (arg=0x2) at bio.c:176
#2 0x00007f836ed81dd5 in start_thread () from /lib64/libpthread.so.0
#3 0x00007f836eaaaead in clone () from /lib64/libc.so.6
-----------------------------------------------
Count: 1
#0 0x00007f836ed85965 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x0000000000485b26 in bioProcessBackgroundJobs (arg=0x0) at bio.c:176
#2 0x00007f836ed81dd5 in start_thread () from /lib64/libpthread.so.0
#3 0x00007f836eaaaead in clone () from /lib64/libc.so.6
-----------------------------------------------