diff options
| author | 2024-10-09 08:34:20 +0000 | |
|---|---|---|
| committer | 2024-10-09 08:34:20 +0000 | |
| commit | 71ecd94b81c5bf366e71fd729f1147acd33a0bdc (patch) | |
| tree | e64d726e6f3f0a05f1711e4dd60caf7c71fa5156 | |
| parent | 91d09b9fbe20c86b0d2fe9c3b7b63038dec0f518 (diff) | |
| parent | 9a486fc305225f173c8025fc60e6d1405b046f04 (diff) | |
Merge "Update nullability annotation for NetworkStats atom queries" into main
3 files changed, 47 insertions, 66 deletions
diff --git a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java index 9662a8787b4b..e7735d8480f8 100644 --- a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java +++ b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java @@ -1275,49 +1275,39 @@ public class StatsPullAtomService extends SystemService { case FrameworkStatsLog.WIFI_BYTES_TRANSFER: { final NetworkStats stats = getUidNetworkStatsSnapshotForTransportLocked( TRANSPORT_WIFI); - if (stats != null) { - ret.add(new NetworkStatsExt(sliceNetworkStatsByUid(stats), - new int[]{TRANSPORT_WIFI}, /*slicedByFgbg=*/false)); - } + ret.add(new NetworkStatsExt(sliceNetworkStatsByUid(stats), + new int[]{TRANSPORT_WIFI}, /*slicedByFgbg=*/false)); break; } case FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: { final NetworkStats stats = getUidNetworkStatsSnapshotForTransportLocked( TRANSPORT_WIFI); - if (stats != null) { - ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats), - new int[]{TRANSPORT_WIFI}, /*slicedByFgbg=*/true)); - } + ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats), + new int[]{TRANSPORT_WIFI}, /*slicedByFgbg=*/true)); break; } case FrameworkStatsLog.MOBILE_BYTES_TRANSFER: { final NetworkStats stats = getUidNetworkStatsSnapshotForTransportLocked(TRANSPORT_CELLULAR); - if (stats != null) { - ret.add(new NetworkStatsExt(sliceNetworkStatsByUid(stats), - new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/false)); - } + ret.add(new NetworkStatsExt(sliceNetworkStatsByUid(stats), + new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/false)); break; } case FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: { final NetworkStats stats = getUidNetworkStatsSnapshotForTransportLocked(TRANSPORT_CELLULAR); - if (stats != null) { - ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats), - new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/true)); - } + ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats), + new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/true)); break; } case FrameworkStatsLog.PROXY_BYTES_TRANSFER_BY_FG_BG: { final NetworkStats stats = getUidNetworkStatsSnapshotForTemplateLocked( new NetworkTemplate.Builder(MATCH_PROXY).build(), /*includeTags=*/false); - if (stats != null) { - ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats), - new int[]{TRANSPORT_BLUETOOTH}, - /*slicedByFgbg=*/true, /*slicedByTag=*/false, - /*slicedByMetered=*/false, TelephonyManager.NETWORK_TYPE_UNKNOWN, - /*subInfo=*/null, OEM_MANAGED_ALL, /*isTypeProxy=*/true)); - } + ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats), + new int[]{TRANSPORT_BLUETOOTH}, + /*slicedByFgbg=*/true, /*slicedByTag=*/false, + /*slicedByMetered=*/false, TelephonyManager.NETWORK_TYPE_UNKNOWN, + /*subInfo=*/null, OEM_MANAGED_ALL, /*isTypeProxy=*/true)); break; } case FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED: { @@ -1326,14 +1316,12 @@ public class StatsPullAtomService extends SystemService { final NetworkStats cellularStats = getUidNetworkStatsSnapshotForTemplateLocked( new NetworkTemplate.Builder(MATCH_MOBILE) .setMeteredness(METERED_YES).build(), /*includeTags=*/true); - if (wifiStats != null && cellularStats != null) { - final NetworkStats stats = wifiStats.add(cellularStats); - ret.add(new NetworkStatsExt(sliceNetworkStatsByUidTagAndMetered(stats), - new int[]{TRANSPORT_WIFI, TRANSPORT_CELLULAR}, - /*slicedByFgbg=*/false, /*slicedByTag=*/true, - /*slicedByMetered=*/true, TelephonyManager.NETWORK_TYPE_UNKNOWN, - /*subInfo=*/null, OEM_MANAGED_ALL, /*isTypeProxy=*/false)); - } + final NetworkStats stats = wifiStats.add(cellularStats); + ret.add(new NetworkStatsExt(sliceNetworkStatsByUidTagAndMetered(stats), + new int[]{TRANSPORT_WIFI, TRANSPORT_CELLULAR}, + /*slicedByFgbg=*/false, /*slicedByTag=*/true, + /*slicedByMetered=*/true, TelephonyManager.NETWORK_TYPE_UNKNOWN, + /*subInfo=*/null, OEM_MANAGED_ALL, /*isTypeProxy=*/false)); break; } case FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER: { @@ -1519,12 +1507,10 @@ public class StatsPullAtomService extends SystemService { final NetworkStats stats = getUidNetworkStatsSnapshotForTemplateLocked( template, false); final Integer transport = ruleAndTransport.second; - if (stats != null) { - ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats), - new int[]{transport}, /*slicedByFgbg=*/true, /*slicedByTag=*/false, - /*slicedByMetered=*/false, TelephonyManager.NETWORK_TYPE_UNKNOWN, - /*subInfo=*/null, oemManaged, /*isTypeProxy=*/false)); - } + ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats), + new int[]{transport}, /*slicedByFgbg=*/true, /*slicedByTag=*/false, + /*slicedByMetered=*/false, TelephonyManager.NETWORK_TYPE_UNKNOWN, + /*subInfo=*/null, oemManaged, /*isTypeProxy=*/false)); } } @@ -1535,7 +1521,7 @@ public class StatsPullAtomService extends SystemService { * Create a snapshot of NetworkStats for a given transport. */ @GuardedBy("mDataBytesTransferLock") - @Nullable + @NonNull private NetworkStats getUidNetworkStatsSnapshotForTransportLocked(int transport) { NetworkTemplate template = null; switch (transport) { @@ -1574,7 +1560,7 @@ public class StatsPullAtomService extends SystemService { * some traffic before boot. */ @GuardedBy("mDataBytesTransferLock") - @Nullable + @NonNull private NetworkStats getUidNetworkStatsSnapshotForTemplateLocked( @NonNull NetworkTemplate template, boolean includeTags) { final long elapsedMillisSinceBoot = SystemClock.elapsedRealtime(); @@ -1613,7 +1599,7 @@ public class StatsPullAtomService extends SystemService { } @GuardedBy("mDataBytesTransferLock") - @Nullable + @NonNull private NetworkStats getUidNetworkStatsSnapshotForTemplateLocked( @NonNull NetworkTemplate template, boolean includeTags, long startTime, long endTime) { final long elapsedMillisSinceBoot = SystemClock.elapsedRealtime(); @@ -1660,12 +1646,10 @@ public class StatsPullAtomService extends SystemService { .setMeteredness(METERED_YES).build(); final NetworkStats stats = getUidNetworkStatsSnapshotForTemplateLocked(template, /*includeTags=*/false); - if (stats != null) { - ret.add(new NetworkStatsExt(sliceNetworkStatsByFgbg(stats), - new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/true, - /*slicedByTag=*/false, /*slicedByMetered=*/false, ratType, subInfo, - OEM_MANAGED_ALL, /*isTypeProxy=*/false)); - } + ret.add(new NetworkStatsExt(sliceNetworkStatsByFgbg(stats), + new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/true, + /*slicedByTag=*/false, /*slicedByMetered=*/false, ratType, subInfo, + OEM_MANAGED_ALL, /*isTypeProxy=*/false)); } return ret; } diff --git a/services/core/java/com/android/server/stats/pull/netstats/NetworkStatsAccumulator.java b/services/core/java/com/android/server/stats/pull/netstats/NetworkStatsAccumulator.java index cc63968ca844..e798bc487031 100644 --- a/services/core/java/com/android/server/stats/pull/netstats/NetworkStatsAccumulator.java +++ b/services/core/java/com/android/server/stats/pull/netstats/NetworkStatsAccumulator.java @@ -17,7 +17,6 @@ package com.android.server.stats.pull.netstats; import android.annotation.NonNull; -import android.annotation.Nullable; import android.net.NetworkStats; import android.net.NetworkTemplate; @@ -55,7 +54,7 @@ public class NetworkStatsAccumulator { * This method method may call {@code queryFunction} more than once, which includes maintaining * an internal cumulative stats snapshot and querying stats after the snapshot. */ - @Nullable + @NonNull public NetworkStats queryStats(long currentTimeMillis, @NonNull StatsQueryFunction queryFunction) { maybeExpandSnapshot(currentTimeMillis, queryFunction); @@ -80,23 +79,21 @@ public class NetworkStatsAccumulator { if (newEndTimeMillis - mSnapshotEndTimeMillis > mBucketDurationMillis) { NetworkStats extraStats = queryFunction.queryNetworkStats(mTemplate, mWithTags, mSnapshotEndTimeMillis, newEndTimeMillis); - if (extraStats != null) { - mSnapshot = mSnapshot.add(extraStats); - mSnapshotEndTimeMillis = newEndTimeMillis; - } + mSnapshot = mSnapshot.add(extraStats); + mSnapshotEndTimeMillis = newEndTimeMillis; } } /** * Adds up stats in the internal cumulative snapshot and the stats that follow after it. */ - @Nullable + @NonNull private NetworkStats snapshotPlusFollowingStats(long currentTimeMillis, @NonNull StatsQueryFunction queryFunction) { // Set end time in the future to include all stats in the active bucket. NetworkStats extraStats = queryFunction.queryNetworkStats(mTemplate, mWithTags, mSnapshotEndTimeMillis, currentTimeMillis + mBucketDurationMillis); - return extraStats != null ? mSnapshot.add(extraStats) : null; + return mSnapshot.add(extraStats); } @FunctionalInterface @@ -104,7 +101,7 @@ public class NetworkStatsAccumulator { /** * Returns network stats during the given time period. */ - @Nullable + @NonNull NetworkStats queryNetworkStats(@NonNull NetworkTemplate template, boolean includeTags, long startTime, long endTime); } diff --git a/services/tests/servicestests/src/com/android/server/stats/pull/netstats/NetworkStatsAccumulatorTest.kt b/services/tests/servicestests/src/com/android/server/stats/pull/netstats/NetworkStatsAccumulatorTest.kt index 7280c6935d73..8cf0e82259a2 100644 --- a/services/tests/servicestests/src/com/android/server/stats/pull/netstats/NetworkStatsAccumulatorTest.kt +++ b/services/tests/servicestests/src/com/android/server/stats/pull/netstats/NetworkStatsAccumulatorTest.kt @@ -60,7 +60,7 @@ class NetworkStatsAccumulatorTest { // Accumulator has data until 1000 (= 0), and its end-point is still in the history period. // Current time is less than one bucket away from snapshot end-point: 1050 - 1000 < 200 - val stats = snapshot.queryStats(1050, FakeStats(500, 1050, 1))!! + val stats = snapshot.queryStats(1050, FakeStats(500, 1050, 1)) // After the query at 1050, accumulator should have 1 * (1050 - 1000) = 50 bytes. assertNetworkStatsEquals(stats, networkStatsWithBytes(50)) @@ -72,7 +72,7 @@ class NetworkStatsAccumulatorTest { // Accumulator has data until 1000 (= 0), and its end-point is still in the history period. // Current time is one bucket away from snapshot end-point: 1250 - 1000 > 200 - val stats = snapshot.queryStats(1250, FakeStats(550, 1250, 2))!! + val stats = snapshot.queryStats(1250, FakeStats(550, 1250, 2)) // After the query at 1250, accumulator should have 2 * (1250 - 1000) = 500 bytes. assertNetworkStatsEquals(stats, networkStatsWithBytes(500)) @@ -84,7 +84,7 @@ class NetworkStatsAccumulatorTest { // Accumulator has data until 1000 (= 0), and its end-point is in the history period. // Current time is two buckets away from snapshot end-point: 1450 - 1000 > 2*200 - val stats = snapshot.queryStats(1450, FakeStats(600, 1450, 3))!! + val stats = snapshot.queryStats(1450, FakeStats(600, 1450, 3)) // After the query at 1450, accumulator should have 3 * (1450 - 1000) = 1350 bytes. assertNetworkStatsEquals(stats, networkStatsWithBytes(1350)) @@ -96,7 +96,7 @@ class NetworkStatsAccumulatorTest { // Accumulator has data until 1000 (= 0), and its end-point is still in the history period. // Current time is many buckets away from snapshot end-point - val stats = snapshot.queryStats(6100, FakeStats(900, 6100, 1))!! + val stats = snapshot.queryStats(6100, FakeStats(900, 6100, 1)) // After the query at 6100, accumulator should have 1 * (6100 - 1000) = 5100 bytes. assertNetworkStatsEquals(stats, networkStatsWithBytes(5100)) @@ -108,9 +108,9 @@ class NetworkStatsAccumulatorTest { // Accumulator is queried within the history period, whose starting point stays the same. // After each query, accumulator should contain bytes from the initial end-point until now. - val stats1 = snapshot.queryStats(5100, FakeStats(900, 5100, 1))!! - val stats2 = snapshot.queryStats(10100, FakeStats(900, 10100, 1))!! - val stats3 = snapshot.queryStats(15100, FakeStats(900, 15100, 1))!! + val stats1 = snapshot.queryStats(5100, FakeStats(900, 5100, 1)) + val stats2 = snapshot.queryStats(10100, FakeStats(900, 10100, 1)) + val stats3 = snapshot.queryStats(15100, FakeStats(900, 15100, 1)) assertNetworkStatsEquals(stats1, networkStatsWithBytes(4100)) assertNetworkStatsEquals(stats2, networkStatsWithBytes(9100)) @@ -123,9 +123,9 @@ class NetworkStatsAccumulatorTest { // Accumulator is queried within the history period, whose starting point is moving. // After each query, accumulator should contain bytes from the initial end-point until now. - val stats1 = snapshot.queryStats(5100, FakeStats(900, 5100, 1))!! - val stats2 = snapshot.queryStats(10100, FakeStats(4000, 10100, 1))!! - val stats3 = snapshot.queryStats(15100, FakeStats(7000, 15100, 1))!! + val stats1 = snapshot.queryStats(5100, FakeStats(900, 5100, 1)) + val stats2 = snapshot.queryStats(10100, FakeStats(4000, 10100, 1)) + val stats3 = snapshot.queryStats(15100, FakeStats(7000, 15100, 1)) assertNetworkStatsEquals(stats1, networkStatsWithBytes(4100)) assertNetworkStatsEquals(stats2, networkStatsWithBytes(9100)) @@ -138,7 +138,7 @@ class NetworkStatsAccumulatorTest { // Accumulator has data until 1000 (= 0), but its end-point is not in the history period. // After the query, accumulator should add only those bytes that are covered by the history. - val stats = snapshot.queryStats(2700, FakeStats(2200, 2700, 1))!! + val stats = snapshot.queryStats(2700, FakeStats(2200, 2700, 1)) assertNetworkStatsEquals(stats, networkStatsWithBytes(500)) } |