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

Commit 61011ab3 authored by Suprabh Shukla's avatar Suprabh Shukla
Browse files

Add more data to removal history in alarm dump

Expanding the history to include more information about the alarm which
was removed. Specifically, the calculations for when the alarm is due is
extremely useful in debugging issues by looking at the dump.

Creating a mechanism to snapshot an alarm object for logging. More
fields can be added to this as needed.

Reversing the order of printing the removal history so it prints the
most recent removal first.

Test: Manually inspect the output of `dumpsys alarm`
Test: atest FrameworksMockingServicesTests:AlarmTest

Bug: 193063877
Change-Id: Id1baaa5c985a201ccd3d5f325c0781edec783fba
parent 14c4746a
Loading
Loading
Loading
Loading
+30 −1
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.util.proto.ProtoOutputStream;
import com.android.internal.annotations.VisibleForTesting;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
@@ -264,7 +265,7 @@ class Alarm {
        return sb.toString();
    }

    private static String policyIndexToString(int index) {
    static String policyIndexToString(int index) {
        switch (index) {
            case REQUESTER_POLICY_INDEX:
                return "requester";
@@ -400,4 +401,32 @@ class Alarm {

        proto.end(token);
    }

    /**
     * Stores a snapshot of an alarm at any given time to be used for logging and diagnostics.
     * This should intentionally avoid holding pointers to objects like {@link Alarm#operation}.
     */
    static class Snapshot {
        final int mType;
        final String mTag;
        final long[] mPolicyWhenElapsed;

        Snapshot(Alarm a) {
            mType = a.type;
            mTag = a.statsTag;
            mPolicyWhenElapsed = Arrays.copyOf(a.mPolicyWhenElapsed, NUM_POLICIES);
        }

        void dump(IndentingPrintWriter pw, long nowElapsed) {
            pw.print("type", typeToString(mType));
            pw.print("tag", mTag);
            pw.println();
            pw.print("policyWhenElapsed:");
            for (int i = 0; i < NUM_POLICIES; i++) {
                pw.print(" " + policyIndexToString(i) + "=");
                TimeUtils.formatDuration(mPolicyWhenElapsed[i], nowElapsed, pw);
            }
            pw.println();
        }
    }
}
+17 −8
Original line number Diff line number Diff line
@@ -618,13 +618,13 @@ public class AlarmManagerService extends SystemService {
        static final int REMOVE_REASON_LISTENER_BINDER_DIED = 5;
        static final int REMOVE_REASON_LISTENER_CACHED = 6;

        final String mTag;
        final Alarm.Snapshot mAlarmSnapshot;
        final long mWhenRemovedElapsed;
        final long mWhenRemovedRtc;
        final int mRemoveReason;

        RemovedAlarm(Alarm a, int removeReason, long nowRtc, long nowElapsed) {
            mTag = a.statsTag;
            mAlarmSnapshot = new Alarm.Snapshot(a);
            mRemoveReason = removeReason;
            mWhenRemovedRtc = nowRtc;
            mWhenRemovedElapsed = nowElapsed;
@@ -656,13 +656,21 @@ public class AlarmManagerService extends SystemService {
        }

        void dump(IndentingPrintWriter pw, long nowElapsed, SimpleDateFormat sdf) {
            pw.print("[tag", mTag);
            pw.print("reason", removeReasonToString(mRemoveReason));
            pw.increaseIndent();

            pw.print("Reason", removeReasonToString(mRemoveReason));
            pw.print("elapsed=");
            TimeUtils.formatDuration(mWhenRemovedElapsed, nowElapsed, pw);
            pw.print(" rtc=");
            pw.print(sdf.format(new Date(mWhenRemovedRtc)));
            pw.println("]");
            pw.println();

            pw.println("Snapshot:");
            pw.increaseIndent();
            mAlarmSnapshot.dump(pw, nowElapsed);
            pw.decreaseIndent();

            pw.decreaseIndent();
        }
    }

@@ -3510,8 +3518,9 @@ public class AlarmManagerService extends SystemService {
                    pw.println(":");
                    pw.increaseIndent();
                    final RemovedAlarm[] historyForUid = mRemovalHistory.valueAt(i).toArray();
                    for (final RemovedAlarm removedAlarm : historyForUid) {
                        removedAlarm.dump(pw, nowELAPSED, sdf);
                    for (int index = historyForUid.length - 1; index >= 0; index--) {
                        pw.print("#" + (historyForUid.length - index) + ": ");
                        historyForUid[index].dump(pw, nowELAPSED, sdf);
                    }
                    pw.decreaseIndent();
                }
+22 −0
Original line number Diff line number Diff line
@@ -210,4 +210,26 @@ public class AlarmTest {
                createDefaultAlarm(anything, anything, FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED)));
        assertTrue("Alarm clock not exempt", isExemptFromTare(createAlarmClock(anything)));
    }

    @Test
    public void snapshotImmutable() {
        final Alarm a = createDefaultAlarm(0, 0, 0);

        final Random random = new Random(234);
        final long[] policyElapsed = new long[NUM_POLICIES];
        for (int i = 0; i < NUM_POLICIES; i++) {
            a.setPolicyElapsed(i, policyElapsed[i] = random.nextInt(1 << 10));
        }

        final Alarm.Snapshot snapshot = new Alarm.Snapshot(a);

        for (int i = 0; i < NUM_POLICIES; i++) {
            assertEquals(policyElapsed[i], snapshot.mPolicyWhenElapsed[i]);
        }

        for (int i = 0; i < NUM_POLICIES; i++) {
            a.setPolicyElapsed(i, policyElapsed[i] + 5 + i);
            assertEquals(policyElapsed[i], snapshot.mPolicyWhenElapsed[i]);
        }
    }
}