[SM07] Make combine subtype configurable from Settings

Note that enabling/disabling would not take effect until device
reboot. This will be addressed in follow-up patch.

Test: 1. atest NetworkStatsServieTest SettingsBackupTest
      2. adb shell settings put global netstats_combine_subtype_enabled 1|0
Bug: 146415925

Change-Id: Ic94da540afa479ed18f1b6fbda4ae3216c37476b
Merged-In: Ic94da540afa479ed18f1b6fbda4ae3216c37476b
(cherry picked from commit c4f77ac90bf2e48a655ad19b162fe74a23bf3fb0)
This commit is contained in:
junyulai
2020-01-02 19:35:59 +08:00
committed by Junyu Lai
parent dddf7d0a9a
commit 1e30781571
2 changed files with 45 additions and 35 deletions

View File

@@ -40,13 +40,6 @@ import java.util.Objects;
public class NetworkIdentity implements Comparable<NetworkIdentity> { public class NetworkIdentity implements Comparable<NetworkIdentity> {
private static final String TAG = "NetworkIdentity"; private static final String TAG = "NetworkIdentity";
/**
* When enabled, combine all {@link #mSubType} together under
* {@link #SUBTYPE_COMBINED}.
*/
// TODO: make this flag configurable through settings. See http://b/146415925
public static final boolean COMBINE_SUBTYPE_ENABLED = false;
public static final int SUBTYPE_COMBINED = -1; public static final int SUBTYPE_COMBINED = -1;
final int mType; final int mType;
@@ -61,7 +54,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
int type, int subType, String subscriberId, String networkId, boolean roaming, int type, int subType, String subscriberId, String networkId, boolean roaming,
boolean metered, boolean defaultNetwork) { boolean metered, boolean defaultNetwork) {
mType = type; mType = type;
mSubType = COMBINE_SUBTYPE_ENABLED ? SUBTYPE_COMBINED : subType; mSubType = subType;
mSubscriberId = subscriberId; mSubscriberId = subscriberId;
mNetworkId = networkId; mNetworkId = networkId;
mRoaming = roaming; mRoaming = roaming;
@@ -93,7 +86,7 @@ public class NetworkIdentity implements Comparable<NetworkIdentity> {
final StringBuilder builder = new StringBuilder("{"); final StringBuilder builder = new StringBuilder("{");
builder.append("type=").append(getNetworkTypeName(mType)); builder.append("type=").append(getNetworkTypeName(mType));
builder.append(", subType="); builder.append(", subType=");
if (COMBINE_SUBTYPE_ENABLED) { if (mSubType == SUBTYPE_COMBINED) {
builder.append("COMBINED"); builder.append("COMBINED");
} else { } else {
builder.append(mSubType); builder.append(mSubType);

View File

@@ -27,7 +27,6 @@ import static android.content.Intent.EXTRA_UID;
import static android.content.pm.PackageManager.PERMISSION_GRANTED; import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED;
import static android.net.ConnectivityManager.isNetworkTypeMobile; import static android.net.ConnectivityManager.isNetworkTypeMobile;
import static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED;
import static android.net.NetworkIdentity.SUBTYPE_COMBINED; import static android.net.NetworkIdentity.SUBTYPE_COMBINED;
import static android.net.NetworkStack.checkNetworkStackPermission; import static android.net.NetworkStack.checkNetworkStackPermission;
import static android.net.NetworkStats.DEFAULT_NETWORK_ALL; import static android.net.NetworkStats.DEFAULT_NETWORK_ALL;
@@ -52,6 +51,7 @@ import static android.net.TrafficStats.KB_IN_BYTES;
import static android.net.TrafficStats.MB_IN_BYTES; import static android.net.TrafficStats.MB_IN_BYTES;
import static android.os.Trace.TRACE_TAG_NETWORK; import static android.os.Trace.TRACE_TAG_NETWORK;
import static android.provider.Settings.Global.NETSTATS_AUGMENT_ENABLED; import static android.provider.Settings.Global.NETSTATS_AUGMENT_ENABLED;
import static android.provider.Settings.Global.NETSTATS_COMBINE_SUBTYPE_ENABLED;
import static android.provider.Settings.Global.NETSTATS_DEV_BUCKET_DURATION; import static android.provider.Settings.Global.NETSTATS_DEV_BUCKET_DURATION;
import static android.provider.Settings.Global.NETSTATS_DEV_DELETE_AGE; import static android.provider.Settings.Global.NETSTATS_DEV_DELETE_AGE;
import static android.provider.Settings.Global.NETSTATS_DEV_PERSIST_BYTES; import static android.provider.Settings.Global.NETSTATS_DEV_PERSIST_BYTES;
@@ -240,12 +240,20 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
* Settings that can be changed externally. * Settings that can be changed externally.
*/ */
public interface NetworkStatsSettings { public interface NetworkStatsSettings {
public long getPollInterval(); long getPollInterval();
public long getPollDelay(); long getPollDelay();
public boolean getSampleEnabled(); boolean getSampleEnabled();
public boolean getAugmentEnabled(); boolean getAugmentEnabled();
/**
* When enabled, all mobile data is reported under {@link NetworkIdentity#SUBTYPE_COMBINED}.
* When disabled, mobile data is broken down by a granular subtype representative of the
* actual subtype. {@see NetworkTemplate#getCollapsedRatType}.
* Enabling this decreases the level of detail but saves performance, disk space and
* amount of data logged.
*/
boolean getCombineSubtypeEnabled();
public static class Config { class Config {
public final long bucketDuration; public final long bucketDuration;
public final long rotateAgeMillis; public final long rotateAgeMillis;
public final long deleteAgeMillis; public final long deleteAgeMillis;
@@ -257,16 +265,16 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
} }
public Config getDevConfig(); Config getDevConfig();
public Config getXtConfig(); Config getXtConfig();
public Config getUidConfig(); Config getUidConfig();
public Config getUidTagConfig(); Config getUidTagConfig();
public long getGlobalAlertBytes(long def); long getGlobalAlertBytes(long def);
public long getDevPersistBytes(long def); long getDevPersistBytes(long def);
public long getXtPersistBytes(long def); long getXtPersistBytes(long def);
public long getUidPersistBytes(long def); long getUidPersistBytes(long def);
public long getUidTagPersistBytes(long def); long getUidTagPersistBytes(long def);
} }
private final Object mStatsLock = new Object(); private final Object mStatsLock = new Object();
@@ -509,9 +517,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, currentRealtime, mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, currentRealtime,
mSettings.getPollInterval(), pollIntent); mSettings.getPollInterval(), pollIntent);
// TODO: listen to changes from all subscriptions. // TODO: 1. listen to changes from all subscriptions.
// 2. listen to settings changed to support dynamically enable/disable.
// watch for networkType changes // watch for networkType changes
if (!COMBINE_SUBTYPE_ENABLED) { if (!mSettings.getCombineSubtypeEnabled()) {
mTeleManager.listen(mPhoneListener, LISTEN_SERVICE_STATE); mTeleManager.listen(mPhoneListener, LISTEN_SERVICE_STATE);
} }
@@ -535,9 +544,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
mContext.unregisterReceiver(mUserReceiver); mContext.unregisterReceiver(mUserReceiver);
mContext.unregisterReceiver(mShutdownReceiver); mContext.unregisterReceiver(mShutdownReceiver);
if (!COMBINE_SUBTYPE_ENABLED) { mTeleManager.listen(mPhoneListener, LISTEN_NONE);
mTeleManager.listen(mPhoneListener, LISTEN_NONE);
}
final long currentTime = mClock.millis(); final long currentTime = mClock.millis();
@@ -1265,12 +1272,14 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
mLastNetworkStates = states; mLastNetworkStates = states;
final boolean combineSubtypeEnabled = mSettings.getCombineSubtypeEnabled();
final ArraySet<String> mobileIfaces = new ArraySet<>(); final ArraySet<String> mobileIfaces = new ArraySet<>();
for (NetworkState state : states) { for (NetworkState state : states) {
if (state.networkInfo.isConnected()) { if (state.networkInfo.isConnected()) {
final boolean isMobile = isNetworkTypeMobile(state.networkInfo.getType()); final boolean isMobile = isNetworkTypeMobile(state.networkInfo.getType());
final boolean isDefault = ArrayUtils.contains(mDefaultNetworks, state.network); final boolean isDefault = ArrayUtils.contains(mDefaultNetworks, state.network);
final int subType = getSubTypeForState(state); final int subType = combineSubtypeEnabled ? SUBTYPE_COMBINED
: getSubTypeForState(state);
final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state, final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state,
isDefault, subType); isDefault, subType);
@@ -1334,13 +1343,11 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
} }
/** /**
* If combine subtype is not enabled. For networks with {@code TRANSPORT_CELLULAR}, get * For networks with {@code TRANSPORT_CELLULAR}, get subType that was obtained through
* subType that obtained through {@link PhoneStateListener}. Otherwise, return 0 given that * {@link PhoneStateListener}. Otherwise, return 0 given that other networks with different
* 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 getSubTypeForState(@NonNull NetworkState state) {
if (COMBINE_SUBTYPE_ENABLED) return SUBTYPE_COMBINED;
if (!state.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { if (!state.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return 0; return 0;
} }
@@ -1702,6 +1709,12 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
return; return;
} }
pw.println("Configs:");
pw.increaseIndent();
pw.printPair(NETSTATS_COMBINE_SUBTYPE_ENABLED, mSettings.getCombineSubtypeEnabled());
pw.println();
pw.decreaseIndent();
pw.println("Active interfaces:"); pw.println("Active interfaces:");
pw.increaseIndent(); pw.increaseIndent();
for (int i = 0; i < mActiveIfaces.size(); i++) { for (int i = 0; i < mActiveIfaces.size(); i++) {
@@ -2130,6 +2143,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
return getGlobalBoolean(NETSTATS_AUGMENT_ENABLED, true); return getGlobalBoolean(NETSTATS_AUGMENT_ENABLED, true);
} }
@Override @Override
public boolean getCombineSubtypeEnabled() {
return getGlobalBoolean(NETSTATS_COMBINE_SUBTYPE_ENABLED, false);
}
@Override
public Config getDevConfig() { public Config getDevConfig() {
return new Config(getGlobalLong(NETSTATS_DEV_BUCKET_DURATION, HOUR_IN_MILLIS), return new Config(getGlobalLong(NETSTATS_DEV_BUCKET_DURATION, HOUR_IN_MILLIS),
getGlobalLong(NETSTATS_DEV_ROTATE_AGE, 15 * DAY_IN_MILLIS), getGlobalLong(NETSTATS_DEV_ROTATE_AGE, 15 * DAY_IN_MILLIS),