Merge "Cherrypicking the following change to TM QPR." into tm-qpr-dev

This commit is contained in:
Yu-Ting Tseng
2023-02-08 22:33:57 +00:00
committed by Android (Google) Code Review
3 changed files with 73 additions and 10 deletions

View File

@@ -4739,8 +4739,14 @@ public class AlarmManagerService extends SystemService {
} }
final ArraySet<Pair<String, Integer>> triggerPackages = final ArraySet<Pair<String, Integer>> triggerPackages =
new ArraySet<>(); new ArraySet<>();
final SparseIntArray countsPerUid = new SparseIntArray();
final SparseIntArray wakeupCountsPerUid = new SparseIntArray();
for (int i = 0; i < triggerList.size(); i++) { for (int i = 0; i < triggerList.size(); i++) {
final Alarm a = triggerList.get(i); final Alarm a = triggerList.get(i);
increment(countsPerUid, a.uid);
if (a.wakeup) {
increment(wakeupCountsPerUid, a.uid);
}
if (mConstants.USE_TARE_POLICY) { if (mConstants.USE_TARE_POLICY) {
if (!isExemptFromTare(a)) { if (!isExemptFromTare(a)) {
triggerPackages.add(Pair.create( triggerPackages.add(Pair.create(
@@ -4761,7 +4767,8 @@ public class AlarmManagerService extends SystemService {
} }
rescheduleKernelAlarmsLocked(); rescheduleKernelAlarmsLocked();
updateNextAlarmClockLocked(); 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. * Attribute blame for a WakeLock.
* *
@@ -5695,12 +5728,7 @@ public class AlarmManagerService extends SystemService {
} }
private void incrementAlarmCount(int uid) { private void incrementAlarmCount(int uid) {
final int uidIndex = mAlarmsPerUid.indexOfKey(uid); increment(mAlarmsPerUid, uid);
if (uidIndex >= 0) {
mAlarmsPerUid.setValueAt(uidIndex, mAlarmsPerUid.valueAt(uidIndex) + 1);
} else {
mAlarmsPerUid.put(uid, 1);
}
} }
/** /**

View File

@@ -111,10 +111,14 @@ class MetricsHelper {
ActivityManager.processStateAmToProto(callerProcState)); 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.write(
FrameworkStatsLog.ALARM_BATCH_DELIVERED, FrameworkStatsLog.ALARM_BATCH_DELIVERED,
numAlarms, numAlarms,
wakeups); wakeups,
uids,
alarmsPerUid,
wakeupAlarmsPerUid);
} }
} }

View File

@@ -205,6 +205,7 @@ public class AlarmManagerServiceTest {
private static final String TAG = AlarmManagerServiceTest.class.getSimpleName(); private static final String TAG = AlarmManagerServiceTest.class.getSimpleName();
private static final int SYSTEM_UI_UID = 12345; 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_USER = UserHandle.getUserId(TEST_CALLING_UID);
private static final int TEST_CALLING_UID_2 = TEST_CALLING_UID + 1;
private long mAppStandbyWindow; private long mAppStandbyWindow;
private long mAllowWhileIdleWindow; private long mAllowWhileIdleWindow;
@@ -3375,10 +3376,40 @@ public class AlarmManagerServiceTest {
final int type = ((i & 1) == 0) ? ELAPSED_REALTIME : ELAPSED_REALTIME_WAKEUP; final int type = ((i & 1) == 0) ? ELAPSED_REALTIME : ELAPSED_REALTIME_WAKEUP;
setTestAlarm(type, mNowElapsedTest + i, getNewMockPendingIntent()); 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; mNowElapsedTest += 100;
mTestTimer.expire(); 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 @Test