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

Commit 98bfb645 authored by Jing Ji's avatar Jing Ji Committed by Automerger Merge Worker
Browse files

Merge "Cache compat change values per ProcessRecord for OomAdjuster" into...

Merge "Cache compat change values per ProcessRecord for OomAdjuster" into sc-qpr1-dev am: d48c1b10

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15685360

Change-Id: Ib730e56b5840e5feaaa6a1e6f40338479dc23520
parents 1b28a2e7 d48c1b10
Loading
Loading
Loading
Loading
+40 −18
Original line number Original line Diff line number Diff line
@@ -72,6 +72,7 @@ import static com.android.server.am.AppProfiler.TAG_PSS;
import static com.android.server.am.ProcessList.TAG_PROCESS_OBSERVERS;
import static com.android.server.am.ProcessList.TAG_PROCESS_OBSERVERS;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;


import android.annotation.IntDef;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.ActivityManager;
import android.app.ActivityThread;
import android.app.ActivityThread;
@@ -114,6 +115,8 @@ import com.android.server.wm.ActivityServiceConnectionsHolder;
import com.android.server.wm.WindowProcessController;
import com.android.server.wm.WindowProcessController;


import java.io.PrintWriter;
import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.lang.ref.WeakReference;
import java.util.ArrayDeque;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.ArrayList;
@@ -171,6 +174,27 @@ public class OomAdjuster {
    @EnabledSince(targetSdkVersion = android.os.Build.VERSION_CODES.S)
    @EnabledSince(targetSdkVersion = android.os.Build.VERSION_CODES.S)
    static final long USE_SHORT_FGS_USAGE_INTERACTION_TIME = 183972877L;
    static final long USE_SHORT_FGS_USAGE_INTERACTION_TIME = 183972877L;


    static final int CACHED_COMPAT_CHANGE_PROCESS_CAPABILITY = 0;
    static final int CACHED_COMPAT_CHANGE_CAMERA_MICROPHONE_CAPABILITY = 1;
    static final int CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME = 2;

    @IntDef(prefix = { "CACHED_COMPAT_CHANGE_" }, value = {
        CACHED_COMPAT_CHANGE_PROCESS_CAPABILITY,
        CACHED_COMPAT_CHANGE_CAMERA_MICROPHONE_CAPABILITY,
        CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME,
    })
    @Retention(RetentionPolicy.SOURCE)
    static @interface CachedCompatChangeId{}

    /**
     * Mapping from CACHED_COMPAT_CHANGE_* to the actual compat change id.
     */
    static final long[] CACHED_COMPAT_CHANGE_IDS_MAPPING = new long[] {
        PROCESS_CAPABILITY_CHANGE_ID,
        CAMERA_MICROPHONE_CAPABILITY_CHANGE_ID,
        USE_SHORT_FGS_USAGE_INTERACTION_TIME,
    };

    /**
    /**
     * For some direct access we need to power manager.
     * For some direct access we need to power manager.
     */
     */
@@ -368,6 +392,12 @@ public class OomAdjuster {
        return mPlatformCompatCache;
        return mPlatformCompatCache;
    }
    }


    boolean isChangeEnabled(@CachedCompatChangeId int cachedCompatChangeId, ApplicationInfo app,
            boolean defaultValue) {
        return getPlatformCompatCache().isChangeEnabled(
                CACHED_COMPAT_CHANGE_IDS_MAPPING[cachedCompatChangeId], app, defaultValue);
    }

    OomAdjuster(ActivityManagerService service, ProcessList processList, ActiveUids activeUids) {
    OomAdjuster(ActivityManagerService service, ProcessList processList, ActiveUids activeUids) {
        this(service, processList, activeUids, createAdjusterThread());
        this(service, processList, activeUids, createAdjusterThread());
    }
    }
@@ -1929,12 +1959,8 @@ public class OomAdjuster {
                            (fgsType & FOREGROUND_SERVICE_TYPE_LOCATION)
                            (fgsType & FOREGROUND_SERVICE_TYPE_LOCATION)
                                    != 0 ? PROCESS_CAPABILITY_FOREGROUND_LOCATION : 0;
                                    != 0 ? PROCESS_CAPABILITY_FOREGROUND_LOCATION : 0;


                    boolean enabled = false;
                    final boolean enabled = state.getCachedCompatChange(
                    try {
                            CACHED_COMPAT_CHANGE_CAMERA_MICROPHONE_CAPABILITY);
                        enabled = getPlatformCompatCache().isChangeEnabled(
                                CAMERA_MICROPHONE_CAPABILITY_CHANGE_ID, s.appInfo);
                    } catch (RemoteException e) {
                    }
                    if (enabled) {
                    if (enabled) {
                        capabilityFromFGS |=
                        capabilityFromFGS |=
                                (fgsType & FOREGROUND_SERVICE_TYPE_CAMERA)
                                (fgsType & FOREGROUND_SERVICE_TYPE_CAMERA)
@@ -2151,12 +2177,8 @@ public class OomAdjuster {
                                // to client's state.
                                // to client's state.
                                clientProcState = PROCESS_STATE_BOUND_TOP;
                                clientProcState = PROCESS_STATE_BOUND_TOP;
                                state.bumpAllowStartFgsState(PROCESS_STATE_BOUND_TOP);
                                state.bumpAllowStartFgsState(PROCESS_STATE_BOUND_TOP);
                                boolean enabled = false;
                                final boolean enabled = cstate.getCachedCompatChange(
                                try {
                                        CACHED_COMPAT_CHANGE_PROCESS_CAPABILITY);
                                    enabled = getPlatformCompatCache().isChangeEnabled(
                                            PROCESS_CAPABILITY_CHANGE_ID, client.info);
                                } catch (RemoteException e) {
                                }
                                if (enabled) {
                                if (enabled) {
                                    if (cr.hasFlag(Context.BIND_INCLUDE_CAPABILITIES)) {
                                    if (cr.hasFlag(Context.BIND_INCLUDE_CAPABILITIES)) {
                                        // TOP process passes all capabilities to the service.
                                        // TOP process passes all capabilities to the service.
@@ -2800,8 +2822,8 @@ public class OomAdjuster {
                state.setProcStateChanged(true);
                state.setProcStateChanged(true);
            }
            }
        } else if (state.hasReportedInteraction()) {
        } else if (state.hasReportedInteraction()) {
            final boolean fgsInteractionChangeEnabled = getPlatformCompatCache().isChangeEnabled(
            final boolean fgsInteractionChangeEnabled = state.getCachedCompatChange(
                    USE_SHORT_FGS_USAGE_INTERACTION_TIME, app.info, false);
                    CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME);
            final long interactionThreshold = fgsInteractionChangeEnabled
            final long interactionThreshold = fgsInteractionChangeEnabled
                    ? mConstants.USAGE_STATS_INTERACTION_INTERVAL_POST_S
                    ? mConstants.USAGE_STATS_INTERACTION_INTERVAL_POST_S
                    : mConstants.USAGE_STATS_INTERACTION_INTERVAL_PRE_S;
                    : mConstants.USAGE_STATS_INTERACTION_INTERVAL_PRE_S;
@@ -2811,8 +2833,8 @@ public class OomAdjuster {
                maybeUpdateUsageStatsLSP(app, nowElapsed);
                maybeUpdateUsageStatsLSP(app, nowElapsed);
            }
            }
        } else {
        } else {
            final boolean fgsInteractionChangeEnabled = getPlatformCompatCache().isChangeEnabled(
            final boolean fgsInteractionChangeEnabled = state.getCachedCompatChange(
                    USE_SHORT_FGS_USAGE_INTERACTION_TIME, app.info, false);
                    CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME);
            final long interactionThreshold = fgsInteractionChangeEnabled
            final long interactionThreshold = fgsInteractionChangeEnabled
                    ? mConstants.SERVICE_USAGE_INTERACTION_TIME_POST_S
                    ? mConstants.SERVICE_USAGE_INTERACTION_TIME_POST_S
                    : mConstants.SERVICE_USAGE_INTERACTION_TIME_PRE_S;
                    : mConstants.SERVICE_USAGE_INTERACTION_TIME_PRE_S;
@@ -2901,8 +2923,8 @@ public class OomAdjuster {
        if (mService.mUsageStatsService == null) {
        if (mService.mUsageStatsService == null) {
            return;
            return;
        }
        }
        final boolean fgsInteractionChangeEnabled = getPlatformCompatCache().isChangeEnabled(
        final boolean fgsInteractionChangeEnabled = state.getCachedCompatChange(
                USE_SHORT_FGS_USAGE_INTERACTION_TIME, app.info, false);
                CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME);
        boolean isInteraction;
        boolean isInteraction;
        // To avoid some abuse patterns, we are going to be careful about what we consider
        // To avoid some abuse patterns, we are going to be careful about what we consider
        // to be an app interaction.  Being the top activity doesn't count while the display
        // to be an app interaction.  Being the top activity doesn't count while the display
+24 −0
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@ import static android.app.ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE
import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;


import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_OOM_ADJ;
import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_OOM_ADJ;
import static com.android.server.am.OomAdjuster.CachedCompatChangeId;
import static com.android.server.am.ProcessRecord.TAG;
import static com.android.server.am.ProcessRecord.TAG;


import android.annotation.ElapsedRealtimeLong;
import android.annotation.ElapsedRealtimeLong;
@@ -377,6 +378,16 @@ final class ProcessStateRecord {
    @GuardedBy("mService")
    @GuardedBy("mService")
    private int mCachedIsReceivingBroadcast = VALUE_INVALID;
    private int mCachedIsReceivingBroadcast = VALUE_INVALID;


    /**
     * Cache the return value of PlatformCompat.isChangeEnabled().
     */
    @GuardedBy("mService")
    private int[] mCachedCompatChanges = new int[] {
        VALUE_INVALID, // CACHED_COMPAT_CHANGE_PROCESS_CAPABILITY
        VALUE_INVALID, // CACHED_COMPAT_CHANGE_CAMERA_MICROPHONE_CAPABILITY
        VALUE_INVALID, // CACHED_COMPAT_CHANGE_USE_SHORT_FGS_USAGE_INTERACTION_TIME
    };

    @GuardedBy("mService")
    @GuardedBy("mService")
    private int mCachedAdj = ProcessList.INVALID_ADJ;
    private int mCachedAdj = ProcessList.INVALID_ADJ;
    @GuardedBy("mService")
    @GuardedBy("mService")
@@ -1008,6 +1019,16 @@ final class ProcessStateRecord {
        return mCachedIsReceivingBroadcast == VALUE_TRUE;
        return mCachedIsReceivingBroadcast == VALUE_TRUE;
    }
    }


    @GuardedBy("mService")
    boolean getCachedCompatChange(@CachedCompatChangeId int cachedCompatChangeId) {
        if (mCachedCompatChanges[cachedCompatChangeId] == VALUE_INVALID) {
            mCachedCompatChanges[cachedCompatChangeId] = mService.mOomAdjuster
                    .isChangeEnabled(cachedCompatChangeId, mApp.info, false /* default */)
                    ? VALUE_TRUE : VALUE_FALSE;
        }
        return mCachedCompatChanges[cachedCompatChangeId] == VALUE_TRUE;
    }

    @GuardedBy("mService")
    @GuardedBy("mService")
    void computeOomAdjFromActivitiesIfNecessary(OomAdjuster.ComputeOomAdjWindowCallback callback,
    void computeOomAdjFromActivitiesIfNecessary(OomAdjuster.ComputeOomAdjWindowCallback callback,
            int adj, boolean foregroundActivities, boolean hasVisibleActivities, int procState,
            int adj, boolean foregroundActivities, boolean hasVisibleActivities, int procState,
@@ -1088,6 +1109,9 @@ final class ProcessStateRecord {
        mCurSchedGroup = mSetSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
        mCurSchedGroup = mSetSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
        mCurProcState = mCurRawProcState = mSetProcState = mAllowStartFgsState =
        mCurProcState = mCurRawProcState = mSetProcState = mAllowStartFgsState =
                PROCESS_STATE_NONEXISTENT;
                PROCESS_STATE_NONEXISTENT;
        for (int i = 0; i < mCachedCompatChanges.length; i++) {
            mCachedCompatChanges[i] = VALUE_INVALID;
        }
    }
    }


    @GuardedBy("mService")
    @GuardedBy("mService")