使用JMX获取线程CPU时间的有效方法 [英] Efficient way of getting thread CPU time using JMX
问题描述
我目前正在以下列方式使用JMX获取总线程CPU时间:
I'm currently getting the total thread CPU time using JMX in the following manner:
private long calculateTotalThreadCpuTime(ThreadMXBean thread) {
long totalTime = 0l;
for (ThreadInfo threadInfo : thread.dumpAllThreads(false, false))
totalTime += thread.getThreadCpuTime(threadInfo.getThreadId());
return totalTime;
}
由于ThreadMXBean实际上是一个远程代理,性能 dreadful ,这个实际方法调用的秒数量级。
As the ThreadMXBean is actually a remote proxy, performance is dreadful, in the order of magnitude of seconds for this actual method call.
有更快的方法吗?
更新:我正在使用它进行性能监控。测量结果是挂钟时间和JProfiler,显示我花费大约85%的时间用于此方法。我确实有一些其他MXBean调用(运行时,内存,GC),但它们便宜得多。很可能因为每次调用 thread.getThreadCpuTime
都是远程调用。
Update: I'm using this for performance monitoring. The measurements were both 'wall clock' time and JProfiler, which showed about 85% of my time spent in this method. I do have some other MXBean calls ( Runtime, Memory, GC ), but they are much cheaper. Most likely because every call to thread.getThreadCpuTime
is a remote one.
更新2 :显示性能问题的JProfiler屏幕截图。
Update 2: JProfiler screenshot showing the performance problems.
推荐答案
优化:
- 在线程池中调用getThreadCPUTime,因为它似乎是网络绑定的;
- 每当找到一个线程在
Thread.STATE.TERMINATED
,将其名称保存在地图中,并在下次跳过查询。
- invoke getThreadCPUTime inside a thread pool, since it seems to be network-bound;
- whenever a thread is found to be in
Thread.STATE.TERMINATED
, keep its name in a Map and skip querying the next time.
这篇关于使用JMX获取线程CPU时间的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!