private String getCommand() { StringBuilder cmdBuilder = new StringBuilder(ProcessCommands.WINDOWS_CPU_COMMAD); if (Configuration.DEFAULT_CSV_FILE_PATH.equals(config.getCsvFilePath())) { cmdBuilder.append(config.getCsvFilePath()); } else { cmdBuilder.append("\"").append(config.getCsvFilePath()).append("\""); } return cmdBuilder.toString(); }
/** * calculates the cpu utilization in % for each process and updates the 'processes' hashmap * * @throws ProcessMonitorException */ private void calcCPUTime() throws ProcessMonitorException { Runtime rt = Runtime.getRuntime(); Process p = null; String cmd = getCommand(); BufferedReader input = null; try { p = rt.exec(cmd); input = new BufferedReader(new InputStreamReader(p.getInputStream())); if (input.readLine() == null) { closeBufferedReader(input); input = new BufferedReader(new InputStreamReader(p.getErrorStream())); String errorString = input.readLine(); if (errorString.toLowerCase().contains("invalid xsl format")) { StringBuilder msg = new StringBuilder(errorString).append(" "); if (Configuration.DEFAULT_CSV_FILE_PATH.equals(config.getCsvFilePath())) { msg.append( "csv.xls not found in C:\\Windows\\System32 or C:\\Windows\\SysWOW64 respectively."); } else { msg.append(config.getCsvFilePath()).append(" not found."); } msg.append(" Cannot process information for CPU usage (value 0 will be reported)."); logger.warn(msg.toString()); return; } } String cpudata; int cpuPosName = -1, cpuPosUserModeTime = -1, cpuPosKernelModeTime = -1, cpuPosProcessId = -1; String header = input.readLine(); // sometimes the first line is empty, so need to cater for this while (header != null && header.trim().equals("")) { if (logger.isDebugEnabled()) { logger.debug("header is empty, checking the next line..."); } header = input.readLine(); } if (header != null) { if (logger.isDebugEnabled()) { logger.debug("header found!"); } String[] words = header.trim().split(","); for (int i = 0; i < words.length; i++) { if (words[i].toLowerCase().equals("name")) { cpuPosName = i; } else if (words[i].toLowerCase().equals("usermodetime")) { cpuPosUserModeTime = i; } else if (words[i].toLowerCase().equals("kernelmodetime")) { cpuPosKernelModeTime = i; } else if (words[i].toLowerCase().equals("processid")) { cpuPosProcessId = i; } } } if (cpuPosName == -1 || cpuPosUserModeTime == -1 || cpuPosKernelModeTime == -1 || cpuPosProcessId == -1) { input.close(); throw new ProcessMonitorException( String.format( "Could not find correct header information of '%s'. Terminating Process Monitor", cmd)); } while ((cpudata = input.readLine()) != null) { String[] words = cpudata.trim().split(","); if (words.length < 5) { continue; } // retrieve single process information String procName = words[cpuPosName]; // divide by 10000 to convert to milliseconds long userModeTime = Long.parseLong(words[cpuPosUserModeTime]) / 10000; long kernelModeTime = Long.parseLong(words[cpuPosKernelModeTime]) / 10000; int pid = Integer.parseInt(words[cpuPosProcessId]); StringBuilder sb = new StringBuilder(procName); procName = sb.append("|PID|").append(pid).toString(); // update hashmaps used for CPU load calculations if (processes.containsKey(procName)) { if (newDeltaCPUTime.containsKey(procName)) { newDeltaCPUTime.put( procName, newDeltaCPUTime.get(procName) + userModeTime + kernelModeTime); } else { newDeltaCPUTime.put(procName, userModeTime + kernelModeTime); } } } // update CPU data in processes hash-map for (String key : newDeltaCPUTime.keySet()) { if (oldDeltaCPUTime.containsKey(key)) { // calculations involving the period and interval float delta = newDeltaCPUTime.get(key) - oldDeltaCPUTime.get(key); float time = reportIntervalSecs * 1000; ProcessData procData = processes.get(key); if (procData != null) { procData.CPUPercent = procData.CPUPercent.add( (new BigDecimal(delta).divide(new BigDecimal(time))) .multiply(new BigDecimal(100))); } } } oldDeltaCPUTime = newDeltaCPUTime; newDeltaCPUTime = new HashMap<String, Long>(); } catch (IOException e) { logger.error("Error in parsing the output of command " + cmd, e); throw new ProcessMonitorException("Error in parsing the output of command " + cmd, e); } catch (Exception e) { logger.error("Exception: ", e); throw new ProcessMonitorException("Exception: ", e); } finally { closeBufferedReader(input); cleanUpProcess(p, cmd); } }