From cf351c458cb9a011e3f10bf2792b59c2f518e2b1 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Wed, 12 Mar 2008 06:23:20 +0000 Subject: [PATCH] use native sigar_cpu_perc in CpuPerc.java --- bindings/SigarWrapper.pm | 34 ++++++++++++++- bindings/java/src/jni/javasigar.c | 14 +++++++ .../java/src/org/hyperic/sigar/CpuPerc.java | 41 ++++++++----------- .../java/src/org/hyperic/sigar/Sigar.java | 6 +-- 4 files changed, 67 insertions(+), 28 deletions(-) diff --git a/bindings/SigarWrapper.pm b/bindings/SigarWrapper.pm index e5de4152..52519a70 100644 --- a/bindings/SigarWrapper.pm +++ b/bindings/SigarWrapper.pm @@ -78,7 +78,7 @@ my %has_name_arg = map { $_, 1 } qw(FileSystemUsage DiskUsage my %proc_no_arg = map { $_, 1 } qw(stat); -my %get_not_impl = map { $_, 1 } qw(net_address net_route net_connection net_stat +my %get_not_impl = map { $_, 1 } qw(net_address net_route net_connection net_stat cpu_perc who cpu_info file_system); #list funcs only sub supported_platforms { @@ -417,6 +417,38 @@ our %classes = ( plat => '*' }, ], + CpuPerc => [ + { + name => 'user', type => 'Double', + desc => 'Percent system cpu user time', + plat => '*' + }, + { + name => 'sys', type => 'Double', + desc => 'Percent system cpu kernel time', + plat => '*' + }, + { + name => 'nice', type => 'Double', + desc => 'Percent system cpu nice time', + plat => 'DFHL' + }, + { + name => 'idle', type => 'Double', + desc => 'Percent system cpu idle time', + plat => '*' + }, + { + name => 'wait', type => 'Double', + desc => 'Percent system cpu io wait time', + plat => 'ALHS' + }, + { + name => 'combined', type => 'Double', + desc => 'Sum of User + Sys + Nice + Wait', + plat => '*' + }, + ], CpuInfo => [ { name => 'vendor', type => 'String', diff --git a/bindings/java/src/jni/javasigar.c b/bindings/java/src/jni/javasigar.c index ba34c97c..dbffa522 100644 --- a/bindings/java/src/jni/javasigar.c +++ b/bindings/java/src/jni/javasigar.c @@ -554,6 +554,20 @@ JNIEXPORT jobjectArray SIGAR_JNIx(getCpuListNative) return cpuarray; } +JNIEXPORT void SIGAR_JNI(CpuPerc_gather) +(JNIEnv *env, jobject jperc, jobject sigar_obj, jobject jprev, jobject jcurr) +{ + sigar_cpu_t prev, curr; + sigar_cpu_perc_t perc; + dSIGAR_VOID; + + JAVA_SIGAR_GET_FIELDS_CPU(jprev, prev); + JAVA_SIGAR_GET_FIELDS_CPU(jcurr, curr); + sigar_cpu_perc_calculate(&prev, &curr, &perc); + JAVA_SIGAR_INIT_FIELDS_CPUPERC(JENV->GetObjectClass(env, jperc)); + JAVA_SIGAR_SET_FIELDS_CPUPERC(NULL, jperc, perc); +} + JNIEXPORT jlongArray SIGAR_JNIx(getProcList) (JNIEnv *env, jobject sigar_obj) { diff --git a/bindings/java/src/org/hyperic/sigar/CpuPerc.java b/bindings/java/src/org/hyperic/sigar/CpuPerc.java index 67520761..50dc3c31 100644 --- a/bindings/java/src/org/hyperic/sigar/CpuPerc.java +++ b/bindings/java/src/org/hyperic/sigar/CpuPerc.java @@ -30,37 +30,30 @@ public class CpuPerc implements java.io.Serializable { private double nice; private double idle; private double wait; + private double combined; CpuPerc() {} - public static CpuPerc calculate(Cpu oldCpu, Cpu curCpu) { - double diffUser, diffSys, diffNice, diffIdle, diffWait, diffTotal; - - diffUser = curCpu.getUser() - oldCpu.getUser(); - diffSys = curCpu.getSys() - oldCpu.getSys(); - diffNice = curCpu.getNice() - oldCpu.getNice(); - diffIdle = curCpu.getIdle() - oldCpu.getIdle(); - diffWait = curCpu.getWait() - oldCpu.getWait(); - - // Sanity check -- sometimes these values waiver in between - // whole numbers when Cpu is checked very rapidly - diffUser = diffUser < 0 ? 0 : diffUser; - diffSys = diffSys < 0 ? 0 : diffSys; - diffNice = diffNice < 0 ? 0 : diffNice; - diffIdle = diffIdle < 0 ? 0 : diffIdle; - diffWait = diffWait < 0 ? 0 : diffWait; - - diffTotal = diffUser + diffSys + diffNice + diffIdle + diffWait; + native void gather(Sigar sigar, Cpu oldCpu, Cpu curCpu); + static CpuPerc fetch(Sigar sigar, Cpu oldCpu, Cpu curCpu) { CpuPerc perc = new CpuPerc(); - perc.user = diffUser / diffTotal; - perc.sys = diffSys / diffTotal; - perc.nice = diffNice / diffTotal; - perc.idle = diffIdle / diffTotal; - perc.wait = diffWait / diffTotal; + perc.gather(sigar, oldCpu, curCpu); return perc; } + /** + * @deprecated + */ + public static CpuPerc calculate(Cpu oldCpu, Cpu curCpu) { + Sigar sigar = new Sigar(); + try { + return fetch(sigar, oldCpu, curCpu); + } finally { + sigar.close(); + } + } + public double getUser() { return this.user; } @@ -85,7 +78,7 @@ public class CpuPerc implements java.io.Serializable { * @return Sum of User + Sys + Nice + Wait */ public double getCombined() { - return this.user + this.sys + this.nice + this.wait; + return this.combined; } public static String format(double val) { diff --git a/bindings/java/src/org/hyperic/sigar/Sigar.java b/bindings/java/src/org/hyperic/sigar/Sigar.java index b347941b..fdd00bb4 100644 --- a/bindings/java/src/org/hyperic/sigar/Sigar.java +++ b/bindings/java/src/org/hyperic/sigar/Sigar.java @@ -334,7 +334,7 @@ public class Sigar implements SigarProxy { this.lastCpu = getCpu(); - return CpuPerc.calculate(oldCpu, this.lastCpu); + return CpuPerc.fetch(this, oldCpu, this.lastCpu); } /** @@ -362,8 +362,8 @@ public class Sigar implements SigarProxy { for (int i=0; i