Program: 192620
Project: 192620
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){
  if (DEBUG_ENERGY) {
    Slog.d(TAG,"String_Node_Str");
  }
  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));
      if (entry.rxBytes != 0) {
        u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA,entry.rxBytes,entry.rxPackets);
        mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(entry.rxBytes);
        mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(entry.rxPackets);
        rxPackets.put(u.getUid(),entry.rxPackets);
        totalRxPackets+=entry.rxPackets;
      }
      if (entry.txBytes != 0) {
        u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA,entry.txBytes,entry.txPackets);
        mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(entry.txBytes);
        mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(entry.txPackets);
        txPackets.put(u.getUid(),entry.txPackets);
        totalTxPackets+=entry.txPackets;
      }
    }
  }
  if (info != null) {
    mHasWifiReporting=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;
    long leftOverTxTimeMs=txTimeMs;
    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");
    }
    if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
      Slog.d(TAG,"String_Node_Str" + totalScanTimeMs + "String_Node_Str"+ txTimeMs+ "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);
        long scanRxTimeSinceMarkMs=scanTimeSinceMarkMs;
        long scanTxTimeSinceMarkMs=scanTimeSinceMarkMs;
        if (totalScanTimeMs > rxTimeMs) {
          scanRxTimeSinceMarkMs=(rxTimeMs * scanRxTimeSinceMarkMs) / totalScanTimeMs;
        }
        if (totalScanTimeMs > txTimeMs) {
          scanTxTimeSinceMarkMs=(txTimeMs * scanTxTimeSinceMarkMs) / totalScanTimeMs;
        }
        if (DEBUG_ENERGY) {
          Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ scanRxTimeSinceMarkMs+ "String_Node_Str"+ scanTxTimeSinceMarkMs+ "String_Node_Str");
        }
        ControllerActivityCounterImpl activityCounter=uid.getOrCreateWifiControllerActivityLocked();
        activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
        activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
        leftOverRxTimeMs-=scanRxTimeSinceMarkMs;
        leftOverTxTimeMs-=scanTxTimeSinceMarkMs;
      }
      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.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter().addCountLocked(myIdleTimeMs);
      }
    }
    if (DEBUG_ENERGY) {
      Slog.d(TAG,"String_Node_Str" + leftOverRxTimeMs + "String_Node_Str");
      Slog.d(TAG,"String_Node_Str" + leftOverTxTimeMs + "String_Node_Str");
    }
    for (int i=0; i < txPackets.size(); i++) {
      final Uid uid=getUidStatsLocked(txPackets.keyAt(i));
      final long myTxTimeMs=(txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
      if (DEBUG_ENERGY) {
        Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ myTxTimeMs+ "String_Node_Str");
      }
      uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0].addCountLocked(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.getOrCreateWifiControllerActivityLocked().getRxTimeCounter().addCountLocked(myRxTimeMs);
    }
    mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
    mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
    mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
    final double opVolt=mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
    if (opVolt != 0) {
      mWifiActivity.getPowerCounter().addCountLocked((long)(info.getControllerEnergyUsed() / opVolt));
    }
  }
}

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){
  if (DEBUG_ENERGY) {
    Slog.d(TAG,"String_Node_Str");
  }
  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));
      if (entry.rxBytes != 0) {
        u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA,entry.rxBytes,entry.rxPackets);
        mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(entry.rxBytes);
        mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(entry.rxPackets);
        rxPackets.put(u.getUid(),entry.rxPackets);
        totalRxPackets+=entry.rxPackets;
      }
      if (entry.txBytes != 0) {
        u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA,entry.txBytes,entry.txPackets);
        mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(entry.txBytes);
        mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(entry.txPackets);
        txPackets.put(u.getUid(),entry.txPackets);
        totalTxPackets+=entry.txPackets;
      }
    }
  }
  if (info != null) {
    mHasWifiReporting=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;
    long leftOverTxTimeMs=txTimeMs;
    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");
    }
    if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
      Slog.d(TAG,"String_Node_Str" + totalScanTimeMs + "String_Node_Str"+ txTimeMs+ "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);
        long scanRxTimeSinceMarkMs=scanTimeSinceMarkMs;
        long scanTxTimeSinceMarkMs=scanTimeSinceMarkMs;
        if (totalScanTimeMs > rxTimeMs) {
          scanRxTimeSinceMarkMs=(rxTimeMs * scanRxTimeSinceMarkMs) / totalScanTimeMs;
        }
        if (totalScanTimeMs > txTimeMs) {
          scanTxTimeSinceMarkMs=(txTimeMs * scanTxTimeSinceMarkMs) / totalScanTimeMs;
        }
        if (DEBUG_ENERGY) {
          Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ scanRxTimeSinceMarkMs+ "String_Node_Str"+ scanTxTimeSinceMarkMs+ "String_Node_Str");
        }
        ControllerActivityCounterImpl activityCounter=uid.getOrCreateWifiControllerActivityLocked();
        activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
        activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
        leftOverRxTimeMs-=scanRxTimeSinceMarkMs;
        leftOverTxTimeMs-=scanTxTimeSinceMarkMs;
      }
      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.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter().addCountLocked(myIdleTimeMs);
      }
    }
    if (DEBUG_ENERGY) {
      Slog.d(TAG,"String_Node_Str" + leftOverRxTimeMs + "String_Node_Str");
      Slog.d(TAG,"String_Node_Str" + leftOverTxTimeMs + "String_Node_Str");
    }
    for (int i=0; i < txPackets.size(); i++) {
      final Uid uid=getUidStatsLocked(txPackets.keyAt(i));
      final long myTxTimeMs=(txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
      if (DEBUG_ENERGY) {
        Slog.d(TAG,"String_Node_Str" + uid.getUid() + "String_Node_Str"+ myTxTimeMs+ "String_Node_Str");
      }
      uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0].addCountLocked(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.getOrCreateWifiControllerActivityLocked().getRxTimeCounter().addCountLocked(myRxTimeMs);
    }
    mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
    mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
    mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
    final double opVolt=mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
    if (opVolt != 0) {
      mWifiActivity.getPowerCounter().addCountLocked((long)(info.getControllerEnergyUsed() / opVolt));
    }
  }
}