我想将“Jboss 7”监视为“CPU使用率”,我可以使用JMX吗?当我写这个函数时,我得到CPU时间,而不是CPU百分比......
public String getCpuUsage(){
try {
Method m=op.getClass().getDeclaredMethod("getProcessCpuTime");
m.setAccessible(true);
Object value=null;
value=m.invoke(op);
return value+"";
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
如何计算JBoss CPU百分比?
答案 0 :(得分:0)
进程cpu时间是进程自启动以来使用的CPU总纳秒数。因此,要获得百分比,您将需要另外2条信息:
实现上述第2项的最佳方法是定期轮询流程cpu时间,并在第一次轮询后,经过的挂钟时间将是当前 System.nanoTime()减去先前投票期间收集的相同数量。
所以现在你有:
利用率百分比 Y /(X x C)* 100
这是一个快速而肮脏的groovy脚本,用于演示:
import java.lang.management.*;
import java.util.concurrent.*;
osx = ManagementFactory.getOperatingSystemMXBean();
cores = osx.getAvailableProcessors();
// Factorial to keep the process busy so we can see some actual activity
factorial = { n ->
int fact = 1;
int i = 1;
while(i <= n) {
i++;
fact *= i;
}
return fact;
}
long elapsedTime = -1, startTime = -1;
long elapsedCpu = -1, startCpu = -1;;
for(i in 0..20) {
startTime = System.nanoTime();
startCpu = osx.getProcessCpuTime();
CountDownLatch latch = new CountDownLatch(cores);
for(x in 1..cores) {
Thread.startDaemon() {
factorial(1000000);
latch.countDown();
}
}
latch.await();
elapsedTime = System.nanoTime()-startTime;
elapsedCpu = osx.getProcessCpuTime()-startCpu;
percUsage = (elapsedCpu / (elapsedTime* cores)) *100;
println "Percent Usage:$percUsage %";
}
输出:
Percent Usage:51.5167066100 %
Percent Usage:53.5206121100 %
Percent Usage:55.3874037900 %
Percent Usage:59.8187255600 %
Percent Usage:60.0488511300 %
Percent Usage:68.3172332900 %
Percent Usage:45.0529180000 %
Percent Usage:58.3662459200 %
Percent Usage:62.6933609700 %
Percent Usage:60.1530673000 %
Percent Usage:69.0448532600 %
Percent Usage:52.0072152200 %
Percent Usage:57.1837311400 %
Percent Usage:65.8311579500 %
Percent Usage:66.3076807600 %
Percent Usage:69.8803721800 %
Percent Usage:28.5111361300 %
Percent Usage:40.1093035100 %
Percent Usage:60.9499459700 %
Percent Usage:68.7113809300 %
Percent Usage:71.8252480400 %
你应该比我做得更好。