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

Commit 833603ca authored by Benedict Wong's avatar Benedict Wong
Browse files

Remove VPN info arrays from NetworkStats(Observer|Recorder)

This change removes the now-unused VPN arrays in the network stats
observer and recorder classes. These are always null values in every
call site.

Bug: 113122541
Bug: 120145746
Bug: 129264869
Bug: 134244752
Test: FrameworksNetTest passing
Test: Manual tests show data usage fixes maintained.
Change-Id: Ieb8645acc400fdaeb0df7092c5369b96f9f35af9
parent 9fbbdebc
Loading
Loading
Loading
Loading
+5 −8
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ import android.util.Slog;
import android.util.SparseArray;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.net.VpnInfo;

import java.util.concurrent.atomic.AtomicInteger;

@@ -104,9 +103,9 @@ class NetworkStatsObservers {
    public void updateStats(NetworkStats xtSnapshot, NetworkStats uidSnapshot,
                ArrayMap<String, NetworkIdentitySet> activeIfaces,
                ArrayMap<String, NetworkIdentitySet> activeUidIfaces,
                VpnInfo[] vpnArray, long currentTime) {
                long currentTime) {
        StatsContext statsContext = new StatsContext(xtSnapshot, uidSnapshot, activeIfaces,
                activeUidIfaces, vpnArray, currentTime);
                activeUidIfaces, currentTime);
        getHandler().sendMessage(mHandler.obtainMessage(MSG_UPDATE_STATS, statsContext));
    }

@@ -354,7 +353,7 @@ class NetworkStatsObservers {
            // thread will update it. We pass a null VPN array because usage is aggregated by uid
            // for this snapshot, so VPN traffic can't be reattributed to responsible apps.
            mRecorder.recordSnapshotLocked(statsContext.mXtSnapshot, statsContext.mActiveIfaces,
                    null /* vpnArray */, statsContext.mCurrentTime);
                    statsContext.mCurrentTime);
        }

        /**
@@ -396,7 +395,7 @@ class NetworkStatsObservers {
            // thread will update it. We pass the VPN info so VPN traffic is reattributed to
            // responsible apps.
            mRecorder.recordSnapshotLocked(statsContext.mUidSnapshot, statsContext.mActiveUidIfaces,
                    statsContext.mVpnArray, statsContext.mCurrentTime);
                    statsContext.mCurrentTime);
        }

        /**
@@ -427,18 +426,16 @@ class NetworkStatsObservers {
        NetworkStats mUidSnapshot;
        ArrayMap<String, NetworkIdentitySet> mActiveIfaces;
        ArrayMap<String, NetworkIdentitySet> mActiveUidIfaces;
        VpnInfo[] mVpnArray;
        long mCurrentTime;

        StatsContext(NetworkStats xtSnapshot, NetworkStats uidSnapshot,
                ArrayMap<String, NetworkIdentitySet> activeIfaces,
                ArrayMap<String, NetworkIdentitySet> activeUidIfaces,
                VpnInfo[] vpnArray, long currentTime) {
                long currentTime) {
            mXtSnapshot = xtSnapshot;
            mUidSnapshot = uidSnapshot;
            mActiveIfaces = activeIfaces;
            mActiveUidIfaces = activeUidIfaces;
            mVpnArray = vpnArray;
            mCurrentTime = currentTime;
        }
    }
+4 −18
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import static android.text.format.DateUtils.YEAR_IN_MILLIS;

import static com.android.internal.util.Preconditions.checkNotNull;

import android.annotation.Nullable;
import android.net.NetworkStats;
import android.net.NetworkStats.NonMonotonicObserver;
import android.net.NetworkStatsHistory;
@@ -37,7 +36,6 @@ import android.util.MathUtils;
import android.util.Slog;
import android.util.proto.ProtoOutputStream;

import com.android.internal.net.VpnInfo;
import com.android.internal.util.FileRotator;
import com.android.internal.util.IndentingPrintWriter;

@@ -202,18 +200,12 @@ public class NetworkStatsRecorder {
    }

    /**
     * Record any delta that occurred since last {@link NetworkStats} snapshot,
     * using the given {@link Map} to identify network interfaces. First
     * snapshot is considered bootstrap, and is not counted as delta.
     *
     * @param vpnArray Optional info about the currently active VPN, if any. This is used to
     *                 redistribute traffic from the VPN app to the underlying responsible apps.
     *                 This should always be set to null if the provided snapshot is aggregated
     *                 across all UIDs (e.g. contains UID_ALL buckets), regardless of VPN state.
     * Record any delta that occurred since last {@link NetworkStats} snapshot, using the given
     * {@link Map} to identify network interfaces. First snapshot is considered bootstrap, and is
     * not counted as delta.
     */
    public void recordSnapshotLocked(NetworkStats snapshot,
            Map<String, NetworkIdentitySet> ifaceIdent, @Nullable VpnInfo[] vpnArray,
            long currentTimeMillis) {
            Map<String, NetworkIdentitySet> ifaceIdent, long currentTimeMillis) {
        final HashSet<String> unknownIfaces = Sets.newHashSet();

        // skip recording when snapshot missing
@@ -232,12 +224,6 @@ public class NetworkStatsRecorder {
        final long end = currentTimeMillis;
        final long start = end - delta.getElapsedRealtime();

        if (vpnArray != null) {
            for (VpnInfo info : vpnArray) {
                delta.migrateTun(info.ownerUid, info.vpnIface, info.underlyingIfaces);
            }
        }

        NetworkStats.Entry entry = null;
        for (int i = 0; i < delta.size(); i++) {
            entry = delta.getValues(i, entry);
+5 −9
Original line number Diff line number Diff line
@@ -1258,28 +1258,24 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        // For xt/dev, we pass a null VPN array because usage is aggregated by UID, so VPN traffic
        // can't be reattributed to responsible apps.
        Trace.traceBegin(TRACE_TAG_NETWORK, "recordDev");
        mDevRecorder.recordSnapshotLocked(
                devSnapshot, mActiveIfaces, null /* vpnArray */, currentTime);
        mDevRecorder.recordSnapshotLocked(devSnapshot, mActiveIfaces, currentTime);
        Trace.traceEnd(TRACE_TAG_NETWORK);
        Trace.traceBegin(TRACE_TAG_NETWORK, "recordXt");
        mXtRecorder.recordSnapshotLocked(
                xtSnapshot, mActiveIfaces, null /* vpnArray */, currentTime);
        mXtRecorder.recordSnapshotLocked(xtSnapshot, mActiveIfaces, currentTime);
        Trace.traceEnd(TRACE_TAG_NETWORK);

        // For per-UID stats, pass the VPN info so VPN traffic is reattributed to responsible apps.
        Trace.traceBegin(TRACE_TAG_NETWORK, "recordUid");
        mUidRecorder.recordSnapshotLocked(
                uidSnapshot, mActiveUidIfaces, null /* vpnArray */, currentTime);
        mUidRecorder.recordSnapshotLocked(uidSnapshot, mActiveUidIfaces, currentTime);
        Trace.traceEnd(TRACE_TAG_NETWORK);
        Trace.traceBegin(TRACE_TAG_NETWORK, "recordUidTag");
        mUidTagRecorder.recordSnapshotLocked(
                uidSnapshot, mActiveUidIfaces, null /* vpnArray */, currentTime);
        mUidTagRecorder.recordSnapshotLocked(uidSnapshot, mActiveUidIfaces, currentTime);
        Trace.traceEnd(TRACE_TAG_NETWORK);

        // We need to make copies of member fields that are sent to the observer to avoid
        // a race condition between the service handler thread and the observer's
        mStatsObservers.updateStats(xtSnapshot, uidSnapshot, new ArrayMap<>(mActiveIfaces),
                new ArrayMap<>(mActiveUidIfaces), null /* vpnArray */, currentTime);
                new ArrayMap<>(mActiveUidIfaces), currentTime);
    }

    /**
+13 −29
Original line number Diff line number Diff line
@@ -52,7 +52,6 @@ import android.util.ArrayMap;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.internal.net.VpnInfo;
import com.android.server.net.NetworkStatsServiceTest.LatchedHandler;
import com.android.testutils.HandlerUtilsKt;

@@ -93,8 +92,6 @@ public class NetworkStatsObserversTest {
    private static final long BASE_BYTES = 7 * MB_IN_BYTES;
    private static final int INVALID_TYPE = -1;

    private static final VpnInfo[] VPN_INFO = new VpnInfo[0];

    private long mElapsedRealtime;

    private HandlerThread mObserverHandlerThread;
@@ -247,8 +244,7 @@ public class NetworkStatsObserversTest {
        NetworkStats uidSnapshot = null;

        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);
        waitForObserverToIdle();
    }

@@ -271,15 +267,13 @@ public class NetworkStatsObserversTest {
                .addIfaceValues(TEST_IFACE, BASE_BYTES, 8L, BASE_BYTES, 16L);
        NetworkStats uidSnapshot = null;
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);

        // Delta
        xtSnapshot = new NetworkStats(TEST_START, 1 /* initialSize */)
                .addIfaceValues(TEST_IFACE, BASE_BYTES + 1024L, 10L, BASE_BYTES + 2048L, 20L);
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);
        waitForObserverToIdle();
    }

@@ -303,16 +297,14 @@ public class NetworkStatsObserversTest {
                .addIfaceValues(TEST_IFACE, BASE_BYTES, 8L, BASE_BYTES, 16L);
        NetworkStats uidSnapshot = null;
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);

        // Delta
        xtSnapshot = new NetworkStats(TEST_START + MINUTE_IN_MILLIS, 1 /* initialSize */)
                .addIfaceValues(TEST_IFACE, BASE_BYTES + THRESHOLD_BYTES, 12L,
                        BASE_BYTES + THRESHOLD_BYTES, 22L);
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);
        waitForObserverToIdle();
        assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.lastMessageType);
    }
@@ -337,8 +329,7 @@ public class NetworkStatsObserversTest {
                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
                        DEFAULT_NETWORK_YES, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);

        // Delta
        uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */)
@@ -346,8 +337,7 @@ public class NetworkStatsObserversTest {
                        DEFAULT_NETWORK_NO, BASE_BYTES + THRESHOLD_BYTES, 2L,
                        BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);
        waitForObserverToIdle();
        assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.lastMessageType);
    }
@@ -372,8 +362,7 @@ public class NetworkStatsObserversTest {
                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
                        DEFAULT_NETWORK_NO, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);

        // Delta
        uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */)
@@ -381,8 +370,7 @@ public class NetworkStatsObserversTest {
                        DEFAULT_NETWORK_NO, BASE_BYTES + THRESHOLD_BYTES, 2L,
                        BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);
        waitForObserverToIdle();
    }

@@ -406,8 +394,7 @@ public class NetworkStatsObserversTest {
                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
                        DEFAULT_NETWORK_YES, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);

        // Delta
        uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */)
@@ -415,8 +402,7 @@ public class NetworkStatsObserversTest {
                        DEFAULT_NETWORK_YES, BASE_BYTES + THRESHOLD_BYTES, 2L,
                        BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);
        waitForObserverToIdle();
        assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.lastMessageType);
    }
@@ -441,8 +427,7 @@ public class NetworkStatsObserversTest {
                .addValues(TEST_IFACE, UID_ANOTHER_USER, SET_DEFAULT, TAG_NONE, METERED_NO,
                        ROAMING_NO, DEFAULT_NETWORK_YES, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);

        // Delta
        uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2 /* initialSize */)
@@ -450,8 +435,7 @@ public class NetworkStatsObserversTest {
                        ROAMING_NO, DEFAULT_NETWORK_NO, BASE_BYTES + THRESHOLD_BYTES, 2L,
                        BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
        mStatsObservers.updateStats(
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces,
                VPN_INFO, TEST_START);
                xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, TEST_START);
        waitForObserverToIdle();
    }