add thread cpu usage percent

This commit is contained in:
Doug MacEachern 2007-02-01 00:34:11 +00:00
parent fb98889e17
commit 60c864fd55
3 changed files with 38 additions and 12 deletions

View File

@ -28,6 +28,7 @@ public class CpuTimer implements CpuTimerMBean {
private long cpuTotal;
private long cpuUser;
private long cpuSys;
private double cpuPercent;
private ThreadCpu cpu = new ThreadCpu();
@ -47,6 +48,7 @@ public class CpuTimer implements CpuTimerMBean {
this.cpuTotal = 0;
this.cpuUser = 0;
this.cpuSys = 0;
this.cpuPercent = 0.0;
}
public void add(CpuTimer timer) {
@ -81,11 +83,13 @@ public class CpuTimer implements CpuTimerMBean {
this.cpuUser += diff.user;
this.cpuSys += diff.sys;
long stopTime = System.currentTimeMillis();
long timeNow = System.currentTimeMillis();
long time = stopTime - this.startTime;
double timeDiff = timeNow - this.startTime;
this.totalTime += time;
this.totalTime += timeDiff;
this.cpuPercent = toMillis(diff.total) / timeDiff;
}
public ThreadCpu getDiff() {
@ -116,16 +120,24 @@ public class CpuTimer implements CpuTimerMBean {
return this.totalTime;
}
private long toMillis(long ns) {
return ns / 1000000; //convert nanos to millis
}
public long getCpuTotal() {
return this.cpuTotal / 1000000; //convert nanos to millis
return toMillis(this.cpuTotal);
}
public long getCpuUser() {
return this.cpuUser / 1000000; //convert nanos to millis
return toMillis(this.cpuUser);
}
public long getCpuSys() {
return this.cpuSys / 1000000; //convert nanos to millis
return toMillis(this.cpuSys / 1000000);
}
public double getCpuUsage() {
return this.cpuPercent;
}
public String format(long elap) {
@ -148,5 +160,6 @@ public class CpuTimer implements CpuTimerMBean {
format(getTotalTime()));
out.println("user....." + format(getCpuUser()));
out.println("sys......" + format(getCpuSys()));
out.println("usage...." + CpuPerc.format(getCpuUsage()));
}
}

View File

@ -26,5 +26,7 @@ public interface CpuTimerMBean {
public long getCpuSys();
public double getCpuUsage();
public long getTotalTime();
}

View File

@ -18,6 +18,7 @@
package org.hyperic.sigar.test;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.CpuTimer;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarNotImplementedException;
@ -46,19 +47,29 @@ public class TestThreadCpu extends SigarTestCase {
assertGtEqZeroTrace("Total", cpu.getTotal());
CpuTimer timer = new CpuTimer(sigar);
timer.start();
for (int i=0; i<1000000; i++) {
System.getProperty("java.home");
}
traceln("\nDiff...\n");
String sleepTime =
System.getProperty("sigar.testThreadCpu.sleep");
if (sleepTime != null) {
Thread.sleep(Integer.parseInt(sleepTime) * 1000);
}
timer.stop();
ThreadCpu diff = timer.getDiff();
traceln("\nUsage...\n");
assertGtEqZeroTrace("User", diff.getUser());
assertGtEqZeroTrace("User", timer.getCpuUser());
assertGtEqZeroTrace("Sys", diff.getSys());
assertGtEqZeroTrace("Sys", timer.getCpuSys());
assertGtEqZeroTrace("Total", diff.getTotal());
assertGtEqZeroTrace("Total", timer.getCpuTotal());
assertGtEqZeroTrace("Real Time", timer.getTotalTime());
traceln("Cpu Percent=" + CpuPerc.format(timer.getCpuUsage()));
}
}