diff --git a/bindings/java/src/net/hyperic/sigar/Sigar.java b/bindings/java/src/net/hyperic/sigar/Sigar.java index dc3b4f28..302b2ecc 100644 --- a/bindings/java/src/net/hyperic/sigar/Sigar.java +++ b/bindings/java/src/net/hyperic/sigar/Sigar.java @@ -461,6 +461,39 @@ public class Sigar implements SigarProxy { 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. * @exception SigarException on failure. diff --git a/bindings/java/src/net/hyperic/sigar/ThreadCpuTime.java b/bindings/java/src/net/hyperic/sigar/ThreadCpuTime.java deleted file mode 100644 index 299619d4..00000000 --- a/bindings/java/src/net/hyperic/sigar/ThreadCpuTime.java +++ /dev/null @@ -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; - } -} diff --git a/bindings/java/src/net/hyperic/sigar/test/TestThreadCpu.java b/bindings/java/src/net/hyperic/sigar/test/TestThreadCpu.java index 4f533345..f9006540 100644 --- a/bindings/java/src/net/hyperic/sigar/test/TestThreadCpu.java +++ b/bindings/java/src/net/hyperic/sigar/test/TestThreadCpu.java @@ -3,7 +3,6 @@ package net.hyperic.sigar.test; import net.hyperic.sigar.Sigar; import net.hyperic.sigar.SigarNotImplementedException; import net.hyperic.sigar.ThreadCpu; -import net.hyperic.sigar.ThreadCpuTime; public class TestThreadCpu extends SigarTestCase { @@ -14,10 +13,9 @@ public class TestThreadCpu extends SigarTestCase { public void testCreate() throws Exception { Sigar sigar = new Sigar(); - ThreadCpuTime cpu = new ThreadCpuTime(sigar); - + ThreadCpu cpu; try { - cpu.getCurrent(); + cpu = sigar.getThreadCpu(); } catch (SigarNotImplementedException e) { return; } @@ -34,7 +32,7 @@ public class TestThreadCpu extends SigarTestCase { traceln("\nDiff...\n"); - ThreadCpu diff = cpu.getDiff(); + ThreadCpu diff = sigar.getThreadCpuDiff(cpu); assertGtEqZeroTrace("User", diff.getUser());