public boolean getSnapshot()

in metrics/src/main/java/com/facebook/battery/metrics/cpu/CpuMetricsCollector.java [47:94]


  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;
  }