使用 top 检查 Linux 中的 Java 线程
我正在 Linux 中检查 Java 进程,
top -H
但是,我无法读取“COMMAND”列中的线程名称(因为它太长)。如果我使用“c”来扩展进程的全名,那么它仍然太长而无法容纳。
如何获取命令的全名?
I am inspecting a Java process in Linux using
top -H
However, I cannot read the name of the thread in the "COMMAND" column (because it is too long). If I use 'c' to expand the full name of the process, then it is still to long to fit.
How can I obtain the full name of the command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
你提到了“Linux”。那么使用小工具“threadcpu”可能是一个解决方案:
threadcpu_-_show_cpu_usage_of_threads
一些示例输出:
输出故意非常简单,以使进一步处理(例如用于监视)更容易。
You mentioned "Linux". Then using the little tool "threadcpu" might be a solution:
threadcpu_-_show_cpu_usage_of_threads
Some sample outputs:
The output is intentionally very simple to make further processing (e.g. for monitoring) more easy.
您可以使用工具
jstack
检查java线程。它将列出属于指定进程 pid 的所有线程的名称、堆栈跟踪和其他有用信息。编辑:jstack的线程转储中的参数nid是线程的pid列中top显示的LWP的十六进制版本。
You can inspect java threads with the tool
jstack
. It will list the names, stacktraces and other useful information of all threads belonging to the specified process pid.Edit: The parameter nid in the thread dump of jstack is the hex version of the LWP that is displayed by top in the pid column for threads.
这可能有点旧,但这是我将 top 和 jstack 合并在一起所做的事情。我使用了两个脚本,但我确信这一切都可以在一个脚本中完成。
首先,我将 top 的输出以及 java 线程的 pid 保存到一个文件中,并将 jstack 输出保存到另一个文件中:
然后我使用 perl 脚本调用 bash 脚本(此处称为 cpu-java.sh)并进行合并两个文件(/tmp/top.log 和 /tmp/jstack.log):
输出帮助我找出哪些线程占用了我的 cpu:
然后我可以返回到 /tmp/jstack.log 并查看有问题的线程的堆栈跟踪并尝试从那里找出发生了什么。当然,这个解决方案是依赖于平台的,但它应该适用于大多数风格的 *nix 以及一些地方的调整。
This might be a little old, but here's what I did to kinda merge top and jstack together. I used two scripts, but I'm sure it all could be done in one.
First, I save the output of top with the pids for my java threads into a file and save the jstack output into another file:
Then I use a perl script to call the bash script (called cpu-java.sh here) and kinda merge the two files (/tmp/top.log and /tmp/jstack.log):
The output helps me to find out which threads are hogging my cpu:
Then I can go back to /tmp/jstack.log and take a look at the stack trace for the problematic thread and try to figure out what's going on from there. Of course this solution is platform-dependent, but it should work with most flavors of *nix and some tweaking here and there.
我创建了一个类似 top 的命令,专门用于可视化按 CPU 使用情况排序的 Java 线程,并将源代码发布在: https: //github.com/jasta/jprocps。命令行语法并不像 top 那样丰富,但它确实支持一些相同的命令:
示例输出(显示 ant 和 IntelliJ 运行):
从这个输出中,我可以在 jconsole 中提取线程的堆栈跟踪手动 或
jstack
并找出发生了什么。注意:
jtop
是用 Python 编写的,需要安装jstack
。I have created a top-like command specifically for visualizing Java threads ordered by CPU usage and posted the source code at: https://github.com/jasta/jprocps. The command-line syntax is not nearly as rich as top, but it does support some of the same commands:
Sample output (showing ant and IntelliJ running):
From this output, I can pull up the thread's stack trace in
jconsole
orjstack
manually and figure out what's going on.NOTE:
jtop
is written in Python and requires thatjstack
be installed.据我发现 jstack 已过时从 JDK 8 开始。我用来检索所有 Java 线程名称的是:
检查 jcmd 文档 了解更多信息。
As far as I found out jstack is outdated as of JDK 8. What I used to retrieve all Java Thread names is:
Check jcmd documentation for more.
对于 Linux 上的 OpenJDK,JavaThread 名称不适用t 传播到 本机线程,当使用任何工具检查本机线程。
不过,还有一些工作正在进行中:
就我个人而言,我发现OpenJDK开发工具很慢,所以我自己打补丁。
With OpenJDK on Linux, JavaThread names don't propagate to native threads, you cannot see java thread name while inspecting native threads with any tool.
However there is some work in progress:
Personally, I find the OpenJDK development tool slow so I just apply patches myself.
就内核而言,线程没有名称;他们只有身份证号码。 JVM 为线程分配名称,但这是进程内的私有内部数据,“顶级”程序无法访问(并且无论如何也不知道)。
Threads don't have names as far as the kernel is concerned; they only have ID numbers. The JVM assigns names to threads, but that's private internal data within the process, which the "top" program can't access (and doesn't know about anyway).
此 shell 脚本结合了 jstack 和 top 的输出,按 CPU 使用情况列出 Java 线程。它需要一个参数,即拥有进程的帐户用户。
名称: jstack-top.sh
This shell script combines the output from jstack and top to list Java threads by CPU usage. It expects one argument, the account user that owns the processes.
Name: jstack-top.sh
扩展 Andre 之前在 Perl 中的答案,这里是一个运行速度明显更快的 Python 答案。
它重复使用之前创建的文件,并且不会在 jstack 输出上循环多次:
Expanding on Andre's earlier answer in Perl, here is one in Python that runs significantly faster.
It re-uses files created earlier and does not loop several times over the jstack output:
老问题,但我在
top
上遇到了同样的问题。事实证明,您只需使用光标键就可以将 top 的输出滚动到右侧:)
(但不幸的是,不会显示任何线程名称)
Old question, but I had just the same problem with
top
.It turns out, you can scroll top's output to the right simply by using the cursors keys :)
(but unfortunately there won't be any thread name shown)