@Test public void testRealProcfileRepeat() throws Exception { String io = "rchar: 100\n" + "wchar: 101\n" + "syscr: 1000\n" + "syscw: 1001\n" + "read_bytes: 500\n" + "write_bytes: 501\n" + "cancelled_write_bytes: 10\n"; String stat = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46"; DiskMetricsCollectorWithProcFile collector = new DiskMetricsCollectorWithProcFile().setPath(createFile(io), createFile(stat)); DiskMetrics snapshot = new DiskMetrics(); assertThat(collector.getSnapshot(snapshot)).isTrue(); assertThat(collector.getSnapshot(snapshot)).isTrue(); assertThat(collector.getSnapshot(snapshot)).isTrue(); assertThat(snapshot.rcharBytes).isEqualTo(100); assertThat(snapshot.wcharBytes).isEqualTo(101); assertThat(snapshot.syscrCount).isEqualTo(1000); assertThat(snapshot.syscwCount).isEqualTo(1001); assertThat(snapshot.readBytes).isEqualTo(500); assertThat(snapshot.writeBytes).isEqualTo(501); assertThat(snapshot.cancelledWriteBytes).isEqualTo(10); assertThat(snapshot.majorFaults).isEqualTo(12); assertThat(snapshot.blkIoTicks).isEqualTo(42); }
@Override @ThreadSafe(enableChecks = false) public synchronized boolean getSnapshot(DiskMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); if (!mIsEnabled) { return false; } try { ProcFileReader ioReader = mProcIoFileReader.get(); if (ioReader == null) { ioReader = new ProcFileReader(getIoFilePath()); mProcIoFileReader.set(ioReader); } ioReader.reset(); if (!ioReader.isValid()) { return false; } snapshot.rcharBytes = readField(ioReader); snapshot.wcharBytes = readField(ioReader); snapshot.syscrCount = readField(ioReader); snapshot.syscwCount = readField(ioReader); snapshot.readBytes = readField(ioReader); snapshot.writeBytes = readField(ioReader); snapshot.cancelledWriteBytes = readField(ioReader); ProcFileReader statReader = mProcStatFileReader.get(); if (statReader == null) { statReader = new ProcFileReader(getStatFilePath()); mProcStatFileReader.set(statReader); } statReader.reset(); if (!statReader.isValid()) { return false; } int index = 0; while (index < PROC_STAT_MAJOR_FAULTS_FIELD) { statReader.skipSpaces(); index++; } snapshot.majorFaults = statReader.readNumber(); while (index < PROC_STAT_BLKIO_TICKS_FIELD) { statReader.skipSpaces(); index++; } snapshot.blkIoTicks = statReader.readNumber(); } catch (ProcFileReader.ParseException pe) { SystemMetricsLogger.wtf(TAG, "Unable to parse disk field", pe); return false; } return true; }
DiskMetricsCollector extends SystemMetricsCollector<DiskMetrics> { @Override @ThreadSafe(enableChecks = false) public synchronized boolean getSnapshot(DiskMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); if (!mIsEnabled) { return false; } try { ProcFileReader ioReader = mProcIoFileReader.get(); if (ioReader == null) { ioReader = new ProcFileReader(getIoFilePath()); mProcIoFileReader.set(ioReader); } ioReader.reset(); if (!ioReader.isValid()) { return false; } snapshot.rcharBytes = readField(ioReader); snapshot.wcharBytes = readField(ioReader); snapshot.syscrCount = readField(ioReader); snapshot.syscwCount = readField(ioReader); snapshot.readBytes = readField(ioReader); snapshot.writeBytes = readField(ioReader); snapshot.cancelledWriteBytes = readField(ioReader); ProcFileReader statReader = mProcStatFileReader.get(); if (statReader == null) { statReader = new ProcFileReader(getStatFilePath()); mProcStatFileReader.set(statReader); } statReader.reset(); if (!statReader.isValid()) { return false; } int index = 0; while (index < PROC_STAT_MAJOR_FAULTS_FIELD) { statReader.skipSpaces(); index++; } snapshot.majorFaults = statReader.readNumber(); while (index < PROC_STAT_BLKIO_TICKS_FIELD) { statReader.skipSpaces(); index++; } snapshot.blkIoTicks = statReader.readNumber(); } catch (ProcFileReader.ParseException pe) { SystemMetricsLogger.wtf(TAG, "Unable to parse disk field", pe); return false; } return true; } }
DiskMetricsCollector extends SystemMetricsCollector<DiskMetrics> { @Override @ThreadSafe(enableChecks = false) public synchronized boolean getSnapshot(DiskMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); if (!mIsEnabled) { return false; } try { ProcFileReader ioReader = mProcIoFileReader.get(); if (ioReader == null) { ioReader = new ProcFileReader(getIoFilePath()); mProcIoFileReader.set(ioReader); } ioReader.reset(); if (!ioReader.isValid()) { return false; } snapshot.rcharBytes = readField(ioReader); snapshot.wcharBytes = readField(ioReader); snapshot.syscrCount = readField(ioReader); snapshot.syscwCount = readField(ioReader); snapshot.readBytes = readField(ioReader); snapshot.writeBytes = readField(ioReader); snapshot.cancelledWriteBytes = readField(ioReader); ProcFileReader statReader = mProcStatFileReader.get(); if (statReader == null) { statReader = new ProcFileReader(getStatFilePath()); mProcStatFileReader.set(statReader); } statReader.reset(); if (!statReader.isValid()) { return false; } int index = 0; while (index < PROC_STAT_MAJOR_FAULTS_FIELD) { statReader.skipSpaces(); index++; } snapshot.majorFaults = statReader.readNumber(); while (index < PROC_STAT_BLKIO_TICKS_FIELD) { statReader.skipSpaces(); index++; } snapshot.blkIoTicks = statReader.readNumber(); } catch (ProcFileReader.ParseException pe) { SystemMetricsLogger.wtf(TAG, "Unable to parse disk field", pe); return false; } return true; }  }
DiskMetricsCollector extends SystemMetricsCollector<DiskMetrics> { @Override @ThreadSafe(enableChecks = false) public synchronized boolean getSnapshot(DiskMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); if (!mIsEnabled) { return false; } try { ProcFileReader ioReader = mProcIoFileReader.get(); if (ioReader == null) { ioReader = new ProcFileReader(getIoFilePath()); mProcIoFileReader.set(ioReader); } ioReader.reset(); if (!ioReader.isValid()) { return false; } snapshot.rcharBytes = readField(ioReader); snapshot.wcharBytes = readField(ioReader); snapshot.syscrCount = readField(ioReader); snapshot.syscwCount = readField(ioReader); snapshot.readBytes = readField(ioReader); snapshot.writeBytes = readField(ioReader); snapshot.cancelledWriteBytes = readField(ioReader); ProcFileReader statReader = mProcStatFileReader.get(); if (statReader == null) { statReader = new ProcFileReader(getStatFilePath()); mProcStatFileReader.set(statReader); } statReader.reset(); if (!statReader.isValid()) { return false; } int index = 0; while (index < PROC_STAT_MAJOR_FAULTS_FIELD) { statReader.skipSpaces(); index++; } snapshot.majorFaults = statReader.readNumber(); while (index < PROC_STAT_BLKIO_TICKS_FIELD) { statReader.skipSpaces(); index++; } snapshot.blkIoTicks = statReader.readNumber(); } catch (ProcFileReader.ParseException pe) { SystemMetricsLogger.wtf(TAG, "Unable to parse disk field", pe); return false; } return true; }  @Override @ThreadSafe(enableChecks = false) synchronized boolean getSnapshot(DiskMetrics snapshot); synchronized void enable(); @Override DiskMetrics createMetrics(); static boolean isSupported(); }
DiskMetricsCollector extends SystemMetricsCollector<DiskMetrics> { @Override @ThreadSafe(enableChecks = false) public synchronized boolean getSnapshot(DiskMetrics snapshot) { checkNotNull(snapshot, "Null value passed to getSnapshot!"); if (!mIsEnabled) { return false; } try { ProcFileReader ioReader = mProcIoFileReader.get(); if (ioReader == null) { ioReader = new ProcFileReader(getIoFilePath()); mProcIoFileReader.set(ioReader); } ioReader.reset(); if (!ioReader.isValid()) { return false; } snapshot.rcharBytes = readField(ioReader); snapshot.wcharBytes = readField(ioReader); snapshot.syscrCount = readField(ioReader); snapshot.syscwCount = readField(ioReader); snapshot.readBytes = readField(ioReader); snapshot.writeBytes = readField(ioReader); snapshot.cancelledWriteBytes = readField(ioReader); ProcFileReader statReader = mProcStatFileReader.get(); if (statReader == null) { statReader = new ProcFileReader(getStatFilePath()); mProcStatFileReader.set(statReader); } statReader.reset(); if (!statReader.isValid()) { return false; } int index = 0; while (index < PROC_STAT_MAJOR_FAULTS_FIELD) { statReader.skipSpaces(); index++; } snapshot.majorFaults = statReader.readNumber(); while (index < PROC_STAT_BLKIO_TICKS_FIELD) { statReader.skipSpaces(); index++; } snapshot.blkIoTicks = statReader.readNumber(); } catch (ProcFileReader.ParseException pe) { SystemMetricsLogger.wtf(TAG, "Unable to parse disk field", pe); return false; } return true; }  @Override @ThreadSafe(enableChecks = false) synchronized boolean getSnapshot(DiskMetrics snapshot); synchronized void enable(); @Override DiskMetrics createMetrics(); static boolean isSupported();  }