Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9853ac54 authored by Ivo Kay's avatar Ivo Kay Committed by Android (Google) Code Review
Browse files

Merge "Populate ProxyBytesTransferByFgBg atom" into main

parents e1d9a7ce 94aa9e84
Loading
Loading
Loading
Loading
+76 −18
Original line number Diff line number Diff line
@@ -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_UNSUPPORTED;
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_ETHERNET;
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
import static android.net.NetworkStats.METERED_YES;
import static android.net.NetworkTemplate.MATCH_ETHERNET;
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.OEM_MANAGED_ALL;
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.MOBILE_BYTES_TRANSFER:
                    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.DATA_USAGE_BYTES_TRANSFER:
                    case FrameworkStatsLog.OEM_MANAGED_BYTES_TRANSFER:
@@ -973,11 +976,16 @@ public class StatsPullAtomService extends SystemService {
        if (DEBUG) {
            Slog.d(TAG, "Registering NetworkStats pullers with statsd");
        }

        boolean canQueryTypeProxy = canQueryNetworkStatsForTypeProxy();

        // Initialize NetworkStats baselines.
        synchronized (mDataBytesTransferLock) {
            mNetworkStatsBaselines.addAll(
                    collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.WIFI_BYTES_TRANSFER));
            mNetworkStatsBaselines.addAll(
                collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG));
                    collectNetworkStatsSnapshotForAtom(
                            FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG));
            mNetworkStatsBaselines.addAll(
                    collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.MOBILE_BYTES_TRANSFER));
            mNetworkStatsBaselines.addAll(collectNetworkStatsSnapshotForAtom(
@@ -985,9 +993,16 @@ public class StatsPullAtomService extends SystemService {
            mNetworkStatsBaselines.addAll(collectNetworkStatsSnapshotForAtom(
                    FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED));
            mNetworkStatsBaselines.addAll(
                collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER));
                    collectNetworkStatsSnapshotForAtom(
                            FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER));
            mNetworkStatsBaselines.addAll(
                collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.OEM_MANAGED_BYTES_TRANSFER));
                    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
        // pulling, this is used by {@code DATA_USAGE_BYTES_TRANSFER}.
@@ -1001,6 +1016,9 @@ public class StatsPullAtomService extends SystemService {
        registerBytesTransferByTagAndMetered();
        registerDataUsageBytesTransfer();
        registerOemManagedBytesTransfer();
        if (canQueryTypeProxy) {
            registerProxyBytesTransferBackground();
        }
    }

    private void initAndRegisterDeferredPullers() {
@@ -1171,6 +1189,18 @@ public class StatsPullAtomService extends SystemService {
                }
                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: {
                final NetworkStats wifiStats = getUidNetworkStatsSnapshotForTemplate(
                        new NetworkTemplate.Builder(MATCH_WIFI).build(), /*includeTags=*/true);
@@ -1183,7 +1213,7 @@ public class StatsPullAtomService extends SystemService {
                            new int[]{TRANSPORT_WIFI, TRANSPORT_CELLULAR},
                            /*slicedByFgbg=*/false, /*slicedByTag=*/true,
                            /*slicedByMetered=*/true, TelephonyManager.NETWORK_TYPE_UNKNOWN,
                            /*subInfo=*/null, OEM_MANAGED_ALL));
                            /*subInfo=*/null, OEM_MANAGED_ALL, /*isTypeProxy=*/false));
                }
                break;
            }
@@ -1225,7 +1255,7 @@ public class StatsPullAtomService extends SystemService {
            final NetworkStatsExt diff = new NetworkStatsExt(
                    removeEmptyEntries(item.stats.subtract(baseline.stats)), item.transports,
                    item.slicedByFgbg, item.slicedByTag, item.slicedByMetered, item.ratType,
                    item.subInfo, item.oemManaged);
                    item.subInfo, item.oemManaged, item.isTypeProxy);

            // If no diff, skip.
            if (!diff.stats.iterator().hasNext()) continue;
@@ -1363,7 +1393,7 @@ public class StatsPullAtomService extends SystemService {
                    ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats),
                            new int[]{transport}, /*slicedByFgbg=*/true, /*slicedByTag=*/false,
                            /*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);
    }

    /**
     * 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
     * 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),
                        new int[]{TRANSPORT_CELLULAR}, /*slicedByFgbg=*/true,
                        /*slicedByTag=*/false, /*slicedByMetered=*/false, ratType, subInfo,
                        OEM_MANAGED_ALL));
                        OEM_MANAGED_ALL, /*isTypeProxy=*/false));
            }
        }
        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() {
        int tagId = FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED;
        PullAtomMetadata metadata = new PullAtomMetadata.Builder()
+6 −3
Original line number Diff line number Diff line
@@ -42,15 +42,17 @@ public class NetworkStatsExt {
    public final int oemManaged;
    @Nullable
    public final SubInfo subInfo;
    public final boolean isTypeProxy; // True if matching ConnectivityManager#TYPE_PROXY

    public NetworkStatsExt(@NonNull NetworkStats stats, int[] transports, boolean slicedByFgbg) {
        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,
            boolean slicedByTag, boolean slicedByMetered, int ratType,
            @Nullable SubInfo subInfo, int oemManaged) {
            @Nullable SubInfo subInfo, int oemManaged, boolean isTypeProxy) {
        this.stats = stats;

        // Sort transports array so that we can test for equality without considering order.
@@ -63,6 +65,7 @@ public class NetworkStatsExt {
        this.ratType = ratType;
        this.subInfo = subInfo;
        this.oemManaged = oemManaged;
        this.isTypeProxy = isTypeProxy;
    }

    /**
@@ -72,6 +75,6 @@ public class NetworkStatsExt {
        return Arrays.equals(transports, other.transports) && slicedByFgbg == other.slicedByFgbg
                && slicedByTag == other.slicedByTag && slicedByMetered == other.slicedByMetered
                && ratType == other.ratType && Objects.equals(subInfo, other.subInfo)
                && oemManaged == other.oemManaged;
                && oemManaged == other.oemManaged && isTypeProxy == other.isTypeProxy;
    }
}