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

Commit 5b926997 authored by Jorim Jaggi's avatar Jorim Jaggi
Browse files

Trace currently resumed app

Since we now have the ability to multi-hour systraces from
devices used daily, it's useful to do additional segmentation
and understanding what part of an app's runtime is due to it
being in foreground vs background.

Test: go/wm-smoke
Test: Take trace, check whether currently resumed app is being
traced.

Bug: 69862220
Change-Id: Idaf9af4c366dfedb29d07b75f7eaf17d9a2165a0
parent ec243338
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -110,6 +110,7 @@ import static android.os.Process.setThreadPriority;
import static android.os.Process.setThreadScheduler;
import static android.os.Process.startWebView;
import static android.os.Process.zygoteProcess;
import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
import static android.provider.Settings.Global.ALWAYS_FINISH_ACTIVITIES;
import static android.provider.Settings.Global.DEBUG_APP;
import static android.provider.Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT;
@@ -736,6 +737,13 @@ public class ActivityManagerService extends IActivityManager.Stub
     */
    private ActivityRecord mLastResumedActivity;
    /**
     * The activity that is currently being traced as the active resumed activity.
     *
     * @see #updateResumedAppTrace
     */
    private @Nullable ActivityRecord mTracedResumedActivity;
    /**
     * If non-null, we are tracking the time the user spends in the currently focused app.
     */
@@ -3423,6 +3431,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        if (mLastResumedActivity != null && r.userId != mLastResumedActivity.userId) {
            mUserController.sendForegroundProfileChanged(r.userId);
        }
        updateResumedAppTrace(r);
        mLastResumedActivity = r;
        mWindowManager.setFocusedApp(r.appToken, true);
@@ -3436,6 +3445,22 @@ public class ActivityManagerService extends IActivityManager.Stub
                reason);
    }
    private void updateResumedAppTrace(@Nullable ActivityRecord resumed) {
        if (mTracedResumedActivity != null) {
            Trace.asyncTraceEnd(TRACE_TAG_ACTIVITY_MANAGER,
                    constructResumedTraceName(mTracedResumedActivity.packageName), 0);
        }
        if (resumed != null) {
            Trace.asyncTraceBegin(TRACE_TAG_ACTIVITY_MANAGER,
                    constructResumedTraceName(resumed.packageName), 0);
        }
        mTracedResumedActivity = resumed;
    }
    private String constructResumedTraceName(String packageName) {
        return "focused app: " + packageName;
    }
    @Override
    public void setFocusedStack(int stackId) {
        enforceCallingPermission(MANAGE_ACTIVITY_STACKS, "setFocusedStack()");
@@ -13073,6 +13098,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            }
            mTopProcessState = ActivityManager.PROCESS_STATE_TOP_SLEEPING;
            mStackSupervisor.goingToSleepLocked();
            updateResumedAppTrace(null /* resumed */);
            updateOomAdjLocked();
        }
    }