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

Commit f6db885c authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Add more data to removal history in alarm dump" into udc-dev am: c0138451

parents 0334f1a5 c0138451
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();
        }
    }

@@ -3512,8 +3520,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]);
        }
    }
}