no-image

【原創】如何檢視某程序下執行的執行緒

                                    


研究 Atlas 過程中,需要了解其程序模型(執行緒模型),那麼如何檢視其執行模式呢?

前提:Atlas 配置了 daemon 模式和 keepalive 功能。

[root@Betty conf]# vi modb.cnf 

[mysql-proxy]
...
daemon = true
keepalive = true
...




檢視方式


1. ps 命令檢視


從 man ps 中可以如下說明,可以顯示程序關係 

To print a process tree:
   ps -ejH
   ps axjf

但 ps -ejH 顯示出來的東東比較難看,所以採用第二種方式檢視,

[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

那麼如何顯示執行緒關係呢?有如下說明,

To get info about threads:
   ps -eLf
   ps axms

執行後輸出(採用第一種方式檢視,原因同上)

[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 上,則執行如下命令,

[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]#

關鍵引數說明如下:

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 。

[root@Betty conf]# ps -e |grep mysql-proxy
 3755 ?        00:00:00 mysql-proxy
 3756 ?        00:00:00 mysql-proxy


分別檢視以上兩個程序的樹形關係

[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 的程序包含的執行緒。

[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 後顯示

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 後變化的項為

* J: P          = Last used cpu (SMP)

按回車或空格儲存,退回到 top 介面,顯示如下

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. 到程序對應的目錄下檢視

[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]#

其中

Threads:        5

表示該程序下包含 5 個執行緒(3756 程序對應的執行緒 由其建立的工作執行緒)。



參考:
http://smilejay.com/2012/06/linux_view_threads/

關聯文章