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

Commit 835a268a authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Create PROCESS_CAPABILITY_IMPLICT_CPU_TIME" into main

parents 64b7571c d6c808fd
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -1019,6 +1019,14 @@ public class ActivityManager {
     */
    public static final int PROCESS_CAPABILITY_CPU_TIME = 1 << 7;

    /**
     * @hide
     * Process does not have an explicit reason to be run on the CPU but it is below the
     * FREEZER_CUTOFF_ADJ, so avoid freezing it.
     * TODO: b/403034947 - Delete this capability once no longer needed.
     */
    public static final int PROCESS_CAPABILITY_IMPLICIT_CPU_TIME = 1 << 8;

    /**
     * @hide all capabilities, the ORing of all flags in {@link ProcessCapability}.
     *
@@ -1032,7 +1040,8 @@ public class ActivityManager {
            | PROCESS_CAPABILITY_BFSL
            | PROCESS_CAPABILITY_USER_RESTRICTED_NETWORK
            | PROCESS_CAPABILITY_FOREGROUND_AUDIO_CONTROL
            | PROCESS_CAPABILITY_CPU_TIME;
            | PROCESS_CAPABILITY_CPU_TIME
            | PROCESS_CAPABILITY_IMPLICIT_CPU_TIME;

    /**
     * All implicit capabilities. This capability set is currently only used for processes under
@@ -1058,6 +1067,7 @@ public class ActivityManager {
        pw.print((caps & PROCESS_CAPABILITY_USER_RESTRICTED_NETWORK) != 0 ? 'U' : '-');
        pw.print((caps & PROCESS_CAPABILITY_FOREGROUND_AUDIO_CONTROL) != 0 ? 'A' : '-');
        pw.print((caps & PROCESS_CAPABILITY_CPU_TIME) != 0 ? 'T' : '-');
        pw.print((caps & PROCESS_CAPABILITY_IMPLICIT_CPU_TIME) != 0 ? 'I' : '-');
    }

    /** @hide */
@@ -1071,6 +1081,7 @@ public class ActivityManager {
        sb.append((caps & PROCESS_CAPABILITY_USER_RESTRICTED_NETWORK) != 0 ? 'U' : '-');
        sb.append((caps & PROCESS_CAPABILITY_FOREGROUND_AUDIO_CONTROL) != 0 ? 'A' : '-');
        sb.append((caps & PROCESS_CAPABILITY_CPU_TIME) != 0 ? 'T' : '-');
        sb.append((caps & PROCESS_CAPABILITY_IMPLICIT_CPU_TIME) != 0 ? 'I' : '-');
    }

    /**
+2 −3
Original line number Diff line number Diff line
@@ -2154,11 +2154,10 @@ public class CachedAppOptimizer {
                            synchronized (mAm.mPidsSelfLocked) {
                                pr = mAm.mPidsSelfLocked.get(blocked);
                            }
                            if (pr != null && pr.mState.getCurAdj()
                                    < mAm.mConstants.FREEZER_CUTOFF_ADJ) {
                            if (pr != null && !pr.isFreezable()) {
                                Slog.d(TAG_AM, app.processName + " (" + pid + ") blocks "
                                        + pr.processName + " (" + blocked + ")");
                                // Found at least one blocked non-cached process
                                // Found at least one blocked unfrozen process
                                unfreezeAppLSP(app, UNFREEZE_REASON_FILE_LOCKS);
                                break;
                            }
+79 −46
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.app.ActivityManager.PROCESS_CAPABILITY_ALL;
import static android.app.ActivityManager.PROCESS_CAPABILITY_ALL_IMPLICIT;
import static android.app.ActivityManager.PROCESS_CAPABILITY_BFSL;
import static android.app.ActivityManager.PROCESS_CAPABILITY_CPU_TIME;
import static android.app.ActivityManager.PROCESS_CAPABILITY_IMPLICIT_CPU_TIME;
import static android.app.ActivityManager.PROCESS_CAPABILITY_FOREGROUND_AUDIO_CONTROL;
import static android.app.ActivityManager.PROCESS_CAPABILITY_FOREGROUND_CAMERA;
import static android.app.ActivityManager.PROCESS_CAPABILITY_FOREGROUND_LOCATION;
@@ -448,6 +449,9 @@ public class OomAdjuster {

    static final long PERCEPTIBLE_TASK_TIMEOUT_MILLIS = 5 * 60 * 1000;

    static final int ALL_CPU_TIME_CAPABILITIES =
            PROCESS_CAPABILITY_CPU_TIME | PROCESS_CAPABILITY_IMPLICIT_CPU_TIME;

    @VisibleForTesting
    public static class Injector {
        boolean isChangeEnabled(@CachedCompatChangeId int cachedCompatChangeId,
@@ -2644,6 +2648,7 @@ public class OomAdjuster {

        capability |= getDefaultCapability(app, procState);
        capability |= getCpuCapability(app, now, foregroundActivities);
        capability |= getImplicitCpuCapability(app, adj);

        // Procstates below BFGS should never have this capability.
        if (procState > PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
@@ -2786,7 +2791,7 @@ public class OomAdjuster {
            if (app.mOptRecord.setShouldNotFreeze(true, dryRun,
                    app.mOptRecord.shouldNotFreezeReason()
                    | client.mOptRecord.shouldNotFreezeReason(), mAdjSeq)) {
                if (Flags.useCpuTimeCapability()) {
                if (Flags.cpuTimeCapabilityBasedFreezePolicy()) {
                    // Do nothing, capability updated check will handle the dryrun output.
                } else {
                    // Bail out early, as we only care about the return value for a dryrun.
@@ -2862,7 +2867,7 @@ public class OomAdjuster {
                            app.mOptRecord.shouldNotFreezeReason()
                            | ProcessCachedOptimizerRecord
                            .SHOULD_NOT_FREEZE_REASON_BINDER_ALLOW_OOM_MANAGEMENT, mAdjSeq)) {
                        if (Flags.useCpuTimeCapability()) {
                        if (Flags.cpuTimeCapabilityBasedFreezePolicy()) {
                            // Do nothing, capability updated check will handle the dryrun output.
                        } else {
                            // Bail out early, as we only care about the return value for a dryrun.
@@ -3107,7 +3112,7 @@ public class OomAdjuster {
                        app.mOptRecord.shouldNotFreezeReason()
                        | ProcessCachedOptimizerRecord
                        .SHOULD_NOT_FREEZE_REASON_BIND_WAIVE_PRIORITY, mAdjSeq)) {
                    if (Flags.useCpuTimeCapability()) {
                    if (Flags.cpuTimeCapabilityBasedFreezePolicy()) {
                        // Do nothing, capability updated check will handle the dryrun output.
                    } else {
                        // Bail out early, as we only care about the return value for a dryrun.
@@ -3170,15 +3175,15 @@ public class OomAdjuster {
                updated = true;
            }

            if (Flags.useCpuTimeCapability()) {
            if (Flags.cpuTimeCapabilityBasedFreezePolicy()) {
                if ((capability != prevCapability)
                        && ((capability & prevCapability) == prevCapability)) {
                    updated = true;
                }
            } else {
                // Ignore PROCESS_CAPABILITY_CPU_TIME in capability comparison
                final int curFiltered = capability & ~PROCESS_CAPABILITY_CPU_TIME;
                final int prevFiltered = prevCapability & ~PROCESS_CAPABILITY_CPU_TIME;
                // Ignore CPU related capabilities in comparison
                final int curFiltered = capability & ~ALL_CPU_TIME_CAPABILITIES;
                final int prevFiltered = prevCapability & ~ALL_CPU_TIME_CAPABILITIES;
                if ((curFiltered != prevFiltered)
                        && ((curFiltered & prevFiltered) == prevFiltered)) {
                    updated = true;
@@ -3271,7 +3276,7 @@ public class OomAdjuster {
            if (app.mOptRecord.setShouldNotFreeze(true, dryRun,
                    app.mOptRecord.shouldNotFreezeReason()
                    | client.mOptRecord.shouldNotFreezeReason(), mAdjSeq)) {
                if (Flags.useCpuTimeCapability()) {
                if (Flags.cpuTimeCapabilityBasedFreezePolicy()) {
                    // Do nothing, capability updated check will handle the dryrun output.
                } else {
                    // Bail out early, as we only care about the return value for a dryrun.
@@ -3357,15 +3362,15 @@ public class OomAdjuster {
                return true;
            }

            if (Flags.useCpuTimeCapability()) {
            if (Flags.cpuTimeCapabilityBasedFreezePolicy()) {
                if ((capability != prevCapability)
                        && ((capability & prevCapability) == prevCapability)) {
                    return true;
                }
            } else {
                // Ignore PROCESS_CAPABILITY_CPU_TIME in capability comparison
                final int curFiltered = capability & ~PROCESS_CAPABILITY_CPU_TIME;
                final int prevFiltered = prevCapability & ~PROCESS_CAPABILITY_CPU_TIME;
                // Ignore CPU related capabilities in comparison
                final int curFiltered = capability & ~ALL_CPU_TIME_CAPABILITIES;
                final int prevFiltered = prevCapability & ~ALL_CPU_TIME_CAPABILITIES;
                if ((curFiltered != prevFiltered)
                        && ((curFiltered & prevFiltered) == prevFiltered)) {
                    return true;
@@ -3455,6 +3460,17 @@ public class OomAdjuster {
        return 0;
    }

    // Grant PROCESS_CAPABILITY_IMPLICIT_CPU_TIME to processes based on oom adj score.
    private int getImplicitCpuCapability(ProcessRecord app, int adj) {
        if (adj < mConstants.FREEZER_CUTOFF_ADJ) {
            return PROCESS_CAPABILITY_IMPLICIT_CPU_TIME;
        }
        if (app.mState.getMaxAdj() < mConstants.FREEZER_CUTOFF_ADJ) {
            return PROCESS_CAPABILITY_IMPLICIT_CPU_TIME;
        }
        return 0;
    }

    /**
     * @return the BFSL capability from a client (of a service binding or provider).
     */
@@ -3508,7 +3524,7 @@ public class OomAdjuster {
    private static int getCpuCapabilityFromClient(OomAdjusterModernImpl.Connection conn,
            ProcessRecord client) {
        if (conn == null || conn.transmitsCpuTime()) {
            return client.mState.getCurCapability() & PROCESS_CAPABILITY_CPU_TIME;
            return client.mState.getCurCapability() & ALL_CPU_TIME_CAPABILITIES;
        } else {
            return 0;
        }
@@ -3859,8 +3875,8 @@ public class OomAdjuster {
    void setAttachingProcessStatesLSP(ProcessRecord app) {
        int initialSchedGroup = SCHED_GROUP_DEFAULT;
        int initialProcState = PROCESS_STATE_CACHED_EMPTY;
        int initialCapability =  PROCESS_CAPABILITY_NONE;
        boolean initialCached = true;
            // Avoid freezing a freshly attached process.
        int initialCapability = ALL_CPU_TIME_CAPABILITIES;
        final ProcessStateRecord state = app.mState;
        final int prevProcState = state.getCurProcState();
        final int prevAdj = state.getCurRawAdj();
@@ -3882,7 +3898,6 @@ public class OomAdjuster {
                    initialProcState = PROCESS_STATE_TOP;
                }
                initialCapability = PROCESS_CAPABILITY_ALL;
                initialCached = false;
            } catch (Exception e) {
                Slog.w(TAG, "Failed to pre-set top priority to " + app + " " + e);
            }
@@ -4096,28 +4111,34 @@ public class OomAdjuster {
    /**
     * Return whether or not a process should be frozen.
     */
    boolean getFreezePolicy(ProcessRecord proc) {
        // Reasons to not freeze:
        if (Flags.useCpuTimeCapability()) {
            if ((proc.mState.getCurCapability() & PROCESS_CAPABILITY_CPU_TIME) != 0) {
                /// App is important enough (see {@link #getCpuCapability}) or bound by something
                /// important enough to not be frozen.
    static boolean getFreezePolicy(ProcessRecord proc) {
        if (Flags.cpuTimeCapabilityBasedFreezePolicy()) {
            if ((proc.mState.getCurCapability() & ALL_CPU_TIME_CAPABILITIES) != 0) {
                /// App is important enough (see {@link #getCpuCapability} and
                /// {@link #getImplicitCpuCapability}) or bound by something important enough to
                /// not be frozen (see {@link #getCpuCapabilityFromClient}).
                return false;
            }

            if (proc.mOptRecord.isFreezeExempt()) {
                return false;
            }

            // Default, freeze a process.
            return true;
        } else {
            // The CPU capability handling covers all setShouldNotFreeze paths. Must check
            // shouldNotFreeze, if the CPU capability is not being used.
            if (proc.mOptRecord.shouldNotFreeze()) {
                return false;
            }
        }

            if (proc.mOptRecord.isFreezeExempt()) {
                return false;
            }

            // Reasons to freeze:
        if (proc.mState.getCurAdj() >= mConstants.FREEZER_CUTOFF_ADJ) {
            if (proc.mState.getCurAdj() >= CACHED_APP_MIN_ADJ) {
                // Oomscore is in a high enough state, it is safe to freeze.
                return true;
            }
@@ -4125,6 +4146,7 @@ public class OomAdjuster {
            // Default, do not freeze a process.
            return false;
        }
    }

    @GuardedBy({"mService", "mProcLock"})
    void updateAppFreezeStateLSP(ProcessRecord app, @OomAdjReason int oomAdjReason,
@@ -4147,7 +4169,16 @@ public class OomAdjuster {
                    (PROCESS_CAPABILITY_CPU_TIME & app.mState.getSetCapability())
                            == PROCESS_CAPABILITY_CPU_TIME;
            final boolean cpuCapabilityChanged = hasCpuCapability != usedToHaveCpuCapability;
            if ((oomAdjChanged || shouldNotFreezeChanged || cpuCapabilityChanged)
            final boolean hasImplicitCpuCapability =
                    (PROCESS_CAPABILITY_IMPLICIT_CPU_TIME & app.mState.getCurCapability())
                            == PROCESS_CAPABILITY_IMPLICIT_CPU_TIME;
            final boolean usedToHaveImplicitCpuCapability =
                    (PROCESS_CAPABILITY_IMPLICIT_CPU_TIME & app.mState.getSetCapability())
                            == PROCESS_CAPABILITY_IMPLICIT_CPU_TIME;
            final boolean implicitCpuCapabilityChanged =
                    hasImplicitCpuCapability != usedToHaveImplicitCpuCapability;
            if ((oomAdjChanged || shouldNotFreezeChanged || cpuCapabilityChanged
                    || implicitCpuCapabilityChanged)
                    && Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
                Trace.instantForTrack(Trace.TRACE_TAG_ACTIVITY_MANAGER,
                        "FreezeLite",
@@ -4156,9 +4187,10 @@ public class OomAdjuster {
                        + (opt.isFreezeExempt() ? "E" : "-")
                        + (opt.shouldNotFreeze() ? "N" : "-")
                        + (hasCpuCapability ? "T" : "-")
                        + (hasImplicitCpuCapability ? "X" : "-")
                        + (immediate ? "I" : "-")
                        + (freezePolicy ? "Z" : "-")
                        + (Flags.useCpuTimeCapability() ? "t" : "-")
                        + (Flags.cpuTimeCapabilityBasedFreezePolicy() ? "t" : "-")
                        + (Flags.prototypeAggressiveFreezing() ? "a" : "-")
                        + "/" + app.getPid()
                        + "/" + state.getCurAdj()
@@ -4175,7 +4207,8 @@ public class OomAdjuster {
                        + " curAdj: " + state.getCurAdj()
                        + " oldOomAdj: " + oldOomAdj
                        + " immediate: " + immediate
                        + " cpuCapability: " + hasCpuCapability);
                        + " cpuCapability: " + hasCpuCapability
                        + " implicitCpuCapability: " + hasImplicitCpuCapability);
            }
        }

@@ -4294,10 +4327,10 @@ public class OomAdjuster {
                        != client.getSetCapability()) {
            // The connection might elevate the importance of the service's capabilities.
            needDryRun = true;
        } else if (Flags.useCpuTimeCapability()
        } else if (Flags.cpuTimeCapabilityBasedFreezePolicy()
                && (client.getSetCapability() & ~app.getSetCapability()
                    & PROCESS_CAPABILITY_CPU_TIME) != 0) {
            // The connection might grant PROCESS_CAPABILITY_CPU_TIME to the service.
                    & ALL_CPU_TIME_CAPABILITIES) != 0) {
            // The connection might grant CPU capability to the service.
            needDryRun = true;
        } else if (Flags.unfreezeBindPolicyFix()
                && cr.hasFlag(Context.BIND_WAIVE_PRIORITY
@@ -4346,9 +4379,9 @@ public class OomAdjuster {
                && client.mOptRecord.shouldNotFreeze()) {
            // Process has shouldNotFreeze and it could have gotten it from the client.
            return true;
        } else if (Flags.useCpuTimeCapability()
        } else if (Flags.cpuTimeCapabilityBasedFreezePolicy()
                && (client.getSetCapability() & app.getSetCapability()
                    & PROCESS_CAPABILITY_CPU_TIME) != 0) {
                & ALL_CPU_TIME_CAPABILITIES) != 0) {
            return true;
        }
        return false;
@@ -4369,10 +4402,10 @@ public class OomAdjuster {
                && client.mOptRecord.shouldNotFreeze()
                && !app.mOptRecord.shouldNotFreeze()) {
            needDryRun = true;
        } else if (Flags.useCpuTimeCapability()
        } else if (Flags.cpuTimeCapabilityBasedFreezePolicy()
                && (client.getSetCapability() & ~app.getSetCapability()
                    & PROCESS_CAPABILITY_CPU_TIME) != 0) {
            // The connection might grant PROCESS_CAPABILITY_CPU_TIME to the provider.
                & ALL_CPU_TIME_CAPABILITIES) != 0) {
            // The connection might grant CPU capability to the provider.
            needDryRun = true;
        }

@@ -4400,9 +4433,9 @@ public class OomAdjuster {
                && client.mOptRecord.shouldNotFreeze()) {
            // Process has shouldNotFreeze and it could have gotten it from the client.
            return true;
        } else if (Flags.useCpuTimeCapability()
        } else if (Flags.cpuTimeCapabilityBasedFreezePolicy()
                && (client.getSetCapability() & app.getSetCapability()
                    & PROCESS_CAPABILITY_CPU_TIME) != 0) {
                    & ALL_CPU_TIME_CAPABILITIES) != 0) {
            return true;
        }

+1 −4
Original line number Diff line number Diff line
@@ -1727,10 +1727,7 @@ class ProcessRecord implements WindowProcessListener {
    }

    boolean isFreezable() {
        return mService.mOomAdjuster.mCachedAppOptimizer.useFreezer()
                && !mOptRecord.isFreezeExempt()
                && !mOptRecord.shouldNotFreeze()
                && mState.getCurAdj() >= mService.mConstants.FREEZER_CUTOFF_ADJ;
        return OomAdjuster.getFreezePolicy(this);
    }

    public void forEachConnectionHost(Consumer<ProcessRecord> consumer) {
+5 −2
Original line number Diff line number Diff line
@@ -256,10 +256,13 @@ flag {
}

flag {
    name: "use_cpu_time_capability"
    name: "cpu_time_capability_based_freeze_policy"
    namespace: "backstage_power"
    description: "Use PROCESS_CAPABILITY_CPU_TIME to control unfreeze state."
    description: "Use PROCESS_CAPABILITY_CPU_TIME and PROCESS_CAPABILITY_IMPLICIT_CPU_TIME to control process freeze state."
    bug: "370817323"
    metadata {
        purpose: PURPOSE_BUGFIX
    }
}

flag {
Loading