【原创】如何查看某进程下运行的线程

研究 Atlas 过程中,需要了解其进程模型(线程模型),那么如何查看其运行模式呢? 

前提:Atlas 配置了 daemon 模式和 keepalive 功能。 
1
2
3
4
5
6
7
[root@Betty conf]# vi modb.cnf
 
[mysql-proxy]
...
daemon = true
keepalive = true
...

查看方式 
1. ps 命令查看 
从 man ps 中可以如下说明,可以显示进程关系 
1
2
3
To print a process tree:
   ps -ejH
   ps axjf
但 ps -ejH 显示出来的东东比较难看,所以采用第二种方式查看, 
1
2
3
[root@Betty conf]# ps axjf|grep mysql-proxy|grep -v grep
    1  3755  3754  3754 ?           -1 S        0   0:00 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
 3755  3756  3754  3754 ?           -1 Sl       0   0:00  \_ mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
那么如何显示线程关系呢?有如下说明, 
1
2
3
To get info about threads:
   ps -eLf
   ps axms
执行后输出(采用第一种方式查看,原因同上) 
1
2
3
4
5
6
7
8
9
[root@Betty conf]# ps -eLf|grep mysql-proxy
UID        PID  PPID   LWP  C NLWP STIME TTY          TIME CMD
root      3755     1  3755  0    1 15:14 ?        00:00:00 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      3756  3755  3756  0    5 15:14 ?        00:00:00 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      3756  3755  3757  0    5 15:14 ?        00:00:00 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      3756  3755  3758  0    5 15:14 ?        00:00:00 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      3756  3755  3759  0    5 15:14 ?        00:00:00 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      3756  3755  3760  0    5 15:14 ?        00:00:00 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      4154 18711  4154  0    1 16:32 pts/5    00:00:00 grep mysql-proxy
其中 
LWP -- 轻量级进程,即线程,这里显示的是 thread id。 
NLWP -- 线程数,即 number of threads in process。 

如果还想要查看线程究竟运行在哪个 CPU 上,则执行如下命令, 
1
2
3
4
5
6
7
8
9
10
[root@Betty conf]# ps -eo ruser,pid,ppid,lwp,psr,args -L|grep mysql-proxy
RUSER      PID  PPID   LWP PSR COMMAND
root      3755     1  3755   2 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      3756  3755  3756   1 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      3756  3755  3757   0 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      3756  3755  3758   3 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      3756  3755  3759   1 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      3756  3755  3760   1 mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/modb.cnf
root      4162 18711  4162   2 grep mysql-proxy
[root@Betty conf]#
关键参数说明如下: 
1
2
3
4
5
6
7
8
To see every process with a user-defined format:
   ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm
   ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
...
-L              Show threads, possibly with LWP and NLWP columns
...
psr        PSR      processor that process is currently assigned to.
...

2. pstree 命令查看 
查看 mysql-proxy 对应的进程 id 。 
1
2
3
[root@Betty conf]# ps -e |grep mysql-proxy
 3755 ?        00:00:00 mysql-proxy
 3756 ?        00:00:00 mysql-proxy
分别查看以上两个进程的树形关系 
1
2
3
4
5
6
7
8
9
[root@Betty conf]# pstree 3755
mysql-proxy---mysql-proxy---4*[{mysql-proxy}]
 
[root@Betty conf]# pstree -p 3756
mysql-proxy(3756)-+-{mysql-proxy}(3757)
                  |-{mysql-proxy}(3758)
                  |-{mysql-proxy}(3759)
                  `-{mysql-proxy}(3760)
[root@Betty conf]#

3. top 命令查看 
查看 id 为 3756 的进程包含的线程。 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@Betty conf]# top -Hp 3756
top - 17:32:19 up  8:26,  6 users,  load average: 0.00, 0.00, 0.00
Tasks:   5 total,   0 running,   5 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.1%ni, 99.7%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   3941948k total,   897296k used,  3044652k free,   116232k buffers
Swap:  5996536k total,        0k used,  5996536k free,   516676k cached
 
 
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                       
 3756 root      15   0  195m 3752 1804 S  0.0  0.1   0:00.00 mysql-proxy
 3757 root      15   0  195m 3752 1804 S  0.0  0.1   0:00.00 mysql-proxy
 3758 root      15   0  195m 3752 1804 S  0.0  0.1   0:00.00 mysql-proxy
 3759 root      15   0  195m 3752 1804 S  0.0  0.1   0:00.00 mysql-proxy
 3760 root      15   0  195m 3752 1804 S  0.0  0.1   0:00.00 mysql-proxy
按 f 后显示 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Current Fields:  AEHIOQTWKNMbcdfgjplrsuvyzX  for window 1:Def
Toggle fields via field letter, type any other key to return
 
* A: PID        = Process Id
* E: USER       = User Name
* H: PR         = Priority
* I: NI         = Nice value
* O: VIRT       = Virtual Image (kb)
* Q: RES        = Resident size (kb)
* T: SHR        = Shared Mem size (kb)
* W: S          = Process Status
* K: %CPU       = CPU usage
* N: %MEM       = Memory usage (RES)
* M: TIME+      = CPU Time, hundredths
  b: PPID       = Parent Process Pid
  c: RUSER      = Real user name
  d: UID        = User Id
  f: GROUP      = Group Name
  g: TTY        = Controlling Tty
  j: P          = Last used cpu (SMP)
  p: SWAP       = Swapped size (kb)
  l: TIME       = CPU Time
  r: CODE       = Code size (kb)
  s: DATA       = Data+Stack size (kb)
  u: nFLT       = Page Fault count
  v: nDRT       = Dirty Pages count
  y: WCHAN      = Sleeping in Function
  z: Flags      = Task Flags <sched.h>
* X: COMMAND    = Command name/line
 
Flags field:
  0x00000001  PF_ALIGNWARN
  0x00000002  PF_STARTING
  0x00000004  PF_EXITING
  0x00000040  PF_FORKNOEXEC
  0x00000100  PF_SUPERPRIV
  0x00000200  PF_DUMPCORE
  0x00000400  PF_SIGNALED
  0x00000800  PF_MEMALLOC
  0x00002000  PF_FREE_PAGES (2.5)
  0x00008000  debug flag (2.5)
  0x00024000  special threads (2.5)
  0x001D0000  special states (2.5)
  0x00100000  PF_USEDFPU (thru 2.4)
按 j 后变化的项为 
1
* J: P          = Last used cpu (SMP)
按回车或空格保存,退回到 top 界面,显示如下 
1
2
3
4
5
6
7
8
9
10
11
12
top - 17:34:07 up  8:27,  6 users,  load average: 0.00, 0.00, 0.00
Tasks:   5 total,   0 running,   5 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   3941948k total,   897172k used,  3044776k free,   116264k buffers
Swap:  5996536k total,        0k used,  5996536k free,   516676k cached
 
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  P COMMAND                                                                    
 3756 root      15   0  195m 3752 1804 S  0.0  0.1   0:00.00 1 mysql-proxy                                                                
 3757 root      15   0  195m 3752 1804 S  0.0  0.1   0:00.00 0 mysql-proxy                                                                
 3758 root      15   0  195m 3752 1804 S  0.0  0.1   0:00.00 1 mysql-proxy                                                                
 3759 root      15   0  195m 3752 1804 S  0.0  0.1   0:00.00 1 mysql-proxy                                                                
 3760 root      15   0  195m 3752 1804 S  0.0  0.1   0:00.00 1 mysql-proxy
在 top 的显示中会多出 P 这一列是最近一次运行该线程(进程)的CPU 。 

4. 到进程对应的目录下查看 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@Betty conf]# cat /proc/3756/status
Name:   mysql-proxy
State:  S (sleeping)
SleepAVG:       98%
Tgid:   3756
Pid:    3756
PPid:   3755
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
FDSize: 64
Groups: 0 1 2 3 4 6 10
VmPeak:   252512 kB
VmSize:   200244 kB
VmLck:         0 kB
VmHWM:      3752 kB
VmRSS:      3752 kB
VmData:   118404 kB
VmStk:        88 kB
VmExe:        16 kB
VmLib:     10888 kB
VmPTE:       288 kB
StaBrk: 0df5c000 kB
Brk:    0e087000 kB
StaStk: 7fffb0fea000 kB
Threads:        5
SigQ:   1/38784
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000381000
SigCgt: 0000000180004a03
CapInh: 0000000000000000
CapPrm: 00000000fffffeff
CapEff: 00000000fffffeff
Cpus_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,0000ffff
Mems_allowed:   00000000,00000001
[root@Betty conf]#
其中 
1
Threads:        5
表示该进程下包含 5 个线程(3756 进程对应的线程 + 由其创建的工作线程)。
上一篇:在Spring2.5中定制自己的属性编辑器


下一篇:揭秘 DockerCon 重量级演讲嘉宾(四)