在生产中进行线程转储 [英] Taking thread dumps in production

查看:113
本文介绍了在生产中进行线程转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在分析获取线程转储的方法之间的差异。以下是我正在研究的几个人

I am analyzing the differences between approaches for taking thread dumps. Below are the couple of them I am researching on


  1. 定义一个jmx bean,点击通过Runtime.exec()触发jstack声明的bean操作。

  1. Defining a jmx bean which triggers jstack through Runtime.exec() on clicking a declared bean operation.

守护程序线程在预定义的时间间隔后重复执行ManagementFactory.getThreadMXBean()。dumpAllThreads(true,true)。

Daemon thread executing "ManagementFactory.getThreadMXBean().dumpAllThreads(true, true)" repeatedly after a predefined interval.

比较两者之间的线程转储输出,我看到方法2的以下缺点

Comparing the thread dump outputs between the two, I see the below disadvantages with approach 2


  1. 使用方法2记录的线程转储无法通过开源线程转储分析器(如TDA)进行解析

  2. 输出不包括本机线程ID可能对分析高cpu问题很有用(对吗?)

  3. 还有吗?

I我希望得到建议/输入

I would appreciate to get suggestions/inputs on


  1. 在生产代码中通过Runtime.exec()执行jstack是否有任何缺点?各种操作系统上的兼容性问题 - windows,linux?

  1. Are there any disadvantages of executing jstack through Runtime.exec() in production code? any compatibility issues on various operating systems - windows, linux?

采取线程转储的其他方法是什么?

Any other approach to take thread dumps?

谢谢。

编辑 -

1和2的组合方法似乎是要走的路。我们可以在后台运行专用线程,并以线程转储分析器可以理解的格式在日志文件中打印线程转储。
如果需要任何额外的信息(比如说可能是本机线程id)只能由jstack输出记录,我们会根据需要手动执行。

A combined approach of 1 and 2 seems to be the way to go. We can have a dedicated thread running in background and printing the thread dumps in the log file in a format understood by the thread dump analyzers. If any extra information is need (like say probably the native thread id) which is logged only by the jstack output, we do it manually as required.

推荐答案

您可以使用

jstack {pid} > stack-trace.log

在进程运行的框中以用户身份运行。

running as the user on the box where the process is running.

如果多次运行,可以使用 diff 来更轻松地查看哪些线程处于活动状态。

If you run this multiple times you can use a diff to see which threads are active more easily.

为了分析堆栈跟踪,我在专用线程中定期使用以下采样。<​​/ p>

For analysing the stack traces I use the following sampled periodically in a dedicated thread.

 Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();

使用此信息可以获取线程的id,运行状态并比较堆栈跟踪。

Using this information you can obtain the thread's id, run state and compare the stack traces.

这篇关于在生产中进行线程转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆