Is there a way to measure CPU usage of the JVM (once a java application is started) cross platform (windows + unix + mac)? I have used Jconsole but what I need is a java code that does this, and not a tool through which I can monitor CPU utilization. I have tried out
ManagementFactory.g开发者_Python百科etOperatingSystemMXBean().getSystemLoadAverage()
using JMX, but it doesn't help since what I need is the specific CPU usage by the JVM (say when I start a server), not the system load average.
You should have a look at the ThreadMXBean.getThreadCPUTime() method from the Thread MBean.
Thread CPU time
A Java virtual machine implementation may support measuring the CPU time for the current thread, for any thread, or for no threads.
There is also the JTop sample application that's part of the JDK jdk\demo\management\JTop\src\JTop.java
or here. Have a look at:
/**
* Get the thread list with CPU consumption and the ThreadInfo for each thread
* sorted by the CPU time.
*/
private List<Map.Entry<Long, ThreadInfo>> getThreadList()
At any given instant, a thread is either running (100% of core) or not (0%). There is no in-between. What you need is a short-term series of snapshots of the thread's running state, and average it over those.
From here
com.sun.management.OperatingSystemMXBean operatingSystemMXBean =
(com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
int availableProcessors = operatingSystemMXBean.getAvailableProcessors();
long prevUpTime = runtimeMXBean.getUptime();
long prevProcessCpuTime = operatingSystemMXBean.getProcessCpuTime();
double cpuUsage;
try
{
Thread.sleep(500);
}
catch (Exception ignored) { }
operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long upTime = runtimeMXBean.getUptime();
long processCpuTime = operatingSystemMXBean.getProcessCpuTime();
long elapsedCpu = processCpuTime - prevProcessCpuTime;
long elapsedTime = upTime - prevUpTime;
cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 10000F * availableProcessors));
System.out.println("Java CPU: " + cpuUsage);
Java itself not providing this feature. There are couple of opensource APIs available to measure CPU Usage.
I recommend Sigar API. Apart from CPU usage, you can get lot more other features like memory usage, System uptime etc.
Perhaps this or similar libraries could help you out.
You can try to use https://github.com/mvysny/webmon . You just add the webmon-analyzer jar to your project and you'll start the sampler and TCP/IP server. The sampler will poll JMX API and will keep the stats for awhile, you can then simply telnet localhost 5455
to get a text dump of those stats. Awesome for production. Disclaimer: I'm the author, and the project is old-ish.
Programmatically querying for CPU usage is impossible using pure Java. There is simply no API for this. A suggested alternative might use Runtime.exec()
to determine the JVM's process ID (PID), call an external, platform-specific command like ps, and parse its output for the PID of interest.
See this link
精彩评论