Esempio n. 1
0
 /**
  * Get CPU stat of specified process.
  *
  * @param pid Process ID
  * @return null may be returned if some unexpected things happens
  */
 public static ProcCpuStat getProcCpuStat(int pid) {
   String file = String.format(Locale.US, FILE_PROC_CPU_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) {
         ProcCpuStat stat = new ProcCpuStat(pid);
         stat.utime = StringUtils.parseLong(fields[13], 0);
         stat.stime = StringUtils.parseLong(fields[14], 0);
         return stat;
       }
     }
   } catch (IOException e) {
     AppLogger.w(TAG, "failed to read proc cpu stat: " + pid, e);
   } finally {
     IoUtils.closeQuietly(br);
   }
   return null;
 }
Esempio n. 2
0
 public static SysCpuStat getCpuStat() {
   BufferedReader br = null;
   try {
     br = new BufferedReader(new FileReader(FILE_SYS_CPU_STAT));
     String line = null;
     while ((line = br.readLine()) != null) {
       String[] fields = line.split("\\s+");
       if (fields.length >= 5 && fields[0].equals("cpu")) {
         SysCpuStat stat = new SysCpuStat();
         stat.utime = StringUtils.parseLong(fields[1], 0);
         stat.ntime = StringUtils.parseLong(fields[2], 0);
         stat.stime = StringUtils.parseLong(fields[3], 0);
         stat.itime = StringUtils.parseLong(fields[4], 0);
         return stat;
       }
     }
   } catch (IOException e) {
     AppLogger.w(TAG, "failed to read sys cpu stat", e);
   } finally {
     IoUtils.closeQuietly(br);
   }
   return null;
 }