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

Commit 697f2d91 authored by Riddle Hsu's avatar Riddle Hsu
Browse files

Remove rankTaskLayersIfNeeded from oom-adjustment calculation

- Call invalidateTaskLayers only when leaf/non-leaf task is moved
  (The original place updateTaskMovement was too noisy that it
  may get called without position change, e.g. move top to top).
- Call rankTaskLayersIfNeeded directly if oom-adj will be updated.
- Post rankTaskLayersIfNeeded in invalidateTaskLayers to make sure
  the rank is updated. E.g. in split-screen, switch focus between
  primary and secondary task won't trigger oom-adj update. But if
  there is a service triggers later, the updated rank can still be
  applied. So the result is close to the original method that pulls
  from OomAdjuster.
- Also fix if a task contains multiple activities, the layer rank
  may be replaced to -1 by iterating the invisible activity in the
  same task.

Bug: 159104503
Test: atest RootWindowContainerTests#testTaskLayerRank

Change-Id: I127c1204a287302ad37de73ef7c42e9940a16fb6
parent 08348e26
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -733,10 +733,6 @@ public final class OomAdjuster {
            uidRec.reset();
        }

        if (mService.mAtmInternal != null) {
            mService.mAtmInternal.rankTaskLayersIfNeeded();
        }

        mAdjSeq++;
        if (fullUpdate) {
            mNewNumServiceProcs = 0;
+0 −3
Original line number Diff line number Diff line
@@ -499,9 +499,6 @@ public abstract class ActivityTaskManagerInternal {
    /** @return the process for the top-most resumed activity in the system. */
    public abstract WindowProcessController getTopApp();

    /** Generate oom-score-adjustment rank for all tasks in the system based on z-order. */
    public abstract void rankTaskLayersIfNeeded();

    /** Destroy all activities. */
    public abstract void scheduleDestroyAllActivities(String reason);

+0 −10
Original line number Diff line number Diff line
@@ -7187,16 +7187,6 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
            }
        }

        @HotPath(caller = HotPath.OOM_ADJUSTMENT)
        @Override
        public void rankTaskLayersIfNeeded() {
            synchronized (mGlobalLockWithoutBoost) {
                if (mRootWindowContainer != null) {
                    mRootWindowContainer.rankTaskLayersIfNeeded();
                }
            }
        }

        @Override
        public void scheduleDestroyAllActivities(String reason) {
            synchronized (mGlobalLock) {
+48 −13
Original line number Diff line number Diff line
@@ -276,6 +276,7 @@ class RootWindowContainer extends WindowContainer<DisplayContent>
    // Whether tasks have moved and we need to rank the tasks before next OOM scoring
    private boolean mTaskLayersChanged = true;
    private int mTmpTaskLayerRank;
    private final LockedScheduler mRankTaskLayersScheduler;

    private boolean mTmpBoolean;
    private RemoteException mTmpRemoteException;
@@ -450,6 +451,12 @@ class RootWindowContainer extends WindowContainer<DisplayContent>
        mStackSupervisor = mService.mStackSupervisor;
        mStackSupervisor.mRootWindowContainer = this;
        mDisplayOffTokenAcquirer = mService.new SleepTokenAcquirerImpl("Display-off");
        mRankTaskLayersScheduler = new LockedScheduler(mService) {
            @Override
            public void execute() {
                rankTaskLayersIfNeeded();
            }
        };
    }

    boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows) {
@@ -2698,27 +2705,25 @@ class RootWindowContainer extends WindowContainer<DisplayContent>

    void invalidateTaskLayers() {
        mTaskLayersChanged = true;
        mRankTaskLayersScheduler.scheduleIfNeeded();
    }

    /** Generate oom-score-adjustment rank for all tasks in the system based on z-order. */
    void rankTaskLayersIfNeeded() {
        if (!mTaskLayersChanged) {
            return;
        }
        mTaskLayersChanged = false;
        mTmpTaskLayerRank = 0;
        final PooledConsumer c = PooledLambda.obtainConsumer(
                RootWindowContainer::rankTaskLayerForActivity, this,
                PooledLambda.__(ActivityRecord.class));
        forAllActivities(c);
        c.recycle();
    }

    private void rankTaskLayerForActivity(ActivityRecord r) {
        if (r.canBeTopRunning() && r.mVisibleRequested) {
            r.getTask().mLayerRank = ++mTmpTaskLayerRank;
        // Only rank for leaf tasks because the score of activity is based on immediate parent.
        forAllLeafTasks(task -> {
            final ActivityRecord r = task.topRunningActivityLocked();
            if (r != null && r.mVisibleRequested) {
                task.mLayerRank = ++mTmpTaskLayerRank;
            } else {
            r.getTask().mLayerRank = -1;
                task.mLayerRank = Task.LAYER_RANK_INVISIBLE;
            }
        }, true /* traverseTopToBottom */);
    }

    void clearOtherAppTimeTrackers(AppTimeTracker except) {
@@ -3680,4 +3685,34 @@ class RootWindowContainer extends WindowContainer<DisplayContent>
                    + ", acquire at " + TimeUtils.formatUptime(mAcquireTime) + "}";
        }
    }

    /**
     * Helper class to schedule the runnable if it hasn't scheduled on display thread inside window
     * manager lock.
     */
    abstract static class LockedScheduler implements Runnable {
        private final ActivityTaskManagerService mService;
        private boolean mScheduled;

        LockedScheduler(ActivityTaskManagerService service) {
            mService = service;
        }

        @Override
        public void run() {
            synchronized (mService.mGlobalLock) {
                mScheduled = false;
                execute();
            }
        }

        abstract void execute();

        void scheduleIfNeeded() {
            if (!mScheduled) {
                mService.mH.post(this);
                mScheduled = true;
            }
        }
    }
}
+6 −2
Original line number Diff line number Diff line
@@ -464,9 +464,10 @@ class Task extends WindowContainer<WindowContainer> {
    int mMinWidth;
    int mMinHeight;

    static final int LAYER_RANK_INVISIBLE = -1;
    // Ranking (from top) of this task among all visible tasks. (-1 means it's not visible)
    // This number will be assigned when we evaluate OOM scores for all visible tasks.
    int mLayerRank = -1;
    int mLayerRank = LAYER_RANK_INVISIBLE;

    /** Helper object used for updating override configuration. */
    private Configuration mTmpConfig = new Configuration();
@@ -1539,7 +1540,6 @@ class Task extends WindowContainer<WindowContainer> {
        if (isPersistable) {
            mLastTimeMoved = System.currentTimeMillis();
        }
        mRootWindowContainer.invalidateTaskLayers();
    }

    // Close up recents linked list.
@@ -7452,6 +7452,10 @@ class Task extends WindowContainer<WindowContainer> {
        if (!mChildren.contains(child)) {
            return;
        }
        if (child.asTask() != null) {
            // Non-root task position changed.
            mRootWindowContainer.invalidateTaskLayers();
        }

        final boolean isTop = getTopChild() == child;
        if (isTop) {
Loading