try {
in = new BufferedReader(new FileReader(PROC_STAT));
final String output = in.readLine();
if (output == null) {
throw new ProfilingException("Cannot read CPU utilization, return value is null");
}
in.close();
in = null;
final Matcher cpuMatcher = CPU_PATTERN.matcher(output);
if (!cpuMatcher.matches()) {
throw new ProfilingException("Cannot extract CPU utilization from output \"" + output + "\"");
}
/*
* Extract the information from the read line according to
* http://www.linuxhowtos.org/System/procstat.htm
*/
final long cpuUser = Long.parseLong(cpuMatcher.group(1));
final long cpuNice = Long.parseLong(cpuMatcher.group(2));
final long cpuSys = Long.parseLong(cpuMatcher.group(3));
final long cpuIdle = Long.parseLong(cpuMatcher.group(4));
final long cpuIOWait = Long.parseLong(cpuMatcher.group(5));
final long cpuIrq = Long.parseLong(cpuMatcher.group(6));
final long cpuSoftirq = Long.parseLong(cpuMatcher.group(7));
// Calculate deltas
final long deltaCpuUser = cpuUser - this.lastCpuUser;
final long deltaCpuNice = cpuNice - this.lastCpuNice;
final long deltaCpuSys = cpuSys - this.lastCpuSys;
final long deltaCpuIdle = cpuIdle - this.lastCpuIdle;
final long deltaCpuIOWait = cpuIOWait - this.lastCpuIOWait;
final long deltaCpuIrq = cpuIrq - this.lastCpuIrq;
final long deltaCpuSoftirq = cpuSoftirq - this.lastCpuSoftirq;
final long deltaSum = deltaCpuUser + deltaCpuNice + deltaCpuSys + deltaCpuIdle + deltaCpuIOWait
+ deltaCpuIrq + deltaCpuSoftirq;
// TODO: Fix deltaSum = 0 situation
// Set the percentage values for the profiling data object
/*
* profilingData.setIdleCPU((int)((deltaCpuIdle*PERCENT)/deltaSum));
* profilingData.setUserCPU((int)((deltaCpuUser*PERCENT)/deltaSum));
* profilingData.setSystemCPU((int)((deltaCpuSys*PERCENT)/deltaSum));
* profilingData.setIoWaitCPU((int)((deltaCpuIOWait*PERCENT)/deltaSum));
* profilingData.setHardIrqCPU((int)((deltaCpuIrq*PERCENT)/deltaSum));
* profilingData.setSoftIrqCPU((int)((deltaCpuSoftirq*PERCENT)/deltaSum));
*/
// TODO: bad quick fix
if (deltaSum > 0) {
profilingData.setIdleCPU((int) ((deltaCpuIdle * PERCENT) / deltaSum));
profilingData.setUserCPU((int) ((deltaCpuUser * PERCENT) / deltaSum));
profilingData.setSystemCPU((int) ((deltaCpuSys * PERCENT) / deltaSum));
profilingData.setIoWaitCPU((int) ((deltaCpuIOWait * PERCENT) / deltaSum));
profilingData.setHardIrqCPU((int) ((deltaCpuIrq * PERCENT) / deltaSum));
profilingData.setSoftIrqCPU((int) ((deltaCpuSoftirq * PERCENT) / deltaSum));
} else {
profilingData.setIdleCPU((int) (deltaCpuIdle));
profilingData.setUserCPU((int) (deltaCpuUser));
profilingData.setSystemCPU((int) (deltaCpuSys));
profilingData.setIoWaitCPU((int) (deltaCpuIOWait));
profilingData.setHardIrqCPU((int) (deltaCpuIrq));
profilingData.setSoftIrqCPU((int) (deltaCpuSoftirq));
}
// Store values for next call
this.lastCpuUser = cpuUser;
this.lastCpuNice = cpuNice;
this.lastCpuSys = cpuSys;
this.lastCpuIdle = cpuIdle;
this.lastCpuIOWait = cpuIOWait;
this.lastCpuIrq = cpuIrq;
this.lastCpuSoftirq = cpuSoftirq;
} catch (IOException ioe) {
throw new ProfilingException("Error while reading CPU utilization: " + StringUtils.stringifyException(ioe));
} catch (NumberFormatException nfe) {
throw new ProfilingException("Error while reading CPU utilization: " + StringUtils.stringifyException(nfe));
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {