fix bug 1361953(bugzilla)/HQ-4820(jira) - changing the way MultiProcess cpu usage(%) is calculated

This commit is contained in:
Zvika Idelsberg 2015-03-25 03:40:28 -06:00
parent 236006e02d
commit 6d4ff51b9c
1 changed files with 13 additions and 41 deletions

View File

@ -28,64 +28,33 @@ public class MultiProcCpu extends ProcCpu {
private long pid; private long pid;
private int nproc = 0; private int nproc = 0;
private static Map ptable = new HashMap();
static synchronized MultiProcCpu get(Sigar sigar, String query) static synchronized MultiProcCpu get(Sigar sigar, String query)
throws SigarException { throws SigarException {
MultiProcCpu cpu; MultiProcCpu cpu = new MultiProcCpu();
cpu.pid = query.hashCode(); //for equals()
cpu = (MultiProcCpu)ptable.get(query);
if (cpu == null) {
cpu = new MultiProcCpu();
cpu.pid = query.hashCode(); //for equals()
ptable.put(query, cpu);
}
long timeNow = System.currentTimeMillis();
double diff = timeNow - cpu.lastTime;
if (diff == 0) {
return cpu; //we were just called within < 1 second ago.
}
cpu.lastTime = timeNow;
long otime = cpu.total;
cpu.total = 0; cpu.total = 0;
cpu.user = 0; cpu.user = 0;
cpu.sys = 0; cpu.sys = 0;
cpu.nproc = 0; cpu.percent = 0.0D;
long[] pids = ProcessFinder.find(sigar, query); long[] pids = ProcessFinder.find(sigar, query);
cpu.nproc = pids.length; cpu.nproc = pids.length;
for (int i=0; i<pids.length; i++) { for (int i=0; i<pids.length; i++) {
ProcTime time;
try { try {
time = sigar.getProcTime(pids[i]); ProcCpu procCpu = sigar.getProcCpu(pids[i]);
cpu.total += procCpu.getTotal();
cpu.user += procCpu.getUser();
cpu.sys += procCpu.getSys();
cpu.percent += procCpu.getPercent();
} catch (SigarException e) { } catch (SigarException e) {
//process may have gone away or EPERM //process may have gone away or EPERM
continue; continue;
} }
cpu.total += time.total;
cpu.user += time.user;
cpu.sys += time.sys;
}
if (otime == 0) {
//XXX could/should pause first time called.
return cpu;
}
cpu.percent = ((cpu.total - otime) / diff);
if (cpu.percent < 0.0) {
//counter wrapped
cpu.percent = (0.0 - cpu.percent);
}
if (cpu.percent >= 1.0) {
cpu.percent = 0.99;
} }
return cpu; return cpu;
@ -94,6 +63,7 @@ public class MultiProcCpu extends ProcCpu {
/** /**
* @return Processes CPU usage percentage. * @return Processes CPU usage percentage.
*/ */
@Override
public double getPercent() { public double getPercent() {
return this.percent; return this.percent;
} }
@ -108,10 +78,12 @@ public class MultiProcCpu extends ProcCpu {
/** /**
* @return Pid of the process. * @return Pid of the process.
*/ */
@Override
public int hashCode() { public int hashCode() {
return (int)this.pid; return (int)this.pid;
} }
@Override
public boolean equals(Object cpu) { public boolean equals(Object cpu) {
if (!(cpu instanceof MultiProcCpu)) { if (!(cpu instanceof MultiProcCpu)) {
return false; return false;