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

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

Merge "Add a bind flag to allow the host to be frozen" into main

parents 2e3384a8 b6a87509
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -744,15 +744,22 @@ public abstract class Context {
     */
    public static final long BIND_MATCH_QUARANTINED_COMPONENTS = 0x2_0000_0000L;

    /**
     * Flag for {@link #bindService} that allows the bound app to be frozen if it is eligible.
     *
     * @hide
     */
    public static final long BIND_ALLOW_FREEZE = 0x4_0000_0000L;

    /**
     * These bind flags reduce the strength of the binding such that we shouldn't
     * consider it as pulling the process up to the level of the one that is bound to it.
     * @hide
     */
    public static final int BIND_REDUCTION_FLAGS =
    public static final long BIND_REDUCTION_FLAGS =
            Context.BIND_ALLOW_OOM_MANAGEMENT | Context.BIND_WAIVE_PRIORITY
                    | Context.BIND_NOT_PERCEPTIBLE | Context.BIND_NOT_VISIBLE;
                    | Context.BIND_NOT_PERCEPTIBLE | Context.BIND_NOT_VISIBLE
                    | Context.BIND_ALLOW_FREEZE;

    /** @hide */
    @IntDef(flag = true, prefix = { "RECEIVER_VISIBLE" }, value = {
+7 −0
Original line number Diff line number Diff line
@@ -142,6 +142,10 @@ final class ConnectionRecord implements OomAdjusterModernImpl.Connection{
                | Context.BIND_BYPASS_USER_NETWORK_RESTRICTIONS);
    }

    @Override
    public boolean transmitsCpuTime() {
        return !hasFlag(Context.BIND_ALLOW_FREEZE);
    }

    public long getFlags() {
        return flags;
@@ -273,6 +277,9 @@ final class ConnectionRecord implements OomAdjusterModernImpl.Connection{
        if (hasFlag(Context.BIND_INCLUDE_CAPABILITIES)) {
            sb.append("CAPS ");
        }
        if (hasFlag(Context.BIND_ALLOW_FREEZE)) {
            sb.append("!CPU ");
        }
        if (serviceDead) {
            sb.append("DEAD ");
        }
+9 −6
Original line number Diff line number Diff line
@@ -2802,7 +2802,7 @@ public class OomAdjuster {
        // we check the final procstate, and remove it if the procsate is below BFGS.
        capability |= getBfslCapabilityFromClient(client);

        capability |= getCpuCapabilityFromClient(client);
        capability |= getCpuCapabilityFromClient(cr, client);

        if (cr.notHasFlag(Context.BIND_WAIVE_PRIORITY)) {
            if (cr.hasFlag(Context.BIND_INCLUDE_CAPABILITIES)) {
@@ -3259,7 +3259,7 @@ public class OomAdjuster {
        // we check the final procstate, and remove it if the procsate is below BFGS.
        capability |= getBfslCapabilityFromClient(client);

        capability |= getCpuCapabilityFromClient(client);
        capability |= getCpuCapabilityFromClient(conn, client);

        if (clientProcState >= PROCESS_STATE_CACHED_ACTIVITY) {
            // If the other app is cached for any reason, for purposes here
@@ -3502,10 +3502,13 @@ public class OomAdjuster {
    /**
     * @return the CPU capability from a client (of a service binding or provider).
     */
    private static int getCpuCapabilityFromClient(ProcessRecord client) {
        // Just grant CPU capability every time
        // TODO(b/370817323): Populate with reasons to not propagate cpu capability across bindings.
    private static int getCpuCapabilityFromClient(OomAdjusterModernImpl.Connection conn,
            ProcessRecord client) {
        if (conn == null || conn.transmitsCpuTime()) {
            return client.mState.getCurCapability() & PROCESS_CAPABILITY_CPU_TIME;
        } else {
            return 0;
        }
    }

    /**
+9 −0
Original line number Diff line number Diff line
@@ -635,6 +635,15 @@ public class OomAdjusterModernImpl extends OomAdjuster {
         * Returns true if this connection can propagate capabilities.
         */
        boolean canAffectCapabilities();

        /**
         * Returns whether this connection transmits PROCESS_CAPABILITY_CPU_TIME to the host, if the
         * client possesses it.
         */
        default boolean transmitsCpuTime() {
            // Always lend this capability by default.
            return true;
        }
    }

    /**
+30 −0
Original line number Diff line number Diff line
@@ -743,6 +743,36 @@ public class MockingOomAdjusterTests {
        assertNoCpuTime(app2);
    }

    @SuppressWarnings("GuardedBy")
    @Test
    @EnableFlags(Flags.FLAG_USE_CPU_TIME_CAPABILITY)
    public void testUpdateOomAdjFreezeState_bindingWithAllowFreeze() {
        ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID,
                MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true));
        WindowProcessController wpc = app.getWindowProcessController();
        doReturn(true).when(wpc).hasVisibleActivities();

        final ProcessRecord app2 = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID,
                MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false));

        // App with a visible activity binds to app2 without any special flag.
        bindService(app2, app, null, null, 0, mock(IBinder.class));

        final ProcessRecord app3 = spy(makeDefaultProcessRecord(MOCKAPP3_PID, MOCKAPP3_UID,
                MOCKAPP3_PROCESSNAME, MOCKAPP3_PACKAGENAME, false));

        // App with a visible activity binds to app3 with ALLOW_FREEZE.
        bindService(app3, app, null, null, Context.BIND_ALLOW_FREEZE, mock(IBinder.class));

        setProcessesToLru(app, app2, app3);

        updateOomAdj(app);

        assertCpuTime(app);
        assertCpuTime(app2);
        assertNoCpuTime(app3);
    }

    @SuppressWarnings("GuardedBy")
    @Test
    @EnableFlags(Flags.FLAG_USE_CPU_TIME_CAPABILITY)