jmx를 이용하여 해당 jvm instance의 cpu usage를 측정하는 메소드는 다음과 같이 되어 있다.

private void _getJavaRuntime() {
OperatingSystemMXBean osbean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
RuntimeMXBean runbean = (RuntimeMXBean) ManagementFactory.getRuntimeMXBean();

long bfprocesstime = osbean.getProcessCpuTime();
long bfuptime = runbean.getUptime();
long ncpus = osbean.getAvailableProcessors();

for (int i = 0; i < 1000000; ++i) {
ncpus = osbean.getAvailableProcessors();
}

long afprocesstime = osbean.getProcessCpuTime();
long afuptime = runbean.getUptime();

float cal = (afprocesstime - bfprocesstime)
/ ((afuptime - bfuptime) * 10000f);

javacpu = Math.min(99f, cal);
uptime = runbean.getUptime(); 
}

Posted by 김용환 '김용환'

댓글을 달아 주세요