[SP01] Add NetworkStats to system API

In order to let external module report their network stats,
expose necessary APIs to construct NetworkStats object.

Test: atest FrameworksNetTests CtsUsageStatsTestCases
Test: m doc-comment-check-docs
Bug: 130855321
Change-Id: Id3ec8aaff3df67948c25eac2319a74cf33a27979
This commit is contained in:
junyulai
2019-11-15 17:15:01 +08:00
committed by Junyu Lai
parent 164c7569c1
commit 55041a4ad2
4 changed files with 194 additions and 45 deletions

View File

@@ -19,6 +19,9 @@ package android.net;
import static android.os.Process.CLAT_UID; import static android.os.Process.CLAT_UID;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.compat.annotation.UnsupportedAppUsage; import android.compat.annotation.UnsupportedAppUsage;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
@@ -48,59 +51,104 @@ import java.util.function.Predicate;
* @hide * @hide
*/ */
// @NotThreadSafe // @NotThreadSafe
public class NetworkStats implements Parcelable { @SystemApi
public final class NetworkStats implements Parcelable {
private static final String TAG = "NetworkStats"; private static final String TAG = "NetworkStats";
/** {@link #iface} value when interface details unavailable. */ /** {@link #iface} value when interface details unavailable. */
public static final String IFACE_ALL = null; @SuppressLint("CompileTimeConstant")
@Nullable public static final String IFACE_ALL = null;
/**
* Virtual network interface for video telephony. This is for VT data usage counting
* purpose.
*/
public static final String IFACE_VT = "vt_data0";
/** {@link #uid} value when UID details unavailable. */ /** {@link #uid} value when UID details unavailable. */
public static final int UID_ALL = -1; public static final int UID_ALL = -1;
/** {@link #tag} value matching any tag. */ /** Special UID value for data usage by tethering. */
public static final int UID_TETHERING = -5;
/**
* {@link #tag} value matching any tag.
* @hide
*/
// TODO: Rename TAG_ALL to TAG_ANY. // TODO: Rename TAG_ALL to TAG_ANY.
public static final int TAG_ALL = -1; public static final int TAG_ALL = -1;
/** {@link #set} value for all sets combined, not including debug sets. */ /**
* {@link #set} value for all sets combined, not including debug sets.
* @hide
*/
public static final int SET_ALL = -1; public static final int SET_ALL = -1;
/** {@link #set} value where background data is accounted. */ /** {@link #set} value where background data is accounted. */
public static final int SET_DEFAULT = 0; public static final int SET_DEFAULT = 0;
/** {@link #set} value where foreground data is accounted. */ /** {@link #set} value where foreground data is accounted. */
public static final int SET_FOREGROUND = 1; public static final int SET_FOREGROUND = 1;
/** All {@link #set} value greater than SET_DEBUG_START are debug {@link #set} values. */ /**
* All {@link #set} value greater than SET_DEBUG_START are debug {@link #set} values.
* @hide
*/
public static final int SET_DEBUG_START = 1000; public static final int SET_DEBUG_START = 1000;
/** Debug {@link #set} value when the VPN stats are moved in. */ /**
* Debug {@link #set} value when the VPN stats are moved in.
* @hide
*/
public static final int SET_DBG_VPN_IN = 1001; public static final int SET_DBG_VPN_IN = 1001;
/** Debug {@link #set} value when the VPN stats are moved out of a vpn UID. */ /**
* Debug {@link #set} value when the VPN stats are moved out of a vpn UID.
* @hide
*/
public static final int SET_DBG_VPN_OUT = 1002; public static final int SET_DBG_VPN_OUT = 1002;
/** Include all interfaces when filtering */ /**
public static final String[] INTERFACES_ALL = null; * Include all interfaces when filtering
* @hide
*/
public @Nullable static final String[] INTERFACES_ALL = null;
/** {@link #tag} value for total data across all tags. */ /** {@link #tag} value for total data across all tags. */
// TODO: Rename TAG_NONE to TAG_ALL. // TODO: Rename TAG_NONE to TAG_ALL.
public static final int TAG_NONE = 0; public static final int TAG_NONE = 0;
/** {@link #metered} value to account for all metered states. */ /**
* {@link #metered} value to account for all metered states.
* @hide
*/
public static final int METERED_ALL = -1; public static final int METERED_ALL = -1;
/** {@link #metered} value where native, unmetered data is accounted. */ /** {@link #metered} value where native, unmetered data is accounted. */
public static final int METERED_NO = 0; public static final int METERED_NO = 0;
/** {@link #metered} value where metered data is accounted. */ /** {@link #metered} value where metered data is accounted. */
public static final int METERED_YES = 1; public static final int METERED_YES = 1;
/** {@link #roaming} value to account for all roaming states. */ /**
* {@link #roaming} value to account for all roaming states.
* @hide
*/
public static final int ROAMING_ALL = -1; public static final int ROAMING_ALL = -1;
/** {@link #roaming} value where native, non-roaming data is accounted. */ /** {@link #roaming} value where native, non-roaming data is accounted. */
public static final int ROAMING_NO = 0; public static final int ROAMING_NO = 0;
/** {@link #roaming} value where roaming data is accounted. */ /** {@link #roaming} value where roaming data is accounted. */
public static final int ROAMING_YES = 1; public static final int ROAMING_YES = 1;
/** {@link #onDefaultNetwork} value to account for all default network states. */ /**
* {@link #onDefaultNetwork} value to account for all default network states.
* @hide
*/
public static final int DEFAULT_NETWORK_ALL = -1; public static final int DEFAULT_NETWORK_ALL = -1;
/** {@link #onDefaultNetwork} value to account for usage while not the default network. */ /** {@link #onDefaultNetwork} value to account for usage while not the default network. */
public static final int DEFAULT_NETWORK_NO = 0; public static final int DEFAULT_NETWORK_NO = 0;
/** {@link #onDefaultNetwork} value to account for usage while the default network. */ /** {@link #onDefaultNetwork} value to account for usage while the default network. */
public static final int DEFAULT_NETWORK_YES = 1; public static final int DEFAULT_NETWORK_YES = 1;
/** Denotes a request for stats at the interface level. */ /**
* Denotes a request for stats at the interface level.
* @hide
*/
public static final int STATS_PER_IFACE = 0; public static final int STATS_PER_IFACE = 0;
/** Denotes a request for stats at the interface and UID level. */ /**
* Denotes a request for stats at the interface and UID level.
* @hide
*/
public static final int STATS_PER_UID = 1; public static final int STATS_PER_UID = 1;
private static final String CLATD_INTERFACE_PREFIX = "v4-"; private static final String CLATD_INTERFACE_PREFIX = "v4-";
@@ -144,60 +192,78 @@ public class NetworkStats implements Parcelable {
@UnsupportedAppUsage @UnsupportedAppUsage
private long[] operations; private long[] operations;
/** @hide */
@SystemApi
public static class Entry { public static class Entry {
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public String iface; public String iface;
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public int uid; public int uid;
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public int set; public int set;
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public int tag; public int tag;
/** /**
* Note that this is only populated w/ the default value when read from /proc or written * Note that this is only populated w/ the default value when read from /proc or written
* to disk. We merge in the correct value when reporting this value to clients of * to disk. We merge in the correct value when reporting this value to clients of
* getSummary(). * getSummary().
* @hide
*/ */
public int metered; public int metered;
/** /**
* Note that this is only populated w/ the default value when read from /proc or written * Note that this is only populated w/ the default value when read from /proc or written
* to disk. We merge in the correct value when reporting this value to clients of * to disk. We merge in the correct value when reporting this value to clients of
* getSummary(). * getSummary().
* @hide
*/ */
public int roaming; public int roaming;
/** /**
* Note that this is only populated w/ the default value when read from /proc or written * Note that this is only populated w/ the default value when read from /proc or written
* to disk. We merge in the correct value when reporting this value to clients of * to disk. We merge in the correct value when reporting this value to clients of
* getSummary(). * getSummary().
* @hide
*/ */
public int defaultNetwork; public int defaultNetwork;
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public long rxBytes; public long rxBytes;
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public long rxPackets; public long rxPackets;
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public long txBytes; public long txBytes;
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public long txPackets; public long txPackets;
/** @hide */
@UnsupportedAppUsage
public long operations; public long operations;
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public Entry() { public Entry() {
this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L); this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
} }
/** @hide */
public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
operations); operations);
} }
/** @hide */
public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets, public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets,
long txBytes, long txPackets, long operations) { long txBytes, long txPackets, long operations) {
this(iface, uid, set, tag, METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, this(iface, uid, set, tag, METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO,
rxBytes, rxPackets, txBytes, txPackets, operations); rxBytes, rxPackets, txBytes, txPackets, operations);
} }
public Entry(String iface, int uid, int set, int tag, int metered, int roaming, public Entry(@Nullable String iface, int uid, int set, int tag, int metered, int roaming,
int defaultNetwork, long rxBytes, long rxPackets, long txBytes, long txPackets, int defaultNetwork, long rxBytes, long rxPackets, long txBytes, long txPackets,
long operations) { long operations) {
this.iface = iface; this.iface = iface;
@@ -214,15 +280,18 @@ public class NetworkStats implements Parcelable {
this.operations = operations; this.operations = operations;
} }
/** @hide */
public boolean isNegative() { public boolean isNegative() {
return rxBytes < 0 || rxPackets < 0 || txBytes < 0 || txPackets < 0 || operations < 0; return rxBytes < 0 || rxPackets < 0 || txBytes < 0 || txPackets < 0 || operations < 0;
} }
/** @hide */
public boolean isEmpty() { public boolean isEmpty() {
return rxBytes == 0 && rxPackets == 0 && txBytes == 0 && txPackets == 0 return rxBytes == 0 && rxPackets == 0 && txBytes == 0 && txPackets == 0
&& operations == 0; && operations == 0;
} }
/** @hide */
public void add(Entry another) { public void add(Entry another) {
this.rxBytes += another.rxBytes; this.rxBytes += another.rxBytes;
this.rxPackets += another.rxPackets; this.rxPackets += another.rxPackets;
@@ -249,6 +318,7 @@ public class NetworkStats implements Parcelable {
return builder.toString(); return builder.toString();
} }
/** @hide */
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (o instanceof Entry) { if (o instanceof Entry) {
@@ -262,13 +332,13 @@ public class NetworkStats implements Parcelable {
return false; return false;
} }
/** @hide */
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(uid, set, tag, metered, roaming, defaultNetwork, iface); return Objects.hash(uid, set, tag, metered, roaming, defaultNetwork, iface);
} }
} }
@UnsupportedAppUsage
public NetworkStats(long elapsedRealtime, int initialSize) { public NetworkStats(long elapsedRealtime, int initialSize) {
this.elapsedRealtime = elapsedRealtime; this.elapsedRealtime = elapsedRealtime;
this.size = 0; this.size = 0;
@@ -292,6 +362,7 @@ public class NetworkStats implements Parcelable {
} }
} }
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public NetworkStats(Parcel parcel) { public NetworkStats(Parcel parcel) {
elapsedRealtime = parcel.readLong(); elapsedRealtime = parcel.readLong();
@@ -312,7 +383,7 @@ public class NetworkStats implements Parcelable {
} }
@Override @Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeLong(elapsedRealtime); dest.writeLong(elapsedRealtime);
dest.writeInt(size); dest.writeInt(size);
dest.writeInt(capacity); dest.writeInt(capacity);
@@ -330,19 +401,23 @@ public class NetworkStats implements Parcelable {
dest.writeLongArray(operations); dest.writeLongArray(operations);
} }
/**
* @hide
*/
@Override @Override
public NetworkStats clone() { public NetworkStats clone() {
final NetworkStats clone = new NetworkStats(elapsedRealtime, size); final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
NetworkStats.Entry entry = null; NetworkStats.Entry entry = null;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
entry = getValues(i, entry); entry = getValues(i, entry);
clone.addValues(entry); clone.addEntry(entry);
} }
return clone; return clone;
} }
/** /**
* Clear all data stored in this object. * Clear all data stored in this object.
* @hide
*/ */
public void clear() { public void clear() {
this.capacity = 0; this.capacity = 0;
@@ -360,25 +435,28 @@ public class NetworkStats implements Parcelable {
this.operations = EmptyArray.LONG; this.operations = EmptyArray.LONG;
} }
/** @hide */
@VisibleForTesting @VisibleForTesting
public NetworkStats addIfaceValues( public NetworkStats addIfaceValues(
String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) { String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
return addValues( return addEntry(
iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L); iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
} }
/** @hide */
@VisibleForTesting @VisibleForTesting
public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes, public NetworkStats addEntry(String iface, int uid, int set, int tag, long rxBytes,
long rxPackets, long txBytes, long txPackets, long operations) { long rxPackets, long txBytes, long txPackets, long operations) {
return addValues(new Entry( return addEntry(new Entry(
iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations)); iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
} }
/** @hide */
@VisibleForTesting @VisibleForTesting
public NetworkStats addValues(String iface, int uid, int set, int tag, int metered, int roaming, public NetworkStats addEntry(String iface, int uid, int set, int tag, int metered, int roaming,
int defaultNetwork, long rxBytes, long rxPackets, long txBytes, long txPackets, int defaultNetwork, long rxBytes, long rxPackets, long txBytes, long txPackets,
long operations) { long operations) {
return addValues(new Entry( return addEntry(new Entry(
iface, uid, set, tag, metered, roaming, defaultNetwork, rxBytes, rxPackets, iface, uid, set, tag, metered, roaming, defaultNetwork, rxBytes, rxPackets,
txBytes, txPackets, operations)); txBytes, txPackets, operations));
} }
@@ -386,8 +464,9 @@ public class NetworkStats implements Parcelable {
/** /**
* Add new stats entry, copying from given {@link Entry}. The {@link Entry} * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
* object can be recycled across multiple calls. * object can be recycled across multiple calls.
* @hide
*/ */
public NetworkStats addValues(Entry entry) { public NetworkStats addEntry(Entry entry) {
if (size >= capacity) { if (size >= capacity) {
final int newLength = Math.max(size, 10) * 3 / 2; final int newLength = Math.max(size, 10) * 3 / 2;
iface = Arrays.copyOf(iface, newLength); iface = Arrays.copyOf(iface, newLength);
@@ -428,6 +507,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return specific stats entry. * Return specific stats entry.
* @hide
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public Entry getValues(int i, Entry recycle) { public Entry getValues(int i, Entry recycle) {
@@ -467,10 +547,12 @@ public class NetworkStats implements Parcelable {
operations[dest] = operations[src]; operations[dest] = operations[src];
} }
/** @hide */
public long getElapsedRealtime() { public long getElapsedRealtime() {
return elapsedRealtime; return elapsedRealtime;
} }
/** @hide */
public void setElapsedRealtime(long time) { public void setElapsedRealtime(long time) {
elapsedRealtime = time; elapsedRealtime = time;
} }
@@ -478,21 +560,25 @@ public class NetworkStats implements Parcelable {
/** /**
* Return age of this {@link NetworkStats} object with respect to * Return age of this {@link NetworkStats} object with respect to
* {@link SystemClock#elapsedRealtime()}. * {@link SystemClock#elapsedRealtime()}.
* @hide
*/ */
public long getElapsedRealtimeAge() { public long getElapsedRealtimeAge() {
return SystemClock.elapsedRealtime() - elapsedRealtime; return SystemClock.elapsedRealtime() - elapsedRealtime;
} }
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public int size() { public int size() {
return size; return size;
} }
/** @hide */
@VisibleForTesting @VisibleForTesting
public int internalSize() { public int internalSize() {
return capacity; return capacity;
} }
/** @hide */
@Deprecated @Deprecated
public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets, public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
long txBytes, long txPackets, long operations) { long txBytes, long txPackets, long operations) {
@@ -501,6 +587,7 @@ public class NetworkStats implements Parcelable {
txPackets, operations); txPackets, operations);
} }
/** @hide */
public NetworkStats combineValues(String iface, int uid, int set, int tag, public NetworkStats combineValues(String iface, int uid, int set, int tag,
long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
return combineValues(new Entry( return combineValues(new Entry(
@@ -509,16 +596,20 @@ public class NetworkStats implements Parcelable {
/** /**
* Combine given values with an existing row, or create a new row if * Combine given values with an existing row, or create a new row if
* {@link #findIndex(String, int, int, int, int)} is unable to find match. Can * {@link #findIndex(String, int, int, int, int, int, int)} is unable to find match. Can
* also be used to subtract values from existing rows. * also be used to subtract values from existing rows. This method mutates the referencing
* {@link NetworkStats} object.
*
* @param entry the {@link Entry} to combine.
* @return a reference to this mutated {@link NetworkStats} object.
* @hide
*/ */
@UnsupportedAppUsage public @NonNull NetworkStats combineValues(@NonNull Entry entry) {
public NetworkStats combineValues(Entry entry) {
final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag, entry.metered, final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag, entry.metered,
entry.roaming, entry.defaultNetwork); entry.roaming, entry.defaultNetwork);
if (i == -1) { if (i == -1) {
// only create new entry when positive contribution // only create new entry when positive contribution
addValues(entry); addEntry(entry);
} else { } else {
rxBytes[i] += entry.rxBytes; rxBytes[i] += entry.rxBytes;
rxPackets[i] += entry.rxPackets; rxPackets[i] += entry.rxPackets;
@@ -530,10 +621,33 @@ public class NetworkStats implements Parcelable {
} }
/** /**
* Combine all values from another {@link NetworkStats} into this object. * Add given values with an existing row, or create a new row if
* {@link #findIndex(String, int, int, int, int, int, int)} is unable to find match. Can
* also be used to subtract values from existing rows.
*
* @param entry the {@link Entry} to add.
* @return a new constructed {@link NetworkStats} object that contains the result.
*/ */
@UnsupportedAppUsage public @NonNull NetworkStats addValues(@NonNull Entry entry) {
public void combineAllValues(NetworkStats another) { return this.clone().combineValues(entry);
}
/**
* Add the given {@link NetworkStats} objects.
*
* @return the sum of two objects.
*/
public @NonNull NetworkStats add(@NonNull NetworkStats another) {
final NetworkStats ret = this.clone();
ret.combineAllValues(another);
return ret;
}
/**
* Combine all values from another {@link NetworkStats} into this object.
* @hide
*/
public void combineAllValues(@NonNull NetworkStats another) {
NetworkStats.Entry entry = null; NetworkStats.Entry entry = null;
for (int i = 0; i < another.size; i++) { for (int i = 0; i < another.size; i++) {
entry = another.getValues(i, entry); entry = another.getValues(i, entry);
@@ -543,6 +657,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Find first stats index that matches the requested parameters. * Find first stats index that matches the requested parameters.
* @hide
*/ */
public int findIndex(String iface, int uid, int set, int tag, int metered, int roaming, public int findIndex(String iface, int uid, int set, int tag, int metered, int roaming,
int defaultNetwork) { int defaultNetwork) {
@@ -560,6 +675,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Find first stats index that matches the requested parameters, starting * Find first stats index that matches the requested parameters, starting
* search around the hinted index as an optimization. * search around the hinted index as an optimization.
* @hide
*/ */
@VisibleForTesting @VisibleForTesting
public int findIndexHinted(String iface, int uid, int set, int tag, int metered, int roaming, public int findIndexHinted(String iface, int uid, int set, int tag, int metered, int roaming,
@@ -589,6 +705,7 @@ public class NetworkStats implements Parcelable {
* Splice in {@link #operations} from the given {@link NetworkStats} based * Splice in {@link #operations} from the given {@link NetworkStats} based
* on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface}, * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
* since operation counts are at data layer. * since operation counts are at data layer.
* @hide
*/ */
public void spliceOperationsFrom(NetworkStats stats) { public void spliceOperationsFrom(NetworkStats stats) {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@@ -604,6 +721,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return list of unique interfaces known by this data structure. * Return list of unique interfaces known by this data structure.
* @hide
*/ */
public String[] getUniqueIfaces() { public String[] getUniqueIfaces() {
final HashSet<String> ifaces = new HashSet<String>(); final HashSet<String> ifaces = new HashSet<String>();
@@ -617,6 +735,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return list of unique UIDs known by this data structure. * Return list of unique UIDs known by this data structure.
* @hide
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public int[] getUniqueUids() { public int[] getUniqueUids() {
@@ -636,6 +755,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return total bytes represented by this snapshot object, usually used when * Return total bytes represented by this snapshot object, usually used when
* checking if a {@link #subtract(NetworkStats)} delta passes a threshold. * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
* @hide
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public long getTotalBytes() { public long getTotalBytes() {
@@ -645,6 +765,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return total of all fields represented by this snapshot object. * Return total of all fields represented by this snapshot object.
* @hide
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public Entry getTotal(Entry recycle) { public Entry getTotal(Entry recycle) {
@@ -654,6 +775,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return total of all fields represented by this snapshot object matching * Return total of all fields represented by this snapshot object matching
* the requested {@link #uid}. * the requested {@link #uid}.
* @hide
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public Entry getTotal(Entry recycle, int limitUid) { public Entry getTotal(Entry recycle, int limitUid) {
@@ -663,11 +785,13 @@ public class NetworkStats implements Parcelable {
/** /**
* Return total of all fields represented by this snapshot object matching * Return total of all fields represented by this snapshot object matching
* the requested {@link #iface}. * the requested {@link #iface}.
* @hide
*/ */
public Entry getTotal(Entry recycle, HashSet<String> limitIface) { public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
return getTotal(recycle, limitIface, UID_ALL, false); return getTotal(recycle, limitIface, UID_ALL, false);
} }
/** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public Entry getTotalIncludingTags(Entry recycle) { public Entry getTotalIncludingTags(Entry recycle) {
return getTotal(recycle, null, UID_ALL, true); return getTotal(recycle, null, UID_ALL, true);
@@ -717,6 +841,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Fast path for battery stats. * Fast path for battery stats.
* @hide
*/ */
public long getTotalPackets() { public long getTotalPackets() {
long total = 0; long total = 0;
@@ -729,9 +854,12 @@ public class NetworkStats implements Parcelable {
/** /**
* Subtract the given {@link NetworkStats}, effectively leaving the delta * Subtract the given {@link NetworkStats}, effectively leaving the delta
* between two snapshots in time. Assumes that statistics rows collect over * between two snapshots in time. Assumes that statistics rows collect over
* time, and that none of them have disappeared. * time, and that none of them have disappeared. This method does not mutate
* the referencing object.
*
* @return the delta between two objects.
*/ */
public NetworkStats subtract(NetworkStats right) { public @NonNull NetworkStats subtract(@NonNull NetworkStats right) {
return subtract(this, right, null, null); return subtract(this, right, null, null);
} }
@@ -742,6 +870,7 @@ public class NetworkStats implements Parcelable {
* <p> * <p>
* If counters have rolled backwards, they are clamped to {@code 0} and * If counters have rolled backwards, they are clamped to {@code 0} and
* reported to the given {@link NonMonotonicObserver}. * reported to the given {@link NonMonotonicObserver}.
* @hide
*/ */
public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right, public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
NonMonotonicObserver<C> observer, C cookie) { NonMonotonicObserver<C> observer, C cookie) {
@@ -759,6 +888,7 @@ public class NetworkStats implements Parcelable {
* If <var>recycle</var> is supplied, this NetworkStats object will be * If <var>recycle</var> is supplied, this NetworkStats object will be
* reused (and returned) as the result if it is large enough to contain * reused (and returned) as the result if it is large enough to contain
* the data. * the data.
* @hide
*/ */
public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right, public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
NonMonotonicObserver<C> observer, C cookie, NetworkStats recycle) { NonMonotonicObserver<C> observer, C cookie, NetworkStats recycle) {
@@ -817,7 +947,7 @@ public class NetworkStats implements Parcelable {
entry.operations = Math.max(entry.operations, 0); entry.operations = Math.max(entry.operations, 0);
} }
result.addValues(entry); result.addEntry(entry);
} }
return result; return result;
@@ -847,6 +977,7 @@ public class NetworkStats implements Parcelable {
* @param stackedTraffic Stats with traffic stacked on top of our ifaces. Will also be mutated. * @param stackedTraffic Stats with traffic stacked on top of our ifaces. Will also be mutated.
* @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both. * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both.
* @param useBpfStats True if eBPF is in use. * @param useBpfStats True if eBPF is in use.
* @hide
*/ */
public static void apply464xlatAdjustments(NetworkStats baseTraffic, public static void apply464xlatAdjustments(NetworkStats baseTraffic,
NetworkStats stackedTraffic, Map<String, String> stackedIfaces, boolean useBpfStats) { NetworkStats stackedTraffic, Map<String, String> stackedIfaces, boolean useBpfStats) {
@@ -899,6 +1030,7 @@ public class NetworkStats implements Parcelable {
* {@link #apply464xlatAdjustments(NetworkStats, NetworkStats, Map)} with {@code this} as * {@link #apply464xlatAdjustments(NetworkStats, NetworkStats, Map)} with {@code this} as
* base and stacked traffic. * base and stacked traffic.
* @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both. * @param stackedIfaces Mapping ipv6if -> ipv4if interface where traffic is counted on both.
* @hide
*/ */
public void apply464xlatAdjustments(Map<String, String> stackedIfaces, boolean useBpfStats) { public void apply464xlatAdjustments(Map<String, String> stackedIfaces, boolean useBpfStats) {
apply464xlatAdjustments(this, this, stackedIfaces, useBpfStats); apply464xlatAdjustments(this, this, stackedIfaces, useBpfStats);
@@ -907,6 +1039,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return total statistics grouped by {@link #iface}; doesn't mutate the * Return total statistics grouped by {@link #iface}; doesn't mutate the
* original structure. * original structure.
* @hide
*/ */
public NetworkStats groupedByIface() { public NetworkStats groupedByIface() {
final NetworkStats stats = new NetworkStats(elapsedRealtime, 10); final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
@@ -938,6 +1071,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return total statistics grouped by {@link #uid}; doesn't mutate the * Return total statistics grouped by {@link #uid}; doesn't mutate the
* original structure. * original structure.
* @hide
*/ */
public NetworkStats groupedByUid() { public NetworkStats groupedByUid() {
final NetworkStats stats = new NetworkStats(elapsedRealtime, 10); final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
@@ -968,6 +1102,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Remove all rows that match one of specified UIDs. * Remove all rows that match one of specified UIDs.
* @hide
*/ */
public void removeUids(int[] uids) { public void removeUids(int[] uids) {
int nextOutputEntry = 0; int nextOutputEntry = 0;
@@ -989,6 +1124,7 @@ public class NetworkStats implements Parcelable {
* @param limitUid UID to filter for, or {@link #UID_ALL}. * @param limitUid UID to filter for, or {@link #UID_ALL}.
* @param limitIfaces Interfaces to filter for, or {@link #INTERFACES_ALL}. * @param limitIfaces Interfaces to filter for, or {@link #INTERFACES_ALL}.
* @param limitTag Tag to filter for, or {@link #TAG_ALL}. * @param limitTag Tag to filter for, or {@link #TAG_ALL}.
* @hide
*/ */
public void filter(int limitUid, String[] limitIfaces, int limitTag) { public void filter(int limitUid, String[] limitIfaces, int limitTag) {
if (limitUid == UID_ALL && limitTag == TAG_ALL && limitIfaces == INTERFACES_ALL) { if (limitUid == UID_ALL && limitTag == TAG_ALL && limitIfaces == INTERFACES_ALL) {
@@ -1004,6 +1140,7 @@ public class NetworkStats implements Parcelable {
* Only keep entries with {@link #set} value less than {@link #SET_DEBUG_START}. * Only keep entries with {@link #set} value less than {@link #SET_DEBUG_START}.
* *
* <p>This mutates the original structure in place. * <p>This mutates the original structure in place.
* @hide
*/ */
public void filterDebugEntries() { public void filterDebugEntries() {
filter(e -> e.set < SET_DEBUG_START); filter(e -> e.set < SET_DEBUG_START);
@@ -1024,6 +1161,7 @@ public class NetworkStats implements Parcelable {
size = nextOutputEntry; size = nextOutputEntry;
} }
/** @hide */
public void dump(String prefix, PrintWriter pw) { public void dump(String prefix, PrintWriter pw) {
pw.print(prefix); pw.print(prefix);
pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime); pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
@@ -1047,6 +1185,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return text description of {@link #set} value. * Return text description of {@link #set} value.
* @hide
*/ */
public static String setToString(int set) { public static String setToString(int set) {
switch (set) { switch (set) {
@@ -1067,6 +1206,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return text description of {@link #set} value. * Return text description of {@link #set} value.
* @hide
*/ */
public static String setToCheckinString(int set) { public static String setToCheckinString(int set) {
switch (set) { switch (set) {
@@ -1087,6 +1227,7 @@ public class NetworkStats implements Parcelable {
/** /**
* @return true if the querySet matches the dataSet. * @return true if the querySet matches the dataSet.
* @hide
*/ */
public static boolean setMatches(int querySet, int dataSet) { public static boolean setMatches(int querySet, int dataSet) {
if (querySet == dataSet) { if (querySet == dataSet) {
@@ -1098,6 +1239,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return text description of {@link #tag} value. * Return text description of {@link #tag} value.
* @hide
*/ */
public static String tagToString(int tag) { public static String tagToString(int tag) {
return "0x" + Integer.toHexString(tag); return "0x" + Integer.toHexString(tag);
@@ -1105,6 +1247,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return text description of {@link #metered} value. * Return text description of {@link #metered} value.
* @hide
*/ */
public static String meteredToString(int metered) { public static String meteredToString(int metered) {
switch (metered) { switch (metered) {
@@ -1121,6 +1264,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return text description of {@link #roaming} value. * Return text description of {@link #roaming} value.
* @hide
*/ */
public static String roamingToString(int roaming) { public static String roamingToString(int roaming) {
switch (roaming) { switch (roaming) {
@@ -1137,6 +1281,7 @@ public class NetworkStats implements Parcelable {
/** /**
* Return text description of {@link #defaultNetwork} value. * Return text description of {@link #defaultNetwork} value.
* @hide
*/ */
public static String defaultNetworkToString(int defaultNetwork) { public static String defaultNetworkToString(int defaultNetwork) {
switch (defaultNetwork) { switch (defaultNetwork) {
@@ -1151,6 +1296,7 @@ public class NetworkStats implements Parcelable {
} }
} }
/** @hide */
@Override @Override
public String toString() { public String toString() {
final CharArrayWriter writer = new CharArrayWriter(); final CharArrayWriter writer = new CharArrayWriter();
@@ -1163,8 +1309,7 @@ public class NetworkStats implements Parcelable {
return 0; return 0;
} }
@UnsupportedAppUsage public static final @NonNull Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
public static final @android.annotation.NonNull Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
@Override @Override
public NetworkStats createFromParcel(Parcel in) { public NetworkStats createFromParcel(Parcel in) {
return new NetworkStats(in); return new NetworkStats(in);
@@ -1176,6 +1321,7 @@ public class NetworkStats implements Parcelable {
} }
}; };
/** @hide */
public interface NonMonotonicObserver<C> { public interface NonMonotonicObserver<C> {
public void foundNonMonotonic( public void foundNonMonotonic(
NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie); NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie);
@@ -1195,6 +1341,7 @@ public class NetworkStats implements Parcelable {
* @param tunUid uid of the VPN application * @param tunUid uid of the VPN application
* @param tunIface iface of the vpn tunnel * @param tunIface iface of the vpn tunnel
* @param underlyingIfaces underlying network ifaces used by the VPN application * @param underlyingIfaces underlying network ifaces used by the VPN application
* @hide
*/ */
public void migrateTun(int tunUid, @NonNull String tunIface, public void migrateTun(int tunUid, @NonNull String tunIface,
@NonNull String[] underlyingIfaces) { @NonNull String[] underlyingIfaces) {

View File

@@ -89,7 +89,7 @@ public class TrafficStats {
* *
* @hide * @hide
*/ */
public static final int UID_TETHERING = -5; public static final int UID_TETHERING = NetworkStats.UID_TETHERING;
/** /**
* Tag values in this range are reserved for the network stack. The network stack is * Tag values in this range are reserved for the network stack. The network stack is

View File

@@ -229,7 +229,7 @@ public class NetworkStatsFactory {
entry.txPackets += reader.nextLong(); entry.txPackets += reader.nextLong();
} }
stats.addValues(entry); stats.addEntry(entry);
reader.finishLine(); reader.finishLine();
} }
} catch (NullPointerException|NumberFormatException e) { } catch (NullPointerException|NumberFormatException e) {
@@ -279,7 +279,7 @@ public class NetworkStatsFactory {
entry.txBytes = reader.nextLong(); entry.txBytes = reader.nextLong();
entry.txPackets = reader.nextLong(); entry.txPackets = reader.nextLong();
stats.addValues(entry); stats.addEntry(entry);
reader.finishLine(); reader.finishLine();
} }
} catch (NullPointerException|NumberFormatException e) { } catch (NullPointerException|NumberFormatException e) {
@@ -439,7 +439,7 @@ public class NetworkStatsFactory {
if ((limitIfaces == null || ArrayUtils.contains(limitIfaces, entry.iface)) if ((limitIfaces == null || ArrayUtils.contains(limitIfaces, entry.iface))
&& (limitUid == UID_ALL || limitUid == entry.uid) && (limitUid == UID_ALL || limitUid == entry.uid)
&& (limitTag == TAG_ALL || limitTag == entry.tag)) { && (limitTag == TAG_ALL || limitTag == entry.tag)) {
stats.addValues(entry); stats.addEntry(entry);
} }
reader.finishLine(); reader.finishLine();

View File

@@ -27,6 +27,7 @@ import static android.net.ConnectivityManager.isNetworkTypeMobile;
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;
import static android.net.NetworkStats.IFACE_ALL; import static android.net.NetworkStats.IFACE_ALL;
import static android.net.NetworkStats.IFACE_VT;
import static android.net.NetworkStats.INTERFACES_ALL; import static android.net.NetworkStats.INTERFACES_ALL;
import static android.net.NetworkStats.METERED_ALL; import static android.net.NetworkStats.METERED_ALL;
import static android.net.NetworkStats.ROAMING_ALL; import static android.net.NetworkStats.ROAMING_ALL;
@@ -211,7 +212,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
/** /**
* Virtual network interface for video telephony. This is for VT data usage counting purpose. * Virtual network interface for video telephony. This is for VT data usage counting purpose.
*/ */
public static final String VT_INTERFACE = "vt_data0"; // TODO: Remove this after no one is using it.
public static final String VT_INTERFACE = NetworkStats.IFACE_VT;
/** /**
* Settings that can be changed externally. * Settings that can be changed externally.
@@ -712,7 +714,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null); final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null);
final NetworkStats stats = new NetworkStats(end - start, 1); final NetworkStats stats = new NetworkStats(end - start, 1);
stats.addValues(new NetworkStats.Entry(IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE, stats.addEntry(new NetworkStats.Entry(IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE,
METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, entry.rxBytes, entry.rxPackets, METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, entry.rxBytes, entry.rxPackets,
entry.txBytes, entry.txPackets, entry.operations)); entry.txBytes, entry.txPackets, entry.operations));
return stats; return stats;
@@ -1179,8 +1181,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
ident.getSubType(), ident.getSubscriberId(), ident.getNetworkId(), ident.getSubType(), ident.getSubscriberId(), ident.getNetworkId(),
ident.getRoaming(), true /* metered */, ident.getRoaming(), true /* metered */,
true /* onDefaultNetwork */); true /* onDefaultNetwork */);
findOrCreateNetworkIdentitySet(mActiveIfaces, VT_INTERFACE).add(vtIdent); findOrCreateNetworkIdentitySet(mActiveIfaces, IFACE_VT).add(vtIdent);
findOrCreateNetworkIdentitySet(mActiveUidIfaces, VT_INTERFACE).add(vtIdent); findOrCreateNetworkIdentitySet(mActiveUidIfaces, IFACE_VT).add(vtIdent);
} }
if (isMobile) { if (isMobile) {