Hi Jez
Thanks for your reply.
Prior to posting, I did take a very good look at the JavaDocs and did some code to get an idea of what CpuTimes exposes. To me, it does not appear to give me the information I need. Here's my understanding of things, please correct me if/where I'm getting it wrong.
CpuTimes tells you whether the cpu was idle or active. Via getCpuUsage(...) you can determine the percentage of time the cpu was idle or active over a period of time.
But this is not what I am trying to determine. I am not trying to determine the percentage of time the cpu was active. I am trying to determine, at a moment in time, how active the cpu is, as a percentage. I would like to understand, at a moment in time, what percentage of cpu capacity is in use. For instance, I can open system monitor on windows and see my cpu usage is currently 20% (see attached screenshot).
Am I misunderstanding? Is this information already there and I'm simply not seeing how to get it out of the API?
Here's the code I put together in an attempt to determine memory and cpu usage,
private static void testJavaSysMon() {
displayMemoryUsage();
displayCpuUsage();
return;
}
private static void displayMemoryUsage() {
JavaSysMon systemMonitor = null;
MemoryStats memoryUsage = null;
int figure = 0;
systemMonitor = new JavaSysMon();
memoryUsage = systemMonitor.physical();
figure = (int)Math.ceil(
100 * ((double)(memoryUsage.getTotalBytes() - memoryUsage.getFreeBytes()) / memoryUsage.getTotalBytes()));
logger.debug("Memory Usage: " + figure);
return;
}
private static void displayCpuUsage() {
JavaSysMon systemMonitor = null;
CpuTimes cpuTimes = null;
int figure = 0;
systemMonitor = new JavaSysMon();
cpuTimes = systemMonitor.cpuTimes();
Methods.sleep(10000);
figure = (int)Math.ceil(100 * cpuTimes.getCpuUsage(cpuTimes));
logger.debug("CPU Usage: " + figure);
return;
}
Here is the output of running the code,
Memory Usage: 66
CPU Usage: 100
The output for memory is correct and what I want. The output for cpu usage is not what I want. The data for cpu usage is based simply on whether the cpu was active or idle during the period and, in this case, the output is reporting that the cpu was always active in the period. But I just don't want to know if it was active, I want to know how active, as a percentage of total cpu capacity, the cpu was in a moment in time - just like my Windows resource monitor shows me.
Would appreciate any feedback you can give me.
Cheers,
Baron.