blob: f65a50f02df32d7bc9a5e1b3affe2578be238c0e [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
Jeff Davidsona6a78072016-01-11 16:02:17 -080071 /** {@link #set} value for all roaming values. */
72 public static final int ROAMING_ALL = -1;
73 /** {@link #set} value where native, non-roaming data is accounted. */
Jeff Davidson1f7e05e2016-03-10 13:21:38 -080074 public static final int ROAMING_NO = 0;
Jeff Davidsona6a78072016-01-11 16:02:17 -080075 /** {@link #set} value where roaming data is accounted. */
Jeff Davidson1f7e05e2016-03-10 13:21:38 -080076 public static final int ROAMING_YES = 1;
Jeff Davidsona6a78072016-01-11 16:02:17 -080077
Jeff Sharkey163e6442011-10-31 16:37:52 -070078 // TODO: move fields to "mVariable" notation
79
Jeff Sharkey9a13f362011-04-26 16:25:36 -070080 /**
81 * {@link SystemClock#elapsedRealtime()} timestamp when this data was
82 * generated.
83 */
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080084 private long elapsedRealtime;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070085 private int size;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080086 private int capacity;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070087 private String[] iface;
88 private int[] uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070089 private int[] set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070090 private int[] tag;
Jeff Davidsona6a78072016-01-11 16:02:17 -080091 private int[] roaming;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070092 private long[] rxBytes;
93 private long[] rxPackets;
94 private long[] txBytes;
95 private long[] txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070096 private long[] operations;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070097
98 public static class Entry {
99 public String iface;
100 public int uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700101 public int set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700102 public int tag;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800103 /**
104 * Note that this is only populated w/ the default value when read from /proc or written
105 * to disk. We merge in the correct value when reporting this value to clients of
106 * getSummary().
107 */
108 public int roaming;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700109 public long rxBytes;
110 public long rxPackets;
111 public long txBytes;
112 public long txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700113 public long operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700114
115 public Entry() {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700116 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700117 }
118
119 public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700120 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
121 operations);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700122 }
123
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700124 public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets,
125 long txBytes, long txPackets, long operations) {
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800126 this(iface, uid, set, tag, ROAMING_NO, rxBytes, rxPackets, txBytes, txPackets,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800127 operations);
128 }
129
130 public Entry(String iface, int uid, int set, int tag, int roaming, long rxBytes,
131 long rxPackets, long txBytes, long txPackets, long operations) {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700132 this.iface = iface;
133 this.uid = uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700134 this.set = set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700135 this.tag = tag;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800136 this.roaming = roaming;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700137 this.rxBytes = rxBytes;
138 this.rxPackets = rxPackets;
139 this.txBytes = txBytes;
140 this.txPackets = txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700141 this.operations = operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700142 }
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700143
Jeff Sharkey63abc372012-01-11 18:38:16 -0800144 public boolean isNegative() {
145 return rxBytes < 0 || rxPackets < 0 || txBytes < 0 || txPackets < 0 || operations < 0;
146 }
147
148 public boolean isEmpty() {
149 return rxBytes == 0 && rxPackets == 0 && txBytes == 0 && txPackets == 0
150 && operations == 0;
151 }
152
Jeff Sharkey70c70532012-05-16 14:51:19 -0700153 public void add(Entry another) {
154 this.rxBytes += another.rxBytes;
155 this.rxPackets += another.rxPackets;
156 this.txBytes += another.txBytes;
157 this.txPackets += another.txPackets;
158 this.operations += another.operations;
159 }
160
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700161 @Override
162 public String toString() {
163 final StringBuilder builder = new StringBuilder();
164 builder.append("iface=").append(iface);
165 builder.append(" uid=").append(uid);
166 builder.append(" set=").append(setToString(set));
167 builder.append(" tag=").append(tagToString(tag));
Jeff Davidsona6a78072016-01-11 16:02:17 -0800168 builder.append(" roaming=").append(roamingToString(roaming));
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700169 builder.append(" rxBytes=").append(rxBytes);
170 builder.append(" rxPackets=").append(rxPackets);
171 builder.append(" txBytes=").append(txBytes);
172 builder.append(" txPackets=").append(txPackets);
173 builder.append(" operations=").append(operations);
174 return builder.toString();
175 }
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -0800176
177 @Override
178 public boolean equals(Object o) {
179 if (o instanceof Entry) {
180 final Entry e = (Entry) o;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800181 return uid == e.uid && set == e.set && tag == e.tag && roaming == e.roaming
182 && rxBytes == e.rxBytes && rxPackets == e.rxPackets && txBytes == e.txBytes
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -0800183 && txPackets == e.txPackets && operations == e.operations
184 && iface.equals(e.iface);
185 }
186 return false;
187 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700188 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700189
Jeff Sharkey4a971222011-06-11 22:16:55 -0700190 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700191 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700192 this.size = 0;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800193 if (initialSize >= 0) {
194 this.capacity = initialSize;
195 this.iface = new String[initialSize];
196 this.uid = new int[initialSize];
197 this.set = new int[initialSize];
198 this.tag = new int[initialSize];
Jeff Davidsona6a78072016-01-11 16:02:17 -0800199 this.roaming = new int[initialSize];
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800200 this.rxBytes = new long[initialSize];
201 this.rxPackets = new long[initialSize];
202 this.txBytes = new long[initialSize];
203 this.txPackets = new long[initialSize];
204 this.operations = new long[initialSize];
205 } else {
206 // Special case for use by NetworkStatsFactory to start out *really* empty.
207 this.capacity = 0;
Jeff Sharkey7a8f1a32014-09-17 09:26:28 -0700208 this.iface = EmptyArray.STRING;
209 this.uid = EmptyArray.INT;
210 this.set = EmptyArray.INT;
211 this.tag = EmptyArray.INT;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800212 this.roaming = EmptyArray.INT;
Jeff Sharkey7a8f1a32014-09-17 09:26:28 -0700213 this.rxBytes = EmptyArray.LONG;
214 this.rxPackets = EmptyArray.LONG;
215 this.txBytes = EmptyArray.LONG;
216 this.txPackets = EmptyArray.LONG;
217 this.operations = EmptyArray.LONG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800218 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700219 }
220
221 public NetworkStats(Parcel parcel) {
222 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700223 size = parcel.readInt();
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800224 capacity = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700225 iface = parcel.createStringArray();
226 uid = parcel.createIntArray();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700227 set = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700228 tag = parcel.createIntArray();
Jeff Davidsona6a78072016-01-11 16:02:17 -0800229 roaming = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700230 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700231 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700232 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700233 txPackets = parcel.createLongArray();
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700234 operations = parcel.createLongArray();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700235 }
236
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700237 @Override
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700238 public void writeToParcel(Parcel dest, int flags) {
239 dest.writeLong(elapsedRealtime);
240 dest.writeInt(size);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800241 dest.writeInt(capacity);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700242 dest.writeStringArray(iface);
243 dest.writeIntArray(uid);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700244 dest.writeIntArray(set);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700245 dest.writeIntArray(tag);
Jeff Davidsona6a78072016-01-11 16:02:17 -0800246 dest.writeIntArray(roaming);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700247 dest.writeLongArray(rxBytes);
248 dest.writeLongArray(rxPackets);
249 dest.writeLongArray(txBytes);
250 dest.writeLongArray(txPackets);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700251 dest.writeLongArray(operations);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700252 }
253
Jeff Sharkey4abb1b82011-11-08 17:35:28 -0800254 @Override
255 public NetworkStats clone() {
256 final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
257 NetworkStats.Entry entry = null;
258 for (int i = 0; i < size; i++) {
259 entry = getValues(i, entry);
260 clone.addValues(entry);
261 }
262 return clone;
263 }
264
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800265 @VisibleForTesting
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700266 public NetworkStats addIfaceValues(
267 String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
268 return addValues(
269 iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700270 }
271
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800272 @VisibleForTesting
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700273 public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
274 long rxPackets, long txBytes, long txPackets, long operations) {
275 return addValues(new Entry(
276 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700277 }
278
Jeff Davidsona6a78072016-01-11 16:02:17 -0800279 @VisibleForTesting
280 public NetworkStats addValues(String iface, int uid, int set, int tag, int roaming,
281 long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
282 return addValues(new Entry(
283 iface, uid, set, tag, roaming, rxBytes, rxPackets, txBytes, txPackets, operations));
284 }
285
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700286 /**
287 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
288 * object can be recycled across multiple calls.
289 */
290 public NetworkStats addValues(Entry entry) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800291 if (size >= capacity) {
292 final int newLength = Math.max(size, 10) * 3 / 2;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700293 iface = Arrays.copyOf(iface, newLength);
294 uid = Arrays.copyOf(uid, newLength);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700295 set = Arrays.copyOf(set, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700296 tag = Arrays.copyOf(tag, newLength);
Jeff Davidsona6a78072016-01-11 16:02:17 -0800297 roaming = Arrays.copyOf(roaming, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700298 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700299 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700300 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700301 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700302 operations = Arrays.copyOf(operations, newLength);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800303 capacity = newLength;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700304 }
305
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700306 iface[size] = entry.iface;
307 uid[size] = entry.uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700308 set[size] = entry.set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700309 tag[size] = entry.tag;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800310 roaming[size] = entry.roaming;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700311 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700312 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700313 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700314 txPackets[size] = entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700315 operations[size] = entry.operations;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700316 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700317
Jeff Sharkey4a971222011-06-11 22:16:55 -0700318 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700319 }
320
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700321 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700322 * Return specific stats entry.
323 */
324 public Entry getValues(int i, Entry recycle) {
325 final Entry entry = recycle != null ? recycle : new Entry();
326 entry.iface = iface[i];
327 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700328 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700329 entry.tag = tag[i];
Jeff Davidsona6a78072016-01-11 16:02:17 -0800330 entry.roaming = roaming[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700331 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700332 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700333 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700334 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700335 entry.operations = operations[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700336 return entry;
337 }
338
339 public long getElapsedRealtime() {
340 return elapsedRealtime;
341 }
342
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800343 public void setElapsedRealtime(long time) {
344 elapsedRealtime = time;
345 }
346
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700347 /**
348 * Return age of this {@link NetworkStats} object with respect to
349 * {@link SystemClock#elapsedRealtime()}.
350 */
351 public long getElapsedRealtimeAge() {
352 return SystemClock.elapsedRealtime() - elapsedRealtime;
353 }
354
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700355 public int size() {
356 return size;
357 }
358
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800359 @VisibleForTesting
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700360 public int internalSize() {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800361 return capacity;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700362 }
363
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700364 @Deprecated
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700365 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700366 long txBytes, long txPackets, long operations) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700367 return combineValues(
Jeff Davidsona6a78072016-01-11 16:02:17 -0800368 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes,
369 txPackets, operations);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700370 }
371
Jeff Davidsona6a78072016-01-11 16:02:17 -0800372 public NetworkStats combineValues(String iface, int uid, int set, int tag,
373 long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700374 return combineValues(new Entry(
375 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700376 }
377
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700378 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700379 * Combine given values with an existing row, or create a new row if
Jeff Davidsona6a78072016-01-11 16:02:17 -0800380 * {@link #findIndex(String, int, int, int, int)} is unable to find match. Can
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700381 * also be used to subtract values from existing rows.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700382 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700383 public NetworkStats combineValues(Entry entry) {
Jeff Davidsona6a78072016-01-11 16:02:17 -0800384 final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag, entry.roaming);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700385 if (i == -1) {
386 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700387 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700388 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700389 rxBytes[i] += entry.rxBytes;
390 rxPackets[i] += entry.rxPackets;
391 txBytes[i] += entry.txBytes;
392 txPackets[i] += entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700393 operations[i] += entry.operations;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700394 }
395 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700396 }
397
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700398 /**
Jeff Sharkey905b5892011-09-30 15:19:49 -0700399 * Combine all values from another {@link NetworkStats} into this object.
400 */
401 public void combineAllValues(NetworkStats another) {
402 NetworkStats.Entry entry = null;
403 for (int i = 0; i < another.size; i++) {
404 entry = another.getValues(i, entry);
405 combineValues(entry);
406 }
407 }
408
409 /**
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700410 * Find first stats index that matches the requested parameters.
411 */
Jeff Davidsona6a78072016-01-11 16:02:17 -0800412 public int findIndex(String iface, int uid, int set, int tag, int roaming) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700413 for (int i = 0; i < size; i++) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700414 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
Jeff Davidsona6a78072016-01-11 16:02:17 -0800415 && roaming == this.roaming[i] && Objects.equals(iface, this.iface[i])) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700416 return i;
417 }
418 }
419 return -1;
420 }
421
422 /**
423 * Find first stats index that matches the requested parameters, starting
424 * search around the hinted index as an optimization.
425 */
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800426 @VisibleForTesting
Jeff Davidsona6a78072016-01-11 16:02:17 -0800427 public int findIndexHinted(String iface, int uid, int set, int tag, int roaming,
428 int hintIndex) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700429 for (int offset = 0; offset < size; offset++) {
430 final int halfOffset = offset / 2;
431
432 // search outwards from hint index, alternating forward and backward
433 final int i;
434 if (offset % 2 == 0) {
435 i = (hintIndex + halfOffset) % size;
436 } else {
437 i = (size + hintIndex - halfOffset - 1) % size;
438 }
439
440 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
Jeff Davidsona6a78072016-01-11 16:02:17 -0800441 && roaming == this.roaming[i] && Objects.equals(iface, this.iface[i])) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700442 return i;
443 }
444 }
445 return -1;
446 }
447
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700448 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700449 * Splice in {@link #operations} from the given {@link NetworkStats} based
450 * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
451 * since operation counts are at data layer.
452 */
453 public void spliceOperationsFrom(NetworkStats stats) {
454 for (int i = 0; i < size; i++) {
Jeff Davidsona6a78072016-01-11 16:02:17 -0800455 final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i], roaming[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700456 if (j == -1) {
457 operations[i] = 0;
458 } else {
459 operations[i] = stats.operations[j];
460 }
461 }
462 }
463
464 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700465 * Return list of unique interfaces known by this data structure.
466 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700467 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700468 final HashSet<String> ifaces = new HashSet<String>();
469 for (String iface : this.iface) {
470 if (iface != IFACE_ALL) {
471 ifaces.add(iface);
472 }
473 }
474 return ifaces.toArray(new String[ifaces.size()]);
475 }
476
477 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700478 * Return list of unique UIDs known by this data structure.
479 */
480 public int[] getUniqueUids() {
481 final SparseBooleanArray uids = new SparseBooleanArray();
482 for (int uid : this.uid) {
483 uids.put(uid, true);
484 }
485
486 final int size = uids.size();
487 final int[] result = new int[size];
488 for (int i = 0; i < size; i++) {
489 result[i] = uids.keyAt(i);
490 }
491 return result;
492 }
493
494 /**
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700495 * Return total bytes represented by this snapshot object, usually used when
496 * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
497 */
498 public long getTotalBytes() {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700499 final Entry entry = getTotal(null);
500 return entry.rxBytes + entry.txBytes;
501 }
502
503 /**
504 * Return total of all fields represented by this snapshot object.
505 */
506 public Entry getTotal(Entry recycle) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800507 return getTotal(recycle, null, UID_ALL, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700508 }
509
510 /**
511 * Return total of all fields represented by this snapshot object matching
512 * the requested {@link #uid}.
513 */
514 public Entry getTotal(Entry recycle, int limitUid) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800515 return getTotal(recycle, null, limitUid, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700516 }
517
518 /**
519 * Return total of all fields represented by this snapshot object matching
520 * the requested {@link #iface}.
521 */
522 public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800523 return getTotal(recycle, limitIface, UID_ALL, false);
524 }
525
526 public Entry getTotalIncludingTags(Entry recycle) {
527 return getTotal(recycle, null, UID_ALL, true);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700528 }
529
530 /**
531 * Return total of all fields represented by this snapshot object matching
532 * the requested {@link #iface} and {@link #uid}.
533 *
534 * @param limitIface Set of {@link #iface} to include in total; or {@code
535 * null} to include all ifaces.
536 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800537 private Entry getTotal(
538 Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags) {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700539 final Entry entry = recycle != null ? recycle : new Entry();
540
541 entry.iface = IFACE_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700542 entry.uid = limitUid;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700543 entry.set = SET_ALL;
544 entry.tag = TAG_NONE;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800545 entry.roaming = ROAMING_ALL;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700546 entry.rxBytes = 0;
547 entry.rxPackets = 0;
548 entry.txBytes = 0;
549 entry.txPackets = 0;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700550 entry.operations = 0;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700551
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700552 for (int i = 0; i < size; i++) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700553 final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]);
554 final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i]));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700555
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700556 if (matchesUid && matchesIface) {
557 // skip specific tags, since already counted in TAG_NONE
Jeff Sharkey63abc372012-01-11 18:38:16 -0800558 if (tag[i] != TAG_NONE && !includeTags) continue;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700559
560 entry.rxBytes += rxBytes[i];
561 entry.rxPackets += rxPackets[i];
562 entry.txBytes += txBytes[i];
563 entry.txPackets += txPackets[i];
564 entry.operations += operations[i];
565 }
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700566 }
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700567 return entry;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700568 }
569
570 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800571 * Fast path for battery stats.
572 */
573 public long getTotalPackets() {
574 long total = 0;
575 for (int i = size-1; i >= 0; i--) {
576 total += rxPackets[i] + txPackets[i];
577 }
578 return total;
579 }
580
581 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700582 * Subtract the given {@link NetworkStats}, effectively leaving the delta
583 * between two snapshots in time. Assumes that statistics rows collect over
584 * time, and that none of them have disappeared.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700585 */
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800586 public NetworkStats subtract(NetworkStats right) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800587 return subtract(this, right, null, null);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800588 }
589
590 /**
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800591 * Subtract the two given {@link NetworkStats} objects, returning the delta
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800592 * between two snapshots in time. Assumes that statistics rows collect over
593 * time, and that none of them have disappeared.
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800594 * <p>
595 * If counters have rolled backwards, they are clamped to {@code 0} and
596 * reported to the given {@link NonMonotonicObserver}.
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800597 */
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800598 public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
599 NonMonotonicObserver<C> observer, C cookie) {
600 return subtract(left, right, observer, cookie, null);
601 }
602
603 /**
604 * Subtract the two given {@link NetworkStats} objects, returning the delta
605 * between two snapshots in time. Assumes that statistics rows collect over
606 * time, and that none of them have disappeared.
607 * <p>
608 * If counters have rolled backwards, they are clamped to {@code 0} and
609 * reported to the given {@link NonMonotonicObserver}.
610 * <p>
611 * If <var>recycle</var> is supplied, this NetworkStats object will be
612 * reused (and returned) as the result if it is large enough to contain
613 * the data.
614 */
615 public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
616 NonMonotonicObserver<C> observer, C cookie, NetworkStats recycle) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800617 long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700618 if (deltaRealtime < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800619 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800620 observer.foundNonMonotonic(left, -1, right, -1, cookie);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800621 }
622 deltaRealtime = 0;
Jeff Sharkey75279902011-05-24 18:39:45 -0700623 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700624
Jeff Sharkey75279902011-05-24 18:39:45 -0700625 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700626 final Entry entry = new Entry();
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800627 final NetworkStats result;
628 if (recycle != null && recycle.capacity >= left.size) {
629 result = recycle;
630 result.size = 0;
631 result.elapsedRealtime = deltaRealtime;
632 } else {
633 result = new NetworkStats(deltaRealtime, left.size);
634 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800635 for (int i = 0; i < left.size; i++) {
636 entry.iface = left.iface[i];
637 entry.uid = left.uid[i];
638 entry.set = left.set[i];
639 entry.tag = left.tag[i];
Jeff Davidsona6a78072016-01-11 16:02:17 -0800640 entry.roaming = left.roaming[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700641
642 // find remote row that matches, and subtract
Jeff Davidsona6a78072016-01-11 16:02:17 -0800643 final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag,
644 entry.roaming, i);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700645 if (j == -1) {
646 // newly appearing row, return entire value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800647 entry.rxBytes = left.rxBytes[i];
648 entry.rxPackets = left.rxPackets[i];
649 entry.txBytes = left.txBytes[i];
650 entry.txPackets = left.txPackets[i];
651 entry.operations = left.operations[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700652 } else {
653 // existing row, subtract remote value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800654 entry.rxBytes = left.rxBytes[i] - right.rxBytes[j];
655 entry.rxPackets = left.rxPackets[i] - right.rxPackets[j];
656 entry.txBytes = left.txBytes[i] - right.txBytes[j];
657 entry.txPackets = left.txPackets[i] - right.txPackets[j];
658 entry.operations = left.operations[i] - right.operations[j];
Jeff Sharkey163e6442011-10-31 16:37:52 -0700659
660 if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
661 || entry.txPackets < 0 || entry.operations < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800662 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800663 observer.foundNonMonotonic(left, i, right, j, cookie);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800664 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800665 entry.rxBytes = Math.max(entry.rxBytes, 0);
666 entry.rxPackets = Math.max(entry.rxPackets, 0);
667 entry.txBytes = Math.max(entry.txBytes, 0);
668 entry.txPackets = Math.max(entry.txPackets, 0);
669 entry.operations = Math.max(entry.operations, 0);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700670 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700671 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700672
673 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700674 }
675
Jeff Sharkey4a971222011-06-11 22:16:55 -0700676 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700677 }
678
Jeff Sharkey905b5892011-09-30 15:19:49 -0700679 /**
680 * Return total statistics grouped by {@link #iface}; doesn't mutate the
681 * original structure.
682 */
683 public NetworkStats groupedByIface() {
684 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
685
686 final Entry entry = new Entry();
687 entry.uid = UID_ALL;
688 entry.set = SET_ALL;
689 entry.tag = TAG_NONE;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800690 entry.roaming = ROAMING_ALL;
Jeff Sharkey905b5892011-09-30 15:19:49 -0700691 entry.operations = 0L;
692
693 for (int i = 0; i < size; i++) {
694 // skip specific tags, since already counted in TAG_NONE
695 if (tag[i] != TAG_NONE) continue;
696
697 entry.iface = iface[i];
698 entry.rxBytes = rxBytes[i];
699 entry.rxPackets = rxPackets[i];
700 entry.txBytes = txBytes[i];
701 entry.txPackets = txPackets[i];
702 stats.combineValues(entry);
703 }
704
705 return stats;
706 }
707
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700708 /**
709 * Return total statistics grouped by {@link #uid}; doesn't mutate the
710 * original structure.
711 */
712 public NetworkStats groupedByUid() {
713 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
714
715 final Entry entry = new Entry();
716 entry.iface = IFACE_ALL;
717 entry.set = SET_ALL;
718 entry.tag = TAG_NONE;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800719 entry.roaming = ROAMING_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700720
721 for (int i = 0; i < size; i++) {
722 // skip specific tags, since already counted in TAG_NONE
723 if (tag[i] != TAG_NONE) continue;
724
725 entry.uid = uid[i];
726 entry.rxBytes = rxBytes[i];
727 entry.rxPackets = rxPackets[i];
728 entry.txBytes = txBytes[i];
729 entry.txPackets = txPackets[i];
730 entry.operations = operations[i];
731 stats.combineValues(entry);
732 }
733
734 return stats;
735 }
736
Jeff Sharkey163e6442011-10-31 16:37:52 -0700737 /**
738 * Return all rows except those attributed to the requested UID; doesn't
739 * mutate the original structure.
740 */
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700741 public NetworkStats withoutUids(int[] uids) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700742 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
743
744 Entry entry = new Entry();
745 for (int i = 0; i < size; i++) {
746 entry = getValues(i, entry);
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700747 if (!ArrayUtils.contains(uids, entry.uid)) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700748 stats.addValues(entry);
749 }
750 }
751
752 return stats;
753 }
754
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700755 public void dump(String prefix, PrintWriter pw) {
756 pw.print(prefix);
757 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700758 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700759 pw.print(prefix);
Jeff Sharkey3359aca2011-11-08 18:08:48 -0800760 pw.print(" ["); pw.print(i); pw.print("]");
761 pw.print(" iface="); pw.print(iface[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700762 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700763 pw.print(" set="); pw.print(setToString(set[i]));
764 pw.print(" tag="); pw.print(tagToString(tag[i]));
Jeff Davidsona6a78072016-01-11 16:02:17 -0800765 pw.print(" roaming="); pw.print(roamingToString(roaming[i]));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700766 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700767 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700768 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700769 pw.print(" txPackets="); pw.print(txPackets[i]);
770 pw.print(" operations="); pw.println(operations[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700771 }
772 }
773
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700774 /**
775 * Return text description of {@link #set} value.
776 */
777 public static String setToString(int set) {
778 switch (set) {
779 case SET_ALL:
780 return "ALL";
781 case SET_DEFAULT:
782 return "DEFAULT";
783 case SET_FOREGROUND:
784 return "FOREGROUND";
Wenchao Tong98170b02015-03-17 16:14:23 -0700785 case SET_DBG_VPN_IN:
786 return "DBG_VPN_IN";
787 case SET_DBG_VPN_OUT:
788 return "DBG_VPN_OUT";
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700789 default:
790 return "UNKNOWN";
791 }
792 }
793
794 /**
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800795 * Return text description of {@link #set} value.
796 */
797 public static String setToCheckinString(int set) {
798 switch (set) {
799 case SET_ALL:
800 return "all";
801 case SET_DEFAULT:
802 return "def";
803 case SET_FOREGROUND:
804 return "fg";
Wenchao Tong98170b02015-03-17 16:14:23 -0700805 case SET_DBG_VPN_IN:
806 return "vpnin";
807 case SET_DBG_VPN_OUT:
808 return "vpnout";
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800809 default:
810 return "unk";
811 }
812 }
813
814 /**
Wenchao Tong98170b02015-03-17 16:14:23 -0700815 * @return true if the querySet matches the dataSet.
816 */
817 public static boolean setMatches(int querySet, int dataSet) {
818 if (querySet == dataSet) {
819 return true;
820 }
821 // SET_ALL matches all non-debugging sets.
822 return querySet == SET_ALL && dataSet < SET_DEBUG_START;
823 }
824
825 /**
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700826 * Return text description of {@link #tag} value.
827 */
828 public static String tagToString(int tag) {
829 return "0x" + Integer.toHexString(tag);
830 }
831
Jeff Davidsona6a78072016-01-11 16:02:17 -0800832 /**
833 * Return text description of {@link #roaming} value.
834 */
835 public static String roamingToString(int roaming) {
836 switch (roaming) {
837 case ROAMING_ALL:
838 return "ALL";
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800839 case ROAMING_NO:
840 return "NO";
841 case ROAMING_YES:
842 return "YES";
Jeff Davidsona6a78072016-01-11 16:02:17 -0800843 default:
844 return "UNKNOWN";
845 }
846 }
847
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700848 @Override
849 public String toString() {
850 final CharArrayWriter writer = new CharArrayWriter();
851 dump("", new PrintWriter(writer));
852 return writer.toString();
853 }
854
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700855 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700856 public int describeContents() {
857 return 0;
858 }
859
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700860 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700861 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700862 public NetworkStats createFromParcel(Parcel in) {
863 return new NetworkStats(in);
864 }
865
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700866 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700867 public NetworkStats[] newArray(int size) {
868 return new NetworkStats[size];
869 }
870 };
Jeff Sharkey163e6442011-10-31 16:37:52 -0700871
Jeff Sharkey63abc372012-01-11 18:38:16 -0800872 public interface NonMonotonicObserver<C> {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800873 public void foundNonMonotonic(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800874 NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700875 }
Wenchao Tong21377c32015-02-26 18:13:07 -0800876
877 /**
878 * VPN accounting. Move some VPN's underlying traffic to other UIDs that use tun0 iface.
879 *
880 * This method should only be called on delta NetworkStats. Do not call this method on a
881 * snapshot {@link NetworkStats} object because the tunUid and/or the underlyingIface may
882 * change over time.
883 *
884 * This method performs adjustments for one active VPN package and one VPN iface at a time.
885 *
886 * It is possible for the VPN software to use multiple underlying networks. This method
887 * only migrates traffic for the primary underlying network.
888 *
889 * @param tunUid uid of the VPN application
890 * @param tunIface iface of the vpn tunnel
891 * @param underlyingIface the primary underlying network iface used by the VPN application
892 * @return true if it successfully adjusts the accounting for VPN, false otherwise
893 */
894 public boolean migrateTun(int tunUid, String tunIface, String underlyingIface) {
895 Entry tunIfaceTotal = new Entry();
896 Entry underlyingIfaceTotal = new Entry();
897
898 tunAdjustmentInit(tunUid, tunIface, underlyingIface, tunIfaceTotal, underlyingIfaceTotal);
899
900 // If tunIface < underlyingIface, it leaves the overhead traffic in the VPN app.
901 // If tunIface > underlyingIface, the VPN app doesn't get credit for data compression.
902 // Negative stats should be avoided.
903 Entry pool = tunGetPool(tunIfaceTotal, underlyingIfaceTotal);
904 if (pool.isEmpty()) {
905 return true;
906 }
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -0700907 Entry moved =
908 addTrafficToApplications(tunUid, tunIface, underlyingIface, tunIfaceTotal, pool);
Wenchao Tong21377c32015-02-26 18:13:07 -0800909 deductTrafficFromVpnApp(tunUid, underlyingIface, moved);
910
911 if (!moved.isEmpty()) {
912 Slog.wtf(TAG, "Failed to deduct underlying network traffic from VPN package. Moved="
913 + moved);
914 return false;
915 }
916 return true;
917 }
918
919 /**
920 * Initializes the data used by the migrateTun() method.
921 *
922 * This is the first pass iteration which does the following work:
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -0700923 * (1) Adds up all the traffic through the tunUid's underlyingIface
Wenchao Tong21377c32015-02-26 18:13:07 -0800924 * (both foreground and background).
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -0700925 * (2) Adds up all the traffic through tun0 excluding traffic from the vpn app itself.
Wenchao Tong21377c32015-02-26 18:13:07 -0800926 */
927 private void tunAdjustmentInit(int tunUid, String tunIface, String underlyingIface,
928 Entry tunIfaceTotal, Entry underlyingIfaceTotal) {
929 Entry recycle = new Entry();
930 for (int i = 0; i < size; i++) {
931 getValues(i, recycle);
932 if (recycle.uid == UID_ALL) {
933 throw new IllegalStateException(
934 "Cannot adjust VPN accounting on an iface aggregated NetworkStats.");
Wenchao Tong98170b02015-03-17 16:14:23 -0700935 } if (recycle.set == SET_DBG_VPN_IN || recycle.set == SET_DBG_VPN_OUT) {
936 throw new IllegalStateException(
937 "Cannot adjust VPN accounting on a NetworkStats containing SET_DBG_VPN_*");
Wenchao Tong21377c32015-02-26 18:13:07 -0800938 }
939
940 if (recycle.uid == tunUid && recycle.tag == TAG_NONE
941 && Objects.equals(underlyingIface, recycle.iface)) {
942 underlyingIfaceTotal.add(recycle);
943 }
944
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -0700945 if (recycle.uid != tunUid && recycle.tag == TAG_NONE
946 && Objects.equals(tunIface, recycle.iface)) {
947 // Add up all tunIface traffic excluding traffic from the vpn app itself.
Wenchao Tong21377c32015-02-26 18:13:07 -0800948 tunIfaceTotal.add(recycle);
949 }
950 }
951 }
952
953 private static Entry tunGetPool(Entry tunIfaceTotal, Entry underlyingIfaceTotal) {
954 Entry pool = new Entry();
955 pool.rxBytes = Math.min(tunIfaceTotal.rxBytes, underlyingIfaceTotal.rxBytes);
956 pool.rxPackets = Math.min(tunIfaceTotal.rxPackets, underlyingIfaceTotal.rxPackets);
957 pool.txBytes = Math.min(tunIfaceTotal.txBytes, underlyingIfaceTotal.txBytes);
958 pool.txPackets = Math.min(tunIfaceTotal.txPackets, underlyingIfaceTotal.txPackets);
959 pool.operations = Math.min(tunIfaceTotal.operations, underlyingIfaceTotal.operations);
960 return pool;
961 }
962
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -0700963 private Entry addTrafficToApplications(int tunUid, String tunIface, String underlyingIface,
Wenchao Tong21377c32015-02-26 18:13:07 -0800964 Entry tunIfaceTotal, Entry pool) {
965 Entry moved = new Entry();
966 Entry tmpEntry = new Entry();
967 tmpEntry.iface = underlyingIface;
968 for (int i = 0; i < size; i++) {
Jeremy Joslin3f0d75a2016-08-08 16:07:37 -0700969 // the vpn app is excluded from the redistribution but all moved traffic will be
970 // deducted from the vpn app (see deductTrafficFromVpnApp below).
971 if (Objects.equals(iface[i], tunIface) && uid[i] != tunUid) {
Wenchao Tong21377c32015-02-26 18:13:07 -0800972 if (tunIfaceTotal.rxBytes > 0) {
973 tmpEntry.rxBytes = pool.rxBytes * rxBytes[i] / tunIfaceTotal.rxBytes;
974 } else {
975 tmpEntry.rxBytes = 0;
976 }
977 if (tunIfaceTotal.rxPackets > 0) {
978 tmpEntry.rxPackets = pool.rxPackets * rxPackets[i] / tunIfaceTotal.rxPackets;
979 } else {
980 tmpEntry.rxPackets = 0;
981 }
982 if (tunIfaceTotal.txBytes > 0) {
983 tmpEntry.txBytes = pool.txBytes * txBytes[i] / tunIfaceTotal.txBytes;
984 } else {
985 tmpEntry.txBytes = 0;
986 }
987 if (tunIfaceTotal.txPackets > 0) {
988 tmpEntry.txPackets = pool.txPackets * txPackets[i] / tunIfaceTotal.txPackets;
989 } else {
990 tmpEntry.txPackets = 0;
991 }
992 if (tunIfaceTotal.operations > 0) {
993 tmpEntry.operations =
994 pool.operations * operations[i] / tunIfaceTotal.operations;
995 } else {
996 tmpEntry.operations = 0;
997 }
998 tmpEntry.uid = uid[i];
999 tmpEntry.tag = tag[i];
1000 tmpEntry.set = set[i];
Jeff Davidsona6a78072016-01-11 16:02:17 -08001001 tmpEntry.roaming = roaming[i];
Wenchao Tong21377c32015-02-26 18:13:07 -08001002 combineValues(tmpEntry);
1003 if (tag[i] == TAG_NONE) {
1004 moved.add(tmpEntry);
Wenchao Tong98170b02015-03-17 16:14:23 -07001005 // Add debug info
1006 tmpEntry.set = SET_DBG_VPN_IN;
1007 combineValues(tmpEntry);
Wenchao Tong21377c32015-02-26 18:13:07 -08001008 }
1009 }
1010 }
1011 return moved;
1012 }
1013
1014 private void deductTrafficFromVpnApp(int tunUid, String underlyingIface, Entry moved) {
Wenchao Tong98170b02015-03-17 16:14:23 -07001015 // Add debug info
1016 moved.uid = tunUid;
1017 moved.set = SET_DBG_VPN_OUT;
1018 moved.tag = TAG_NONE;
1019 moved.iface = underlyingIface;
Jeff Davidsona6a78072016-01-11 16:02:17 -08001020 moved.roaming = ROAMING_ALL;
Wenchao Tong98170b02015-03-17 16:14:23 -07001021 combineValues(moved);
1022
Wenchao Tong21377c32015-02-26 18:13:07 -08001023 // Caveat: if the vpn software uses tag, the total tagged traffic may be greater than
1024 // the TAG_NONE traffic.
Jeff Davidsona6a78072016-01-11 16:02:17 -08001025 //
Jeff Davidson1f7e05e2016-03-10 13:21:38 -08001026 // Relies on the fact that the underlying traffic only has state ROAMING_NO, which
Jeff Davidsona6a78072016-01-11 16:02:17 -08001027 // should be the case as it comes directly from the /proc file. We only blend in the
1028 // roaming data after applying these adjustments, by checking the NetworkIdentity of the
1029 // underlying iface.
1030 int idxVpnBackground = findIndex(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE,
Jeff Davidson1f7e05e2016-03-10 13:21:38 -08001031 ROAMING_NO);
Wenchao Tong21377c32015-02-26 18:13:07 -08001032 if (idxVpnBackground != -1) {
1033 tunSubtract(idxVpnBackground, this, moved);
1034 }
1035
Jeff Davidsona6a78072016-01-11 16:02:17 -08001036 int idxVpnForeground = findIndex(underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE,
Jeff Davidson1f7e05e2016-03-10 13:21:38 -08001037 ROAMING_NO);
Wenchao Tong21377c32015-02-26 18:13:07 -08001038 if (idxVpnForeground != -1) {
1039 tunSubtract(idxVpnForeground, this, moved);
1040 }
1041 }
1042
1043 private static void tunSubtract(int i, NetworkStats left, Entry right) {
1044 long rxBytes = Math.min(left.rxBytes[i], right.rxBytes);
1045 left.rxBytes[i] -= rxBytes;
1046 right.rxBytes -= rxBytes;
1047
1048 long rxPackets = Math.min(left.rxPackets[i], right.rxPackets);
1049 left.rxPackets[i] -= rxPackets;
1050 right.rxPackets -= rxPackets;
1051
1052 long txBytes = Math.min(left.txBytes[i], right.txBytes);
1053 left.txBytes[i] -= txBytes;
1054 right.txBytes -= txBytes;
1055
1056 long txPackets = Math.min(left.txPackets[i], right.txPackets);
1057 left.txPackets[i] -= txPackets;
1058 right.txPackets -= txPackets;
1059 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001060}