Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b1f04f37 authored by Bookatz's avatar Bookatz
Browse files

Batterystats track background bad ble scan time

Allows tracking ble scan time (total and background) for unoptimized
scans. Whether the scan is unoptimized is provided by the bluetooth
code when calling batterystats.

Bug: 38461344
Test: runtest -x frameworks/base/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java
Test: run cts-dev -m CtsIncidentHostTestCases -t com.android.server.cts.BatteryStatsValidationTest#testUnoptimizedBleScans
Test: cts-tradefed run cts-dev -m CtsDumpsysHostTestCases -t android.dumpsys.cts.BatteryStatsDumpsysTest

Change-Id: I814482ff663424170eac4b413464d24c14a5cf91
parent 61e2b158
Loading
Loading
Loading
Loading
+106 −15
Original line number Original line Diff line number Diff line
@@ -161,6 +161,11 @@ public abstract class BatteryStats implements Parcelable {
     */
     */
    public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
    public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;


    /**
     * A constant indicating a bluetooth scan timer for unoptimized scans.
     */
    public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;

    /**
    /**
     * Include all of the data in the stats, including previously saved data.
     * Include all of the data in the stats, including previously saved data.
     */
     */
@@ -191,8 +196,12 @@ public abstract class BatteryStats implements Parcelable {
     * New in version 21:
     * New in version 21:
     *   - Actual (not just apportioned) Wakelock time is also recorded.
     *   - Actual (not just apportioned) Wakelock time is also recorded.
     *   - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
     *   - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
     *   - BLE scan result count
     *   - CPU frequency time per uid
     * New in version 22:
     *   - BLE scan result background count, BLE unoptimized scan time
     */
     */
    static final String CHECKIN_VERSION = "21";
    static final String CHECKIN_VERSION = "22";


    /**
    /**
     * Old version, we hit 9 and ran out of room, need to remove.
     * Old version, we hit 9 and ran out of room, need to remove.
@@ -565,7 +574,10 @@ public abstract class BatteryStats implements Parcelable {
        public abstract Timer getForegroundActivityTimer();
        public abstract Timer getForegroundActivityTimer();
        public abstract Timer getBluetoothScanTimer();
        public abstract Timer getBluetoothScanTimer();
        public abstract Timer getBluetoothScanBackgroundTimer();
        public abstract Timer getBluetoothScanBackgroundTimer();
        public abstract Timer getBluetoothUnoptimizedScanTimer();
        public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
        public abstract Counter getBluetoothScanResultCounter();
        public abstract Counter getBluetoothScanResultCounter();
        public abstract Counter getBluetoothScanResultBgCounter();


        public abstract long[] getCpuFreqTimes(int which);
        public abstract long[] getCpuFreqTimes(int which);
        public abstract long[] getScreenOffCpuFreqTimes(int which);
        public abstract long[] getScreenOffCpuFreqTimes(int which);
@@ -3429,10 +3441,29 @@ public abstract class BatteryStats implements Parcelable {
                    final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
                    final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
                    final long actualTimeBg = bleTimerBg != null ?
                    final long actualTimeBg = bleTimerBg != null ?
                            bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
                            bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
                    // Result counters
                    final int resultCount = u.getBluetoothScanResultCounter() != null ?
                    final int resultCount = u.getBluetoothScanResultCounter() != null ?
                            u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
                            u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
                    final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
                            u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
                    // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
                    final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
                    final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
                            unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
                    final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
                            unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
                    // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
                    final Timer unoptimizedScanTimerBg =
                            u.getBluetoothUnoptimizedScanBackgroundTimer();
                    final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
                            unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
                    final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
                            unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;

                    dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
                    dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
                            countBg, actualTime, actualTimeBg, resultCount);
                            countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
                            unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
                            unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
                }
                }
            }
            }


@@ -4625,34 +4656,94 @@ public abstract class BatteryStats implements Parcelable {
                    final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
                    final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
                    final long actualTimeMsBg = bleTimerBg != null ?
                    final long actualTimeMsBg = bleTimerBg != null ?
                            bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
                            bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
                    // Result counters
                    final int resultCount = u.getBluetoothScanResultCounter() != null ?
                    final int resultCount = u.getBluetoothScanResultCounter() != null ?
                            u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
                            u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
                    final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
                            u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
                    // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
                    final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
                    final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
                            unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
                    final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
                            unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
                    // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
                    final Timer unoptimizedScanTimerBg =
                            u.getBluetoothUnoptimizedScanBackgroundTimer();
                    final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
                            unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
                    final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
                            unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;


                    sb.setLength(0);
                    sb.setLength(0);
                    sb.append(prefix);
                    sb.append("    ");
                    sb.append("Bluetooth Scan");
                    sb.append(": ");
                    if (actualTimeMs != totalTimeMs) {
                    if (actualTimeMs != totalTimeMs) {
                        sb.append(prefix);
                        sb.append("    Bluetooth Scan (total blamed realtime): ");
                        formatTimeMs(sb, totalTimeMs);
                        formatTimeMs(sb, totalTimeMs);
                        sb.append("blamed realtime, ");
                        sb.append(" (");
                        sb.append(count);
                        sb.append(" times)");
                        if (bleTimer.isRunningLocked()) {
                            sb.append(" (currently running)");
                        }
                        }
                    formatTimeMs(sb, actualTimeMs); // since reset, regardless of 'which'
                        sb.append("\n");
                    sb.append("realtime (");
                    }

                    sb.append(prefix);
                    sb.append("    Bluetooth Scan (total actual realtime): ");
                    formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
                    sb.append(" (");
                    sb.append(count);
                    sb.append(count);
                    sb.append(" times)");
                    sb.append(" times)");
                    if (bleTimer.isRunningLocked()) {
                    if (bleTimer.isRunningLocked()) {
                            sb.append(" (running)");
                            sb.append(" (currently running)");
                    }
                    }
                    if (actualTimeMsBg != 0 || countBg > 0) {
                    sb.append("\n");
                        sb.append(", ");
                    if (actualTimeMsBg > 0 || countBg > 0) {
                        formatTimeMs(sb, actualTimeMsBg); // since reset, regardless of 'which'
                        sb.append(prefix);
                        sb.append("background (");
                        sb.append("    Bluetooth Scan (background realtime): ");
                        formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
                        sb.append(" (");
                        sb.append(countBg);
                        sb.append(countBg);
                        sb.append(" times)");
                        sb.append(" times)");
                        if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
                            sb.append(" (currently running in background)");
                        }
                        sb.append("\n");
                    }
                    }
                    sb.append("; Results count ");

                    sb.append(prefix);
                    sb.append("    Bluetooth Scan Results: ");
                    sb.append(resultCount);
                    sb.append(resultCount);
                    sb.append(" (");
                    sb.append(resultCountBg);
                    sb.append(" in background)");

                    if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
                        sb.append("\n");
                        sb.append(prefix);
                        sb.append("    Unoptimized Bluetooth Scan (realtime): ");
                        formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
                        sb.append(" (max ");
                        formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
                        sb.append(")");
                        if (unoptimizedScanTimer != null
                                && unoptimizedScanTimer.isRunningLocked()) {
                            sb.append(" (currently running unoptimized)");
                        }
                        if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
                            sb.append("\n");
                            sb.append(prefix);
                            sb.append("    Unoptimized Bluetooth Scan (background realtime): ");
                            formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
                            sb.append(" (max ");
                            formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
                            sb.append(")");
                            if (unoptimizedScanTimerBg.isRunningLocked()) {
                                sb.append(" (currently running unoptimized in background)");
                            }
                        }
                    }
                    pw.println(sb.toString());
                    pw.println(sb.toString());
                    uidActivity = true;
                    uidActivity = true;
                }
                }
+1 −1
Original line number Original line Diff line number Diff line
@@ -129,7 +129,7 @@ interface IBatteryStats {
    long getAwakeTimeBattery();
    long getAwakeTimeBattery();
    long getAwakeTimePlugged();
    long getAwakeTimePlugged();


    void noteBleScanStarted(in WorkSource ws);
    void noteBleScanStarted(in WorkSource ws, boolean isUnoptimized);
    void noteBleScanStopped(in WorkSource ws);
    void noteBleScanStopped(in WorkSource ws);
    void noteResetBleScan();
    void noteResetBleScan();
    void noteBleScanResults(in WorkSource ws, int numNewResults);
    void noteBleScanResults(in WorkSource ws, int numNewResults);
+109 −5
Original line number Original line Diff line number Diff line
@@ -4821,7 +4821,7 @@ public class BatteryStatsImpl extends BatteryStats {
        }
        }
    }
    }


    private void noteBluetoothScanStartedLocked(int uid) {
    private void noteBluetoothScanStartedLocked(int uid, boolean isUnoptimized) {
        uid = mapUid(uid);
        uid = mapUid(uid);
        final long elapsedRealtime = mClocks.elapsedRealtime();
        final long elapsedRealtime = mClocks.elapsedRealtime();
        final long uptime = mClocks.uptimeMillis();
        final long uptime = mClocks.uptimeMillis();
@@ -4833,13 +4833,13 @@ public class BatteryStatsImpl extends BatteryStats {
            mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
            mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
        }
        }
        mBluetoothScanNesting++;
        mBluetoothScanNesting++;
        getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime);
        getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime, isUnoptimized);
    }
    }


    public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws) {
    public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
        final int N = ws.size();
        final int N = ws.size();
        for (int i = 0; i < N; i++) {
        for (int i = 0; i < N; i++) {
            noteBluetoothScanStartedLocked(ws.get(i));
            noteBluetoothScanStartedLocked(ws.get(i), isUnoptimized);
        }
        }
    }
    }


@@ -5611,7 +5611,9 @@ public class BatteryStatsImpl extends BatteryStats {
        /** Total time spent by the uid holding any partial wakelocks. */
        /** Total time spent by the uid holding any partial wakelocks. */
        DualTimer mAggregatedPartialWakelockTimer;
        DualTimer mAggregatedPartialWakelockTimer;
        DualTimer mBluetoothScanTimer;
        DualTimer mBluetoothScanTimer;
        DualTimer mBluetoothUnoptimizedScanTimer;
        Counter mBluetoothScanResultCounter;
        Counter mBluetoothScanResultCounter;
        Counter mBluetoothScanResultBgCounter;


        int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
        int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
        StopwatchTimer[] mProcessStateTimer;
        StopwatchTimer[] mProcessStateTimer;
@@ -6096,20 +6098,41 @@ public class BatteryStatsImpl extends BatteryStats {
            return mBluetoothScanTimer;
            return mBluetoothScanTimer;
        }
        }


        public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs) {
        public DualTimer createBluetoothUnoptimizedScanTimerLocked() {
            if (mBluetoothUnoptimizedScanTimer == null) {
                mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
                        BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
                        mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
            }
            return mBluetoothUnoptimizedScanTimer;
        }

        public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
            createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
            createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
            if (isUnoptimized) {
                createBluetoothUnoptimizedScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
            }
        }
        }


        public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
        public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
            if (mBluetoothScanTimer != null) {
            if (mBluetoothScanTimer != null) {
                mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
                mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
            }
            }
            // In the ble code, a scan cannot change types and nested starts are not possible.
            // So if an unoptimizedScan is running, it is now being stopped.
            if (mBluetoothUnoptimizedScanTimer != null
                    && mBluetoothUnoptimizedScanTimer.isRunningLocked()) {
                mBluetoothUnoptimizedScanTimer.stopRunningLocked(elapsedRealtimeMs);
            }
        }
        }


        public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
        public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
            if (mBluetoothScanTimer != null) {
            if (mBluetoothScanTimer != null) {
                mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
                mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
            }
            }
            if (mBluetoothUnoptimizedScanTimer != null) {
                mBluetoothUnoptimizedScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
            }
        }
        }


        public Counter createBluetoothScanResultCounterLocked() {
        public Counter createBluetoothScanResultCounterLocked() {
@@ -6119,8 +6142,17 @@ public class BatteryStatsImpl extends BatteryStats {
            return mBluetoothScanResultCounter;
            return mBluetoothScanResultCounter;
        }
        }


        public Counter createBluetoothScanResultBgCounterLocked() {
            if (mBluetoothScanResultBgCounter == null) {
                mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase);
            }
            return mBluetoothScanResultBgCounter;
        }

        public void noteBluetoothScanResultsLocked(int numNewResults) {
        public void noteBluetoothScanResultsLocked(int numNewResults) {
            createBluetoothScanResultCounterLocked().addAtomic(numNewResults);
            createBluetoothScanResultCounterLocked().addAtomic(numNewResults);
            // Uses background timebase, so the count will only be incremented if uid in background.
            createBluetoothScanResultBgCounterLocked().addAtomic(numNewResults);
        }
        }


        @Override
        @Override
@@ -6276,11 +6308,29 @@ public class BatteryStatsImpl extends BatteryStats {
            return mBluetoothScanTimer.getSubTimer();
            return mBluetoothScanTimer.getSubTimer();
        }
        }


        @Override
        public Timer getBluetoothUnoptimizedScanTimer() {
            return mBluetoothUnoptimizedScanTimer;
        }

        @Override
        public Timer getBluetoothUnoptimizedScanBackgroundTimer() {
            if (mBluetoothUnoptimizedScanTimer == null) {
                return null;
            }
            return mBluetoothUnoptimizedScanTimer.getSubTimer();
        }

        @Override
        @Override
        public Counter getBluetoothScanResultCounter() {
        public Counter getBluetoothScanResultCounter() {
            return mBluetoothScanResultCounter;
            return mBluetoothScanResultCounter;
        }
        }


        @Override
        public Counter getBluetoothScanResultBgCounter() {
            return mBluetoothScanResultBgCounter;
        }

        void makeProcessState(int i, Parcel in) {
        void makeProcessState(int i, Parcel in) {
            if (i < 0 || i >= NUM_PROCESS_STATE) return;
            if (i < 0 || i >= NUM_PROCESS_STATE) return;


@@ -6531,9 +6581,13 @@ public class BatteryStatsImpl extends BatteryStats {
            active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
            active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
            active |= !resetTimerIfNotNull(mAggregatedPartialWakelockTimer, false);
            active |= !resetTimerIfNotNull(mAggregatedPartialWakelockTimer, false);
            active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
            active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
            active |= !resetTimerIfNotNull(mBluetoothUnoptimizedScanTimer, false);
            if (mBluetoothScanResultCounter != null) {
            if (mBluetoothScanResultCounter != null) {
                mBluetoothScanResultCounter.reset(false);
                mBluetoothScanResultCounter.reset(false);
            }
            }
            if (mBluetoothScanResultBgCounter != null) {
                mBluetoothScanResultBgCounter.reset(false);
            }


            if (mProcessStateTimer != null) {
            if (mProcessStateTimer != null) {
                for (int i = 0; i < NUM_PROCESS_STATE; i++) {
                for (int i = 0; i < NUM_PROCESS_STATE; i++) {
@@ -6731,10 +6785,18 @@ public class BatteryStatsImpl extends BatteryStats {
                    mBluetoothScanTimer.detach();
                    mBluetoothScanTimer.detach();
                    mBluetoothScanTimer = null;
                    mBluetoothScanTimer = null;
                }
                }
                if (mBluetoothUnoptimizedScanTimer != null) {
                    mBluetoothUnoptimizedScanTimer.detach();
                    mBluetoothUnoptimizedScanTimer = null;
                }
                if (mBluetoothScanResultCounter != null) {
                if (mBluetoothScanResultCounter != null) {
                    mBluetoothScanResultCounter.detach();
                    mBluetoothScanResultCounter.detach();
                    mBluetoothScanResultCounter = null;
                    mBluetoothScanResultCounter = null;
                }
                }
                if (mBluetoothScanResultBgCounter != null) {
                    mBluetoothScanResultBgCounter.detach();
                    mBluetoothScanResultBgCounter = null;
                }
                if (mUserActivityCounters != null) {
                if (mUserActivityCounters != null) {
                    for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
                    for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
                        mUserActivityCounters[i].detach();
                        mUserActivityCounters[i].detach();
@@ -6919,12 +6981,24 @@ public class BatteryStatsImpl extends BatteryStats {
            } else {
            } else {
                out.writeInt(0);
                out.writeInt(0);
            }
            }
            if (mBluetoothUnoptimizedScanTimer != null) {
                out.writeInt(1);
                mBluetoothUnoptimizedScanTimer.writeToParcel(out, elapsedRealtimeUs);
            } else {
                out.writeInt(0);
            }
            if (mBluetoothScanResultCounter != null) {
            if (mBluetoothScanResultCounter != null) {
                out.writeInt(1);
                out.writeInt(1);
                mBluetoothScanResultCounter.writeToParcel(out);
                mBluetoothScanResultCounter.writeToParcel(out);
            } else {
            } else {
                out.writeInt(0);
                out.writeInt(0);
            }
            }
            if (mBluetoothScanResultBgCounter != null) {
                out.writeInt(1);
                mBluetoothScanResultBgCounter.writeToParcel(out);
            } else {
                out.writeInt(0);
            }
            for (int i = 0; i < NUM_PROCESS_STATE; i++) {
            for (int i = 0; i < NUM_PROCESS_STATE; i++) {
                if (mProcessStateTimer[i] != null) {
                if (mProcessStateTimer[i] != null) {
                    out.writeInt(1);
                    out.writeInt(1);
@@ -7167,11 +7241,23 @@ public class BatteryStatsImpl extends BatteryStats {
            } else {
            } else {
                mBluetoothScanTimer = null;
                mBluetoothScanTimer = null;
            }
            }
            if (in.readInt() != 0) {
                mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
                        BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
                        mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in);
            } else {
                mBluetoothUnoptimizedScanTimer = null;
            }
            if (in.readInt() != 0) {
            if (in.readInt() != 0) {
                mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase, in);
                mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase, in);
            } else {
            } else {
                mBluetoothScanResultCounter = null;
                mBluetoothScanResultCounter = null;
            }
            }
            if (in.readInt() != 0) {
                mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase, in);
            } else {
                mBluetoothScanResultBgCounter = null;
            }
            mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
            mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
            for (int i = 0; i < NUM_PROCESS_STATE; i++) {
            for (int i = 0; i < NUM_PROCESS_STATE; i++) {
                if (in.readInt() != 0) {
                if (in.readInt() != 0) {
@@ -11377,9 +11463,15 @@ public class BatteryStatsImpl extends BatteryStats {
            if (in.readInt() != 0) {
            if (in.readInt() != 0) {
                u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
                u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
            }
            }
            if (in.readInt() != 0) {
                u.createBluetoothUnoptimizedScanTimerLocked().readSummaryFromParcelLocked(in);
            }
            if (in.readInt() != 0) {
            if (in.readInt() != 0) {
                u.createBluetoothScanResultCounterLocked().readSummaryFromParcelLocked(in);
                u.createBluetoothScanResultCounterLocked().readSummaryFromParcelLocked(in);
            }
            }
            if (in.readInt() != 0) {
                u.createBluetoothScanResultBgCounterLocked().readSummaryFromParcelLocked(in);
            }
            u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
            u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
            for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
            for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
                if (in.readInt() != 0) {
                if (in.readInt() != 0) {
@@ -11787,12 +11879,24 @@ public class BatteryStatsImpl extends BatteryStats {
            } else {
            } else {
                out.writeInt(0);
                out.writeInt(0);
            }
            }
            if (u.mBluetoothUnoptimizedScanTimer != null) {
                out.writeInt(1);
                u.mBluetoothUnoptimizedScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
            } else {
                out.writeInt(0);
            }
            if (u.mBluetoothScanResultCounter != null) {
            if (u.mBluetoothScanResultCounter != null) {
                out.writeInt(1);
                out.writeInt(1);
                u.mBluetoothScanResultCounter.writeSummaryFromParcelLocked(out);
                u.mBluetoothScanResultCounter.writeSummaryFromParcelLocked(out);
            } else {
            } else {
                out.writeInt(0);
                out.writeInt(0);
            }
            }
            if (u.mBluetoothScanResultBgCounter != null) {
                out.writeInt(1);
                u.mBluetoothScanResultBgCounter.writeSummaryFromParcelLocked(out);
            } else {
                out.writeInt(0);
            }
            for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
            for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
                if (u.mProcessStateTimer[i] != null) {
                if (u.mProcessStateTimer[i] != null) {
                    out.writeInt(1);
                    out.writeInt(1);
+31 −8
Original line number Original line Diff line number Diff line
@@ -205,9 +205,9 @@ public class BatteryStatsBackgroundStatsTest extends TestCase {
        // App in foreground
        // App in foreground
        bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND);
        bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND);


        // Start timer
        // Start timer (optimized)
        curr = 1000 * (clocks.realtime = clocks.uptime = 202);
        curr = 1000 * (clocks.realtime = clocks.uptime = 202);
        bi.noteBluetoothScanStartedFromSourceLocked(ws);
        bi.noteBluetoothScanStartedFromSourceLocked(ws, false);


        // Move to background
        // Move to background
        curr = 1000 * (clocks.realtime = clocks.uptime = 254);
        curr = 1000 * (clocks.realtime = clocks.uptime = 254);
@@ -221,21 +221,44 @@ public class BatteryStatsBackgroundStatsTest extends TestCase {
        curr = 1000 * (clocks.realtime = clocks.uptime = 409);
        curr = 1000 * (clocks.realtime = clocks.uptime = 409);
        bi.noteBluetoothScanStoppedFromSourceLocked(ws);
        bi.noteBluetoothScanStoppedFromSourceLocked(ws);


        // Start timer (unoptimized)
        curr = 1000 * (clocks.realtime = clocks.uptime = 1000);
        bi.noteBluetoothScanStartedFromSourceLocked(ws, true);

        // On battery
        curr = 1000 * (clocks.realtime = clocks.uptime = 2001);
        bi.updateTimeBasesLocked(true, false, curr, curr); // on battery

        // Move to foreground
        curr = 1000 * (clocks.realtime = clocks.uptime = 3004);
        bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_TOP);

        // Stop timer
        curr = 1000 * (clocks.realtime = clocks.uptime = 4008);
        bi.noteBluetoothScanStoppedFromSourceLocked(ws);

        // Test
        // Test
        curr = 1000 * (clocks.realtime = clocks.uptime = 657);
        curr = 1000 * (clocks.realtime = clocks.uptime = 5000);
        BatteryStats.Timer timer = bi.getUidStats().get(UID).getBluetoothScanTimer();
        BatteryStats.Timer timer = bi.getUidStats().get(UID).getBluetoothScanTimer();
        BatteryStats.Timer bgTimer = bi.getUidStats().get(UID).getBluetoothScanBackgroundTimer();
        BatteryStats.Timer bgTimer = bi.getUidStats().get(UID).getBluetoothScanBackgroundTimer();
        BatteryStats.Timer badTimer = bi.getUidStats().get(UID).getBluetoothUnoptimizedScanTimer();
        BatteryStats.Timer badBgTimer = bi.getUidStats().get(UID)
                .getBluetoothUnoptimizedScanBackgroundTimer();


        long time = timer.getTotalTimeLocked(curr, STATS_SINCE_CHARGED);
        long time = timer.getTotalTimeLocked(curr, STATS_SINCE_CHARGED);
        int count = timer.getCountLocked(STATS_SINCE_CHARGED);
        int count = timer.getCountLocked(STATS_SINCE_CHARGED);
        int bgCount = bgTimer.getCountLocked(STATS_SINCE_CHARGED);
        int bgCount = bgTimer.getCountLocked(STATS_SINCE_CHARGED);
        long actualTime = timer.getTotalDurationMsLocked(clocks.realtime) * 1000;
        long actualTime = timer.getTotalDurationMsLocked(clocks.realtime) * 1000;
        long bgTime = bgTimer.getTotalDurationMsLocked(clocks.realtime) * 1000;
        long bgTime = bgTimer.getTotalDurationMsLocked(clocks.realtime) * 1000;
        assertEquals((305 - 202) * 1000, time);
        long badTime = badTimer.getTotalDurationMsLocked(clocks.realtime) * 1000;
        assertEquals(1, count);
        long badBgTime = badBgTimer.getTotalDurationMsLocked(clocks.realtime) * 1000;
        assertEquals(0, bgCount);
        assertEquals((305 - 202 + 4008 - 2001) * 1000, time);
        assertEquals((305 - 202) * 1000, actualTime);
        assertEquals(1, count); // second scan starts off-battery
        assertEquals((305 - 254) * 1000, bgTime);
        assertEquals(0, bgCount); // first scan starts in fg, second starts off-battery
        assertEquals((305 - 202 + 4008 - 2001) * 1000, actualTime);
        assertEquals((305 - 254 + 3004 - 2001) * 1000, bgTime);
        assertEquals((4008 - 2001) * 1000, badTime);
        assertEquals((3004 - 2001) * 1000, badBgTime);
    }
    }


    @SmallTest
    @SmallTest
+16 −0
Original line number Original line Diff line number Diff line
@@ -37,12 +37,28 @@ public class BatteryStatsNoteTest extends TestCase{
    public void testNoteBluetoothScanResultLocked() throws Exception {
    public void testNoteBluetoothScanResultLocked() throws Exception {
        MockBatteryStatsImpl bi = new MockBatteryStatsImpl(new MockClocks());
        MockBatteryStatsImpl bi = new MockBatteryStatsImpl(new MockClocks());
        bi.updateTimeBasesLocked(true, true, 0, 0);
        bi.updateTimeBasesLocked(true, true, 0, 0);
        bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_TOP);


        bi.noteBluetoothScanResultsFromSourceLocked(WS, 1);
        bi.noteBluetoothScanResultsFromSourceLocked(WS, 1);
        bi.noteBluetoothScanResultsFromSourceLocked(WS, 100);
        bi.noteBluetoothScanResultsFromSourceLocked(WS, 100);
        assertEquals(101,
        assertEquals(101,
                bi.getUidStats().get(UID).getBluetoothScanResultCounter()
                bi.getUidStats().get(UID).getBluetoothScanResultCounter()
                        .getCountLocked(STATS_SINCE_CHARGED));
                        .getCountLocked(STATS_SINCE_CHARGED));
        // TODO: remove next line when Counter misreporting values when plugged-in bug is fixed.
        bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND);
        BatteryStats.Counter bgCntr = bi.getUidStats().get(UID).getBluetoothScanResultBgCounter();
        if (bgCntr != null) {
            assertEquals(0, bgCntr.getCountLocked(STATS_SINCE_CHARGED));
        }

        bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND);
        bi.noteBluetoothScanResultsFromSourceLocked(WS, 17);
        assertEquals(101 + 17,
                bi.getUidStats().get(UID).getBluetoothScanResultCounter()
                        .getCountLocked(STATS_SINCE_CHARGED));
        assertEquals(17,
                bi.getUidStats().get(UID).getBluetoothScanResultBgCounter()
                        .getCountLocked(STATS_SINCE_CHARGED));
    }
    }


    /** Test BatteryStatsImpl.Uid.noteStartWakeLocked. */
    /** Test BatteryStatsImpl.Uid.noteStartWakeLocked. */
Loading