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

Commit 9b69a04e authored by Jeff Sharkey's avatar Jeff Sharkey Committed by Android (Google) Code Review
Browse files

Merge "Sample network statistics for sanity check."

parents c7f159e2 07b0dd9a
Loading
Loading
Loading
Loading
+25 −4
Original line number Diff line number Diff line
@@ -320,15 +320,36 @@ public class NetworkStats implements Parcelable {
     * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
     */
    public long getTotalBytes() {
        long totalBytes = 0;
        final Entry entry = getTotal(null);
        return entry.rxBytes + entry.txBytes;
    }

    /**
     * Return total of all fields represented by this snapshot object.
     */
    public Entry getTotal(Entry recycle) {
        final Entry entry = recycle != null ? recycle : new Entry();

        entry.iface = IFACE_ALL;
        entry.uid = UID_ALL;
        entry.set = SET_ALL;
        entry.tag = TAG_NONE;
        entry.rxBytes = 0;
        entry.rxPackets = 0;
        entry.txBytes = 0;
        entry.txPackets = 0;

        for (int i = 0; i < size; i++) {
            // skip specific tags, since already counted in TAG_NONE
            if (tag[i] != TAG_NONE) continue;

            totalBytes += rxBytes[i];
            totalBytes += txBytes[i];
            entry.rxBytes += rxBytes[i];
            entry.rxPackets += rxPackets[i];
            entry.txBytes += txBytes[i];
            entry.txPackets += txPackets[i];
            entry.operations += operations[i];
        }
        return totalBytes;
        return entry;
    }

    /**
+7 −0
Original line number Diff line number Diff line
@@ -137,3 +137,10 @@ option java_package com.android.server
# [ 8- 3] Detailed state ordinal (as defined by NetworkInfo.DetailedState)
# [ 2- 0] Network type (as defined by ConnectivityManager)
50020 connectivity_state_changed (custom|1|5)


# ---------------------------
# NetworkStatsService.java
# ---------------------------
51100 netstats_mobile_sample (iface_rx|2|2),(iface_tx|2|2),(uid_rx|2|2),(uid_tx|2|2)
51101 netstats_wifi_sample (iface_rx|2|2),(iface_tx|2|2),(uid_rx|2|2),(uid_tx|2|2)
+44 −0
Original line number Diff line number Diff line
@@ -31,6 +31,8 @@ import static android.net.NetworkStats.SET_DEFAULT;
import static android.net.NetworkStats.SET_FOREGROUND;
import static android.net.NetworkStats.TAG_NONE;
import static android.net.NetworkStats.UID_ALL;
import static android.net.NetworkTemplate.buildTemplateMobileAll;
import static android.net.NetworkTemplate.buildTemplateWifi;
import static android.net.TrafficStats.UID_REMOVED;
import static android.provider.Settings.Secure.NETSTATS_NETWORK_BUCKET_DURATION;
import static android.provider.Settings.Secure.NETSTATS_NETWORK_MAX_HISTORY;
@@ -76,6 +78,7 @@ import android.os.RemoteException;
import android.os.SystemClock;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.EventLog;
import android.util.NtpTrustedTime;
import android.util.Slog;
import android.util.SparseIntArray;
@@ -83,6 +86,7 @@ import android.util.TrustedTime;

import com.android.internal.os.AtomicFile;
import com.android.internal.util.Objects;
import com.android.server.EventLogTags;
import com.google.android.collect.Lists;
import com.google.android.collect.Maps;
import com.google.android.collect.Sets;
@@ -387,7 +391,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
                    entry.uid = UID_ALL;
                    entry.tag = TAG_NONE;
                    entry.rxBytes = historyEntry.rxBytes;
                    entry.rxPackets = historyEntry.rxPackets;
                    entry.txBytes = historyEntry.txBytes;
                    entry.txPackets = historyEntry.txPackets;

                    stats.combineValues(entry);
                }
@@ -716,6 +722,11 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
            Slog.v(TAG, "performPollLocked() took " + duration + "ms");
        }

        // sample stats after detailed poll
        if (detailedPoll) {
            performSample();
        }

        // finally, dispatch updated event to any listeners
        final Intent updatedIntent = new Intent(ACTION_NETWORK_STATS_UPDATED);
        updatedIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
@@ -808,6 +819,33 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        mOperations = new NetworkStats(0L, 10);
    }

    /**
     * Sample recent statistics summary into {@link EventLog}.
     */
    private void performSample() {
        // take sample as total over last 4 hours
        final long end = mTime.hasCache() ? mTime.currentTimeMillis() : System.currentTimeMillis();
        final long start = end - (4 * HOUR_IN_MILLIS);

        NetworkTemplate template = null;
        NetworkStats.Entry ifaceTotal = null;
        NetworkStats.Entry uidTotal = null;

        // collect mobile sample
        template = buildTemplateMobileAll(getActiveSubscriberId(mContext));
        ifaceTotal = getSummaryForNetwork(template, start, end).getTotal(ifaceTotal);
        uidTotal = getSummaryForAllUid(template, start, end, false).getTotal(uidTotal);
        EventLogTags.writeNetstatsMobileSample(
                ifaceTotal.rxBytes, ifaceTotal.txBytes, uidTotal.rxBytes, uidTotal.txBytes);

        // collect wifi sample
        template = buildTemplateWifi();
        ifaceTotal = getSummaryForNetwork(template, start, end).getTotal(ifaceTotal);
        uidTotal = getSummaryForAllUid(template, start, end, false).getTotal(uidTotal);
        EventLogTags.writeNetstatsWifiSample(
                ifaceTotal.rxBytes, ifaceTotal.txBytes, uidTotal.rxBytes, uidTotal.txBytes);
    }

    /**
     * Clean up {@link #mUidStats} after UID is removed.
     */
@@ -1249,6 +1287,12 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        }
    };

    private static String getActiveSubscriberId(Context context) {
        final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
                Context.TELEPHONY_SERVICE);
        return telephony.getSubscriberId();
    }

    /**
     * Key uniquely identifying a {@link NetworkStatsHistory} for a UID.
     */