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

Commit 94c5a313 authored by Bookatz's avatar Bookatz Committed by Andre Eisenbach
Browse files

Batterystats handles nested unoptimized ble scans

When a ble scan starts, it tells batterystats whether that scan is
unoptimized. When the scan stops, batterystats is not informed of
whether the stopped scan was unoptimized. Because the ble scan call
could not be nested (couldn't call start twice without stopping first),
this was fine, but now nesting is possible, so batterystats needs to
know whether the stopped ble scan is unoptimized.

Bug: 63456783
Test: runtest -x frameworks/base/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java
Test: no new errors when run cts-dev -m CtsIncidentHostTestCases -t com.android.server.cts.BatteryStatsValidationTest#testUnoptimizedBleScans

Change-Id: Ia73f294cf1807ddaf20f1c0bcc28add001cac78c
parent cdcbbecc
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -130,7 +130,7 @@ interface IBatteryStats {
    long getAwakeTimePlugged();

    void noteBleScanStarted(in WorkSource ws, boolean isUnoptimized);
    void noteBleScanStopped(in WorkSource ws);
    void noteBleScanStopped(in WorkSource ws, boolean isUnoptimized);
    void noteResetBleScan();
    void noteBleScanResults(in WorkSource ws, int numNewResults);

+6 −9
Original line number Diff line number Diff line
@@ -4852,7 +4852,7 @@ public class BatteryStatsImpl extends BatteryStats {
        }
    }

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

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

@@ -6121,14 +6121,11 @@ public class BatteryStatsImpl extends BatteryStats {
            }
        }

        public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
        public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
            if (mBluetoothScanTimer != null) {
                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()) {
            if (isUnoptimized && mBluetoothUnoptimizedScanTimer != null) {
                mBluetoothUnoptimizedScanTimer.stopRunningLocked(elapsedRealtimeMs);
            }
        }
+6 −6
Original line number Diff line number Diff line
@@ -217,10 +217,6 @@ public class BatteryStatsBackgroundStatsTest extends TestCase {
        curr = 1000 * (clocks.realtime = clocks.uptime = 305);
        bi.updateTimeBasesLocked(false, false, curr, curr); // off battery

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

        // Start timer (unoptimized)
        curr = 1000 * (clocks.realtime = clocks.uptime = 1000);
        bi.noteBluetoothScanStartedFromSourceLocked(ws, true);
@@ -233,9 +229,13 @@ public class BatteryStatsBackgroundStatsTest extends TestCase {
        curr = 1000 * (clocks.realtime = clocks.uptime = 3004);
        bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_TOP);

        // Stop timer
        // Stop timer (optimized)
        curr = 1000 * (clocks.realtime = clocks.uptime = 3409);
        bi.noteBluetoothScanStoppedFromSourceLocked(ws, false);

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

        // Test
        curr = 1000 * (clocks.realtime = clocks.uptime = 5000);
+0 −2
Original line number Diff line number Diff line
@@ -44,8 +44,6 @@ public class BatteryStatsNoteTest extends TestCase{
        assertEquals(101,
                bi.getUidStats().get(UID).getBluetoothScanResultCounter()
                        .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));
+2 −2
Original line number Diff line number Diff line
@@ -985,10 +985,10 @@ public final class BatteryStatsService extends IBatteryStats.Stub
    }

    @Override
    public void noteBleScanStopped(WorkSource ws) {
    public void noteBleScanStopped(WorkSource ws, boolean isUnoptimized) {
        enforceCallingPermission();
        synchronized (mStats) {
            mStats.noteBluetoothScanStoppedFromSourceLocked(ws);
            mStats.noteBluetoothScanStoppedFromSourceLocked(ws, isUnoptimized);
        }
    }