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

Commit 40d3f397 authored by Lee Shombert's avatar Lee Shombert
Browse files

Small modifications to AnrTimer

This change has three small modifications to AnrTimer:

 1. In AnrTimer.start(), instead of testing if the timer exists before
    canceling it, just cancel it.  The cancel() method does the same
    test internally.  Starting a timer that is already running is a
    restart: keep a count.

 2. Remove an obsolete synchronization block around mTimerArgMap.  The
    code was left over from an older revision in which mTimerArgMap
    was static.

 3. Do not restart the ticker when a timer is canceled.  Starting the
    ticker is expensive and is inline with the cancel operation.  In
    normal operation the timer will not have to be restarted very
    often at all.

Test: atest
 * FrameworksServicesTests:AnrTimerTest

Flag: com.android.server.utils.anr_timer_service
Bug: 325594551
Change-Id: I929e51fec5fd433f4a636f372c39b3e9e2bc9b8f
parent e6e2ada8
Loading
Loading
Loading
Loading
+11 −9
Original line number Diff line number Diff line
@@ -193,6 +193,10 @@ public class AnrTimer<V> implements AutoCloseable {
    @GuardedBy("mLock")
    private int mTotalStarted = 0;

    /** The total number of timers that were restarted without an explicit cancel. */
    @GuardedBy("mLock")
    private int mTotalRestarted = 0;

    /** The total number of errors detected. */
    @GuardedBy("mLock")
    private int mTotalErrors = 0;
@@ -434,10 +438,10 @@ public class AnrTimer<V> implements AutoCloseable {
        @Override
        void start(@NonNull V arg, int pid, int uid, long timeoutMs) {
            synchronized (mLock) {
                if (mTimerIdMap.containsKey(arg)) {
                    // There is an existing timer.  Cancel it.
                    cancel(arg);
                }
                // If there is an existing timer, cancel it.  This is a nop if the timer does not
                // exist.
                if (cancel(arg)) mTotalRestarted++;

                int timerId = nativeAnrTimerStart(mNative, pid, uid, timeoutMs, mExtend);
                if (timerId > 0) {
                    mTimerIdMap.put(arg, timerId);
@@ -546,10 +550,8 @@ public class AnrTimer<V> implements AutoCloseable {
        private Integer removeLocked(V arg) {
            Integer r = mTimerIdMap.remove(arg);
            if (r != null) {
                synchronized (mTimerArgMap) {
                mTimerArgMap.remove(r);
            }
            }
            return r;
        }
    }
@@ -672,8 +674,8 @@ public class AnrTimer<V> implements AutoCloseable {
        synchronized (mLock) {
            pw.format("timer: %s\n", mLabel);
            pw.increaseIndent();
            pw.format("started=%d maxStarted=%d running=%d expired=%d errors=%d\n",
                    mTotalStarted, mMaxStarted, mTimerIdMap.size(),
            pw.format("started=%d maxStarted=%d  restarted=%d running=%d expired=%d errors=%d\n",
                    mTotalStarted, mMaxStarted, mTotalRestarted, mTimerIdMap.size(),
                    mTotalExpired, mTotalErrors);
            pw.decreaseIndent();
            mFeature.dump(pw, false);
+0 −2
Original line number Diff line number Diff line
@@ -487,7 +487,6 @@ class AnrTimerService::Ticker {
        timer_id_t front = headTimerId();
        auto found = running_.find(key);
        if (found != running_.end()) running_.erase(found);
        if (front != headTimerId()) restartLocked();
    }

    // Remove every timer associated with the service.
@@ -501,7 +500,6 @@ class AnrTimerService::Ticker {
                i++;
            }
        }
        if (front != headTimerId()) restartLocked();
    }

    // Return the number of timers still running.