moving ThreadCpuTime.getDiff into Sigar.getThreadCpuDiff

This commit is contained in:
Doug MacEachern 2004-11-21 19:40:21 +00:00
parent a169dba74b
commit 921a4ce41c
3 changed files with 36 additions and 38 deletions

View File

@ -461,6 +461,39 @@ public class Sigar implements SigarProxy {
return getProcPort(Integer.parseInt(port)); return getProcPort(Integer.parseInt(port));
} }
/**
* Get the cumulative cpu time for the calling thread.
*/
public ThreadCpu getThreadCpu() throws SigarException {
return ThreadCpu.fetch(this, 0);
}
private ThreadCpu diff = null;
/**
* Get cpu time since the last time cpu.gather was called
* for the calling thread.
*/
public ThreadCpu getThreadCpuDiff(ThreadCpu cpu)
throws SigarException {
long startTotal = cpu.total;
long startUser = cpu.user;
long startSys = cpu.sys;
if (this.diff == null) {
this.diff = new ThreadCpu();
}
cpu.gather(this, 0);
this.diff.total = cpu.total - startTotal;
this.diff.user = cpu.user - startUser;
this.diff.sys = cpu.sys - startSys;
return this.diff;
}
/** /**
* Get list of file systems. * Get list of file systems.
* @exception SigarException on failure. * @exception SigarException on failure.

View File

@ -1,33 +0,0 @@
package net.hyperic.sigar;
public class ThreadCpuTime extends ThreadCpu {
private ThreadCpu diff = null;
private Sigar sigar;
public ThreadCpuTime(Sigar sigar) {
super();
this.sigar = sigar;
}
public void getCurrent() throws SigarException {
this.gather(this.sigar, 0);
}
public ThreadCpu getDiff() throws SigarException {
long startTotal = this.total;
long startUser = this.user;
long startSys = this.sys;
if (this.diff == null) {
this.diff = new ThreadCpu();
}
getCurrent();
this.diff.total = this.total - startTotal;
this.diff.user = this.user - startUser;
this.diff.sys = this.sys - startSys;
return this.diff;
}
}

View File

@ -3,7 +3,6 @@ package net.hyperic.sigar.test;
import net.hyperic.sigar.Sigar; import net.hyperic.sigar.Sigar;
import net.hyperic.sigar.SigarNotImplementedException; import net.hyperic.sigar.SigarNotImplementedException;
import net.hyperic.sigar.ThreadCpu; import net.hyperic.sigar.ThreadCpu;
import net.hyperic.sigar.ThreadCpuTime;
public class TestThreadCpu extends SigarTestCase { public class TestThreadCpu extends SigarTestCase {
@ -14,10 +13,9 @@ public class TestThreadCpu extends SigarTestCase {
public void testCreate() throws Exception { public void testCreate() throws Exception {
Sigar sigar = new Sigar(); Sigar sigar = new Sigar();
ThreadCpuTime cpu = new ThreadCpuTime(sigar); ThreadCpu cpu;
try { try {
cpu.getCurrent(); cpu = sigar.getThreadCpu();
} catch (SigarNotImplementedException e) { } catch (SigarNotImplementedException e) {
return; return;
} }
@ -34,7 +32,7 @@ public class TestThreadCpu extends SigarTestCase {
traceln("\nDiff...\n"); traceln("\nDiff...\n");
ThreadCpu diff = cpu.getDiff(); ThreadCpu diff = sigar.getThreadCpuDiff(cpu);
assertGtEqZeroTrace("User", diff.getUser()); assertGtEqZeroTrace("User", diff.getUser());