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

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

psc: Migrate ProcessRecord freeze APIs to ProcessRecordInternal

The `shouldNotFreeze()`, `setShouldNotFreeze()`, and
`shouldNotFreezeReason()` methods are now abstract methods in
`ProcessRecordInternal`. `ProcessRecord` implements these methods by
delegating to its `ProcessCachedOptimizerRecord` (`mOptRecord`).

This change unblocks `OomAdjuster.computeServiceHostOomAdjLSP()` to use
`ProcessRecordInternal` for its `client` parameter.

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

Change-Id: If0f0bdae6f7770586f8b1575fc10e3d3ce7d6f0f
parent d04c367e
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -1994,7 +1994,7 @@ public abstract class OomAdjuster {
     */
    @GuardedBy({"mService", "mProcLock"})
    public abstract boolean computeServiceHostOomAdjLSP(ConnectionRecord cr, ProcessRecord app,
            ProcessRecord client, long now, boolean dryRun);
            ProcessRecordInternal client, long now, boolean dryRun);

    /**
     * Computes the impact on {@code app} the provider connections from {@code client} has.
@@ -2139,7 +2139,7 @@ public abstract class OomAdjuster {
     * @return the CPU capability from a client (of a service binding or provider).
     */
    protected static int getCpuCapabilitiesFromClient(ProcessRecordInternal app,
            ProcessRecord client, OomAdjusterImpl.Connection conn) {
            ProcessRecordInternal client, OomAdjusterImpl.Connection conn) {
        final int clientCpuCaps = client.getCurCapability() & ALL_CPU_TIME_CAPABILITIES;
        final @CpuTimeReasons int clientCpuReasons = client.getCurCpuTimeReasons();
        final @ImplicitCpuTimeReasons int clientImplicitCpuReasons =
@@ -2800,7 +2800,7 @@ public abstract class OomAdjuster {
                        + "/" + app.getPid()
                        + "/" + state.getCurAdj()
                        + "/" + oldOomAdj
                        + "/" + opt.shouldNotFreezeReason()
                        + "/" + app.shouldNotFreezeReason()
                        + "/" + cpuTimeReasons
                        + "/" + implicitCpuTimeReasons);
                Trace.instantForTrack(Trace.TRACE_TAG_ACTIVITY_MANAGER,
@@ -2809,8 +2809,8 @@ public abstract class OomAdjuster {
                        + " pid: " + app.getPid()
                        + " isFreezeExempt: " + opt.isFreezeExempt()
                        + " isFrozen: " + opt.isFrozen()
                        + " shouldNotFreeze: " + opt.shouldNotFreeze()
                        + " shouldNotFreezeReason: " + opt.shouldNotFreezeReason()
                        + " shouldNotFreeze: " + app.shouldNotFreeze()
                        + " shouldNotFreezeReason: " + app.shouldNotFreezeReason()
                        + " curAdj: " + state.getCurAdj()
                        + " oldOomAdj: " + oldOomAdj
                        + " immediate: " + immediate
+11 −13
Original line number Diff line number Diff line
@@ -1848,7 +1848,7 @@ public class OomAdjusterImpl extends OomAdjuster {
    @GuardedBy({"mService", "mProcLock"})
    @Override
    public boolean computeServiceHostOomAdjLSP(ConnectionRecord cr, ProcessRecord app,
            ProcessRecord client, long now, boolean dryRun) {
            ProcessRecordInternal client, long now, boolean dryRun) {
        if (app.isPendingFinishAttach()) {
            // We've set the attaching process state in the computeInitialOomAdjLSP. Skip it here.
            return false;
@@ -1884,11 +1884,10 @@ public class OomAdjusterImpl extends OomAdjuster {
                    && !cstate.isBackgroundRestricted()));
        }

        if (client.mOptRecord.shouldNotFreeze()) {
        if (client.shouldNotFreeze()) {
            // Propagate the shouldNotFreeze flag down the bindings.
            if (app.mOptRecord.setShouldNotFreeze(true, dryRun,
                    app.mOptRecord.shouldNotFreezeReason()
                            | client.mOptRecord.shouldNotFreezeReason(), mAdjSeq)) {
            if (app.setShouldNotFreeze(true, dryRun,
                    app.shouldNotFreezeReason() | client.shouldNotFreezeReason(), mAdjSeq)) {
                if (Flags.cpuTimeCapabilityBasedFreezePolicy()) {
                    // Do nothing, capability updated check will handle the dryrun output.
                } else {
@@ -1957,8 +1956,8 @@ public class OomAdjusterImpl extends OomAdjuster {
            if (cr.hasFlag(Context.BIND_ALLOW_OOM_MANAGEMENT)) {
                // Similar to BIND_WAIVE_PRIORITY, keep it unfrozen.
                if (clientAdj < CACHED_APP_MIN_ADJ) {
                    if (app.mOptRecord.setShouldNotFreeze(true, dryRun,
                            app.mOptRecord.shouldNotFreezeReason()
                    if (app.setShouldNotFreeze(true, dryRun,
                            app.shouldNotFreezeReason()
                                    | SHOULD_NOT_FREEZE_REASON_BINDER_ALLOW_OOM_MANAGEMENT,
                            mAdjSeq)) {
                        if (Flags.cpuTimeCapabilityBasedFreezePolicy()) {
@@ -2202,8 +2201,8 @@ public class OomAdjusterImpl extends OomAdjuster {
            // bound by an unfrozen app via a WPRI binding has to remain
            // unfrozen.
            if (clientAdj < CACHED_APP_MIN_ADJ) {
                if (app.mOptRecord.setShouldNotFreeze(true, dryRun,
                        app.mOptRecord.shouldNotFreezeReason()
                if (app.setShouldNotFreeze(true, dryRun,
                        app.shouldNotFreezeReason()
                                | ProcessCachedOptimizerRecord
                                .SHOULD_NOT_FREEZE_REASON_BIND_WAIVE_PRIORITY, mAdjSeq)) {
                    if (Flags.cpuTimeCapabilityBasedFreezePolicy()) {
@@ -2346,11 +2345,10 @@ public class OomAdjusterImpl extends OomAdjuster {
            // we are going to consider it empty.
            clientProcState = PROCESS_STATE_CACHED_EMPTY;
        }
        if (client.mOptRecord.shouldNotFreeze()) {
        if (client.shouldNotFreeze()) {
            // Propagate the shouldNotFreeze flag down the bindings.
            if (app.mOptRecord.setShouldNotFreeze(true, dryRun,
                    app.mOptRecord.shouldNotFreezeReason()
                            | client.mOptRecord.shouldNotFreezeReason(), mAdjSeq)) {
            if (app.setShouldNotFreeze(true, dryRun,
                    app.shouldNotFreezeReason() | client.shouldNotFreezeReason(), mAdjSeq)) {
                if (Flags.cpuTimeCapabilityBasedFreezePolicy()) {
                    // Do nothing, capability updated check will handle the dryrun output.
                } else {
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ import java.util.concurrent.Executor;
/**
 * The state info of app when it's cached, used by the optimizer.
 */
final class ProcessCachedOptimizerRecord {
public final class ProcessCachedOptimizerRecord {

    static final int SHOULD_NOT_FREEZE_REASON_NONE = 1;
    static final int SHOULD_NOT_FREEZE_REASON_UID_ALLOWLISTED = 1 << 1;
+18 −0
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ import com.android.internal.app.procstats.ProcessStats;
import com.android.internal.os.Zygote;
import com.android.server.FgThread;
import com.android.server.am.OomAdjusterImpl.ProcessRecordNode;
import com.android.server.am.ProcessCachedOptimizerRecord.ShouldNotFreezeReason;
import com.android.server.am.psc.PlatformCompatCache.CachedCompatChangeId;
import com.android.server.am.psc.ProcessRecordInternal;
import com.android.server.wm.WindowProcessController;
@@ -1174,6 +1175,23 @@ class ProcessRecord extends ProcessRecordInternal implements WindowProcessListen
        return mServices.hasAboveClient();
    }


    @Override
    public boolean shouldNotFreeze() {
        return mOptRecord.shouldNotFreeze();
    }

    @Override
    public boolean setShouldNotFreeze(boolean shouldNotFreeze, boolean dryRun,
            @ShouldNotFreezeReason int reason, int adjSeq) {
        return mOptRecord.setShouldNotFreeze(shouldNotFreeze, dryRun, reason, adjSeq);
    }

    @Override
    public @ShouldNotFreezeReason int shouldNotFreezeReason() {
        return mOptRecord.shouldNotFreezeReason();
    }

    boolean hasActivitiesOrRecentTasks() {
        return mWindowProcessController.hasActivitiesOrRecentTasks();
    }
+11 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ import com.android.internal.annotations.CompositeRWLock;
import com.android.internal.annotations.GuardedBy;
import com.android.server.am.Flags;
import com.android.server.am.OomAdjuster;
import com.android.server.am.ProcessCachedOptimizerRecord.ShouldNotFreezeReason;
import com.android.server.am.psc.PlatformCompatCache.CachedCompatChangeId;

import java.io.PrintWriter;
@@ -197,6 +198,16 @@ public abstract class ProcessRecordInternal {
    /** Returns true if there is an active instrumentation running in this process. */
    public abstract boolean hasActiveInstrumentation();

    /** Returns whether this process should be exempt from freezing. */
    public abstract boolean shouldNotFreeze();

    /** Sets whether this process should be exempt from freezing and records the reason. */
    public abstract boolean setShouldNotFreeze(boolean shouldNotFreeze, boolean dryRun,
            @ShouldNotFreezeReason int reason, int adjSeq);

    /** Returns the aggregated reasons why this process is currently exempt from freezing. */
    public abstract @ShouldNotFreezeReason int shouldNotFreezeReason();

    // Enable this to trace all OomAdjuster state transitions
    private static final boolean TRACE_OOM_ADJ = false;