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

本小妞迷上赌 提交于 2019-11-27 04:09:08

问题


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 process?


回答1:


try

ps huH p <PID_OF_U_PROCESS> | wc -l

or htop




回答2:


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 }'



回答3:


Each thread in a process creates a directory under /proc/<pid>/task. Count the number of directories, and you have the number of threads.




回答4:


cat /proc/<PROCESS_PID>/status | grep Threads



回答5:


ps -eLf on the shell shall give you a list of all the threads and processes currently running on the system. Or, you can run top command then hit 'H' to toggle thread listings.




回答6:


JStack is quite inexpensive - one option would be to pipe the output through grep to find active threads and then pipe through wc -l.

More graphically is JConsole, which displays the thread count for a given process.




回答7:


If you use:

ps uH p <PID_OF_U_PROCESS> | wc -l

You have to subtract 1 to the result, as one of the lines "wc" is counting is the headers of the "ps" command.




回答8:


Here is one command that displays the number of threads of a given process :

ps -L -o pid= -p <pid> | wc -l

Unlike the other ps based answers, there is here no need to substract 1 from its output as there is no ps header line thanks to the -o pid=option.




回答9:


$ 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



回答10:


Newer JDK distributions ship with JConsole and VisualVM. Both are fantastic tools for getting the dirty details from a running Java process. If you have to do this programmatically, investigate JMX.




回答11:


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




回答12:


If you are trying to find out the number of threads using cpu for a given pid I would use:

top -bc -H -n2 -p <pid> | awk '{if ($9 != "0.0" && $1 ~ /^[0-9]+$/) print $1 }' | sort -u | wc -l



回答13:


The easiest way is using "htop". You can install "htop" (a fancier version of top) which will show you all your cores, process and memory usage.

Press "Shift+H" to show all process or press again to hide it. Press "F4" key to search your process name.

Installing on Ubuntu or Debian:

sudo apt-get install htop

Installing on Redhat or CentOS:

yum install htop
dnf install htop      [On Fedora 22+ releases]

If you want to compile "htop" from source code, you will find it here.




回答14:


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.




回答15:


VisualVM can show clear states of threads of a given JVM process



来源:https://stackoverflow.com/questions/268680/how-can-i-monitor-the-thread-count-of-a-process-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!