blob: c7576057e196d7a783dffe626a254b56aa1f48ad [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 Sharkeyfd8be3e2011-07-11 14:36:15 -0700138 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700139
Jeff Sharkey4a971222011-06-11 22:16:55 -0700140 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700141 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700142 this.size = 0;
143 this.iface = new String[initialSize];
144 this.uid = new int[initialSize];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700145 this.set = new int[initialSize];
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700146 this.tag = new int[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700147 this.rxBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700148 this.rxPackets = new long[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700149 this.txBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700150 this.txPackets = new long[initialSize];
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700151 this.operations = new long[initialSize];
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700152 }
153
154 public NetworkStats(Parcel parcel) {
155 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700156 size = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700157 iface = parcel.createStringArray();
158 uid = parcel.createIntArray();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700159 set = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700160 tag = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700161 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700162 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700163 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700164 txPackets = parcel.createLongArray();
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700165 operations = parcel.createLongArray();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700166 }
167
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700168 @Override
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700169 public void writeToParcel(Parcel dest, int flags) {
170 dest.writeLong(elapsedRealtime);
171 dest.writeInt(size);
172 dest.writeStringArray(iface);
173 dest.writeIntArray(uid);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700174 dest.writeIntArray(set);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700175 dest.writeIntArray(tag);
176 dest.writeLongArray(rxBytes);
177 dest.writeLongArray(rxPackets);
178 dest.writeLongArray(txBytes);
179 dest.writeLongArray(txPackets);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700180 dest.writeLongArray(operations);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700181 }
182
Jeff Sharkey4abb1b82011-11-08 17:35:28 -0800183 @Override
184 public NetworkStats clone() {
185 final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
186 NetworkStats.Entry entry = null;
187 for (int i = 0; i < size; i++) {
188 entry = getValues(i, entry);
189 clone.addValues(entry);
190 }
191 return clone;
192 }
193
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800194 @VisibleForTesting
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700195 public NetworkStats addIfaceValues(
196 String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
197 return addValues(
198 iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700199 }
200
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800201 @VisibleForTesting
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700202 public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
203 long rxPackets, long txBytes, long txPackets, long operations) {
204 return addValues(new Entry(
205 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700206 }
207
208 /**
209 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
210 * object can be recycled across multiple calls.
211 */
212 public NetworkStats addValues(Entry entry) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700213 if (size >= this.iface.length) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700214 final int newLength = Math.max(iface.length, 10) * 3 / 2;
215 iface = Arrays.copyOf(iface, newLength);
216 uid = Arrays.copyOf(uid, newLength);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700217 set = Arrays.copyOf(set, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700218 tag = Arrays.copyOf(tag, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700219 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700220 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700221 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700222 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700223 operations = Arrays.copyOf(operations, newLength);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700224 }
225
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700226 iface[size] = entry.iface;
227 uid[size] = entry.uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700228 set[size] = entry.set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700229 tag[size] = entry.tag;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700230 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700231 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700232 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700233 txPackets[size] = entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700234 operations[size] = entry.operations;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700235 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700236
Jeff Sharkey4a971222011-06-11 22:16:55 -0700237 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700238 }
239
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700240 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700241 * Return specific stats entry.
242 */
243 public Entry getValues(int i, Entry recycle) {
244 final Entry entry = recycle != null ? recycle : new Entry();
245 entry.iface = iface[i];
246 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700247 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700248 entry.tag = tag[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700249 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700250 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700251 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700252 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700253 entry.operations = operations[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700254 return entry;
255 }
256
257 public long getElapsedRealtime() {
258 return elapsedRealtime;
259 }
260
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700261 /**
262 * Return age of this {@link NetworkStats} object with respect to
263 * {@link SystemClock#elapsedRealtime()}.
264 */
265 public long getElapsedRealtimeAge() {
266 return SystemClock.elapsedRealtime() - elapsedRealtime;
267 }
268
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700269 public int size() {
270 return size;
271 }
272
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800273 @VisibleForTesting
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700274 public int internalSize() {
275 return iface.length;
276 }
277
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700278 @Deprecated
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700279 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700280 long txBytes, long txPackets, long operations) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700281 return combineValues(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700282 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations);
283 }
284
285 public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes,
286 long rxPackets, long txBytes, long txPackets, long operations) {
287 return combineValues(new Entry(
288 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700289 }
290
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700291 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700292 * Combine given values with an existing row, or create a new row if
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700293 * {@link #findIndex(String, int, int, int)} is unable to find match. Can
294 * also be used to subtract values from existing rows.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700295 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700296 public NetworkStats combineValues(Entry entry) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700297 final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700298 if (i == -1) {
299 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700300 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700301 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700302 rxBytes[i] += entry.rxBytes;
303 rxPackets[i] += entry.rxPackets;
304 txBytes[i] += entry.txBytes;
305 txPackets[i] += entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700306 operations[i] += entry.operations;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700307 }
308 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700309 }
310
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700311 /**
Jeff Sharkey905b5892011-09-30 15:19:49 -0700312 * Combine all values from another {@link NetworkStats} into this object.
313 */
314 public void combineAllValues(NetworkStats another) {
315 NetworkStats.Entry entry = null;
316 for (int i = 0; i < another.size; i++) {
317 entry = another.getValues(i, entry);
318 combineValues(entry);
319 }
320 }
321
322 /**
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700323 * Find first stats index that matches the requested parameters.
324 */
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700325 public int findIndex(String iface, int uid, int set, int tag) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700326 for (int i = 0; i < size; i++) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700327 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
328 && Objects.equal(iface, this.iface[i])) {
329 return i;
330 }
331 }
332 return -1;
333 }
334
335 /**
336 * Find first stats index that matches the requested parameters, starting
337 * search around the hinted index as an optimization.
338 */
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800339 @VisibleForTesting
Jeff Sharkey163e6442011-10-31 16:37:52 -0700340 public int findIndexHinted(String iface, int uid, int set, int tag, int hintIndex) {
341 for (int offset = 0; offset < size; offset++) {
342 final int halfOffset = offset / 2;
343
344 // search outwards from hint index, alternating forward and backward
345 final int i;
346 if (offset % 2 == 0) {
347 i = (hintIndex + halfOffset) % size;
348 } else {
349 i = (size + hintIndex - halfOffset - 1) % size;
350 }
351
352 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
353 && Objects.equal(iface, this.iface[i])) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700354 return i;
355 }
356 }
357 return -1;
358 }
359
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700360 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700361 * Splice in {@link #operations} from the given {@link NetworkStats} based
362 * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
363 * since operation counts are at data layer.
364 */
365 public void spliceOperationsFrom(NetworkStats stats) {
366 for (int i = 0; i < size; i++) {
Jeff Sharkey21a54782012-04-09 10:27:55 -0700367 final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700368 if (j == -1) {
369 operations[i] = 0;
370 } else {
371 operations[i] = stats.operations[j];
372 }
373 }
374 }
375
376 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700377 * Return list of unique interfaces known by this data structure.
378 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700379 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700380 final HashSet<String> ifaces = new HashSet<String>();
381 for (String iface : this.iface) {
382 if (iface != IFACE_ALL) {
383 ifaces.add(iface);
384 }
385 }
386 return ifaces.toArray(new String[ifaces.size()]);
387 }
388
389 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700390 * Return list of unique UIDs known by this data structure.
391 */
392 public int[] getUniqueUids() {
393 final SparseBooleanArray uids = new SparseBooleanArray();
394 for (int uid : this.uid) {
395 uids.put(uid, true);
396 }
397
398 final int size = uids.size();
399 final int[] result = new int[size];
400 for (int i = 0; i < size; i++) {
401 result[i] = uids.keyAt(i);
402 }
403 return result;
404 }
405
406 /**
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700407 * Return total bytes represented by this snapshot object, usually used when
408 * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
409 */
410 public long getTotalBytes() {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700411 final Entry entry = getTotal(null);
412 return entry.rxBytes + entry.txBytes;
413 }
414
415 /**
416 * Return total of all fields represented by this snapshot object.
417 */
418 public Entry getTotal(Entry recycle) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800419 return getTotal(recycle, null, UID_ALL, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700420 }
421
422 /**
423 * Return total of all fields represented by this snapshot object matching
424 * the requested {@link #uid}.
425 */
426 public Entry getTotal(Entry recycle, int limitUid) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800427 return getTotal(recycle, null, limitUid, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700428 }
429
430 /**
431 * Return total of all fields represented by this snapshot object matching
432 * the requested {@link #iface}.
433 */
434 public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800435 return getTotal(recycle, limitIface, UID_ALL, false);
436 }
437
438 public Entry getTotalIncludingTags(Entry recycle) {
439 return getTotal(recycle, null, UID_ALL, true);
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} and {@link #uid}.
445 *
446 * @param limitIface Set of {@link #iface} to include in total; or {@code
447 * null} to include all ifaces.
448 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800449 private Entry getTotal(
450 Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags) {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700451 final Entry entry = recycle != null ? recycle : new Entry();
452
453 entry.iface = IFACE_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700454 entry.uid = limitUid;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700455 entry.set = SET_ALL;
456 entry.tag = TAG_NONE;
457 entry.rxBytes = 0;
458 entry.rxPackets = 0;
459 entry.txBytes = 0;
460 entry.txPackets = 0;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700461 entry.operations = 0;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700462
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700463 for (int i = 0; i < size; i++) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700464 final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]);
465 final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i]));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700466
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700467 if (matchesUid && matchesIface) {
468 // skip specific tags, since already counted in TAG_NONE
Jeff Sharkey63abc372012-01-11 18:38:16 -0800469 if (tag[i] != TAG_NONE && !includeTags) continue;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700470
471 entry.rxBytes += rxBytes[i];
472 entry.rxPackets += rxPackets[i];
473 entry.txBytes += txBytes[i];
474 entry.txPackets += txPackets[i];
475 entry.operations += operations[i];
476 }
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700477 }
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700478 return entry;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700479 }
480
481 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700482 * Subtract the given {@link NetworkStats}, effectively leaving the delta
483 * between two snapshots in time. Assumes that statistics rows collect over
484 * time, and that none of them have disappeared.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700485 */
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800486 public NetworkStats subtract(NetworkStats right) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800487 return subtract(this, right, null, null);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800488 }
489
490 /**
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800491 * Subtract the two given {@link NetworkStats} objects, returning the delta
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800492 * between two snapshots in time. Assumes that statistics rows collect over
493 * time, and that none of them have disappeared.
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800494 * <p>
495 * If counters have rolled backwards, they are clamped to {@code 0} and
496 * reported to the given {@link NonMonotonicObserver}.
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800497 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800498 public static <C> NetworkStats subtract(
499 NetworkStats left, NetworkStats right, NonMonotonicObserver<C> observer, C cookie) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800500 long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700501 if (deltaRealtime < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800502 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800503 observer.foundNonMonotonic(left, -1, right, -1, cookie);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800504 }
505 deltaRealtime = 0;
Jeff Sharkey75279902011-05-24 18:39:45 -0700506 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700507
Jeff Sharkey75279902011-05-24 18:39:45 -0700508 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700509 final Entry entry = new Entry();
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800510 final NetworkStats result = new NetworkStats(deltaRealtime, left.size);
511 for (int i = 0; i < left.size; i++) {
512 entry.iface = left.iface[i];
513 entry.uid = left.uid[i];
514 entry.set = left.set[i];
515 entry.tag = left.tag[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700516
517 // find remote row that matches, and subtract
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800518 final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag, i);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700519 if (j == -1) {
520 // newly appearing row, return entire value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800521 entry.rxBytes = left.rxBytes[i];
522 entry.rxPackets = left.rxPackets[i];
523 entry.txBytes = left.txBytes[i];
524 entry.txPackets = left.txPackets[i];
525 entry.operations = left.operations[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700526 } else {
527 // existing row, subtract remote value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800528 entry.rxBytes = left.rxBytes[i] - right.rxBytes[j];
529 entry.rxPackets = left.rxPackets[i] - right.rxPackets[j];
530 entry.txBytes = left.txBytes[i] - right.txBytes[j];
531 entry.txPackets = left.txPackets[i] - right.txPackets[j];
532 entry.operations = left.operations[i] - right.operations[j];
Jeff Sharkey163e6442011-10-31 16:37:52 -0700533
534 if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
535 || entry.txPackets < 0 || entry.operations < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800536 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800537 observer.foundNonMonotonic(left, i, right, j, cookie);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800538 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800539 entry.rxBytes = Math.max(entry.rxBytes, 0);
540 entry.rxPackets = Math.max(entry.rxPackets, 0);
541 entry.txBytes = Math.max(entry.txBytes, 0);
542 entry.txPackets = Math.max(entry.txPackets, 0);
543 entry.operations = Math.max(entry.operations, 0);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700544 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700545 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700546
547 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700548 }
549
Jeff Sharkey4a971222011-06-11 22:16:55 -0700550 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700551 }
552
Jeff Sharkey905b5892011-09-30 15:19:49 -0700553 /**
554 * Return total statistics grouped by {@link #iface}; doesn't mutate the
555 * original structure.
556 */
557 public NetworkStats groupedByIface() {
558 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
559
560 final Entry entry = new Entry();
561 entry.uid = UID_ALL;
562 entry.set = SET_ALL;
563 entry.tag = TAG_NONE;
564 entry.operations = 0L;
565
566 for (int i = 0; i < size; i++) {
567 // skip specific tags, since already counted in TAG_NONE
568 if (tag[i] != TAG_NONE) continue;
569
570 entry.iface = iface[i];
571 entry.rxBytes = rxBytes[i];
572 entry.rxPackets = rxPackets[i];
573 entry.txBytes = txBytes[i];
574 entry.txPackets = txPackets[i];
575 stats.combineValues(entry);
576 }
577
578 return stats;
579 }
580
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700581 /**
582 * Return total statistics grouped by {@link #uid}; doesn't mutate the
583 * original structure.
584 */
585 public NetworkStats groupedByUid() {
586 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
587
588 final Entry entry = new Entry();
589 entry.iface = IFACE_ALL;
590 entry.set = SET_ALL;
591 entry.tag = TAG_NONE;
592
593 for (int i = 0; i < size; i++) {
594 // skip specific tags, since already counted in TAG_NONE
595 if (tag[i] != TAG_NONE) continue;
596
597 entry.uid = uid[i];
598 entry.rxBytes = rxBytes[i];
599 entry.rxPackets = rxPackets[i];
600 entry.txBytes = txBytes[i];
601 entry.txPackets = txPackets[i];
602 entry.operations = operations[i];
603 stats.combineValues(entry);
604 }
605
606 return stats;
607 }
608
Jeff Sharkey163e6442011-10-31 16:37:52 -0700609 /**
610 * Return all rows except those attributed to the requested UID; doesn't
611 * mutate the original structure.
612 */
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700613 public NetworkStats withoutUids(int[] uids) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700614 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
615
616 Entry entry = new Entry();
617 for (int i = 0; i < size; i++) {
618 entry = getValues(i, entry);
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700619 if (!ArrayUtils.contains(uids, entry.uid)) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700620 stats.addValues(entry);
621 }
622 }
623
624 return stats;
625 }
626
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700627 public void dump(String prefix, PrintWriter pw) {
628 pw.print(prefix);
629 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700630 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700631 pw.print(prefix);
Jeff Sharkey3359aca2011-11-08 18:08:48 -0800632 pw.print(" ["); pw.print(i); pw.print("]");
633 pw.print(" iface="); pw.print(iface[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700634 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700635 pw.print(" set="); pw.print(setToString(set[i]));
636 pw.print(" tag="); pw.print(tagToString(tag[i]));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700637 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700638 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700639 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700640 pw.print(" txPackets="); pw.print(txPackets[i]);
641 pw.print(" operations="); pw.println(operations[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700642 }
643 }
644
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700645 /**
646 * Return text description of {@link #set} value.
647 */
648 public static String setToString(int set) {
649 switch (set) {
650 case SET_ALL:
651 return "ALL";
652 case SET_DEFAULT:
653 return "DEFAULT";
654 case SET_FOREGROUND:
655 return "FOREGROUND";
656 default:
657 return "UNKNOWN";
658 }
659 }
660
661 /**
662 * Return text description of {@link #tag} value.
663 */
664 public static String tagToString(int tag) {
665 return "0x" + Integer.toHexString(tag);
666 }
667
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700668 @Override
669 public String toString() {
670 final CharArrayWriter writer = new CharArrayWriter();
671 dump("", new PrintWriter(writer));
672 return writer.toString();
673 }
674
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700675 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700676 public int describeContents() {
677 return 0;
678 }
679
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700680 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700681 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700682 public NetworkStats createFromParcel(Parcel in) {
683 return new NetworkStats(in);
684 }
685
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700686 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700687 public NetworkStats[] newArray(int size) {
688 return new NetworkStats[size];
689 }
690 };
Jeff Sharkey163e6442011-10-31 16:37:52 -0700691
Jeff Sharkey63abc372012-01-11 18:38:16 -0800692 public interface NonMonotonicObserver<C> {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800693 public void foundNonMonotonic(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800694 NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700695 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700696}