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

Commit 5999affa authored by Yu-Ting Tseng's avatar Yu-Ting Tseng Committed by Android (Google) Code Review
Browse files

Merge "Add tracing for updateAppFreezeStateLSP." into main

parents 4425fb7e fdb0e916
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -127,6 +127,7 @@ import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SERVICE;
import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_SERVICE_EXECUTING;
import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
import static com.android.server.am.ProcessList.UNKNOWN_ADJ;

import android.Manifest;
import android.annotation.IntDef;
@@ -4304,7 +4305,7 @@ public final class ActiveServices {
                // queued up in the app side as they're one way calls. And we'll also hold off
                // the service timeout timer until the process is unfrozen.
                mAm.mOomAdjuster.updateAppFreezeStateLSP(callerApp, OOM_ADJ_REASON_BIND_SERVICE,
                        true);
                        true, UNKNOWN_ADJ);
            }

            final boolean wasStopped = hostApp == null ? wasStopped(s) : false;
+3 −3
Original line number Diff line number Diff line
@@ -83,6 +83,8 @@ import com.android.internal.os.ProcLocksReader;
import com.android.internal.util.FrameworkStatsLog;
import com.android.server.ServiceThread;

import dalvik.annotation.optimization.NeverCompile;

import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
@@ -98,8 +100,6 @@ import java.util.Map;
import java.util.Random;
import java.util.Set;

import dalvik.annotation.optimization.NeverCompile;

public final class CachedAppOptimizer {

    // Flags stored in the DeviceConfig API.
@@ -248,7 +248,7 @@ public final class CachedAppOptimizer {
    private static final int COMPACT_ACTION_ANON_FLAG = 2;

    private static final String ATRACE_COMPACTION_TRACK = "Compaction";
    private static final String ATRACE_FREEZER_TRACK = "Freezer";
    public static final String ATRACE_FREEZER_TRACK = "Freezer";

    private static final int FREEZE_BINDER_TIMEOUT_MS = 0;
    private static final int FREEZE_DEADLOCK_TIMEOUT_MS = 1000;
+40 −9
Original line number Diff line number Diff line
@@ -1832,7 +1832,8 @@ public class OomAdjuster {
            state.setNoKillOnBgRestrictedAndIdle(false);
            // If this UID is currently allowlisted, it should not be frozen.
            final UidRecord uidRec = app.getUidRecord();
            app.mOptRecord.setShouldNotFreeze(uidRec != null && uidRec.isCurAllowListed());
            app.mOptRecord.setShouldNotFreeze(uidRec != null && uidRec.isCurAllowListed(),
                    ProcessCachedOptimizerRecord.SHOULD_NOT_FREEZE_REASON_UID_ALLOWLISTED, mAdjSeq);
        }

        final int appUid = app.info.uid;
@@ -2672,7 +2673,9 @@ public class OomAdjuster {

        if (client.mOptRecord.shouldNotFreeze()) {
            // Propagate the shouldNotFreeze flag down the bindings.
            if (app.mOptRecord.setShouldNotFreeze(true, dryRun)) {
            if (app.mOptRecord.setShouldNotFreeze(true, dryRun,
                    app.mOptRecord.shouldNotFreezeReason()
                    | client.mOptRecord.shouldNotFreezeReason(), mAdjSeq)) {
                // Bail out early, as we only care about the return value for a dryrun.
                return true;
            }
@@ -2739,7 +2742,10 @@ public class OomAdjuster {
            if (cr.hasFlag(Context.BIND_ALLOW_OOM_MANAGEMENT)) {
                // Similar to BIND_WAIVE_PRIORITY, keep it unfrozen.
                if (clientAdj < CACHED_APP_MIN_ADJ) {
                    if (app.mOptRecord.setShouldNotFreeze(true, dryRun)) {
                    if (app.mOptRecord.setShouldNotFreeze(true, dryRun,
                            app.mOptRecord.shouldNotFreezeReason()
                            | ProcessCachedOptimizerRecord
                            .SHOULD_NOT_FREEZE_REASON_BINDER_ALLOW_OOM_MANAGEMENT, mAdjSeq)) {
                        // Bail out early, as we only care about the return value for a dryrun.
                        return true;
                    }
@@ -2976,7 +2982,10 @@ public class OomAdjuster {
            // bound by an unfrozen app via a WPRI binding has to remain
            // unfrozen.
            if (clientAdj < CACHED_APP_MIN_ADJ) {
                if (app.mOptRecord.setShouldNotFreeze(true, dryRun)) {
                if (app.mOptRecord.setShouldNotFreeze(true, dryRun,
                        app.mOptRecord.shouldNotFreezeReason()
                        | ProcessCachedOptimizerRecord
                        .SHOULD_NOT_FREEZE_REASON_BIND_WAIVE_PRIORITY, mAdjSeq)) {
                    // Bail out early, as we only care about the return value for a dryrun.
                    return true;
                }
@@ -3117,7 +3126,9 @@ public class OomAdjuster {
        }
        if (client.mOptRecord.shouldNotFreeze()) {
            // Propagate the shouldNotFreeze flag down the bindings.
            if (app.mOptRecord.setShouldNotFreeze(true, dryRun)) {
            if (app.mOptRecord.setShouldNotFreeze(true, dryRun,
                    app.mOptRecord.shouldNotFreezeReason()
                    | client.mOptRecord.shouldNotFreezeReason(), mAdjSeq)) {
                // Bail out early, as we only care about the return value for a dryrun.
                return true;
            }
@@ -3476,7 +3487,7 @@ public class OomAdjuster {
            changes |= ActivityManagerService.ProcessChangeItem.CHANGE_ACTIVITIES;
        }

        updateAppFreezeStateLSP(app, oomAdjReson, false);
        updateAppFreezeStateLSP(app, oomAdjReson, false, oldOomAdj);

        if (state.getReportedProcState() != state.getCurProcState()) {
            state.setReportedProcState(state.getCurProcState());
@@ -3873,16 +3884,37 @@ public class OomAdjuster {

    @GuardedBy({"mService", "mProcLock"})
    void updateAppFreezeStateLSP(ProcessRecord app, @OomAdjReason int oomAdjReason,
            boolean immediate) {
            boolean immediate, int oldOomAdj) {
        if (!mCachedAppOptimizer.useFreezer()) {
            return;
        }

        final ProcessCachedOptimizerRecord opt = app.mOptRecord;
        final ProcessStateRecord state = app.mState;
        if (Flags.traceUpdateAppFreezeStateLsp()) {
            final boolean oomAdjChanged =
                    (state.getCurAdj() >= FREEZER_CUTOFF_ADJ ^ oldOomAdj >= FREEZER_CUTOFF_ADJ)
                    || oldOomAdj == UNKNOWN_ADJ;
            final boolean shouldNotFreezeChanged = opt.shouldNotFreezeAdjSeq() == mAdjSeq;
            if ((oomAdjChanged || shouldNotFreezeChanged)
                    && Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {
                Trace.instantForTrack(Trace.TRACE_TAG_ACTIVITY_MANAGER,
                        CachedAppOptimizer.ATRACE_FREEZER_TRACK,
                        "updateAppFreezeStateLSP " + app.processName
                        + " isFreezeExempt: " + opt.isFreezeExempt()
                        + " isFrozen: " + opt.isFrozen()
                        + " shouldNotFreeze: " + opt.shouldNotFreeze()
                        + " shouldNotFreezeReason: " + opt.shouldNotFreezeReason()
                        + " curAdj: " + state.getCurAdj()
                        + " oldOomAdj: " + oldOomAdj
                        + " immediate: " + immediate);
            }
        }

        if (app.mOptRecord.isFreezeExempt()) {
            return;
        }

        final ProcessCachedOptimizerRecord opt = app.mOptRecord;
        // if an app is already frozen and shouldNotFreeze becomes true, immediately unfreeze
        if (opt.isFrozen() && opt.shouldNotFreeze()) {
            mCachedAppOptimizer.unfreezeAppLSP(app,
@@ -3890,7 +3922,6 @@ public class OomAdjuster {
            return;
        }

        final ProcessStateRecord state = app.mState;
        // Use current adjustment when freezing, set adjustment when unfreezing.
        if (state.getCurAdj() >= FREEZER_CUTOFF_ADJ && !opt.isFrozen()
                && !opt.shouldNotFreeze()) {
+59 −5
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.am;

import android.annotation.IntDef;
import android.annotation.UptimeMillisLong;
import android.app.ActivityManagerInternal.OomAdjReason;
import android.util.TimeUtils;
@@ -23,14 +24,31 @@ import android.util.TimeUtils;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

import java.io.PrintWriter;

import dalvik.annotation.optimization.NeverCompile;

import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * The state info of app when it's cached, used by the optimizer.
 */
final class ProcessCachedOptimizerRecord {

    static final int SHOULD_NOT_FREEZE_REASON_NONE = 1;
    static final int SHOULD_NOT_FREEZE_REASON_UID_ALLOWLISTED = 1 << 1;
    static final int SHOULD_NOT_FREEZE_REASON_BINDER_ALLOW_OOM_MANAGEMENT = 1 << 2;
    static final int SHOULD_NOT_FREEZE_REASON_BIND_WAIVE_PRIORITY = 1 << 3;

    @IntDef(flag = true, prefix = {"SHOULD_NOT_FREEZE_REASON_"}, value = {
        SHOULD_NOT_FREEZE_REASON_NONE,
        SHOULD_NOT_FREEZE_REASON_UID_ALLOWLISTED,
        SHOULD_NOT_FREEZE_REASON_BINDER_ALLOW_OOM_MANAGEMENT,
        SHOULD_NOT_FREEZE_REASON_BIND_WAIVE_PRIORITY,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface ShouldNotFreezeReason {}

    private final ProcessRecord mApp;

    private final ActivityManagerGlobalLock mProcLock;
@@ -111,6 +129,18 @@ final class ProcessCachedOptimizerRecord {
    @GuardedBy("mProcLock")
    private boolean mShouldNotFreeze;

    /**
     * Reason for mShouldNotFreeze being set to a particular value.
     */
    @GuardedBy("mProcLock")
    private @ShouldNotFreezeReason int mShouldNotFreezeReason;

    /**
     * The value of adjSeq when last time mShouldNotFreeze was set.
     */
    @GuardedBy("mProcLock")
    private int mShouldNotFreezeAdjSeq;

    /**
     * Exempt from freezer (now for system apps with INSTALL_PACKAGES permission)
     */
@@ -275,8 +305,19 @@ final class ProcessCachedOptimizerRecord {
    }

    @GuardedBy("mProcLock")
    void setShouldNotFreeze(boolean shouldNotFreeze) {
        setShouldNotFreeze(shouldNotFreeze, false);
    @ShouldNotFreezeReason int shouldNotFreezeReason() {
        return mShouldNotFreezeReason;
    }

    @GuardedBy("mProcLock")
    int shouldNotFreezeAdjSeq() {
        return mShouldNotFreezeAdjSeq;
    }

    @GuardedBy("mProcLock")
    void setShouldNotFreeze(boolean shouldNotFreeze, @ShouldNotFreezeReason int reason,
            int adjSeq) {
        setShouldNotFreeze(shouldNotFreeze, false, reason, adjSeq);
    }

    /**
@@ -284,10 +325,23 @@ final class ProcessCachedOptimizerRecord {
     * if it was a real run.
     */
    @GuardedBy("mProcLock")
    boolean setShouldNotFreeze(boolean shouldNotFreeze, boolean dryRun) {
    boolean setShouldNotFreeze(boolean shouldNotFreeze, boolean dryRun,
            @ShouldNotFreezeReason int reason, int adjSeq) {
        if (dryRun) {
            return mFrozen && !shouldNotFreeze;
        }
        if (Flags.traceUpdateAppFreezeStateLsp()) {
            if (shouldNotFreeze) {
                reason &= ~SHOULD_NOT_FREEZE_REASON_NONE;
            } else {
                reason = SHOULD_NOT_FREEZE_REASON_NONE;
            }

            if (reason != mShouldNotFreezeReason || shouldNotFreeze != mShouldNotFreeze) {
                mShouldNotFreezeAdjSeq = adjSeq;
            }
            mShouldNotFreezeReason = reason;
        }
        mShouldNotFreeze = shouldNotFreeze;
        return false;
    }
+8 −0
Original line number Diff line number Diff line
@@ -124,6 +124,14 @@ flag {
    bug: "323376416"
}

flag {
    name: "trace_update_app_freeze_state_lsp"
    namespace: "backstage_power"
    description: "Output process oom adj and other metadata to trace in updateAppFreezeStateLSP"
    bug: "345547733"
    is_fixed_read_only: true
}

flag {
    name: "simplify_process_traversal"
    namespace: "backstage_power"