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

Commit 1e51e198 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Use new NetworkStats#addEntries API" into main am: 65bab500 am: ef1359f8

parents 099939d8 ef1359f8
Loading
Loading
Loading
Loading
+23 −6
Original line number Diff line number Diff line
@@ -71,6 +71,7 @@ import static com.android.server.stats.pull.ProcfsMemoryUtil.getProcessCmdlines;
import static com.android.server.stats.pull.ProcfsMemoryUtil.readCmdlineFromProcfs;
import static com.android.server.stats.pull.ProcfsMemoryUtil.readMemorySnapshotFromProcfs;
import static com.android.server.stats.pull.netstats.NetworkStatsUtils.fromPublicNetworkStats;
import static com.android.server.stats.pull.netstats.NetworkStatsUtils.isAddEntriesSupported;

import static libcore.io.IoUtils.closeQuietly;

@@ -1388,14 +1389,22 @@ public class StatsPullAtomService extends SystemService {

    @NonNull
    private static NetworkStats removeEmptyEntries(NetworkStats stats) {
        NetworkStats ret = new NetworkStats(0, 1);
        final ArrayList<NetworkStats.Entry> entries = new ArrayList<>();
        for (NetworkStats.Entry e : stats) {
            if (e.getRxBytes() != 0 || e.getRxPackets() != 0 || e.getTxBytes() != 0
                    || e.getTxPackets() != 0 || e.getOperations() != 0) {
                ret = ret.addEntry(e);
                entries.add(e);
            }
        }
        return ret;
        if (isAddEntriesSupported()) {
            return new NetworkStats(0, entries.size()).addEntries(entries);
        } else {
            NetworkStats outputStats = new NetworkStats(0L, 1);
            for (NetworkStats.Entry e : entries) {
                outputStats = outputStats.addEntry(e);
            }
            return outputStats;
        }
    }

    private void addNetworkStats(int atomTag, @NonNull List<StatsEvent> ret,
@@ -1720,11 +1729,19 @@ public class StatsPullAtomService extends SystemService {
    @NonNull
    private NetworkStats sliceNetworkStats(@NonNull NetworkStats stats,
            @NonNull Function<NetworkStats.Entry, NetworkStats.Entry> slicer) {
        NetworkStats ret = new NetworkStats(0, 1);
        final ArrayList<NetworkStats.Entry> entries = new ArrayList();
        for (NetworkStats.Entry e : stats) {
            ret = ret.addEntry(slicer.apply(e));
            entries.add(slicer.apply(e));
        }
        if (isAddEntriesSupported()) {
            return new NetworkStats(0, entries.size()).addEntries(entries);
        } else {
            NetworkStats outputStats = new NetworkStats(0L, 1);
            for (NetworkStats.Entry e : entries) {
                outputStats = outputStats.addEntry(e);
            }
            return outputStats;
        }
        return ret;
    }

    private void registerWifiBytesTransferBackground() {
+19 −3
Original line number Diff line number Diff line
@@ -24,6 +24,9 @@ import static android.net.NetworkStats.SET_ALL;
import android.app.usage.NetworkStats;

import com.android.internal.annotations.VisibleForTesting;
import com.android.server.stats.Flags;

import java.util.ArrayList;

/**
 * Utility methods for accessing {@link android.net.NetworkStats}.
@@ -35,13 +38,22 @@ public class NetworkStatsUtils {
     */
    public static android.net.NetworkStats fromPublicNetworkStats(
            NetworkStats publiceNetworkStats) {
        android.net.NetworkStats stats = new android.net.NetworkStats(0L, 0);
        final ArrayList<android.net.NetworkStats.Entry> entries = new ArrayList<>();
        while (publiceNetworkStats.hasNextBucket()) {
            NetworkStats.Bucket bucket = new NetworkStats.Bucket();
            publiceNetworkStats.getNextBucket(bucket);
            final android.net.NetworkStats.Entry entry = fromBucket(bucket);
            entries.add(fromBucket(bucket));
        }
        android.net.NetworkStats stats = new android.net.NetworkStats(0L, 1);
        // The new API is only supported on devices running the mainline version of `NetworkStats`.
        // It should always be used when available for memory efficiency.
        if (isAddEntriesSupported()) {
            stats = stats.addEntries(entries);
        } else {
            for (android.net.NetworkStats.Entry entry : entries) {
                stats = stats.addEntry(entry);
            }
        }
        return stats;
    }

@@ -106,4 +118,8 @@ public class NetworkStatsUtils {
        }
        return 0;
    }

    public static boolean isAddEntriesSupported() {
        return Flags.netstatsUseAddEntries();
    }
}
+14 −0
Original line number Diff line number Diff line
package: "com.android.server.stats"
container: "system"

# Note: To ensure compatibility across all release configurations, initiate the ramp-up process
# only after the 'com.android.net.flags.netstats_add_entries' flag has been fully deployed.
# This flag provides the necessary API from the Connectivity module.
# The flag was added because the flag 'com.android.net.flags.netstats_add_entries' for API
# is already being rolled out, and modifying behavior during an active rollout might
# lead to unwanted issues.
flag {
    name: "netstats_use_add_entries"
    namespace: "statsd"
    description: "Use NetworkStats#addEntries to reduce memory footprint"
    bug: "335680025"
    is_fixed_read_only: true
}

flag {
    name: "add_mobile_bytes_transfer_by_proc_state_puller"
    namespace: "statsd"