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

Commit 54ae32ae authored by Michael Wachenschwanz's avatar Michael Wachenschwanz
Browse files

Push Broadcast related state to ProcessStateController

Also delete the current receiver ref counting and deprecated current
receiver tracking.
OomAdjuster and ActivityManagerService only really care whether a
receiver is running or not for a given process, so a boolean state
should be enough.

Flag: com.android.server.am.push_broadcast_state_to_oomadjuster
Fixes: 401350498
Test: atest MockingOomAdjusterTests
Test: atest BroadcastQueueTest
Change-Id: Ie89c0567064800bd7707c316a35b44daf68b7636
parent 87c96f5a
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -15839,7 +15839,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        for (int i = mProcessList.mRemovedProcesses.size() - 1; i >= 0; i--) {
            final ProcessRecord app = mProcessList.mRemovedProcesses.get(i);
            if (!app.hasActivitiesOrRecentTasks()
                    && app.mReceivers.numberOfCurReceivers() == 0
                    && !app.mReceivers.isReceivingBroadcast()
                    && app.mServices.numberOfRunningServices() == 0) {
                final IApplicationThread thread = app.getThread();
                Slog.i(TAG, "Exiting empty application process "
@@ -17041,7 +17041,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                            (WindowProcessController) procsToKill.get(i);
                    final ProcessRecord pr = (ProcessRecord) wpc.mOwner;
                    if (ActivityManager.isProcStateBackground(pr.mState.getSetProcState())
                            && pr.mReceivers.numberOfCurReceivers() == 0
                            && !pr.mReceivers.isReceivingBroadcast()
                            && !pr.mState.hasStartedServices()) {
                        pr.killLocked("remove task", ApplicationExitInfo.REASON_USER_REQUESTED,
                                ApplicationExitInfo.SUBREASON_REMOVE_TASK, true);
+0 −19
Original line number Diff line number Diff line
@@ -248,13 +248,6 @@ class BroadcastProcessQueue {
     */
    private long mProcessStartInitiatedTimestampMillis;

    /**
     * Indicates whether the number of current receivers has been incremented using
     * {@link ProcessReceiverRecord#incrementCurReceivers()}. This allows to skip decrementing
     * the receivers when it is not required.
     */
    private boolean mCurReceiversIncremented;

    public BroadcastProcessQueue(@NonNull BroadcastConstants constants,
            @NonNull String processName, int uid) {
        this.constants = Objects.requireNonNull(constants);
@@ -662,18 +655,6 @@ class BroadcastProcessQueue {
        return mActiveFirstLaunch;
    }

    public void incrementCurAppReceivers() {
        app.mReceivers.incrementCurReceivers();
        mCurReceiversIncremented = true;
    }

    public void decrementCurAppReceivers() {
        if (mCurReceiversIncremented) {
            app.mReceivers.decrementCurReceivers();
            mCurReceiversIncremented = false;
        }
    }

    public void setProcessStartInitiatedTimestampMillis(@UptimeMillisLong long timestampMillis) {
        mProcessStartInitiatedTimestampMillis = timestampMillis;
    }
+3 −5
Original line number Diff line number Diff line
@@ -77,7 +77,6 @@ import android.text.format.DateUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.IndentingPrintWriter;
import android.util.MathUtils;
import android.util.Pair;
import android.util.Slog;
import android.util.SparseArray;
@@ -2085,8 +2084,6 @@ class BroadcastQueueImpl extends BroadcastQueue {
    @GuardedBy("mService")
    private void notifyStartedRunning(@NonNull BroadcastProcessQueue queue) {
        if (queue.app != null) {
            queue.incrementCurAppReceivers();

            // Don't bump its LRU position if it's in the background restricted.
            if (mService.mInternal.getRestrictionLevel(
                    queue.uid) < ActivityManager.RESTRICTION_LEVEL_RESTRICTED_BUCKET) {
@@ -2096,6 +2093,8 @@ class BroadcastQueueImpl extends BroadcastQueue {
            mService.mOomAdjuster.unfreezeTemporarily(queue.app,
                    CachedAppOptimizer.UNFREEZE_REASON_START_RECEIVER);

            mService.mProcessStateController.noteBroadcastDeliveryStarted(queue.app,
                    queue.getPreferredSchedulingGroupLocked());
            if (queue.runningOomAdjusted) {
                queue.app.mState.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER);
                mService.enqueueOomAdjTargetLocked(queue.app);
@@ -2110,8 +2109,7 @@ class BroadcastQueueImpl extends BroadcastQueue {
    @GuardedBy("mService")
    private void notifyStoppedRunning(@NonNull BroadcastProcessQueue queue) {
        if (queue.app != null) {
            queue.decrementCurAppReceivers();

            mService.mProcessStateController.noteBroadcastDeliveryEnded(queue.app);
            if (queue.runningOomAdjusted) {
                mService.enqueueOomAdjTargetLocked(queue.app);
            }
+10 −2
Original line number Diff line number Diff line
@@ -1702,6 +1702,14 @@ public abstract class OomAdjuster {
        }
    }

    protected boolean isReceivingBroadcast(ProcessRecord app) {
        if (Flags.pushBroadcastStateToOomadjuster()) {
            return app.mReceivers.isReceivingBroadcast();
        } else {
            return app.mState.getCachedIsReceivingBroadcast(mTmpSchedGroup);
        }
    }

    /**
     * @return The proposed change to the schedGroup.
     */
@@ -1816,7 +1824,7 @@ public abstract class OomAdjuster {
        } else if (app.mServices.hasForegroundServices()) {
            return PROCESS_CAPABILITY_CPU_TIME;
        }
        if (app.mReceivers.numberOfCurReceivers() > 0) {
        if (app.mReceivers.isReceivingBroadcast()) {
            return PROCESS_CAPABILITY_CPU_TIME;
        }
        if (app.hasActiveInstrumentation()) {
@@ -1958,7 +1966,7 @@ public abstract class OomAdjuster {
        }

        final int curSchedGroup = state.getCurrentSchedulingGroup();
        if (app.getWaitingToKill() != null && app.mReceivers.numberOfCurReceivers() == 0
        if (app.getWaitingToKill() != null && !app.mReceivers.isReceivingBroadcast()
                && ActivityManager.isProcStateBackground(state.getCurProcState())
                && !state.hasStartedServices()) {
            app.killLocked(app.getWaitingToKill(), ApplicationExitInfo.REASON_USER_REQUESTED,
+7 −2
Original line number Diff line number Diff line
@@ -1474,13 +1474,18 @@ public class OomAdjusterImpl extends OomAdjuster {
            if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
                reportOomAdjMessageLocked(TAG_OOM_ADJ, "Making instrumentation: " + app);
            }
        } else if (state.getCachedIsReceivingBroadcast(mTmpSchedGroup)) {
        } else if (isReceivingBroadcast(app)) {
            // An app that is currently receiving a broadcast also
            // counts as being in the foreground for OOM killer purposes.
            // It's placed in a sched group based on the nature of the
            // broadcast as reflected by which queue it's active in.
            adj = FOREGROUND_APP_ADJ;
            if (Flags.pushBroadcastStateToOomadjuster()) {
                schedGroup = app.mReceivers.getBroadcastReceiverSchedGroup();
            } else {
                /// Priority was stored in mTmpSchedGroup by {@link #isReceivingBroadcast)
                schedGroup = mTmpSchedGroup[0];
            }
            state.setAdjType("broadcast");
            procState = ActivityManager.PROCESS_STATE_RECEIVER;
            if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
Loading