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

Commit 8b99e0bd authored by markchien's avatar markchien
Browse files

Replace INetd#trafficSwapActiveStatsMap with ConnectivityManager API

INetd#trafficSwapActiveStatsMap is deprecated and the implementaion is
moved into tethering (connectivity) mainline module. A new
ConnectivityManager API swapActiveStatsMap is temprary exposed for
NetworkStatsFactory. Once NetworkStatsFactory move into tethering
module, swapActiveStatsMap API would be removed and NetworkStatsFactory
should could call the implemeneted inside the module directly. This
should happen before T sdk finalization.

Bug: 209935649
Test: atest NetworkStatsFactoryTest
Change-Id: I6685fe513511e7af9fa6521601891c66d1297da0
parent 614c30b6
Loading
Loading
Loading
Loading
+12 −11
Original line number Diff line number Diff line
@@ -26,10 +26,10 @@ import static com.android.server.NetworkManagementSocketTagger.kernelToTag;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.net.INetd;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkStats;
import android.net.UnderlyingNetworkInfo;
import android.os.RemoteException;
import android.os.StrictMode;
import android.os.SystemClock;

@@ -70,7 +70,7 @@ public class NetworkStatsFactory {

    private final boolean mUseBpfStats;

    private final INetd mNetd;
    private final Context mContext;

    /**
     * Guards persistent data access in this class
@@ -158,12 +158,12 @@ public class NetworkStatsFactory {
        NetworkStats.apply464xlatAdjustments(baseTraffic, stackedTraffic, mStackedIfaces);
    }

    public NetworkStatsFactory(@NonNull INetd netd) {
        this(new File("/proc/"), true, netd);
    public NetworkStatsFactory(@NonNull Context ctx) {
        this(ctx, new File("/proc/"), true);
    }

    @VisibleForTesting
    public NetworkStatsFactory(File procRoot, boolean useBpfStats, @NonNull INetd netd) {
    public NetworkStatsFactory(@NonNull Context ctx, File procRoot, boolean useBpfStats) {
        mStatsXtIfaceAll = new File(procRoot, "net/xt_qtaguid/iface_stat_all");
        mStatsXtIfaceFmt = new File(procRoot, "net/xt_qtaguid/iface_stat_fmt");
        mStatsXtUid = new File(procRoot, "net/xt_qtaguid/stats");
@@ -172,7 +172,7 @@ public class NetworkStatsFactory {
            mPersistSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), -1);
            mTunAnd464xlatAdjustedStats = new NetworkStats(SystemClock.elapsedRealtime(), -1);
        }
        mNetd = netd;
        mContext = ctx;
    }

    public NetworkStats readBpfNetworkStatsDev() throws IOException {
@@ -295,11 +295,12 @@ public class NetworkStatsFactory {
    }

    @GuardedBy("mPersistentDataLock")
    private void requestSwapActiveStatsMapLocked() throws RemoteException {
        // Ask netd to do a active map stats swap. When the binder call successfully returns,
    private void requestSwapActiveStatsMapLocked() {
        // Do a active map stats swap. When the binder call successfully returns,
        // the system server should be able to safely read and clean the inactive map
        // without race problem.
        mNetd.trafficSwapActiveStatsMap();
        final ConnectivityManager cm = mContext.getSystemService(ConnectivityManager.class);
        cm.swapActiveStatsMap();
    }

    /**
@@ -327,7 +328,7 @@ public class NetworkStatsFactory {
                if (mUseBpfStats) {
                    try {
                        requestSwapActiveStatsMapLocked();
                    } catch (RemoteException e) {
                    } catch (RuntimeException e) {
                        throw new IOException(e);
                    }
                    // Stats are always read from the inactive map, so they must be read after the
+12 −3
Original line number Diff line number Diff line
@@ -418,7 +418,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        final NetworkStatsService service = new NetworkStatsService(context,
                INetd.Stub.asInterface((IBinder) context.getSystemService(Context.NETD_SERVICE)),
                alarmManager, wakeLock, getDefaultClock(),
                new DefaultNetworkStatsSettings(), new NetworkStatsFactory(netd),
                new DefaultNetworkStatsSettings(), new NetworkStatsFactory(context),
                new NetworkStatsObservers(), getDefaultSystemDir(), getDefaultBaseDir(),
                new Dependencies());

@@ -987,8 +987,17 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
        }

        // TODO: switch to data layer stats once kernel exports
        // for now, read network layer stats and flatten across all ifaces
        final NetworkStats networkLayer = readNetworkStatsUidDetail(uid, INTERFACES_ALL, TAG_ALL);
        // for now, read network layer stats and flatten across all ifaces.
        // This function is used to query NeworkStats for calle's uid. The only caller method
        // TrafficStats#getDataLayerSnapshotForUid alrady claim no special permission to query
        // its own NetworkStats.
        final long ident = Binder.clearCallingIdentity();
        final NetworkStats networkLayer;
        try {
            networkLayer = readNetworkStatsUidDetail(uid, INTERFACES_ALL, TAG_ALL);
        } finally {
            Binder.restoreCallingIdentity(ident);
        }

        // splice in operation counts
        networkLayer.spliceOperationsFrom(mUidOperations);