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

Commit bad8d910 authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Make TOP_SLEEPING procstate more like a cached process.

When an app is on the top of the activity stack but the screen
is not on, this doesn't really count as a top app in the normal
sense.  In particular, we'd really like to apply the normal
restrictions we have on background and cached apps: no network
access, no ability to use wake locks, etc.  (In other words, in
this state the app's activity is stopped, so from its perspective
it is no different than the user leaving it to go to another app.)

To do this, we change the order of the TOP_SLEEPING proc state
out from the range of foreground states down to between the
cached and background states.

Test: ActivityManagerProcessStateTest
Bug: 70808931
Change-Id: I994caba8c27553a452de75efa358be0e683d046f
parent d2cafca0
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -3961,7 +3961,8 @@ package android.app {
    field public static final int IMPORTANCE_PERCEPTIBLE = 230; // 0xe6
    field public static final int IMPORTANCE_PERCEPTIBLE_PRE_26 = 130; // 0x82
    field public static final int IMPORTANCE_SERVICE = 300; // 0x12c
    field public static final int IMPORTANCE_TOP_SLEEPING = 150; // 0x96
    field public static final int IMPORTANCE_TOP_SLEEPING = 325; // 0x145
    field public static final deprecated int IMPORTANCE_TOP_SLEEPING_PRE_28 = 150; // 0x96
    field public static final int IMPORTANCE_VISIBLE = 200; // 0xc8
    field public static final int REASON_PROVIDER_IN_USE = 1; // 0x1
    field public static final int REASON_SERVICE_IN_USE = 2; // 0x2
+40 −26
Original line number Diff line number Diff line
@@ -458,16 +458,17 @@ public class ActivityManager {
    public static final int USER_OP_ERROR_RELATED_USERS_CANNOT_STOP = -4;

    /**
     * @hide
     * Process states, describing the kind of state a particular process is in.
     * When updating these, make sure to also check all related references to the
     * constant in code, and update these arrays:
     *
     * com.android.internal.app.procstats.ProcessState#PROCESS_STATE_TO_STATE
     * com.android.server.am.ProcessList#sProcStateToProcMem
     * com.android.server.am.ProcessList#sFirstAwakePssTimes
     * com.android.server.am.ProcessList#sSameAwakePssTimes
     * com.android.server.am.ProcessList#sTestFirstPssTimes
     * com.android.server.am.ProcessList#sTestSamePssTimes
     * @see com.android.internal.app.procstats.ProcessState#PROCESS_STATE_TO_STATE
     * @see com.android.server.am.ProcessList#sProcStateToProcMem
     * @see com.android.server.am.ProcessList#sFirstAwakePssTimes
     * @see com.android.server.am.ProcessList#sSameAwakePssTimes
     * @see com.android.server.am.ProcessList#sTestFirstPssTimes
     * @see com.android.server.am.ProcessList#sTestSamePssTimes
     */

    /** @hide Not a real process state. */
@@ -489,31 +490,31 @@ public class ActivityManager {
    /** @hide Process is hosting a foreground service. */
    public static final int PROCESS_STATE_FOREGROUND_SERVICE = 4;

    /** @hide Same as {@link #PROCESS_STATE_TOP} but while device is sleeping. */
    public static final int PROCESS_STATE_TOP_SLEEPING = 5;

    /** @hide Process is important to the user, and something they are aware of. */
    public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 6;
    public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 5;

    /** @hide Process is important to the user, but not something they are aware of. */
    public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 7;
    public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 6;

    /** @hide Process is in the background transient so we will try to keep running. */
    public static final int PROCESS_STATE_TRANSIENT_BACKGROUND = 8;
    public static final int PROCESS_STATE_TRANSIENT_BACKGROUND = 7;

    /** @hide Process is in the background running a backup/restore operation. */
    public static final int PROCESS_STATE_BACKUP = 9;
    public static final int PROCESS_STATE_BACKUP = 8;

    /** @hide Process is in the background running a service.  Unlike oom_adj, this level
     * is used for both the normal running in background state and the executing
     * operations state. */
    public static final int PROCESS_STATE_SERVICE = 10;
    public static final int PROCESS_STATE_SERVICE = 9;

    /** @hide Process is in the background running a receiver.   Note that from the
     * perspective of oom_adj, receivers run at a higher foreground level, but for our
     * prioritization here that is not necessary and putting them below services means
     * many fewer changes in some process states as they receive broadcasts. */
    public static final int PROCESS_STATE_RECEIVER = 11;
    public static final int PROCESS_STATE_RECEIVER = 10;

    /** @hide Same as {@link #PROCESS_STATE_TOP} but while device is sleeping. */
    public static final int PROCESS_STATE_TOP_SLEEPING = 11;

    /** @hide Process is in the background, but it can't restore its state so we want
     * to try to avoid killing it. */
@@ -2897,13 +2898,13 @@ public class ActivityManager {
        public static final int IMPORTANCE_FOREGROUND_SERVICE = 125;

        /**
         * Constant for {@link #importance}: This process is running the foreground
         * UI, but the device is asleep so it is not visible to the user.  This means
         * the user is not really aware of the process, because they can not see or
         * interact with it, but it is quite important because it what they expect to
         * return to once unlocking the device.
         * @deprecated Pre-{@link android.os.Build.VERSION_CODES#P} version of
         * {@link #IMPORTANCE_TOP_SLEEPING}.  As of Android
         * {@link android.os.Build.VERSION_CODES#P}, this is considered much less
         * important since we want to reduce what apps can do when the screen is off.
         */
        public static final int IMPORTANCE_TOP_SLEEPING = 150;
        @Deprecated
        public static final int IMPORTANCE_TOP_SLEEPING_PRE_28 = 150;

        /**
         * Constant for {@link #importance}: This process is running something
@@ -2963,6 +2964,15 @@ public class ActivityManager {
         */
        public static final int IMPORTANCE_SERVICE = 300;

        /**
         * Constant for {@link #importance}: This process is running the foreground
         * UI, but the device is asleep so it is not visible to the user.  Though the
         * system will try hard to keep its process from being killed, in all other
         * ways we consider it a kind of cached process, with the limitations that go
         * along with that state: network access, running background services, etc.
         */
        public static final int IMPORTANCE_TOP_SLEEPING = 325;

        /**
         * Constant for {@link #importance}: This process is running an
         * application that can not save its state, and thus can't be killed
@@ -3008,14 +3018,14 @@ public class ActivityManager {
                return IMPORTANCE_CACHED;
            } else if (procState == PROCESS_STATE_HEAVY_WEIGHT) {
                return IMPORTANCE_CANT_SAVE_STATE;
            } else if (procState >= PROCESS_STATE_TOP_SLEEPING) {
                return IMPORTANCE_TOP_SLEEPING;
            } else if (procState >= PROCESS_STATE_SERVICE) {
                return IMPORTANCE_SERVICE;
            } else if (procState >= PROCESS_STATE_TRANSIENT_BACKGROUND) {
                return IMPORTANCE_PERCEPTIBLE;
            } else if (procState >= PROCESS_STATE_IMPORTANT_FOREGROUND) {
                return IMPORTANCE_VISIBLE;
            } else if (procState >= PROCESS_STATE_TOP_SLEEPING) {
                return IMPORTANCE_TOP_SLEEPING;
            } else if (procState >= PROCESS_STATE_FOREGROUND_SERVICE) {
                return IMPORTANCE_FOREGROUND_SERVICE;
            } else {
@@ -3049,6 +3059,8 @@ public class ActivityManager {
                switch (importance) {
                    case IMPORTANCE_PERCEPTIBLE:
                        return IMPORTANCE_PERCEPTIBLE_PRE_26;
                    case IMPORTANCE_TOP_SLEEPING:
                        return IMPORTANCE_TOP_SLEEPING_PRE_28;
                    case IMPORTANCE_CANT_SAVE_STATE:
                        return IMPORTANCE_CANT_SAVE_STATE_PRE_26;
                }
@@ -3062,16 +3074,18 @@ public class ActivityManager {
                return PROCESS_STATE_NONEXISTENT;
            } else if (importance >= IMPORTANCE_CACHED) {
                return PROCESS_STATE_HOME;
            } else if (importance == IMPORTANCE_CANT_SAVE_STATE) {
            } else if (importance >= IMPORTANCE_CANT_SAVE_STATE) {
                return PROCESS_STATE_HEAVY_WEIGHT;
            } else if (importance >= IMPORTANCE_TOP_SLEEPING) {
                return PROCESS_STATE_TOP_SLEEPING;
            } else if (importance >= IMPORTANCE_SERVICE) {
                return PROCESS_STATE_SERVICE;
            } else if (importance >= IMPORTANCE_PERCEPTIBLE) {
                return PROCESS_STATE_TRANSIENT_BACKGROUND;
            } else if (importance >= IMPORTANCE_VISIBLE) {
                return PROCESS_STATE_IMPORTANT_FOREGROUND;
            } else if (importance >= IMPORTANCE_TOP_SLEEPING) {
                return PROCESS_STATE_TOP_SLEEPING;
            } else if (importance >= IMPORTANCE_TOP_SLEEPING_PRE_28) {
                return PROCESS_STATE_FOREGROUND_SERVICE;
            } else if (importance >= IMPORTANCE_FOREGROUND_SERVICE) {
                return PROCESS_STATE_FOREGROUND_SERVICE;
            } else {
+18 −10
Original line number Diff line number Diff line
@@ -657,33 +657,41 @@ public abstract class BatteryStats implements Parcelable {
         * none in the "top" state.
         */
        public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
        /**
         * Time this uid has any process that is top while the device is sleeping, but none
         * in the "foreground service" or better state.
         */
        public static final int PROCESS_STATE_TOP_SLEEPING = 2;
        /**
         * Time this uid has any process in an active foreground state, but none in the
         * "top sleeping" or better state.
         */
        public static final int PROCESS_STATE_FOREGROUND = 3;
        public static final int PROCESS_STATE_FOREGROUND = 2;
        /**
         * Time this uid has any process in an active background state, but none in the
         * "foreground" or better state.
         */
        public static final int PROCESS_STATE_BACKGROUND = 4;
        public static final int PROCESS_STATE_BACKGROUND = 3;
        /**
         * Time this uid has any process that is top while the device is sleeping, but not
         * active for any other reason.  We kind-of consider it a kind of cached process
         * for execution restrictions.
         */
        public static final int PROCESS_STATE_TOP_SLEEPING = 4;
        /**
         * Time this uid has any process that is in the background but it has an activity
         * marked as "can't save state".  This is essentially a cached process, though the
         * system will try much harder than normal to avoid killing it.
         */
        public static final int PROCESS_STATE_HEAVY_WEIGHT = 5;
        /**
         * Time this uid has any processes that are sitting around cached, not in one of the
         * other active states.
         */
        public static final int PROCESS_STATE_CACHED = 5;
        public static final int PROCESS_STATE_CACHED = 6;
        /**
         * Total number of process states we track.
         */
        public static final int NUM_PROCESS_STATE = 6;
        public static final int NUM_PROCESS_STATE = 7;

        static final String[] PROCESS_STATE_NAMES = {
            "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
                "Top", "Fg Service", "Foreground", "Background", "Top Sleeping", "Heavy Weight",
                "Cached"
        };

        public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
+1 −1
Original line number Diff line number Diff line
@@ -91,13 +91,13 @@ public final class ProcessState {
        STATE_TOP,                      // ActivityManager.PROCESS_STATE_TOP
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE
        STATE_TOP,                      // ActivityManager.PROCESS_STATE_TOP_SLEEPING
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
        STATE_IMPORTANT_BACKGROUND,     // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
        STATE_IMPORTANT_BACKGROUND,     // ActivityManager.PROCESS_STATE_TRANSIENT_BACKGROUND
        STATE_BACKUP,                   // ActivityManager.PROCESS_STATE_BACKUP
        STATE_SERVICE,                  // ActivityManager.PROCESS_STATE_SERVICE
        STATE_RECEIVER,                 // ActivityManager.PROCESS_STATE_RECEIVER
        STATE_TOP,                      // ActivityManager.PROCESS_STATE_TOP_SLEEPING
        STATE_HEAVY_WEIGHT,             // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT
        STATE_HOME,                     // ActivityManager.PROCESS_STATE_HOME
        STATE_LAST_ACTIVITY,            // ActivityManager.PROCESS_STATE_LAST_ACTIVITY
+5 −3
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ public class BatteryStatsImpl extends BatteryStats {
    private static final int MAGIC = 0xBA757475; // 'BATSTATS'
    // Current on-disk Parcel version
    private static final int VERSION = 170 + (USE_OLD_HISTORY ? 1000 : 0);
    private static final int VERSION = 171 + (USE_OLD_HISTORY ? 1000 : 0);
    // Maximum number of items we will record in the history.
    private static final int MAX_HISTORY_ITEMS;
@@ -8678,13 +8678,15 @@ public class BatteryStatsImpl extends BatteryStats {
            } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
                // Persistent and other foreground states go here.
                uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
            } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
                uidRunningState = PROCESS_STATE_TOP_SLEEPING;
            } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
                // Persistent and other foreground states go here.
                uidRunningState = PROCESS_STATE_FOREGROUND;
            } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
                uidRunningState = PROCESS_STATE_BACKGROUND;
            } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
                uidRunningState = PROCESS_STATE_TOP_SLEEPING;
            } else if (procState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) {
                uidRunningState = PROCESS_STATE_HEAVY_WEIGHT;
            } else {
                uidRunningState = PROCESS_STATE_CACHED;
            }
Loading