Remove Scanner usage from CPU Monitor.
TBR=wzh@webrtc.org BUG=b/28560555 Review URL: https://codereview.webrtc.org/1956063002 . Cr-Commit-Position: refs/heads/master@{#12648}
This commit is contained in:
@ -70,10 +70,10 @@ import org.appspot.apprtc.util.LooperExecutor;
|
|||||||
|
|
||||||
class CpuMonitor {
|
class CpuMonitor {
|
||||||
private static final String TAG = "CpuMonitor";
|
private static final String TAG = "CpuMonitor";
|
||||||
private static final int MOVING_AVERAGE_SAMPLES = 10;
|
private static final int MOVING_AVERAGE_SAMPLES = 5;
|
||||||
|
|
||||||
private static final int CPU_STAT_SAMPLE_PERIOD_MS = 1000;
|
private static final int CPU_STAT_SAMPLE_PERIOD_MS = 2000;
|
||||||
private static final int CPU_STAT_LOG_PERIOD_MS = 5000;
|
private static final int CPU_STAT_LOG_PERIOD_MS = 6000;
|
||||||
|
|
||||||
private final Context appContext;
|
private final Context appContext;
|
||||||
// User CPU usage at current frequency.
|
// User CPU usage at current frequency.
|
||||||
@ -209,6 +209,7 @@ class CpuMonitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void scheduleCpuUtilizationTask() {
|
private void scheduleCpuUtilizationTask() {
|
||||||
|
executor.cancelScheduledTasks();
|
||||||
executor.scheduleAtFixedRate(new Runnable() {
|
executor.scheduleAtFixedRate(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -438,21 +439,29 @@ class CpuMonitor {
|
|||||||
private long readFreqFromFile(String fileName) {
|
private long readFreqFromFile(String fileName) {
|
||||||
long number = 0;
|
long number = 0;
|
||||||
try {
|
try {
|
||||||
FileReader fin = new FileReader(fileName);
|
BufferedReader reader = new BufferedReader(new FileReader(fileName));
|
||||||
try {
|
try {
|
||||||
BufferedReader rdr = new BufferedReader(fin);
|
String line = reader.readLine();
|
||||||
Scanner scannerC = new Scanner(rdr);
|
number = parseLong(line);
|
||||||
number = scannerC.nextLong();
|
|
||||||
scannerC.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
// CPU presumably got offline just after we opened file.
|
|
||||||
} finally {
|
} finally {
|
||||||
fin.close();
|
reader.close();
|
||||||
}
|
}
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
// CPU is offline, not an error.
|
// CPU core is off, so file with its scaling frequency .../cpufreq/scaling_cur_freq
|
||||||
|
// is not present. This is not an error.
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "Error closing file");
|
// CPU core is off, so file with its scaling frequency .../cpufreq/scaling_cur_freq
|
||||||
|
// is empty. This is not an error.
|
||||||
|
}
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long parseLong(String value) {
|
||||||
|
long number = 0;
|
||||||
|
try {
|
||||||
|
number = Long.parseLong(value);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Log.e(TAG, "parseLong error.", e);
|
||||||
}
|
}
|
||||||
return number;
|
return number;
|
||||||
}
|
}
|
||||||
@ -466,32 +475,36 @@ class CpuMonitor {
|
|||||||
long systemTime = 0;
|
long systemTime = 0;
|
||||||
long idleTime = 0;
|
long idleTime = 0;
|
||||||
try {
|
try {
|
||||||
FileReader fin = new FileReader("/proc/stat");
|
BufferedReader reader = new BufferedReader(new FileReader("/proc/stat"));
|
||||||
try {
|
try {
|
||||||
BufferedReader rdr = new BufferedReader(fin);
|
// line should contain something like this:
|
||||||
Scanner scanner = new Scanner(rdr);
|
// cpu 5093818 271838 3512830 165934119 101374 447076 272086 0 0 0
|
||||||
scanner.next();
|
// user nice system idle iowait irq softirq
|
||||||
userTime = scanner.nextLong(); // user
|
String line = reader.readLine();
|
||||||
long nice = scanner.nextLong(); // nice
|
String lines[] = line.split("\\s+");
|
||||||
userTime += nice;
|
int length = lines.length;
|
||||||
systemTime = scanner.nextLong(); // system
|
if (length >= 5) {
|
||||||
idleTime = scanner.nextLong(); // idle
|
userTime = parseLong(lines[1]); // user
|
||||||
long ioWaitTime = scanner.nextLong(); // iowait
|
userTime += parseLong(lines[2]); // nice
|
||||||
userTime += ioWaitTime;
|
systemTime = parseLong(lines[3]); // system
|
||||||
long irqTime = scanner.nextLong() + scanner.nextLong(); // irq + softirq
|
idleTime = parseLong(lines[4]); // idle
|
||||||
systemTime += irqTime;
|
}
|
||||||
scanner.close();
|
if (length >= 8) {
|
||||||
|
userTime += parseLong(lines[5]); // iowait
|
||||||
|
systemTime += parseLong(lines[6]); // irq
|
||||||
|
systemTime += parseLong(lines[7]); // softirq
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(TAG, "Problems parsing /proc/stat");
|
Log.e(TAG, "Problems parsing /proc/stat", e);
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
fin.close();
|
reader.close();
|
||||||
}
|
}
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
Log.e(TAG, "Cannot open /proc/stat for reading");
|
Log.e(TAG, "Cannot open /proc/stat for reading", e);
|
||||||
return null;
|
return null;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "Problems reading /proc/stat");
|
Log.e(TAG, "Problems reading /proc/stat", e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return new ProcStat(userTime, systemTime, idleTime);
|
return new ProcStat(userTime, systemTime, idleTime);
|
||||||
|
|||||||
Reference in New Issue
Block a user