pstack说明
- 在程序运行过程中,如果发生阻塞或者work状态,可以使用该命令显示进程的栈跟踪。
- 该命令必须由进程的属主或root运行。
- 使用该命令来确定进程挂起的位置。
- 该命令允许使用的唯一选项是要检查的进程的 pid,即运行格式为:
pstack pid
。
此命令可显示每个进程的栈跟踪。pstack 命令必须由相应进程的属主或 root 运行。可以使用 pstack 来确定进程挂起的位置。此命令允许使用的唯一选项是要检查的进程的 pid。请参见 proc(1) 手册页。
这个命令在排查进程问题时非常有用,比如我们发现一个服务一直处于work状态(如假死状态,好似死循环),使用这个命令就能轻松定位问题所在;可以在一段时间内,多执行几次pstack,若发现代码栈总是停在同一个位置,那个位置就需要重点关注,很可能就是出问题的地方;
pstree使用
- 查看某个服务的线程信息
pstree -p work | grep redis
work为工作用户,-p为显示进程识别码,如下图所示:redis共启动了3个子线程,加上主线程共4个线程。
pstree -p redis | grep redis
redis-server(1190)- -{redis-server}(1191)
|-{redis-server}(1192)
`-{redis-server}(1193)
- 查看某个进程的所有线程
[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使用
- 查看某个进程的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=, 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=, argv=) at server.c:4603
- 格式化查看pstack信息
- 生成脚本
(
cat < ./pstack.py
chmod x pstack.py
- 执行脚本
[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=, 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=, argv=) 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
-----------------------------------------------