blob: e8f60b49686f45bb37ef59e537defa89c8bd9fbd [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 Sharkeya63ba592011-07-19 23:47:12 -070024import com.android.internal.util.Objects;
25
Jeff Sharkey9a13f362011-04-26 16:25:36 -070026import java.io.CharArrayWriter;
27import java.io.PrintWriter;
Jeff Sharkey4a971222011-06-11 22:16:55 -070028import java.util.Arrays;
Jeff Sharkey75279902011-05-24 18:39:45 -070029import java.util.HashSet;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070030
31/**
Jeff Sharkey75279902011-05-24 18:39:45 -070032 * Collection of active network statistics. Can contain summary details across
33 * all interfaces, or details with per-UID granularity. Internally stores data
34 * as a large table, closely matching {@code /proc/} data format. This structure
35 * optimizes for rapid in-memory comparison, but consider using
36 * {@link NetworkStatsHistory} when persisting.
Jeff Sharkey9a13f362011-04-26 16:25:36 -070037 *
38 * @hide
39 */
40public class NetworkStats implements Parcelable {
Jeff Sharkey75279902011-05-24 18:39:45 -070041 /** {@link #iface} value when interface details unavailable. */
Jeff Sharkey9a13f362011-04-26 16:25:36 -070042 public static final String IFACE_ALL = null;
Jeff Sharkey75279902011-05-24 18:39:45 -070043 /** {@link #uid} value when UID details unavailable. */
44 public static final int UID_ALL = -1;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070045 /** {@link #set} value when all sets combined. */
46 public static final int SET_ALL = -1;
47 /** {@link #set} value where background data is accounted. */
48 public static final int SET_DEFAULT = 0;
49 /** {@link #set} value where foreground data is accounted. */
50 public static final int SET_FOREGROUND = 1;
51 /** {@link #tag} value for total data across all tags. */
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070052 public static final int TAG_NONE = 0;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070053
Jeff Sharkey163e6442011-10-31 16:37:52 -070054 // TODO: move fields to "mVariable" notation
55
Jeff Sharkey9a13f362011-04-26 16:25:36 -070056 /**
57 * {@link SystemClock#elapsedRealtime()} timestamp when this data was
58 * generated.
59 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070060 private final long elapsedRealtime;
61 private int size;
62 private String[] iface;
63 private int[] uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070064 private int[] set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070065 private int[] tag;
66 private long[] rxBytes;
67 private long[] rxPackets;
68 private long[] txBytes;
69 private long[] txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070070 private long[] operations;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070071
72 public static class Entry {
73 public String iface;
74 public int uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070075 public int set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070076 public int tag;
77 public long rxBytes;
78 public long rxPackets;
79 public long txBytes;
80 public long txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070081 public long operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070082
83 public Entry() {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070084 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
Jeff Sharkey63d27a92011-08-03 17:04:22 -070085 }
86
87 public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070088 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
89 operations);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070090 }
91
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070092 public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets,
93 long txBytes, long txPackets, long operations) {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070094 this.iface = iface;
95 this.uid = uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070096 this.set = set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070097 this.tag = tag;
98 this.rxBytes = rxBytes;
99 this.rxPackets = rxPackets;
100 this.txBytes = txBytes;
101 this.txPackets = txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700102 this.operations = operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700103 }
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700104
105 @Override
106 public String toString() {
107 final StringBuilder builder = new StringBuilder();
108 builder.append("iface=").append(iface);
109 builder.append(" uid=").append(uid);
110 builder.append(" set=").append(setToString(set));
111 builder.append(" tag=").append(tagToString(tag));
112 builder.append(" rxBytes=").append(rxBytes);
113 builder.append(" rxPackets=").append(rxPackets);
114 builder.append(" txBytes=").append(txBytes);
115 builder.append(" txPackets=").append(txPackets);
116 builder.append(" operations=").append(operations);
117 return builder.toString();
118 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700119 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700120
Jeff Sharkey4a971222011-06-11 22:16:55 -0700121 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700122 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700123 this.size = 0;
124 this.iface = new String[initialSize];
125 this.uid = new int[initialSize];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700126 this.set = new int[initialSize];
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700127 this.tag = new int[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700128 this.rxBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700129 this.rxPackets = new long[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700130 this.txBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700131 this.txPackets = new long[initialSize];
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700132 this.operations = new long[initialSize];
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700133 }
134
135 public NetworkStats(Parcel parcel) {
136 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700137 size = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700138 iface = parcel.createStringArray();
139 uid = parcel.createIntArray();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700140 set = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700141 tag = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700142 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700143 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700144 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700145 txPackets = parcel.createLongArray();
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700146 operations = parcel.createLongArray();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700147 }
148
149 /** {@inheritDoc} */
150 public void writeToParcel(Parcel dest, int flags) {
151 dest.writeLong(elapsedRealtime);
152 dest.writeInt(size);
153 dest.writeStringArray(iface);
154 dest.writeIntArray(uid);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700155 dest.writeIntArray(set);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700156 dest.writeIntArray(tag);
157 dest.writeLongArray(rxBytes);
158 dest.writeLongArray(rxPackets);
159 dest.writeLongArray(txBytes);
160 dest.writeLongArray(txPackets);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700161 dest.writeLongArray(operations);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700162 }
163
Jeff Sharkey4abb1b82011-11-08 17:35:28 -0800164 @Override
165 public NetworkStats clone() {
166 final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
167 NetworkStats.Entry entry = null;
168 for (int i = 0; i < size; i++) {
169 entry = getValues(i, entry);
170 clone.addValues(entry);
171 }
172 return clone;
173 }
174
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700175 // @VisibleForTesting
176 public NetworkStats addIfaceValues(
177 String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
178 return addValues(
179 iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700180 }
181
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700182 // @VisibleForTesting
183 public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
184 long rxPackets, long txBytes, long txPackets, long operations) {
185 return addValues(new Entry(
186 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700187 }
188
189 /**
190 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
191 * object can be recycled across multiple calls.
192 */
193 public NetworkStats addValues(Entry entry) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700194 if (size >= this.iface.length) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700195 final int newLength = Math.max(iface.length, 10) * 3 / 2;
196 iface = Arrays.copyOf(iface, newLength);
197 uid = Arrays.copyOf(uid, newLength);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700198 set = Arrays.copyOf(set, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700199 tag = Arrays.copyOf(tag, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700200 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700201 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700202 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700203 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700204 operations = Arrays.copyOf(operations, newLength);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700205 }
206
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700207 iface[size] = entry.iface;
208 uid[size] = entry.uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700209 set[size] = entry.set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700210 tag[size] = entry.tag;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700211 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700212 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700213 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700214 txPackets[size] = entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700215 operations[size] = entry.operations;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700216 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700217
Jeff Sharkey4a971222011-06-11 22:16:55 -0700218 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700219 }
220
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700221 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700222 * Return specific stats entry.
223 */
224 public Entry getValues(int i, Entry recycle) {
225 final Entry entry = recycle != null ? recycle : new Entry();
226 entry.iface = iface[i];
227 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700228 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700229 entry.tag = tag[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700230 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700231 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700232 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700233 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700234 entry.operations = operations[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700235 return entry;
236 }
237
238 public long getElapsedRealtime() {
239 return elapsedRealtime;
240 }
241
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700242 /**
243 * Return age of this {@link NetworkStats} object with respect to
244 * {@link SystemClock#elapsedRealtime()}.
245 */
246 public long getElapsedRealtimeAge() {
247 return SystemClock.elapsedRealtime() - elapsedRealtime;
248 }
249
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700250 public int size() {
251 return size;
252 }
253
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700254 // @VisibleForTesting
255 public int internalSize() {
256 return iface.length;
257 }
258
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700259 @Deprecated
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700260 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700261 long txBytes, long txPackets, long operations) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700262 return combineValues(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700263 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations);
264 }
265
266 public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes,
267 long rxPackets, long txBytes, long txPackets, long operations) {
268 return combineValues(new Entry(
269 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700270 }
271
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700272 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700273 * Combine given values with an existing row, or create a new row if
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700274 * {@link #findIndex(String, int, int, int)} is unable to find match. Can
275 * also be used to subtract values from existing rows.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700276 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700277 public NetworkStats combineValues(Entry entry) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700278 final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700279 if (i == -1) {
280 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700281 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700282 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700283 rxBytes[i] += entry.rxBytes;
284 rxPackets[i] += entry.rxPackets;
285 txBytes[i] += entry.txBytes;
286 txPackets[i] += entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700287 operations[i] += entry.operations;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700288 }
289 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700290 }
291
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700292 /**
Jeff Sharkey905b5892011-09-30 15:19:49 -0700293 * Combine all values from another {@link NetworkStats} into this object.
294 */
295 public void combineAllValues(NetworkStats another) {
296 NetworkStats.Entry entry = null;
297 for (int i = 0; i < another.size; i++) {
298 entry = another.getValues(i, entry);
299 combineValues(entry);
300 }
301 }
302
303 /**
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700304 * Find first stats index that matches the requested parameters.
305 */
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700306 public int findIndex(String iface, int uid, int set, int tag) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700307 for (int i = 0; i < size; i++) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700308 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
309 && Objects.equal(iface, this.iface[i])) {
310 return i;
311 }
312 }
313 return -1;
314 }
315
316 /**
317 * Find first stats index that matches the requested parameters, starting
318 * search around the hinted index as an optimization.
319 */
320 // @VisibleForTesting
321 public int findIndexHinted(String iface, int uid, int set, int tag, int hintIndex) {
322 for (int offset = 0; offset < size; offset++) {
323 final int halfOffset = offset / 2;
324
325 // search outwards from hint index, alternating forward and backward
326 final int i;
327 if (offset % 2 == 0) {
328 i = (hintIndex + halfOffset) % size;
329 } else {
330 i = (size + hintIndex - halfOffset - 1) % size;
331 }
332
333 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
334 && Objects.equal(iface, this.iface[i])) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700335 return i;
336 }
337 }
338 return -1;
339 }
340
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700341 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700342 * Splice in {@link #operations} from the given {@link NetworkStats} based
343 * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
344 * since operation counts are at data layer.
345 */
346 public void spliceOperationsFrom(NetworkStats stats) {
347 for (int i = 0; i < size; i++) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700348 final int j = stats.findIndex(IFACE_ALL, uid[i], set[i], tag[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700349 if (j == -1) {
350 operations[i] = 0;
351 } else {
352 operations[i] = stats.operations[j];
353 }
354 }
355 }
356
357 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700358 * Return list of unique interfaces known by this data structure.
359 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700360 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700361 final HashSet<String> ifaces = new HashSet<String>();
362 for (String iface : this.iface) {
363 if (iface != IFACE_ALL) {
364 ifaces.add(iface);
365 }
366 }
367 return ifaces.toArray(new String[ifaces.size()]);
368 }
369
370 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700371 * Return list of unique UIDs known by this data structure.
372 */
373 public int[] getUniqueUids() {
374 final SparseBooleanArray uids = new SparseBooleanArray();
375 for (int uid : this.uid) {
376 uids.put(uid, true);
377 }
378
379 final int size = uids.size();
380 final int[] result = new int[size];
381 for (int i = 0; i < size; i++) {
382 result[i] = uids.keyAt(i);
383 }
384 return result;
385 }
386
387 /**
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700388 * Return total bytes represented by this snapshot object, usually used when
389 * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
390 */
391 public long getTotalBytes() {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700392 final Entry entry = getTotal(null);
393 return entry.rxBytes + entry.txBytes;
394 }
395
396 /**
397 * Return total of all fields represented by this snapshot object.
398 */
399 public Entry getTotal(Entry recycle) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700400 return getTotal(recycle, null, UID_ALL);
401 }
402
403 /**
404 * Return total of all fields represented by this snapshot object matching
405 * the requested {@link #uid}.
406 */
407 public Entry getTotal(Entry recycle, int limitUid) {
408 return getTotal(recycle, null, limitUid);
409 }
410
411 /**
412 * Return total of all fields represented by this snapshot object matching
413 * the requested {@link #iface}.
414 */
415 public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
416 return getTotal(recycle, limitIface, UID_ALL);
417 }
418
419 /**
420 * Return total of all fields represented by this snapshot object matching
421 * the requested {@link #iface} and {@link #uid}.
422 *
423 * @param limitIface Set of {@link #iface} to include in total; or {@code
424 * null} to include all ifaces.
425 */
426 private Entry getTotal(Entry recycle, HashSet<String> limitIface, int limitUid) {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700427 final Entry entry = recycle != null ? recycle : new Entry();
428
429 entry.iface = IFACE_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700430 entry.uid = limitUid;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700431 entry.set = SET_ALL;
432 entry.tag = TAG_NONE;
433 entry.rxBytes = 0;
434 entry.rxPackets = 0;
435 entry.txBytes = 0;
436 entry.txPackets = 0;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700437 entry.operations = 0;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700438
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700439 for (int i = 0; i < size; i++) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700440 final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]);
441 final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i]));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700442
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700443 if (matchesUid && matchesIface) {
444 // skip specific tags, since already counted in TAG_NONE
445 if (tag[i] != TAG_NONE) continue;
446
447 entry.rxBytes += rxBytes[i];
448 entry.rxPackets += rxPackets[i];
449 entry.txBytes += txBytes[i];
450 entry.txPackets += txPackets[i];
451 entry.operations += operations[i];
452 }
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700453 }
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700454 return entry;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700455 }
456
457 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700458 * Subtract the given {@link NetworkStats}, effectively leaving the delta
459 * between two snapshots in time. Assumes that statistics rows collect over
460 * time, and that none of them have disappeared.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700461 */
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800462 public NetworkStats subtract(NetworkStats right) {
463 return subtract(this, right, null);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800464 }
465
466 /**
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800467 * Subtract the two given {@link NetworkStats} objects, returning the delta
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800468 * between two snapshots in time. Assumes that statistics rows collect over
469 * time, and that none of them have disappeared.
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800470 * <p>
471 * If counters have rolled backwards, they are clamped to {@code 0} and
472 * reported to the given {@link NonMonotonicObserver}.
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800473 */
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800474 public static NetworkStats subtract(
475 NetworkStats left, NetworkStats right, NonMonotonicObserver observer) {
476 long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700477 if (deltaRealtime < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800478 if (observer != null) {
479 observer.foundNonMonotonic(left, -1, right, -1);
480 }
481 deltaRealtime = 0;
Jeff Sharkey75279902011-05-24 18:39:45 -0700482 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700483
Jeff Sharkey75279902011-05-24 18:39:45 -0700484 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700485 final Entry entry = new Entry();
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800486 final NetworkStats result = new NetworkStats(deltaRealtime, left.size);
487 for (int i = 0; i < left.size; i++) {
488 entry.iface = left.iface[i];
489 entry.uid = left.uid[i];
490 entry.set = left.set[i];
491 entry.tag = left.tag[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700492
493 // find remote row that matches, and subtract
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800494 final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag, i);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700495 if (j == -1) {
496 // newly appearing row, return entire value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800497 entry.rxBytes = left.rxBytes[i];
498 entry.rxPackets = left.rxPackets[i];
499 entry.txBytes = left.txBytes[i];
500 entry.txPackets = left.txPackets[i];
501 entry.operations = left.operations[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700502 } else {
503 // existing row, subtract remote value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800504 entry.rxBytes = left.rxBytes[i] - right.rxBytes[j];
505 entry.rxPackets = left.rxPackets[i] - right.rxPackets[j];
506 entry.txBytes = left.txBytes[i] - right.txBytes[j];
507 entry.txPackets = left.txPackets[i] - right.txPackets[j];
508 entry.operations = left.operations[i] - right.operations[j];
Jeff Sharkey163e6442011-10-31 16:37:52 -0700509
510 if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
511 || entry.txPackets < 0 || entry.operations < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800512 if (observer != null) {
513 observer.foundNonMonotonic(left, i, right, j);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800514 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800515 entry.rxBytes = Math.max(entry.rxBytes, 0);
516 entry.rxPackets = Math.max(entry.rxPackets, 0);
517 entry.txBytes = Math.max(entry.txBytes, 0);
518 entry.txPackets = Math.max(entry.txPackets, 0);
519 entry.operations = Math.max(entry.operations, 0);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700520 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700521 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700522
523 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700524 }
525
Jeff Sharkey4a971222011-06-11 22:16:55 -0700526 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700527 }
528
Jeff Sharkey905b5892011-09-30 15:19:49 -0700529 /**
530 * Return total statistics grouped by {@link #iface}; doesn't mutate the
531 * original structure.
532 */
533 public NetworkStats groupedByIface() {
534 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
535
536 final Entry entry = new Entry();
537 entry.uid = UID_ALL;
538 entry.set = SET_ALL;
539 entry.tag = TAG_NONE;
540 entry.operations = 0L;
541
542 for (int i = 0; i < size; i++) {
543 // skip specific tags, since already counted in TAG_NONE
544 if (tag[i] != TAG_NONE) continue;
545
546 entry.iface = iface[i];
547 entry.rxBytes = rxBytes[i];
548 entry.rxPackets = rxPackets[i];
549 entry.txBytes = txBytes[i];
550 entry.txPackets = txPackets[i];
551 stats.combineValues(entry);
552 }
553
554 return stats;
555 }
556
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700557 /**
558 * Return total statistics grouped by {@link #uid}; doesn't mutate the
559 * original structure.
560 */
561 public NetworkStats groupedByUid() {
562 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
563
564 final Entry entry = new Entry();
565 entry.iface = IFACE_ALL;
566 entry.set = SET_ALL;
567 entry.tag = TAG_NONE;
568
569 for (int i = 0; i < size; i++) {
570 // skip specific tags, since already counted in TAG_NONE
571 if (tag[i] != TAG_NONE) continue;
572
573 entry.uid = uid[i];
574 entry.rxBytes = rxBytes[i];
575 entry.rxPackets = rxPackets[i];
576 entry.txBytes = txBytes[i];
577 entry.txPackets = txPackets[i];
578 entry.operations = operations[i];
579 stats.combineValues(entry);
580 }
581
582 return stats;
583 }
584
Jeff Sharkey163e6442011-10-31 16:37:52 -0700585 /**
586 * Return all rows except those attributed to the requested UID; doesn't
587 * mutate the original structure.
588 */
589 public NetworkStats withoutUid(int uid) {
590 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
591
592 Entry entry = new Entry();
593 for (int i = 0; i < size; i++) {
594 entry = getValues(i, entry);
595 if (entry.uid != uid) {
596 stats.addValues(entry);
597 }
598 }
599
600 return stats;
601 }
602
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700603 public void dump(String prefix, PrintWriter pw) {
604 pw.print(prefix);
605 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700606 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700607 pw.print(prefix);
Jeff Sharkey3359aca2011-11-08 18:08:48 -0800608 pw.print(" ["); pw.print(i); pw.print("]");
609 pw.print(" iface="); pw.print(iface[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700610 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700611 pw.print(" set="); pw.print(setToString(set[i]));
612 pw.print(" tag="); pw.print(tagToString(tag[i]));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700613 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700614 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700615 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700616 pw.print(" txPackets="); pw.print(txPackets[i]);
617 pw.print(" operations="); pw.println(operations[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700618 }
619 }
620
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700621 /**
622 * Return text description of {@link #set} value.
623 */
624 public static String setToString(int set) {
625 switch (set) {
626 case SET_ALL:
627 return "ALL";
628 case SET_DEFAULT:
629 return "DEFAULT";
630 case SET_FOREGROUND:
631 return "FOREGROUND";
632 default:
633 return "UNKNOWN";
634 }
635 }
636
637 /**
638 * Return text description of {@link #tag} value.
639 */
640 public static String tagToString(int tag) {
641 return "0x" + Integer.toHexString(tag);
642 }
643
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700644 @Override
645 public String toString() {
646 final CharArrayWriter writer = new CharArrayWriter();
647 dump("", new PrintWriter(writer));
648 return writer.toString();
649 }
650
651 /** {@inheritDoc} */
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700652 public int describeContents() {
653 return 0;
654 }
655
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700656 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
657 public NetworkStats createFromParcel(Parcel in) {
658 return new NetworkStats(in);
659 }
660
661 public NetworkStats[] newArray(int size) {
662 return new NetworkStats[size];
663 }
664 };
Jeff Sharkey163e6442011-10-31 16:37:52 -0700665
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800666 public interface NonMonotonicObserver {
667 public void foundNonMonotonic(
668 NetworkStats left, int leftIndex, NetworkStats right, int rightIndex);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700669 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700670}