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

Commit 317750d6 authored by Chih-Yu Huang's avatar Chih-Yu Huang
Browse files

am: Decouple ProcessRecord from OomAdjuster

This refactoring makes the OomAdjuster's methods
`computeServiceHostOomAdjLSP` and `computeProviderHostOomAdjLSP` using
`ProcessRecordInternal` instead of `ProcessRecord`.

To achieve that, it elevates `setTreatLikeActivity`,
`setPendingUiClean`, and `getApplicationUid` to abstract methods in
`ProcessRecordInternal` class.

Bug: 425766486
Test: m services.core
Test: atest MockingOomAdjusterTests OomAdjusterTests
Test: atest FrameworksServicesTestsRavenwood_ProcessStateController
Flag: EXEMPT pure refactor

Change-Id: I67233e38f97c668de858f6993dd9f8a498bd803f
parent a3faa4f7
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.util.proto.ProtoUtils;

import com.android.internal.app.procstats.AssociationState;
import com.android.internal.app.procstats.ProcessStats;
import com.android.server.am.psc.ProcessRecordInternal;
import com.android.server.wm.ActivityServiceConnectionsHolder;

import java.io.PrintWriter;
@@ -148,8 +149,8 @@ final class ConnectionRecord implements OomAdjusterImpl.Connection{
    }

    @Override
    public void computeHostOomAdjLSP(OomAdjuster oomAdjuster, ProcessRecord host,
            ProcessRecord client, long now, ProcessRecord topApp, boolean doingAll,
    public void computeHostOomAdjLSP(OomAdjuster oomAdjuster, ProcessRecordInternal host,
            ProcessRecordInternal client, long now, ProcessRecordInternal topApp, boolean doingAll,
            int oomAdjReason, int cachedAdj) {
        oomAdjuster.computeServiceHostOomAdjLSP(this, host, client, now, false);
    }
+3 −2
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.util.TimeUtils;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.app.procstats.AssociationState;
import com.android.internal.app.procstats.ProcessStats;
import com.android.server.am.psc.ProcessRecordInternal;

/**
 * Represents a link between a content provider and client.
@@ -74,8 +75,8 @@ public final class ContentProviderConnection extends Binder implements
    }

    @Override
    public void computeHostOomAdjLSP(OomAdjuster oomAdjuster, ProcessRecord host,
            ProcessRecord client, long now, ProcessRecord topApp, boolean doingAll,
    public void computeHostOomAdjLSP(OomAdjuster oomAdjuster, ProcessRecordInternal host,
            ProcessRecordInternal client, long now, ProcessRecordInternal topApp, boolean doingAll,
            int oomAdjReason, int cachedAdj) {
        oomAdjuster.computeProviderHostOomAdjLSP(this, host, client, false);
    }
+3 −3
Original line number Diff line number Diff line
@@ -1992,15 +1992,15 @@ public abstract class OomAdjuster {
     * Computes the impact on {@code app} the service connections from {@code client} has.
     */
    @GuardedBy({"mService", "mProcLock"})
    public abstract boolean computeServiceHostOomAdjLSP(ConnectionRecord cr, ProcessRecord app,
            ProcessRecordInternal client, long now, boolean dryRun);
    public abstract boolean computeServiceHostOomAdjLSP(ConnectionRecord cr,
            ProcessRecordInternal app, ProcessRecordInternal client, long now, boolean dryRun);

    /**
     * Computes the impact on {@code app} the provider connections from {@code client} has.
     */
    @GuardedBy({"mService", "mProcLock"})
    public abstract boolean computeProviderHostOomAdjLSP(ContentProviderConnection conn,
            ProcessRecord app, ProcessRecord client, boolean dryRun);
            ProcessRecordInternal app, ProcessRecordInternal client, boolean dryRun);

    protected int getDefaultCapability(ProcessRecordInternal app, int procState) {
        final int networkCapabilities =
+69 −76
Original line number Diff line number Diff line
@@ -513,8 +513,9 @@ public class OomAdjusterImpl extends OomAdjuster {
        /**
         * Compute the impact this connection has on the host's importance values.
         */
        void computeHostOomAdjLSP(OomAdjuster oomAdjuster, ProcessRecord host, ProcessRecord client,
                long now, ProcessRecord topApp, boolean doingAll, int oomAdjReason, int cachedAdj);
        void computeHostOomAdjLSP(OomAdjuster oomAdjuster, ProcessRecordInternal host,
                ProcessRecordInternal client, long now, ProcessRecordInternal topApp,
                boolean doingAll, int oomAdjReason, int cachedAdj);

        /**
         * Returns true if this connection can propagate capabilities.
@@ -1847,41 +1848,39 @@ public class OomAdjusterImpl extends OomAdjuster {

    @GuardedBy({"mService", "mProcLock"})
    @Override
    public boolean computeServiceHostOomAdjLSP(ConnectionRecord cr, ProcessRecord app,
    public boolean computeServiceHostOomAdjLSP(ConnectionRecord cr, ProcessRecordInternal app,
            ProcessRecordInternal client, long now, boolean dryRun) {
        if (app.isPendingFinishAttach()) {
            // We've set the attaching process state in the computeInitialOomAdjLSP. Skip it here.
            return false;
        }

        final ProcessRecordInternal state = app;
        ProcessRecordInternal cstate = client;
        boolean updated = false;

        int clientAdj = cstate.getCurRawAdj();
        int clientProcState = cstate.getCurRawProcState();
        int clientAdj = client.getCurRawAdj();
        int clientProcState = client.getCurRawProcState();

        final boolean clientIsSystem = clientProcState < PROCESS_STATE_TOP;

        int adj = state.getCurRawAdj();
        int procState = state.getCurRawProcState();
        int schedGroup = state.getCurrentSchedulingGroup();
        int capability = state.getCurCapability();
        int adj = app.getCurRawAdj();
        int procState = app.getCurRawProcState();
        int schedGroup = app.getCurrentSchedulingGroup();
        int capability = app.getCurCapability();

        final int prevRawAdj = adj;
        final int prevProcState = procState;
        final int prevSchedGroup = schedGroup;
        final int prevCapability = capability;

        final int appUid = app.info.uid;
        final int appUid = app.getApplicationUid();
        final int logUid = mService.mCurOomAdjUid;

        if (!dryRun) {
            state.setCurBoundByNonBgRestrictedApp(state.isCurBoundByNonBgRestrictedApp()
                    || cstate.isCurBoundByNonBgRestrictedApp()
            app.setCurBoundByNonBgRestrictedApp(app.isCurBoundByNonBgRestrictedApp()
                    || client.isCurBoundByNonBgRestrictedApp()
                    || clientProcState <= PROCESS_STATE_BOUND_TOP
                    || (clientProcState == PROCESS_STATE_FOREGROUND_SERVICE
                    && !cstate.isBackgroundRestricted()));
                    && !client.isBackgroundRestricted()));
        }

        if (client.shouldNotFreeze()) {
@@ -1908,7 +1907,7 @@ public class OomAdjusterImpl extends OomAdjuster {

        if (cr.notHasFlag(Context.BIND_WAIVE_PRIORITY)) {
            if (cr.hasFlag(Context.BIND_INCLUDE_CAPABILITIES)) {
                capability |= cstate.getCurCapability();
                capability |= client.getCurCapability();
            }

            // If an app has network capability by default
@@ -1916,7 +1915,7 @@ public class OomAdjusterImpl extends OomAdjuster {
            // elevated to a high enough procstate anyway to get network unless they
            // request otherwise, so don't propagate the network capability by default
            // in this case unless they explicitly request it.
            if ((cstate.getCurCapability()
            if ((client.getCurCapability()
                    & PROCESS_CAPABILITY_POWER_RESTRICTED_NETWORK) != 0) {
                if (clientProcState <= PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
                    // This is used to grant network access to Expedited Jobs.
@@ -1927,7 +1926,7 @@ public class OomAdjusterImpl extends OomAdjuster {
                    capability |= PROCESS_CAPABILITY_POWER_RESTRICTED_NETWORK;
                }
            }
            if ((cstate.getCurCapability()
            if ((client.getCurCapability()
                    & PROCESS_CAPABILITY_USER_RESTRICTED_NETWORK) != 0) {
                if (clientProcState <= PROCESS_STATE_IMPORTANT_FOREGROUND) {
                    // This is used to grant network access to User Initiated Jobs.
@@ -1939,7 +1938,7 @@ public class OomAdjusterImpl extends OomAdjuster {

            // Sandbox should be able to control audio only when bound client
            // has this capability.
            if ((cstate.getCurCapability()
            if ((client.getCurCapability()
                    & PROCESS_CAPABILITY_FOREGROUND_AUDIO_CONTROL) != 0) {
                if (app.isSdkSandbox) {
                    capability |= PROCESS_CAPABILITY_FOREGROUND_AUDIO_CONTROL;
@@ -1970,7 +1969,7 @@ public class OomAdjusterImpl extends OomAdjuster {
                }
                // Not doing bind OOM management, so treat
                // this guy more like a started service.
                if (state.getHasShownUi() && !isHomeProcess(app)) {
                if (app.getHasShownUi() && !isHomeProcess(app)) {
                    // If this process has shown some UI, let it immediately
                    // go to the LRU list because it may be pretty heavy with
                    // UI stuff.  We'll tag it with a label just to help
@@ -1979,7 +1978,7 @@ public class OomAdjusterImpl extends OomAdjuster {
                        adjType = "cch-bound-ui-services";
                    }

                    if (state.isCached() && dryRun) {
                    if (app.isCached() && dryRun) {
                        // Bail out early, as we only care about the return value for a dryrun.
                        return true;
                    }
@@ -2006,7 +2005,7 @@ public class OomAdjusterImpl extends OomAdjuster {
                // is less important than a state that can be actively running, then we don't
                // care about the binding as much as we care about letting this process get into
                // the LRU list to be killed and restarted if needed for memory.
                if (state.getHasShownUi() && !isHomeProcess(app)
                if (app.getHasShownUi() && !isHomeProcess(app)
                        && clientAdj > CACHING_UI_SERVICE_CLIENT_ADJ_THRESHOLD) {
                    if (adj >= CACHED_APP_MIN_ADJ) {
                        adjType = "cch-bound-ui-services";
@@ -2066,8 +2065,8 @@ public class OomAdjusterImpl extends OomAdjuster {
                        }
                    }

                    if (!cstate.isCached()) {
                        if (state.isCached() && dryRun) {
                    if (!client.isCached()) {
                        if (app.isCached() && dryRun) {
                            // Bail out early, as we only care about the return value for a dryrun.
                            return true;
                        }
@@ -2080,7 +2079,7 @@ public class OomAdjusterImpl extends OomAdjuster {

                    if (adj >  newAdj) {
                        adj = newAdj;
                        if (state.setCurRawAdj(adj, dryRun)) {
                        if (app.setCurRawAdj(adj, dryRun)) {
                            // Bail out early, as we only care about the return value for a dryrun.
                        }
                        adjType = "service";
@@ -2092,7 +2091,7 @@ public class OomAdjusterImpl extends OomAdjuster {
                // This will treat important bound services identically to
                // the top app, which may behave differently than generic
                // foreground work.
                final int curSchedGroup = cstate.getCurrentSchedulingGroup();
                final int curSchedGroup = client.getCurrentSchedulingGroup();
                if (curSchedGroup > schedGroup) {
                    if (cr.hasFlag(Context.BIND_IMPORTANT)) {
                        schedGroup = curSchedGroup;
@@ -2120,18 +2119,18 @@ public class OomAdjusterImpl extends OomAdjuster {
                    // Go at most to BOUND_TOP, unless requested to elevate
                    // to client's state.
                    clientProcState = PROCESS_STATE_BOUND_TOP;
                    final boolean enabled = cstate.getCachedCompatChange(
                    final boolean enabled = client.getCachedCompatChange(
                            CACHED_COMPAT_CHANGE_PROCESS_CAPABILITY);
                    if (enabled) {
                        if (cr.hasFlag(Context.BIND_INCLUDE_CAPABILITIES)) {
                            // TOP process passes all capabilities to the service.
                            capability |= cstate.getCurCapability();
                            capability |= client.getCurCapability();
                        } else {
                            // TOP process passes no capability to the service.
                        }
                    } else {
                        // TOP process passes all capabilities to the service.
                        capability |= cstate.getCurCapability();
                        capability |= client.getCurCapability();
                    }
                }
            } else if (cr.notHasFlag(Context.BIND_IMPORTANT_BACKGROUND)) {
@@ -2154,7 +2153,7 @@ public class OomAdjusterImpl extends OomAdjuster {
                        return true;
                    }
                } else {
                    state.setScheduleLikeTopApp(true);
                    app.setScheduleLikeTopApp(true);
                }
            }

@@ -2164,7 +2163,7 @@ public class OomAdjusterImpl extends OomAdjuster {

            if (procState > clientProcState) {
                procState = clientProcState;
                if (state.setCurRawProcState(procState, dryRun)) {
                if (app.setCurRawProcState(procState, dryRun)) {
                    // Bail out early, as we only care about the return value for a dryrun.
                    return true;
                }
@@ -2177,12 +2176,11 @@ public class OomAdjusterImpl extends OomAdjuster {
                app.setPendingUiClean(true);
            }
            if (adjType != null && !dryRun) {
                state.setAdjType(adjType);
                state.setAdjTypeCode(ActivityManager.RunningAppProcessInfo
                        .REASON_SERVICE_IN_USE);
                state.setAdjSource(client);
                state.setAdjSourceProcState(clientProcState);
                state.setAdjTarget(cr.binding.service.instanceName);
                app.setAdjType(adjType);
                app.setAdjTypeCode(ActivityManager.RunningAppProcessInfo.REASON_SERVICE_IN_USE);
                app.setAdjSource(client);
                app.setAdjSourceProcState(clientProcState);
                app.setAdjTarget(cr.binding.service.instanceName);
                if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
                    reportOomAdjMessageLocked(TAG_OOM_ADJ, "Raise to " + adjType
                            + ": " + app + ", due to " + client
@@ -2216,14 +2214,14 @@ public class OomAdjusterImpl extends OomAdjuster {
        }
        if (cr.hasFlag(Context.BIND_TREAT_LIKE_ACTIVITY)) {
            if (!dryRun) {
                app.mServices.setTreatLikeActivity(true);
                app.setTreatLikeActivity(true);
            }
            if (clientProcState <= PROCESS_STATE_CACHED_ACTIVITY
                    && procState > PROCESS_STATE_CACHED_ACTIVITY) {
                // This is a cached process, but somebody wants us to treat it like it has
                // an activity, okay!
                procState = PROCESS_STATE_CACHED_ACTIVITY;
                state.setAdjType("cch-as-act");
                app.setAdjType("cch-as-act");
            }
        }
        final ActivityServiceConnectionsHolder a = cr.activity;
@@ -2231,7 +2229,7 @@ public class OomAdjusterImpl extends OomAdjuster {
            if (a != null && adj > FOREGROUND_APP_ADJ
                    && a.isActivityVisible()) {
                adj = FOREGROUND_APP_ADJ;
                if (state.setCurRawAdj(adj, dryRun)) {
                if (app.setCurRawAdj(adj, dryRun)) {
                    return true;
                }
                if (cr.notHasFlag(Context.BIND_NOT_FOREGROUND)) {
@@ -2243,12 +2241,11 @@ public class OomAdjusterImpl extends OomAdjuster {
                }

                if (!dryRun) {
                    state.setAdjType("service");
                    state.setAdjTypeCode(ActivityManager.RunningAppProcessInfo
                            .REASON_SERVICE_IN_USE);
                    state.setAdjSource(a);
                    state.setAdjSourceProcState(procState);
                    state.setAdjTarget(cr.binding.service.instanceName);
                    app.setAdjType("service");
                    app.setAdjTypeCode(ActivityManager.RunningAppProcessInfo.REASON_SERVICE_IN_USE);
                    app.setAdjSource(a);
                    app.setAdjSourceProcState(procState);
                    app.setAdjTarget(cr.binding.service.instanceName);
                    if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
                        reportOomAdjMessageLocked(TAG_OOM_ADJ,
                                "Raise to service w/activity: " + app);
@@ -2294,44 +2291,41 @@ public class OomAdjusterImpl extends OomAdjuster {
            setIntermediateProcStateLSP(app, procState);
        }
        if (schedGroup > prevSchedGroup) {
            setIntermediateSchedGroupLSP(state, schedGroup);
            setIntermediateSchedGroupLSP(app, schedGroup);
        }
        state.setCurCapability(capability);
        app.setCurCapability(capability);

        return updated;
    }

    @GuardedBy({"mService", "mProcLock"})
    @Override
    public boolean computeProviderHostOomAdjLSP(ContentProviderConnection conn, ProcessRecord app,
            ProcessRecord client, boolean dryRun) {
    public boolean computeProviderHostOomAdjLSP(ContentProviderConnection conn,
            ProcessRecordInternal app, ProcessRecordInternal client, boolean dryRun) {
        if (app.isPendingFinishAttach()) {
            // We've set the attaching process state in the computeInitialOomAdjLSP. Skip it here.
            return false;
        }

        final ProcessRecordInternal state = app;
        final ProcessRecordInternal cstate = client;

        if (client == app) {
            // Being our own client is not interesting.
            return false;
        }

        int clientAdj = cstate.getCurRawAdj();
        int clientProcState = cstate.getCurRawProcState();
        int clientAdj = client.getCurRawAdj();
        int clientProcState = client.getCurRawProcState();

        int adj = state.getCurRawAdj();
        int procState = state.getCurRawProcState();
        int schedGroup = state.getCurrentSchedulingGroup();
        int capability = state.getCurCapability();
        int adj = app.getCurRawAdj();
        int procState = app.getCurRawProcState();
        int schedGroup = app.getCurrentSchedulingGroup();
        int capability = app.getCurCapability();

        final int prevRawAdj = adj;
        final int prevProcState = procState;
        final int prevSchedGroup = schedGroup;
        final int prevCapability = capability;

        final int appUid = app.info.uid;
        final int appUid = app.getApplicationUid();
        final int logUid = mService.mCurOomAdjUid;

        // We always propagate PROCESS_CAPABILITY_BFSL to providers here,
@@ -2359,28 +2353,28 @@ public class OomAdjusterImpl extends OomAdjuster {
        }

        if (!dryRun) {
            state.setCurBoundByNonBgRestrictedApp(state.isCurBoundByNonBgRestrictedApp()
                    || cstate.isCurBoundByNonBgRestrictedApp()
            app.setCurBoundByNonBgRestrictedApp(app.isCurBoundByNonBgRestrictedApp()
                    || client.isCurBoundByNonBgRestrictedApp()
                    || clientProcState <= PROCESS_STATE_BOUND_TOP
                    || (clientProcState == PROCESS_STATE_FOREGROUND_SERVICE
                    && !cstate.isBackgroundRestricted()));
                    && !client.isBackgroundRestricted()));
        }

        String adjType = null;
        if (adj > clientAdj) {
            if (state.getHasShownUi() && !isHomeProcess(app)
            if (app.getHasShownUi() && !isHomeProcess(app)
                    && clientAdj > PERCEPTIBLE_APP_ADJ) {
                adjType = "cch-ui-provider";
            } else {
                adj = Math.max(clientAdj, FOREGROUND_APP_ADJ);
                if (state.setCurRawAdj(adj, dryRun)) {
                if (app.setCurRawAdj(adj, dryRun)) {
                    // Bail out early, as we only care about the return value for a dryrun.
                    return true;
                }
                adjType = "provider";
            }

            if (state.isCached() && !cstate.isCached() && dryRun) {
            if (app.isCached() && !client.isCached() && dryRun) {
                // Bail out early, as we only care about the return value for a dryrun.
                return true;
            }
@@ -2402,21 +2396,20 @@ public class OomAdjusterImpl extends OomAdjuster {
        }
        if (procState > clientProcState) {
            procState = clientProcState;
            if (state.setCurRawProcState(procState, dryRun)) {
            if (app.setCurRawProcState(procState, dryRun)) {
                // Bail out early, as we only care about the return value for a dryrun.
                return true;
            }
        }
        if (cstate.getCurrentSchedulingGroup() > schedGroup) {
        if (client.getCurrentSchedulingGroup() > schedGroup) {
            schedGroup = SCHED_GROUP_DEFAULT;
        }
        if (adjType != null && !dryRun) {
            state.setAdjType(adjType);
            state.setAdjTypeCode(ActivityManager.RunningAppProcessInfo
                    .REASON_PROVIDER_IN_USE);
            state.setAdjSource(client);
            state.setAdjSourceProcState(clientProcState);
            state.setAdjTarget(conn.provider.name);
            app.setAdjType(adjType);
            app.setAdjTypeCode(ActivityManager.RunningAppProcessInfo.REASON_PROVIDER_IN_USE);
            app.setAdjSource(client);
            app.setAdjSourceProcState(clientProcState);
            app.setAdjTarget(conn.provider.name);
            if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
                reportOomAdjMessageLocked(TAG_OOM_ADJ, "Raise to " + adjType
                        + ": " + app + ", due to " + client
@@ -2458,9 +2451,9 @@ public class OomAdjusterImpl extends OomAdjuster {
            setIntermediateProcStateLSP(app, procState);
        }
        if (schedGroup > prevSchedGroup) {
            setIntermediateSchedGroupLSP(state, schedGroup);
            setIntermediateSchedGroupLSP(app, schedGroup);
        }
        state.setCurCapability(capability);
        app.setCurCapability(capability);

        return false;
    }
+9 −0
Original line number Diff line number Diff line
@@ -1175,6 +1175,10 @@ class ProcessRecord extends ProcessRecordInternal implements WindowProcessListen
        return mServices.hasAboveClient();
    }

    @Override
    public void setTreatLikeActivity(boolean treatLikeActivity) {
        mServices.setTreatLikeActivity(treatLikeActivity);
    }

    @Override
    public boolean shouldNotFreeze() {
@@ -1192,6 +1196,11 @@ class ProcessRecord extends ProcessRecordInternal implements WindowProcessListen
        return mOptRecord.shouldNotFreezeReason();
    }

    @Override
    public int getApplicationUid() {
        return info.uid;
    }

    boolean hasActivitiesOrRecentTasks() {
        return mWindowProcessController.hasActivitiesOrRecentTasks();
    }
Loading