[FUI17] Migrate NetworkStatsService to use NetworkStateSnapshot
This change migrates NetworkStatsService and related code to use NetworkStateSnapshot which is used for replacing the NetworkState. This patch also changes some formating which is suggested by the linter. Test: FrameworksNetTests NetworkPolicyManagerServiceTest Bug: 174123988 Change-Id: I547da8f411cb45bdadc376ac3cadf3f3c55bb282
This commit is contained in:
@@ -19,7 +19,7 @@ package android.net;
|
|||||||
import android.net.DataUsageRequest;
|
import android.net.DataUsageRequest;
|
||||||
import android.net.INetworkStatsSession;
|
import android.net.INetworkStatsSession;
|
||||||
import android.net.Network;
|
import android.net.Network;
|
||||||
import android.net.NetworkState;
|
import android.net.NetworkStateSnapshot;
|
||||||
import android.net.NetworkStats;
|
import android.net.NetworkStats;
|
||||||
import android.net.NetworkStatsHistory;
|
import android.net.NetworkStatsHistory;
|
||||||
import android.net.NetworkTemplate;
|
import android.net.NetworkTemplate;
|
||||||
@@ -68,7 +68,7 @@ interface INetworkStatsService {
|
|||||||
/** Force update of ifaces. */
|
/** Force update of ifaces. */
|
||||||
void forceUpdateIfaces(
|
void forceUpdateIfaces(
|
||||||
in Network[] defaultNetworks,
|
in Network[] defaultNetworks,
|
||||||
in NetworkState[] networkStates,
|
in NetworkStateSnapshot[] snapshots,
|
||||||
in String activeIface,
|
in String activeIface,
|
||||||
in UnderlyingNetworkInfo[] underlyingNetworkInfos);
|
in UnderlyingNetworkInfo[] underlyingNetworkInfos);
|
||||||
/** Force update of statistics. */
|
/** Force update of statistics. */
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ package android.net;
|
|||||||
|
|
||||||
import static android.net.ConnectivityManager.TYPE_WIFI;
|
import static android.net.ConnectivityManager.TYPE_WIFI;
|
||||||
|
|
||||||
import android.annotation.NonNull;
|
|
||||||
import android.annotation.Nullable;
|
import android.annotation.Nullable;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.wifi.WifiInfo;
|
import android.net.wifi.WifiInfo;
|
||||||
@@ -180,29 +179,42 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build a {@link NetworkIdentity} from the given {@link NetworkState} and {@code subType},
|
* Build a {@link NetworkIdentity} from the given {@link NetworkState} and
|
||||||
* assuming that any mobile networks are using the current IMSI. The subType if applicable,
|
* {@code subType}, assuming that any mobile networks are using the current IMSI.
|
||||||
* should be set as one of the TelephonyManager.NETWORK_TYPE_* constants, or
|
* The subType if applicable, should be set as one of the TelephonyManager.NETWORK_TYPE_*
|
||||||
* {@link android.telephony.TelephonyManager#NETWORK_TYPE_UNKNOWN} if not.
|
* constants, or {@link android.telephony.TelephonyManager#NETWORK_TYPE_UNKNOWN} if not.
|
||||||
*/
|
*/
|
||||||
public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state,
|
// TODO: Delete this function after NetworkPolicyManagerService finishes the migration.
|
||||||
boolean defaultNetwork, @NetworkType int subType) {
|
public static NetworkIdentity buildNetworkIdentity(Context context,
|
||||||
final int legacyType = state.legacyNetworkType;
|
NetworkState state, boolean defaultNetwork, @NetworkType int subType) {
|
||||||
|
final NetworkStateSnapshot snapshot = new NetworkStateSnapshot(state.linkProperties,
|
||||||
|
state.networkCapabilities, state.network, state.subscriberId,
|
||||||
|
state.legacyNetworkType);
|
||||||
|
return buildNetworkIdentity(context, snapshot, defaultNetwork, subType);
|
||||||
|
}
|
||||||
|
|
||||||
String subscriberId = null;
|
/**
|
||||||
|
* Build a {@link NetworkIdentity} from the given {@link NetworkStateSnapshot} and
|
||||||
|
* {@code subType}, assuming that any mobile networks are using the current IMSI.
|
||||||
|
* The subType if applicable, should be set as one of the TelephonyManager.NETWORK_TYPE_*
|
||||||
|
* constants, or {@link android.telephony.TelephonyManager#NETWORK_TYPE_UNKNOWN} if not.
|
||||||
|
*/
|
||||||
|
public static NetworkIdentity buildNetworkIdentity(Context context,
|
||||||
|
NetworkStateSnapshot snapshot, boolean defaultNetwork, @NetworkType int subType) {
|
||||||
|
final int legacyType = snapshot.legacyType;
|
||||||
|
|
||||||
|
final String subscriberId = snapshot.subscriberId;
|
||||||
String networkId = null;
|
String networkId = null;
|
||||||
boolean roaming = !state.networkCapabilities.hasCapability(
|
boolean roaming = !snapshot.networkCapabilities.hasCapability(
|
||||||
NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
|
NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
|
||||||
boolean metered = !state.networkCapabilities.hasCapability(
|
boolean metered = !snapshot.networkCapabilities.hasCapability(
|
||||||
NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
|
NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
|
||||||
|
|
||||||
subscriberId = state.subscriberId;
|
final int oemManaged = getOemBitfield(snapshot.networkCapabilities);
|
||||||
|
|
||||||
final int oemManaged = getOemBitfield(state.networkCapabilities);
|
|
||||||
|
|
||||||
if (legacyType == TYPE_WIFI) {
|
if (legacyType == TYPE_WIFI) {
|
||||||
if (state.networkCapabilities.getSsid() != null) {
|
if (snapshot.networkCapabilities.getSsid() != null) {
|
||||||
networkId = state.networkCapabilities.getSsid();
|
networkId = snapshot.networkCapabilities.getSsid();
|
||||||
if (networkId == null) {
|
if (networkId == null) {
|
||||||
// TODO: Figure out if this code path never runs. If so, remove them.
|
// TODO: Figure out if this code path never runs. If so, remove them.
|
||||||
final WifiManager wifi = (WifiManager) context.getSystemService(
|
final WifiManager wifi = (WifiManager) context.getSystemService(
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ import android.net.Network;
|
|||||||
import android.net.NetworkCapabilities;
|
import android.net.NetworkCapabilities;
|
||||||
import android.net.NetworkIdentity;
|
import android.net.NetworkIdentity;
|
||||||
import android.net.NetworkStack;
|
import android.net.NetworkStack;
|
||||||
import android.net.NetworkState;
|
import android.net.NetworkStateSnapshot;
|
||||||
import android.net.NetworkStats;
|
import android.net.NetworkStats;
|
||||||
import android.net.NetworkStats.NonMonotonicObserver;
|
import android.net.NetworkStats.NonMonotonicObserver;
|
||||||
import android.net.NetworkStatsHistory;
|
import android.net.NetworkStatsHistory;
|
||||||
@@ -296,7 +296,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
/** Last states of all networks sent from ConnectivityService. */
|
/** Last states of all networks sent from ConnectivityService. */
|
||||||
@GuardedBy("mStatsLock")
|
@GuardedBy("mStatsLock")
|
||||||
@Nullable
|
@Nullable
|
||||||
private NetworkState[] mLastNetworkStates = null;
|
private NetworkStateSnapshot[] mLastNetworkStateSnapshots = null;
|
||||||
|
|
||||||
private final DropBoxNonMonotonicObserver mNonMonotonicObserver =
|
private final DropBoxNonMonotonicObserver mNonMonotonicObserver =
|
||||||
new DropBoxNonMonotonicObserver();
|
new DropBoxNonMonotonicObserver();
|
||||||
@@ -378,8 +378,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
}
|
}
|
||||||
case MSG_UPDATE_IFACES: {
|
case MSG_UPDATE_IFACES: {
|
||||||
// If no cached states, ignore.
|
// If no cached states, ignore.
|
||||||
if (mLastNetworkStates == null) break;
|
if (mLastNetworkStateSnapshots == null) break;
|
||||||
updateIfaces(mDefaultNetworks, mLastNetworkStates, mActiveIface);
|
updateIfaces(mDefaultNetworks, mLastNetworkStateSnapshots, mActiveIface);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case MSG_PERFORM_POLL_REGISTER_ALERT: {
|
case MSG_PERFORM_POLL_REGISTER_ALERT: {
|
||||||
@@ -967,10 +967,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void forceUpdateIfaces(
|
public void forceUpdateIfaces(
|
||||||
Network[] defaultNetworks,
|
Network[] defaultNetworks,
|
||||||
NetworkState[] networkStates,
|
NetworkStateSnapshot[] networkStates,
|
||||||
String activeIface,
|
String activeIface,
|
||||||
UnderlyingNetworkInfo[] underlyingNetworkInfos) {
|
UnderlyingNetworkInfo[] underlyingNetworkInfos) {
|
||||||
checkNetworkStackPermission(mContext);
|
checkNetworkStackPermission(mContext);
|
||||||
@@ -1248,13 +1247,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
|
|
||||||
private void updateIfaces(
|
private void updateIfaces(
|
||||||
Network[] defaultNetworks,
|
Network[] defaultNetworks,
|
||||||
NetworkState[] networkStates,
|
NetworkStateSnapshot[] snapshots,
|
||||||
String activeIface) {
|
String activeIface) {
|
||||||
synchronized (mStatsLock) {
|
synchronized (mStatsLock) {
|
||||||
mWakeLock.acquire();
|
mWakeLock.acquire();
|
||||||
try {
|
try {
|
||||||
mActiveIface = activeIface;
|
mActiveIface = activeIface;
|
||||||
updateIfacesLocked(defaultNetworks, networkStates);
|
updateIfacesLocked(defaultNetworks, snapshots);
|
||||||
} finally {
|
} finally {
|
||||||
mWakeLock.release();
|
mWakeLock.release();
|
||||||
}
|
}
|
||||||
@@ -1262,13 +1261,13 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inspect all current {@link NetworkState} to derive mapping from {@code iface} to {@link
|
* Inspect all current {@link NetworkStateSnapshot}s to derive mapping from {@code iface} to
|
||||||
* NetworkStatsHistory}. When multiple networks are active on a single {@code iface},
|
* {@link NetworkStatsHistory}. When multiple networks are active on a single {@code iface},
|
||||||
* they are combined under a single {@link NetworkIdentitySet}.
|
* they are combined under a single {@link NetworkIdentitySet}.
|
||||||
*/
|
*/
|
||||||
@GuardedBy("mStatsLock")
|
@GuardedBy("mStatsLock")
|
||||||
private void updateIfacesLocked(@Nullable Network[] defaultNetworks,
|
private void updateIfacesLocked(@Nullable Network[] defaultNetworks,
|
||||||
@NonNull NetworkState[] states) {
|
@NonNull NetworkStateSnapshot[] snapshots) {
|
||||||
if (!mSystemReady) return;
|
if (!mSystemReady) return;
|
||||||
if (LOGV) Slog.v(TAG, "updateIfacesLocked()");
|
if (LOGV) Slog.v(TAG, "updateIfacesLocked()");
|
||||||
|
|
||||||
@@ -1288,21 +1287,21 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
mDefaultNetworks = defaultNetworks;
|
mDefaultNetworks = defaultNetworks;
|
||||||
}
|
}
|
||||||
|
|
||||||
mLastNetworkStates = states;
|
mLastNetworkStateSnapshots = snapshots;
|
||||||
|
|
||||||
final boolean combineSubtypeEnabled = mSettings.getCombineSubtypeEnabled();
|
final boolean combineSubtypeEnabled = mSettings.getCombineSubtypeEnabled();
|
||||||
final ArraySet<String> mobileIfaces = new ArraySet<>();
|
final ArraySet<String> mobileIfaces = new ArraySet<>();
|
||||||
for (NetworkState state : states) {
|
for (NetworkStateSnapshot snapshot : snapshots) {
|
||||||
final boolean isMobile = isNetworkTypeMobile(state.legacyNetworkType);
|
final boolean isMobile = isNetworkTypeMobile(snapshot.legacyType);
|
||||||
final boolean isDefault = ArrayUtils.contains(mDefaultNetworks, state.network);
|
final boolean isDefault = ArrayUtils.contains(mDefaultNetworks, snapshot.network);
|
||||||
final int subType = combineSubtypeEnabled ? SUBTYPE_COMBINED
|
final int subType = combineSubtypeEnabled ? SUBTYPE_COMBINED
|
||||||
: getSubTypeForState(state);
|
: getSubTypeForStateSnapshot(snapshot);
|
||||||
final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state,
|
final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, snapshot,
|
||||||
isDefault, subType);
|
isDefault, subType);
|
||||||
|
|
||||||
// Traffic occurring on the base interface is always counted for
|
// Traffic occurring on the base interface is always counted for
|
||||||
// both total usage and UID details.
|
// both total usage and UID details.
|
||||||
final String baseIface = state.linkProperties.getInterfaceName();
|
final String baseIface = snapshot.linkProperties.getInterfaceName();
|
||||||
if (baseIface != null) {
|
if (baseIface != null) {
|
||||||
findOrCreateNetworkIdentitySet(mActiveIfaces, baseIface).add(ident);
|
findOrCreateNetworkIdentitySet(mActiveIfaces, baseIface).add(ident);
|
||||||
findOrCreateNetworkIdentitySet(mActiveUidIfaces, baseIface).add(ident);
|
findOrCreateNetworkIdentitySet(mActiveUidIfaces, baseIface).add(ident);
|
||||||
@@ -1312,7 +1311,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
// If IMS is metered, then the IMS network usage has already included VT usage.
|
// If IMS is metered, then the IMS network usage has already included VT usage.
|
||||||
// VT is considered always metered in framework's layer. If VT is not metered
|
// VT is considered always metered in framework's layer. If VT is not metered
|
||||||
// per carrier's policy, modem will report 0 usage for VT calls.
|
// per carrier's policy, modem will report 0 usage for VT calls.
|
||||||
if (state.networkCapabilities.hasCapability(
|
if (snapshot.networkCapabilities.hasCapability(
|
||||||
NetworkCapabilities.NET_CAPABILITY_IMS) && !ident.getMetered()) {
|
NetworkCapabilities.NET_CAPABILITY_IMS) && !ident.getMetered()) {
|
||||||
|
|
||||||
// Copy the identify from IMS one but mark it as metered.
|
// Copy the identify from IMS one but mark it as metered.
|
||||||
@@ -1358,7 +1357,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
// (or non eBPF offloaded) TX they would appear on both, however egress interface
|
// (or non eBPF offloaded) TX they would appear on both, however egress interface
|
||||||
// accounting is explicitly bypassed for traffic from the clat uid.
|
// accounting is explicitly bypassed for traffic from the clat uid.
|
||||||
//
|
//
|
||||||
final List<LinkProperties> stackedLinks = state.linkProperties.getStackedLinks();
|
final List<LinkProperties> stackedLinks = snapshot.linkProperties.getStackedLinks();
|
||||||
for (LinkProperties stackedLink : stackedLinks) {
|
for (LinkProperties stackedLink : stackedLinks) {
|
||||||
final String stackedIface = stackedLink.getInterfaceName();
|
final String stackedIface = stackedLink.getInterfaceName();
|
||||||
if (stackedIface != null) {
|
if (stackedIface != null) {
|
||||||
@@ -1381,7 +1380,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
|||||||
* {@link PhoneStateListener}. Otherwise, return 0 given that other networks with different
|
* {@link PhoneStateListener}. Otherwise, return 0 given that other networks with different
|
||||||
* transport types do not actually fill this value.
|
* transport types do not actually fill this value.
|
||||||
*/
|
*/
|
||||||
private int getSubTypeForState(@NonNull NetworkState state) {
|
private int getSubTypeForStateSnapshot(@NonNull NetworkStateSnapshot state) {
|
||||||
if (!state.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
if (!state.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user