Wednesday, June 6, 2012

Thread count in a linux process

Have you ever tried to see how many threads a process has ?
You might have used GDB to figure this out. Is it possible to get number of threads a process has without using GDB ?

Yes, It is possible. Lets see two ways of figuring this out without using GDB.




1. Using 'ps -eLo pid,cmd,nlwp' command.


$  ps -eLo pid,cmd,nlwp
  PID CMD                         NLWP
10440 ps -eLo pid,cmd,nlwp           1
20026 /sbin/rsyslogd -f /etc/rsys    4
32595 java -jar slave.jar           12

The 3rd column, shows number of threads of the process with id shown in first column.


2. Using 'Proc' filesystem.

status file in proc psuedo file system provide complete details of the process. To know more about proc file system refer the article Power of Proc-8: Details of a process current status
Identify process Id for which you want to find the number of threads.

Lets take the above example. Choose process id 32595.

Run the following command.

$  cat /proc/32595/status
Name:   java
State:  S (sleeping)
Tgid:   32595
Pid:    32595
PPid:   32590
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
Utrace: 0
FDSize: 32
Groups: 0 1 2 3 4 6 10
VmPeak:   422900 kB
VmSize:   418064 kB
VmLck:         0 kB
VmHWM:     54932 kB
VmRSS:     45704 kB
VmData:   392060 kB
VmStk:        88 kB
VmExe:        40 kB
VmLib:     17372 kB
VmPTE:       188 kB
VmSwap:        0 kB
Threads:        12
SigQ:   0/7959
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000000
SigCgt: 1000000181005ccf
CapInh: 0000000000000000
CapPrm: ffffffffffffffff
CapEff: ffffffffffffffff
CapBnd: ffffffffffffffff
Cpus_allowed:   1
Cpus_allowed_list:      0
Mems_allowed:   1
Mems_allowed_list:      0
voluntary_ctxt_switches:        1
nonvoluntary_ctxt_switches:     2

Hope you like it. please share this tip.


Pin It
Related Posts Plugin for WordPress, Blogger...