/**
* Get CPU stat of specified process.
* @param pid Process ID
* @return null may be returned if some unexpected things happens
*/
public static long[] getProcCpuStat(int pid) {
String file = String.format(Locale.US, "/proc/%d/stat", pid);
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
String[] fields = line.split("\\s+");
if (fields.length >= 15) {
long[] stat = new long[]{Long.parseLong(fields[13])/**utime**/,
Long.parseLong(fields[14]) /**stime***/};
return stat;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(br);
}
return null;
}