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

Commit 6e25f4e8 authored by junyulai's avatar junyulai
Browse files

Remove unused getTetherStats

Before Android R, getTetherStats were used to collect tethering
iface stats of all clients who extends ITetheringStatsProvider,
which typically contains NetdTetheringStatsProvider and
OffloadController.

However, this always returns 0 since Android R, because:
  1. OffloadController implemented NetworkStatsProvider and no
     longer reports stats through getTetherStats.
  2. NetdTetheringStatsProvider always return 0 by design since
     non-offloaded iface tethering stats are already included
     in native iface stats.

This change is a no-op refactoring to remove unused
getTetherStats and update test which is not pratical.
Fix for offloaded tethering stats will be in follow-up CLs.

Test: atest FrameworksNetTests TetheringTests
Bug: 162292214
Change-Id: Icd0717c5c2807ae3bd98626b897e4b148f142815
parent ffcc7427
Loading
Loading
Loading
Loading
+10 −53
Original line number Diff line number Diff line
@@ -1083,13 +1083,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        if (nativeIfaceStats == -1) {
            return nativeIfaceStats;
        } else {
            // When tethering offload is in use, nativeIfaceStats does not contain usage from
            // TODO: When tethering offload is in use, nativeIfaceStats does not contain usage from
            //  offload, add it back here.
            // When tethering offload is not in use, nativeIfaceStats contains tethering usage.
            // this does not cause double-counting of tethering traffic, because
            // NetdTetheringStatsProvider returns zero NetworkStats
            // when called with STATS_PER_IFACE.
            return nativeIfaceStats + getTetherStats(iface, type);
            return nativeIfaceStats;
        }
    }

@@ -1100,42 +1096,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
            return nativeTotalStats;
        } else {
            // Refer to comment in getIfaceStats
            return nativeTotalStats + getTetherStats(IFACE_ALL, type);
        }
    }

    private long getTetherStats(String iface, int type) {
        final NetworkStats tetherSnapshot;
        final long token = Binder.clearCallingIdentity();
        try {
            tetherSnapshot = getNetworkStatsTethering(STATS_PER_IFACE);
        } catch (RemoteException e) {
            Slog.w(TAG, "Error get TetherStats: " + e);
            return 0;
        } finally {
            Binder.restoreCallingIdentity(token);
        }
        HashSet<String> limitIfaces;
        if (iface == IFACE_ALL) {
            limitIfaces = null;
        } else {
            limitIfaces = new HashSet<String>();
            limitIfaces.add(iface);
        }
        NetworkStats.Entry entry = tetherSnapshot.getTotal(null, limitIfaces);
        if (LOGD) Slog.d(TAG, "TetherStats: iface=" + iface + " type=" + type +
                " entry=" + entry);
        switch (type) {
            case 0: // TYPE_RX_BYTES
                return entry.rxBytes;
            case 1: // TYPE_RX_PACKETS
                return entry.rxPackets;
            case 2: // TYPE_TX_BYTES
                return entry.txBytes;
            case 3: // TYPE_TX_PACKETS
                return entry.txPackets;
            default:
                return 0;
            return nativeTotalStats;
        }
    }

@@ -1429,14 +1390,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        final NetworkStats devSnapshot = readNetworkStatsSummaryDev();
        Trace.traceEnd(TRACE_TAG_NETWORK);

        // Tethering snapshot for dev and xt stats. Counts per-interface data from tethering stats
        // providers that isn't already counted by dev and XT stats.
        Trace.traceBegin(TRACE_TAG_NETWORK, "snapshotTether");
        final NetworkStats tetherSnapshot = getNetworkStatsTethering(STATS_PER_IFACE);
        Trace.traceEnd(TRACE_TAG_NETWORK);
        xtSnapshot.combineAllValues(tetherSnapshot);
        devSnapshot.combineAllValues(tetherSnapshot);

        // Snapshot for dev/xt stats from all custom stats providers. Counts per-interface data
        // from stats providers that isn't already counted by dev and XT stats.
        Trace.traceBegin(TRACE_TAG_NETWORK, "snapshotStatsProvider");
@@ -1931,9 +1884,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
    }

    /**
     * Return snapshot of current tethering statistics. Will return empty
     * {@link NetworkStats} if any problems are encountered.
     * Return snapshot of current non-offloaded tethering statistics. Will return empty
     * {@link NetworkStats} if any problems are encountered, or queried by {@code STATS_PER_IFACE}
     * since it is already included by {@link #nativeGetIfaceStat}.
     * See {@code OffloadTetheringStatsProvider} for offloaded tethering stats.
     */
    // TODO: Remove this by implementing {@link NetworkStatsProvider} for non-offloaded
    //  tethering stats.
    private NetworkStats getNetworkStatsTethering(int how) throws RemoteException {
        try {
            return mNetworkManager.getNetworkStatsTethering(how);
+4 −18
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import static android.net.NetworkStats.ROAMING_YES;
import static android.net.NetworkStats.SET_ALL;
import static android.net.NetworkStats.SET_DEFAULT;
import static android.net.NetworkStats.SET_FOREGROUND;
import static android.net.NetworkStats.STATS_PER_IFACE;
import static android.net.NetworkStats.STATS_PER_UID;
import static android.net.NetworkStats.TAG_ALL;
import static android.net.NetworkStats.TAG_NONE;
@@ -1006,20 +1005,18 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {

        // Traffic seen by kernel counters (includes software tethering).
        final NetworkStats ifaceStats = new NetworkStats(getElapsedRealtime(), 1)
                .insertEntry(TEST_IFACE, 1536L, 12L, 384L, 3L);
        // Hardware tethering traffic, not seen by kernel counters.
        final NetworkStats tetherStatsHardware = new NetworkStats(getElapsedRealtime(), 1)
                .insertEntry(TEST_IFACE, 512L, 4L, 128L, 1L);
                .insertEntry(TEST_IFACE, 2048L, 16L, 512L, 4L);
        // TODO: add hardware tethering traffic, not seen by kernel counters.

        // Traffic for UID_RED.
        final NetworkStats uidStats = new NetworkStats(getElapsedRealtime(), 1)
                .insertEntry(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L);
        // All tethering traffic, both hardware and software.
        // All software tethering traffic.
        final NetworkStats tetherStats = new NetworkStats(getElapsedRealtime(), 1)
                .insertEntry(TEST_IFACE, UID_TETHERING, SET_DEFAULT, TAG_NONE, 1920L, 14L, 384L, 2L,
                        0L);

        expectNetworkStatsSummary(ifaceStats, tetherStatsHardware);
        expectNetworkStatsSummary(ifaceStats);
        expectNetworkStatsUidDetail(uidStats, tetherStats);
        forcePollAndWaitForIdle();

@@ -1362,12 +1359,6 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
    }

    private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
        expectNetworkStatsSummary(summary, new NetworkStats(0L, 0));
    }

    private void expectNetworkStatsSummary(NetworkStats summary, NetworkStats tetherStats)
            throws Exception {
        expectNetworkStatsTethering(STATS_PER_IFACE, tetherStats);
        expectNetworkStatsSummaryDev(summary.clone());
        expectNetworkStatsSummaryXt(summary.clone());
    }
@@ -1380,11 +1371,6 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
        when(mStatsFactory.readNetworkStatsSummaryXt()).thenReturn(summary);
    }

    private void expectNetworkStatsTethering(int how, NetworkStats stats)
            throws Exception {
        when(mNetManager.getNetworkStatsTethering(how)).thenReturn(stats);
    }

    private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception {
        expectNetworkStatsUidDetail(detail, new NetworkStats(0L, 0));
    }