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 int nproc = 0;
private static Map ptable = new HashMap();
static synchronized MultiProcCpu get(Sigar sigar, String query)
throws SigarException {
MultiProcCpu cpu;
cpu = (MultiProcCpu)ptable.get(query);
if (cpu == null) {
cpu = new MultiProcCpu();
MultiProcCpu 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.user = 0;
cpu.sys = 0;
cpu.nproc = 0;
cpu.percent = 0.0D;
long[] pids = ProcessFinder.find(sigar, query);
cpu.nproc = pids.length;
for (int i=0; i<pids.length; i++) {
ProcTime time;
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) {
//process may have gone away or EPERM
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;
@ -94,6 +63,7 @@ public class MultiProcCpu extends ProcCpu {
/**
* @return Processes CPU usage percentage.
*/
@Override
public double getPercent() {
return this.percent;
}
@ -108,10 +78,12 @@ public class MultiProcCpu extends ProcCpu {
/**
* @return Pid of the process.
*/
@Override
public int hashCode() {
return (int)this.pid;
}
@Override
public boolean equals(Object cpu) {
if (!(cpu instanceof MultiProcCpu)) {
return false;