blob: 77ce65b0815a58e1c7c78999e86ef0ec9562e30c [file] [log] [blame]
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.os.SystemClock;
Wenchao Tong21377c32015-02-26 18:13:07 -080022import android.util.Slog;
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -070023import android.util.SparseBooleanArray;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070024
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080025import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkeydaa57e82012-09-19 14:10:39 -070026import com.android.internal.util.ArrayUtils;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070027
Jeff Sharkey7a8f1a32014-09-17 09:26:28 -070028import libcore.util.EmptyArray;
29
Jeff Sharkey9a13f362011-04-26 16:25:36 -070030import java.io.CharArrayWriter;
31import java.io.PrintWriter;
Jeff Sharkey4a971222011-06-11 22:16:55 -070032import java.util.Arrays;
Jeff Sharkey75279902011-05-24 18:39:45 -070033import java.util.HashSet;
Kenny Roote6585b32013-12-13 12:00:26 -080034import java.util.Objects;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070035
36/**
Jeff Sharkey75279902011-05-24 18:39:45 -070037 * Collection of active network statistics. Can contain summary details across
38 * all interfaces, or details with per-UID granularity. Internally stores data
39 * as a large table, closely matching {@code /proc/} data format. This structure
40 * optimizes for rapid in-memory comparison, but consider using
41 * {@link NetworkStatsHistory} when persisting.
Jeff Sharkey9a13f362011-04-26 16:25:36 -070042 *
43 * @hide
44 */
45public class NetworkStats implements Parcelable {
Wenchao Tong21377c32015-02-26 18:13:07 -080046 private static final String TAG = "NetworkStats";
Jeff Sharkey75279902011-05-24 18:39:45 -070047 /** {@link #iface} value when interface details unavailable. */
Jeff Sharkey9a13f362011-04-26 16:25:36 -070048 public static final String IFACE_ALL = null;
Jeff Sharkey75279902011-05-24 18:39:45 -070049 /** {@link #uid} value when UID details unavailable. */
50 public static final int UID_ALL = -1;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080051 /** {@link #tag} value matching any tag. */
Antonio Cansado46c753672015-12-10 15:57:56 -080052 // TODO: Rename TAG_ALL to TAG_ANY.
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080053 public static final int TAG_ALL = -1;
Jeff Davidsona6a78072016-01-11 16:02:17 -080054 /** {@link #set} value for all sets combined, not including debug sets. */
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070055 public static final int SET_ALL = -1;
56 /** {@link #set} value where background data is accounted. */
57 public static final int SET_DEFAULT = 0;
58 /** {@link #set} value where foreground data is accounted. */
59 public static final int SET_FOREGROUND = 1;
Wenchao Tong98170b02015-03-17 16:14:23 -070060 /** All {@link #set} value greater than SET_DEBUG_START are debug {@link #set} values. */
61 public static final int SET_DEBUG_START = 1000;
62 /** Debug {@link #set} value when the VPN stats are moved in. */
63 public static final int SET_DBG_VPN_IN = 1001;
64 /** Debug {@link #set} value when the VPN stats are moved out of a vpn UID. */
65 public static final int SET_DBG_VPN_OUT = 1002;
66
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070067 /** {@link #tag} value for total data across all tags. */
Antonio Cansado46c753672015-12-10 15:57:56 -080068 // TODO: Rename TAG_NONE to TAG_ALL.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070069 public static final int TAG_NONE = 0;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070070
Stephen Chenc926b732016-10-21 12:44:26 -070071 /** {@link #metered} value to account for all metered states. */
72 public static final int METERED_ALL = -1;
73 /** {@link #metered} value where native, unmetered data is accounted. */
74 public static final int METERED_NO = 0;
75 /** {@link #metered} value where metered data is accounted. */
76 public static final int METERED_YES = 1;
77
78 /** {@link #roaming} value to account for all roaming states. */
Jeff Davidsona6a78072016-01-11 16:02:17 -080079 public static final int ROAMING_ALL = -1;
Stephen Chenc926b732016-10-21 12:44:26 -070080 /** {@link #roaming} value where native, non-roaming data is accounted. */
Jeff Davidson1f7e05e2016-03-10 13:21:38 -080081 public static final int ROAMING_NO = 0;
Stephen Chenc926b732016-10-21 12:44:26 -070082 /** {@link #roaming} value where roaming data is accounted. */
Jeff Davidson1f7e05e2016-03-10 13:21:38 -080083 public static final int ROAMING_YES = 1;
Jeff Davidsona6a78072016-01-11 16:02:17 -080084
Jeff Sharkey163e6442011-10-31 16:37:52 -070085 // TODO: move fields to "mVariable" notation
86
Jeff Sharkey9a13f362011-04-26 16:25:36 -070087 /**
88 * {@link SystemClock#elapsedRealtime()} timestamp when this data was
89 * generated.
90 */
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080091 private long elapsedRealtime;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070092 private int size;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080093 private int capacity;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070094 private String[] iface;
95 private int[] uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070096 private int[] set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070097 private int[] tag;
Stephen Chenc926b732016-10-21 12:44:26 -070098 private int[] metered;
Jeff Davidsona6a78072016-01-11 16:02:17 -080099 private int[] roaming;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700100 private long[] rxBytes;
101 private long[] rxPackets;
102 private long[] txBytes;
103 private long[] txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700104 private long[] operations;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700105
106 public static class Entry {
107 public String iface;
108 public int uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700109 public int set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700110 public int tag;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800111 /**
112 * Note that this is only populated w/ the default value when read from /proc or written
113 * to disk. We merge in the correct value when reporting this value to clients of
114 * getSummary().
115 */
Stephen Chenc926b732016-10-21 12:44:26 -0700116 public int metered;
117 /**
118 * Note that this is only populated w/ the default value when read from /proc or written
119 * to disk. We merge in the correct value when reporting this value to clients of
120 * getSummary().
121 */
Jeff Davidsona6a78072016-01-11 16:02:17 -0800122 public int roaming;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700123 public long rxBytes;
124 public long rxPackets;
125 public long txBytes;
126 public long txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700127 public long operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700128
129 public Entry() {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700130 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700131 }
132
133 public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700134 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
135 operations);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700136 }
137
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700138 public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets,
139 long txBytes, long txPackets, long operations) {
Stephen Chenc926b732016-10-21 12:44:26 -0700140 this(iface, uid, set, tag, METERED_NO, ROAMING_NO, rxBytes, rxPackets, txBytes,
141 txPackets, operations);
Jeff Davidsona6a78072016-01-11 16:02:17 -0800142 }
143
Stephen Chenc926b732016-10-21 12:44:26 -0700144 public Entry(String iface, int uid, int set, int tag, int metered, int roaming,
145 long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700146 this.iface = iface;
147 this.uid = uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700148 this.set = set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700149 this.tag = tag;
Stephen Chenc926b732016-10-21 12:44:26 -0700150 this.metered = metered;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800151 this.roaming = roaming;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700152 this.rxBytes = rxBytes;
153 this.rxPackets = rxPackets;
154 this.txBytes = txBytes;
155 this.txPackets = txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700156 this.operations = operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700157 }
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700158
Jeff Sharkey63abc372012-01-11 18:38:16 -0800159 public boolean isNegative() {
160 return rxBytes < 0 || rxPackets < 0 || txBytes < 0 || txPackets < 0 || operations < 0;
161 }
162
163 public boolean isEmpty() {
164 return rxBytes == 0 && rxPackets == 0 && txBytes == 0 && txPackets == 0
165 && operations == 0;
166 }
167
Jeff Sharkey70c70532012-05-16 14:51:19 -0700168 public void add(Entry another) {
169 this.rxBytes += another.rxBytes;
170 this.rxPackets += another.rxPackets;
171 this.txBytes += another.txBytes;
172 this.txPackets += another.txPackets;
173 this.operations += another.operations;
174 }
175
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700176 @Override
177 public String toString() {
178 final StringBuilder builder = new StringBuilder();
179 builder.append("iface=").append(iface);
180 builder.append(" uid=").append(uid);
181 builder.append(" set=").append(setToString(set));
182 builder.append(" tag=").append(tagToString(tag));
Stephen Chenc926b732016-10-21 12:44:26 -0700183 builder.append(" metered=").append(meteredToString(metered));
Jeff Davidsona6a78072016-01-11 16:02:17 -0800184 builder.append(" roaming=").append(roamingToString(roaming));
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700185 builder.append(" rxBytes=").append(rxBytes);
186 builder.append(" rxPackets=").append(rxPackets);
187 builder.append(" txBytes=").append(txBytes);
188 builder.append(" txPackets=").append(txPackets);
189 builder.append(" operations=").append(operations);
190 return builder.toString();
191 }
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -0800192
193 @Override
194 public boolean equals(Object o) {
195 if (o instanceof Entry) {
196 final Entry e = (Entry) o;
Stephen Chenc926b732016-10-21 12:44:26 -0700197 return uid == e.uid && set == e.set && tag == e.tag && metered == e.metered
198 && roaming == e.roaming && rxBytes == e.rxBytes && rxPackets == e.rxPackets
199 && txBytes == e.txBytes && txPackets == e.txPackets
200 && operations == e.operations && iface.equals(e.iface);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -0800201 }
202 return false;
203 }
Stephen Chenc926b732016-10-21 12:44:26 -0700204
205 @Override
206 public int hashCode() {
207 return Objects.hash(uid, set, tag, metered, roaming, iface);
208 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700209 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700210
Jeff Sharkey4a971222011-06-11 22:16:55 -0700211 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700212 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700213 this.size = 0;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800214 if (initialSize >= 0) {
215 this.capacity = initialSize;
216 this.iface = new String[initialSize];
217 this.uid = new int[initialSize];
218 this.set = new int[initialSize];
219 this.tag = new int[initialSize];
Stephen Chenc926b732016-10-21 12:44:26 -0700220 this.metered = new int[initialSize];
Jeff Davidsona6a78072016-01-11 16:02:17 -0800221 this.roaming = new int[initialSize];
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800222 this.rxBytes = new long[initialSize];
223 this.rxPackets = new long[initialSize];
224 this.txBytes = new long[initialSize];
225 this.txPackets = new long[initialSize];
226 this.operations = new long[initialSize];
227 } else {
228 // Special case for use by NetworkStatsFactory to start out *really* empty.
229 this.capacity = 0;
Jeff Sharkey7a8f1a32014-09-17 09:26:28 -0700230 this.iface = EmptyArray.STRING;
231 this.uid = EmptyArray.INT;
232 this.set = EmptyArray.INT;
233 this.tag = EmptyArray.INT;
Stephen Chenc926b732016-10-21 12:44:26 -0700234 this.metered = EmptyArray.INT;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800235 this.roaming = EmptyArray.INT;
Jeff Sharkey7a8f1a32014-09-17 09:26:28 -0700236 this.rxBytes = EmptyArray.LONG;
237 this.rxPackets = EmptyArray.LONG;
238 this.txBytes = EmptyArray.LONG;
239 this.txPackets = EmptyArray.LONG;
240 this.operations = EmptyArray.LONG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800241 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700242 }
243
244 public NetworkStats(Parcel parcel) {
245 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700246 size = parcel.readInt();
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800247 capacity = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700248 iface = parcel.createStringArray();
249 uid = parcel.createIntArray();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700250 set = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700251 tag = parcel.createIntArray();
Stephen Chenc926b732016-10-21 12:44:26 -0700252 metered = parcel.createIntArray();
Jeff Davidsona6a78072016-01-11 16:02:17 -0800253 roaming = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700254 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700255 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700256 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700257 txPackets = parcel.createLongArray();
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700258 operations = parcel.createLongArray();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700259 }
260
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700261 @Override
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700262 public void writeToParcel(Parcel dest, int flags) {
263 dest.writeLong(elapsedRealtime);
264 dest.writeInt(size);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800265 dest.writeInt(capacity);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700266 dest.writeStringArray(iface);
267 dest.writeIntArray(uid);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700268 dest.writeIntArray(set);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700269 dest.writeIntArray(tag);
Stephen Chenc926b732016-10-21 12:44:26 -0700270 dest.writeIntArray(metered);
Jeff Davidsona6a78072016-01-11 16:02:17 -0800271 dest.writeIntArray(roaming);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700272 dest.writeLongArray(rxBytes);
273 dest.writeLongArray(rxPackets);
274 dest.writeLongArray(txBytes);
275 dest.writeLongArray(txPackets);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700276 dest.writeLongArray(operations);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700277 }
278
Jeff Sharkey4abb1b82011-11-08 17:35:28 -0800279 @Override
280 public NetworkStats clone() {
281 final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
282 NetworkStats.Entry entry = null;
283 for (int i = 0; i < size; i++) {
284 entry = getValues(i, entry);
285 clone.addValues(entry);
286 }
287 return clone;
288 }
289
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800290 @VisibleForTesting
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700291 public NetworkStats addIfaceValues(
292 String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
293 return addValues(
294 iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700295 }
296
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800297 @VisibleForTesting
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700298 public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
299 long rxPackets, long txBytes, long txPackets, long operations) {
300 return addValues(new Entry(
301 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700302 }
303
Jeff Davidsona6a78072016-01-11 16:02:17 -0800304 @VisibleForTesting
Stephen Chenc926b732016-10-21 12:44:26 -0700305 public NetworkStats addValues(String iface, int uid, int set, int tag, int metered, int roaming,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800306 long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
307 return addValues(new Entry(
Stephen Chenc926b732016-10-21 12:44:26 -0700308 iface, uid, set, tag, metered, roaming, rxBytes, rxPackets, txBytes, txPackets,
309 operations));
Jeff Davidsona6a78072016-01-11 16:02:17 -0800310 }
311
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700312 /**
313 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
314 * object can be recycled across multiple calls.
315 */
316 public NetworkStats addValues(Entry entry) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800317 if (size >= capacity) {
318 final int newLength = Math.max(size, 10) * 3 / 2;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700319 iface = Arrays.copyOf(iface, newLength);
320 uid = Arrays.copyOf(uid, newLength);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700321 set = Arrays.copyOf(set, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700322 tag = Arrays.copyOf(tag, newLength);
Stephen Chenc926b732016-10-21 12:44:26 -0700323 metered = Arrays.copyOf(metered, newLength);
Jeff Davidsona6a78072016-01-11 16:02:17 -0800324 roaming = Arrays.copyOf(roaming, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700325 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700326 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700327 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700328 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700329 operations = Arrays.copyOf(operations, newLength);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800330 capacity = newLength;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700331 }
332
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700333 iface[size] = entry.iface;
334 uid[size] = entry.uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700335 set[size] = entry.set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700336 tag[size] = entry.tag;
Stephen Chenc926b732016-10-21 12:44:26 -0700337 metered[size] = entry.metered;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800338 roaming[size] = entry.roaming;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700339 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700340 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700341 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700342 txPackets[size] = entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700343 operations[size] = entry.operations;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700344 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700345
Jeff Sharkey4a971222011-06-11 22:16:55 -0700346 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700347 }
348
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700349 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700350 * Return specific stats entry.
351 */
352 public Entry getValues(int i, Entry recycle) {
353 final Entry entry = recycle != null ? recycle : new Entry();
354 entry.iface = iface[i];
355 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700356 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700357 entry.tag = tag[i];
Stephen Chenc926b732016-10-21 12:44:26 -0700358 entry.metered = metered[i];
Jeff Davidsona6a78072016-01-11 16:02:17 -0800359 entry.roaming = roaming[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700360 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700361 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700362 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700363 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700364 entry.operations = operations[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700365 return entry;
366 }
367
368 public long getElapsedRealtime() {
369 return elapsedRealtime;
370 }
371
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800372 public void setElapsedRealtime(long time) {
373 elapsedRealtime = time;
374 }
375
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700376 /**
377 * Return age of this {@link NetworkStats} object with respect to
378 * {@link SystemClock#elapsedRealtime()}.
379 */
380 public long getElapsedRealtimeAge() {
381 return SystemClock.elapsedRealtime() - elapsedRealtime;
382 }
383
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700384 public int size() {
385 return size;
386 }
387
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800388 @VisibleForTesting
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700389 public int internalSize() {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800390 return capacity;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700391 }
392
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700393 @Deprecated
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700394 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700395 long txBytes, long txPackets, long operations) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700396 return combineValues(
Jeff Davidsona6a78072016-01-11 16:02:17 -0800397 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes,
398 txPackets, operations);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700399 }
400
Jeff Davidsona6a78072016-01-11 16:02:17 -0800401 public NetworkStats combineValues(String iface, int uid, int set, int tag,
402 long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700403 return combineValues(new Entry(
404 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700405 }
406
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700407 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700408 * Combine given values with an existing row, or create a new row if
Jeff Davidsona6a78072016-01-11 16:02:17 -0800409 * {@link #findIndex(String, int, int, int, int)} is unable to find match. Can
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700410 * also be used to subtract values from existing rows.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700411 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700412 public NetworkStats combineValues(Entry entry) {
Stephen Chenc926b732016-10-21 12:44:26 -0700413 final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag, entry.metered,
414 entry.roaming);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700415 if (i == -1) {
416 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700417 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700418 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700419 rxBytes[i] += entry.rxBytes;
420 rxPackets[i] += entry.rxPackets;
421 txBytes[i] += entry.txBytes;
422 txPackets[i] += entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700423 operations[i] += entry.operations;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700424 }
425 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700426 }
427
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700428 /**
Jeff Sharkey905b5892011-09-30 15:19:49 -0700429 * Combine all values from another {@link NetworkStats} into this object.
430 */
431 public void combineAllValues(NetworkStats another) {
432 NetworkStats.Entry entry = null;
433 for (int i = 0; i < another.size; i++) {
434 entry = another.getValues(i, entry);
435 combineValues(entry);
436 }
437 }
438
439 /**
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700440 * Find first stats index that matches the requested parameters.
441 */
Stephen Chenc926b732016-10-21 12:44:26 -0700442 public int findIndex(String iface, int uid, int set, int tag, int metered, int roaming) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700443 for (int i = 0; i < size; i++) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700444 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
Stephen Chenc926b732016-10-21 12:44:26 -0700445 && metered == this.metered[i] && roaming == this.roaming[i]
446 && Objects.equals(iface, this.iface[i])) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700447 return i;
448 }
449 }
450 return -1;
451 }
452
453 /**
454 * Find first stats index that matches the requested parameters, starting
455 * search around the hinted index as an optimization.
456 */
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800457 @VisibleForTesting
Stephen Chenc926b732016-10-21 12:44:26 -0700458 public int findIndexHinted(String iface, int uid, int set, int tag, int metered, int roaming,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800459 int hintIndex) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700460 for (int offset = 0; offset < size; offset++) {
461 final int halfOffset = offset / 2;
462
463 // search outwards from hint index, alternating forward and backward
464 final int i;
465 if (offset % 2 == 0) {
466 i = (hintIndex + halfOffset) % size;
467 } else {
468 i = (size + hintIndex - halfOffset - 1) % size;
469 }
470
471 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
Stephen Chenc926b732016-10-21 12:44:26 -0700472 && metered == this.metered[i] && roaming == this.roaming[i]
473 && Objects.equals(iface, this.iface[i])) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700474 return i;
475 }
476 }
477 return -1;
478 }
479
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700480 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700481 * Splice in {@link #operations} from the given {@link NetworkStats} based
482 * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
483 * since operation counts are at data layer.
484 */
485 public void spliceOperationsFrom(NetworkStats stats) {
486 for (int i = 0; i < size; i++) {
Stephen Chenc926b732016-10-21 12:44:26 -0700487 final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i], metered[i], roaming[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700488 if (j == -1) {
489 operations[i] = 0;
490 } else {
491 operations[i] = stats.operations[j];
492 }
493 }
494 }
495
496 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700497 * Return list of unique interfaces known by this data structure.
498 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700499 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700500 final HashSet<String> ifaces = new HashSet<String>();
501 for (String iface : this.iface) {
502 if (iface != IFACE_ALL) {
503 ifaces.add(iface);
504 }
505 }
506 return ifaces.toArray(new String[ifaces.size()]);
507 }
508
509 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700510 * Return list of unique UIDs known by this data structure.
511 */
512 public int[] getUniqueUids() {
513 final SparseBooleanArray uids = new SparseBooleanArray();
514 for (int uid : this.uid) {
515 uids.put(uid, true);
516 }
517
518 final int size = uids.size();
519 final int[] result = new int[size];
520 for (int i = 0; i < size; i++) {
521 result[i] = uids.keyAt(i);
522 }
523 return result;
524 }
525
526 /**
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700527 * Return total bytes represented by this snapshot object, usually used when
528 * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
529 */
530 public long getTotalBytes() {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700531 final Entry entry = getTotal(null);
532 return entry.rxBytes + entry.txBytes;
533 }
534
535 /**
536 * Return total of all fields represented by this snapshot object.
537 */
538 public Entry getTotal(Entry recycle) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800539 return getTotal(recycle, null, UID_ALL, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700540 }
541
542 /**
543 * Return total of all fields represented by this snapshot object matching
544 * the requested {@link #uid}.
545 */
546 public Entry getTotal(Entry recycle, int limitUid) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800547 return getTotal(recycle, null, limitUid, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700548 }
549
550 /**
551 * Return total of all fields represented by this snapshot object matching
552 * the requested {@link #iface}.
553 */
554 public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800555 return getTotal(recycle, limitIface, UID_ALL, false);
556 }
557
558 public Entry getTotalIncludingTags(Entry recycle) {
559 return getTotal(recycle, null, UID_ALL, true);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700560 }
561
562 /**
563 * Return total of all fields represented by this snapshot object matching
564 * the requested {@link #iface} and {@link #uid}.
565 *
566 * @param limitIface Set of {@link #iface} to include in total; or {@code
567 * null} to include all ifaces.
568 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800569 private Entry getTotal(
570 Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags) {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700571 final Entry entry = recycle != null ? recycle : new Entry();
572
573 entry.iface = IFACE_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700574 entry.uid = limitUid;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700575 entry.set = SET_ALL;
576 entry.tag = TAG_NONE;
Stephen Chenc926b732016-10-21 12:44:26 -0700577 entry.metered = METERED_ALL;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800578 entry.roaming = ROAMING_ALL;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700579 entry.rxBytes = 0;
580 entry.rxPackets = 0;
581 entry.txBytes = 0;
582 entry.txPackets = 0;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700583 entry.operations = 0;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700584
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700585 for (int i = 0; i < size; i++) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700586 final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]);
587 final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i]));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700588
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700589 if (matchesUid && matchesIface) {
590 // skip specific tags, since already counted in TAG_NONE
Jeff Sharkey63abc372012-01-11 18:38:16 -0800591 if (tag[i] != TAG_NONE && !includeTags) continue;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700592
593 entry.rxBytes += rxBytes[i];
594 entry.rxPackets += rxPackets[i];
595 entry.txBytes += txBytes[i];
596 entry.txPackets += txPackets[i];
597 entry.operations += operations[i];
598 }
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700599 }
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700600 return entry;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700601 }
602
603 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800604 * Fast path for battery stats.
605 */
606 public long getTotalPackets() {
607 long total = 0;
608 for (int i = size-1; i >= 0; i--) {
609 total += rxPackets[i] + txPackets[i];
610 }
611 return total;
612 }
613
614 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700615 * Subtract the given {@link NetworkStats}, effectively leaving the delta
616 * between two snapshots in time. Assumes that statistics rows collect over
617 * time, and that none of them have disappeared.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700618 */
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800619 public NetworkStats subtract(NetworkStats right) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800620 return subtract(this, right, null, null);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800621 }
622
623 /**
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800624 * Subtract the two given {@link NetworkStats} objects, returning the delta
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800625 * between two snapshots in time. Assumes that statistics rows collect over
626 * time, and that none of them have disappeared.
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800627 * <p>
628 * If counters have rolled backwards, they are clamped to {@code 0} and
629 * reported to the given {@link NonMonotonicObserver}.
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800630 */
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800631 public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
632 NonMonotonicObserver<C> observer, C cookie) {
633 return subtract(left, right, observer, cookie, null);
634 }
635
636 /**
637 * Subtract the two given {@link NetworkStats} objects, returning the delta
638 * between two snapshots in time. Assumes that statistics rows collect over
639 * time, and that none of them have disappeared.
640 * <p>
641 * If counters have rolled backwards, they are clamped to {@code 0} and
642 * reported to the given {@link NonMonotonicObserver}.
643 * <p>
644 * If <var>recycle</var> is supplied, this NetworkStats object will be
645 * reused (and returned) as the result if it is large enough to contain
646 * the data.
647 */
648 public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
649 NonMonotonicObserver<C> observer, C cookie, NetworkStats recycle) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800650 long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700651 if (deltaRealtime < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800652 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800653 observer.foundNonMonotonic(left, -1, right, -1, cookie);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800654 }
655 deltaRealtime = 0;
Jeff Sharkey75279902011-05-24 18:39:45 -0700656 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700657
Jeff Sharkey75279902011-05-24 18:39:45 -0700658 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700659 final Entry entry = new Entry();
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800660 final NetworkStats result;
661 if (recycle != null && recycle.capacity >= left.size) {
662 result = recycle;
663 result.size = 0;
664 result.elapsedRealtime = deltaRealtime;
665 } else {
666 result = new NetworkStats(deltaRealtime, left.size);
667 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800668 for (int i = 0; i < left.size; i++) {
669 entry.iface = left.iface[i];
670 entry.uid = left.uid[i];
671 entry.set = left.set[i];
672 entry.tag = left.tag[i];
Stephen Chenc926b732016-10-21 12:44:26 -0700673 entry.metered = left.metered[i];
Jeff Davidsona6a78072016-01-11 16:02:17 -0800674 entry.roaming = left.roaming[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700675
676 // find remote row that matches, and subtract
Jeff Davidsona6a78072016-01-11 16:02:17 -0800677 final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag,
Stephen Chenc926b732016-10-21 12:44:26 -0700678 entry.metered, entry.roaming, i);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700679 if (j == -1) {
680 // newly appearing row, return entire value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800681 entry.rxBytes = left.rxBytes[i];
682 entry.rxPackets = left.rxPackets[i];
683 entry.txBytes = left.txBytes[i];
684 entry.txPackets = left.txPackets[i];
685 entry.operations = left.operations[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700686 } else {
687 // existing row, subtract remote value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800688 entry.rxBytes = left.rxBytes[i] - right.rxBytes[j];
689 entry.rxPackets = left.rxPackets[i] - right.rxPackets[j];
690 entry.txBytes = left.txBytes[i] - right.txBytes[j];
691 entry.txPackets = left.txPackets[i] - right.txPackets[j];
692 entry.operations = left.operations[i] - right.operations[j];
Jeff Sharkey163e6442011-10-31 16:37:52 -0700693
694 if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
695 || entry.txPackets < 0 || entry.operations < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800696 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800697 observer.foundNonMonotonic(left, i, right, j, cookie);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800698 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800699 entry.rxBytes = Math.max(entry.rxBytes, 0);
700 entry.rxPackets = Math.max(entry.rxPackets, 0);
701 entry.txBytes = Math.max(entry.txBytes, 0);
702 entry.txPackets = Math.max(entry.txPackets, 0);
703 entry.operations = Math.max(entry.operations, 0);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700704 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700705 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700706
707 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700708 }
709
Jeff Sharkey4a971222011-06-11 22:16:55 -0700710 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700711 }
712
Jeff Sharkey905b5892011-09-30 15:19:49 -0700713 /**
714 * Return total statistics grouped by {@link #iface}; doesn't mutate the
715 * original structure.
716 */
717 public NetworkStats groupedByIface() {
718 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
719
720 final Entry entry = new Entry();
721 entry.uid = UID_ALL;
722 entry.set = SET_ALL;
723 entry.tag = TAG_NONE;
Stephen Chenc926b732016-10-21 12:44:26 -0700724 entry.metered = METERED_ALL;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800725 entry.roaming = ROAMING_ALL;
Jeff Sharkey905b5892011-09-30 15:19:49 -0700726 entry.operations = 0L;
727
728 for (int i = 0; i < size; i++) {
729 // skip specific tags, since already counted in TAG_NONE
730 if (tag[i] != TAG_NONE) continue;
731
732 entry.iface = iface[i];
733 entry.rxBytes = rxBytes[i];
734 entry.rxPackets = rxPackets[i];
735 entry.txBytes = txBytes[i];
736 entry.txPackets = txPackets[i];
737 stats.combineValues(entry);
738 }
739
740 return stats;
741 }
742
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700743 /**
744 * Return total statistics grouped by {@link #uid}; doesn't mutate the
745 * original structure.
746 */
747 public NetworkStats groupedByUid() {
748 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
749
750 final Entry entry = new Entry();
751 entry.iface = IFACE_ALL;
752 entry.set = SET_ALL;
753 entry.tag = TAG_NONE;
Stephen Chenc926b732016-10-21 12:44:26 -0700754 entry.metered = METERED_ALL;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800755 entry.roaming = ROAMING_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700756
757 for (int i = 0; i < size; i++) {
758 // skip specific tags, since already counted in TAG_NONE
759 if (tag[i] != TAG_NONE) continue;
760
761 entry.uid = uid[i];
762 entry.rxBytes = rxBytes[i];
763 entry.rxPackets = rxPackets[i];
764 entry.txBytes = txBytes[i];
765 entry.txPackets = txPackets[i];
766 entry.operations = operations[i];
767 stats.combineValues(entry);
768 }
769
770 return stats;
771 }
772
Jeff Sharkey163e6442011-10-31 16:37:52 -0700773 /**
774 * Return all rows except those attributed to the requested UID; doesn't
775 * mutate the original structure.
776 */
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700777 public NetworkStats withoutUids(int[] uids) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700778 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
779
780 Entry entry = new Entry();
781 for (int i = 0; i < size; i++) {
782 entry = getValues(i, entry);
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700783 if (!ArrayUtils.contains(uids, entry.uid)) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700784 stats.addValues(entry);
785 }
786 }
787
788 return stats;
789 }
790
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700791 public void dump(String prefix, PrintWriter pw) {
792 pw.print(prefix);
793 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700794 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700795 pw.print(prefix);
Jeff Sharkey3359aca2011-11-08 18:08:48 -0800796 pw.print(" ["); pw.print(i); pw.print("]");
797 pw.print(" iface="); pw.print(iface[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700798 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700799 pw.print(" set="); pw.print(setToString(set[i]));
800 pw.print(" tag="); pw.print(tagToString(tag[i]));
Stephen Chenc926b732016-10-21 12:44:26 -0700801 pw.print(" metered="); pw.print(meteredToString(metered[i]));
Jeff Davidsona6a78072016-01-11 16:02:17 -0800802 pw.print(" roaming="); pw.print(roamingToString(roaming[i]));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700803 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700804 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700805 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700806 pw.print(" txPackets="); pw.print(txPackets[i]);
807 pw.print(" operations="); pw.println(operations[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700808 }
809 }
810
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700811 /**
812 * Return text description of {@link #set} value.
813 */
814 public static String setToString(int set) {
815 switch (set) {
816 case SET_ALL:
817 return "ALL";
818 case SET_DEFAULT:
819 return "DEFAULT";
820 case SET_FOREGROUND:
821 return "FOREGROUND";
Wenchao Tong98170b02015-03-17 16:14:23 -0700822 case SET_DBG_VPN_IN:
823 return "DBG_VPN_IN";
824 case SET_DBG_VPN_OUT:
825 return "DBG_VPN_OUT";
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700826 default:
827 return "UNKNOWN";
828 }
829 }
830
831 /**
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800832 * Return text description of {@link #set} value.
833 */
834 public static String setToCheckinString(int set) {
835 switch (set) {
836 case SET_ALL:
837 return "all";
838 case SET_DEFAULT:
839 return "def";
840 case SET_FOREGROUND:
841 return "fg";
Wenchao Tong98170b02015-03-17 16:14:23 -0700842 case SET_DBG_VPN_IN:
843 return "vpnin";
844 case SET_DBG_VPN_OUT:
845 return "vpnout";
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800846 default:
847 return "unk";
848 }
849 }
850
851 /**
Wenchao Tong98170b02015-03-17 16:14:23 -0700852 * @return true if the querySet matches the dataSet.
853 */
854 public static boolean setMatches(int querySet, int dataSet) {
855 if (querySet == dataSet) {
856 return true;
857 }
858 // SET_ALL matches all non-debugging sets.
859 return querySet == SET_ALL && dataSet < SET_DEBUG_START;
860 }
861
862 /**
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700863 * Return text description of {@link #tag} value.
864 */
865 public static String tagToString(int tag) {
866 return "0x" + Integer.toHexString(tag);
867 }
868
Jeff Davidsona6a78072016-01-11 16:02:17 -0800869 /**
Stephen Chenc926b732016-10-21 12:44:26 -0700870 * Return text description of {@link #metered} value.
871 */
872 public static String meteredToString(int metered) {
873 switch (metered) {
874 case METERED_ALL:
875 return "ALL";
876 case METERED_NO:
877 return "NO";
878 case METERED_YES:
879 return "YES";
880 default:
881 return "UNKNOWN";
882 }
883 }
884
885 /**
Jeff Davidsona6a78072016-01-11 16:02:17 -0800886 * Return text description of {@link #roaming} value.
887 */
888 public static String roamingToString(int roaming) {
889 switch (roaming) {
890 case ROAMING_ALL:
891 return "ALL";
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800892 case ROAMING_NO:
893 return "NO";
894 case ROAMING_YES:
895 return "YES";
Jeff Davidsona6a78072016-01-11 16:02:17 -0800896 default:
897 return "UNKNOWN";
898 }
899 }
900
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700901 @Override
902 public String toString() {
903 final CharArrayWriter writer = new CharArrayWriter();
904 dump("", new PrintWriter(writer));
905 return writer.toString();
906 }
907
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700908 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700909 public int describeContents() {
910 return 0;
911 }
912
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700913 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700914 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700915 public NetworkStats createFromParcel(Parcel in) {
916 return new NetworkStats(in);
917 }
918
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700919 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700920 public NetworkStats[] newArray(int size) {
921 return new NetworkStats[size];
922 }
923 };
Jeff Sharkey163e6442011-10-31 16:37:52 -0700924
Jeff Sharkey63abc372012-01-11 18:38:16 -0800925 public interface NonMonotonicObserver<C> {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800926 public void foundNonMonotonic(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800927 NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700928 }
Wenchao Tong21377c32015-02-26 18:13:07 -0800929
930 /**
931 * VPN accounting. Move some VPN's underlying traffic to other UIDs that use tun0 iface.
932 *
933 * This method should only be called on delta NetworkStats. Do not call this method on a
934 * snapshot {@link NetworkStats} object because the tunUid and/or the underlyingIface may
935 * change over time.
936 *
937 * This method performs adjustments for one active VPN package and one VPN iface at a time.
938 *
939 * It is possible for the VPN software to use multiple underlying networks. This method
940 * only migrates traffic for the primary underlying network.
941 *
942 * @param tunUid uid of the VPN application
943 * @param tunIface iface of the vpn tunnel
944 * @param underlyingIface the primary underlying network iface used by the VPN application
945 * @return true if it successfully adjusts the accounting for VPN, false otherwise
946 */
947 public boolean migrateTun(int tunUid, String tunIface, String underlyingIface) {
948 Entry tunIfaceTotal = new Entry();
949 Entry underlyingIfaceTotal = new Entry();
950
951 tunAdjustmentInit(tunUid, tunIface, underlyingIface, tunIfaceTotal, underlyingIfaceTotal);
952
953 // If tunIface < underlyingIface, it leaves the overhead traffic in the VPN app.
954 // If tunIface > underlyingIface, the VPN app doesn't get credit for data compression.
955 // Negative stats should be avoided.
956 Entry pool = tunGetPool(tunIfaceTotal, underlyingIfaceTotal);
957 if (pool.isEmpty()) {
958 return true;
959 }
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -0700960 Entry moved =
961 addTrafficToApplications(tunUid, tunIface, underlyingIface, tunIfaceTotal, pool);
Wenchao Tong21377c32015-02-26 18:13:07 -0800962 deductTrafficFromVpnApp(tunUid, underlyingIface, moved);
963
964 if (!moved.isEmpty()) {
965 Slog.wtf(TAG, "Failed to deduct underlying network traffic from VPN package. Moved="
966 + moved);
967 return false;
968 }
969 return true;
970 }
971
972 /**
973 * Initializes the data used by the migrateTun() method.
974 *
975 * This is the first pass iteration which does the following work:
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -0700976 * (1) Adds up all the traffic through the tunUid's underlyingIface
Wenchao Tong21377c32015-02-26 18:13:07 -0800977 * (both foreground and background).
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -0700978 * (2) Adds up all the traffic through tun0 excluding traffic from the vpn app itself.
Wenchao Tong21377c32015-02-26 18:13:07 -0800979 */
980 private void tunAdjustmentInit(int tunUid, String tunIface, String underlyingIface,
981 Entry tunIfaceTotal, Entry underlyingIfaceTotal) {
982 Entry recycle = new Entry();
983 for (int i = 0; i < size; i++) {
984 getValues(i, recycle);
985 if (recycle.uid == UID_ALL) {
986 throw new IllegalStateException(
987 "Cannot adjust VPN accounting on an iface aggregated NetworkStats.");
Wenchao Tong98170b02015-03-17 16:14:23 -0700988 } if (recycle.set == SET_DBG_VPN_IN || recycle.set == SET_DBG_VPN_OUT) {
989 throw new IllegalStateException(
990 "Cannot adjust VPN accounting on a NetworkStats containing SET_DBG_VPN_*");
Wenchao Tong21377c32015-02-26 18:13:07 -0800991 }
992
993 if (recycle.uid == tunUid && recycle.tag == TAG_NONE
994 && Objects.equals(underlyingIface, recycle.iface)) {
995 underlyingIfaceTotal.add(recycle);
996 }
997
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -0700998 if (recycle.uid != tunUid && recycle.tag == TAG_NONE
999 && Objects.equals(tunIface, recycle.iface)) {
1000 // Add up all tunIface traffic excluding traffic from the vpn app itself.
Wenchao Tong21377c32015-02-26 18:13:07 -08001001 tunIfaceTotal.add(recycle);
1002 }
1003 }
1004 }
1005
1006 private static Entry tunGetPool(Entry tunIfaceTotal, Entry underlyingIfaceTotal) {
1007 Entry pool = new Entry();
1008 pool.rxBytes = Math.min(tunIfaceTotal.rxBytes, underlyingIfaceTotal.rxBytes);
1009 pool.rxPackets = Math.min(tunIfaceTotal.rxPackets, underlyingIfaceTotal.rxPackets);
1010 pool.txBytes = Math.min(tunIfaceTotal.txBytes, underlyingIfaceTotal.txBytes);
1011 pool.txPackets = Math.min(tunIfaceTotal.txPackets, underlyingIfaceTotal.txPackets);
1012 pool.operations = Math.min(tunIfaceTotal.operations, underlyingIfaceTotal.operations);
1013 return pool;
1014 }
1015
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -07001016 private Entry addTrafficToApplications(int tunUid, String tunIface, String underlyingIface,
Wenchao Tong21377c32015-02-26 18:13:07 -08001017 Entry tunIfaceTotal, Entry pool) {
1018 Entry moved = new Entry();
1019 Entry tmpEntry = new Entry();
1020 tmpEntry.iface = underlyingIface;
1021 for (int i = 0; i < size; i++) {
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -07001022 // the vpn app is excluded from the redistribution but all moved traffic will be
1023 // deducted from the vpn app (see deductTrafficFromVpnApp below).
1024 if (Objects.equals(iface[i], tunIface) && uid[i] != tunUid) {
Wenchao Tong21377c32015-02-26 18:13:07 -08001025 if (tunIfaceTotal.rxBytes > 0) {
1026 tmpEntry.rxBytes = pool.rxBytes * rxBytes[i] / tunIfaceTotal.rxBytes;
1027 } else {
1028 tmpEntry.rxBytes = 0;
1029 }
1030 if (tunIfaceTotal.rxPackets > 0) {
1031 tmpEntry.rxPackets = pool.rxPackets * rxPackets[i] / tunIfaceTotal.rxPackets;
1032 } else {
1033 tmpEntry.rxPackets = 0;
1034 }
1035 if (tunIfaceTotal.txBytes > 0) {
1036 tmpEntry.txBytes = pool.txBytes * txBytes[i] / tunIfaceTotal.txBytes;
1037 } else {
1038 tmpEntry.txBytes = 0;
1039 }
1040 if (tunIfaceTotal.txPackets > 0) {
1041 tmpEntry.txPackets = pool.txPackets * txPackets[i] / tunIfaceTotal.txPackets;
1042 } else {
1043 tmpEntry.txPackets = 0;
1044 }
1045 if (tunIfaceTotal.operations > 0) {
1046 tmpEntry.operations =
1047 pool.operations * operations[i] / tunIfaceTotal.operations;
1048 } else {
1049 tmpEntry.operations = 0;
1050 }
1051 tmpEntry.uid = uid[i];
1052 tmpEntry.tag = tag[i];
1053 tmpEntry.set = set[i];
Stephen Chenc926b732016-10-21 12:44:26 -07001054 tmpEntry.metered = metered[i];
Jeff Davidsona6a78072016-01-11 16:02:17 -08001055 tmpEntry.roaming = roaming[i];
Wenchao Tong21377c32015-02-26 18:13:07 -08001056 combineValues(tmpEntry);
1057 if (tag[i] == TAG_NONE) {
1058 moved.add(tmpEntry);
Wenchao Tong98170b02015-03-17 16:14:23 -07001059 // Add debug info
1060 tmpEntry.set = SET_DBG_VPN_IN;
1061 combineValues(tmpEntry);
Wenchao Tong21377c32015-02-26 18:13:07 -08001062 }
1063 }
1064 }
1065 return moved;
1066 }
1067
1068 private void deductTrafficFromVpnApp(int tunUid, String underlyingIface, Entry moved) {
Wenchao Tong98170b02015-03-17 16:14:23 -07001069 // Add debug info
1070 moved.uid = tunUid;
1071 moved.set = SET_DBG_VPN_OUT;
1072 moved.tag = TAG_NONE;
1073 moved.iface = underlyingIface;
Stephen Chenc926b732016-10-21 12:44:26 -07001074 moved.metered = METERED_ALL;
Jeff Davidsona6a78072016-01-11 16:02:17 -08001075 moved.roaming = ROAMING_ALL;
Wenchao Tong98170b02015-03-17 16:14:23 -07001076 combineValues(moved);
1077
Wenchao Tong21377c32015-02-26 18:13:07 -08001078 // Caveat: if the vpn software uses tag, the total tagged traffic may be greater than
1079 // the TAG_NONE traffic.
Jeff Davidsona6a78072016-01-11 16:02:17 -08001080 //
Stephen Chenc926b732016-10-21 12:44:26 -07001081 // Relies on the fact that the underlying traffic only has state ROAMING_NO and METERED_NO,
1082 // which should be the case as it comes directly from the /proc file. We only blend in the
Jeff Davidsona6a78072016-01-11 16:02:17 -08001083 // roaming data after applying these adjustments, by checking the NetworkIdentity of the
1084 // underlying iface.
1085 int idxVpnBackground = findIndex(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE,
Stephen Chenc926b732016-10-21 12:44:26 -07001086 METERED_NO, ROAMING_NO);
Wenchao Tong21377c32015-02-26 18:13:07 -08001087 if (idxVpnBackground != -1) {
1088 tunSubtract(idxVpnBackground, this, moved);
1089 }
1090
Jeff Davidsona6a78072016-01-11 16:02:17 -08001091 int idxVpnForeground = findIndex(underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE,
Stephen Chenc926b732016-10-21 12:44:26 -07001092 METERED_NO, ROAMING_NO);
Wenchao Tong21377c32015-02-26 18:13:07 -08001093 if (idxVpnForeground != -1) {
1094 tunSubtract(idxVpnForeground, this, moved);
1095 }
1096 }
1097
1098 private static void tunSubtract(int i, NetworkStats left, Entry right) {
1099 long rxBytes = Math.min(left.rxBytes[i], right.rxBytes);
1100 left.rxBytes[i] -= rxBytes;
1101 right.rxBytes -= rxBytes;
1102
1103 long rxPackets = Math.min(left.rxPackets[i], right.rxPackets);
1104 left.rxPackets[i] -= rxPackets;
1105 right.rxPackets -= rxPackets;
1106
1107 long txBytes = Math.min(left.txBytes[i], right.txBytes);
1108 left.txBytes[i] -= txBytes;
1109 right.txBytes -= txBytes;
1110
1111 long txPackets = Math.min(left.txPackets[i], right.txPackets);
1112 left.txPackets[i] -= txPackets;
1113 right.txPackets -= txPackets;
1114 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001115}