Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CPUUsage {
- private static final String TAG = "CpuUsage";
- private RandomAccessFile statFile;
- private CpuInfo mCpuInfoTotal;
- private ArrayList<CpuInfo> mCpuInfoList;
- CPUUsage() {
- }
- private void update() {
- try {
- createFile();
- parseFile();
- closeFile();
- } catch (FileNotFoundException e) {
- statFile = null;
- Log.e(TAG, "cannot open /proc/stat: " + e);
- } catch (IOException e) {
- Log.e(TAG, "cannot close /proc/stat: " + e);
- }
- }
- private void createFile() throws FileNotFoundException {
- statFile = new RandomAccessFile("/proc/stat", "r");
- }
- private void closeFile() throws IOException {
- if (statFile != null)
- statFile.close();
- }
- private void parseFile() {
- if (statFile != null) {
- try {
- statFile.seek(0);
- String cpuLine;
- int cpuId = -1;
- do {
- cpuLine = statFile.readLine();
- parseCpuLine(cpuId, cpuLine);
- cpuId++;
- } while (cpuLine != null);
- } catch (IOException e) {
- Log.e(TAG, "Ops: " + e);
- }
- }
- }
- private void parseCpuLine(int cpuId, String cpuLine) {
- if (cpuLine != null && cpuLine.length() > 0) {
- String[] parts = cpuLine.split("[ ]+");
- String cpuLabel = "cpu";
- if (parts[0].contains(cpuLabel)) {
- createCpuInfo(cpuId, parts);
- }
- } else {
- Log.e(TAG, "unable to get cpu line");
- }
- }
- private void createCpuInfo(int cpuId, String[] parts) {
- if (cpuId == -1) {
- if (mCpuInfoTotal == null)
- mCpuInfoTotal = new CpuInfo();
- mCpuInfoTotal.update(parts);
- } else {
- if (mCpuInfoList == null)
- mCpuInfoList = new ArrayList<>();
- if (cpuId < mCpuInfoList.size())
- mCpuInfoList.get(cpuId).update(parts);
- else {
- CpuInfo info = new CpuInfo();
- info.update(parts);
- mCpuInfoList.add(info);
- }
- }
- }
- int getTotalCpuUsage() {
- update();
- int usage = 0;
- if (mCpuInfoTotal != null)
- usage = mCpuInfoTotal.getUsage();
- return usage;
- }
- public String toString() {
- update();
- StringBuilder buf = new StringBuilder();
- if (mCpuInfoTotal != null) {
- buf.append("Cpu Total : ");
- buf.append(mCpuInfoTotal.getUsage());
- buf.append("%");
- }
- if (mCpuInfoList != null) {
- for (int i = 0; i < mCpuInfoList.size(); i++) {
- CpuInfo info = mCpuInfoList.get(i);
- buf.append(" Cpu Core(").append(i).append(") : ");
- buf.append(info.getUsage());
- buf.append("%");
- info.getUsage();
- }
- }
- return buf.toString();
- }
- private class CpuInfo {
- private int mUsage;
- private long mLastTotal;
- private long mLastIdle;
- CpuInfo() {
- mUsage = 0;
- mLastTotal = 0;
- mLastIdle = 0;
- }
- private int getUsage() {
- return mUsage;
- }
- void update(String[] parts) {
- long idle = Long.parseLong(parts[4], 10);
- long total = 0;
- boolean head = true;
- for (String part : parts) {
- if (head) {
- head = false;
- continue;
- }
- total += Long.parseLong(part, 10);
- }
- long diffIdle = idle - mLastIdle;
- long diffTotal = total - mLastTotal;
- mUsage = (int) ((float) (diffTotal - diffIdle) / diffTotal * 100);
- mLastTotal = total;
- mLastIdle = idle;
- Log.i(TAG, "CPU total=" + total + "; idle=" + idle + "; usage=" + mUsage);
- }
- }
- cpu 32875 8617 24283 55887 1700 1288 1280 0 0 0
- cpu0 9055 2312 8395 34225 1281 643 625 0 0 0
- cpu1 7098 2263 5864 121 1 219 203 0 0 0
- cpu2 7526 2176 4967 141 0 219 186 0 0 0
- cpu3 5462 1593 3823 134 0 156 162 0 0 0
- cpu4 1284 91 440 5692 72 20 16 0 0 0
- cpu5 473 44 211 2588 125 6 67 0 0 0
- cpu6 516 47 210 3047 49 10 3 0 0 0
- cpu7 1461 91 373 9939 172 15 18 0 0 0
Add Comment
Please, Sign In to add comment