From 66c8cb6b3d22046a421e6dc0443dd28b6f46f18b Mon Sep 17 00:00:00 2001 From: Riddle Hsu Date: Mon, 2 Nov 2020 20:17:37 +0800 Subject: [PATCH] Update oom score reference state if activity state is changed Previously, the update of reference state may be scheduled in display thread. That causes a race for the case of activity stopped: when ActivityTaskManagerService#activityStopped is called, it will first set STOPPED state and then invoke trimApplications which will trigger a full oom-adj calculation. But if the scheduled reference state is updated later than the calculation, the adj and process state of the stopped activity may stay at a stale perceptible state temporally. That seems somewhat inconsistent between ATMS and AMS. This change ensures that any activity states change applies to the oom reference state immediately. So any direct or posted oom-adj calculation can refer to the latest state. Also correct the place of setting activity state flags that should be outside of the loop. Bug: 159104503 Bug: 170685888 Test: ActivityLifecycleTests#testRestoreFromKill WindowProcessControllerTests#testComputeOomAdjFromActivities Change-Id: I42bd43f71315dbf8f5d6abc7ab2f742419f0b6da --- .../com/android/server/wm/ActivityRecord.java | 2 +- .../server/wm/RootWindowContainer.java | 3 +- .../server/wm/WindowProcessController.java | 98 ++++++------------- .../wm/WindowProcessControllerTests.java | 6 -- 4 files changed, 34 insertions(+), 75 deletions(-) diff --git a/services/core/java/com/android/server/wm/ActivityRecord.java b/services/core/java/com/android/server/wm/ActivityRecord.java index 0a7f08bfbe8cb..f65a5024bdea9 100644 --- a/services/core/java/com/android/server/wm/ActivityRecord.java +++ b/services/core/java/com/android/server/wm/ActivityRecord.java @@ -4489,7 +4489,7 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A detachChildren(); } if (app != null) { - app.invalidateOomScoreReferenceState(false /* computeNow */); + app.computeProcessActivityState(); } switch (state) { diff --git a/services/core/java/com/android/server/wm/RootWindowContainer.java b/services/core/java/com/android/server/wm/RootWindowContainer.java index 04b303053d8f2..757c57f94a368 100644 --- a/services/core/java/com/android/server/wm/RootWindowContainer.java +++ b/services/core/java/com/android/server/wm/RootWindowContainer.java @@ -2728,8 +2728,7 @@ class RootWindowContainer extends WindowContainer }, true /* traverseTopToBottom */); for (int i = mTmpTaskLayerChangedProcs.size() - 1; i >= 0; i--) { - mTmpTaskLayerChangedProcs.valueAt(i).invalidateOomScoreReferenceState( - true /* computeNow */); + mTmpTaskLayerChangedProcs.valueAt(i).computeProcessActivityState(); } mTmpTaskLayerChangedProcs.clear(); } diff --git a/services/core/java/com/android/server/wm/WindowProcessController.java b/services/core/java/com/android/server/wm/WindowProcessController.java index 2e7905c64049e..5bfa662ea4cb4 100644 --- a/services/core/java/com/android/server/wm/WindowProcessController.java +++ b/services/core/java/com/android/server/wm/WindowProcessController.java @@ -225,8 +225,19 @@ public class WindowProcessController extends ConfigurationContainer