Loading core/java/android/content/Context.java +9 −2 Original line number Diff line number Diff line Loading @@ -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 = { Loading services/core/java/com/android/server/am/ConnectionRecord.java +7 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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 "); } Loading services/core/java/com/android/server/am/OomAdjuster.java +9 −6 Original line number Diff line number Diff line Loading @@ -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)) { Loading Loading @@ -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 Loading Loading @@ -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; } } /** Loading services/core/java/com/android/server/am/OomAdjusterModernImpl.java +9 −0 Original line number Diff line number Diff line Loading @@ -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; } } /** Loading services/tests/mockingservicestests/src/com/android/server/am/MockingOomAdjusterTests.java +30 −0 Original line number Diff line number Diff line Loading @@ -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) Loading Loading
core/java/android/content/Context.java +9 −2 Original line number Diff line number Diff line Loading @@ -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 = { Loading
services/core/java/com/android/server/am/ConnectionRecord.java +7 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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 "); } Loading
services/core/java/com/android/server/am/OomAdjuster.java +9 −6 Original line number Diff line number Diff line Loading @@ -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)) { Loading Loading @@ -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 Loading Loading @@ -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; } } /** Loading
services/core/java/com/android/server/am/OomAdjusterModernImpl.java +9 −0 Original line number Diff line number Diff line Loading @@ -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; } } /** Loading
services/tests/mockingservicestests/src/com/android/server/am/MockingOomAdjusterTests.java +30 −0 Original line number Diff line number Diff line Loading @@ -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) Loading