Merge "Populate ProxyBytesTransferByFgBg atom" into main

This commit is contained in:
Ivo Kay 2023-11-20 21:00:35 +00:00 committed by Android (Google) Code Review
commit 9853ac5408
2 changed files with 82 additions and 21 deletions

View File

@ -24,12 +24,14 @@ import static android.content.pm.PermissionInfo.PROTECTION_DANGEROUS;
import static android.hardware.display.HdrConversionMode.HDR_CONVERSION_PASSTHROUGH; import static android.hardware.display.HdrConversionMode.HDR_CONVERSION_PASSTHROUGH;
import static android.hardware.display.HdrConversionMode.HDR_CONVERSION_UNSUPPORTED; import static android.hardware.display.HdrConversionMode.HDR_CONVERSION_UNSUPPORTED;
import static android.hardware.graphics.common.Hdr.DOLBY_VISION; import static android.hardware.graphics.common.Hdr.DOLBY_VISION;
import static android.net.NetworkCapabilities.TRANSPORT_BLUETOOTH;
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET; import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET;
import static android.net.NetworkCapabilities.TRANSPORT_WIFI; import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
import static android.net.NetworkStats.METERED_YES; import static android.net.NetworkStats.METERED_YES;
import static android.net.NetworkTemplate.MATCH_ETHERNET; import static android.net.NetworkTemplate.MATCH_ETHERNET;
import static android.net.NetworkTemplate.MATCH_MOBILE; import static android.net.NetworkTemplate.MATCH_MOBILE;
import static android.net.NetworkTemplate.MATCH_PROXY;
import static android.net.NetworkTemplate.MATCH_WIFI; import static android.net.NetworkTemplate.MATCH_WIFI;
import static android.net.NetworkTemplate.OEM_MANAGED_ALL; import static android.net.NetworkTemplate.OEM_MANAGED_ALL;
import static android.net.NetworkTemplate.OEM_MANAGED_PAID; import static android.net.NetworkTemplate.OEM_MANAGED_PAID;
@ -488,6 +490,7 @@ public class StatsPullAtomService extends SystemService {
case FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: case FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG:
case FrameworkStatsLog.MOBILE_BYTES_TRANSFER: case FrameworkStatsLog.MOBILE_BYTES_TRANSFER:
case FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: case FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG:
case FrameworkStatsLog.PROXY_BYTES_TRANSFER_BY_FG_BG:
case FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED: case FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED:
case FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER: case FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER:
case FrameworkStatsLog.OEM_MANAGED_BYTES_TRANSFER: case FrameworkStatsLog.OEM_MANAGED_BYTES_TRANSFER:
@ -973,21 +976,33 @@ public class StatsPullAtomService extends SystemService {
if (DEBUG) { if (DEBUG) {
Slog.d(TAG, "Registering NetworkStats pullers with statsd"); Slog.d(TAG, "Registering NetworkStats pullers with statsd");
} }
boolean canQueryTypeProxy = canQueryNetworkStatsForTypeProxy();
// Initialize NetworkStats baselines. // Initialize NetworkStats baselines.
mNetworkStatsBaselines.addAll( synchronized (mDataBytesTransferLock) {
collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.WIFI_BYTES_TRANSFER)); mNetworkStatsBaselines.addAll(
mNetworkStatsBaselines.addAll( collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.WIFI_BYTES_TRANSFER));
collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG)); mNetworkStatsBaselines.addAll(
mNetworkStatsBaselines.addAll( collectNetworkStatsSnapshotForAtom(
collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.MOBILE_BYTES_TRANSFER)); FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG));
mNetworkStatsBaselines.addAll(collectNetworkStatsSnapshotForAtom( mNetworkStatsBaselines.addAll(
FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG)); collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.MOBILE_BYTES_TRANSFER));
mNetworkStatsBaselines.addAll(collectNetworkStatsSnapshotForAtom( mNetworkStatsBaselines.addAll(collectNetworkStatsSnapshotForAtom(
FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED)); FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG));
mNetworkStatsBaselines.addAll( mNetworkStatsBaselines.addAll(collectNetworkStatsSnapshotForAtom(
collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER)); FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED));
mNetworkStatsBaselines.addAll( mNetworkStatsBaselines.addAll(
collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.OEM_MANAGED_BYTES_TRANSFER)); collectNetworkStatsSnapshotForAtom(
FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER));
mNetworkStatsBaselines.addAll(
collectNetworkStatsSnapshotForAtom(
FrameworkStatsLog.OEM_MANAGED_BYTES_TRANSFER));
if (canQueryTypeProxy) {
mNetworkStatsBaselines.addAll(collectNetworkStatsSnapshotForAtom(
FrameworkStatsLog.PROXY_BYTES_TRANSFER_BY_FG_BG));
}
}
// Listen to subscription changes to record historical subscriptions that activated before // Listen to subscription changes to record historical subscriptions that activated before
// pulling, this is used by {@code DATA_USAGE_BYTES_TRANSFER}. // pulling, this is used by {@code DATA_USAGE_BYTES_TRANSFER}.
@ -1001,6 +1016,9 @@ public class StatsPullAtomService extends SystemService {
registerBytesTransferByTagAndMetered(); registerBytesTransferByTagAndMetered();
registerDataUsageBytesTransfer(); registerDataUsageBytesTransfer();
registerOemManagedBytesTransfer(); registerOemManagedBytesTransfer();
if (canQueryTypeProxy) {
registerProxyBytesTransferBackground();
}
} }
private void initAndRegisterDeferredPullers() { private void initAndRegisterDeferredPullers() {
@ -1171,6 +1189,18 @@ public class StatsPullAtomService extends SystemService {
} }
break; break;
} }
case FrameworkStatsLog.PROXY_BYTES_TRANSFER_BY_FG_BG: {
final NetworkStats stats = getUidNetworkStatsSnapshotForTemplate(
new NetworkTemplate.Builder(MATCH_PROXY).build(), /*includeTags=*/true);
if (stats != null) {
ret.add(new NetworkStatsExt(sliceNetworkStatsByUidTagAndMetered(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: { case FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED: {
final NetworkStats wifiStats = getUidNetworkStatsSnapshotForTemplate( final NetworkStats wifiStats = getUidNetworkStatsSnapshotForTemplate(
new NetworkTemplate.Builder(MATCH_WIFI).build(), /*includeTags=*/true); new NetworkTemplate.Builder(MATCH_WIFI).build(), /*includeTags=*/true);
@ -1183,7 +1213,7 @@ public class StatsPullAtomService extends SystemService {
new int[]{TRANSPORT_WIFI, TRANSPORT_CELLULAR}, new int[]{TRANSPORT_WIFI, TRANSPORT_CELLULAR},
/*slicedByFgbg=*/false, /*slicedByTag=*/true, /*slicedByFgbg=*/false, /*slicedByTag=*/true,
/*slicedByMetered=*/true, TelephonyManager.NETWORK_TYPE_UNKNOWN, /*slicedByMetered=*/true, TelephonyManager.NETWORK_TYPE_UNKNOWN,
/*subInfo=*/null, OEM_MANAGED_ALL)); /*subInfo=*/null, OEM_MANAGED_ALL, /*isTypeProxy=*/false));
} }
break; break;
} }
@ -1225,7 +1255,7 @@ public class StatsPullAtomService extends SystemService {
final NetworkStatsExt diff = new NetworkStatsExt( final NetworkStatsExt diff = new NetworkStatsExt(
removeEmptyEntries(item.stats.subtract(baseline.stats)), item.transports, removeEmptyEntries(item.stats.subtract(baseline.stats)), item.transports,
item.slicedByFgbg, item.slicedByTag, item.slicedByMetered, item.ratType, item.slicedByFgbg, item.slicedByTag, item.slicedByMetered, item.ratType,
item.subInfo, item.oemManaged); item.subInfo, item.oemManaged, item.isTypeProxy);
// If no diff, skip. // If no diff, skip.
if (!diff.stats.iterator().hasNext()) continue; if (!diff.stats.iterator().hasNext()) continue;
@ -1363,7 +1393,7 @@ public class StatsPullAtomService extends SystemService {
ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats), ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats),
new int[]{transport}, /*slicedByFgbg=*/true, /*slicedByTag=*/false, new int[]{transport}, /*slicedByFgbg=*/true, /*slicedByTag=*/false,
/*slicedByMetered=*/false, TelephonyManager.NETWORK_TYPE_UNKNOWN, /*slicedByMetered=*/false, TelephonyManager.NETWORK_TYPE_UNKNOWN,
/*subInfo=*/null, oemManaged)); /*subInfo=*/null, oemManaged, /*isTypeProxy=*/false));
} }
} }
} }
@ -1391,6 +1421,21 @@ public class StatsPullAtomService extends SystemService {
return getUidNetworkStatsSnapshotForTemplate(template, /*includeTags=*/false); return getUidNetworkStatsSnapshotForTemplate(template, /*includeTags=*/false);
} }
/**
* Check if it is possible to query NetworkStats for TYPE_PROXY. This should only be possible
* if the build includes r.android.com/2828315
* @return true if querying for TYPE_PROXY is allowed
*/
private static boolean canQueryNetworkStatsForTypeProxy() {
try {
new NetworkTemplate.Builder(MATCH_PROXY).build();
return true;
} catch (IllegalArgumentException e) {
Slog.w(TAG, "Querying network stats for TYPE_PROXY is not allowed");
return false;
}
}
/** /**
* Create a snapshot of NetworkStats since boot for the given template, but add 1 bucket * Create a snapshot of NetworkStats since boot for the given template, but add 1 bucket
* duration before boot as a buffer to ensure at least one full bucket will be included. * duration before boot as a buffer to ensure at least one full bucket will be included.
@ -1450,7 +1495,7 @@ public class StatsPullAtomService extends SystemService {
ret.add(new NetworkStatsExt(sliceNetworkStatsByFgbg(stats), ret.add(new NetworkStatsExt(sliceNetworkStatsByFgbg(stats),
new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/true, new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/true,
/*slicedByTag=*/false, /*slicedByMetered=*/false, ratType, subInfo, /*slicedByTag=*/false, /*slicedByMetered=*/false, ratType, subInfo,
OEM_MANAGED_ALL)); OEM_MANAGED_ALL, /*isTypeProxy=*/false));
} }
} }
return ret; return ret;
@ -1600,6 +1645,19 @@ public class StatsPullAtomService extends SystemService {
); );
} }
private void registerProxyBytesTransferBackground() {
int tagId = FrameworkStatsLog.PROXY_BYTES_TRANSFER_BY_FG_BG;
PullAtomMetadata metadata = new PullAtomMetadata.Builder()
.setAdditiveFields(new int[]{3, 4, 5, 6})
.build();
mStatsManager.setPullAtomCallback(
tagId,
metadata,
DIRECT_EXECUTOR,
mStatsCallbackImpl
);
}
private void registerBytesTransferByTagAndMetered() { private void registerBytesTransferByTagAndMetered() {
int tagId = FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED; int tagId = FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED;
PullAtomMetadata metadata = new PullAtomMetadata.Builder() PullAtomMetadata metadata = new PullAtomMetadata.Builder()

View File

@ -42,15 +42,17 @@ public class NetworkStatsExt {
public final int oemManaged; public final int oemManaged;
@Nullable @Nullable
public final SubInfo subInfo; public final SubInfo subInfo;
public final boolean isTypeProxy; // True if matching ConnectivityManager#TYPE_PROXY
public NetworkStatsExt(@NonNull NetworkStats stats, int[] transports, boolean slicedByFgbg) { public NetworkStatsExt(@NonNull NetworkStats stats, int[] transports, boolean slicedByFgbg) {
this(stats, transports, slicedByFgbg, /*slicedByTag=*/false, /*slicedByMetered=*/false, this(stats, transports, slicedByFgbg, /*slicedByTag=*/false, /*slicedByMetered=*/false,
TelephonyManager.NETWORK_TYPE_UNKNOWN, /*subInfo=*/null, OEM_MANAGED_ALL); TelephonyManager.NETWORK_TYPE_UNKNOWN, /*subInfo=*/null,
OEM_MANAGED_ALL, /*isTypeProxy=*/false);
} }
public NetworkStatsExt(@NonNull NetworkStats stats, int[] transports, boolean slicedByFgbg, public NetworkStatsExt(@NonNull NetworkStats stats, int[] transports, boolean slicedByFgbg,
boolean slicedByTag, boolean slicedByMetered, int ratType, boolean slicedByTag, boolean slicedByMetered, int ratType,
@Nullable SubInfo subInfo, int oemManaged) { @Nullable SubInfo subInfo, int oemManaged, boolean isTypeProxy) {
this.stats = stats; this.stats = stats;
// Sort transports array so that we can test for equality without considering order. // Sort transports array so that we can test for equality without considering order.
@ -63,6 +65,7 @@ public class NetworkStatsExt {
this.ratType = ratType; this.ratType = ratType;
this.subInfo = subInfo; this.subInfo = subInfo;
this.oemManaged = oemManaged; this.oemManaged = oemManaged;
this.isTypeProxy = isTypeProxy;
} }
/** /**
@ -72,6 +75,6 @@ public class NetworkStatsExt {
return Arrays.equals(transports, other.transports) && slicedByFgbg == other.slicedByFgbg return Arrays.equals(transports, other.transports) && slicedByFgbg == other.slicedByFgbg
&& slicedByTag == other.slicedByTag && slicedByMetered == other.slicedByMetered && slicedByTag == other.slicedByTag && slicedByMetered == other.slicedByMetered
&& ratType == other.ratType && Objects.equals(subInfo, other.subInfo) && ratType == other.ratType && Objects.equals(subInfo, other.subInfo)
&& oemManaged == other.oemManaged; && oemManaged == other.oemManaged && isTypeProxy == other.isTypeProxy;
} }
} }