@Test public void testBrokenFile() throws Exception { TestableCpuMetricsCollector collector = new TestableCpuMetricsCollector().setPath(createFile("I am a weird android manufacturer")); CpuMetrics snapshot = new CpuMetrics(); assertThat(collector.getSnapshot(snapshot)).isFalse(); }
@Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(CpuMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); try { ProcFileReader reader = mProcFileReader.get(); if (reader == null) { reader = new ProcFileReader(getPath()); mProcFileReader.set(reader); } reader.reset(); if (!reader.isValid()) { return false; } int index = 0; while (index < PROC_USER_TIME_FIELD) { reader.skipSpaces(); index++; } snapshot.userTimeS = readField(reader); snapshot.systemTimeS = readField(reader); snapshot.childUserTimeS = readField(reader); snapshot.childSystemTimeS = readField(reader); } catch (ProcFileReader.ParseException pe) { SystemMetricsLogger.wtf(TAG, "Unable to parse CPU time field", pe); return false; } if (mLastSnapshot.get() == null) { mLastSnapshot.set(new CpuMetrics()); } CpuMetrics lastSnapshot = mLastSnapshot.get(); if (Double.compare(snapshot.userTimeS, lastSnapshot.userTimeS) < 0 || Double.compare(snapshot.systemTimeS, lastSnapshot.systemTimeS) < 0 || Double.compare(snapshot.childUserTimeS, lastSnapshot.childUserTimeS) < 0 || Double.compare(snapshot.childSystemTimeS, lastSnapshot.childSystemTimeS) < 0) { SystemMetricsLogger.wtf( TAG, "Cpu Time Decreased from " + lastSnapshot.toString() + " to " + snapshot.toString()); return false; } lastSnapshot.set(snapshot); return true; }
CpuMetricsCollector extends SystemMetricsCollector<CpuMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(CpuMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); try { ProcFileReader reader = mProcFileReader.get(); if (reader == null) { reader = new ProcFileReader(getPath()); mProcFileReader.set(reader); } reader.reset(); if (!reader.isValid()) { return false; } int index = 0; while (index < PROC_USER_TIME_FIELD) { reader.skipSpaces(); index++; } snapshot.userTimeS = readField(reader); snapshot.systemTimeS = readField(reader); snapshot.childUserTimeS = readField(reader); snapshot.childSystemTimeS = readField(reader); } catch (ProcFileReader.ParseException pe) { SystemMetricsLogger.wtf(TAG, "Unable to parse CPU time field", pe); return false; } if (mLastSnapshot.get() == null) { mLastSnapshot.set(new CpuMetrics()); } CpuMetrics lastSnapshot = mLastSnapshot.get(); if (Double.compare(snapshot.userTimeS, lastSnapshot.userTimeS) < 0 || Double.compare(snapshot.systemTimeS, lastSnapshot.systemTimeS) < 0 || Double.compare(snapshot.childUserTimeS, lastSnapshot.childUserTimeS) < 0 || Double.compare(snapshot.childSystemTimeS, lastSnapshot.childSystemTimeS) < 0) { SystemMetricsLogger.wtf( TAG, "Cpu Time Decreased from " + lastSnapshot.toString() + " to " + snapshot.toString()); return false; } lastSnapshot.set(snapshot); return true; } }
CpuMetricsCollector extends SystemMetricsCollector<CpuMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(CpuMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); try { ProcFileReader reader = mProcFileReader.get(); if (reader == null) { reader = new ProcFileReader(getPath()); mProcFileReader.set(reader); } reader.reset(); if (!reader.isValid()) { return false; } int index = 0; while (index < PROC_USER_TIME_FIELD) { reader.skipSpaces(); index++; } snapshot.userTimeS = readField(reader); snapshot.systemTimeS = readField(reader); snapshot.childUserTimeS = readField(reader); snapshot.childSystemTimeS = readField(reader); } catch (ProcFileReader.ParseException pe) { SystemMetricsLogger.wtf(TAG, "Unable to parse CPU time field", pe); return false; } if (mLastSnapshot.get() == null) { mLastSnapshot.set(new CpuMetrics()); } CpuMetrics lastSnapshot = mLastSnapshot.get(); if (Double.compare(snapshot.userTimeS, lastSnapshot.userTimeS) < 0 || Double.compare(snapshot.systemTimeS, lastSnapshot.systemTimeS) < 0 || Double.compare(snapshot.childUserTimeS, lastSnapshot.childUserTimeS) < 0 || Double.compare(snapshot.childSystemTimeS, lastSnapshot.childSystemTimeS) < 0) { SystemMetricsLogger.wtf( TAG, "Cpu Time Decreased from " + lastSnapshot.toString() + " to " + snapshot.toString()); return false; } lastSnapshot.set(snapshot); return true; } CpuMetricsCollector(); }
CpuMetricsCollector extends SystemMetricsCollector<CpuMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(CpuMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); try { ProcFileReader reader = mProcFileReader.get(); if (reader == null) { reader = new ProcFileReader(getPath()); mProcFileReader.set(reader); } reader.reset(); if (!reader.isValid()) { return false; } int index = 0; while (index < PROC_USER_TIME_FIELD) { reader.skipSpaces(); index++; } snapshot.userTimeS = readField(reader); snapshot.systemTimeS = readField(reader); snapshot.childUserTimeS = readField(reader); snapshot.childSystemTimeS = readField(reader); } catch (ProcFileReader.ParseException pe) { SystemMetricsLogger.wtf(TAG, "Unable to parse CPU time field", pe); return false; } if (mLastSnapshot.get() == null) { mLastSnapshot.set(new CpuMetrics()); } CpuMetrics lastSnapshot = mLastSnapshot.get(); if (Double.compare(snapshot.userTimeS, lastSnapshot.userTimeS) < 0 || Double.compare(snapshot.systemTimeS, lastSnapshot.systemTimeS) < 0 || Double.compare(snapshot.childUserTimeS, lastSnapshot.childUserTimeS) < 0 || Double.compare(snapshot.childSystemTimeS, lastSnapshot.childSystemTimeS) < 0) { SystemMetricsLogger.wtf( TAG, "Cpu Time Decreased from " + lastSnapshot.toString() + " to " + snapshot.toString()); return false; } lastSnapshot.set(snapshot); return true; } CpuMetricsCollector(); @Override @ThreadSafe(enableChecks = false) boolean getSnapshot(CpuMetrics snapshot); @Override CpuMetrics createMetrics(); }
CpuMetricsCollector extends SystemMetricsCollector<CpuMetrics> { @Override @ThreadSafe(enableChecks = false) public boolean getSnapshot(CpuMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); try { ProcFileReader reader = mProcFileReader.get(); if (reader == null) { reader = new ProcFileReader(getPath()); mProcFileReader.set(reader); } reader.reset(); if (!reader.isValid()) { return false; } int index = 0; while (index < PROC_USER_TIME_FIELD) { reader.skipSpaces(); index++; } snapshot.userTimeS = readField(reader); snapshot.systemTimeS = readField(reader); snapshot.childUserTimeS = readField(reader); snapshot.childSystemTimeS = readField(reader); } catch (ProcFileReader.ParseException pe) { SystemMetricsLogger.wtf(TAG, "Unable to parse CPU time field", pe); return false; } if (mLastSnapshot.get() == null) { mLastSnapshot.set(new CpuMetrics()); } CpuMetrics lastSnapshot = mLastSnapshot.get(); if (Double.compare(snapshot.userTimeS, lastSnapshot.userTimeS) < 0 || Double.compare(snapshot.systemTimeS, lastSnapshot.systemTimeS) < 0 || Double.compare(snapshot.childUserTimeS, lastSnapshot.childUserTimeS) < 0 || Double.compare(snapshot.childSystemTimeS, lastSnapshot.childSystemTimeS) < 0) { SystemMetricsLogger.wtf( TAG, "Cpu Time Decreased from " + lastSnapshot.toString() + " to " + snapshot.toString()); return false; } lastSnapshot.set(snapshot); return true; } CpuMetricsCollector(); @Override @ThreadSafe(enableChecks = false) boolean getSnapshot(CpuMetrics snapshot); @Override CpuMetrics createMetrics();  }