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

Commit 418d12dc authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Avoid reading network stats when disabled.

Bug: 5518868
Change-Id: Idb5c588b018bf735597ad7ff904ecc69224dd0a0
parent c2fc52dc
Loading
Loading
Loading
Loading
+25 −11
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.internal.os;
import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.UID_ALL;
import static android.text.format.DateUtils.SECOND_IN_MILLIS;
import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
@@ -35,6 +36,7 @@ import android.os.ParcelFormatException;
import android.os.Parcelable;
import android.os.Process;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.WorkSource;
import android.telephony.ServiceState;
import android.telephony.SignalStrength;
@@ -5713,11 +5715,17 @@ public final class BatteryStatsImpl extends BatteryStats {
        synchronized (this) {
            if (mNetworkSummaryCache == null
                    || mNetworkSummaryCache.getElapsedRealtimeAge() > SECOND_IN_MILLIS) {
                mNetworkSummaryCache = null;

                if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) {
                    try {
                        mNetworkSummaryCache = mNetworkStatsFactory.readNetworkStatsSummary();
                    } catch (IllegalStateException e) {
                    // log problem and return empty object
                        Log.wtf(TAG, "problem reading network stats", e);
                    }
                }

                if (mNetworkSummaryCache == null) {
                    mNetworkSummaryCache = new NetworkStats(SystemClock.elapsedRealtime(), 0);
                }
            }
@@ -5730,12 +5738,18 @@ public final class BatteryStatsImpl extends BatteryStats {
        synchronized (this) {
            if (mNetworkDetailCache == null
                    || mNetworkDetailCache.getElapsedRealtimeAge() > SECOND_IN_MILLIS) {
                mNetworkDetailCache = null;

                if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) {
                    try {
                        mNetworkDetailCache = mNetworkStatsFactory
                                .readNetworkStatsDetail().groupedByUid();
                    } catch (IllegalStateException e) {
                    // log problem and return empty object
                        Log.wtf(TAG, "problem reading network stats", e);
                    }
                }

                if (mNetworkDetailCache == null) {
                    mNetworkDetailCache = new NetworkStats(SystemClock.elapsedRealtime(), 0);
                }
            }
+23 −16
Original line number Diff line number Diff line
@@ -80,16 +80,17 @@ public final class NetworkManagementSocketTagger extends SocketTagger {
    }

    private void tagSocketFd(FileDescriptor fd, int tag, int uid) {
        int errno;
        if (tag == -1 && uid == -1) return;

        errno = native_tagSocketFd(fd, tag, uid);
        if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) {
            final int errno = native_tagSocketFd(fd, tag, uid);
            if (errno < 0) {
                Log.i(TAG, "tagSocketFd(" + fd.getInt$() + ", "
                      + tag + ", " +
                      + uid + ") failed with errno" + errno);
            }
        }
    }

    @Override
    public void untag(FileDescriptor fd) throws SocketException {
@@ -101,14 +102,15 @@ public final class NetworkManagementSocketTagger extends SocketTagger {

    private void unTagSocketFd(FileDescriptor fd) {
        final SocketTags options = threadSocketTags.get();
        int errno;
        if (options.statsTag == -1 && options.statsUid == -1) return;

        errno = native_untagSocketFd(fd);
        if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) {
            final int errno = native_untagSocketFd(fd);
            if (errno < 0) {
                Log.w(TAG, "untagSocket(" + fd.getInt$() + ") failed with errno " + errno);
            }
        }
    }

    public static class SocketTags {
        public int statsTag = -1;
@@ -116,18 +118,23 @@ public final class NetworkManagementSocketTagger extends SocketTagger {
    }

    public static void setKernelCounterSet(int uid, int counterSet) {
        int errno = native_setCounterSet(counterSet, uid);
        if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) {
            final int errno = native_setCounterSet(counterSet, uid);
            if (errno < 0) {
            Log.w(TAG, "setKernelCountSet(" + uid + ", " + counterSet + ") failed with errno " + errno);
                Log.w(TAG, "setKernelCountSet(" + uid + ", " + counterSet + ") failed with errno "
                        + errno);
            }
        }
    }

    public static void resetKernelUidStats(int uid) {
        if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) {
            int errno = native_deleteTagData(0, uid);
            if (errno < 0) {
                Slog.w(TAG, "problem clearing counters for uid " + uid + " : errno " + errno);
            }
        }
    }

    /**
     * Convert {@code /proc/} tag format to {@link Integer}. Assumes incoming
+14 −0
Original line number Diff line number Diff line
@@ -268,6 +268,11 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
    }

    public void systemReady() {
        if (!isBandwidthControlEnabled()) {
            Slog.w(TAG, "bandwidth controls disabled, unable to track stats");
            return;
        }

        synchronized (mStatsLock) {
            // read historical network stats from disk, since policy service
            // might need them right away. we delay loading detailed UID stats
@@ -1644,6 +1649,15 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        return telephony.getSubscriberId();
    }

    private boolean isBandwidthControlEnabled() {
        try {
            return mNetworkManager.isBandwidthControlEnabled();
        } catch (RemoteException e) {
            // ignored; service lives in system_server
            return false;
        }
    }

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