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

Commit f5bf5222 authored by David Stevens's avatar David Stevens
Browse files

Clean up logic for killing bg restricted apps

Clean up logic around killing cached processes in idle, background
restricted UIDs. ProcessStateRecord includes flags for marking a process
as exempt from this type of killing, but this functionality is not and
has never been used. Remove the flags that are always false to simplify
the code. Also, rename mLastCanKillOnBgRestrictedAndIdleTime to
mLastCachedTime, since that is what it's actually tracking.

Test: atest ActivityManagerTest#testKillAppIfBgRestrictedCachedIdle
Flag: EXEMPT refactor
Change-Id: I401661d4066404f44f17235114acddee311d29c3
parent 97370356
Loading
Loading
Loading
Loading
+13 −17
Original line number Diff line number Diff line
@@ -2178,14 +2178,12 @@ public abstract class OomAdjuster {
                            + " target=" + state.getAdjTarget());
        }

        if (state.isCached() && !state.shouldNotKillOnBgRestrictedAndIdle()) {
            // It's eligible to get killed when in UID idle and bg restricted mode,
            // check if these states are just flipped.
            if (!state.isSetCached() || state.isSetNoKillOnBgRestrictedAndIdle()) {
                // Take the timestamp, we'd hold the killing for the background settle time
                // (for states debouncing to avoid from thrashing).
                state.setLastCanKillOnBgRestrictedAndIdleTime(nowElapsed);
                // Kick off the delayed checkup message if needed.
        if (state.isCached() && !state.isSetCached()) {
            // Cached procs are eligible to get killed when in an UID idle and bg restricted.
            // However, we want to debounce state changes to avoid thrashing. Mark down when this
            // process became eligible and then schedule a check for eligible processes after
            // a background settling time, if needed.
            state.setLastCachedTime(nowElapsed);
            if (mService.mDeterministicUidIdle
                    || !mService.mHandler.hasMessages(IDLE_UIDS_MSG)) {
                if (mLogger.shouldLog(app.uid)) {
@@ -2197,9 +2195,7 @@ public abstract class OomAdjuster {
                        mConstants.mKillBgRestrictedAndCachedIdleSettleTimeMs);
            }
        }
        }
        state.setSetCached(state.isCached());
        state.setSetNoKillOnBgRestrictedAndIdle(state.shouldNotKillOnBgRestrictedAndIdle());
        if (((oldProcState != state.getSetProcState()) || (oldOomAdj != state.getSetAdj()))
                && mLogger.shouldLog(app.uid)) {
            mLogger.logProcStateChanged(app.uid, app.getPid(),
+0 −1
Original line number Diff line number Diff line
@@ -1160,7 +1160,6 @@ public class OomAdjusterImpl extends OomAdjuster {
        state.setAdjSource(null);
        state.setAdjTarget(null);

        state.setNoKillOnBgRestrictedAndIdle(false);
        // If this UID is currently allowlisted, it should not be frozen.
        final UidRecord uidRec = app.getUidRecord();
        app.mOptRecord.setShouldNotFreeze(uidRec != null && uidRec.isCurAllowListed(),
+4 −4
Original line number Diff line number Diff line
@@ -5613,14 +5613,14 @@ public final class ProcessList {
    @GuardedBy("mService")
    long killAppIfBgRestrictedAndCachedIdleLocked(ProcessRecord app, long nowElapsed) {
        final UidRecord uidRec = app.getUidRecord();
        final long lastCanKillTime = app.mState.getLastCanKillOnBgRestrictedAndIdleTime();
        final long lastCachedTime = app.mState.getLastCachedTime();
        if (!mService.mConstants.mKillBgRestrictedAndCachedIdle
                || app.isKilled() || app.getThread() == null || uidRec == null || !uidRec.isIdle()
                || !app.isCached() || app.mState.shouldNotKillOnBgRestrictedAndIdle()
                || !app.mState.isBackgroundRestricted() || lastCanKillTime == 0) {
                || !app.isCached() || !app.mState.isBackgroundRestricted()
                || lastCachedTime == 0) {
            return 0;
        }
        final long future = lastCanKillTime
        final long future = lastCachedTime
                + mService.mConstants.mKillBgRestrictedAndCachedIdleSettleTimeMs;
        if (future <= nowElapsed) {
            app.killLocked("cached idle & background restricted",
+7 −42
Original line number Diff line number Diff line
@@ -378,13 +378,6 @@ final class ProcessStateRecord {
    @ElapsedRealtimeLong
    private long mLastInvisibleTime;

    /**
     * Whether or not this process could be killed when it's in background restricted mode
     * and cached &amp; idle state.
     */
    @GuardedBy("mService")
    private boolean mNoKillOnBgRestrictedAndIdle;

    /**
     * Last set value of {@link #isCached()}.
     */
@@ -392,19 +385,11 @@ final class ProcessStateRecord {
    private boolean mSetCached;

    /**
     * Last set value of {@link #mNoKillOnBgRestrictedAndIdle}.
     */
    @GuardedBy("mService")
    private boolean mSetNoKillOnBgRestrictedAndIdle;

    /**
     * The last time when the {@link #mNoKillOnBgRestrictedAndIdle} is false and the
     * {@link #isCached()} is true, and either the former state is flipping from true to false
     * when latter state is true, or the latter state is flipping from false to true when the
     * former state is false.
     * When the proc became cached. Used to debounce killing bg restricted apps in
     * an idle UID.
     */
    @GuardedBy("mService")
    private @ElapsedRealtimeLong long mLastCanKillOnBgRestrictedAndIdleTime;
    private @ElapsedRealtimeLong long mLastCachedTime;

    // Below are the cached task info for OomAdjuster only
    private static final int VALUE_INVALID = -1;
@@ -1271,16 +1256,6 @@ final class ProcessStateRecord {
        return mLastInvisibleTime;
    }

    @GuardedBy("mService")
    void setNoKillOnBgRestrictedAndIdle(boolean shouldNotKill) {
        mNoKillOnBgRestrictedAndIdle = shouldNotKill;
    }

    @GuardedBy("mService")
    boolean shouldNotKillOnBgRestrictedAndIdle() {
        return mNoKillOnBgRestrictedAndIdle;
    }

    @GuardedBy("mService")
    void setSetCached(boolean cached) {
        mSetCached = cached;
@@ -1292,24 +1267,14 @@ final class ProcessStateRecord {
    }

    @GuardedBy("mService")
    void setSetNoKillOnBgRestrictedAndIdle(boolean shouldNotKill) {
        mSetNoKillOnBgRestrictedAndIdle = shouldNotKill;
    }

    @GuardedBy("mService")
    boolean isSetNoKillOnBgRestrictedAndIdle() {
        return mSetNoKillOnBgRestrictedAndIdle;
    }

    @GuardedBy("mService")
    void setLastCanKillOnBgRestrictedAndIdleTime(@ElapsedRealtimeLong long now) {
        mLastCanKillOnBgRestrictedAndIdleTime = now;
    void setLastCachedTime(@ElapsedRealtimeLong long now) {
        mLastCachedTime = now;
    }

    @ElapsedRealtimeLong
    @GuardedBy("mService")
    long getLastCanKillOnBgRestrictedAndIdleTime() {
        return mLastCanKillOnBgRestrictedAndIdleTime;
    long getLastCachedTime() {
        return mLastCachedTime;
    }

    public void setCacheOomRankerRss(long rss, long rssTimeMs) {