下面列出了java.lang.management.CompilationMXBean#isCompilationTimeMonitoringSupported ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void sense(final MetricContext metricContext)
{
CompilationMXBean mxBean = ManagementFactory.getCompilationMXBean();
// Compilation time may not be supported on some platforms, skip if so.
if (!mxBean.isCompilationTimeMonitoringSupported()) {
return;
}
long total = mxBean.getTotalCompilationTime();
metricContext.record(TOTAL_COMPILATION_TIME, total, Unit.MILLISECOND);
metricContext.record(COMPILATION_TIME, total - prevTotal, Unit.MILLISECOND);
this.prevTotal = total;
}
private final int doCompilationMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- CompilationMXBean") ;
try {
ObjectName compilationName =
new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);
if ( mbsc.isRegistered(compilationName) ) {
MBeanInfo mbInfo = mbsc.getMBeanInfo(compilationName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
CompilationMXBean compilation = null ;
compilation =
JMX.newMXBeanProxy(mbsc,
compilationName,
CompilationMXBean.class) ;
System.out.println("getName\t\t"
+ compilation.getName());
boolean supported =
compilation.isCompilationTimeMonitoringSupported() ;
System.out.println("isCompilationTimeMonitoringSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("getTotalCompilationTime\t\t"
+ compilation.getTotalCompilationTime());
}
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private final int doCompilationMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- CompilationMXBean") ;
try {
ObjectName compilationName =
new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);
if ( mbsc.isRegistered(compilationName) ) {
MBeanInfo mbInfo = mbsc.getMBeanInfo(compilationName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
CompilationMXBean compilation = null ;
compilation =
JMX.newMXBeanProxy(mbsc,
compilationName,
CompilationMXBean.class) ;
System.out.println("getName\t\t"
+ compilation.getName());
boolean supported =
compilation.isCompilationTimeMonitoringSupported() ;
System.out.println("isCompilationTimeMonitoringSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("getTotalCompilationTime\t\t"
+ compilation.getTotalCompilationTime());
}
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
/**
* Returns the total time of asynchronous JIT compilation in milliseconds.
*
* @return JIT compile time
*/
public static long getJITCompileTime(){
long ret = -1; //unsupported
CompilationMXBean cmx = ManagementFactory.getCompilationMXBean();
if( cmx.isCompilationTimeMonitoringSupported() )
{
ret = cmx.getTotalCompilationTime();
ret += jitCompileTime; //add from remote processes
}
return ret;
}
private final int doCompilationMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- CompilationMXBean") ;
try {
ObjectName compilationName =
new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);
if ( mbsc.isRegistered(compilationName) ) {
MBeanInfo mbInfo = mbsc.getMBeanInfo(compilationName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
CompilationMXBean compilation = null ;
compilation =
JMX.newMXBeanProxy(mbsc,
compilationName,
CompilationMXBean.class) ;
System.out.println("getName\t\t"
+ compilation.getName());
boolean supported =
compilation.isCompilationTimeMonitoringSupported() ;
System.out.println("isCompilationTimeMonitoringSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("getTotalCompilationTime\t\t"
+ compilation.getTotalCompilationTime());
}
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
@Override
public void bindTo(MeterRegistry registry) {
CompilationMXBean compilationBean = ManagementFactory.getCompilationMXBean();
if (compilationBean != null && compilationBean.isCompilationTimeMonitoringSupported()) {
FunctionCounter.builder("jvm.compilation.time", compilationBean, CompilationMXBean::getTotalCompilationTime)
.tags(Tags.concat(tags, "compiler", compilationBean.getName()))
.description("The approximate accumulated elapsed time spent in compilation")
.baseUnit(BaseUnits.MILLISECONDS)
.register(registry);
}
}
private static void printCompilationInfo() {
CompilationMXBean compilation = ManagementFactory.getCompilationMXBean();
System.out.println("JIT编译器名称:" + compilation.getName());
//判断jvm是否支持编译时间的监控
if (compilation.isCompilationTimeMonitoringSupported()) {
System.out.println("总编译时间:" + compilation.getTotalCompilationTime() + "秒");
}
}
private static void printCompilationInfo() {
CompilationMXBean compilation = ManagementFactory.getCompilationMXBean();
System.out.println("JIT编译器名称:" + compilation.getName());
//判断jvm是否支持编译时间的监控
if (compilation.isCompilationTimeMonitoringSupported()) {
System.out.println("总编译时间:" + compilation.getTotalCompilationTime() + "秒");
}
}
private final int doCompilationMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- CompilationMXBean") ;
try {
ObjectName compilationName =
new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);
if ( mbsc.isRegistered(compilationName) ) {
MBeanInfo mbInfo = mbsc.getMBeanInfo(compilationName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
CompilationMXBean compilation = null ;
compilation =
JMX.newMXBeanProxy(mbsc,
compilationName,
CompilationMXBean.class) ;
System.out.println("getName\t\t"
+ compilation.getName());
boolean supported =
compilation.isCompilationTimeMonitoringSupported() ;
System.out.println("isCompilationTimeMonitoringSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("getTotalCompilationTime\t\t"
+ compilation.getTotalCompilationTime());
}
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private final int doCompilationMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- CompilationMXBean") ;
try {
ObjectName compilationName =
new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);
if ( mbsc.isRegistered(compilationName) ) {
MBeanInfo mbInfo = mbsc.getMBeanInfo(compilationName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
CompilationMXBean compilation = null ;
compilation =
JMX.newMXBeanProxy(mbsc,
compilationName,
CompilationMXBean.class) ;
System.out.println("getName\t\t"
+ compilation.getName());
boolean supported =
compilation.isCompilationTimeMonitoringSupported() ;
System.out.println("isCompilationTimeMonitoringSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("getTotalCompilationTime\t\t"
+ compilation.getTotalCompilationTime());
}
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private final int doCompilationMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- CompilationMXBean") ;
try {
ObjectName compilationName =
new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);
if ( mbsc.isRegistered(compilationName) ) {
MBeanInfo mbInfo = mbsc.getMBeanInfo(compilationName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
CompilationMXBean compilation = null ;
compilation =
JMX.newMXBeanProxy(mbsc,
compilationName,
CompilationMXBean.class) ;
System.out.println("getName\t\t"
+ compilation.getName());
boolean supported =
compilation.isCompilationTimeMonitoringSupported() ;
System.out.println("isCompilationTimeMonitoringSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("getTotalCompilationTime\t\t"
+ compilation.getTotalCompilationTime());
}
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private final int doCompilationMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- CompilationMXBean") ;
try {
ObjectName compilationName =
new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);
if ( mbsc.isRegistered(compilationName) ) {
MBeanInfo mbInfo = mbsc.getMBeanInfo(compilationName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
CompilationMXBean compilation = null ;
compilation =
JMX.newMXBeanProxy(mbsc,
compilationName,
CompilationMXBean.class) ;
System.out.println("getName\t\t"
+ compilation.getName());
boolean supported =
compilation.isCompilationTimeMonitoringSupported() ;
System.out.println("isCompilationTimeMonitoringSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("getTotalCompilationTime\t\t"
+ compilation.getTotalCompilationTime());
}
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
/**
* Returns the total time of asynchronous JIT compilation in milliseconds.
*
* @return JIT compile time
*/
public static long getJITCompileTime(){
long ret = -1; //unsupported
CompilationMXBean cmx = ManagementFactory.getCompilationMXBean();
if( cmx.isCompilationTimeMonitoringSupported() )
{
ret = cmx.getTotalCompilationTime();
ret += jitCompileTime; //add from remote processes
}
return ret;
}
/**
* This method writes the data retrieved from the ClassLoading and
* Compilation MXBeans to the ClassData's log using the MXBeans directly or
* through a proxy.
*
* @param clb
* The ClassLoading MXBean used to retrieve information about
* loaded classes.
* @param cb
* The Compilation MXBean used to retrieve compilation
* information.
* @param rb
* The Runtime MXBean used to retrieve general information about
* the runtime used.
* @param append
* If the append value is true the data is appended to the log,
* otherwise the log is overwritten.
*/
public void writeData(ClassLoadingMXBean clb, CompilationMXBean cb, RuntimeMXBean rb, boolean append)
throws UndeclaredThrowableException {
if ((clb == null)) {
Assert.fail("ClassLoadingMXBean is null");
}
openLogFile(append);
DecimalFormat df = new DecimalFormat("#0.00"); // Print out all times to 2 d.p.
try {
// Record the time and whether the verbose output is enabled
out.println("Class Loading and Compilation Information Retrieved at "
+ DateFormat.getDateTimeInstance().format(new Date()));
out.println("");
// Get stats for the class loader, check the values and write them to a file
int load_cls_cnt = clb.getLoadedClassCount();
long unload_cls_cnt = clb.getUnloadedClassCount();
long total_cls_cnt = clb.getTotalLoadedClassCount();
// Check the class counts are valid
checkClassCounts("proxy", load_cls_cnt, unload_cls_cnt, total_cls_cnt);
// Print the results to the log
out.println("CLASS LOADING STATS");
out.println(" Current Loaded Class Count: " + load_cls_cnt);
out.println(" Total Unloaded Class Count: " + unload_cls_cnt);
out.println(" Total Loaded Class Count: " + total_cls_cnt);
out.print(" Verbose output enabled: ");
if (clb.isVerbose()) {
out.println("Yes");
} else {
out.println("No");
}
out.println("");
out.println("");
// If there is a compiler get the info
Map<String, String> props = rb.getSystemProperties();
String sys_comp = props.get("java.compiler");
if ((sys_comp != null) && (!(sys_comp.equals("")))) {
// Get the info for the compiler and write to file
out.println("COMPILER INFORMATION");
String mxbean_comp = cb.getName();
// Check if the compiler matches the compiler specified in the system properties
checkCompiler("proxy", sys_comp, mxbean_comp);
out.println(" Just-in-time (JIT) compiler: " + mxbean_comp);
if (cb.isCompilationTimeMonitoringSupported()) {
out.println(" Total Compilation Time: " + df.format(cb.getTotalCompilationTime() * 1e-3)
+ " seconds"); // Convert milliseconds to seconds
}
out.println("");
out.println("");
}
closeLogFile();
} catch (UnsupportedOperationException uoe) {
Message.logOut("One of the operations you tried is not supported");
uoe.printStackTrace();
Assert.fail("One of the operations you tried is not supported");
}
}