Program: 197537
Project: 197537
Buggy source:
void updateCpuStatsNow(){
synchronized (mProcessCpuThread) {
    mProcessCpuMutexFree.set(false);
    final long now=SystemClock.uptimeMillis();
    boolean haveNewCpuStats=false;
    if (MONITOR_CPU_USAGE && mLastCpuTime.get() < (now - MONITOR_CPU_MIN_TIME)) {
      mLastCpuTime.set(now);
      haveNewCpuStats=true;
      mProcessCpuTracker.update();
      if ("String_Node_Str".equals(SystemProperties.get("String_Node_Str"))) {
        int user=mProcessCpuTracker.getLastUserTime();
        int system=mProcessCpuTracker.getLastSystemTime();
        int iowait=mProcessCpuTracker.getLastIoWaitTime();
        int irq=mProcessCpuTracker.getLastIrqTime();
        int softIrq=mProcessCpuTracker.getLastSoftIrqTime();
        int idle=mProcessCpuTracker.getLastIdleTime();
        int total=user + system + iowait+ irq+ softIrq+ idle;
        if (total == 0)         total=1;
        EventLog.writeEvent(EventLogTags.CPU,((user + system + iowait+ irq+ softIrq) * 100) / total,(user * 100) / total,(system * 100) / total,(iowait * 100) / total,(irq * 100) / total,(softIrq * 100) / total);
      }
    }
    long[] cpuSpeedTimes=mProcessCpuTracker.getLastCpuSpeedTimes();
    final BatteryStatsImpl bstats=mBatteryStatsService.getActiveStatistics();
synchronized (bstats) {
synchronized (mPidsSelfLocked) {
        if (haveNewCpuStats) {
          if (mOnBattery) {
            int perc=bstats.startAddingCpuLocked();
            int totalUTime=0;
            int totalSTime=0;
            final int N=mProcessCpuTracker.countStats();
            for (int i=0; i < N; i++) {
              ProcessCpuTracker.Stats st=mProcessCpuTracker.getStats(i);
              if (!st.working) {
                continue;
              }
              ProcessRecord pr=mPidsSelfLocked.get(st.pid);
              int otherUTime=(st.rel_utime * perc) / 100;
              int otherSTime=(st.rel_stime * perc) / 100;
              totalUTime+=otherUTime;
              totalSTime+=otherSTime;
              if (pr != null) {
                BatteryStatsImpl.Uid.Proc ps=pr.curProcBatteryStats;
                if (ps == null || !ps.isActive()) {
                  pr.curProcBatteryStats=ps=bstats.getProcessStatsLocked(pr.info.uid,pr.processName);
                }
                ps.addCpuTimeLocked(st.rel_utime - otherUTime,st.rel_stime - otherSTime);
                ps.addSpeedStepTimes(cpuSpeedTimes);
                pr.curCpuTime+=(st.rel_utime + st.rel_stime) * 10;
              }
 else {
                BatteryStatsImpl.Uid.Proc ps=st.batteryStats;
                if (ps == null || !ps.isActive()) {
                  st.batteryStats=ps=bstats.getProcessStatsLocked(bstats.mapUid(st.uid),st.name);
                }
                ps.addCpuTimeLocked(st.rel_utime - otherUTime,st.rel_stime - otherSTime);
                ps.addSpeedStepTimes(cpuSpeedTimes);
              }
            }
            bstats.finishAddingCpuLocked(perc,totalUTime,totalSTime,cpuSpeedTimes);
          }
        }
      }
      if (mLastWriteTime < (now - BATTERY_STATS_TIME)) {
        mLastWriteTime=now;
        mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
      }
    }
  }
}

Fixed version (ground truth):
void updateCpuStatsNow(){
synchronized (mProcessCpuTracker) {
    mProcessCpuMutexFree.set(false);
    final long now=SystemClock.uptimeMillis();
    boolean haveNewCpuStats=false;
    if (MONITOR_CPU_USAGE && mLastCpuTime.get() < (now - MONITOR_CPU_MIN_TIME)) {
      mLastCpuTime.set(now);
      haveNewCpuStats=true;
      mProcessCpuTracker.update();
      if ("String_Node_Str".equals(SystemProperties.get("String_Node_Str"))) {
        int user=mProcessCpuTracker.getLastUserTime();
        int system=mProcessCpuTracker.getLastSystemTime();
        int iowait=mProcessCpuTracker.getLastIoWaitTime();
        int irq=mProcessCpuTracker.getLastIrqTime();
        int softIrq=mProcessCpuTracker.getLastSoftIrqTime();
        int idle=mProcessCpuTracker.getLastIdleTime();
        int total=user + system + iowait+ irq+ softIrq+ idle;
        if (total == 0)         total=1;
        EventLog.writeEvent(EventLogTags.CPU,((user + system + iowait+ irq+ softIrq) * 100) / total,(user * 100) / total,(system * 100) / total,(iowait * 100) / total,(irq * 100) / total,(softIrq * 100) / total);
      }
    }
    long[] cpuSpeedTimes=mProcessCpuTracker.getLastCpuSpeedTimes();
    final BatteryStatsImpl bstats=mBatteryStatsService.getActiveStatistics();
synchronized (bstats) {
synchronized (mPidsSelfLocked) {
        if (haveNewCpuStats) {
          if (mOnBattery) {
            int perc=bstats.startAddingCpuLocked();
            int totalUTime=0;
            int totalSTime=0;
            final int N=mProcessCpuTracker.countStats();
            for (int i=0; i < N; i++) {
              ProcessCpuTracker.Stats st=mProcessCpuTracker.getStats(i);
              if (!st.working) {
                continue;
              }
              ProcessRecord pr=mPidsSelfLocked.get(st.pid);
              int otherUTime=(st.rel_utime * perc) / 100;
              int otherSTime=(st.rel_stime * perc) / 100;
              totalUTime+=otherUTime;
              totalSTime+=otherSTime;
              if (pr != null) {
                BatteryStatsImpl.Uid.Proc ps=pr.curProcBatteryStats;
                if (ps == null || !ps.isActive()) {
                  pr.curProcBatteryStats=ps=bstats.getProcessStatsLocked(pr.info.uid,pr.processName);
                }
                ps.addCpuTimeLocked(st.rel_utime - otherUTime,st.rel_stime - otherSTime);
                ps.addSpeedStepTimes(cpuSpeedTimes);
                pr.curCpuTime+=(st.rel_utime + st.rel_stime) * 10;
              }
 else {
                BatteryStatsImpl.Uid.Proc ps=st.batteryStats;
                if (ps == null || !ps.isActive()) {
                  st.batteryStats=ps=bstats.getProcessStatsLocked(bstats.mapUid(st.uid),st.name);
                }
                ps.addCpuTimeLocked(st.rel_utime - otherUTime,st.rel_stime - otherSTime);
                ps.addSpeedStepTimes(cpuSpeedTimes);
              }
            }
            bstats.finishAddingCpuLocked(perc,totalUTime,totalSTime,cpuSpeedTimes);
          }
        }
      }
      if (mLastWriteTime < (now - BATTERY_STATS_TIME)) {
        mLastWriteTime=now;
        mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
      }
    }
  }
}