Dont require internal Sigar reference

This commit is contained in:
Doug MacEachern 2007-01-31 18:50:51 +00:00
parent ba73629ca0
commit 1d9e3ac5a1
1 changed files with 16 additions and 4 deletions

View File

@ -32,7 +32,7 @@ public class CpuTimer {
private long startTime; private long startTime;
public CpuTimer() { public CpuTimer() {
this(new Sigar()); this(null);
} }
public CpuTimer(Sigar sigar) { public CpuTimer(Sigar sigar) {
@ -55,17 +55,25 @@ public class CpuTimer {
} }
public void start() { public void start() {
start(this.sigar);
}
public void start(Sigar sigar) {
this.startTime = System.currentTimeMillis(); this.startTime = System.currentTimeMillis();
try { try {
this.cpu.gather(this.sigar, 0); this.cpu.gather(sigar, 0);
} catch (SigarException e) { } catch (SigarException e) {
throw new IllegalArgumentException(e.toString()); throw new IllegalArgumentException(e.toString());
} }
} }
public void stop() { public void stop() {
ThreadCpu diff = getDiff(); stop(this.sigar);
}
public void stop(Sigar sigar) {
ThreadCpu diff = getDiff(sigar);
this.cpuTotal += diff.total; this.cpuTotal += diff.total;
this.cpuUser += diff.user; this.cpuUser += diff.user;
@ -79,6 +87,10 @@ public class CpuTimer {
} }
public ThreadCpu getDiff() { public ThreadCpu getDiff() {
return getDiff(this.sigar);
}
public ThreadCpu getDiff(Sigar sigar) {
long startTotal = this.cpu.total; long startTotal = this.cpu.total;
long startUser = this.cpu.user; long startUser = this.cpu.user;
long startSys = this.cpu.sys; long startSys = this.cpu.sys;
@ -86,7 +98,7 @@ public class CpuTimer {
ThreadCpu diff = new ThreadCpu(); ThreadCpu diff = new ThreadCpu();
try { try {
this.cpu.gather(this.sigar, 0); this.cpu.gather(sigar, 0);
} catch (SigarException e) { } catch (SigarException e) {
throw new IllegalArgumentException(e.toString()); throw new IllegalArgumentException(e.toString());
} }