Revert "NetworkStatsService: Fix getDetailedUidStats to take VPNs into account."

This reverts commit 8481d9d55d.

Reason for revert: This change has been implicated in 4-way deadlocks as seen in b/134244752.

Bug: 134244752
Change-Id: I0c00e8f0e30cee987b71b561079a97bf09d4dae4
This commit is contained in:
Benedict Wong
2019-06-06 17:06:48 -07:00
committed by Lorenzo Colitti
parent bebb34732d
commit 75fc9e4e15
3 changed files with 10 additions and 93 deletions

View File

@@ -34,7 +34,6 @@ import libcore.util.EmptyArray;
import java.io.CharArrayWriter; import java.io.CharArrayWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Predicate;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@@ -995,33 +994,23 @@ public class NetworkStats implements Parcelable {
if (limitUid == UID_ALL && limitTag == TAG_ALL && limitIfaces == INTERFACES_ALL) { if (limitUid == UID_ALL && limitTag == TAG_ALL && limitIfaces == INTERFACES_ALL) {
return; return;
} }
filter(e -> (limitUid == UID_ALL || limitUid == e.uid)
&& (limitTag == TAG_ALL || limitTag == e.tag)
&& (limitIfaces == INTERFACES_ALL
|| ArrayUtils.contains(limitIfaces, e.iface)));
}
/**
* Only keep entries with {@link #set} value less than {@link #SET_DEBUG_START}.
*
* <p>This mutates the original structure in place.
*/
public void filterDebugEntries() {
filter(e -> e.set < SET_DEBUG_START);
}
private void filter(Predicate<Entry> predicate) {
Entry entry = new Entry(); Entry entry = new Entry();
int nextOutputEntry = 0; int nextOutputEntry = 0;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
entry = getValues(i, entry); entry = getValues(i, entry);
if (predicate.test(entry)) { final boolean matches =
if (nextOutputEntry != i) { (limitUid == UID_ALL || limitUid == entry.uid)
setValues(nextOutputEntry, entry); && (limitTag == TAG_ALL || limitTag == entry.tag)
} && (limitIfaces == INTERFACES_ALL
|| ArrayUtils.contains(limitIfaces, entry.iface));
if (matches) {
setValues(nextOutputEntry, entry);
nextOutputEntry++; nextOutputEntry++;
} }
} }
size = nextOutputEntry; size = nextOutputEntry;
} }

View File

@@ -263,10 +263,6 @@ public class NetworkStatsFactory {
return stats; return stats;
} }
/**
* @deprecated Use NetworkStatsService#getDetailedUidStats which also accounts for
* VPN traffic
*/
public NetworkStats readNetworkStatsDetail() throws IOException { public NetworkStats readNetworkStatsDetail() throws IOException {
return readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null); return readNetworkStatsDetail(UID_ALL, null, TAG_ALL, null);
} }

View File

@@ -293,22 +293,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
/** Data layer operation counters for splicing into other structures. */ /** Data layer operation counters for splicing into other structures. */
private NetworkStats mUidOperations = new NetworkStats(0L, 10); private NetworkStats mUidOperations = new NetworkStats(0L, 10);
/**
* Snapshot containing most recent network stats for all UIDs across all interfaces and tags
* since boot.
*
* <p>Maintains migrated VPN stats which are result of performing TUN migration on {@link
* #mLastUidDetailSnapshot}.
*/
@GuardedBy("mStatsLock")
private NetworkStats mTunAdjustedStats;
/**
* Used by {@link #mTunAdjustedStats} to migrate VPN traffic over delta between this snapshot
* and latest snapshot.
*/
@GuardedBy("mStatsLock")
private NetworkStats mLastUidDetailSnapshot;
/** Must be set in factory by calling #setHandler. */ /** Must be set in factory by calling #setHandler. */
private Handler mHandler; private Handler mHandler;
private Handler.Callback mHandlerCallback; private Handler.Callback mHandlerCallback;
@@ -828,39 +812,15 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
@Override @Override
public NetworkStats getDetailedUidStats(String[] requiredIfaces) { public NetworkStats getDetailedUidStats(String[] requiredIfaces) {
try { try {
// Get the latest snapshot from NetworkStatsFactory.
// TODO: Querying for INTERFACES_ALL may incur performance penalty. Consider restricting
// this to limited set of ifaces.
NetworkStats uidDetailStats = getNetworkStatsUidDetail(INTERFACES_ALL);
// Migrate traffic from VPN UID over delta and update mTunAdjustedStats.
NetworkStats result;
synchronized (mStatsLock) {
migrateTunTraffic(uidDetailStats, mVpnInfos);
result = mTunAdjustedStats.clone();
}
// Apply filter based on ifacesToQuery.
final String[] ifacesToQuery = final String[] ifacesToQuery =
NetworkStatsFactory.augmentWithStackedInterfaces(requiredIfaces); NetworkStatsFactory.augmentWithStackedInterfaces(requiredIfaces);
result.filter(UID_ALL, ifacesToQuery, TAG_ALL); return getNetworkStatsUidDetail(ifacesToQuery);
return result;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.wtf(TAG, "Error compiling UID stats", e); Log.wtf(TAG, "Error compiling UID stats", e);
return new NetworkStats(0L, 0); return new NetworkStats(0L, 0);
} }
} }
@VisibleForTesting
NetworkStats getTunAdjustedStats() {
synchronized (mStatsLock) {
if (mTunAdjustedStats == null) {
return null;
}
return mTunAdjustedStats.clone();
}
}
@Override @Override
public String[] getMobileIfaces() { public String[] getMobileIfaces() {
return mMobileIfaces; return mMobileIfaces;
@@ -1335,34 +1295,6 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
// a race condition between the service handler thread and the observer's // a race condition between the service handler thread and the observer's
mStatsObservers.updateStats(xtSnapshot, uidSnapshot, new ArrayMap<>(mActiveIfaces), mStatsObservers.updateStats(xtSnapshot, uidSnapshot, new ArrayMap<>(mActiveIfaces),
new ArrayMap<>(mActiveUidIfaces), vpnArray, currentTime); new ArrayMap<>(mActiveUidIfaces), vpnArray, currentTime);
migrateTunTraffic(uidSnapshot, vpnArray);
}
/**
* Updates {@link #mTunAdjustedStats} with the delta containing traffic migrated off of VPNs.
*/
@GuardedBy("mStatsLock")
private void migrateTunTraffic(NetworkStats uidDetailStats, VpnInfo[] vpnInfoArray) {
if (mTunAdjustedStats == null) {
// Either device booted or system server restarted, hence traffic cannot be migrated
// correctly without knowing the past state of VPN's underlying networks.
mTunAdjustedStats = uidDetailStats;
mLastUidDetailSnapshot = uidDetailStats;
return;
}
// Migrate delta traffic from VPN to other apps.
NetworkStats delta = uidDetailStats.subtract(mLastUidDetailSnapshot);
for (VpnInfo info : vpnInfoArray) {
delta.migrateTun(info.ownerUid, info.vpnIface, info.underlyingIfaces);
}
// Filter out debug entries as that may lead to over counting.
delta.filterDebugEntries();
// Update #mTunAdjustedStats with migrated delta.
mTunAdjustedStats.combineAllValues(delta);
mTunAdjustedStats.setElapsedRealtime(uidDetailStats.getElapsedRealtime());
// Update last snapshot.
mLastUidDetailSnapshot = uidDetailStats;
} }
/** /**