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

Commit e62e2875 authored by Chenbo Feng's avatar Chenbo Feng
Browse files

Use networkStatsService to get detailed network uid stats.

The native implementation of network stats no longer keep a persistent
stats since boot. Everytime networkStatsService read the detail stats,
the data will be cleaned for performance reason. For the service that
are directly calling networkStatsFactory, the results are no longer
accurate anymore. Use the binder interface provided by
networkStatsService instead to get a cumulative detailed uid stats since
device boot.

Bug: 111441138
Test: android.app.usage.cts.NetworkUsageStatsTest
Change-Id: I230c1edbf64cfeb3dbb560db368b5e420f7b79a4
parent a04ae299
Loading
Loading
Loading
Loading
+10 −6
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.content.IntentFilter;
import android.database.ContentObserver;
import android.hardware.usb.UsbManager;
import android.net.ConnectivityManager;
import android.net.INetworkStatsService;
import android.net.NetworkStats;
import android.net.Uri;
import android.net.wifi.WifiActivityEnergyInfo;
@@ -86,7 +87,6 @@ import android.view.Display;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.location.gnssmetrics.GnssMetrics;
import com.android.internal.net.NetworkStatsFactory;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.FastPrintWriter;
import com.android.internal.util.FastXmlSerializer;
@@ -11012,7 +11012,6 @@ public class BatteryStatsImpl extends BatteryStats {
        }
    }
    private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
    private final Pools.Pool<NetworkStats> mNetworkStatsPool = new Pools.SynchronizedPool<>(6);
    private final Object mWifiNetworkLock = new Object();
@@ -11034,11 +11033,16 @@ public class BatteryStatsImpl extends BatteryStats {
    private NetworkStats readNetworkStatsLocked(String[] ifaces) {
        try {
            if (!ArrayUtils.isEmpty(ifaces)) {
                return mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
                        NetworkStats.TAG_NONE, mNetworkStatsPool.acquire());
                INetworkStatsService statsService = INetworkStatsService.Stub.asInterface(
                        ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
                if (statsService != null) {
                    return statsService.getDetailedUidStats(ifaces);
                } else {
                    Slog.e(TAG, "Failed to get networkStatsService ");
                }
        } catch (IOException e) {
            Slog.e(TAG, "failed to read network stats for ifaces: " + Arrays.toString(ifaces));
            }
        } catch (RemoteException e) {
            Slog.e(TAG, "failed to read network stats for ifaces: " + Arrays.toString(ifaces) + e);
        }
        return null;
    }
+28 −21
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.hardware.fingerprint.FingerprintManager;
import android.net.ConnectivityManager;
import android.net.INetworkStatsService;
import android.net.Network;
import android.net.NetworkRequest;
import android.net.NetworkStats;
@@ -90,7 +91,6 @@ import android.util.proto.ProtoOutputStream;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.app.procstats.IProcessStats;
import com.android.internal.app.procstats.ProcessStats;
import com.android.internal.net.NetworkStatsFactory;
import com.android.internal.os.BatterySipper;
import com.android.internal.os.BatteryStatsHelper;
import com.android.internal.os.BinderCallsStats.ExportedCallStat;
@@ -210,6 +210,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {

    private final Context mContext;
    private final AlarmManager mAlarmManager;
    private final INetworkStatsService mNetworkStatsService;
    @GuardedBy("sStatsdLock")
    private static IStatsManager sStatsd;
    private static final Object sStatsdLock = new Object();
@@ -257,6 +258,8 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        super();
        mContext = context;
        mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        mNetworkStatsService = INetworkStatsService.Stub.asInterface(
              ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
        mBaseDir.mkdirs();
        mAppUpdateReceiver = new AppUpdateReceiver();
        mUserUpdateReceiver = new BroadcastReceiver() {
@@ -788,14 +791,14 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            if (ifaces.length == 0) {
                return;
            }
            NetworkStatsFactory nsf = new NetworkStatsFactory();
            if (mNetworkStatsService == null) {
                Slog.e(TAG, "NetworkStats Service is not available!");
                return;
            }
            // Combine all the metrics per Uid into one record.
            NetworkStats stats =
                    nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE,
                            null)
                            .groupedByUid();
            NetworkStats stats = mNetworkStatsService.getDetailedUidStats(ifaces).groupedByUid();
            addNetworkStats(tagId, pulledData, stats, false);
        } catch (java.io.IOException e) {
        } catch (RemoteException e) {
            Slog.e(TAG, "Pulling netstats for wifi bytes has error", e);
        } finally {
            Binder.restoreCallingIdentity(token);
@@ -812,12 +815,14 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            if (ifaces.length == 0) {
                return;
            }
            NetworkStatsFactory nsf = new NetworkStatsFactory();
            if (mNetworkStatsService == null) {
                Slog.e(TAG, "NetworkStats Service is not available!");
                return;
            }
            NetworkStats stats = rollupNetworkStatsByFGBG(
                    nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE,
                            null));
                    mNetworkStatsService.getDetailedUidStats(ifaces));
            addNetworkStats(tagId, pulledData, stats, true);
        } catch (java.io.IOException e) {
        } catch (RemoteException e) {
            Slog.e(TAG, "Pulling netstats for wifi bytes w/ fg/bg has error", e);
        } finally {
            Binder.restoreCallingIdentity(token);
@@ -834,14 +839,14 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            if (ifaces.length == 0) {
                return;
            }
            NetworkStatsFactory nsf = new NetworkStatsFactory();
            if (mNetworkStatsService == null) {
                Slog.e(TAG, "NetworkStats Service is not available!");
                return;
            }
            // Combine all the metrics per Uid into one record.
            NetworkStats stats =
                    nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE,
                            null)
                            .groupedByUid();
            NetworkStats stats = mNetworkStatsService.getDetailedUidStats(ifaces).groupedByUid();
            addNetworkStats(tagId, pulledData, stats, false);
        } catch (java.io.IOException e) {
        } catch (RemoteException e) {
            Slog.e(TAG, "Pulling netstats for mobile bytes has error", e);
        } finally {
            Binder.restoreCallingIdentity(token);
@@ -874,12 +879,14 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            if (ifaces.length == 0) {
                return;
            }
            NetworkStatsFactory nsf = new NetworkStatsFactory();
            if (mNetworkStatsService == null) {
                Slog.e(TAG, "NetworkStats Service is not available!");
                return;
            }
            NetworkStats stats = rollupNetworkStatsByFGBG(
                    nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE,
                            null));
                    mNetworkStatsService.getDetailedUidStats(ifaces));
            addNetworkStats(tagId, pulledData, stats, true);
        } catch (java.io.IOException e) {
        } catch (RemoteException e) {
            Slog.e(TAG, "Pulling netstats for mobile bytes w/ fg/bg has error", e);
        } finally {
            Binder.restoreCallingIdentity(token);