Program: 195334
Project: 195334
Buggy source:
/** 
 * Distribute WiFi energy info and network traffic to apps.
 * @param info The energy information from the WiFi controller.
 */
public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info){
  final long elapsedRealtimeMs=SystemClock.elapsedRealtime();
  NetworkStats delta=null;
  try {
    if (!ArrayUtils.isEmpty(mWifiIfaces)) {
      delta=getNetworkStatsDeltaLocked(mWifiIfaces,mWifiNetworkStats);
    }
  }
 catch (  IOException e) {
    Slog.wtf(TAG,"String_Node_Str",e);
    return;
  }
  if (!mOnBatteryInternal) {
    return;
  }
  SparseLongArray rxPackets=new SparseLongArray();
  SparseLongArray txPackets=new SparseLongArray();
  long totalTxPackets=0;
  long totalRxPackets=0;
  if (delta != null) {
    final int size=delta.size();
    for (int i=0; i < size; i++) {
      final NetworkStats.Entry entry=delta.getValues(i,mTmpNetworkStatsEntry);
      if (DEBUG_ENERGY) {
        Slog.d(TAG,"String_Node_Str" + entry.uid + "String_Node_Str"+ entry.rxBytes+ "String_Node_Str"+ entry.txBytes+ "String_Node_Str"+ entry.rxPackets+ "String_Node_Str"+ entry.txPackets);
      }
      if (entry.rxBytes == 0 || entry.txBytes == 0) {
        continue;
      }
      final Uid u=getUidStatsLocked(mapUid(entry.uid));
      u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA,entry.rxBytes,entry.rxPackets);
      u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA,entry.txBytes,entry.txPackets);
      rxPackets.put(u.getUid(),entry.rxPackets);
      txPackets.put(u.getUid(),entry.txPackets);
      totalRxPackets+=entry.rxPackets;
      totalTxPackets+=entry.txPackets;
      mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(entry.rxBytes);
      mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(entry.txBytes);
      mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(entry.rxPackets);
      mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(entry.txPackets);
    }
  }
  if (info != null) {
    final long txTimeMs=info.getControllerTxTimeMillis();
    final long rxTimeMs=info.getControllerRxTimeMillis();
    final long idleTimeMs=info.getControllerIdleTimeMillis();
    final long totalTimeMs=txTimeMs + rxTimeMs + idleTimeMs;
    long leftOverRxTimeMs=rxTimeMs;
    if (DEBUG_ENERGY) {
      Slog.d(TAG,"String_Node_Str");
      Slog.d(TAG,"String_Node_Str" + txTimeMs + "String_Node_Str");
      Slog.d(TAG,"String_Node_Str" + rxTimeMs + "String_Node_Str");
      Slog.d(TAG,"String_Node_Str" + idleTimeMs + "String_Node_Str");
      Slog.d(TAG,"String_Node_Str" + totalTimeMs + "String_Node_Str");
    }
    long totalWifiLockTimeMs=0;
    long totalScanTimeMs=0;
    final int uidStatsSize=mUidStats.size();
    for (int i=0; i < uidStatsSize; i++) {
      final Uid uid=mUidStats.valueAt(i);
      totalScanTimeMs+=uid.mWifiScanTimer.getTimeSinceMarkLocked(elapsedRealtimeMs * 1000) / 1000;
      totalWifiLockTimeMs+=uid.mFullWifiLockTimer.getTimeSinceMarkLocked(elapsedRealtimeMs * 1000) / 1000;
    }
    if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
      Slog.d(TAG,"String_Node_Str" + totalScanTimeMs + "String_Node_Str"+ rxTimeMs+ "String_Node_Str");
    }
    for (int i=0; i < uidStatsSize; i++) {
      final Uid uid=mUidStats.valueAt(i);
      long scanTimeSinceMarkMs=uid.mWifiScanTimer.getTimeSinceMarkLocked(elapsedRealtimeMs * 1000) / 1000;
      if (scanTimeSinceMarkMs > 0) {
        uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
        if (totalScanTimeMs > rxTimeMs) {
          scanTimeSinceMarkMs=(rxTimeMs * scanTimeSinceMarkMs) / totalScanTimeMs;
        }
        if (DEBUG_ENERGY) {
          Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ scanTimeSinceMarkMs+ "String_Node_Str");
        }
        uid.noteWifiControllerActivityLocked(CONTROLLER_RX_TIME,scanTimeSinceMarkMs);
        leftOverRxTimeMs-=scanTimeSinceMarkMs;
      }
      final long wifiLockTimeSinceMarkMs=uid.mFullWifiLockTimer.getTimeSinceMarkLocked(elapsedRealtimeMs * 1000) / 1000;
      if (wifiLockTimeSinceMarkMs > 0) {
        uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
        final long myIdleTimeMs=(wifiLockTimeSinceMarkMs * idleTimeMs) / totalWifiLockTimeMs;
        if (DEBUG_ENERGY) {
          Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ myIdleTimeMs+ "String_Node_Str");
        }
        uid.noteWifiControllerActivityLocked(CONTROLLER_IDLE_TIME,myIdleTimeMs);
      }
    }
    if (DEBUG_ENERGY) {
      Slog.d(TAG,"String_Node_Str" + leftOverRxTimeMs + "String_Node_Str");
    }
    for (int i=0; i < txPackets.size(); i++) {
      final Uid uid=getUidStatsLocked(txPackets.keyAt(i));
      final long myTxTimeMs=(txPackets.valueAt(i) * txTimeMs) / totalTxPackets;
      if (DEBUG_ENERGY) {
        Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ myTxTimeMs+ "String_Node_Str");
      }
      uid.noteWifiControllerActivityLocked(CONTROLLER_TX_TIME,myTxTimeMs);
    }
    for (int i=0; i < rxPackets.size(); i++) {
      final Uid uid=getUidStatsLocked(rxPackets.keyAt(i));
      final long myRxTimeMs=(rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
      if (DEBUG_ENERGY) {
        Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ myRxTimeMs+ "String_Node_Str");
      }
      uid.noteWifiControllerActivityLocked(CONTROLLER_RX_TIME,myRxTimeMs);
    }
    mWifiActivityCounters[CONTROLLER_RX_TIME].addCountLocked(info.getControllerRxTimeMillis());
    mWifiActivityCounters[CONTROLLER_TX_TIME].addCountLocked(info.getControllerTxTimeMillis());
    mWifiActivityCounters[CONTROLLER_IDLE_TIME].addCountLocked(info.getControllerIdleTimeMillis());
    final double powerDrainMaMs;
    if (mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) == 0) {
      powerDrainMaMs=0.0;
    }
 else {
      powerDrainMaMs=info.getControllerEnergyUsed() / mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE);
    }
    mWifiActivityCounters[CONTROLLER_POWER_DRAIN].addCountLocked((long)powerDrainMaMs);
  }
}

Fixed version (ground truth):
/** 
 * Distribute WiFi energy info and network traffic to apps.
 * @param info The energy information from the WiFi controller.
 */
public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info){
  final long elapsedRealtimeMs=SystemClock.elapsedRealtime();
  NetworkStats delta=null;
  try {
    if (!ArrayUtils.isEmpty(mWifiIfaces)) {
      delta=getNetworkStatsDeltaLocked(mWifiIfaces,mWifiNetworkStats);
    }
  }
 catch (  IOException e) {
    Slog.wtf(TAG,"String_Node_Str",e);
    return;
  }
  if (!mOnBatteryInternal) {
    return;
  }
  SparseLongArray rxPackets=new SparseLongArray();
  SparseLongArray txPackets=new SparseLongArray();
  long totalTxPackets=0;
  long totalRxPackets=0;
  if (delta != null) {
    final int size=delta.size();
    for (int i=0; i < size; i++) {
      final NetworkStats.Entry entry=delta.getValues(i,mTmpNetworkStatsEntry);
      if (DEBUG_ENERGY) {
        Slog.d(TAG,"String_Node_Str" + entry.uid + "String_Node_Str"+ entry.rxBytes+ "String_Node_Str"+ entry.txBytes+ "String_Node_Str"+ entry.rxPackets+ "String_Node_Str"+ entry.txPackets);
      }
      if (entry.rxBytes == 0 || entry.txBytes == 0) {
        continue;
      }
      final Uid u=getUidStatsLocked(mapUid(entry.uid));
      u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA,entry.rxBytes,entry.rxPackets);
      u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA,entry.txBytes,entry.txPackets);
      rxPackets.put(u.getUid(),entry.rxPackets);
      txPackets.put(u.getUid(),entry.txPackets);
      totalRxPackets+=entry.rxPackets;
      totalTxPackets+=entry.txPackets;
      mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(entry.rxBytes);
      mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(entry.txBytes);
      mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(entry.rxPackets);
      mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(entry.txPackets);
    }
  }
  if (info != null) {
    mHasWifiEnergyReporting=true;
    final long txTimeMs=info.getControllerTxTimeMillis();
    final long rxTimeMs=info.getControllerRxTimeMillis();
    final long idleTimeMs=info.getControllerIdleTimeMillis();
    final long totalTimeMs=txTimeMs + rxTimeMs + idleTimeMs;
    long leftOverRxTimeMs=rxTimeMs;
    if (DEBUG_ENERGY) {
      Slog.d(TAG,"String_Node_Str");
      Slog.d(TAG,"String_Node_Str" + txTimeMs + "String_Node_Str");
      Slog.d(TAG,"String_Node_Str" + rxTimeMs + "String_Node_Str");
      Slog.d(TAG,"String_Node_Str" + idleTimeMs + "String_Node_Str");
      Slog.d(TAG,"String_Node_Str" + totalTimeMs + "String_Node_Str");
    }
    long totalWifiLockTimeMs=0;
    long totalScanTimeMs=0;
    final int uidStatsSize=mUidStats.size();
    for (int i=0; i < uidStatsSize; i++) {
      final Uid uid=mUidStats.valueAt(i);
      totalScanTimeMs+=uid.mWifiScanTimer.getTimeSinceMarkLocked(elapsedRealtimeMs * 1000) / 1000;
      totalWifiLockTimeMs+=uid.mFullWifiLockTimer.getTimeSinceMarkLocked(elapsedRealtimeMs * 1000) / 1000;
    }
    if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
      Slog.d(TAG,"String_Node_Str" + totalScanTimeMs + "String_Node_Str"+ rxTimeMs+ "String_Node_Str");
    }
    for (int i=0; i < uidStatsSize; i++) {
      final Uid uid=mUidStats.valueAt(i);
      long scanTimeSinceMarkMs=uid.mWifiScanTimer.getTimeSinceMarkLocked(elapsedRealtimeMs * 1000) / 1000;
      if (scanTimeSinceMarkMs > 0) {
        uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
        if (totalScanTimeMs > rxTimeMs) {
          scanTimeSinceMarkMs=(rxTimeMs * scanTimeSinceMarkMs) / totalScanTimeMs;
        }
        if (DEBUG_ENERGY) {
          Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ scanTimeSinceMarkMs+ "String_Node_Str");
        }
        uid.noteWifiControllerActivityLocked(CONTROLLER_RX_TIME,scanTimeSinceMarkMs);
        leftOverRxTimeMs-=scanTimeSinceMarkMs;
      }
      final long wifiLockTimeSinceMarkMs=uid.mFullWifiLockTimer.getTimeSinceMarkLocked(elapsedRealtimeMs * 1000) / 1000;
      if (wifiLockTimeSinceMarkMs > 0) {
        uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
        final long myIdleTimeMs=(wifiLockTimeSinceMarkMs * idleTimeMs) / totalWifiLockTimeMs;
        if (DEBUG_ENERGY) {
          Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ myIdleTimeMs+ "String_Node_Str");
        }
        uid.noteWifiControllerActivityLocked(CONTROLLER_IDLE_TIME,myIdleTimeMs);
      }
    }
    if (DEBUG_ENERGY) {
      Slog.d(TAG,"String_Node_Str" + leftOverRxTimeMs + "String_Node_Str");
    }
    for (int i=0; i < txPackets.size(); i++) {
      final Uid uid=getUidStatsLocked(txPackets.keyAt(i));
      final long myTxTimeMs=(txPackets.valueAt(i) * txTimeMs) / totalTxPackets;
      if (DEBUG_ENERGY) {
        Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ myTxTimeMs+ "String_Node_Str");
      }
      uid.noteWifiControllerActivityLocked(CONTROLLER_TX_TIME,myTxTimeMs);
    }
    for (int i=0; i < rxPackets.size(); i++) {
      final Uid uid=getUidStatsLocked(rxPackets.keyAt(i));
      final long myRxTimeMs=(rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
      if (DEBUG_ENERGY) {
        Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ myRxTimeMs+ "String_Node_Str");
      }
      uid.noteWifiControllerActivityLocked(CONTROLLER_RX_TIME,myRxTimeMs);
    }
    mWifiActivityCounters[CONTROLLER_RX_TIME].addCountLocked(info.getControllerRxTimeMillis());
    mWifiActivityCounters[CONTROLLER_TX_TIME].addCountLocked(info.getControllerTxTimeMillis());
    mWifiActivityCounters[CONTROLLER_IDLE_TIME].addCountLocked(info.getControllerIdleTimeMillis());
    final double powerDrainMaMs;
    if (mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) == 0) {
      powerDrainMaMs=0.0;
    }
 else {
      powerDrainMaMs=info.getControllerEnergyUsed() / mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE);
    }
    mWifiActivityCounters[CONTROLLER_POWER_DRAIN].addCountLocked((long)powerDrainMaMs);
  }
}