blob: 7a1ef6623e5b5fe41a2263aa704e9e15cfe3d24b [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 Sharkeyb3d59572011-09-07 17:20:27 -0700114 @Override
115 public String toString() {
116 final StringBuilder builder = new StringBuilder();
117 builder.append("iface=").append(iface);
118 builder.append(" uid=").append(uid);
119 builder.append(" set=").append(setToString(set));
120 builder.append(" tag=").append(tagToString(tag));
121 builder.append(" rxBytes=").append(rxBytes);
122 builder.append(" rxPackets=").append(rxPackets);
123 builder.append(" txBytes=").append(txBytes);
124 builder.append(" txPackets=").append(txPackets);
125 builder.append(" operations=").append(operations);
126 return builder.toString();
127 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700128 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700129
Jeff Sharkey4a971222011-06-11 22:16:55 -0700130 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700131 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700132 this.size = 0;
133 this.iface = new String[initialSize];
134 this.uid = new int[initialSize];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700135 this.set = new int[initialSize];
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700136 this.tag = new int[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700137 this.rxBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700138 this.rxPackets = new long[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700139 this.txBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700140 this.txPackets = new long[initialSize];
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700141 this.operations = new long[initialSize];
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700142 }
143
144 public NetworkStats(Parcel parcel) {
145 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700146 size = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700147 iface = parcel.createStringArray();
148 uid = parcel.createIntArray();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700149 set = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700150 tag = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700151 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700152 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700153 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700154 txPackets = parcel.createLongArray();
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700155 operations = parcel.createLongArray();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700156 }
157
158 /** {@inheritDoc} */
159 public void writeToParcel(Parcel dest, int flags) {
160 dest.writeLong(elapsedRealtime);
161 dest.writeInt(size);
162 dest.writeStringArray(iface);
163 dest.writeIntArray(uid);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700164 dest.writeIntArray(set);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700165 dest.writeIntArray(tag);
166 dest.writeLongArray(rxBytes);
167 dest.writeLongArray(rxPackets);
168 dest.writeLongArray(txBytes);
169 dest.writeLongArray(txPackets);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700170 dest.writeLongArray(operations);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700171 }
172
Jeff Sharkey4abb1b82011-11-08 17:35:28 -0800173 @Override
174 public NetworkStats clone() {
175 final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
176 NetworkStats.Entry entry = null;
177 for (int i = 0; i < size; i++) {
178 entry = getValues(i, entry);
179 clone.addValues(entry);
180 }
181 return clone;
182 }
183
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700184 // @VisibleForTesting
185 public NetworkStats addIfaceValues(
186 String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
187 return addValues(
188 iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700189 }
190
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700191 // @VisibleForTesting
192 public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
193 long rxPackets, long txBytes, long txPackets, long operations) {
194 return addValues(new Entry(
195 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700196 }
197
198 /**
199 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
200 * object can be recycled across multiple calls.
201 */
202 public NetworkStats addValues(Entry entry) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700203 if (size >= this.iface.length) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700204 final int newLength = Math.max(iface.length, 10) * 3 / 2;
205 iface = Arrays.copyOf(iface, newLength);
206 uid = Arrays.copyOf(uid, newLength);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700207 set = Arrays.copyOf(set, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700208 tag = Arrays.copyOf(tag, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700209 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700210 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700211 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700212 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700213 operations = Arrays.copyOf(operations, newLength);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700214 }
215
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700216 iface[size] = entry.iface;
217 uid[size] = entry.uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700218 set[size] = entry.set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700219 tag[size] = entry.tag;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700220 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700221 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700222 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700223 txPackets[size] = entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700224 operations[size] = entry.operations;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700225 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700226
Jeff Sharkey4a971222011-06-11 22:16:55 -0700227 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700228 }
229
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700230 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700231 * Return specific stats entry.
232 */
233 public Entry getValues(int i, Entry recycle) {
234 final Entry entry = recycle != null ? recycle : new Entry();
235 entry.iface = iface[i];
236 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700237 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700238 entry.tag = tag[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700239 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700240 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700241 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700242 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700243 entry.operations = operations[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700244 return entry;
245 }
246
247 public long getElapsedRealtime() {
248 return elapsedRealtime;
249 }
250
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700251 /**
252 * Return age of this {@link NetworkStats} object with respect to
253 * {@link SystemClock#elapsedRealtime()}.
254 */
255 public long getElapsedRealtimeAge() {
256 return SystemClock.elapsedRealtime() - elapsedRealtime;
257 }
258
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700259 public int size() {
260 return size;
261 }
262
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700263 // @VisibleForTesting
264 public int internalSize() {
265 return iface.length;
266 }
267
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700268 @Deprecated
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700269 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700270 long txBytes, long txPackets, long operations) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700271 return combineValues(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700272 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations);
273 }
274
275 public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes,
276 long rxPackets, long txBytes, long txPackets, long operations) {
277 return combineValues(new Entry(
278 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700279 }
280
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700281 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700282 * Combine given values with an existing row, or create a new row if
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700283 * {@link #findIndex(String, int, int, int)} is unable to find match. Can
284 * also be used to subtract values from existing rows.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700285 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700286 public NetworkStats combineValues(Entry entry) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700287 final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700288 if (i == -1) {
289 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700290 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700291 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700292 rxBytes[i] += entry.rxBytes;
293 rxPackets[i] += entry.rxPackets;
294 txBytes[i] += entry.txBytes;
295 txPackets[i] += entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700296 operations[i] += entry.operations;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700297 }
298 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700299 }
300
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700301 /**
Jeff Sharkey905b5892011-09-30 15:19:49 -0700302 * Combine all values from another {@link NetworkStats} into this object.
303 */
304 public void combineAllValues(NetworkStats another) {
305 NetworkStats.Entry entry = null;
306 for (int i = 0; i < another.size; i++) {
307 entry = another.getValues(i, entry);
308 combineValues(entry);
309 }
310 }
311
312 /**
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700313 * Find first stats index that matches the requested parameters.
314 */
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700315 public int findIndex(String iface, int uid, int set, int tag) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700316 for (int i = 0; i < size; i++) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700317 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
318 && Objects.equal(iface, this.iface[i])) {
319 return i;
320 }
321 }
322 return -1;
323 }
324
325 /**
326 * Find first stats index that matches the requested parameters, starting
327 * search around the hinted index as an optimization.
328 */
329 // @VisibleForTesting
330 public int findIndexHinted(String iface, int uid, int set, int tag, int hintIndex) {
331 for (int offset = 0; offset < size; offset++) {
332 final int halfOffset = offset / 2;
333
334 // search outwards from hint index, alternating forward and backward
335 final int i;
336 if (offset % 2 == 0) {
337 i = (hintIndex + halfOffset) % size;
338 } else {
339 i = (size + hintIndex - halfOffset - 1) % size;
340 }
341
342 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
343 && Objects.equal(iface, this.iface[i])) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700344 return i;
345 }
346 }
347 return -1;
348 }
349
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700350 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700351 * Splice in {@link #operations} from the given {@link NetworkStats} based
352 * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
353 * since operation counts are at data layer.
354 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800355 @Deprecated
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700356 public void spliceOperationsFrom(NetworkStats stats) {
357 for (int i = 0; i < size; i++) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700358 final int j = stats.findIndex(IFACE_ALL, uid[i], set[i], tag[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700359 if (j == -1) {
360 operations[i] = 0;
361 } else {
362 operations[i] = stats.operations[j];
363 }
364 }
365 }
366
367 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700368 * Return list of unique interfaces known by this data structure.
369 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700370 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700371 final HashSet<String> ifaces = new HashSet<String>();
372 for (String iface : this.iface) {
373 if (iface != IFACE_ALL) {
374 ifaces.add(iface);
375 }
376 }
377 return ifaces.toArray(new String[ifaces.size()]);
378 }
379
380 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700381 * Return list of unique UIDs known by this data structure.
382 */
383 public int[] getUniqueUids() {
384 final SparseBooleanArray uids = new SparseBooleanArray();
385 for (int uid : this.uid) {
386 uids.put(uid, true);
387 }
388
389 final int size = uids.size();
390 final int[] result = new int[size];
391 for (int i = 0; i < size; i++) {
392 result[i] = uids.keyAt(i);
393 }
394 return result;
395 }
396
397 /**
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700398 * Return total bytes represented by this snapshot object, usually used when
399 * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
400 */
401 public long getTotalBytes() {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700402 final Entry entry = getTotal(null);
403 return entry.rxBytes + entry.txBytes;
404 }
405
406 /**
407 * Return total of all fields represented by this snapshot object.
408 */
409 public Entry getTotal(Entry recycle) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800410 return getTotal(recycle, null, UID_ALL, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700411 }
412
413 /**
414 * Return total of all fields represented by this snapshot object matching
415 * the requested {@link #uid}.
416 */
417 public Entry getTotal(Entry recycle, int limitUid) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800418 return getTotal(recycle, null, limitUid, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700419 }
420
421 /**
422 * Return total of all fields represented by this snapshot object matching
423 * the requested {@link #iface}.
424 */
425 public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800426 return getTotal(recycle, limitIface, UID_ALL, false);
427 }
428
429 public Entry getTotalIncludingTags(Entry recycle) {
430 return getTotal(recycle, null, UID_ALL, true);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700431 }
432
433 /**
434 * Return total of all fields represented by this snapshot object matching
435 * the requested {@link #iface} and {@link #uid}.
436 *
437 * @param limitIface Set of {@link #iface} to include in total; or {@code
438 * null} to include all ifaces.
439 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800440 private Entry getTotal(
441 Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags) {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700442 final Entry entry = recycle != null ? recycle : new Entry();
443
444 entry.iface = IFACE_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700445 entry.uid = limitUid;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700446 entry.set = SET_ALL;
447 entry.tag = TAG_NONE;
448 entry.rxBytes = 0;
449 entry.rxPackets = 0;
450 entry.txBytes = 0;
451 entry.txPackets = 0;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700452 entry.operations = 0;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700453
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700454 for (int i = 0; i < size; i++) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700455 final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]);
456 final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i]));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700457
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700458 if (matchesUid && matchesIface) {
459 // skip specific tags, since already counted in TAG_NONE
Jeff Sharkey63abc372012-01-11 18:38:16 -0800460 if (tag[i] != TAG_NONE && !includeTags) continue;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700461
462 entry.rxBytes += rxBytes[i];
463 entry.rxPackets += rxPackets[i];
464 entry.txBytes += txBytes[i];
465 entry.txPackets += txPackets[i];
466 entry.operations += operations[i];
467 }
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700468 }
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700469 return entry;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700470 }
471
472 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700473 * Subtract the given {@link NetworkStats}, effectively leaving the delta
474 * between two snapshots in time. Assumes that statistics rows collect over
475 * time, and that none of them have disappeared.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700476 */
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800477 public NetworkStats subtract(NetworkStats right) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800478 return subtract(this, right, null, null);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800479 }
480
481 /**
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800482 * Subtract the two given {@link NetworkStats} objects, returning the delta
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800483 * between two snapshots in time. Assumes that statistics rows collect over
484 * time, and that none of them have disappeared.
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800485 * <p>
486 * If counters have rolled backwards, they are clamped to {@code 0} and
487 * reported to the given {@link NonMonotonicObserver}.
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800488 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800489 public static <C> NetworkStats subtract(
490 NetworkStats left, NetworkStats right, NonMonotonicObserver<C> observer, C cookie) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800491 long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700492 if (deltaRealtime < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800493 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800494 observer.foundNonMonotonic(left, -1, right, -1, cookie);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800495 }
496 deltaRealtime = 0;
Jeff Sharkey75279902011-05-24 18:39:45 -0700497 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700498
Jeff Sharkey75279902011-05-24 18:39:45 -0700499 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700500 final Entry entry = new Entry();
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800501 final NetworkStats result = new NetworkStats(deltaRealtime, left.size);
502 for (int i = 0; i < left.size; i++) {
503 entry.iface = left.iface[i];
504 entry.uid = left.uid[i];
505 entry.set = left.set[i];
506 entry.tag = left.tag[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700507
508 // find remote row that matches, and subtract
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800509 final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag, i);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700510 if (j == -1) {
511 // newly appearing row, return entire value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800512 entry.rxBytes = left.rxBytes[i];
513 entry.rxPackets = left.rxPackets[i];
514 entry.txBytes = left.txBytes[i];
515 entry.txPackets = left.txPackets[i];
516 entry.operations = left.operations[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700517 } else {
518 // existing row, subtract remote value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800519 entry.rxBytes = left.rxBytes[i] - right.rxBytes[j];
520 entry.rxPackets = left.rxPackets[i] - right.rxPackets[j];
521 entry.txBytes = left.txBytes[i] - right.txBytes[j];
522 entry.txPackets = left.txPackets[i] - right.txPackets[j];
523 entry.operations = left.operations[i] - right.operations[j];
Jeff Sharkey163e6442011-10-31 16:37:52 -0700524
525 if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
526 || entry.txPackets < 0 || entry.operations < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800527 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800528 observer.foundNonMonotonic(left, i, right, j, cookie);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800529 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800530 entry.rxBytes = Math.max(entry.rxBytes, 0);
531 entry.rxPackets = Math.max(entry.rxPackets, 0);
532 entry.txBytes = Math.max(entry.txBytes, 0);
533 entry.txPackets = Math.max(entry.txPackets, 0);
534 entry.operations = Math.max(entry.operations, 0);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700535 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700536 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700537
538 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700539 }
540
Jeff Sharkey4a971222011-06-11 22:16:55 -0700541 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700542 }
543
Jeff Sharkey905b5892011-09-30 15:19:49 -0700544 /**
545 * Return total statistics grouped by {@link #iface}; doesn't mutate the
546 * original structure.
547 */
548 public NetworkStats groupedByIface() {
549 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
550
551 final Entry entry = new Entry();
552 entry.uid = UID_ALL;
553 entry.set = SET_ALL;
554 entry.tag = TAG_NONE;
555 entry.operations = 0L;
556
557 for (int i = 0; i < size; i++) {
558 // skip specific tags, since already counted in TAG_NONE
559 if (tag[i] != TAG_NONE) continue;
560
561 entry.iface = iface[i];
562 entry.rxBytes = rxBytes[i];
563 entry.rxPackets = rxPackets[i];
564 entry.txBytes = txBytes[i];
565 entry.txPackets = txPackets[i];
566 stats.combineValues(entry);
567 }
568
569 return stats;
570 }
571
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700572 /**
573 * Return total statistics grouped by {@link #uid}; doesn't mutate the
574 * original structure.
575 */
576 public NetworkStats groupedByUid() {
577 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
578
579 final Entry entry = new Entry();
580 entry.iface = IFACE_ALL;
581 entry.set = SET_ALL;
582 entry.tag = TAG_NONE;
583
584 for (int i = 0; i < size; i++) {
585 // skip specific tags, since already counted in TAG_NONE
586 if (tag[i] != TAG_NONE) continue;
587
588 entry.uid = uid[i];
589 entry.rxBytes = rxBytes[i];
590 entry.rxPackets = rxPackets[i];
591 entry.txBytes = txBytes[i];
592 entry.txPackets = txPackets[i];
593 entry.operations = operations[i];
594 stats.combineValues(entry);
595 }
596
597 return stats;
598 }
599
Jeff Sharkey163e6442011-10-31 16:37:52 -0700600 /**
601 * Return all rows except those attributed to the requested UID; doesn't
602 * mutate the original structure.
603 */
604 public NetworkStats withoutUid(int uid) {
605 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
606
607 Entry entry = new Entry();
608 for (int i = 0; i < size; i++) {
609 entry = getValues(i, entry);
610 if (entry.uid != uid) {
611 stats.addValues(entry);
612 }
613 }
614
615 return stats;
616 }
617
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700618 public void dump(String prefix, PrintWriter pw) {
619 pw.print(prefix);
620 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700621 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700622 pw.print(prefix);
Jeff Sharkey3359aca2011-11-08 18:08:48 -0800623 pw.print(" ["); pw.print(i); pw.print("]");
624 pw.print(" iface="); pw.print(iface[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700625 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700626 pw.print(" set="); pw.print(setToString(set[i]));
627 pw.print(" tag="); pw.print(tagToString(tag[i]));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700628 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700629 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700630 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700631 pw.print(" txPackets="); pw.print(txPackets[i]);
632 pw.print(" operations="); pw.println(operations[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700633 }
634 }
635
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700636 /**
637 * Return text description of {@link #set} value.
638 */
639 public static String setToString(int set) {
640 switch (set) {
641 case SET_ALL:
642 return "ALL";
643 case SET_DEFAULT:
644 return "DEFAULT";
645 case SET_FOREGROUND:
646 return "FOREGROUND";
647 default:
648 return "UNKNOWN";
649 }
650 }
651
652 /**
653 * Return text description of {@link #tag} value.
654 */
655 public static String tagToString(int tag) {
656 return "0x" + Integer.toHexString(tag);
657 }
658
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700659 @Override
660 public String toString() {
661 final CharArrayWriter writer = new CharArrayWriter();
662 dump("", new PrintWriter(writer));
663 return writer.toString();
664 }
665
666 /** {@inheritDoc} */
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700667 public int describeContents() {
668 return 0;
669 }
670
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700671 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
672 public NetworkStats createFromParcel(Parcel in) {
673 return new NetworkStats(in);
674 }
675
676 public NetworkStats[] newArray(int size) {
677 return new NetworkStats[size];
678 }
679 };
Jeff Sharkey163e6442011-10-31 16:37:52 -0700680
Jeff Sharkey63abc372012-01-11 18:38:16 -0800681 public interface NonMonotonicObserver<C> {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800682 public void foundNonMonotonic(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800683 NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700684 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700685}