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

Commit 041d917b authored by Adam Lesinski's avatar Adam Lesinski
Browse files

BatteryStats: Report learned battery capacity as event

This adds the notion of int formatters to events, which allow
events to supply a value other than a UID.

Bug:32645990
Test: Manual
Change-Id: I3855f6b7d16d355c9df8cd2d576cc7faae4a1c4f
parent abcb9f3e
Loading
Loading
Loading
Loading
+23 −4
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import android.content.Context;
import android.content.pm.ApplicationInfo;
@@ -1336,9 +1337,11 @@ public abstract class BatteryStats implements Parcelable {
        public static final int EVENT_WAKEUP_AP = 0x0013;
        // Event for reporting that a specific partial wake lock has been held for a long duration.
        public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
        // Event reporting the new estimated (learned) capacity of the battery in mAh.
        public static final int EVENT_ESTIMATED_BATTERY_CAP = 0x0015;

        // Number of event types.
        public static final int EVENT_COUNT = 0x0015;
        public static final int EVENT_COUNT = 0x0016;
        // Mask to extract out only the type part of the event.
        public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);

@@ -2034,13 +2037,28 @@ public abstract class BatteryStats implements Parcelable {
    public static final String[] HISTORY_EVENT_NAMES = new String[] {
            "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
            "active", "pkginst", "pkgunin", "alarm", "stats", "inactive", "active", "tmpwhitelist",
            "screenwake", "wakeupap", "longwake"
            "screenwake", "wakeupap", "longwake", "est_capacity"
    };

    public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
            "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
            "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
            "Esw", "Ewa", "Elw"
            "Esw", "Ewa", "Elw", "Eec"
    };

    @FunctionalInterface
    public interface IntToString {
        String applyAsString(int val);
    }

    private static final IntToString sUidToString = UserHandle::formatUid;
    private static final IntToString sIntToString = Integer::toString;

    public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
            sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
            sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
            sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
            sUidToString, sUidToString, sUidToString, sIntToString
    };

    /**
@@ -4958,7 +4976,8 @@ public abstract class BatteryStats implements Parcelable {
                    if (checkin) {
                        pw.print(rec.eventTag.poolIdx);
                    } else {
                        UserHandle.formatUid(pw, rec.eventTag.uid);
                        pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
                                .applyAsString(rec.eventTag.uid));
                        pw.print(":\"");
                        pw.print(rec.eventTag.string);
                        pw.print("\"");
+1 −1
Original line number Diff line number Diff line
@@ -125,7 +125,7 @@ interface IBatteryStats {
    void noteNetworkStatsEnabled();
    void noteDeviceIdleMode(int mode, String activeReason, int activeUid);
    void setBatteryState(int status, int health, int plugType, int level, int temp, int volt,
            int chargeUAh);
            int chargeUAh, int chargeFullUAh);
    long getAwakeTimeBattery();
    long getAwakeTimePlugged();

+15 −1
Original line number Diff line number Diff line
@@ -559,6 +559,10 @@ public class BatteryStatsImpl extends BatteryStats {

    private int mEstimatedBatteryCapacity = -1;

    // Last learned capacity reported by BatteryService in
    // setBatteryState().
    private int mLastChargeFullUAh = 0;

    private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();

    private PowerProfile mPowerProfile;
@@ -9757,7 +9761,7 @@ public class BatteryStatsImpl extends BatteryStats {
    public static final int BATTERY_PLUGGED_NONE = 0;

    public void setBatteryStateLocked(int status, int health, int plugType, int level,
            int temp, int volt, int chargeUAh) {
            int temp, int volt, int chargeUAh, int chargeFullUAh) {
        final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
        final long uptime = mClocks.uptimeMillis();
        final long elapsedRealtime = mClocks.elapsedRealtime();
@@ -9919,6 +9923,16 @@ public class BatteryStatsImpl extends BatteryStats {
            // The next time we are unplugged, history will be cleared.
            mRecordingHistory = DEBUG;
        }

        if (differsByMoreThan(chargeFullUAh, mLastChargeFullUAh, 100)) {
            mLastChargeFullUAh = chargeFullUAh;
            addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ESTIMATED_BATTERY_CAP,
                    "", chargeFullUAh / 1000);
        }
    }

    private static boolean differsByMoreThan(int left, int right, int diff) {
        return Math.abs(left - right) > diff;
    }

    public long getAwakeTimeBattery() {
+2 −1
Original line number Diff line number Diff line
@@ -364,7 +364,8 @@ public final class BatteryService extends SystemService {
        try {
            mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
                    mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
                    mBatteryProps.batteryVoltage, mBatteryProps.batteryChargeCounter);
                    mBatteryProps.batteryVoltage, mBatteryProps.batteryChargeCounter,
                    mBatteryProps.batteryFullCharge);
        } catch (RemoteException e) {
            // Should never happen.
        }
+4 −3
Original line number Diff line number Diff line
@@ -991,7 +991,8 @@ public final class BatteryStatsService extends IBatteryStats.Stub

    @Override
    public void setBatteryState(final int status, final int health, final int plugType,
            final int level, final int temp, final int volt, final int chargeUAh) {
            final int level, final int temp, final int volt, final int chargeUAh,
            final int chargeFullUAh) {
        enforceCallingPermission();

        // BatteryService calls us here and we may update external state. It would be wrong
@@ -1005,7 +1006,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                        // The battery state has not changed, so we don't need to sync external
                        // stats immediately.
                        mStats.setBatteryStateLocked(status, health, plugType, level, temp, volt,
                                chargeUAh);
                                chargeUAh, chargeFullUAh);
                        return;
                    }
                }
@@ -1015,7 +1016,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub
                updateExternalStatsSync("battery-state", BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
                synchronized (mStats) {
                    mStats.setBatteryStateLocked(status, health, plugType, level, temp, volt,
                            chargeUAh);
                            chargeUAh, chargeFullUAh);
                }
            }
        });