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

Commit 0085aee0 authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Proactively drop SHORT FGS procstate on timeout...

... Rather than waiting for the next "natural" oom-adjustment.

Bug: 257270313
Test: atest CtsShortFgsTestCases
Test: Manual test using a test app
checked all 3 events -- timeout, procstate demotion, and ANR

Change-Id: I0fcbccd2bb2ec1449284696b60f21022f6f67136
parent bdf3e2ea
Loading
Loading
Loading
Loading
+30 −4
Original line number Diff line number Diff line
@@ -2939,6 +2939,8 @@ public final class ActiveServices {

    void unscheduleShortFgsTimeoutLocked(ServiceRecord sr) {
        mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_SHORT_FGS_ANR_TIMEOUT_MSG, sr);
        mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_SHORT_FGS_PROCSTATE_TIMEOUT_MSG,
                sr);
        mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_SHORT_FGS_TIMEOUT_MSG, sr);
    }

@@ -2992,12 +2994,21 @@ public final class ActiveServices {
            } catch (RemoteException e) {
                // TODO(short-service): Anything to do here?
            }
            // Schedule the ANR timeout.
            // Schedule the procstate demotion timeout and ANR timeout.
            {
                final Message msg = mAm.mHandler.obtainMessage(
                        ActivityManagerService.SERVICE_SHORT_FGS_PROCSTATE_TIMEOUT_MSG, sr);
                mAm.mHandler.sendMessageAtTime(
                        msg, sr.getShortFgsInfo().getProcStateDemoteTime());
            }

            {
                final Message msg = mAm.mHandler.obtainMessage(
                        ActivityManagerService.SERVICE_SHORT_FGS_ANR_TIMEOUT_MSG, sr);
                mAm.mHandler.sendMessageAtTime(msg, sr.getShortFgsInfo().getAnrTime());
            }
        }
    }

    boolean shouldServiceTimeOutLocked(ComponentName className, IBinder token) {
        final int userId = UserHandle.getCallingUserId();
@@ -3013,6 +3024,21 @@ public final class ActiveServices {
        }
    }

    void onShortFgsProcstateTimeout(ServiceRecord sr) {
        synchronized (mAm) {
            if (!sr.shouldDemoteShortFgsProcState()) {
                if (DEBUG_SHORT_SERVICE) {
                    Slog.d(TAG_SERVICE, "[STALE] Short FGS procstate demotion: " + sr);
                }
                return;
            }

            Slog.e(TAG_SERVICE, "Short FGS procstate demoted: " + sr);

            mAm.updateOomAdjLocked(sr.app, OomAdjuster.OOM_ADJ_REASON_SHORT_FGS_TIMEOUT);
        }
    }

    void onShortFgsAnrTimeout(ServiceRecord sr) {
        final String reason = "A foreground service of FOREGROUND_SERVICE_TYPE_SHORT_SERVICE"
                + " did not stop within a timeout: " + sr.getComponentName();
+5 −1
Original line number Diff line number Diff line
@@ -1542,7 +1542,8 @@ public class ActivityManagerService extends IActivityManager.Stub
    static final int DISPATCH_SENDING_BROADCAST_EVENT = 74;
    static final int DISPATCH_BINDING_SERVICE_EVENT = 75;
    static final int SERVICE_SHORT_FGS_TIMEOUT_MSG = 76;
    static final int SERVICE_SHORT_FGS_ANR_TIMEOUT_MSG = 77;
    static final int SERVICE_SHORT_FGS_PROCSTATE_TIMEOUT_MSG = 77;
    static final int SERVICE_SHORT_FGS_ANR_TIMEOUT_MSG = 78;
    static final int FIRST_BROADCAST_QUEUE_MSG = 200;
@@ -1880,6 +1881,9 @@ public class ActivityManagerService extends IActivityManager.Stub
                case SERVICE_SHORT_FGS_TIMEOUT_MSG: {
                    mServices.onShortFgsTimeout((ServiceRecord) msg.obj);
                } break;
                case SERVICE_SHORT_FGS_PROCSTATE_TIMEOUT_MSG: {
                    mServices.onShortFgsProcstateTimeout((ServiceRecord) msg.obj);
                } break;
                case SERVICE_SHORT_FGS_ANR_TIMEOUT_MSG: {
                    mServices.onShortFgsAnrTimeout((ServiceRecord) msg.obj);
                } break;
+6 −1
Original line number Diff line number Diff line
@@ -163,6 +163,7 @@ public class OomAdjuster {
    static final int OOM_ADJ_REASON_ALLOWLIST = 10;
    static final int OOM_ADJ_REASON_PROCESS_BEGIN = 11;
    static final int OOM_ADJ_REASON_PROCESS_END = 12;
    static final int OOM_ADJ_REASON_SHORT_FGS_TIMEOUT = 13;

    @IntDef(prefix = {"OOM_ADJ_REASON_"},
            value = {OOM_ADJ_REASON_NONE, OOM_ADJ_REASON_ACTIVITY, OOM_ADJ_REASON_FINISH_RECEIVER,
@@ -170,7 +171,8 @@ public class OomAdjuster {
                    OOM_ADJ_REASON_UNBIND_SERVICE, OOM_ADJ_REASON_START_SERVICE,
                    OOM_ADJ_REASON_GET_PROVIDER, OOM_ADJ_REASON_REMOVE_PROVIDER,
                    OOM_ADJ_REASON_UI_VISIBILITY, OOM_ADJ_REASON_ALLOWLIST,
                    OOM_ADJ_REASON_PROCESS_BEGIN, OOM_ADJ_REASON_PROCESS_END})
                    OOM_ADJ_REASON_PROCESS_BEGIN, OOM_ADJ_REASON_PROCESS_END,
                    OOM_ADJ_REASON_SHORT_FGS_TIMEOUT})
    @Retention(RetentionPolicy.SOURCE)
    public @interface OomAdjReason {}

@@ -202,6 +204,7 @@ public class OomAdjuster {
                return AppProtoEnums.OOM_ADJ_REASON_PROCESS_BEGIN;
            case OOM_ADJ_REASON_PROCESS_END:
                return AppProtoEnums.OOM_ADJ_REASON_PROCESS_END;
            case OOM_ADJ_REASON_SHORT_FGS_TIMEOUT: // TODO(short-service) add value to AppProtoEnums
            default:
                return AppProtoEnums.OOM_ADJ_REASON_UNKNOWN_TO_PROTO;
        }
@@ -236,6 +239,8 @@ public class OomAdjuster {
                return OOM_ADJ_REASON_METHOD + "_processBegin";
            case OOM_ADJ_REASON_PROCESS_END:
                return OOM_ADJ_REASON_METHOD + "_processEnd";
            case OOM_ADJ_REASON_SHORT_FGS_TIMEOUT:
                return OOM_ADJ_REASON_METHOD + "_shortFgs";
            default:
                return "_unknown";
        }
+14 −0
Original line number Diff line number Diff line
@@ -1419,6 +1419,20 @@ final class ServiceRecord extends Binder implements ComponentName.WithComponentN
        return mShortFgsInfo.getTimeoutTime() <= SystemClock.uptimeMillis();
    }

    /**
     * @return true if it's a short FGS's procstate should be demoted.
     */
    public boolean shouldDemoteShortFgsProcState() {
        if (!isAppAlive()) {
            return false;
        }
        if (!this.startRequested || !isShortFgs() || mShortFgsInfo == null
                || !mShortFgsInfo.isCurrent()) {
            return false;
        }
        return mShortFgsInfo.getProcStateDemoteTime() <= SystemClock.uptimeMillis();
    }

    /**
     * @return true if it's a short FGS that's still up and running, and should be declared
     * an ANR.