Fill the other values for DailyKeepaliveInfoReported am: 886b131df8
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/23723903
Change-Id: I041b1e0296c4415568fe9294640b82d74e719d9a
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java b/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java
index 3f0d14c..34e11c5 100644
--- a/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java
+++ b/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java
@@ -455,7 +455,9 @@
autoKi.getNetwork(),
autoKi.mKi.getSlot(),
autoKi.mKi.getNai().networkCapabilities,
- autoKi.mKi.getKeepaliveIntervalSec());
+ autoKi.mKi.getKeepaliveIntervalSec(),
+ autoKi.mKi.getUid(),
+ STATE_ALWAYS_ON != autoKi.mAutomaticOnOffState);
// Add automatic on/off request into list to track its life cycle.
try {
diff --git a/service/src/com/android/server/connectivity/KeepaliveStatsTracker.java b/service/src/com/android/server/connectivity/KeepaliveStatsTracker.java
index 9f08673..b91f158 100644
--- a/service/src/com/android/server/connectivity/KeepaliveStatsTracker.java
+++ b/service/src/com/android/server/connectivity/KeepaliveStatsTracker.java
@@ -45,11 +45,12 @@
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import java.util.Set;
-// TODO(b/273451360): Also track DailykeepaliveInfoReported
/**
* Tracks carrier and duration metrics of automatic on/off keepalives.
*
@@ -81,6 +82,10 @@
public final int transportTypes;
// The keepalive interval in millis.
public final int intervalMs;
+ // The uid of the app that requested the keepalive.
+ public final int appUid;
+ // Indicates if the keepalive is an automatic keepalive.
+ public final boolean isAutoKeepalive;
// Snapshot of the lifetime stats
public static class LifetimeStats {
@@ -123,10 +128,18 @@
return mKeepaliveActive;
}
- KeepaliveStats(int carrierId, int transportTypes, int intervalSeconds, long timeNow) {
+ KeepaliveStats(
+ int carrierId,
+ int transportTypes,
+ int intervalSeconds,
+ int appUid,
+ boolean isAutoKeepalive,
+ long timeNow) {
this.carrierId = carrierId;
this.transportTypes = transportTypes;
this.intervalMs = intervalSeconds * 1000;
+ this.appUid = appUid;
+ this.isAutoKeepalive = isAutoKeepalive;
mLastUpdateLifetimeTimestamp = timeNow;
}
@@ -217,6 +230,10 @@
final Map<LifetimeKey, KeepaliveLifetimeForCarrier.Builder> mAggregateKeepaliveLifetime =
new HashMap<>();
+ private final Set<Integer> mAppUids = new HashSet<Integer>();
+ private int mNumKeepaliveRequests = 0;
+ private int mNumAutomaticKeepaliveRequests = 0;
+
private int mNumRegisteredKeepalive = 0;
private int mNumActiveKeepalive = 0;
@@ -375,14 +392,20 @@
@NonNull Network network,
int slot,
@NonNull NetworkCapabilities nc,
- int intervalSeconds) {
+ int intervalSeconds,
+ int appUid,
+ boolean isAutoKeepalive) {
ensureRunningOnHandlerThread();
-
final int keepaliveId = getKeepaliveId(network, slot);
if (mKeepaliveStatsPerId.contains(keepaliveId)) {
throw new IllegalArgumentException(
"Attempt to start keepalive stats on a known network, slot pair");
}
+
+ mNumKeepaliveRequests++;
+ if (isAutoKeepalive) mNumAutomaticKeepaliveRequests++;
+ mAppUids.add(appUid);
+
final long timeNow = mDependencies.getUptimeMillis();
updateDurationsPerNumOfKeepalive(timeNow);
@@ -391,7 +414,12 @@
final KeepaliveStats newKeepaliveStats =
new KeepaliveStats(
- getCarrierId(nc), getTransportTypes(nc), intervalSeconds, timeNow);
+ getCarrierId(nc),
+ getTransportTypes(nc),
+ intervalSeconds,
+ appUid,
+ isAutoKeepalive,
+ timeNow);
mKeepaliveStatsPerId.put(keepaliveId, newKeepaliveStats);
}
@@ -548,9 +576,12 @@
final DailykeepaliveInfoReported.Builder dailyKeepaliveInfoReported =
DailykeepaliveInfoReported.newBuilder();
- // TODO(b/273451360): fill all the other values and write to ConnectivityStatsLog.
dailyKeepaliveInfoReported.setDurationPerNumOfKeepalive(durationPerNumOfKeepalive);
dailyKeepaliveInfoReported.setKeepaliveLifetimePerCarrier(keepaliveLifetimePerCarrier);
+ dailyKeepaliveInfoReported.setKeepaliveRequests(mNumKeepaliveRequests);
+ dailyKeepaliveInfoReported.setAutomaticKeepaliveRequests(mNumAutomaticKeepaliveRequests);
+ dailyKeepaliveInfoReported.setDistinctUserCount(mAppUids.size());
+ dailyKeepaliveInfoReported.addAllUid(mAppUids);
return dailyKeepaliveInfoReported.build();
}
@@ -568,12 +599,22 @@
final DailykeepaliveInfoReported metrics = buildKeepaliveMetrics(timeNow);
mDurationPerNumOfKeepalive.clear();
+ mAggregateKeepaliveLifetime.clear();
+ mAppUids.clear();
+ mNumKeepaliveRequests = 0;
+ mNumAutomaticKeepaliveRequests = 0;
+
+ // Update the metrics with the existing keepalives.
ensureDurationPerNumOfKeepaliveSize();
mAggregateKeepaliveLifetime.clear();
// Reset the stats for existing keepalives
for (int i = 0; i < mKeepaliveStatsPerId.size(); i++) {
- mKeepaliveStatsPerId.valueAt(i).resetLifetimeStats(timeNow);
+ final KeepaliveStats keepaliveStats = mKeepaliveStatsPerId.valueAt(i);
+ keepaliveStats.resetLifetimeStats(timeNow);
+ mAppUids.add(keepaliveStats.appUid);
+ mNumKeepaliveRequests++;
+ if (keepaliveStats.isAutoKeepalive) mNumAutomaticKeepaliveRequests++;
}
return metrics;
diff --git a/service/src/com/android/server/connectivity/KeepaliveTracker.java b/service/src/com/android/server/connectivity/KeepaliveTracker.java
index 1fd8a62..b4f74d5 100644
--- a/service/src/com/android/server/connectivity/KeepaliveTracker.java
+++ b/service/src/com/android/server/connectivity/KeepaliveTracker.java
@@ -271,6 +271,10 @@
return mInterval;
}
+ public int getUid() {
+ return mUid;
+ }
+
private int checkNetworkConnected() {
if (!mNai.networkInfo.isConnectedOrConnecting()) {
return ERROR_INVALID_NETWORK;
diff --git a/tests/unit/java/com/android/server/connectivity/KeepaliveStatsTrackerTest.java b/tests/unit/java/com/android/server/connectivity/KeepaliveStatsTrackerTest.java
index b469ccd..6558004 100644
--- a/tests/unit/java/com/android/server/connectivity/KeepaliveStatsTrackerTest.java
+++ b/tests/unit/java/com/android/server/connectivity/KeepaliveStatsTrackerTest.java
@@ -21,10 +21,9 @@
import static com.android.testutils.HandlerUtils.visibleOnHandlerThread;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
-import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
@@ -54,6 +53,7 @@
import com.android.metrics.KeepaliveLifetimeForCarrier;
import com.android.metrics.KeepaliveLifetimePerCarrier;
import com.android.modules.utils.BackgroundThread;
+import com.android.net.module.util.CollectionUtils;
import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRunner;
import com.android.testutils.HandlerUtils;
@@ -89,6 +89,7 @@
buildCellNetworkCapabilitiesWithSubId(TEST_SUB_ID_1);
private static final NetworkCapabilities TEST_NETWORK_CAPABILITIES_2 =
buildCellNetworkCapabilitiesWithSubId(TEST_SUB_ID_2);
+ private static final int TEST_UID = 1234;
private static NetworkCapabilities buildCellNetworkCapabilitiesWithSubId(int subId) {
final TelephonyNetworkSpecifier telephonyNetworkSpecifier =
@@ -238,9 +239,15 @@
private void onStartKeepalive(
long time, int slot, NetworkCapabilities nc, int intervalSeconds) {
+ onStartKeepalive(time, slot, nc, intervalSeconds, TEST_UID, /* isAutoKeepalive= */ true);
+ }
+
+ private void onStartKeepalive(long time, int slot, NetworkCapabilities nc, int intervalSeconds,
+ int uid, boolean isAutoKeepalive) {
setUptimeMillis(time);
visibleOnHandlerThread(mTestHandler, () ->
- mKeepaliveStatsTracker.onStartKeepalive(TEST_NETWORK, slot, nc, intervalSeconds));
+ mKeepaliveStatsTracker.onStartKeepalive(TEST_NETWORK, slot, nc, intervalSeconds,
+ uid, isAutoKeepalive));
}
private void onPauseKeepalive(long time, int slot) {
@@ -270,7 +277,9 @@
TEST_NETWORK,
TEST_SLOT,
TEST_NETWORK_CAPABILITIES,
- TEST_KEEPALIVE_INTERVAL_SEC));
+ TEST_KEEPALIVE_INTERVAL_SEC,
+ TEST_UID,
+ /* isAutoKeepalive */ true));
assertThrows(
IllegalStateException.class,
() -> mKeepaliveStatsTracker.onPauseKeepalive(TEST_NETWORK, TEST_SLOT));
@@ -378,14 +387,20 @@
private void assertDailyKeepaliveInfoReported(
DailykeepaliveInfoReported dailyKeepaliveInfoReported,
+ int expectRequestsCount,
+ int expectAutoRequestsCount,
+ int[] expectAppUids,
int[] expectRegisteredDurations,
int[] expectActiveDurations,
KeepaliveCarrierStats[] expectKeepaliveCarrierStatsArray) {
- // TODO(b/273451360) Assert these values when they are filled.
- assertFalse(dailyKeepaliveInfoReported.hasKeepaliveRequests());
- assertFalse(dailyKeepaliveInfoReported.hasAutomaticKeepaliveRequests());
- assertFalse(dailyKeepaliveInfoReported.hasDistinctUserCount());
- assertTrue(dailyKeepaliveInfoReported.getUidList().isEmpty());
+ assertEquals(expectRequestsCount, dailyKeepaliveInfoReported.getKeepaliveRequests());
+ assertEquals(
+ expectAutoRequestsCount,
+ dailyKeepaliveInfoReported.getAutomaticKeepaliveRequests());
+ assertEquals(expectAppUids.length, dailyKeepaliveInfoReported.getDistinctUserCount());
+
+ final int[] uidArray = CollectionUtils.toIntArray(dailyKeepaliveInfoReported.getUidList());
+ assertArrayEquals(expectAppUids, uidArray);
final DurationPerNumOfKeepalive actualDurations =
dailyKeepaliveInfoReported.getDurationPerNumOfKeepalive();
@@ -410,6 +425,9 @@
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 0,
+ /* expectAutoRequestsCount= */ 0,
+ /* expectAppUids= */ new int[0],
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[0]);
@@ -438,6 +456,9 @@
final int[] expectActiveDurations = new int[] {startTime, writeTime - startTime};
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[] {
@@ -473,6 +494,9 @@
new int[] {startTime + (writeTime - pauseTime), pauseTime - startTime};
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[] {
@@ -514,6 +538,9 @@
};
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[] {
@@ -559,6 +586,9 @@
};
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[] {
@@ -598,6 +628,9 @@
new int[] {startTime + (writeTime - pauseTime), (pauseTime - startTime)};
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[] {
@@ -646,6 +679,9 @@
};
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[] {
@@ -719,6 +755,9 @@
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 2,
+ /* expectAutoRequestsCount= */ 2,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations,
expectActiveDurations,
// The carrier stats are aggregated here since the keepalives have the same
@@ -754,6 +793,9 @@
final int[] expectActiveDurations = new int[] {startTime, writeTime - startTime};
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[] {
@@ -767,6 +809,9 @@
// Expect the stored durations to be 0 but still contain the number of keepalive = 1.
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported2,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
/* expectRegisteredDurations= */ new int[] {0, 0},
/* expectActiveDurations= */ new int[] {0, 0},
new KeepaliveCarrierStats[] {getDefaultCarrierStats(0, 0)});
@@ -783,6 +828,9 @@
new int[] {writeTime2 - stopTime, stopTime - writeTime};
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported3,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations2,
expectActiveDurations2,
new KeepaliveCarrierStats[] {
@@ -844,6 +892,9 @@
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 2,
+ /* expectAutoRequestsCount= */ 2,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[] {
@@ -868,6 +919,9 @@
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported2,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations2,
expectActiveDurations2,
new KeepaliveCarrierStats[] {expectKeepaliveCarrierStats3});
@@ -893,6 +947,9 @@
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[] {
@@ -932,6 +989,9 @@
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 2,
+ /* expectAutoRequestsCount= */ 2,
+ /* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[] {
@@ -976,6 +1036,9 @@
writeTime - startTime);
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 2,
+ /* expectAutoRequestsCount= */ 2,
+ /* expectAppUids= */ new int[] {TEST_UID},
/* expectRegisteredDurations= */ new int[] {startTime, 0, writeTime - startTime},
/* expectActiveDurations= */ new int[] {startTime, 0, writeTime - startTime},
new KeepaliveCarrierStats[] {
@@ -1016,8 +1079,53 @@
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 1,
+ /* expectAutoRequestsCount= */ 1,
+ /* expectAppUids= */ new int[] {TEST_UID},
/* expectRegisteredDurations= */ new int[] {startTime, writeTime - startTime},
/* expectActiveDurations= */ new int[] {startTime, writeTime - startTime},
new KeepaliveCarrierStats[] {expectKeepaliveCarrierStats});
}
+
+ @Test
+ public void testKeepaliveCountsAndUids() {
+ final int startTime1 = 1000, startTime2 = 2000, startTime3 = 3000;
+ final int writeTime = 5000;
+ final int[] uids = new int[] {TEST_UID, TEST_UID + 1, TEST_UID + 2};
+ onStartKeepalive(startTime1, TEST_SLOT, TEST_NETWORK_CAPABILITIES,
+ TEST_KEEPALIVE_INTERVAL_SEC, uids[0], /* isAutoKeepalive= */ true);
+ onStartKeepalive(startTime2, TEST_SLOT + 1, TEST_NETWORK_CAPABILITIES,
+ TEST_KEEPALIVE_INTERVAL_SEC, uids[1], /* isAutoKeepalive= */ false);
+ onStartKeepalive(startTime3, TEST_SLOT + 2, TEST_NETWORK_CAPABILITIES,
+ TEST_KEEPALIVE_INTERVAL_SEC, uids[2], /* isAutoKeepalive= */ true);
+
+ final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
+ buildKeepaliveMetrics(writeTime);
+ final int[] expectRegisteredDurations =
+ new int[] {
+ startTime1,
+ (startTime2 - startTime1),
+ (startTime3 - startTime2),
+ (writeTime - startTime3)
+ };
+ final int[] expectActiveDurations =
+ new int[] {
+ startTime1,
+ (startTime2 - startTime1),
+ (startTime3 - startTime2),
+ (writeTime - startTime3)
+ };
+ assertDailyKeepaliveInfoReported(
+ dailyKeepaliveInfoReported,
+ /* expectRequestsCount= */ 3,
+ /* expectAutoRequestsCount= */ 2,
+ /* expectAppUids= */ uids,
+ expectRegisteredDurations,
+ expectActiveDurations,
+ new KeepaliveCarrierStats[] {
+ getDefaultCarrierStats(
+ writeTime * 3 - startTime1 - startTime2 - startTime3,
+ writeTime * 3 - startTime1 - startTime2 - startTime3)
+ });
+ }
}