Guest User

Untitled

a guest
Jul 23rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. class CPUUsage {
  2. private static final String TAG = "CpuUsage";
  3. private RandomAccessFile statFile;
  4. private CpuInfo mCpuInfoTotal;
  5. private ArrayList<CpuInfo> mCpuInfoList;
  6.  
  7. CPUUsage() {
  8. }
  9.  
  10. private void update() {
  11. try {
  12. createFile();
  13. parseFile();
  14. closeFile();
  15. } catch (FileNotFoundException e) {
  16. statFile = null;
  17. Log.e(TAG, "cannot open /proc/stat: " + e);
  18. } catch (IOException e) {
  19. Log.e(TAG, "cannot close /proc/stat: " + e);
  20. }
  21. }
  22.  
  23. private void createFile() throws FileNotFoundException {
  24. statFile = new RandomAccessFile("/proc/stat", "r");
  25. }
  26.  
  27. private void closeFile() throws IOException {
  28. if (statFile != null)
  29. statFile.close();
  30. }
  31.  
  32. private void parseFile() {
  33. if (statFile != null) {
  34. try {
  35. statFile.seek(0);
  36. String cpuLine;
  37. int cpuId = -1;
  38. do {
  39. cpuLine = statFile.readLine();
  40. parseCpuLine(cpuId, cpuLine);
  41. cpuId++;
  42. } while (cpuLine != null);
  43. } catch (IOException e) {
  44. Log.e(TAG, "Ops: " + e);
  45. }
  46. }
  47. }
  48.  
  49. private void parseCpuLine(int cpuId, String cpuLine) {
  50. if (cpuLine != null && cpuLine.length() > 0) {
  51. String[] parts = cpuLine.split("[ ]+");
  52. String cpuLabel = "cpu";
  53. if (parts[0].contains(cpuLabel)) {
  54. createCpuInfo(cpuId, parts);
  55. }
  56. } else {
  57. Log.e(TAG, "unable to get cpu line");
  58. }
  59. }
  60.  
  61. private void createCpuInfo(int cpuId, String[] parts) {
  62. if (cpuId == -1) {
  63. if (mCpuInfoTotal == null)
  64. mCpuInfoTotal = new CpuInfo();
  65. mCpuInfoTotal.update(parts);
  66. } else {
  67. if (mCpuInfoList == null)
  68. mCpuInfoList = new ArrayList<>();
  69. if (cpuId < mCpuInfoList.size())
  70. mCpuInfoList.get(cpuId).update(parts);
  71. else {
  72. CpuInfo info = new CpuInfo();
  73. info.update(parts);
  74. mCpuInfoList.add(info);
  75. }
  76. }
  77. }
  78.  
  79. int getTotalCpuUsage() {
  80. update();
  81. int usage = 0;
  82. if (mCpuInfoTotal != null)
  83. usage = mCpuInfoTotal.getUsage();
  84. return usage;
  85. }
  86.  
  87.  
  88. public String toString() {
  89. update();
  90. StringBuilder buf = new StringBuilder();
  91. if (mCpuInfoTotal != null) {
  92. buf.append("Cpu Total : ");
  93. buf.append(mCpuInfoTotal.getUsage());
  94. buf.append("%");
  95. }
  96. if (mCpuInfoList != null) {
  97. for (int i = 0; i < mCpuInfoList.size(); i++) {
  98. CpuInfo info = mCpuInfoList.get(i);
  99. buf.append(" Cpu Core(").append(i).append(") : ");
  100. buf.append(info.getUsage());
  101. buf.append("%");
  102. info.getUsage();
  103. }
  104. }
  105. return buf.toString();
  106. }
  107.  
  108. private class CpuInfo {
  109. private int mUsage;
  110. private long mLastTotal;
  111. private long mLastIdle;
  112.  
  113. CpuInfo() {
  114. mUsage = 0;
  115. mLastTotal = 0;
  116. mLastIdle = 0;
  117. }
  118.  
  119. private int getUsage() {
  120. return mUsage;
  121. }
  122.  
  123. void update(String[] parts) {
  124. long idle = Long.parseLong(parts[4], 10);
  125. long total = 0;
  126. boolean head = true;
  127. for (String part : parts) {
  128. if (head) {
  129. head = false;
  130. continue;
  131. }
  132. total += Long.parseLong(part, 10);
  133. }
  134. long diffIdle = idle - mLastIdle;
  135. long diffTotal = total - mLastTotal;
  136. mUsage = (int) ((float) (diffTotal - diffIdle) / diffTotal * 100);
  137. mLastTotal = total;
  138. mLastIdle = idle;
  139. Log.i(TAG, "CPU total=" + total + "; idle=" + idle + "; usage=" + mUsage);
  140. }
  141. }
  142.  
  143. cpu 32875 8617 24283 55887 1700 1288 1280 0 0 0
  144. cpu0 9055 2312 8395 34225 1281 643 625 0 0 0
  145. cpu1 7098 2263 5864 121 1 219 203 0 0 0
  146. cpu2 7526 2176 4967 141 0 219 186 0 0 0
  147. cpu3 5462 1593 3823 134 0 156 162 0 0 0
  148. cpu4 1284 91 440 5692 72 20 16 0 0 0
  149. cpu5 473 44 211 2588 125 6 67 0 0 0
  150. cpu6 516 47 210 3047 49 10 3 0 0 0
  151. cpu7 1461 91 373 9939 172 15 18 0 0 0
Add Comment
Please, Sign In to add comment