blob: 9cb904d4c400541ab64dc741683945b96e03e9ad [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;
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -070022import android.util.SparseBooleanArray;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070023
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080024import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkeydaa57e82012-09-19 14:10:39 -070025import com.android.internal.util.ArrayUtils;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070026import com.android.internal.util.Objects;
27
Jeff Sharkey9a13f362011-04-26 16:25:36 -070028import java.io.CharArrayWriter;
29import java.io.PrintWriter;
Jeff Sharkey4a971222011-06-11 22:16:55 -070030import java.util.Arrays;
Jeff Sharkey75279902011-05-24 18:39:45 -070031import java.util.HashSet;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070032
33/**
Jeff Sharkey75279902011-05-24 18:39:45 -070034 * Collection of active network statistics. Can contain summary details across
35 * all interfaces, or details with per-UID granularity. Internally stores data
36 * as a large table, closely matching {@code /proc/} data format. This structure
37 * optimizes for rapid in-memory comparison, but consider using
38 * {@link NetworkStatsHistory} when persisting.
Jeff Sharkey9a13f362011-04-26 16:25:36 -070039 *
40 * @hide
41 */
42public class NetworkStats implements Parcelable {
Jeff Sharkey75279902011-05-24 18:39:45 -070043 /** {@link #iface} value when interface details unavailable. */
Jeff Sharkey9a13f362011-04-26 16:25:36 -070044 public static final String IFACE_ALL = null;
Jeff Sharkey75279902011-05-24 18:39:45 -070045 /** {@link #uid} value when UID details unavailable. */
46 public static final int UID_ALL = -1;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070047 /** {@link #set} value when all sets combined. */
48 public static final int SET_ALL = -1;
49 /** {@link #set} value where background data is accounted. */
50 public static final int SET_DEFAULT = 0;
51 /** {@link #set} value where foreground data is accounted. */
52 public static final int SET_FOREGROUND = 1;
53 /** {@link #tag} value for total data across all tags. */
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070054 public static final int TAG_NONE = 0;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070055
Jeff Sharkey163e6442011-10-31 16:37:52 -070056 // TODO: move fields to "mVariable" notation
57
Jeff Sharkey9a13f362011-04-26 16:25:36 -070058 /**
59 * {@link SystemClock#elapsedRealtime()} timestamp when this data was
60 * generated.
61 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070062 private final long elapsedRealtime;
63 private int size;
64 private String[] iface;
65 private int[] uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070066 private int[] set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070067 private int[] tag;
68 private long[] rxBytes;
69 private long[] rxPackets;
70 private long[] txBytes;
71 private long[] txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070072 private long[] operations;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070073
74 public static class Entry {
75 public String iface;
76 public int uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070077 public int set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070078 public int tag;
79 public long rxBytes;
80 public long rxPackets;
81 public long txBytes;
82 public long txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070083 public long operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070084
85 public Entry() {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070086 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
Jeff Sharkey63d27a92011-08-03 17:04:22 -070087 }
88
89 public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070090 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
91 operations);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070092 }
93
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070094 public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets,
95 long txBytes, long txPackets, long operations) {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070096 this.iface = iface;
97 this.uid = uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070098 this.set = set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070099 this.tag = tag;
100 this.rxBytes = rxBytes;
101 this.rxPackets = rxPackets;
102 this.txBytes = txBytes;
103 this.txPackets = txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700104 this.operations = operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700105 }
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700106
Jeff Sharkey63abc372012-01-11 18:38:16 -0800107 public boolean isNegative() {
108 return rxBytes < 0 || rxPackets < 0 || txBytes < 0 || txPackets < 0 || operations < 0;
109 }
110
111 public boolean isEmpty() {
112 return rxBytes == 0 && rxPackets == 0 && txBytes == 0 && txPackets == 0
113 && operations == 0;
114 }
115
Jeff Sharkey70c70532012-05-16 14:51:19 -0700116 public void add(Entry another) {
117 this.rxBytes += another.rxBytes;
118 this.rxPackets += another.rxPackets;
119 this.txBytes += another.txBytes;
120 this.txPackets += another.txPackets;
121 this.operations += another.operations;
122 }
123
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700124 @Override
125 public String toString() {
126 final StringBuilder builder = new StringBuilder();
127 builder.append("iface=").append(iface);
128 builder.append(" uid=").append(uid);
129 builder.append(" set=").append(setToString(set));
130 builder.append(" tag=").append(tagToString(tag));
131 builder.append(" rxBytes=").append(rxBytes);
132 builder.append(" rxPackets=").append(rxPackets);
133 builder.append(" txBytes=").append(txBytes);
134 builder.append(" txPackets=").append(txPackets);
135 builder.append(" operations=").append(operations);
136 return builder.toString();
137 }
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -0800138
139 @Override
140 public boolean equals(Object o) {
141 if (o instanceof Entry) {
142 final Entry e = (Entry) o;
143 return uid == e.uid && set == e.set && tag == e.tag && rxBytes == e.rxBytes
144 && rxPackets == e.rxPackets && txBytes == e.txBytes
145 && txPackets == e.txPackets && operations == e.operations
146 && iface.equals(e.iface);
147 }
148 return false;
149 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700150 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700151
Jeff Sharkey4a971222011-06-11 22:16:55 -0700152 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700153 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700154 this.size = 0;
155 this.iface = new String[initialSize];
156 this.uid = new int[initialSize];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700157 this.set = new int[initialSize];
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700158 this.tag = new int[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700159 this.rxBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700160 this.rxPackets = new long[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700161 this.txBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700162 this.txPackets = new long[initialSize];
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700163 this.operations = new long[initialSize];
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700164 }
165
166 public NetworkStats(Parcel parcel) {
167 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700168 size = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700169 iface = parcel.createStringArray();
170 uid = parcel.createIntArray();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700171 set = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700172 tag = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700173 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700174 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700175 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700176 txPackets = parcel.createLongArray();
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700177 operations = parcel.createLongArray();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700178 }
179
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700180 @Override
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700181 public void writeToParcel(Parcel dest, int flags) {
182 dest.writeLong(elapsedRealtime);
183 dest.writeInt(size);
184 dest.writeStringArray(iface);
185 dest.writeIntArray(uid);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700186 dest.writeIntArray(set);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700187 dest.writeIntArray(tag);
188 dest.writeLongArray(rxBytes);
189 dest.writeLongArray(rxPackets);
190 dest.writeLongArray(txBytes);
191 dest.writeLongArray(txPackets);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700192 dest.writeLongArray(operations);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700193 }
194
Jeff Sharkey4abb1b82011-11-08 17:35:28 -0800195 @Override
196 public NetworkStats clone() {
197 final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
198 NetworkStats.Entry entry = null;
199 for (int i = 0; i < size; i++) {
200 entry = getValues(i, entry);
201 clone.addValues(entry);
202 }
203 return clone;
204 }
205
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800206 @VisibleForTesting
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700207 public NetworkStats addIfaceValues(
208 String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
209 return addValues(
210 iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700211 }
212
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800213 @VisibleForTesting
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700214 public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
215 long rxPackets, long txBytes, long txPackets, long operations) {
216 return addValues(new Entry(
217 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700218 }
219
220 /**
221 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
222 * object can be recycled across multiple calls.
223 */
224 public NetworkStats addValues(Entry entry) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700225 if (size >= this.iface.length) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700226 final int newLength = Math.max(iface.length, 10) * 3 / 2;
227 iface = Arrays.copyOf(iface, newLength);
228 uid = Arrays.copyOf(uid, newLength);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700229 set = Arrays.copyOf(set, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700230 tag = Arrays.copyOf(tag, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700231 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700232 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700233 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700234 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700235 operations = Arrays.copyOf(operations, newLength);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700236 }
237
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700238 iface[size] = entry.iface;
239 uid[size] = entry.uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700240 set[size] = entry.set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700241 tag[size] = entry.tag;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700242 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700243 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700244 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700245 txPackets[size] = entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700246 operations[size] = entry.operations;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700247 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700248
Jeff Sharkey4a971222011-06-11 22:16:55 -0700249 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700250 }
251
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700252 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700253 * Return specific stats entry.
254 */
255 public Entry getValues(int i, Entry recycle) {
256 final Entry entry = recycle != null ? recycle : new Entry();
257 entry.iface = iface[i];
258 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700259 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700260 entry.tag = tag[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700261 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700262 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700263 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700264 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700265 entry.operations = operations[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700266 return entry;
267 }
268
269 public long getElapsedRealtime() {
270 return elapsedRealtime;
271 }
272
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700273 /**
274 * Return age of this {@link NetworkStats} object with respect to
275 * {@link SystemClock#elapsedRealtime()}.
276 */
277 public long getElapsedRealtimeAge() {
278 return SystemClock.elapsedRealtime() - elapsedRealtime;
279 }
280
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700281 public int size() {
282 return size;
283 }
284
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800285 @VisibleForTesting
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700286 public int internalSize() {
287 return iface.length;
288 }
289
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700290 @Deprecated
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700291 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700292 long txBytes, long txPackets, long operations) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700293 return combineValues(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700294 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations);
295 }
296
297 public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes,
298 long rxPackets, long txBytes, long txPackets, long operations) {
299 return combineValues(new Entry(
300 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700301 }
302
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700303 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700304 * Combine given values with an existing row, or create a new row if
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700305 * {@link #findIndex(String, int, int, int)} is unable to find match. Can
306 * also be used to subtract values from existing rows.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700307 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700308 public NetworkStats combineValues(Entry entry) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700309 final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700310 if (i == -1) {
311 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700312 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700313 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700314 rxBytes[i] += entry.rxBytes;
315 rxPackets[i] += entry.rxPackets;
316 txBytes[i] += entry.txBytes;
317 txPackets[i] += entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700318 operations[i] += entry.operations;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700319 }
320 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700321 }
322
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700323 /**
Jeff Sharkey905b5892011-09-30 15:19:49 -0700324 * Combine all values from another {@link NetworkStats} into this object.
325 */
326 public void combineAllValues(NetworkStats another) {
327 NetworkStats.Entry entry = null;
328 for (int i = 0; i < another.size; i++) {
329 entry = another.getValues(i, entry);
330 combineValues(entry);
331 }
332 }
333
334 /**
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700335 * Find first stats index that matches the requested parameters.
336 */
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700337 public int findIndex(String iface, int uid, int set, int tag) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700338 for (int i = 0; i < size; i++) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700339 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
340 && Objects.equal(iface, this.iface[i])) {
341 return i;
342 }
343 }
344 return -1;
345 }
346
347 /**
348 * Find first stats index that matches the requested parameters, starting
349 * search around the hinted index as an optimization.
350 */
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800351 @VisibleForTesting
Jeff Sharkey163e6442011-10-31 16:37:52 -0700352 public int findIndexHinted(String iface, int uid, int set, int tag, int hintIndex) {
353 for (int offset = 0; offset < size; offset++) {
354 final int halfOffset = offset / 2;
355
356 // search outwards from hint index, alternating forward and backward
357 final int i;
358 if (offset % 2 == 0) {
359 i = (hintIndex + halfOffset) % size;
360 } else {
361 i = (size + hintIndex - halfOffset - 1) % size;
362 }
363
364 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
365 && Objects.equal(iface, this.iface[i])) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700366 return i;
367 }
368 }
369 return -1;
370 }
371
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700372 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700373 * Splice in {@link #operations} from the given {@link NetworkStats} based
374 * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
375 * since operation counts are at data layer.
376 */
377 public void spliceOperationsFrom(NetworkStats stats) {
378 for (int i = 0; i < size; i++) {
Jeff Sharkey21a54782012-04-09 10:27:55 -0700379 final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700380 if (j == -1) {
381 operations[i] = 0;
382 } else {
383 operations[i] = stats.operations[j];
384 }
385 }
386 }
387
388 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700389 * Return list of unique interfaces known by this data structure.
390 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700391 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700392 final HashSet<String> ifaces = new HashSet<String>();
393 for (String iface : this.iface) {
394 if (iface != IFACE_ALL) {
395 ifaces.add(iface);
396 }
397 }
398 return ifaces.toArray(new String[ifaces.size()]);
399 }
400
401 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700402 * Return list of unique UIDs known by this data structure.
403 */
404 public int[] getUniqueUids() {
405 final SparseBooleanArray uids = new SparseBooleanArray();
406 for (int uid : this.uid) {
407 uids.put(uid, true);
408 }
409
410 final int size = uids.size();
411 final int[] result = new int[size];
412 for (int i = 0; i < size; i++) {
413 result[i] = uids.keyAt(i);
414 }
415 return result;
416 }
417
418 /**
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700419 * Return total bytes represented by this snapshot object, usually used when
420 * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
421 */
422 public long getTotalBytes() {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700423 final Entry entry = getTotal(null);
424 return entry.rxBytes + entry.txBytes;
425 }
426
427 /**
428 * Return total of all fields represented by this snapshot object.
429 */
430 public Entry getTotal(Entry recycle) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800431 return getTotal(recycle, null, UID_ALL, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700432 }
433
434 /**
435 * Return total of all fields represented by this snapshot object matching
436 * the requested {@link #uid}.
437 */
438 public Entry getTotal(Entry recycle, int limitUid) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800439 return getTotal(recycle, null, limitUid, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700440 }
441
442 /**
443 * Return total of all fields represented by this snapshot object matching
444 * the requested {@link #iface}.
445 */
446 public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800447 return getTotal(recycle, limitIface, UID_ALL, false);
448 }
449
450 public Entry getTotalIncludingTags(Entry recycle) {
451 return getTotal(recycle, null, UID_ALL, true);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700452 }
453
454 /**
455 * Return total of all fields represented by this snapshot object matching
456 * the requested {@link #iface} and {@link #uid}.
457 *
458 * @param limitIface Set of {@link #iface} to include in total; or {@code
459 * null} to include all ifaces.
460 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800461 private Entry getTotal(
462 Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags) {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700463 final Entry entry = recycle != null ? recycle : new Entry();
464
465 entry.iface = IFACE_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700466 entry.uid = limitUid;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700467 entry.set = SET_ALL;
468 entry.tag = TAG_NONE;
469 entry.rxBytes = 0;
470 entry.rxPackets = 0;
471 entry.txBytes = 0;
472 entry.txPackets = 0;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700473 entry.operations = 0;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700474
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700475 for (int i = 0; i < size; i++) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700476 final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]);
477 final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i]));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700478
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700479 if (matchesUid && matchesIface) {
480 // skip specific tags, since already counted in TAG_NONE
Jeff Sharkey63abc372012-01-11 18:38:16 -0800481 if (tag[i] != TAG_NONE && !includeTags) continue;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700482
483 entry.rxBytes += rxBytes[i];
484 entry.rxPackets += rxPackets[i];
485 entry.txBytes += txBytes[i];
486 entry.txPackets += txPackets[i];
487 entry.operations += operations[i];
488 }
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700489 }
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700490 return entry;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700491 }
492
493 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700494 * Subtract the given {@link NetworkStats}, effectively leaving the delta
495 * between two snapshots in time. Assumes that statistics rows collect over
496 * time, and that none of them have disappeared.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700497 */
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800498 public NetworkStats subtract(NetworkStats right) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800499 return subtract(this, right, null, null);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800500 }
501
502 /**
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800503 * Subtract the two given {@link NetworkStats} objects, returning the delta
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800504 * between two snapshots in time. Assumes that statistics rows collect over
505 * time, and that none of them have disappeared.
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800506 * <p>
507 * If counters have rolled backwards, they are clamped to {@code 0} and
508 * reported to the given {@link NonMonotonicObserver}.
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800509 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800510 public static <C> NetworkStats subtract(
511 NetworkStats left, NetworkStats right, NonMonotonicObserver<C> observer, C cookie) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800512 long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700513 if (deltaRealtime < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800514 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800515 observer.foundNonMonotonic(left, -1, right, -1, cookie);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800516 }
517 deltaRealtime = 0;
Jeff Sharkey75279902011-05-24 18:39:45 -0700518 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700519
Jeff Sharkey75279902011-05-24 18:39:45 -0700520 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700521 final Entry entry = new Entry();
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800522 final NetworkStats result = new NetworkStats(deltaRealtime, left.size);
523 for (int i = 0; i < left.size; i++) {
524 entry.iface = left.iface[i];
525 entry.uid = left.uid[i];
526 entry.set = left.set[i];
527 entry.tag = left.tag[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700528
529 // find remote row that matches, and subtract
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800530 final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag, i);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700531 if (j == -1) {
532 // newly appearing row, return entire value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800533 entry.rxBytes = left.rxBytes[i];
534 entry.rxPackets = left.rxPackets[i];
535 entry.txBytes = left.txBytes[i];
536 entry.txPackets = left.txPackets[i];
537 entry.operations = left.operations[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700538 } else {
539 // existing row, subtract remote value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800540 entry.rxBytes = left.rxBytes[i] - right.rxBytes[j];
541 entry.rxPackets = left.rxPackets[i] - right.rxPackets[j];
542 entry.txBytes = left.txBytes[i] - right.txBytes[j];
543 entry.txPackets = left.txPackets[i] - right.txPackets[j];
544 entry.operations = left.operations[i] - right.operations[j];
Jeff Sharkey163e6442011-10-31 16:37:52 -0700545
546 if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
547 || entry.txPackets < 0 || entry.operations < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800548 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800549 observer.foundNonMonotonic(left, i, right, j, cookie);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800550 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800551 entry.rxBytes = Math.max(entry.rxBytes, 0);
552 entry.rxPackets = Math.max(entry.rxPackets, 0);
553 entry.txBytes = Math.max(entry.txBytes, 0);
554 entry.txPackets = Math.max(entry.txPackets, 0);
555 entry.operations = Math.max(entry.operations, 0);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700556 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700557 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700558
559 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700560 }
561
Jeff Sharkey4a971222011-06-11 22:16:55 -0700562 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700563 }
564
Jeff Sharkey905b5892011-09-30 15:19:49 -0700565 /**
566 * Return total statistics grouped by {@link #iface}; doesn't mutate the
567 * original structure.
568 */
569 public NetworkStats groupedByIface() {
570 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
571
572 final Entry entry = new Entry();
573 entry.uid = UID_ALL;
574 entry.set = SET_ALL;
575 entry.tag = TAG_NONE;
576 entry.operations = 0L;
577
578 for (int i = 0; i < size; i++) {
579 // skip specific tags, since already counted in TAG_NONE
580 if (tag[i] != TAG_NONE) continue;
581
582 entry.iface = iface[i];
583 entry.rxBytes = rxBytes[i];
584 entry.rxPackets = rxPackets[i];
585 entry.txBytes = txBytes[i];
586 entry.txPackets = txPackets[i];
587 stats.combineValues(entry);
588 }
589
590 return stats;
591 }
592
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700593 /**
594 * Return total statistics grouped by {@link #uid}; doesn't mutate the
595 * original structure.
596 */
597 public NetworkStats groupedByUid() {
598 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
599
600 final Entry entry = new Entry();
601 entry.iface = IFACE_ALL;
602 entry.set = SET_ALL;
603 entry.tag = TAG_NONE;
604
605 for (int i = 0; i < size; i++) {
606 // skip specific tags, since already counted in TAG_NONE
607 if (tag[i] != TAG_NONE) continue;
608
609 entry.uid = uid[i];
610 entry.rxBytes = rxBytes[i];
611 entry.rxPackets = rxPackets[i];
612 entry.txBytes = txBytes[i];
613 entry.txPackets = txPackets[i];
614 entry.operations = operations[i];
615 stats.combineValues(entry);
616 }
617
618 return stats;
619 }
620
Jeff Sharkey163e6442011-10-31 16:37:52 -0700621 /**
622 * Return all rows except those attributed to the requested UID; doesn't
623 * mutate the original structure.
624 */
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700625 public NetworkStats withoutUids(int[] uids) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700626 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
627
628 Entry entry = new Entry();
629 for (int i = 0; i < size; i++) {
630 entry = getValues(i, entry);
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700631 if (!ArrayUtils.contains(uids, entry.uid)) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700632 stats.addValues(entry);
633 }
634 }
635
636 return stats;
637 }
638
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700639 public void dump(String prefix, PrintWriter pw) {
640 pw.print(prefix);
641 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700642 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700643 pw.print(prefix);
Jeff Sharkey3359aca2011-11-08 18:08:48 -0800644 pw.print(" ["); pw.print(i); pw.print("]");
645 pw.print(" iface="); pw.print(iface[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700646 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700647 pw.print(" set="); pw.print(setToString(set[i]));
648 pw.print(" tag="); pw.print(tagToString(tag[i]));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700649 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700650 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700651 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700652 pw.print(" txPackets="); pw.print(txPackets[i]);
653 pw.print(" operations="); pw.println(operations[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700654 }
655 }
656
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700657 /**
658 * Return text description of {@link #set} value.
659 */
660 public static String setToString(int set) {
661 switch (set) {
662 case SET_ALL:
663 return "ALL";
664 case SET_DEFAULT:
665 return "DEFAULT";
666 case SET_FOREGROUND:
667 return "FOREGROUND";
668 default:
669 return "UNKNOWN";
670 }
671 }
672
673 /**
674 * Return text description of {@link #tag} value.
675 */
676 public static String tagToString(int tag) {
677 return "0x" + Integer.toHexString(tag);
678 }
679
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700680 @Override
681 public String toString() {
682 final CharArrayWriter writer = new CharArrayWriter();
683 dump("", new PrintWriter(writer));
684 return writer.toString();
685 }
686
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700687 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700688 public int describeContents() {
689 return 0;
690 }
691
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700692 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700693 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700694 public NetworkStats createFromParcel(Parcel in) {
695 return new NetworkStats(in);
696 }
697
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700698 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700699 public NetworkStats[] newArray(int size) {
700 return new NetworkStats[size];
701 }
702 };
Jeff Sharkey163e6442011-10-31 16:37:52 -0700703
Jeff Sharkey63abc372012-01-11 18:38:16 -0800704 public interface NonMonotonicObserver<C> {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800705 public void foundNonMonotonic(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800706 NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700707 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700708}