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

Commit 1d87a9fe authored by Achim Thesmann's avatar Achim Thesmann
Browse files

Log length of grace period used to start Activity

If the Activity start is only allowed by grace period (neither creator
nor sender allow with another BAL code) log the length of the grace
period used. With the collected data (from droidfood) we can determine
what a reasonable cutoff would be.
Also adds a flag for reducing the grace period as soon as we have collected sufficient data.

Test: atest BackgroundActivityLaunchTest
Flag: EXEMPT log only update
Bug: 362575865
Change-Id: I2b18caf87b63c32c763060b72062c8ba44637063
parent 59526079
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -71,3 +71,11 @@ flag {
    bug: "339720406"
}

flag {
    name: "bal_reduce_grace_period"
    namespace: "responsible_apis"
    description: "Changes to reduce or ideally remove the grace period exemption."
    bug: "362575865"
}

+15 −0
Original line number Diff line number Diff line
@@ -1685,6 +1685,21 @@ public class BackgroundActivityStartController {
                            (state.mOriginatingPendingIntent != null));
        }

        if (finalVerdict.getRawCode() == BAL_ALLOW_GRACE_PERIOD) {
            if (state.realCallerExplicitOptInOrAutoOptIn()
                    && state.mResultForRealCaller.allows()
                    && state.mResultForRealCaller.getRawCode() != BAL_ALLOW_GRACE_PERIOD) {
                // real caller could allow with a different exemption
            } else if (state.callerExplicitOptInOrAutoOptIn() && state.mResultForCaller.allows()
                    && state.mResultForCaller.getRawCode() != BAL_ALLOW_GRACE_PERIOD) {
                // caller could allow with a different exemption
            } else {
                // log to determine grace period length distribution
                Slog.wtf(TAG, "Activity start ONLY allowed by BAL_ALLOW_GRACE_PERIOD "
                        + finalVerdict.mMessage + ": " + state);
            }
        }

        if (balImprovedMetrics()) {
            if (shouldLogStats(finalVerdict, state)) {
                String activityName;
+8 −16
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.server.wm;

import static com.android.internal.util.Preconditions.checkArgument;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_ACTIVITY_STARTS;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
import static com.android.server.wm.ActivityTaskManagerService.ACTIVITY_BG_START_GRACE_PERIOD_MS;
@@ -48,7 +47,6 @@ import android.os.SystemClock;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.IntArray;
import android.util.Slog;

import com.android.internal.annotations.GuardedBy;
import com.android.server.wm.BackgroundActivityStartController.BalVerdict;
@@ -138,22 +136,16 @@ class BackgroundLaunchProcessController {
        if (appSwitchState == APP_SWITCH_ALLOW) {
            // Allow if any activity in the caller has either started or finished very recently, and
            // it must be started or finished after last stop app switches time.
            final long now = SystemClock.uptimeMillis();
            if (now - lastActivityLaunchTime < ACTIVITY_BG_START_GRACE_PERIOD_MS
                    || now - lastActivityFinishTime < ACTIVITY_BG_START_GRACE_PERIOD_MS) {
                // If activity is started and finished before stop app switch time, we should not
                // let app to be able to start background activity even it's in grace period.
            if (lastActivityLaunchTime > lastStopAppSwitchesTime
                    || lastActivityFinishTime > lastStopAppSwitchesTime) {
                final long now = SystemClock.uptimeMillis();
                long timeSinceLastStartOrFinish = now - Math.max(lastActivityLaunchTime,
                        lastActivityFinishTime);
                if (timeSinceLastStartOrFinish < ACTIVITY_BG_START_GRACE_PERIOD_MS) {
                    return new BalVerdict(BAL_ALLOW_GRACE_PERIOD, /*background*/ true,
                            "within " + ACTIVITY_BG_START_GRACE_PERIOD_MS + "ms grace period");
                            "within " + ACTIVITY_BG_START_GRACE_PERIOD_MS + "ms grace period ("
                                    + timeSinceLastStartOrFinish + "ms)");
                }
                if (DEBUG_ACTIVITY_STARTS) {
                    Slog.d(TAG, "[Process(" + pid + ")] Activity start within "
                            + ACTIVITY_BG_START_GRACE_PERIOD_MS
                            + "ms grace period but also within stop app switch window");
                }

            }
        }
        return BalVerdict.BLOCK;