如何查看当前进程有多少个存活线程呢?可以使用系统自动的top查看也可以使用其他的专门工具查看。下面以zookeeper的进程做例子查看线程(ID 5806)

  • 方法1:
    pstree -p 5806(进程ID)。如下截图所示:
[root@k8s-m1 ~]# ps -ef | grep zook
root      2651  2618  0 14:35 pts/2    00:00:00 grep --color=auto zook
root      5806     1  0  2019 ?        1-04:03:42 java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /opt/zookeeper/zookeeper-3.4.6/bin/../build/classes:/opt/zookeeper/zookeeper-3.4.6/bin/../build/lib/*.jar:/opt/zookeeper/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/zookeeper/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/opt/zookeeper/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/opt/zookeeper/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/opt/zookeeper/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/opt/zookeeper/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/opt/zookeeper/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/opt/zookeeper/zookeeper-3.4.6/bin/../conf: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /opt/zookeeper/zookeeper-3.4.6/bin/../conf/zoo.cfg
[root@k8s-m1 ~]# 
[root@k8s-m1 ~]# 
[root@k8s-m1 ~]# pstree -p 5806
java(5806)─┬─{java}(5809)
           ├─{java}(5810)
           ├─{java}(5811)
           ├─{java}(5812)
           ├─{java}(5813)
           ├─{java}(5814)
           ├─{java}(5815)
           ├─{java}(5816)
           ├─{java}(5817)
           ├─{java}(5818)
           ├─{java}(5829)
           ├─{java}(5830)
           ├─{java}(5831)
           ├─{java}(5832)
           ├─{java}(5834)
           ├─{java}(5835)
           └─{java}(5836)
  • 方法2:查看进程的系统文件(进程ID 5806)
[root@k8s-m1 ~]# cat /proc/5806/status  
Name:	java
State:	S (sleeping)
Tgid:	5806
Ngid:	0
Pid:	5806
PPid:	1
TracerPid:	0
Uid:	0	0	0	0
Gid:	0	0	0	0
FDSize:	256
Groups:	0 
VmPeak:	 2138728 kB
VmSize:	 2137972 kB
VmLck:	       0 kB
VmPin:	       0 kB
VmHWM:	  314960 kB
VmRSS:	   81196 kB
VmData:	 1946632 kB
VmStk:	     136 kB
VmExe:	       4 kB
VmLib:	   21068 kB
VmPTE:	     964 kB
VmSwap:	   40680 kB
Threads:	18    
SigQ:	0/7288
SigPnd:	0000000000000000
ShdPnd:	0000000000000000
SigBlk:	0000000000000000
SigIgn:	0000000000000003
SigCgt:	2000000181005ccc
CapInh:	0000000000000000
CapPrm:	0000001fffffffff
CapEff:	0000001fffffffff
CapBnd:	0000001fffffffff
Seccomp:	0
Cpus_allowed:	3
Cpus_allowed_list:	0-1
Mems_allowed:	00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
Mems_allowed_list:	0
voluntary_ctxt_switches:	56
nonvoluntary_ctxt_switches:	34

Threads: 18 就是是该进程的线程数

  • 方法3:top -p 5806(进程ID),然后按H。
Threads:  18 total,   0 running,  18 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.0 us,  1.2 sy,  0.0 ni, 95.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.2 st
KiB Mem :  1884000 total,   115544 free,   631080 used,  1137376 buff/cache
KiB Swap:  2097148 total,  1955724 free,   141424 used.  1007888 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                                                                      
 5806 root      20   0 2137972  81196   5132 S  0.0  4.3   0:00.01 java                                                                                                                                         
 5809 root      20   0 2137972  81196   5132 S  0.0  4.3   0:01.39 java                                                                                                                                         
 5810 root      20   0 2137972  81196   5132 S  0.0  4.3   4:30.45 java                                                                                                                                         
 5811 root      20   0 2137972  81196   5132 S  0.0  4.3   4:30.16 java                                                                                                                                         
 5812 root      20   0 2137972  81196   5132 S  0.0  4.3  21:35.39 java                                                                                                                                         
 5813 root      20   0 2137972  81196   5132 S  0.0  4.3   0:00.09 java                                                                                                                                         
 5814 root      20   0 2137972  81196   5132 S  0.0  4.3   0:00.02 java                                                                                                                                         
 5815 root      20   0 2137972  81196   5132 S  0.0  4.3   0:00.00 java                                                                                                                                         
 5816 root      20   0 2137972  81196   5132 S  0.0  4.3   0:05.06 java                                                                                                                                         
 5817 root      20   0 2137972  81196   5132 S  0.0  4.3   0:05.79 java                                                                                                                                         
 5818 root      20   0 2137972  81196   5132 S  0.0  4.3   0:00.00 java                                                                                                                                         
 5829 root      20   0 2137972  81196   5132 S  0.0  4.3   0:00.00 java                                                                                                                                         
 5830 root      20   0 2137972  81196   5132 S  0.0  4.3 399:34.59 java                                                                                                                                         
 5831 root      20   0 2137972  81196   5132 S  0.0  4.3   0:02.42 java                                                                                                                                         
 5832 root      20   0 2137972  81196   5132 S  0.0  4.3 505:20.46 java                                                                                                                                         
 5834 root      20   0 2137972  81196   5132 S  0.0  4.3  13:30.68 java                                                                                                                                         
 5835 root      20   0 2137972  81196   5132 S  0.0  4.3 576:26.72 java                                                                                                                                         
 5836 root      20   0 2137972  81196   5132 S  0.0  4.3 157:57.50 java                                             
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐