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

Commit e65b5d36 authored by Yeabkal Wubshit's avatar Yeabkal Wubshit
Browse files

Implement screen state batterystat events

This change defines a new batterystat event for screen state changes and
implements the creation of the events when BatteryStats.noteScreenState
is called. noteScreenState now accepts the display ID and the screen
state reason, and batterystat logs will show screen state event entries
that contain the display ID, state, and reason.

Note that we will follow up with a change to pass the real display ID
from DisplayPowerController to batterystats (right now, we always use
the default display ID 0). We will do that in a separate change since
its impact can potentially extend beyond screen state events (e.g.
batterystats does display-specific data processing).

Bug: 364350206
Flag: com.android.server.power.optimization.battery_stats_screen_state_event
Test: manual
Test: atest PowerStatTests

Change-Id: If29d4fb014364581c900798a23b8cafd10afbe02
parent b9a7ac0a
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -2066,9 +2066,11 @@ public abstract class BatteryStats {
        public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
        // Event for reporting change of some device states, triggered by a specific UID
        public static final int EVENT_STATE_CHANGE = 0x0015;
        // Event for reporting change of screen states.
        public static final int EVENT_DISPLAY_STATE_CHANGED = 0x0016;

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

@@ -3079,13 +3081,14 @@ public abstract class BatteryStats {
    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", "pkginactive", "pkgactive",
            "tmpwhitelist", "screenwake", "wakeupap", "longwake", "state"
            "tmpwhitelist", "screenwake", "wakeupap", "longwake", "state",
            "display_state_changed"
    };

    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", "Eec", "Esc"
            "Esw", "Ewa", "Elw", "Eec", "Esc", "Eds"
    };

    @FunctionalInterface
+1 −1
Original line number Diff line number Diff line
@@ -144,7 +144,7 @@ interface IBatteryStats {
    @EnforcePermission("UPDATE_DEVICE_STATS")
    void noteGpsSignalQuality(int signalLevel);
    @EnforcePermission("UPDATE_DEVICE_STATS")
    void noteScreenState(int state);
    void noteScreenState(int displayId, int state, int reason);
    @EnforcePermission("UPDATE_DEVICE_STATS")
    void noteScreenBrightness(int brightness);
    @EnforcePermission("UPDATE_DEVICE_STATS")
+3 −2
Original line number Diff line number Diff line
@@ -1834,7 +1834,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub

    @Override
    @EnforcePermission(UPDATE_DEVICE_STATS)
    public void noteScreenState(final int state) {
    public void noteScreenState(final int displayId, final int state, final int reason) {
        super.noteScreenState_enforcePermission();

        synchronized (mLock) {
@@ -1844,7 +1844,8 @@ public final class BatteryStatsService extends IBatteryStats.Stub
            mHandler.post(() -> {
                if (DBG) Slog.d(TAG, "begin noteScreenState");
                synchronized (mStats) {
                    mStats.noteScreenStateLocked(0, state, elapsedRealtime, uptime, currentTime);
                    mStats.noteScreenStateLocked(
                            displayId, state, reason, elapsedRealtime, uptime, currentTime);
                }
                if (DBG) Slog.d(TAG, "end noteScreenState");
            });
+1 −1
Original line number Diff line number Diff line
@@ -2792,7 +2792,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
        if (mBatteryStats != null) {
            try {
                // TODO(multi-display): make this multi-display
                mBatteryStats.noteScreenState(screenState);
                mBatteryStats.noteScreenState(/* displayId= */ 0, screenState, reason);
            } catch (RemoteException e) {
                // same process
            }
+18 −6
Original line number Diff line number Diff line
@@ -5031,9 +5031,7 @@ public class BatteryStatsImpl extends BatteryStats {
        if (mPretendScreenOff != pretendScreenOff) {
            mPretendScreenOff = pretendScreenOff;
            final int primaryScreenState = mPerDisplayBatteryStats[0].screenState;
            noteScreenStateLocked(0, primaryScreenState,
                    mClock.elapsedRealtime(), mClock.uptimeMillis(),
                    mClock.currentTimeMillis());
            noteScreenStateLocked(0, primaryScreenState);
        }
    }
@@ -5554,15 +5552,29 @@ public class BatteryStatsImpl extends BatteryStats {
        }
    }
    private static String getScreenStateTag(
            int display, int state, @Display.StateReason int reason) {
        return String.format(
                "display=%d state=%s reason=%s",
                display, Display.stateToString(state), Display.stateReasonToString(reason));
    }
    @GuardedBy("this")
    public void noteScreenStateLocked(int display, int state) {
        noteScreenStateLocked(display, state, mClock.elapsedRealtime(), mClock.uptimeMillis(),
                mClock.currentTimeMillis());
        noteScreenStateLocked(display, state, Display.STATE_REASON_UNKNOWN,
                mClock.elapsedRealtime(), mClock.uptimeMillis(), mClock.currentTimeMillis());
    }
    @GuardedBy("this")
    public void noteScreenStateLocked(int display, int displayState,
            long elapsedRealtimeMs, long uptimeMs, long currentTimeMs) {
            @Display.StateReason int displayStateReason, long elapsedRealtimeMs, long uptimeMs,
            long currentTimeMs) {
        if (Flags.batteryStatsScreenStateEvent()) {
            mHistory.recordEvent(
                    elapsedRealtimeMs, uptimeMs, HistoryItem.EVENT_DISPLAY_STATE_CHANGED,
                    getScreenStateTag(display, displayState, displayStateReason),
                    Process.INVALID_UID);
        }
        // Battery stats relies on there being 4 states. To accommodate this, new states beyond the
        // original 4 are mapped to one of the originals.
        if (displayState > MAX_TRACKED_SCREEN_STATE) {
Loading