如何在 linux 上监视进程的线程数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/268680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:42:20  来源:igfitidea点击:

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

linuxmultithreadingmonitor

提问by

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?

我想监视 Linux 上特定进程使用的线程数。有没有一种简单的方法可以在不影响流程性能的情况下获取这些信息?

回答by rhys keepence

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

JStack 非常便宜 - 一种选择是通过 grep 管道输出以查找活动线程,然后通过 wc -l 管道。

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

更图形化的是 JConsole,它显示给定进程的线程数。

回答by basszero

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.

较新的 JDK 发行版附带 JConsole 和 VisualVM。两者都是从正在运行的 Java 进程中获取脏细节的绝佳工具。如果您必须以编程方式执行此操作,请研究 JMX。

回答by slav0nic

try

尝试

ps huH p <PID_OF_U_PROCESS> | wc -l

or htop

htop

回答by flexo

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.

您必须将结果减去 1,因为“wc”正在计算的行之一是“ps”命令的标题。

回答by bdonlan

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

进程中的每个线程在/proc/<pid>/task. 计算目录的数量,你就有了线程的数量。

回答by MRalwasser

jvmtopcan show the current jvm thread count beside other metrics.

jvmtop可以在其他指标旁边显示当前的 jvm 线程数。

回答by jlliagre

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 psbased answers, there is here no need to substract 1from its output as there is no psheader line thanks to the -o pid=option.

与其他ps基于答案的答案不同,这里不需要1从其输出中减去,ps因为该-o pid=选项没有标题行。

回答by jlliagre

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

ps -eLf在 shell 上会给你一个当前在系统上运行的所有线程和进程的列表。或者,您可以运行top命令然后点击“H”来切换线程列表。

回答by PbxMan

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

回答by Partly Cloudy

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.

如果你对那些真正活跃的线程感兴趣——比如在做某事(不阻塞,不定时等待,不报告“线程正在运行”但真正等待流提供数据)而不是闲置但活跃—— - 那么你可能对jstack-active感兴趣。

This simple bash script runs jstackthen 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.

这个简单的 bash 脚本运行jstack然后过滤掉所有通过启发式似乎空闲的线程,向您显示那些实际消耗 CPU 周期的线程的堆栈跟踪。