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

Commit 99e57c3c authored by Mike Ma's avatar Mike Ma Committed by Bookatz
Browse files

Remove which from BatteryStats

BatteryStats tracks three "which" states:
STATS_SINCE_CHARGED,
STATS_CURRENT,
STATS_SINCE_UNPLUGGED

However, only STATS_SINCE_CHARGED is really needed; the other two
just take up a lot of unjustifiable RAM. We therefore remove them from
the implementation here.
We also remove mLastTime and mLastCount from Timers and Counters, since
it was always zero anyway.

While the other two "whiches" are left as constants, they are ignored,
and all functions treat any which as STATS_SINCE_CHARGED. In particular,
SystemHealthManager now uses STATS_SINCE_CHARGED, rather than
STATS_SINCE_UNPLUGGED for all of its Battery statistics.

This is adapted from ag/4310406, but slimmed down to just the 'which'
removal parts.

Bug: 70892428
Test: com.android.internal.os.BatteryStatsTests
Change-Id: I86f2743ff30afc15bbaa12c68f3206ea528857a1
parent 47119cf9
Loading
Loading
Loading
Loading
+30 −58
Original line number Diff line number Diff line
@@ -200,13 +200,21 @@ public abstract class BatteryStats implements Parcelable {

    /**
     * Include only the current run in the stats.
     *
     * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, only {@link #STATS_SINCE_CHARGED}
     * is supported.
     */
    @UnsupportedAppUsage
    @Deprecated
    public static final int STATS_CURRENT = 1;

    /**
     * Include only the run since the last time the device was unplugged in the stats.
     *
     * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, only {@link #STATS_SINCE_CHARGED}
     * is supported.
     */
    @Deprecated
    public static final int STATS_SINCE_UNPLUGGED = 2;

    // NOTE: Update this list if you add/change any stats above.
@@ -255,8 +263,10 @@ public abstract class BatteryStats implements Parcelable {
     *   - Ambient display properly output in data dump.
     * New in version 33:
     *   - Fixed bug in min learned capacity updating process.
     * New in version 34:
     *   - Deprecated STATS_SINCE_UNPLUGGED and STATS_CURRENT.
     */
    static final int CHECKIN_VERSION = 33;
    static final int CHECKIN_VERSION = 34;

    /**
     * Old version, we hit 9 and ran out of room, need to remove.
@@ -3848,20 +3858,6 @@ public abstract class BatteryStats implements Parcelable {
                multicastWakeLockTimeTotalMicros / 1000,
                multicastWakeLockCountTotal);

        if (which == STATS_SINCE_UNPLUGGED) {
            dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
                    getDischargeCurrentLevel());
        }

        if (which == STATS_SINCE_UNPLUGGED) {
            dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
                    getDischargeStartLevel()-getDischargeCurrentLevel(),
                    getDischargeStartLevel()-getDischargeCurrentLevel(),
                    getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
                    dischargeCount / 1000, dischargeScreenOffCount / 1000,
                    getDischargeAmountScreenDoze(), dischargeScreenDozeCount / 1000,
                    dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
        } else {
        dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
                getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
                getDischargeAmountScreenOnSinceCharge(),
@@ -3869,7 +3865,6 @@ public abstract class BatteryStats implements Parcelable {
                dischargeCount / 1000, dischargeScreenOffCount / 1000,
                getDischargeAmountScreenDozeSinceCharge(), dischargeScreenDozeCount / 1000,
                dischargeLightDozeCount / 1000, dischargeDeepDozeCount / 1000);
        }

        if (reqUid < 0) {
            final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
@@ -5044,28 +5039,6 @@ public abstract class BatteryStats implements Parcelable {

        pw.println();

        if (which == STATS_SINCE_UNPLUGGED) {
            if (getIsOnBattery()) {
                pw.print(prefix); pw.println("  Device is currently unplugged");
                pw.print(prefix); pw.print("    Discharge cycle start level: ");
                        pw.println(getDischargeStartLevel());
                pw.print(prefix); pw.print("    Discharge cycle current level: ");
                        pw.println(getDischargeCurrentLevel());
            } else {
                pw.print(prefix); pw.println("  Device is currently plugged into power");
                pw.print(prefix); pw.print("    Last discharge cycle start level: ");
                        pw.println(getDischargeStartLevel());
                pw.print(prefix); pw.print("    Last discharge cycle end level: ");
                        pw.println(getDischargeCurrentLevel());
            }
            pw.print(prefix); pw.print("    Amount discharged while screen on: ");
            pw.println(getDischargeAmountScreenOn());
            pw.print(prefix); pw.print("    Amount discharged while screen off: ");
            pw.println(getDischargeAmountScreenOff());
            pw.print(prefix); pw.print("    Amount discharged while screen doze: ");
            pw.println(getDischargeAmountScreenDoze());
            pw.println(" ");
        } else {
        pw.print(prefix); pw.println("  Device battery use since last full charge");
        pw.print(prefix); pw.print("    Amount discharged (lower bound): ");
        pw.println(getLowDischargeAmountSinceCharge());
@@ -5078,7 +5051,6 @@ public abstract class BatteryStats implements Parcelable {
        pw.print(prefix); pw.print("    Amount discharged while screen doze: ");
        pw.println(getDischargeAmountScreenDozeSinceCharge());
        pw.println();
        }

        final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
        helper.create(this);
+7 −5
Original line number Diff line number Diff line
@@ -37,11 +37,13 @@ import com.android.internal.app.IBatteryStats;
 * by the logging itself.  It can be substantial.
 * <p>
 * <b>Battery Usage</b><br>
 * The statistics related to power (battery) usage are recorded since the device
 * was last unplugged. It is expected that applications schedule more work to do
 * while the device is plugged in (e.g. using {@link android.app.job.JobScheduler
 * JobScheduler}), and while that can affect charging rates, it is still preferable
 * to actually draining the battery.
 * Since Android version {@link android.os.Build.VERSION_CODES#Q}, the statistics related to power
 * (battery) usage are recorded since the device was last considered fully charged (for previous
 * versions, it is instead since the device was last unplugged).
 * It is expected that applications schedule more work to do while the device is
 * plugged in (e.g. using {@link android.app.job.JobScheduler JobScheduler}), and
 * while that can affect charging rates, it is still preferable to actually draining
 * the battery.
 */
@SystemService(Context.SYSTEM_HEALTH_SERVICE)
public class SystemHealthManager {
+69 −468

File changed.

Preview size limit exceeded, changes collapsed.

+0 −22
Original line number Diff line number Diff line
@@ -44,8 +44,6 @@ public class BatteryStatsCounterTest extends TestCase {
        counter.stepAtomic();
        counter.stepAtomic();
        assertEquals(0, counter.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
        assertEquals(0, counter.getCountLocked(BatteryStats.STATS_CURRENT));
        assertEquals(0, counter.getCountLocked(BatteryStats.STATS_SINCE_UNPLUGGED));

        // timeBase on (i.e. unplugged)
        timeBase.setRunning(true, 2, 2);
@@ -54,8 +52,6 @@ public class BatteryStatsCounterTest extends TestCase {
        counter.stepAtomic();
        counter.stepAtomic();
        assertEquals(4, counter.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
        assertEquals(4, counter.getCountLocked(BatteryStats.STATS_CURRENT));
        assertEquals(4, counter.getCountLocked(BatteryStats.STATS_SINCE_UNPLUGGED));

        // timeBase off (i.e. plugged in)
        timeBase.setRunning(false, 3, 3);
@@ -63,16 +59,12 @@ public class BatteryStatsCounterTest extends TestCase {
        counter.stepAtomic();
        counter.stepAtomic();
        assertEquals(4, counter.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
        assertEquals(4, counter.getCountLocked(BatteryStats.STATS_CURRENT));
        assertEquals(4, counter.getCountLocked(BatteryStats.STATS_SINCE_UNPLUGGED));

        // timeBase on (i.e. unplugged)
        timeBase.setRunning(true, 4, 4);
        counter.stepAtomic();
        counter.stepAtomic();
        assertEquals(6, counter.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
        assertEquals(6, counter.getCountLocked(BatteryStats.STATS_CURRENT));
        assertEquals(2, counter.getCountLocked(BatteryStats.STATS_SINCE_UNPLUGGED));
    }


@@ -88,8 +80,6 @@ public class BatteryStatsCounterTest extends TestCase {
        timeBase.setRunning(true, 1, 1);
        origCounter.stepAtomic(); origCounter.stepAtomic(); origCounter.stepAtomic(); // three times
        assertEquals(3, origCounter.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
        assertEquals(3, origCounter.getCountLocked(BatteryStats.STATS_CURRENT));
        assertEquals(3, origCounter.getCountLocked(BatteryStats.STATS_SINCE_UNPLUGGED));

        // Test summary parcelling (from origCounter)
        final Parcel summaryParcel = Parcel.obtain();
@@ -102,22 +92,16 @@ public class BatteryStatsCounterTest extends TestCase {
        // timeBase still on (i.e. unplugged)
        summaryCounter.stepAtomic(); // once
        assertEquals(4, summaryCounter.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
        assertEquals(1, summaryCounter.getCountLocked(BatteryStats.STATS_CURRENT));
        assertEquals(1, summaryCounter.getCountLocked(BatteryStats.STATS_SINCE_UNPLUGGED));

        // timeBase off (i.e. plugged in)
        timeBase.setRunning(false, 3, 3);
        summaryCounter.stepAtomic(); summaryCounter.stepAtomic(); // twice
        assertEquals(4, summaryCounter.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
        assertEquals(1, summaryCounter.getCountLocked(BatteryStats.STATS_CURRENT));
        assertEquals(1, summaryCounter.getCountLocked(BatteryStats.STATS_SINCE_UNPLUGGED));

        // timeBase on (i.e. unplugged)
        timeBase.setRunning(true, 4, 4);
        summaryCounter.stepAtomic(); summaryCounter.stepAtomic(); // twice
        assertEquals(6, summaryCounter.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
        assertEquals(3, summaryCounter.getCountLocked(BatteryStats.STATS_CURRENT));
        assertEquals(2, summaryCounter.getCountLocked(BatteryStats.STATS_SINCE_UNPLUGGED));


        // Test full parcelling (from summaryCounter)
@@ -130,21 +114,15 @@ public class BatteryStatsCounterTest extends TestCase {
        // timeBase still on (i.e. unplugged)
        fullParcelCounter.stepAtomic(); // once
        assertEquals(7, fullParcelCounter.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
        assertEquals(4, fullParcelCounter.getCountLocked(BatteryStats.STATS_CURRENT));
        assertEquals(3, fullParcelCounter.getCountLocked(BatteryStats.STATS_SINCE_UNPLUGGED));

        // timeBase off (i.e. plugged in)
        timeBase.setRunning(false, 5, 5);
        fullParcelCounter.stepAtomic(); fullParcelCounter.stepAtomic(); // twice
        assertEquals(7, fullParcelCounter.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
        assertEquals(4, fullParcelCounter.getCountLocked(BatteryStats.STATS_CURRENT));
        assertEquals(3, fullParcelCounter.getCountLocked(BatteryStats.STATS_SINCE_UNPLUGGED));

        // timeBase on (i.e. unplugged)
        timeBase.setRunning(true, 6, 6);
        fullParcelCounter.stepAtomic(); fullParcelCounter.stepAtomic(); // twice
        assertEquals(9, fullParcelCounter.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
        assertEquals(6, fullParcelCounter.getCountLocked(BatteryStats.STATS_CURRENT));
        assertEquals(2, fullParcelCounter.getCountLocked(BatteryStats.STATS_SINCE_UNPLUGGED));
    }
}
+62 −0
Original line number Diff line number Diff line
@@ -100,8 +100,70 @@ public class BatteryStatsDurationTimerTest extends TestCase {
        assertEquals(30802, timer.getTotalDurationMsLocked(220302));
    }

    /**
     * Tests that reset(boolean detachIfReset) clears the correct times if detachIfReset is false.
     */
    @SmallTest
    public void testReset() throws Exception {
        final MockClocks clocks = new MockClocks();

        final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
        timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());

        final BatteryStatsImpl.DurationTimer timer = new BatteryStatsImpl.DurationTimer(clocks,
                null, BatteryStats.WAKE_TYPE_PARTIAL, null, timeBase);

        timeBase.setRunning(true, /* uptimeUs */ 0, /* realtimeUs */ 100_000);
        timer.startRunningLocked(700);
        timer.stopRunningLocked(3_100);
        assertFalse(timer.isRunningLocked());
        assertEquals(0, timer.getCurrentDurationMsLocked(6_300));
        assertEquals(2_400, timer.getMaxDurationMsLocked(6_301));
        assertEquals(2_400, timer.getTotalDurationMsLocked(6_302));

        timer.reset(false);
        assertEquals(0, timer.getCurrentDurationMsLocked(12_000));
        assertEquals(0, timer.getMaxDurationMsLocked(12_001));
        assertEquals(0, timer.getTotalDurationMsLocked(12_002));

        assertEquals(true, timeBase.hasObserver(timer));

        timer.startRunningLocked(24_100);
        clocks.uptime = clocks.realtime = 24_200;
        timer.reset(false);
        assertEquals(34_300, timer.getCurrentDurationMsLocked(58_500));
        assertEquals(34_301, timer.getMaxDurationMsLocked(58_501));
        assertEquals(34_302, timer.getTotalDurationMsLocked(58_502));
    }

    /**
     * Tests that reset(boolean detachIfReset) clears the correct times if detachIfReset is true.
     */
    @SmallTest
    public void testResetAndDetach() throws Exception {
        final MockClocks clocks = new MockClocks();

        final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
        timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());

        final BatteryStatsImpl.DurationTimer timer = new BatteryStatsImpl.DurationTimer(clocks,
                null, BatteryStats.WAKE_TYPE_PARTIAL, null, timeBase);

        timeBase.setRunning(true, /* uptimeUs */ 0, /* realtimeUs */ 100_000);
        timer.startRunningLocked(700);
        timer.stopRunningLocked(3_100);
        assertFalse(timer.isRunningLocked());
        assertEquals(0, timer.getCurrentDurationMsLocked(6_300));
        assertEquals(2_400, timer.getMaxDurationMsLocked(6_301));
        assertEquals(2_400, timer.getTotalDurationMsLocked(6_302));

        timer.reset(true);
        clocks.uptime = clocks.realtime = 7_000;
        assertEquals(0, timer.getCurrentDurationMsLocked(8_000));
        assertEquals(0, timer.getMaxDurationMsLocked(8_001));
        assertEquals(0, timer.getTotalDurationMsLocked(8_002));

        assertEquals(false, timeBase.hasObserver(timer));
    }

    @SmallTest
Loading