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

Commit 71678ddc authored by Brad Fitzpatrick's avatar Brad Fitzpatrick
Browse files

StrictMode: optimize common case (just dropboxing)

The way StrictMode is used during development, just dropboxing
violations, could be a little more optimal, taking the
ActivityManagerService call off the main thread.  But we can only do
this safely in the case where that's the only penalty.

Data suggests this call, despite being async, still takes around 30
milliseconds.  This isn't a major win, and arguably it might be a
_better_ idea to slow down people's event loops more and further jank
up their animations on violations, but I thought any less overhead
from StrictMode, the better.

Change-Id: Iad9cce1cb4a084fa64abc4b5e1b4f3bff6a08c94
parent 5164246d
Loading
Loading
Loading
Loading
+35 −1
Original line number Diff line number Diff line
@@ -172,6 +172,12 @@ public final class StrictMode {
     */
    public static final int PENALTY_GATHER = 0x100;

    /**
     * Mask of all the penalty bits.
     */
    private static final int PENALTY_MASK =
            PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER;

    /**
     * The current VmPolicy in effect.
     */
@@ -882,7 +888,7 @@ public final class StrictMode {
                }
            }

            // The violationMask, passed to ActivityManager, is a
            // The violationMaskSubset, passed to ActivityManager, is a
            // subset of the original StrictMode policy bitmask, with
            // only the bit violated and penalty bits to be executed
            // by the ActivityManagerService remaining set.
@@ -900,7 +906,35 @@ public final class StrictMode {
            if (violationMaskSubset != 0) {
                int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
                violationMaskSubset |= violationBit;
                final int violationMaskSubsetFinal = violationMaskSubset;
                final int savedPolicyMask = getThreadPolicyMask();

                final boolean justDropBox = (info.policy & PENALTY_MASK) == PENALTY_DROPBOX;
                if (justDropBox) {
                    // If all we're going to ask the activity manager
                    // to do is dropbox it (the common case during
                    // platform development), we can avoid doing this
                    // call synchronously which Binder data suggests
                    // isn't always super fast, despite the implementation
                    // in the ActivityManager trying to be mostly async.
                    new Thread("callActivityManagerForStrictModeDropbox") {
                        public void run() {
                            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                            try {
                                ActivityManagerNative.getDefault().
                                        handleApplicationStrictModeViolation(
                                            RuntimeInit.getApplicationObject(),
                                            violationMaskSubsetFinal,
                                            info);
                            } catch (RemoteException e) {
                                Log.e(TAG, "RemoteException handling StrictMode violation", e);
                            }
                        }
                    }.start();
                    return;
                }

                // Normal synchronous call to the ActivityManager.
                try {
                    // First, remove any policy before we call into the Activity Manager,
                    // otherwise we'll infinite recurse as we try to log policy violations