How can I monitor the thread count of a process on linux?

前端 未结 17 916
暖寄归人
暖寄归人 2020-12-02 03:53

I would like to monitor the number of threads used by a specific process on Linux. Is there an easy way to get this information without impacting the performance of the proc

相关标签:
17条回答
  • 2020-12-02 04:45

    $ ps H p pid-id

    H - Lists all the individual threads in a process

    or

    $cat /proc/pid-id/status

    pid-id is the Process ID

    eg.. (Truncated the below output)

    root@abc:~# cat /proc/8443/status
    Name:   abcdd
    State:  S (sleeping)
    Tgid:   8443
    VmSwap:        0 kB
    Threads:    4
    SigQ:   0/256556
    SigPnd: 0000000000000000
    
    0 讨论(0)
  • 2020-12-02 04:47

    If you're interested in those threads which are really active -- as in doing something (not blocked, not timed_waiting, not reporting "thread running" but really waiting for a stream to give data) as opposed to sitting around idle but live -- then you might be interested in jstack-active.

    This simple bash script runs jstack then filters out all the threads which by heuristics seem to be idling, showing you stack traces for those threads which are actually consuming CPU cycles.

    0 讨论(0)
  • 2020-12-02 04:50

    To get the number of threads for a given pid:

    $ ps -o nlwp <pid>
    

    Where nlwp stands for Number of Light Weight Processes (threads). Thus ps aliases nlwp to thcount, which means that

    $ ps -o thcount <pid>
    

    does also work.

    If you want to monitor the thread count, simply use watch:

    $ watch ps -o thcount <pid>
    

    To get the sum of all threads running in the system:

    $ ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'
    
    0 讨论(0)
  • 2020-12-02 04:51

    jvmtop can show the current jvm thread count beside other metrics.

    0 讨论(0)
  • 2020-12-02 04:52
    cat /proc/<PROCESS_PID>/status | grep Threads
    
    0 讨论(0)
提交回复
热议问题