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

Commit b5cf2f91 authored by Riddle Hsu's avatar Riddle Hsu
Browse files

Streamline dump state for input ANR

This reduce ~90% execution time and memory usage of the dump.
The case when there are only the home and ANR app:
 80ms -> 5ms
 111k chars -> 11k

- Only dump related task/activity/window
- Removed the looper dump in DisplayPolicy because PhoneWindowManager
  already has the same info.

Bug: 322386106
Bug: 322947521
Test: Trigger ANR by a an activity that sleeps 6s on UI thread.
      Check the duration of trace dumpAnrStateLocked.
      Check the output from
       adb shell dumpsys activity lastanr
       adb shell dumpsys window lastanr

Change-Id: I5db34283cf5ef02bce10827f2a1be7523b5852b9
parent ce8d4a38
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -639,6 +639,10 @@ public class ActivityStartController {
        return mPendingRemoteAnimationRegistry;
    }

    ActivityRecord getLastStartActivity() {
        return mLastStarter != null ? mLastStarter.mStartActivity : null;
    }

    void dumpLastHomeActivityStartResult(PrintWriter pw, String prefix) {
        pw.print(prefix);
        pw.print("mLastHomeActivityStartResult=");
+18 −7
Original line number Diff line number Diff line
@@ -5554,7 +5554,7 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
     * Saves the current activity manager state and includes the saved state in the next dump of
     * activity manager.
     */
    void saveANRState(String reason) {
    void saveANRState(ActivityRecord activity, String reason) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new FastPrintWriter(sw, false, 1024);
        pw.println("  ANR time: " + DateFormat.getDateTimeInstance().format(new Date()));
@@ -5562,14 +5562,25 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
            pw.println("  Reason: " + reason);
        }
        pw.println();
        getActivityStartController().dump(pw, "  ", null);
        if (activity != null) {
            final Task rootTask = activity.getRootTask();
            if (rootTask != null) {
                rootTask.forAllTaskFragments(
                        tf -> tf.dumpInner("  ", pw, true /* dumpAll */, null /* dumpPackage */));
                pw.println();
            }
            mActivityStartController.dump(pw, "  ", activity.packageName);
            if (mActivityStartController.getLastStartActivity() != activity) {
                activity.dump(pw, "  ", true /* dumpAll */);
            }
        }
        ActivityTaskSupervisor.printThisActivity(pw, mRootWindowContainer.getTopResumedActivity(),
                null /* dumpPackage */, INVALID_DISPLAY, true /* needSep */,
                "  ResumedActivity: ", /* header= */ null /* header */);
        mLockTaskController.dump(pw, "  ");
        mKeyguardController.dump(pw, "  ");
        pw.println("-------------------------------------------------------------------"
                + "------------");
        dumpActivitiesLocked(null /* fd */, pw, null /* args */, 0 /* opti */,
                true /* dumpAll */, false /* dumpClient */, null /* dumpPackage */,
                INVALID_DISPLAY, "" /* header */);
        pw.println();
        pw.close();

        mLastANRState = sw.toString();
+1 −1
Original line number Diff line number Diff line
@@ -367,7 +367,7 @@ class AnrController {
                Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "dumpAnrStateLocked()");
                synchronized (mService.mGlobalLock) {
                    mService.saveANRStateLocked(activity, windowState, reason);
                    mService.mAtmService.saveANRState(reason);
                    mService.mAtmService.saveANRState(activity, reason);
                }
            } finally {
                Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
+0 −4
Original line number Diff line number Diff line
@@ -104,7 +104,6 @@ import android.os.SystemProperties;
import android.os.Trace;
import android.os.UserHandle;
import android.util.ArraySet;
import android.util.PrintWriterPrinter;
import android.util.Slog;
import android.util.SparseArray;
import android.view.DisplayInfo;
@@ -2891,9 +2890,6 @@ public class DisplayPolicy {
        if (!CLIENT_TRANSIENT) {
            mSystemGestures.dump(pw, prefix);
        }

        pw.print(prefix); pw.println("Looper state:");
        mHandler.getLooper().dump(new PrintWriterPrinter(pw), prefix + "  ");
    }

    private boolean supportsPointerLocation() {
+23 −9
Original line number Diff line number Diff line
@@ -6750,11 +6750,6 @@ public class WindowManagerService extends IWindowManager.Stub
    private void dumpWindowsLocked(PrintWriter pw, boolean dumpAll,
            ArrayList<WindowState> windows) {
        pw.println("WINDOW MANAGER WINDOWS (dumpsys window windows)");
        dumpWindowsNoHeaderLocked(pw, dumpAll, windows);
    }

    private void dumpWindowsNoHeaderLocked(PrintWriter pw, boolean dumpAll,
            ArrayList<WindowState> windows) {
        mRoot.dumpWindowsNoHeader(pw, dumpAll, windows);

        if (!mHidingNonSystemOverlayWindows.isEmpty()) {
@@ -6989,9 +6984,15 @@ public class WindowManagerService extends IWindowManager.Stub
        if (reason != null) {
            pw.println("  Reason: " + reason);
        }
        pw.println();
        final ArrayList<WindowState> relatedWindows = new ArrayList<>();
        for (int i = mRoot.getChildCount() - 1; i >= 0; i--) {
            final DisplayContent dc = mRoot.getChildAt(i);
            final int displayId = dc.getDisplayId();
            final WindowState currentFocus = dc.mCurrentFocus;
            final ActivityRecord focusedApp = dc.mFocusedApp;
            pw.println("  Display #" + displayId + " currentFocus=" + currentFocus
                    + " focusedApp=" + focusedApp);
            if (!dc.mWinAddedSinceNullFocus.isEmpty()) {
                pw.println("  Windows added in display #" + displayId + " since null focus: "
                        + dc.mWinAddedSinceNullFocus);
@@ -7000,12 +7001,25 @@ public class WindowManagerService extends IWindowManager.Stub
                pw.println("  Windows removed in display #" + displayId + " since null focus: "
                        + dc.mWinRemovedSinceNullFocus);
            }
        }
            pw.println("  Tasks in top down Z order:");
            dc.forAllTaskDisplayAreas(tda -> {
                tda.dump(pw, "    ", false /* dumpAll */);
            });
            dc.getInputMonitor().dump(pw, "  ");
            pw.println();
        dumpWindowsNoHeaderLocked(pw, true, null);
            dc.forAllWindows(w -> {
                if ((currentFocus != null && Objects.equals(w.mAttrs.packageName,
                        currentFocus.mAttrs.packageName)) || (focusedApp != null
                        && Objects.equals(w.mAttrs.packageName, focusedApp.packageName))) {
                    relatedWindows.add(w);
                }
            }, true /* traverseTopToBottom */);
        }
        if (windowState != null && !relatedWindows.contains(windowState)) {
            relatedWindows.add(windowState);
        }
        mRoot.dumpWindowsNoHeader(pw, true /* dumpAll */, relatedWindows);
        pw.println();
        pw.println("Last ANR continued");
        mRoot.dumpDisplayContents(pw);
        pw.close();
        mLastANRState = sw.toString();