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

Commit 27f7a866 authored by Jeff Brown's avatar Jeff Brown
Browse files

Track app and display wakelocks separately.

Change the power manager to use two different kernel wakelocks
to distinguish between an application or the display keeping the
CPU alive.  This may help make the output of "dumpsys batteryinfo"
easier to interpret.

Bug: 7726759
Change-Id: Iaff96ad74030d00200617b459679ea16390a8da5
parent fba73030
Loading
Loading
Loading
Loading
+66 −33
Original line number Original line Diff line number Diff line
@@ -238,11 +238,20 @@ public final class PowerManagerService extends IPowerManager.Stub
    // is actually on or actually off or whatever was requested.
    // is actually on or actually off or whatever was requested.
    private boolean mDisplayReady;
    private boolean mDisplayReady;


    // True if holding a wake-lock to block suspend of the CPU.
    // The suspend blocker used to keep the CPU alive when an application has acquired
    // a wake lock.
    private final SuspendBlocker mWakeLockSuspendBlocker;

    // True if the wake lock suspend blocker has been acquired.
    private boolean mHoldingWakeLockSuspendBlocker;
    private boolean mHoldingWakeLockSuspendBlocker;


    // The suspend blocker used to keep the CPU alive when wake locks have been acquired.
    // The suspend blocker used to keep the CPU alive when the display is on, the
    private final SuspendBlocker mWakeLockSuspendBlocker;
    // display is getting ready or there is user activity (in which case the display
    // must be on).
    private final SuspendBlocker mDisplaySuspendBlocker;

    // True if the display suspend blocker has been acquired.
    private boolean mHoldingDisplaySuspendBlocker;


    // The screen on blocker used to keep the screen from turning on while the lock
    // The screen on blocker used to keep the screen from turning on while the lock
    // screen is coming up.
    // screen is coming up.
@@ -368,11 +377,13 @@ public final class PowerManagerService extends IPowerManager.Stub


    public PowerManagerService() {
    public PowerManagerService() {
        synchronized (mLock) {
        synchronized (mLock) {
            mWakeLockSuspendBlocker = createSuspendBlockerLocked("PowerManagerService");
            mWakeLockSuspendBlocker = createSuspendBlockerLocked("PowerManagerService.WakeLocks");
            mWakeLockSuspendBlocker.acquire();
            mDisplaySuspendBlocker = createSuspendBlockerLocked("PowerManagerService.Display");
            mDisplaySuspendBlocker.acquire();
            mHoldingDisplaySuspendBlocker = true;

            mScreenOnBlocker = new ScreenOnBlockerImpl();
            mScreenOnBlocker = new ScreenOnBlockerImpl();
            mDisplayBlanker = new DisplayBlankerImpl();
            mDisplayBlanker = new DisplayBlankerImpl();
            mHoldingWakeLockSuspendBlocker = true;
            mWakefulness = WAKEFULNESS_AWAKE;
            mWakefulness = WAKEFULNESS_AWAKE;
        }
        }


@@ -653,17 +664,21 @@ public final class PowerManagerService extends IPowerManager.Stub


    private void releaseWakeLockInternal(IBinder lock, int flags) {
    private void releaseWakeLockInternal(IBinder lock, int flags) {
        synchronized (mLock) {
        synchronized (mLock) {
            int index = findWakeLockIndexLocked(lock);
            if (index < 0) {
                if (DEBUG_SPEW) {
                if (DEBUG_SPEW) {
                    Slog.d(TAG, "releaseWakeLockInternal: lock=" + Objects.hashCode(lock)
                    Slog.d(TAG, "releaseWakeLockInternal: lock=" + Objects.hashCode(lock)
                        + ", flags=0x" + Integer.toHexString(flags));
                            + " [not found], flags=0x" + Integer.toHexString(flags));
                }
                }

            int index = findWakeLockIndexLocked(lock);
            if (index < 0) {
                return;
                return;
            }
            }


            WakeLock wakeLock = mWakeLocks.get(index);
            WakeLock wakeLock = mWakeLocks.get(index);
            if (DEBUG_SPEW) {
                Slog.d(TAG, "releaseWakeLockInternal: lock=" + Objects.hashCode(lock)
                        + " [" + wakeLock.mTag + "], flags=0x" + Integer.toHexString(flags));
            }

            mWakeLocks.remove(index);
            mWakeLocks.remove(index);
            notifyWakeLockReleasedLocked(wakeLock);
            notifyWakeLockReleasedLocked(wakeLock);
            wakeLock.mLock.unlinkToDeath(wakeLock, 0);
            wakeLock.mLock.unlinkToDeath(wakeLock, 0);
@@ -681,7 +696,8 @@ public final class PowerManagerService extends IPowerManager.Stub
    private void handleWakeLockDeath(WakeLock wakeLock) {
    private void handleWakeLockDeath(WakeLock wakeLock) {
        synchronized (mLock) {
        synchronized (mLock) {
            if (DEBUG_SPEW) {
            if (DEBUG_SPEW) {
                Slog.d(TAG, "handleWakeLockDeath: lock=" + Objects.hashCode(wakeLock.mLock));
                Slog.d(TAG, "handleWakeLockDeath: lock=" + Objects.hashCode(wakeLock.mLock)
                        + " [" + wakeLock.mTag + "]");
            }
            }


            int index = mWakeLocks.indexOf(wakeLock);
            int index = mWakeLocks.indexOf(wakeLock);
@@ -733,10 +749,19 @@ public final class PowerManagerService extends IPowerManager.Stub
        synchronized (mLock) {
        synchronized (mLock) {
            int index = findWakeLockIndexLocked(lock);
            int index = findWakeLockIndexLocked(lock);
            if (index < 0) {
            if (index < 0) {
                if (DEBUG_SPEW) {
                    Slog.d(TAG, "updateWakeLockWorkSourceInternal: lock=" + Objects.hashCode(lock)
                            + " [not found], ws=" + ws);
                }
                throw new IllegalArgumentException("Wake lock not active");
                throw new IllegalArgumentException("Wake lock not active");
            }
            }


            WakeLock wakeLock = mWakeLocks.get(index);
            WakeLock wakeLock = mWakeLocks.get(index);
            if (DEBUG_SPEW) {
                Slog.d(TAG, "updateWakeLockWorkSourceInternal: lock=" + Objects.hashCode(lock)
                        + " [" + wakeLock.mTag + "], ws=" + ws);
            }

            if (!wakeLock.hasSameWorkSource(ws)) {
            if (!wakeLock.hasSameWorkSource(ws)) {
                notifyWakeLockReleasedLocked(wakeLock);
                notifyWakeLockReleasedLocked(wakeLock);
                wakeLock.updateWorkSource(ws);
                wakeLock.updateWorkSource(ws);
@@ -1709,29 +1734,30 @@ public final class PowerManagerService extends IPowerManager.Stub
     * This function must have no other side-effects.
     * This function must have no other side-effects.
     */
     */
    private void updateSuspendBlockerLocked() {
    private void updateSuspendBlockerLocked() {
        boolean wantCpu = isCpuNeededLocked();
        final boolean needWakeLockSuspendBlocker = (mWakeLockSummary != 0);
        if (wantCpu != mHoldingWakeLockSuspendBlocker) {
        final boolean needDisplaySuspendBlocker = (mUserActivitySummary != 0
            mHoldingWakeLockSuspendBlocker = wantCpu;
                || mDisplayPowerRequest.screenState != DisplayPowerRequest.SCREEN_STATE_OFF
            if (wantCpu) {
                || !mDisplayReady || !mBootCompleted);
                if (DEBUG) {

                    Slog.d(TAG, "updateSuspendBlockerLocked: Acquiring suspend blocker.");
        // First acquire suspend blockers if needed.
                }
        if (needWakeLockSuspendBlocker && !mHoldingWakeLockSuspendBlocker) {
            mWakeLockSuspendBlocker.acquire();
            mWakeLockSuspendBlocker.acquire();
            } else {
            mHoldingWakeLockSuspendBlocker = true;
                if (DEBUG) {
                    Slog.d(TAG, "updateSuspendBlockerLocked: Releasing suspend blocker.");
        }
        }
                mWakeLockSuspendBlocker.release();
        if (needDisplaySuspendBlocker && !mHoldingDisplaySuspendBlocker) {
            mDisplaySuspendBlocker.acquire();
            mHoldingDisplaySuspendBlocker = true;
        }
        }

        // Then release suspend blockers if needed.
        if (!needWakeLockSuspendBlocker && mHoldingWakeLockSuspendBlocker) {
            mWakeLockSuspendBlocker.release();
            mHoldingWakeLockSuspendBlocker = false;
        }
        }
        if (!needDisplaySuspendBlocker && mHoldingDisplaySuspendBlocker) {
            mDisplaySuspendBlocker.release();
            mHoldingDisplaySuspendBlocker = false;
        }
        }

    private boolean isCpuNeededLocked() {
        return !mBootCompleted
                || mWakeLockSummary != 0
                || mUserActivitySummary != 0
                || mDisplayPowerRequest.screenState != DisplayPowerRequest.SCREEN_STATE_OFF
                || !mDisplayReady;
    }
    }


    @Override // Binder call
    @Override // Binder call
@@ -2201,6 +2227,7 @@ public final class PowerManagerService extends IPowerManager.Stub
                    + TimeUtils.formatUptime(mLastUserActivityTimeNoChangeLights));
                    + TimeUtils.formatUptime(mLastUserActivityTimeNoChangeLights));
            pw.println("  mDisplayReady=" + mDisplayReady);
            pw.println("  mDisplayReady=" + mDisplayReady);
            pw.println("  mHoldingWakeLockSuspendBlocker=" + mHoldingWakeLockSuspendBlocker);
            pw.println("  mHoldingWakeLockSuspendBlocker=" + mHoldingWakeLockSuspendBlocker);
            pw.println("  mHoldingDisplaySuspendBlocker=" + mHoldingDisplaySuspendBlocker);


            pw.println();
            pw.println();
            pw.println("Settings and Configuration:");
            pw.println("Settings and Configuration:");
@@ -2496,6 +2523,9 @@ public final class PowerManagerService extends IPowerManager.Stub
            synchronized (this) {
            synchronized (this) {
                mReferenceCount += 1;
                mReferenceCount += 1;
                if (mReferenceCount == 1) {
                if (mReferenceCount == 1) {
                    if (DEBUG_SPEW) {
                        Slog.d(TAG, "Acquiring suspend blocker \"" + mName + "\".");
                    }
                    nativeAcquireSuspendBlocker(mName);
                    nativeAcquireSuspendBlocker(mName);
                }
                }
            }
            }
@@ -2506,6 +2536,9 @@ public final class PowerManagerService extends IPowerManager.Stub
            synchronized (this) {
            synchronized (this) {
                mReferenceCount -= 1;
                mReferenceCount -= 1;
                if (mReferenceCount == 0) {
                if (mReferenceCount == 0) {
                    if (DEBUG_SPEW) {
                        Slog.d(TAG, "Releasing suspend blocker \"" + mName + "\".");
                    }
                    nativeReleaseSuspendBlocker(mName);
                    nativeReleaseSuspendBlocker(mName);
                } else if (mReferenceCount < 0) {
                } else if (mReferenceCount < 0) {
                    Log.wtf(TAG, "Suspend blocker \"" + mName
                    Log.wtf(TAG, "Suspend blocker \"" + mName