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

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

Grant CPU time to receivers and instrumentations

These are valid lifecycle states that require CPU and hence should be
granted the capability.

Test: atest FrameworksMockingServicesTests:MockingOomAdjusterTests

Flag: com.android.server.am.use_cpu_time_capability

Bug: 370817323
Bug: 370798593
Change-Id: I34aa245b5b31f697ed26838e18a70ed1d6bb09d5
parent 8e6240de
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -14565,7 +14565,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                    app.mProfile.addHostingComponentType(HOSTING_COMPONENT_TYPE_INSTRUMENTATION);
                }
                app.setActiveInstrumentation(activeInstr);
                mProcessStateController.setActiveInstrumentation(app, activeInstr);
                activeInstr.mFinished = false;
                activeInstr.mSourceUid = callingUid;
                activeInstr.mRunningProcesses.add(app);
@@ -14711,7 +14711,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                        abiOverride,
                        ZYGOTE_POLICY_FLAG_EMPTY);
                app.setActiveInstrumentation(activeInstr);
                mProcessStateController.setActiveInstrumentation(app, activeInstr);
                activeInstr.mFinished = false;
                activeInstr.mSourceUid = callingUid;
                activeInstr.mRunningProcesses.add(app);
@@ -14848,7 +14848,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                }
                instr.removeProcess(app);
                app.setActiveInstrumentation(null);
                mProcessStateController.setActiveInstrumentation(app, null);
            }
            app.mProfile.clearHostingComponentType(HOSTING_COMPONENT_TYPE_INSTRUMENTATION);
+4 −2
Original line number Diff line number Diff line
@@ -2477,13 +2477,15 @@ public class AppProfiler {
                            // This is the wildcard mode, where every process brought up for
                            // the target instrumentation should be included.
                            if (aInstr.mTargetInfo.packageName.equals(app.info.packageName)) {
                                app.setActiveInstrumentation(aInstr);
                                mService.mProcessStateController.setActiveInstrumentation(app,
                                        aInstr);
                                aInstr.mRunningProcesses.add(app);
                            }
                        } else {
                            for (String proc : aInstr.mTargetProcesses) {
                                if (proc.equals(app.processName)) {
                                    app.setActiveInstrumentation(aInstr);
                                    mService.mProcessStateController.setActiveInstrumentation(app,
                                            aInstr);
                                    aInstr.mRunningProcesses.add(app);
                                    break;
                                }
+7 −1
Original line number Diff line number Diff line
@@ -3403,7 +3403,7 @@ public class OomAdjuster {
    private static int getCpuCapability(ProcessRecord app, long nowUptime) {
        final UidRecord uidRec = app.getUidRecord();
        if (uidRec != null && uidRec.isCurAllowListed()) {
            // Process has user visible activities.
            // Process is in the power allowlist.
            return PROCESS_CAPABILITY_CPU_TIME;
        }
        if (UserHandle.isCore(app.uid)) {
@@ -3418,6 +3418,12 @@ public class OomAdjuster {
            // It running a short fgs, just give it cpu time.
            return PROCESS_CAPABILITY_CPU_TIME;
        }
        if (app.mReceivers.numberOfCurReceivers() > 0) {
            return PROCESS_CAPABILITY_CPU_TIME;
        }
        if (app.hasActiveInstrumentation()) {
            return PROCESS_CAPABILITY_CPU_TIME;
        }
        // TODO(b/370817323): Populate this method with all of the reasons to keep a process
        //  unfrozen.
        return 0;
+32 −5
Original line number Diff line number Diff line
@@ -246,12 +246,11 @@ public class ProcessStateController {
    }

    /**
     * Set what sched group to grant a process due to running a broadcast.
     * {@link ProcessList.SCHED_GROUP_UNDEFINED} means the process is not running a broadcast.
     * Sets an active instrumentation running within the given process.
     */
    public void setBroadcastSchedGroup(@NonNull ProcessRecord proc, int schedGroup) {
        // TODO(b/302575389): Migrate state pulled from BroadcastQueue to a pushed model
        throw new UnsupportedOperationException("Not implemented yet");
    public void setActiveInstrumentation(@NonNull ProcessRecord proc,
            ActiveInstrumentation activeInstrumentation) {
        proc.setActiveInstrumentation(activeInstrumentation);
    }

    /********************* Process Visibility State Events *********************/
@@ -587,6 +586,34 @@ public class ProcessStateController {
        psr.updateHasTopStartedAlmostPerceptibleServices();
    }

    /************************ Broadcast Receiver State Events **************************/
    /**
     * Set what sched group to grant a process due to running a broadcast.
     * {@link ProcessList.SCHED_GROUP_UNDEFINED} means the process is not running a broadcast.
     */
    public void setBroadcastSchedGroup(@NonNull ProcessRecord proc, int schedGroup) {
        // TODO(b/302575389): Migrate state pulled from BroadcastQueue to a pushed model
        throw new UnsupportedOperationException("Not implemented yet");
    }

    /**
     * Note that the process has started processing a broadcast receiver.
     */
    public boolean incrementCurReceivers(@NonNull ProcessRecord app) {
        // TODO(b/302575389): Migrate state pulled from ATMS to a pushed model
        // maybe used ActivityStateFlags instead.
        throw new UnsupportedOperationException("Not implemented yet");
    }

    /**
     * Note that the process has finished processing a broadcast receiver.
     */
    public boolean decrementCurReceivers(@NonNull ProcessRecord app) {
        // TODO(b/302575389): Migrate state pulled from ATMS to a pushed model
        // maybe used ActivityStateFlags instead.
        throw new UnsupportedOperationException("Not implemented yet");
    }

    /**
     * Builder for ProcessStateController.
     */
+37 −0
Original line number Diff line number Diff line
@@ -741,6 +741,43 @@ public class MockingOomAdjusterTests {
        assertNoCpuTime(app2);
    }

    @SuppressWarnings("GuardedBy")
    @Test
    @EnableFlags(Flags.FLAG_USE_CPU_TIME_CAPABILITY)
    public void testUpdateOomAdjFreezeState_receivers() {
        final ProcessRecord app = makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID,
                MOCKAPP_PROCESSNAME, MOCKAPP_PACKAGENAME, true);

        updateOomAdj(app);
        assertNoCpuTime(app);

        app.mReceivers.incrementCurReceivers();
        updateOomAdj(app);
        assertCpuTime(app);

        app.mReceivers.decrementCurReceivers();
        updateOomAdj(app);
        assertNoCpuTime(app);
    }

    @SuppressWarnings("GuardedBy")
    @Test
    @EnableFlags(Flags.FLAG_USE_CPU_TIME_CAPABILITY)
    public void testUpdateOomAdjFreezeState_activeInstrumentation() {
        ProcessRecord app = makeDefaultProcessRecord(MOCKAPP_PID, MOCKAPP_UID, MOCKAPP_PROCESSNAME,
                MOCKAPP_PACKAGENAME, true);
        updateOomAdj(app);
        assertNoCpuTime(app);

        mProcessStateController.setActiveInstrumentation(app, mock(ActiveInstrumentation.class));
        updateOomAdj(app);
        assertCpuTime(app);

        mProcessStateController.setActiveInstrumentation(app, null);
        updateOomAdj(app);
        assertNoCpuTime(app);
    }

    @SuppressWarnings("GuardedBy")
    @Test
    public void testUpdateOomAdj_DoOne_OverlayUi() {