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

Commit 55783ace authored by Junyu Lai's avatar Junyu Lai
Browse files

Move fromPublicNetworkStats to statsd

This is a no-op refactor to reduce complexity to prevent from
accessing trunk stable flags from static libs. Also it
makes more sense since statsd is the only caller.

Test: atest CtsStatsdAtomHostTestCases:android.cts.statsdatom.net.BytesTransferredTest
Test: adb shell cmd stats pull-source 10000
Test: atest FrameworksServicesTests:com.android.server.stats.pull.netstats.NetworkStatsUtilsTest
Bug: 335680025
Change-Id: I1674fcb06df7c90d063f8dc9b2644b465be09feb
parent a35b1f13
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ import static com.android.server.stats.pull.IonMemoryUtil.readSystemIonHeapSizeF
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 libcore.io.IoUtils.closeQuietly;

@@ -203,7 +204,6 @@ import com.android.internal.os.SelectedProcessCpuThreadReader;
import com.android.internal.os.StoragedUidIoStatsReader;
import com.android.internal.util.CollectionUtils;
import com.android.internal.util.FrameworkStatsLog;
import com.android.net.module.util.NetworkStatsUtils;
import com.android.role.RoleManagerLocal;
import com.android.server.BinderCallsStatsService;
import com.android.server.LocalManagerRegistry;
@@ -1522,7 +1522,7 @@ public class StatsPullAtomService extends SystemService {
                        currentTimeInMillis);

        final NetworkStats nonTaggedStats =
                NetworkStatsUtils.fromPublicNetworkStats(queryNonTaggedStats);
                fromPublicNetworkStats(queryNonTaggedStats);
        queryNonTaggedStats.close();
        if (!includeTags) return nonTaggedStats;

@@ -1531,7 +1531,7 @@ public class StatsPullAtomService extends SystemService {
                        currentTimeInMillis - elapsedMillisSinceBoot - bucketDuration,
                        currentTimeInMillis);
        final NetworkStats taggedStats =
                NetworkStatsUtils.fromPublicNetworkStats(queryTaggedStats);
                fromPublicNetworkStats(queryTaggedStats);
        queryTaggedStats.close();
        return nonTaggedStats.add(taggedStats);
    }
+109 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.stats.pull.netstats;

import static android.net.NetworkStats.DEFAULT_NETWORK_ALL;
import static android.net.NetworkStats.METERED_ALL;
import static android.net.NetworkStats.ROAMING_ALL;
import static android.net.NetworkStats.SET_ALL;

import android.app.usage.NetworkStats;

import com.android.internal.annotations.VisibleForTesting;

/**
 * Utility methods for accessing {@link android.net.NetworkStats}.
 */
public class NetworkStatsUtils {

    /**
     * Convert structure from android.app.usage.NetworkStats to android.net.NetworkStats.
     */
    public static android.net.NetworkStats fromPublicNetworkStats(
            NetworkStats publiceNetworkStats) {
        android.net.NetworkStats stats = new android.net.NetworkStats(0L, 0);
        while (publiceNetworkStats.hasNextBucket()) {
            NetworkStats.Bucket bucket = new NetworkStats.Bucket();
            publiceNetworkStats.getNextBucket(bucket);
            final android.net.NetworkStats.Entry entry = fromBucket(bucket);
            stats = stats.addEntry(entry);
        }
        return stats;
    }

    /**
     * Convert structure from android.app.usage.NetworkStats.Bucket
     * to android.net.NetworkStats.Entry.
     */
    @VisibleForTesting
    public static android.net.NetworkStats.Entry fromBucket(NetworkStats.Bucket bucket) {
        return new android.net.NetworkStats.Entry(
                null /* IFACE_ALL */, bucket.getUid(), convertBucketState(bucket.getState()),
                convertBucketTag(bucket.getTag()), convertBucketMetered(bucket.getMetered()),
                convertBucketRoaming(bucket.getRoaming()),
                convertBucketDefaultNetworkStatus(bucket.getDefaultNetworkStatus()),
                bucket.getRxBytes(), bucket.getRxPackets(),
                bucket.getTxBytes(), bucket.getTxPackets(), 0 /* operations */);
    }

    private static int convertBucketState(int networkStatsSet) {
        switch (networkStatsSet) {
            case NetworkStats.Bucket.STATE_ALL: return SET_ALL;
            case NetworkStats.Bucket.STATE_DEFAULT: return android.net.NetworkStats.SET_DEFAULT;
            case NetworkStats.Bucket.STATE_FOREGROUND:
                return android.net.NetworkStats.SET_FOREGROUND;
        }
        return 0;
    }

    private static int convertBucketTag(int tag) {
        switch (tag) {
            case NetworkStats.Bucket.TAG_NONE: return android.net.NetworkStats.TAG_NONE;
        }
        return tag;
    }

    private static int convertBucketMetered(int metered) {
        switch (metered) {
            case NetworkStats.Bucket.METERED_ALL: return METERED_ALL;
            case NetworkStats.Bucket.METERED_NO: return android.net.NetworkStats.METERED_NO;
            case NetworkStats.Bucket.METERED_YES: return android.net.NetworkStats.METERED_YES;
        }
        return 0;
    }

    private static int convertBucketRoaming(int roaming) {
        switch (roaming) {
            case NetworkStats.Bucket.ROAMING_ALL: return ROAMING_ALL;
            case NetworkStats.Bucket.ROAMING_NO: return android.net.NetworkStats.ROAMING_NO;
            case NetworkStats.Bucket.ROAMING_YES: return android.net.NetworkStats.ROAMING_YES;
        }
        return 0;
    }

    private static int convertBucketDefaultNetworkStatus(int defaultNetworkStatus) {
        switch (defaultNetworkStatus) {
            case NetworkStats.Bucket.DEFAULT_NETWORK_ALL:
                return DEFAULT_NETWORK_ALL;
            case NetworkStats.Bucket.DEFAULT_NETWORK_NO:
                return android.net.NetworkStats.DEFAULT_NETWORK_NO;
            case NetworkStats.Bucket.DEFAULT_NETWORK_YES:
                return android.net.NetworkStats.DEFAULT_NETWORK_YES;
        }
        return 0;
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ android_test {
        "securebox",
        "flag-junit",
        "ravenwood-junit",
        "net-tests-utils",
        "net_flags_lib",
        "CtsVirtualDeviceCommonLib",
        "com_android_server_accessibility_flags_lib",
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.stats.pull.netstats

import android.net.NetworkStats
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.testutils.assertEntryEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock

/**
 * Build/Install/Run:
 * atest FrameworksServicesTests:NetworkStatsUtilsTest
 */
@RunWith(AndroidJUnit4::class)
class NetworkStatsUtilsTest {

    @Test
    fun testBucketToEntry() {
        val bucket = makeMockBucket(android.app.usage.NetworkStats.Bucket.UID_ALL,
                android.app.usage.NetworkStats.Bucket.TAG_NONE,
                android.app.usage.NetworkStats.Bucket.STATE_DEFAULT,
                android.app.usage.NetworkStats.Bucket.METERED_YES,
                android.app.usage.NetworkStats.Bucket.ROAMING_NO,
                android.app.usage.NetworkStats.Bucket.DEFAULT_NETWORK_ALL, 1024, 8, 2048, 12)
        val entry = NetworkStatsUtils.fromBucket(bucket)
        val expectedEntry = NetworkStats.Entry(null /* IFACE_ALL */, NetworkStats.UID_ALL,
                NetworkStats.SET_DEFAULT, NetworkStats.TAG_NONE, NetworkStats.METERED_YES,
                NetworkStats.ROAMING_NO, NetworkStats.DEFAULT_NETWORK_ALL, 1024, 8, 2048, 12,
                0 /* operations */)

        assertEntryEquals(expectedEntry, entry)
    }

    private fun makeMockBucket(
            uid: Int,
            tag: Int,
            state: Int,
            metered: Int,
            roaming: Int,
            defaultNetwork: Int,
            rxBytes: Long,
            rxPackets: Long,
            txBytes: Long,
            txPackets: Long
    ): android.app.usage.NetworkStats.Bucket {
        val ret: android.app.usage.NetworkStats.Bucket =
                mock(android.app.usage.NetworkStats.Bucket::class.java)
        doReturn(uid).`when`(ret).getUid()
        doReturn(tag).`when`(ret).getTag()
        doReturn(state).`when`(ret).getState()
        doReturn(metered).`when`(ret).getMetered()
        doReturn(roaming).`when`(ret).getRoaming()
        doReturn(defaultNetwork).`when`(ret).getDefaultNetworkStatus()
        doReturn(rxBytes).`when`(ret).getRxBytes()
        doReturn(rxPackets).`when`(ret).getRxPackets()
        doReturn(txBytes).`when`(ret).getTxBytes()
        doReturn(txPackets).`when`(ret).getTxPackets()
        return ret
    }
}
 No newline at end of file