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

Commit 9d74e620 authored by Chih-Yu Huang's avatar Chih-Yu Huang
Browse files

am: Decouple ConnectionRecord with OomAdjuster

This change makes `ConnectionRecordInternal` implements the interface
`OomAdjusterImpl.Connection`.

Now the method `OomAdjuster.computeServiceHostOomAdjLSP` no longer uses
ConnectionRecord and switches to use `ConnectionRecordInternal`.

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

Change-Id: I9f724edd874436c8d6ca1b29fdff098c493a6da1
parent 49351e1b
Loading
Loading
Loading
Loading
+8 −18
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ 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.ConnectionRecordInternal;
import com.android.server.am.psc.ProcessRecordInternal;
import com.android.server.wm.ActivityServiceConnectionsHolder;

import java.io.PrintWriter;
@@ -42,7 +41,7 @@ import java.io.PrintWriter;
 * Description of a single binding to a service.
 */
@RavenwoodKeepWholeClass
final class ConnectionRecord extends ConnectionRecordInternal implements OomAdjusterImpl.Connection{
final class ConnectionRecord extends ConnectionRecordInternal {
    BoundServiceSession mBoundServiceSession;  // The associated bound service session if created.
    final AppBindRecord binding;    // The application/service binding.
    final ActivityServiceConnectionsHolder<ConnectionRecord> activity;  // If non-null, the owning activity.
@@ -141,28 +140,18 @@ final class ConnectionRecord extends ConnectionRecordInternal implements OomAdju
    }

    @Override
    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);
    public ActivityServiceConnectionsHolder<ConnectionRecord> getActivity() {
        return activity;
    }

    @Override
    public boolean canAffectCapabilities() {
        return hasFlag(Context.BIND_INCLUDE_CAPABILITIES
                | Context.BIND_BYPASS_USER_NETWORK_RESTRICTIONS);
    public long getServiceLastActivityTimeMillis() {
        return binding.service.lastActivity;
    }

    @Override
    public int cpuTimeTransmissionType() {
        if (getOngoingCalls()) {
            return CPU_TIME_TRANSMISSION_NORMAL;
        }
        if (hasFlag(Context.BIND_ALLOW_FREEZE)) {
            return CPU_TIME_TRANSMISSION_NONE;
        }
        return hasFlag(Context.BIND_SIMULATE_ALLOW_FREEZE) ? CPU_TIME_TRANSMISSION_LEGACY
                : CPU_TIME_TRANSMISSION_NORMAL;
    public ComponentName getServiceInstanceName() {
        return binding.service.instanceName;
    }

    public void startAssociationIfNeeded() {
@@ -191,6 +180,7 @@ final class ConnectionRecord extends ConnectionRecordInternal implements OomAdju
        }
    }

    @Override
    public void trackProcState(int procState, int seq) {
        if (association != null) {
            synchronized (mProcStatsLock) {
+2 −1
Original line number Diff line number Diff line
@@ -148,6 +148,7 @@ import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.ServiceThread;
import com.android.server.am.psc.ActiveUidsInternal;
import com.android.server.am.psc.ConnectionRecordInternal;
import com.android.server.am.psc.PlatformCompatCache;
import com.android.server.am.psc.PlatformCompatCache.CachedCompatChangeId;
import com.android.server.am.psc.ProcessRecordInternal;
@@ -1989,7 +1990,7 @@ 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,
    public abstract boolean computeServiceHostOomAdjLSP(ConnectionRecordInternal cr,
            ProcessRecordInternal app, ProcessRecordInternal client, long now, boolean dryRun);

    /**
+7 −6
Original line number Diff line number Diff line
@@ -105,6 +105,7 @@ import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.ServiceThread;
import com.android.server.am.psc.ActiveUidsInternal;
import com.android.server.am.psc.ConnectionRecordInternal;
import com.android.server.am.psc.ProcessRecordInternal;
import com.android.server.am.psc.UidRecordInternal;
import com.android.server.wm.ActivityServiceConnectionsHolder;
@@ -1848,8 +1849,8 @@ public class OomAdjusterImpl extends OomAdjuster {

    @GuardedBy({"mService", "mProcLock"})
    @Override
    public boolean computeServiceHostOomAdjLSP(ConnectionRecord cr, ProcessRecordInternal app,
            ProcessRecordInternal client, long now, boolean dryRun) {
    public boolean computeServiceHostOomAdjLSP(ConnectionRecordInternal 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;
@@ -1986,7 +1987,7 @@ public class OomAdjusterImpl extends OomAdjuster {
                    clientAdj = adj;
                    clientProcState = procState;
                } else {
                    if (now >= (cr.binding.service.lastActivity
                    if (now >= (cr.getServiceLastActivityTimeMillis()
                            + mConstants.MAX_SERVICE_INACTIVITY)) {
                        // This service has not seen activity within
                        // recent memory, so allow it to drop to the
@@ -2180,7 +2181,7 @@ public class OomAdjusterImpl extends OomAdjuster {
                app.setAdjTypeCode(ActivityManager.RunningAppProcessInfo.REASON_SERVICE_IN_USE);
                app.setAdjSource(client);
                app.setAdjSourceProcState(clientProcState);
                app.setAdjTarget(cr.binding.service.instanceName);
                app.setAdjTarget(cr.getServiceInstanceName());
                if (reportDebugMsgs) {
                    reportOomAdjMessageLocked(TAG_OOM_ADJ, "Raise to " + adjType
                            + ": " + app + ", due to " + client
@@ -2224,7 +2225,7 @@ public class OomAdjusterImpl extends OomAdjuster {
                app.setAdjType("cch-as-act");
            }
        }
        final ActivityServiceConnectionsHolder a = cr.activity;
        final ActivityServiceConnectionsHolder a = cr.getActivity();
        if (cr.hasFlag(Context.BIND_ADJUST_WITH_ACTIVITY)) {
            if (a != null && adj > FOREGROUND_APP_ADJ
                    && a.isActivityVisible()) {
@@ -2245,7 +2246,7 @@ public class OomAdjusterImpl extends OomAdjuster {
                    app.setAdjTypeCode(ActivityManager.RunningAppProcessInfo.REASON_SERVICE_IN_USE);
                    app.setAdjSource(a);
                    app.setAdjSourceProcState(procState);
                    app.setAdjTarget(cr.binding.service.instanceName);
                    app.setAdjTarget(cr.getServiceInstanceName());
                    if (reportDebugMsgs) {
                        reportOomAdjMessageLocked(TAG_OOM_ADJ,
                                "Raise to service w/activity: " + app);
+51 −1
Original line number Diff line number Diff line
@@ -16,19 +16,44 @@

package com.android.server.am.psc;

import android.content.ComponentName;
import android.content.Context;
import android.ravenwood.annotation.RavenwoodKeepWholeClass;

import com.android.server.am.OomAdjuster;
import com.android.server.am.OomAdjusterImpl;
import com.android.server.wm.ActivityServiceConnectionsHolder;

/**
 * An abstract base class encapsulating common internal properties and state for a single binding
 * to a service.
 */
@RavenwoodKeepWholeClass
public abstract class ConnectionRecordInternal {
public abstract class ConnectionRecordInternal implements OomAdjusterImpl.Connection {
    /** The service binding operation. */
    private final long mFlags;
    /** Whether there are currently ongoing transactions over this service connection. */
    private boolean mOngoingCalls;

    /** Returns the {@link ActivityServiceConnectionsHolder} associated with this connection. */
    public abstract ActivityServiceConnectionsHolder getActivity();

    /**
     * Returns the last activity time of the service associated with this connection,
     * in milliseconds.
     * TODO(b/425766486): Remove it once ConnectionRecordInternal could access ServiceRecord.
     */
    public abstract long getServiceLastActivityTimeMillis();

    /**
     * Returns the {@link ComponentName} of the service instance that this connection is bound to.
     * TODO(b/425766486): Remove it once ConnectionRecordInternal could access ServiceRecord.
     */
    public abstract ComponentName getServiceInstanceName();

    /** Tracks the current process state and sequence number for association management. */
    public abstract void trackProcState(int procState, int seq);

    public ConnectionRecordInternal(long flags) {
        this.mFlags = flags;
    }
@@ -79,4 +104,29 @@ public abstract class ConnectionRecordInternal {
        }
        return false;
    }

    @Override
    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);
    }

    @Override
    public boolean canAffectCapabilities() {
        return hasFlag(Context.BIND_INCLUDE_CAPABILITIES
                | Context.BIND_BYPASS_USER_NETWORK_RESTRICTIONS);
    }

    @Override
    public int cpuTimeTransmissionType() {
        if (mOngoingCalls) {
            return CPU_TIME_TRANSMISSION_NORMAL;
        }
        if (hasFlag(Context.BIND_ALLOW_FREEZE)) {
            return CPU_TIME_TRANSMISSION_NONE;
        }
        return hasFlag(Context.BIND_SIMULATE_ALLOW_FREEZE) ? CPU_TIME_TRANSMISSION_LEGACY
                : CPU_TIME_TRANSMISSION_NORMAL;
    }
}