Support Wikipedia Follow My Heart: Why VisualVM 1.3.3 cannot do CPU Time Sampling

2011年11月28日星期一

Why VisualVM 1.3.3 cannot do CPU Time Sampling

Today I installed VisualVM 1.3.3 (the newest released version from visualvm.java.net). However, it appears that CPU Sampling Function is automatically disabled.

When I debug the VisualVM 1.3.3 through NetBeans, it appears the following exception information.


I searched the resource code and find that :



  1. in VisualVM 1.3.3, three new source files are added in plugin folder “Sample/CPU”. It appears that some detailed CPU usage information is available in the 1.3.3 version.

  2. in the new-added source “ThreadsCPU.java”, line 55. It writes that method “getThreadCpuTime” of ThreadMXBean is called, and expected return type is “long[]”. In contrast, in VisualVM 1.3.2 and former version, there is no such new code.

Interestingly, I found that there is “//NOI18N” comment in the end of the code like follows:




So let’s have a look at the javaDoc of JDK itself.


If we open the javadoc of OpenJDK, we can easily find that there is no such method in ThreadMXBean:


but if we open Sun Jdk (newer than 6u25), we can find the following hint:


So if users’ JDK is OpenJDK, or older version JDK (< 6u25), the “NoSuchMethodException” will definitely occured!

 

But the problem is WHY THE WHOLE CPU SAMPLING FUNCTION IS DISABLED ?

Let’s look at where the ThreadsCPU.java is invoked. It is in SampleImpl.java, line 551. Here we can easily find that the try-catch structure is so strong that normal logics are broken down.


try {
tcpu.getThreadsCPUInfo();
} catch (Exception ex) {
tcpu = null;

throw new RuntimeException(ex);

}



So let’s make some tiny change to it, as follows:





OK, we are DONE!


Now if users’ JDK does not support new getThreadCpuTime method, users can AT LEAST use functions same with VisualVM (Version 1.3.2).

没有评论:

发表评论