use native sigar_cpu_perc in CpuPerc.java

This commit is contained in:
Doug MacEachern 2008-03-12 06:23:20 +00:00
parent 043afd2608
commit cf351c458c
4 changed files with 67 additions and 28 deletions

View File

@ -78,7 +78,7 @@ my %has_name_arg = map { $_, 1 } qw(FileSystemUsage DiskUsage
my %proc_no_arg = map { $_, 1 } qw(stat); 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 who cpu_info file_system); #list funcs only
sub supported_platforms { sub supported_platforms {
@ -417,6 +417,38 @@ our %classes = (
plat => '*' 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 => [ CpuInfo => [
{ {
name => 'vendor', type => 'String', name => 'vendor', type => 'String',

View File

@ -554,6 +554,20 @@ JNIEXPORT jobjectArray SIGAR_JNIx(getCpuListNative)
return cpuarray; 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) JNIEXPORT jlongArray SIGAR_JNIx(getProcList)
(JNIEnv *env, jobject sigar_obj) (JNIEnv *env, jobject sigar_obj)
{ {

View File

@ -30,37 +30,30 @@ public class CpuPerc implements java.io.Serializable {
private double nice; private double nice;
private double idle; private double idle;
private double wait; private double wait;
private double combined;
CpuPerc() {} CpuPerc() {}
public static CpuPerc calculate(Cpu oldCpu, Cpu curCpu) { native void gather(Sigar sigar, 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;
static CpuPerc fetch(Sigar sigar, Cpu oldCpu, Cpu curCpu) {
CpuPerc perc = new CpuPerc(); CpuPerc perc = new CpuPerc();
perc.user = diffUser / diffTotal; perc.gather(sigar, oldCpu, curCpu);
perc.sys = diffSys / diffTotal;
perc.nice = diffNice / diffTotal;
perc.idle = diffIdle / diffTotal;
perc.wait = diffWait / diffTotal;
return perc; 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() { public double getUser() {
return this.user; return this.user;
} }
@ -85,7 +78,7 @@ public class CpuPerc implements java.io.Serializable {
* @return Sum of User + Sys + Nice + Wait * @return Sum of User + Sys + Nice + Wait
*/ */
public double getCombined() { public double getCombined() {
return this.user + this.sys + this.nice + this.wait; return this.combined;
} }
public static String format(double val) { public static String format(double val) {

View File

@ -334,7 +334,7 @@ public class Sigar implements SigarProxy {
this.lastCpu = getCpu(); 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<curLen; i++) { for (int i=0; i<curLen; i++) {
perc[i] = perc[i] =
CpuPerc.calculate(oldCpuList[i], CpuPerc.fetch(this, oldCpuList[i],
this.lastCpuList[i]); this.lastCpuList[i]);
} }
return perc; return perc;