/** * get current process cpu usage data. * * @param pid process id * @return ProcessUsage */ private ProcessUsage currentProcessUsage(final long pid) { String file = getProcessStatFile(pid); try { String cpuline = FileUtils.file2string(file); return new ProcessUsage(cpuline); } catch (IOException ioe) { System.err.println("cannot find stat of process: " + pid); System.exit(1); return null; } }
/** * get current thread cpu usage data. * * @param pid process id * @param tid thread id * @return ThreadUsage */ private ThreadUsage currentThreadUsage(final long pid, final long tid) { String file = getThreadStatFile(pid, tid); try { String cpuline = FileUtils.file2string(file); return new ThreadUsage(cpuline); } catch (IOException ioe) { System.err.println(String.format("cannot find stat of process: %d, thread: %d", pid, tid)); System.exit(1); return null; } }
/** * read /proc/cpuinfo to get cpu numbers. * * @return cpu number */ public static int numCpus() { List<String> cpuline_list = null; try { cpuline_list = FileUtils.grepFile(CPU_NUM_PATTERN, CPU_INFO_FILE); } catch (IOException ioe) { System.err.println("cannot find stat file: " + CPU_STAT_FILE); System.exit(1); return 0; } if (cpuline_list != null) { return cpuline_list.size(); } return 0; }
/** * get current cpu usage data. * * @return CPUUsage */ private CPUUsage currentCPUUsage() { List<String> cpuline_list = null; try { cpuline_list = FileUtils.grepFile(CPU_JIFFIES_PATTERN, CPU_STAT_FILE); } catch (IOException ioe) { System.err.println("cannot find stat file: " + CPU_STAT_FILE); System.exit(1); return null; } if (cpuline_list != null && !cpuline_list.isEmpty()) { return new CPUUsage(cpuline_list.get(0)); } return null; }
/** * read /proc/cpuinfo to get cpu frequency in hz. * * @return cpu frequency in hz */ public static long cpuFrequencyInHz() { List<String> cpuline_list = null; try { cpuline_list = FileUtils.grepFile(CPU_FREQ_PATTERN, CPU_INFO_FILE); } catch (IOException ioe) { System.err.println("cannot find stat file: " + CPU_STAT_FILE); System.exit(1); return 0; } if (cpuline_list != null && !cpuline_list.isEmpty()) { String freqstr = cpuline_list.get(0); int len = freqstr.length(); BigDecimal freq = new BigDecimal(freqstr.substring(0, len - 3)); long multiplier = getMultipiler(freqstr.charAt(len - 3)); return freq.multiply(new BigDecimal(Long.toString(multiplier))).longValue(); } return 0; }