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

Commit edb639d8 authored by Mohamad Mahmoud's avatar Mohamad Mahmoud
Browse files

Add VolatileDropboxEntryStates for timely header state collection

- Add `VolatileDropboxEntryStates` class in `ActivityManagerService` to capture certain header values as early as possible.
- Refactor `addErrorToDropBox` and related methods to accept `VolatileDropboxEntryStates`.
- Use prefetched headers in `ProcessErrorStateRecord` for ANR reporting to capture the 'Frozen' header value early, allowing for more accurate value by getting it before running dumpStackTraces.
- Adjust method calls throughout the system to align with the new method signatures.

Bug: 296063593
Test: atest and manual testing
Change-Id: I7ca5cfdd93f457bb74dfd67361d3f69b310660ef
parent 9bfda99a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -935,7 +935,7 @@ public class Watchdog implements Dumpable {
                        mActivity.addErrorToDropBox(
                                dropboxTag, null, "system_server", null, null, null,
                                null, report.toString(), stack, null, null, null,
                                errorId);
                                errorId, null);
                    }
                }
            };
+41 −8
Original line number Diff line number Diff line
@@ -2112,6 +2112,34 @@ public class ActivityManagerService extends IActivityManager.Stub
        mVoiceInteractionManagerProvider = provider;
    }
    /**
     * Represents volatile states associated with a Dropbox entry.
     * <p>
     * These states, such as the process frozen state, can change quickly over time and thus
     * should be captured as soon as possible to ensure accurate state. If a state is undefined,
     * it means that the state was not read early and a fallback value can be used.
     * </p>
     */
    static class VolatileDropboxEntryStates {
        private final Boolean mIsProcessFrozen;
        private VolatileDropboxEntryStates(Boolean frozenState) {
            this.mIsProcessFrozen = frozenState;
        }
        public static VolatileDropboxEntryStates withProcessFrozenState(boolean frozenState) {
            return new VolatileDropboxEntryStates(frozenState);
        }
        public static VolatileDropboxEntryStates emptyVolatileDropboxEnytyStates() {
            return new VolatileDropboxEntryStates(null);
        }
        public Boolean isProcessFrozen() {
            return mIsProcessFrozen;
        }
    }
    static class MemBinder extends Binder {
        ActivityManagerService mActivityManagerService;
        private final PriorityDump.PriorityDumper mPriorityDumper =
@@ -8888,7 +8916,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        addErrorToDropBox(
                eventType, r, processName, null, null, null, null, null, null, crashInfo,
                new Float(loadingProgress), incrementalMetrics, null);
                new Float(loadingProgress), incrementalMetrics, null, null);
        // For GWP-ASan recoverable crashes, don't make the app crash (the whole point of
        // 'recoverable' is that the app doesn't crash). Normally, for nonrecoreable native crashes,
@@ -8997,7 +9025,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        final StringBuilder sb = new StringBuilder(1024);
        synchronized (sb) {
            appendDropBoxProcessHeaders(process, processName, sb);
            appendDropBoxProcessHeaders(process, processName, null, sb);
            sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
            sb.append("System-App: ").append(isSystemApp).append("\n");
            sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n");
@@ -9100,7 +9128,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                callingPid, (r != null) ? r.getProcessClassEnum() : 0);
        addErrorToDropBox("wtf", r, processName, null, null, null, tag, null, null, crashInfo,
                null, null, null);
                null, null, null, null);
        return r;
    }
@@ -9125,7 +9153,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        for (Pair<String, ApplicationErrorReport.CrashInfo> p = list.poll();
                p != null; p = list.poll()) {
            addErrorToDropBox("wtf", proc, "system_server", null, null, null, p.first, null, null,
                    p.second, null, null, null);
                    p.second, null, null, null, null);
        }
    }
@@ -9148,7 +9176,7 @@ public class ActivityManagerService extends IActivityManager.Stub
     * to append various headers to the dropbox log text.
     */
    void appendDropBoxProcessHeaders(ProcessRecord process, String processName,
            final StringBuilder sb) {
            final VolatileDropboxEntryStates volatileStates, final StringBuilder sb) {
        // Watchdog thread ends up invoking this function (with
        // a null ProcessRecord) to add the stack file to dropbox.
        // Do not acquire a lock on this (am) in such cases, as it
@@ -9167,7 +9195,12 @@ public class ActivityManagerService extends IActivityManager.Stub
            sb.append("PID: ").append(process.getPid()).append("\n");
            sb.append("UID: ").append(process.uid).append("\n");
            if (process.mOptRecord != null) {
                sb.append("Frozen: ").append(process.mOptRecord.isFrozen()).append("\n");
                // Use 'isProcessFrozen' from 'volatileStates' if it'snon-null (present),
                // otherwise use 'isFrozen' from 'mOptRecord'.
                sb.append("Frozen: ").append(
                    (volatileStates != null && volatileStates.isProcessFrozen() != null)
                    ? volatileStates.isProcessFrozen() : process.mOptRecord.isFrozen()
                ).append("\n");
            }
            int flags = process.info.flags;
            final IPackageManager pm = AppGlobals.getPackageManager();
@@ -9275,7 +9308,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            String subject, final String report, final File dataFile,
            final ApplicationErrorReport.CrashInfo crashInfo,
            @Nullable Float loadingProgress, @Nullable IncrementalMetrics incrementalMetrics,
            @Nullable UUID errorId) {
            @Nullable UUID errorId, @Nullable VolatileDropboxEntryStates volatileStates) {
        // NOTE -- this must never acquire the ActivityManagerService lock,
        // otherwise the watchdog may be prevented from resetting the system.
@@ -9297,7 +9330,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        if (rateLimitResult.shouldRateLimit()) return;
        final StringBuilder sb = new StringBuilder(1024);
        appendDropBoxProcessHeaders(process, processName, sb);
        appendDropBoxProcessHeaders(process, processName, volatileStates, sb);
        if (process != null) {
            sb.append("Foreground: ")
                    .append(process.isInterestingToUserLocked() ? "Yes" : "No")
+2 −1
Original line number Diff line number Diff line
@@ -1838,7 +1838,8 @@ public class AppProfiler {
        dropBuilder.append(catSw.toString());
        FrameworkStatsLog.write(FrameworkStatsLog.LOW_MEM_REPORTED);
        mService.addErrorToDropBox("lowmem", null, "system_server", null,
                null, null, tag.toString(), dropBuilder.toString(), null, null, null, null, null);
                null, null, tag.toString(), dropBuilder.toString(), null, null, null, null, null,
                null);
        synchronized (mService) {
            long now = SystemClock.uptimeMillis();
            if (mLastMemUsageReportTime < now) {
+6 −1
Original line number Diff line number Diff line
@@ -297,6 +297,7 @@ class ProcessErrorStateRecord {

        ArrayList<Integer> firstPids = new ArrayList<>(5);
        SparseBooleanArray lastPids = new SparseBooleanArray(20);
        ActivityManagerService.VolatileDropboxEntryStates volatileDropboxEntriyStates = null;

        mApp.getWindowProcessController().appEarlyNotResponding(annotation, () -> {
            latencyTracker.waitingOnAMSLockStarted();
@@ -341,6 +342,9 @@ class ProcessErrorStateRecord {
            synchronized (mProcLock) {
                latencyTracker.waitingOnProcLockEnded();
                setNotResponding(true);
                volatileDropboxEntriyStates =
                        ActivityManagerService.VolatileDropboxEntryStates
                                .withProcessFrozenState(mApp.mOptRecord.isFrozen());
            }

            // Log the ANR to the event log.
@@ -618,7 +622,8 @@ class ProcessErrorStateRecord {
                ? (ProcessRecord) parentProcess.mOwner : null;
        mService.addErrorToDropBox("anr", mApp, mApp.processName, activityShortComponentName,
                parentShortComponentName, parentPr, null, report.toString(), tracesFile,
                null, new Float(loadingProgress), incrementalMetrics, errorId);
                null, new Float(loadingProgress), incrementalMetrics, errorId,
                volatileDropboxEntriyStates);

        if (mApp.getWindowProcessController().appNotResponding(info.toString(),
                () -> {
+1 −1
Original line number Diff line number Diff line
@@ -5622,7 +5622,7 @@ public final class ProcessList {
            if (logToDropbox) {
                final long now = SystemClock.elapsedRealtime();
                final StringBuilder sb = new StringBuilder();
                mService.appendDropBoxProcessHeaders(app, app.processName, sb);
                mService.appendDropBoxProcessHeaders(app, app.processName, null, sb);
                sb.append("Reason: " + reason).append("\n");
                sb.append("Requester UID: " + requester).append("\n");
                dbox.addText(DROPBOX_TAG_IMPERCEPTIBLE_KILL, sb.toString());