blob: fb7a4f8ad216b9e704041908902ee8b51b924a04 [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
Jeff Sharkey63abc372012-01-11 18:38:16 -0800105 public boolean isNegative() {
106 return rxBytes < 0 || rxPackets < 0 || txBytes < 0 || txPackets < 0 || operations < 0;
107 }
108
109 public boolean isEmpty() {
110 return rxBytes == 0 && rxPackets == 0 && txBytes == 0 && txPackets == 0
111 && operations == 0;
112 }
113
Jeff Sharkey70c70532012-05-16 14:51:19 -0700114 public void add(Entry another) {
115 this.rxBytes += another.rxBytes;
116 this.rxPackets += another.rxPackets;
117 this.txBytes += another.txBytes;
118 this.txPackets += another.txPackets;
119 this.operations += another.operations;
120 }
121
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700122 @Override
123 public String toString() {
124 final StringBuilder builder = new StringBuilder();
125 builder.append("iface=").append(iface);
126 builder.append(" uid=").append(uid);
127 builder.append(" set=").append(setToString(set));
128 builder.append(" tag=").append(tagToString(tag));
129 builder.append(" rxBytes=").append(rxBytes);
130 builder.append(" rxPackets=").append(rxPackets);
131 builder.append(" txBytes=").append(txBytes);
132 builder.append(" txPackets=").append(txPackets);
133 builder.append(" operations=").append(operations);
134 return builder.toString();
135 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700136 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700137
Jeff Sharkey4a971222011-06-11 22:16:55 -0700138 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700139 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700140 this.size = 0;
141 this.iface = new String[initialSize];
142 this.uid = new int[initialSize];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700143 this.set = new int[initialSize];
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700144 this.tag = new int[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700145 this.rxBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700146 this.rxPackets = new long[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700147 this.txBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700148 this.txPackets = new long[initialSize];
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700149 this.operations = new long[initialSize];
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700150 }
151
152 public NetworkStats(Parcel parcel) {
153 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700154 size = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700155 iface = parcel.createStringArray();
156 uid = parcel.createIntArray();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700157 set = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700158 tag = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700159 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700160 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700161 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700162 txPackets = parcel.createLongArray();
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700163 operations = parcel.createLongArray();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700164 }
165
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700166 @Override
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700167 public void writeToParcel(Parcel dest, int flags) {
168 dest.writeLong(elapsedRealtime);
169 dest.writeInt(size);
170 dest.writeStringArray(iface);
171 dest.writeIntArray(uid);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700172 dest.writeIntArray(set);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700173 dest.writeIntArray(tag);
174 dest.writeLongArray(rxBytes);
175 dest.writeLongArray(rxPackets);
176 dest.writeLongArray(txBytes);
177 dest.writeLongArray(txPackets);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700178 dest.writeLongArray(operations);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700179 }
180
Jeff Sharkey4abb1b82011-11-08 17:35:28 -0800181 @Override
182 public NetworkStats clone() {
183 final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
184 NetworkStats.Entry entry = null;
185 for (int i = 0; i < size; i++) {
186 entry = getValues(i, entry);
187 clone.addValues(entry);
188 }
189 return clone;
190 }
191
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700192 // @VisibleForTesting
193 public NetworkStats addIfaceValues(
194 String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
195 return addValues(
196 iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700197 }
198
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700199 // @VisibleForTesting
200 public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
201 long rxPackets, long txBytes, long txPackets, long operations) {
202 return addValues(new Entry(
203 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700204 }
205
206 /**
207 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
208 * object can be recycled across multiple calls.
209 */
210 public NetworkStats addValues(Entry entry) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700211 if (size >= this.iface.length) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700212 final int newLength = Math.max(iface.length, 10) * 3 / 2;
213 iface = Arrays.copyOf(iface, newLength);
214 uid = Arrays.copyOf(uid, newLength);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700215 set = Arrays.copyOf(set, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700216 tag = Arrays.copyOf(tag, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700217 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700218 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700219 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700220 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700221 operations = Arrays.copyOf(operations, newLength);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700222 }
223
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700224 iface[size] = entry.iface;
225 uid[size] = entry.uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700226 set[size] = entry.set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700227 tag[size] = entry.tag;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700228 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700229 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700230 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700231 txPackets[size] = entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700232 operations[size] = entry.operations;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700233 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700234
Jeff Sharkey4a971222011-06-11 22:16:55 -0700235 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700236 }
237
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700238 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700239 * Return specific stats entry.
240 */
241 public Entry getValues(int i, Entry recycle) {
242 final Entry entry = recycle != null ? recycle : new Entry();
243 entry.iface = iface[i];
244 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700245 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700246 entry.tag = tag[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700247 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700248 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700249 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700250 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700251 entry.operations = operations[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700252 return entry;
253 }
254
255 public long getElapsedRealtime() {
256 return elapsedRealtime;
257 }
258
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700259 /**
260 * Return age of this {@link NetworkStats} object with respect to
261 * {@link SystemClock#elapsedRealtime()}.
262 */
263 public long getElapsedRealtimeAge() {
264 return SystemClock.elapsedRealtime() - elapsedRealtime;
265 }
266
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700267 public int size() {
268 return size;
269 }
270
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700271 // @VisibleForTesting
272 public int internalSize() {
273 return iface.length;
274 }
275
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700276 @Deprecated
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700277 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700278 long txBytes, long txPackets, long operations) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700279 return combineValues(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700280 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations);
281 }
282
283 public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes,
284 long rxPackets, long txBytes, long txPackets, long operations) {
285 return combineValues(new Entry(
286 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700287 }
288
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700289 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700290 * Combine given values with an existing row, or create a new row if
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700291 * {@link #findIndex(String, int, int, int)} is unable to find match. Can
292 * also be used to subtract values from existing rows.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700293 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700294 public NetworkStats combineValues(Entry entry) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700295 final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700296 if (i == -1) {
297 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700298 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700299 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700300 rxBytes[i] += entry.rxBytes;
301 rxPackets[i] += entry.rxPackets;
302 txBytes[i] += entry.txBytes;
303 txPackets[i] += entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700304 operations[i] += entry.operations;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700305 }
306 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700307 }
308
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700309 /**
Jeff Sharkey905b5892011-09-30 15:19:49 -0700310 * Combine all values from another {@link NetworkStats} into this object.
311 */
312 public void combineAllValues(NetworkStats another) {
313 NetworkStats.Entry entry = null;
314 for (int i = 0; i < another.size; i++) {
315 entry = another.getValues(i, entry);
316 combineValues(entry);
317 }
318 }
319
320 /**
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700321 * Find first stats index that matches the requested parameters.
322 */
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700323 public int findIndex(String iface, int uid, int set, int tag) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700324 for (int i = 0; i < size; i++) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700325 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
326 && Objects.equal(iface, this.iface[i])) {
327 return i;
328 }
329 }
330 return -1;
331 }
332
333 /**
334 * Find first stats index that matches the requested parameters, starting
335 * search around the hinted index as an optimization.
336 */
337 // @VisibleForTesting
338 public int findIndexHinted(String iface, int uid, int set, int tag, int hintIndex) {
339 for (int offset = 0; offset < size; offset++) {
340 final int halfOffset = offset / 2;
341
342 // search outwards from hint index, alternating forward and backward
343 final int i;
344 if (offset % 2 == 0) {
345 i = (hintIndex + halfOffset) % size;
346 } else {
347 i = (size + hintIndex - halfOffset - 1) % size;
348 }
349
350 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
351 && Objects.equal(iface, this.iface[i])) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700352 return i;
353 }
354 }
355 return -1;
356 }
357
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700358 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700359 * Splice in {@link #operations} from the given {@link NetworkStats} based
360 * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
361 * since operation counts are at data layer.
362 */
363 public void spliceOperationsFrom(NetworkStats stats) {
364 for (int i = 0; i < size; i++) {
Jeff Sharkey21a54782012-04-09 10:27:55 -0700365 final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700366 if (j == -1) {
367 operations[i] = 0;
368 } else {
369 operations[i] = stats.operations[j];
370 }
371 }
372 }
373
374 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700375 * Return list of unique interfaces known by this data structure.
376 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700377 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700378 final HashSet<String> ifaces = new HashSet<String>();
379 for (String iface : this.iface) {
380 if (iface != IFACE_ALL) {
381 ifaces.add(iface);
382 }
383 }
384 return ifaces.toArray(new String[ifaces.size()]);
385 }
386
387 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700388 * Return list of unique UIDs known by this data structure.
389 */
390 public int[] getUniqueUids() {
391 final SparseBooleanArray uids = new SparseBooleanArray();
392 for (int uid : this.uid) {
393 uids.put(uid, true);
394 }
395
396 final int size = uids.size();
397 final int[] result = new int[size];
398 for (int i = 0; i < size; i++) {
399 result[i] = uids.keyAt(i);
400 }
401 return result;
402 }
403
404 /**
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700405 * Return total bytes represented by this snapshot object, usually used when
406 * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
407 */
408 public long getTotalBytes() {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700409 final Entry entry = getTotal(null);
410 return entry.rxBytes + entry.txBytes;
411 }
412
413 /**
414 * Return total of all fields represented by this snapshot object.
415 */
416 public Entry getTotal(Entry recycle) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800417 return getTotal(recycle, null, UID_ALL, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700418 }
419
420 /**
421 * Return total of all fields represented by this snapshot object matching
422 * the requested {@link #uid}.
423 */
424 public Entry getTotal(Entry recycle, int limitUid) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800425 return getTotal(recycle, null, limitUid, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700426 }
427
428 /**
429 * Return total of all fields represented by this snapshot object matching
430 * the requested {@link #iface}.
431 */
432 public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800433 return getTotal(recycle, limitIface, UID_ALL, false);
434 }
435
436 public Entry getTotalIncludingTags(Entry recycle) {
437 return getTotal(recycle, null, UID_ALL, true);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700438 }
439
440 /**
441 * Return total of all fields represented by this snapshot object matching
442 * the requested {@link #iface} and {@link #uid}.
443 *
444 * @param limitIface Set of {@link #iface} to include in total; or {@code
445 * null} to include all ifaces.
446 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800447 private Entry getTotal(
448 Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags) {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700449 final Entry entry = recycle != null ? recycle : new Entry();
450
451 entry.iface = IFACE_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700452 entry.uid = limitUid;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700453 entry.set = SET_ALL;
454 entry.tag = TAG_NONE;
455 entry.rxBytes = 0;
456 entry.rxPackets = 0;
457 entry.txBytes = 0;
458 entry.txPackets = 0;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700459 entry.operations = 0;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700460
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700461 for (int i = 0; i < size; i++) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700462 final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]);
463 final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i]));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700464
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700465 if (matchesUid && matchesIface) {
466 // skip specific tags, since already counted in TAG_NONE
Jeff Sharkey63abc372012-01-11 18:38:16 -0800467 if (tag[i] != TAG_NONE && !includeTags) continue;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700468
469 entry.rxBytes += rxBytes[i];
470 entry.rxPackets += rxPackets[i];
471 entry.txBytes += txBytes[i];
472 entry.txPackets += txPackets[i];
473 entry.operations += operations[i];
474 }
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700475 }
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700476 return entry;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700477 }
478
479 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700480 * Subtract the given {@link NetworkStats}, effectively leaving the delta
481 * between two snapshots in time. Assumes that statistics rows collect over
482 * time, and that none of them have disappeared.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700483 */
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800484 public NetworkStats subtract(NetworkStats right) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800485 return subtract(this, right, null, null);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800486 }
487
488 /**
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800489 * Subtract the two given {@link NetworkStats} objects, returning the delta
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800490 * between two snapshots in time. Assumes that statistics rows collect over
491 * time, and that none of them have disappeared.
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800492 * <p>
493 * If counters have rolled backwards, they are clamped to {@code 0} and
494 * reported to the given {@link NonMonotonicObserver}.
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800495 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800496 public static <C> NetworkStats subtract(
497 NetworkStats left, NetworkStats right, NonMonotonicObserver<C> observer, C cookie) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800498 long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700499 if (deltaRealtime < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800500 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800501 observer.foundNonMonotonic(left, -1, right, -1, cookie);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800502 }
503 deltaRealtime = 0;
Jeff Sharkey75279902011-05-24 18:39:45 -0700504 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700505
Jeff Sharkey75279902011-05-24 18:39:45 -0700506 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700507 final Entry entry = new Entry();
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800508 final NetworkStats result = new NetworkStats(deltaRealtime, left.size);
509 for (int i = 0; i < left.size; i++) {
510 entry.iface = left.iface[i];
511 entry.uid = left.uid[i];
512 entry.set = left.set[i];
513 entry.tag = left.tag[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700514
515 // find remote row that matches, and subtract
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800516 final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag, i);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700517 if (j == -1) {
518 // newly appearing row, return entire value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800519 entry.rxBytes = left.rxBytes[i];
520 entry.rxPackets = left.rxPackets[i];
521 entry.txBytes = left.txBytes[i];
522 entry.txPackets = left.txPackets[i];
523 entry.operations = left.operations[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700524 } else {
525 // existing row, subtract remote value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800526 entry.rxBytes = left.rxBytes[i] - right.rxBytes[j];
527 entry.rxPackets = left.rxPackets[i] - right.rxPackets[j];
528 entry.txBytes = left.txBytes[i] - right.txBytes[j];
529 entry.txPackets = left.txPackets[i] - right.txPackets[j];
530 entry.operations = left.operations[i] - right.operations[j];
Jeff Sharkey163e6442011-10-31 16:37:52 -0700531
532 if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
533 || entry.txPackets < 0 || entry.operations < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800534 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800535 observer.foundNonMonotonic(left, i, right, j, cookie);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800536 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800537 entry.rxBytes = Math.max(entry.rxBytes, 0);
538 entry.rxPackets = Math.max(entry.rxPackets, 0);
539 entry.txBytes = Math.max(entry.txBytes, 0);
540 entry.txPackets = Math.max(entry.txPackets, 0);
541 entry.operations = Math.max(entry.operations, 0);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700542 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700543 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700544
545 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700546 }
547
Jeff Sharkey4a971222011-06-11 22:16:55 -0700548 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700549 }
550
Jeff Sharkey905b5892011-09-30 15:19:49 -0700551 /**
552 * Return total statistics grouped by {@link #iface}; doesn't mutate the
553 * original structure.
554 */
555 public NetworkStats groupedByIface() {
556 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
557
558 final Entry entry = new Entry();
559 entry.uid = UID_ALL;
560 entry.set = SET_ALL;
561 entry.tag = TAG_NONE;
562 entry.operations = 0L;
563
564 for (int i = 0; i < size; i++) {
565 // skip specific tags, since already counted in TAG_NONE
566 if (tag[i] != TAG_NONE) continue;
567
568 entry.iface = iface[i];
569 entry.rxBytes = rxBytes[i];
570 entry.rxPackets = rxPackets[i];
571 entry.txBytes = txBytes[i];
572 entry.txPackets = txPackets[i];
573 stats.combineValues(entry);
574 }
575
576 return stats;
577 }
578
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700579 /**
580 * Return total statistics grouped by {@link #uid}; doesn't mutate the
581 * original structure.
582 */
583 public NetworkStats groupedByUid() {
584 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
585
586 final Entry entry = new Entry();
587 entry.iface = IFACE_ALL;
588 entry.set = SET_ALL;
589 entry.tag = TAG_NONE;
590
591 for (int i = 0; i < size; i++) {
592 // skip specific tags, since already counted in TAG_NONE
593 if (tag[i] != TAG_NONE) continue;
594
595 entry.uid = uid[i];
596 entry.rxBytes = rxBytes[i];
597 entry.rxPackets = rxPackets[i];
598 entry.txBytes = txBytes[i];
599 entry.txPackets = txPackets[i];
600 entry.operations = operations[i];
601 stats.combineValues(entry);
602 }
603
604 return stats;
605 }
606
Jeff Sharkey163e6442011-10-31 16:37:52 -0700607 /**
608 * Return all rows except those attributed to the requested UID; doesn't
609 * mutate the original structure.
610 */
611 public NetworkStats withoutUid(int uid) {
612 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
613
614 Entry entry = new Entry();
615 for (int i = 0; i < size; i++) {
616 entry = getValues(i, entry);
617 if (entry.uid != uid) {
618 stats.addValues(entry);
619 }
620 }
621
622 return stats;
623 }
624
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700625 public void dump(String prefix, PrintWriter pw) {
626 pw.print(prefix);
627 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700628 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700629 pw.print(prefix);
Jeff Sharkey3359aca2011-11-08 18:08:48 -0800630 pw.print(" ["); pw.print(i); pw.print("]");
631 pw.print(" iface="); pw.print(iface[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700632 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700633 pw.print(" set="); pw.print(setToString(set[i]));
634 pw.print(" tag="); pw.print(tagToString(tag[i]));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700635 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700636 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700637 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700638 pw.print(" txPackets="); pw.print(txPackets[i]);
639 pw.print(" operations="); pw.println(operations[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700640 }
641 }
642
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700643 /**
644 * Return text description of {@link #set} value.
645 */
646 public static String setToString(int set) {
647 switch (set) {
648 case SET_ALL:
649 return "ALL";
650 case SET_DEFAULT:
651 return "DEFAULT";
652 case SET_FOREGROUND:
653 return "FOREGROUND";
654 default:
655 return "UNKNOWN";
656 }
657 }
658
659 /**
660 * Return text description of {@link #tag} value.
661 */
662 public static String tagToString(int tag) {
663 return "0x" + Integer.toHexString(tag);
664 }
665
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700666 @Override
667 public String toString() {
668 final CharArrayWriter writer = new CharArrayWriter();
669 dump("", new PrintWriter(writer));
670 return writer.toString();
671 }
672
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700673 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700674 public int describeContents() {
675 return 0;
676 }
677
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700678 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700679 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700680 public NetworkStats createFromParcel(Parcel in) {
681 return new NetworkStats(in);
682 }
683
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700684 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700685 public NetworkStats[] newArray(int size) {
686 return new NetworkStats[size];
687 }
688 };
Jeff Sharkey163e6442011-10-31 16:37:52 -0700689
Jeff Sharkey63abc372012-01-11 18:38:16 -0800690 public interface NonMonotonicObserver<C> {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800691 public void foundNonMonotonic(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800692 NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700693 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700694}