Merge "Log per-uid breakdown to the AlarmBatchDelivered atom."

This commit is contained in:
Yu-Ting Tseng
2023-01-27 23:23:29 +00:00
committed by Android (Google) Code Review
3 changed files with 71 additions and 10 deletions

View File

@@ -4805,10 +4805,14 @@ public class AlarmManagerService extends SystemService {
}
final ArraySet<UserPackage> triggerPackages = new ArraySet<>();
final IntArray wakeupUids = new IntArray();
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) {
wakeupUids.add(a.uid);
increment(wakeupCountsPerUid, a.uid);
}
if (mConstants.USE_TARE_POLICY == EconomyManager.ENABLED_MODE_ON) {
if (!isExemptFromTare(a)) {
@@ -4835,7 +4839,8 @@ public class AlarmManagerService extends SystemService {
}
rescheduleKernelAlarmsLocked();
updateNextAlarmClockLocked();
MetricsHelper.pushAlarmBatchDelivered(triggerList.size(), wakeUps);
logAlarmBatchDelivered(
triggerList.size(), wakeUps, countsPerUid, wakeupCountsPerUid);
}
}
@@ -4850,6 +4855,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.
*
@@ -5766,12 +5797,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);
}
/**

View File

@@ -117,10 +117,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);
}
}

View File

@@ -208,6 +208,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;
@@ -3412,10 +3413,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