| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 package org.appspot.apprtc; | 11 package org.appspot.apprtc; |
| 12 | 12 |
| 13 import android.content.Context; | 13 import android.content.Context; |
| 14 import android.content.Intent; | 14 import android.content.Intent; |
| 15 import android.content.IntentFilter; | 15 import android.content.IntentFilter; |
| 16 import android.os.BatteryManager; | 16 import android.os.BatteryManager; |
| 17 import android.os.SystemClock; | 17 import android.os.SystemClock; |
| 18 import android.util.Log; | 18 import android.util.Log; |
| 19 | |
| 20 import java.io.BufferedReader; | 19 import java.io.BufferedReader; |
| 21 import java.io.FileNotFoundException; | 20 import java.io.FileNotFoundException; |
| 22 import java.io.FileReader; | 21 import java.io.FileReader; |
| 23 import java.io.IOException; | 22 import java.io.IOException; |
| 24 import java.util.Arrays; | 23 import java.util.Arrays; |
| 25 import java.util.Scanner; | 24 import java.util.Scanner; |
| 26 import java.util.concurrent.Executors; | 25 import java.util.concurrent.Executors; |
| 26 import java.util.concurrent.Future; |
| 27 import java.util.concurrent.ScheduledExecutorService; | 27 import java.util.concurrent.ScheduledExecutorService; |
| 28 import java.util.concurrent.TimeUnit; | 28 import java.util.concurrent.TimeUnit; |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * Simple CPU monitor. The caller creates a CpuMonitor object which can then | 31 * Simple CPU monitor. The caller creates a CpuMonitor object which can then |
| 32 * be used via sampleCpuUtilization() to collect the percentual use of the | 32 * be used via sampleCpuUtilization() to collect the percentual use of the |
| 33 * cumulative CPU capacity for all CPUs running at their nominal frequency. 3 | 33 * cumulative CPU capacity for all CPUs running at their nominal frequency. 3 |
| 34 * values are generated: (1) getCpuCurrent() returns the use since the last | 34 * values are generated: (1) getCpuCurrent() returns the use since the last |
| 35 * sampleCpuUtilization(), (2) getCpuAvg3() returns the use since 3 prior | 35 * sampleCpuUtilization(), (2) getCpuAvg3() returns the use since 3 prior |
| 36 * calls, and (3) getCpuAvgAll() returns the use over all SAMPLE_SAVE_NUMBER | 36 * calls, and (3) getCpuAvgAll() returns the use over all SAMPLE_SAVE_NUMBER |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 return doubleToPercent(frequencyScale.getAverage()); | 197 return doubleToPercent(frequencyScale.getAverage()); |
| 198 } | 198 } |
| 199 | 199 |
| 200 private void scheduleCpuUtilizationTask() { | 200 private void scheduleCpuUtilizationTask() { |
| 201 if (executor != null) { | 201 if (executor != null) { |
| 202 executor.shutdownNow(); | 202 executor.shutdownNow(); |
| 203 executor = null; | 203 executor = null; |
| 204 } | 204 } |
| 205 | 205 |
| 206 executor = Executors.newSingleThreadScheduledExecutor(); | 206 executor = Executors.newSingleThreadScheduledExecutor(); |
| 207 executor.scheduleAtFixedRate(new Runnable() { | 207 @SuppressWarnings("unused") // Prevent downstream linter warnings. |
| 208 Future<?> possiblyIgnoredError = executor.scheduleAtFixedRate(new Runnable()
{ |
| 208 @Override | 209 @Override |
| 209 public void run() { | 210 public void run() { |
| 210 cpuUtilizationTask(); | 211 cpuUtilizationTask(); |
| 211 } | 212 } |
| 212 }, 0, CPU_STAT_SAMPLE_PERIOD_MS, TimeUnit.MILLISECONDS); | 213 }, 0, CPU_STAT_SAMPLE_PERIOD_MS, TimeUnit.MILLISECONDS); |
| 213 } | 214 } |
| 214 | 215 |
| 215 private void cpuUtilizationTask() { | 216 private void cpuUtilizationTask() { |
| 216 boolean cpuMonitorAvailable = sampleCpuUtilization(); | 217 boolean cpuMonitorAvailable = sampleCpuUtilization(); |
| 217 if (cpuMonitorAvailable | 218 if (cpuMonitorAvailable |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 } catch (FileNotFoundException e) { | 502 } catch (FileNotFoundException e) { |
| 502 Log.e(TAG, "Cannot open /proc/stat for reading", e); | 503 Log.e(TAG, "Cannot open /proc/stat for reading", e); |
| 503 return null; | 504 return null; |
| 504 } catch (IOException e) { | 505 } catch (IOException e) { |
| 505 Log.e(TAG, "Problems reading /proc/stat", e); | 506 Log.e(TAG, "Problems reading /proc/stat", e); |
| 506 return null; | 507 return null; |
| 507 } | 508 } |
| 508 return new ProcStat(userTime, systemTime, idleTime); | 509 return new ProcStat(userTime, systemTime, idleTime); |
| 509 } | 510 } |
| 510 } | 511 } |
| OLD | NEW |