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

Commit a33690a4 authored by Suprabh Shukla's avatar Suprabh Shukla
Browse files

Grant FGS cpu_time capability by default

All FGS should be granted cpu_time to achieve parity with the current
implementation of mShouldNotFreeze.

Stricter behavior has been moved to the prototype flag which is meant to
have more aggressive policies and is still under development.

Flag: com.android.server.am.use_cpu_time_capability
Flag: com.android.server.am.prototype_aggressive_freezing

Test: atest FrameworksMockingServicesTests:MockingOomAdjusterTests

Bug: 370817323
Bug: 370798593

Change-Id: If9a7d991bf94d9ce5de6775c6c2d1f52de53efef
parent 73c4873c
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -3409,8 +3409,12 @@ public class OomAdjuster {
            // Process has user visible activities.
            return PROCESS_CAPABILITY_CPU_TIME;
        }
        if (Flags.prototypeAggressiveFreezing()) {
            if (app.mServices.hasUndemotedShortForegroundService(nowUptime)) {
            // It running a short fgs, just give it cpu time.
                // Grant cpu time for short FGS even when aggressively freezing.
                return PROCESS_CAPABILITY_CPU_TIME;
            }
        } else if (app.mServices.hasForegroundServices()) {
            return PROCESS_CAPABILITY_CPU_TIME;
        }
        if (app.mReceivers.numberOfCurReceivers() > 0) {
+41 −1
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import static android.app.ActivityManagerInternal.OOM_ADJ_REASON_ACTIVITY;
import static android.app.ActivityManagerInternal.OOM_ADJ_REASON_NONE;
import static android.app.ActivityManagerInternal.OOM_ADJ_REASON_SHORT_FGS_TIMEOUT;
import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE;
import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

@@ -107,6 +108,7 @@ import android.os.PowerManagerInternal;
import android.os.Process;
import android.os.SystemClock;
import android.os.UserHandle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.annotations.Presubmit;
import android.platform.test.flag.junit.SetFlagsRule;
@@ -698,7 +700,7 @@ public class MockingOomAdjusterTests {

    @SuppressWarnings("GuardedBy")
    @Test
    @EnableFlags(Flags.FLAG_USE_CPU_TIME_CAPABILITY)
    @EnableFlags({Flags.FLAG_USE_CPU_TIME_CAPABILITY, Flags.FLAG_PROTOTYPE_AGGRESSIVE_FREEZING})
    public void testUpdateOomAdjFreezeState_bindingFromShortFgs() {
        // Setting up a started short FGS within app1.
        final ServiceRecord s = ServiceRecord.newEmptyInstanceForTest(mService);
@@ -741,6 +743,44 @@ public class MockingOomAdjusterTests {
        assertNoCpuTime(app2);
    }

    @SuppressWarnings("GuardedBy")
    @Test
    @EnableFlags(Flags.FLAG_USE_CPU_TIME_CAPABILITY)
    @DisableFlags(Flags.FLAG_PROTOTYPE_AGGRESSIVE_FREEZING)
    public void testUpdateOomAdjFreezeState_bindingFromFgs() {
        final ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID,
                MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true));
        mProcessStateController.setHasForegroundServices(app.mServices, true,
                FOREGROUND_SERVICE_TYPE_SPECIAL_USE, false);

        final ProcessRecord app2 = spy(makeDefaultProcessRecord(MOCKAPP2_PID, MOCKAPP2_UID,
                MOCKAPP2_PROCESSNAME, MOCKAPP2_PACKAGENAME, false));
        // App with a foreground service binds to app2
        bindService(app2, app, null, null, 0, mock(IBinder.class));

        setProcessesToLru(app, app2);
        updateOomAdj(app);

        assertCpuTime(app);
        assertCpuTime(app2);
    }

    @SuppressWarnings("GuardedBy")
    @Test
    @EnableFlags(Flags.FLAG_USE_CPU_TIME_CAPABILITY)
    @DisableFlags(Flags.FLAG_PROTOTYPE_AGGRESSIVE_FREEZING)
    public void testUpdateOomAdjFreezeState_soloFgs() {
        final ProcessRecord app = spy(makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID,
                MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true));
        mProcessStateController.setHasForegroundServices(app.mServices, true,
                FOREGROUND_SERVICE_TYPE_SPECIAL_USE, false);

        setProcessesToLru(app);
        updateOomAdj(app);

        assertCpuTime(app);
    }

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