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

Commit 5507d9ff authored by Yu-Ting Tseng's avatar Yu-Ting Tseng
Browse files

Cherrypicking the following change to TM QPR.

Log per-uid breakdown to the AlarmBatchDelivered atom.

Bug: 261910877
Test: atest AlarmManagerServiceTest
Change-Id: I953fe4401d9c8d16398f9d16db0500aed9bf488b
Merged-In: I953fe4401d9c8d16398f9d16db0500aed9bf488b
parent a8825e86
Loading
Loading
Loading
Loading
+35 −7
Original line number Diff line number Diff line
@@ -4739,8 +4739,14 @@ public class AlarmManagerService extends SystemService {
                            }
                            final ArraySet<Pair<String, Integer>> triggerPackages =
                                    new ArraySet<>();
                            final SparseIntArray countsPerUid = new SparseIntArray();
                            final SparseIntArray wakeupCountsPerUid = new SparseIntArray();
                            for (int i = 0; i < triggerList.size(); i++) {
                                final Alarm a = triggerList.get(i);
                                increment(countsPerUid, a.uid);
                                if (a.wakeup) {
                                    increment(wakeupCountsPerUid, a.uid);
                                }
                                if (mConstants.USE_TARE_POLICY) {
                                    if (!isExemptFromTare(a)) {
                                        triggerPackages.add(Pair.create(
@@ -4761,7 +4767,8 @@ public class AlarmManagerService extends SystemService {
                            }
                            rescheduleKernelAlarmsLocked();
                            updateNextAlarmClockLocked();
                            MetricsHelper.pushAlarmBatchDelivered(triggerList.size(), wakeUps);
                            logAlarmBatchDelivered(
                                    triggerList.size(), wakeUps, countsPerUid, wakeupCountsPerUid);
                        }
                    }

@@ -4776,6 +4783,32 @@ public class AlarmManagerService extends SystemService {
        }
    }

    private static void increment(SparseIntArray array, int key) {
        final int index = array.indexOfKey(key);
        if (index >= 0) {
            array.setValueAt(index, array.valueAt(index) + 1);
        } else {
            array.put(key, 1);
        }
    }

    private void logAlarmBatchDelivered(
            int alarms,
            int wakeups,
            SparseIntArray countsPerUid,
            SparseIntArray wakeupCountsPerUid) {
        final int[] uids = new int[countsPerUid.size()];
        final int[] countsArray = new int[countsPerUid.size()];
        final int[] wakeupCountsArray = new int[countsPerUid.size()];
        for (int i = 0; i < countsPerUid.size(); i++) {
            uids[i] = countsPerUid.keyAt(i);
            countsArray[i] = countsPerUid.valueAt(i);
            wakeupCountsArray[i] = wakeupCountsPerUid.get(uids[i], 0);
        }
        MetricsHelper.pushAlarmBatchDelivered(
                alarms, wakeups, uids, countsArray, wakeupCountsArray);
    }

    /**
     * Attribute blame for a WakeLock.
     *
@@ -5695,12 +5728,7 @@ public class AlarmManagerService extends SystemService {
    }

    private void incrementAlarmCount(int uid) {
        final int uidIndex = mAlarmsPerUid.indexOfKey(uid);
        if (uidIndex >= 0) {
            mAlarmsPerUid.setValueAt(uidIndex, mAlarmsPerUid.valueAt(uidIndex) + 1);
        } else {
            mAlarmsPerUid.put(uid, 1);
        }
        increment(mAlarmsPerUid, uid);
    }

    /**
+6 −2
Original line number Diff line number Diff line
@@ -111,10 +111,14 @@ class MetricsHelper {
                ActivityManager.processStateAmToProto(callerProcState));
    }

    static void pushAlarmBatchDelivered(int numAlarms, int wakeups) {
    static void pushAlarmBatchDelivered(
            int numAlarms, int wakeups, int[] uids, int[] alarmsPerUid, int[] wakeupAlarmsPerUid) {
        FrameworkStatsLog.write(
                FrameworkStatsLog.ALARM_BATCH_DELIVERED,
                numAlarms,
                wakeups);
                wakeups,
                uids,
                alarmsPerUid,
                wakeupAlarmsPerUid);
    }
}
+32 −1
Original line number Diff line number Diff line
@@ -205,6 +205,7 @@ public class AlarmManagerServiceTest {
    private static final String TAG = AlarmManagerServiceTest.class.getSimpleName();
    private static final int SYSTEM_UI_UID = 12345;
    private static final int TEST_CALLING_USER = UserHandle.getUserId(TEST_CALLING_UID);
    private static final int TEST_CALLING_UID_2 = TEST_CALLING_UID + 1;

    private long mAppStandbyWindow;
    private long mAllowWhileIdleWindow;
@@ -3375,10 +3376,40 @@ public class AlarmManagerServiceTest {
            final int type = ((i & 1) == 0) ? ELAPSED_REALTIME : ELAPSED_REALTIME_WAKEUP;
            setTestAlarm(type, mNowElapsedTest + i, getNewMockPendingIntent());
        }
        for (int i = 0; i < 4; i++) {
            final int type = ((i & 1) == 0) ? ELAPSED_REALTIME : ELAPSED_REALTIME_WAKEUP;
            setTestAlarm(
                    type,
                    mNowElapsedTest + i,
                    getNewMockPendingIntent(),
                    0,
                    FLAG_STANDALONE,
                    TEST_CALLING_UID_2);
        }
        mNowElapsedTest += 100;
        mTestTimer.expire();

        verify(() -> MetricsHelper.pushAlarmBatchDelivered(10, 5));
        final ArgumentCaptor<int[]> uidsCaptor = ArgumentCaptor.forClass(int[].class);
        final ArgumentCaptor<int[]> alarmsPerUidCaptor = ArgumentCaptor.forClass(int[].class);
        final ArgumentCaptor<int[]> wakeupAlarmsPerUidCaptor = ArgumentCaptor.forClass(int[].class);

        verify(() -> MetricsHelper.pushAlarmBatchDelivered(
                eq(14),
                eq(7),
                uidsCaptor.capture(),
                alarmsPerUidCaptor.capture(),
                wakeupAlarmsPerUidCaptor.capture()));
        assertEquals(2, uidsCaptor.getValue().length);
        assertEquals(2, alarmsPerUidCaptor.getValue().length);
        assertEquals(2, wakeupAlarmsPerUidCaptor.getValue().length);
        final int uid1Idx = uidsCaptor.getValue()[0] == TEST_CALLING_UID ? 0 : 1;
        final int uid2Idx = 1 - uid1Idx;
        assertEquals(TEST_CALLING_UID, uidsCaptor.getValue()[uid1Idx]);
        assertEquals(TEST_CALLING_UID_2, uidsCaptor.getValue()[uid2Idx]);
        assertEquals(10, alarmsPerUidCaptor.getValue()[uid1Idx]);
        assertEquals(5, wakeupAlarmsPerUidCaptor.getValue()[uid1Idx]);
        assertEquals(4, alarmsPerUidCaptor.getValue()[uid2Idx]);
        assertEquals(2, wakeupAlarmsPerUidCaptor.getValue()[uid2Idx]);
    }

    @Test