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

Commit 7b030b06 authored by Yanye Li's avatar Yanye Li
Browse files

Power: Add a flag and internal API for disabling wakelocks

Add a flag in PowerManagerService and when it's true, all partial
wakelocks will be disabled. Otherwise wakelocks will follow
original rule for disable/enable.

Also add an API to internal service to change the value of this
flag.

Bug: 370557028
Test: atest PowerServiceTests
Flag: com.android.server.power.feature.flags.force_disable_wakelocks
Change-Id: Ibb278f48654e00f90be7b744dc7107accfe879e1
parent bf18a51e
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -208,6 +208,14 @@ public abstract class PowerManagerInternal {

    public abstract void uidIdle(int uid);

    /**
     * Used to notify the power manager that wakelocks should be disabled.
     *
     * @param force {@code true} to activate force disable wakelocks, {@code false} to turn it off.
     */

    public abstract void setForceDisableWakelocks(boolean force);

    /**
     * Boost: It is sent when user interacting with the device, for example,
     * touchscreen events are incoming.
+35 −0
Original line number Diff line number Diff line
@@ -192,6 +192,9 @@ public final class PowerManagerService extends SystemService
    // Message: Sent when the processes frozen state changes
    private static final int MSG_PROCESS_FROZEN_STATE_CHANGED = 7;

    // Message: Sent when the policy wants to force disable wakelocks.
    private static final int MSG_FORCE_DISABLE_WAKELOCKS = 8;

    // Dirty bit: mWakeLocks changed
    private static final int DIRTY_WAKE_LOCKS = 1 << 0;
    // Dirty bit: mWakefulness changed
@@ -742,6 +745,10 @@ public final class PowerManagerService extends SystemService
    // Whether to keep dreaming when the device is unplugging.
    private boolean mKeepDreamingWhenUnplugging;

    // Whether to force disable wakelocks.
    @GuardedBy("mLock")
    private boolean mForceDisableWakelocks;

    @GuardedBy("mLock")
    private ScreenTimeoutOverridePolicy mScreenTimeoutOverridePolicy;

@@ -4392,6 +4399,15 @@ public final class PowerManagerService extends SystemService
        }
    }

    void setForceDisableWakelocksInternal(boolean force) {
        synchronized (mLock) {
            if (mFeatureFlags.isForceDisableWakelocksEnabled()) {
                mForceDisableWakelocks = force;
                updateWakeLockDisabledStatesLocked();
            }
        }
    }

    @GuardedBy("mLock")
    private boolean doesIdleStateBlockWakeLocksLocked() {
        return mDeviceIdleMode || (mLightDeviceIdleMode && disableWakelocksInLightIdle());
@@ -4470,6 +4486,10 @@ public final class PowerManagerService extends SystemService
                    }
                }
            }
            // Disable all PARTAIL_WAKE_LOCKS if mForceDisableWakelocks is true.
            if (mForceDisableWakelocks) {
                disabled = true;
            }
            return wakeLock.setDisabled(disabled);
        } else if (mDisableScreenWakeLocksWhileCached && isScreenLock(wakeLock)) {
            final int appid = UserHandle.getAppId(wakeLock.mOwnerUid);
@@ -5511,6 +5531,13 @@ public final class PowerManagerService extends SystemService
                case MSG_PROCESS_FROZEN_STATE_CHANGED:
                    handleProcessFrozenStateChange(msg.obj, msg.arg1);
                    break;
                case MSG_FORCE_DISABLE_WAKELOCKS:
                    if (msg.arg1 == 1) {
                        setForceDisableWakelocksInternal(true);
                    } else {
                        setForceDisableWakelocksInternal(false);
                    }
                    break;
            }

            return true;
@@ -7565,6 +7592,14 @@ public final class PowerManagerService extends SystemService
                updateSettingsLocked();
            }
        }

        @Override
        public void setForceDisableWakelocks(boolean force) {
            Slog.i(TAG, (force ? "Starting" : "Stopping") + " to force disable partial wakelocks");
            Message msg = mHandler.obtainMessage(MSG_FORCE_DISABLE_WAKELOCKS,
                    force ? 1 : 0,  0 /*unused*/);
            mHandler.sendMessageAtTime(msg, mClock.uptimeMillis());
        }
    }

    /**
+11 −0
Original line number Diff line number Diff line
@@ -68,6 +68,9 @@ public class PowerManagerFlags {
            new FlagState(Flags.FLAG_DISABLE_FROZEN_PROCESS_WAKELOCKS,
                    Flags::disableFrozenProcessWakelocks);

    private final FlagState mForceDisableWakelocks =
            new FlagState(Flags.FLAG_FORCE_DISABLE_WAKELOCKS, Flags::forceDisableWakelocks);

    private final FlagState mEnableAppWakelockDataSource =
            new FlagState(Flags.FLAG_ENABLE_APP_WAKELOCK_DATA_SOURCE,
                    Flags::enableAppWakelockDataSource);
@@ -125,6 +128,13 @@ public class PowerManagerFlags {
        return mDisableFrozenProcessWakelocks.isEnabled();
    }

    /**
     * @return Whether the feature to force disable wakelocks is enabled
     */
    public boolean isForceDisableWakelocksEnabled() {
        return mForceDisableWakelocks.isEnabled();
    }

    /**
     * @return Whether the new Perfetto data source for tracing app wakelocks is enabled
     */
@@ -144,6 +154,7 @@ public class PowerManagerFlags {
        pw.println(" " + mMoveWscLoggingToNotifier);
        pw.println(" " + mWakelockAttributionViaWorkchain);
        pw.println(" " + mDisableFrozenProcessWakelocks);
        pw.println(" " + mForceDisableWakelocks);
        pw.println(" " + mEnableAppWakelockDataSource);
    }

+7 −0
Original line number Diff line number Diff line
@@ -71,6 +71,13 @@ flag {
    bug: "291115867"
}

flag {
    name: "force_disable_wakelocks"
    namespace: "power"
    description: "Feature flag to force disable wakelocks"
    bug: "370557028"
}

flag {
    name: "enable_app_wakelock_data_source"
    namespace: "power"
+20 −0
Original line number Diff line number Diff line
@@ -2991,6 +2991,26 @@ public class PowerManagerServiceTest {
        assertThat(wakeLock.mDisabled).isFalse();
    }

    @Test
    @RequiresFlagsEnabled({Flags.FLAG_FORCE_DISABLE_WAKELOCKS})
    public void testDisableWakelocks_whenForced() {
        createService();
        startSystem();

        WakeLock wakeLock = acquireWakeLock("forceDisableTestWakeLock",
                PowerManager.PARTIAL_WAKE_LOCK, Display.INVALID_DISPLAY);
        assertThat(wakeLock.mDisabled).isFalse();
        advanceTime(1000);

        mService.setForceDisableWakelocksInternal(true);
        advanceTime(1000);
        assertThat(wakeLock.mDisabled).isTrue();

        mService.setForceDisableWakelocksInternal(false);
        advanceTime(1000);
        assertThat(wakeLock.mDisabled).isFalse();
    }

    @Test
    @RequiresFlagsEnabled({Flags.FLAG_DISABLE_FROZEN_PROCESS_WAKELOCKS})
    public void testDisableWakelocks_whenFrozen() {