Merge "[DU03-1]Remove INetworkStatsService from BatteryStatsImpl" am: acd0aef603 am: 9c737b782c am: 761a4eb36c

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1948496

Change-Id: I06e8e68ec621c70698eb464f36a770c361ae4c13
This commit is contained in:
Frank Li
2022-01-21 09:06:50 +00:00
committed by Automerger Merge Worker
6 changed files with 84 additions and 39 deletions

View File

@@ -1856,6 +1856,8 @@ package android.app.usage {
}
public class NetworkStatsManager {
method @NonNull @RequiresPermission(android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) public android.net.NetworkStats getMobileUidStats();
method @NonNull @RequiresPermission(android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) public android.net.NetworkStats getWifiUidStats();
method @NonNull @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_STATS_PROVIDER, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void registerNetworkStatsProvider(@NonNull String, @NonNull android.net.netstats.provider.NetworkStatsProvider);
method @NonNull @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_STATS_PROVIDER, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public void unregisterNetworkStatsProvider(@NonNull android.net.netstats.provider.NetworkStatsProvider);
}

View File

@@ -136,9 +136,7 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
@@ -11873,13 +11871,15 @@ public class BatteryStatsImpl extends BatteryStats {
private NetworkStats mLastModemNetworkStats = new NetworkStats(0, -1);
@VisibleForTesting
protected NetworkStats readNetworkStatsLocked(@NonNull NetworkStatsManager networkStatsManager,
String[] ifaces) {
Objects.requireNonNull(networkStatsManager);
if (!ArrayUtils.isEmpty(ifaces)) {
return networkStatsManager.getDetailedUidStats(Set.of(ifaces));
}
return null;
protected NetworkStats readMobileNetworkStatsLocked(
@NonNull NetworkStatsManager networkStatsManager) {
return networkStatsManager.getMobileUidStats();
}
@VisibleForTesting
protected NetworkStats readWifiNetworkStatsLocked(
@NonNull NetworkStatsManager networkStatsManager) {
return networkStatsManager.getWifiUidStats();
}
/**
@@ -11896,8 +11896,7 @@ public class BatteryStatsImpl extends BatteryStats {
// Grab a separate lock to acquire the network stats, which may do I/O.
NetworkStats delta = null;
synchronized (mWifiNetworkLock) {
final NetworkStats latestStats = readNetworkStatsLocked(networkStatsManager,
mWifiIfaces);
final NetworkStats latestStats = readWifiNetworkStatsLocked(networkStatsManager);
if (latestStats != null) {
delta = NetworkStats.subtract(latestStats, mLastWifiNetworkStats, null, null,
mNetworkStatsPool.acquire());
@@ -12264,8 +12263,7 @@ public class BatteryStatsImpl extends BatteryStats {
// Grab a separate lock to acquire the network stats, which may do I/O.
NetworkStats delta = null;
synchronized (mModemNetworkLock) {
final NetworkStats latestStats = readNetworkStatsLocked(networkStatsManager,
mModemIfaces);
final NetworkStats latestStats = readMobileNetworkStatsLocked(networkStatsManager);
if (latestStats != null) {
delta = NetworkStats.subtract(latestStats, mLastModemNetworkStats, null, null,
mNetworkStatsPool.acquire());

View File

@@ -40,6 +40,7 @@ import java.util.concurrent.Future;
public class MockBatteryStatsImpl extends BatteryStatsImpl {
public BatteryStatsImpl.Clocks clocks;
public boolean mForceOnBattery;
// The mNetworkStats will be used for both wifi and mobile categories
private NetworkStats mNetworkStats;
private DummyExternalStatsSync mExternalStatsSync = new DummyExternalStatsSync();
@@ -108,11 +109,16 @@ public class MockBatteryStatsImpl extends BatteryStatsImpl {
}
@Override
protected NetworkStats readNetworkStatsLocked(@NonNull NetworkStatsManager networkStatsManager,
String[] ifaces) {
protected NetworkStats readMobileNetworkStatsLocked(
@NonNull NetworkStatsManager networkStatsManager) {
return mNetworkStats;
}
@Override
protected NetworkStats readWifiNetworkStatsLocked(
@NonNull NetworkStatsManager networkStatsManager) {
return mNetworkStats;
}
public MockBatteryStatsImpl setPowerProfile(PowerProfile powerProfile) {
mPowerProfile = powerProfile;
return this;

View File

@@ -17,6 +17,8 @@
package android.app.usage;
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
import android.Manifest;
import android.annotation.NonNull;
@@ -55,7 +57,6 @@ import com.android.net.module.util.NetworkIdentityUtils;
import java.util.List;
import java.util.Objects;
import java.util.Set;
/**
* Provides access to network usage history and statistics. Usage data is collected in
@@ -670,26 +671,49 @@ public class NetworkStatsManager {
}
/**
* Query realtime network usage statistics details with interfaces constrains.
* Return snapshot of current UID statistics, including any {@link TrafficStats#UID_TETHERING},
* video calling data usage and count of network operations that set by
* {@link TrafficStats#incrementOperationCount}. The returned data doesn't include any
* statistics that is reported by {@link NetworkStatsProvider}.
* Query realtime mobile network usage statistics.
*
* @param requiredIfaces A list of interfaces the stats should be restricted to, or
* {@link NetworkStats#INTERFACES_ALL}.
* Return a snapshot of current UID network statistics, as it applies
* to the mobile radios of the device. The snapshot will include any
* tethering traffic, video calling data usage and count of
* network operations set by {@link TrafficStats#incrementOperationCount}
* made over a mobile radio.
* The snapshot will not include any statistics that cannot be seen by
* the kernel, e.g. statistics reported by {@link NetworkStatsProvider}s.
*
* @hide
*/
//@SystemApi
@SystemApi
@RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
@NonNull public android.net.NetworkStats getDetailedUidStats(
@NonNull Set<String> requiredIfaces) {
Objects.requireNonNull(requiredIfaces, "requiredIfaces cannot be null");
@NonNull public android.net.NetworkStats getMobileUidStats() {
try {
return mService.getDetailedUidStats(requiredIfaces.toArray(new String[0]));
return mService.getUidStatsForTransport(TRANSPORT_CELLULAR);
} catch (RemoteException e) {
if (DBG) Log.d(TAG, "Remote exception when get detailed uid stats");
if (DBG) Log.d(TAG, "Remote exception when get Mobile uid stats");
throw e.rethrowFromSystemServer();
}
}
/**
* Query realtime Wi-Fi network usage statistics.
*
* Return a snapshot of current UID network statistics, as it applies
* to the Wi-Fi radios of the device. The snapshot will include any
* tethering traffic, video calling data usage and count of
* network operations set by {@link TrafficStats#incrementOperationCount}
* made over a Wi-Fi radio.
* The snapshot will not include any statistics that cannot be seen by
* the kernel, e.g. statistics reported by {@link NetworkStatsProvider}s.
*
* @hide
*/
@SystemApi
@RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK)
@NonNull public android.net.NetworkStats getWifiUidStats() {
try {
return mService.getUidStatsForTransport(TRANSPORT_WIFI);
} catch (RemoteException e) {
if (DBG) Log.d(TAG, "Remote exception when get WiFi uid stats");
throw e.rethrowFromSystemServer();
}
}

View File

@@ -49,14 +49,8 @@ interface INetworkStatsService {
@UnsupportedAppUsage
NetworkStats getDataLayerSnapshotForUid(int uid);
/** Get a detailed snapshot of stats since boot for all UIDs.
*
* <p>Results will not always be limited to stats on requiredIfaces when specified: stats for
* interfaces stacked on the specified interfaces, or for interfaces on which the specified
* interfaces are stacked on, will also be included.
* @param requiredIfaces Interface names to get data for, or {@link NetworkStats#INTERFACES_ALL}.
*/
NetworkStats getDetailedUidStats(in String[] requiredIfaces);
/** Get the transport NetworkStats for all UIDs since boot. */
NetworkStats getUidStatsForTransport(int transport);
/** Return set of any ifaces associated with mobile networks since boot. */
@UnsupportedAppUsage

View File

@@ -28,6 +28,7 @@ import static android.content.Intent.ACTION_UID_REMOVED;
import static android.content.Intent.ACTION_USER_REMOVED;
import static android.content.Intent.EXTRA_UID;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
import static android.net.NetworkStats.DEFAULT_NETWORK_ALL;
import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.IFACE_VT;
@@ -291,6 +292,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
/** Set of any ifaces associated with mobile networks since boot. */
private volatile String[] mMobileIfaces = new String[0];
/** Set of any ifaces associated with wifi networks since boot. */
private volatile String[] mWifiIfaces = new String[0];
/** Set of all ifaces currently used by traffic that does not explicitly specify a Network. */
@GuardedBy("mStatsLock")
private Network[] mDefaultNetworks = new Network[0];
@@ -1005,11 +1009,15 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
}
@Override
public NetworkStats getDetailedUidStats(String[] requiredIfaces) {
public NetworkStats getUidStatsForTransport(int transport) {
enforceAnyPermissionOf(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK);
try {
final String[] relevantIfaces =
transport == TRANSPORT_WIFI ? mWifiIfaces : mMobileIfaces;
// TODO(b/215633405) : mMobileIfaces and mWifiIfaces already contain the stacked
// interfaces, so this is not useful, remove it.
final String[] ifacesToQuery =
mStatsFactory.augmentWithStackedInterfaces(requiredIfaces);
mStatsFactory.augmentWithStackedInterfaces(relevantIfaces);
return getNetworkStatsUidDetail(ifacesToQuery);
} catch (RemoteException e) {
Log.wtf(TAG, "Error compiling UID stats", e);
@@ -1368,10 +1376,12 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
final boolean combineSubtypeEnabled = mSettings.getCombineSubtypeEnabled();
final ArraySet<String> mobileIfaces = new ArraySet<>();
final ArraySet<String> wifiIfaces = new ArraySet<>();
for (NetworkStateSnapshot snapshot : snapshots) {
final int displayTransport =
getDisplayTransport(snapshot.getNetworkCapabilities().getTransportTypes());
final boolean isMobile = (NetworkCapabilities.TRANSPORT_CELLULAR == displayTransport);
final boolean isWifi = (NetworkCapabilities.TRANSPORT_WIFI == displayTransport);
final boolean isDefault = CollectionUtils.contains(
mDefaultNetworks, snapshot.getNetwork());
final int ratType = combineSubtypeEnabled ? NetworkTemplate.NETWORK_TYPE_ALL
@@ -1407,6 +1417,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
if (isMobile) {
mobileIfaces.add(baseIface);
}
if (isWifi) {
wifiIfaces.add(baseIface);
}
}
// Traffic occurring on stacked interfaces is usually clatd.
@@ -1448,6 +1461,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
if (isMobile) {
mobileIfaces.add(iface);
}
if (isWifi) {
wifiIfaces.add(iface);
}
mStatsFactory.noteStackedIface(iface, baseIface);
}
@@ -1455,11 +1471,16 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
}
mMobileIfaces = mobileIfaces.toArray(new String[0]);
mWifiIfaces = wifiIfaces.toArray(new String[0]);
// TODO (b/192758557): Remove debug log.
if (CollectionUtils.contains(mMobileIfaces, null)) {
throw new NullPointerException(
"null element in mMobileIfaces: " + Arrays.toString(mMobileIfaces));
}
if (CollectionUtils.contains(mWifiIfaces, null)) {
throw new NullPointerException(
"null element in mWifiIfaces: " + Arrays.toString(mWifiIfaces));
}
}
private static int getSubIdForMobile(@NonNull NetworkStateSnapshot state) {