blob: 446bbf0509db511c4af8acf0c43d99aa8877e027 [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 Sharkeydaa57e82012-09-19 14:10:39 -070024import com.android.internal.util.ArrayUtils;
Jeff Sharkeya63ba592011-07-19 23:47:12 -070025import com.android.internal.util.Objects;
26
Jeff Sharkey9a13f362011-04-26 16:25:36 -070027import java.io.CharArrayWriter;
28import java.io.PrintWriter;
Jeff Sharkey4a971222011-06-11 22:16:55 -070029import java.util.Arrays;
Jeff Sharkey75279902011-05-24 18:39:45 -070030import java.util.HashSet;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070031
32/**
Jeff Sharkey75279902011-05-24 18:39:45 -070033 * Collection of active network statistics. Can contain summary details across
34 * all interfaces, or details with per-UID granularity. Internally stores data
35 * as a large table, closely matching {@code /proc/} data format. This structure
36 * optimizes for rapid in-memory comparison, but consider using
37 * {@link NetworkStatsHistory} when persisting.
Jeff Sharkey9a13f362011-04-26 16:25:36 -070038 *
39 * @hide
40 */
41public class NetworkStats implements Parcelable {
Jeff Sharkey75279902011-05-24 18:39:45 -070042 /** {@link #iface} value when interface details unavailable. */
Jeff Sharkey9a13f362011-04-26 16:25:36 -070043 public static final String IFACE_ALL = null;
Jeff Sharkey75279902011-05-24 18:39:45 -070044 /** {@link #uid} value when UID details unavailable. */
45 public static final int UID_ALL = -1;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070046 /** {@link #set} value when all sets combined. */
47 public static final int SET_ALL = -1;
48 /** {@link #set} value where background data is accounted. */
49 public static final int SET_DEFAULT = 0;
50 /** {@link #set} value where foreground data is accounted. */
51 public static final int SET_FOREGROUND = 1;
52 /** {@link #tag} value for total data across all tags. */
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070053 public static final int TAG_NONE = 0;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070054
Jeff Sharkey163e6442011-10-31 16:37:52 -070055 // TODO: move fields to "mVariable" notation
56
Jeff Sharkey9a13f362011-04-26 16:25:36 -070057 /**
58 * {@link SystemClock#elapsedRealtime()} timestamp when this data was
59 * generated.
60 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070061 private final long elapsedRealtime;
62 private int size;
63 private String[] iface;
64 private int[] uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070065 private int[] set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070066 private int[] tag;
67 private long[] rxBytes;
68 private long[] rxPackets;
69 private long[] txBytes;
70 private long[] txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070071 private long[] operations;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070072
73 public static class Entry {
74 public String iface;
75 public int uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070076 public int set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070077 public int tag;
78 public long rxBytes;
79 public long rxPackets;
80 public long txBytes;
81 public long txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070082 public long operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070083
84 public Entry() {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070085 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
Jeff Sharkey63d27a92011-08-03 17:04:22 -070086 }
87
88 public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070089 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
90 operations);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070091 }
92
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070093 public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets,
94 long txBytes, long txPackets, long operations) {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070095 this.iface = iface;
96 this.uid = uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070097 this.set = set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070098 this.tag = tag;
99 this.rxBytes = rxBytes;
100 this.rxPackets = rxPackets;
101 this.txBytes = txBytes;
102 this.txPackets = txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700103 this.operations = operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700104 }
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700105
Jeff Sharkey63abc372012-01-11 18:38:16 -0800106 public boolean isNegative() {
107 return rxBytes < 0 || rxPackets < 0 || txBytes < 0 || txPackets < 0 || operations < 0;
108 }
109
110 public boolean isEmpty() {
111 return rxBytes == 0 && rxPackets == 0 && txBytes == 0 && txPackets == 0
112 && operations == 0;
113 }
114
Jeff Sharkey70c70532012-05-16 14:51:19 -0700115 public void add(Entry another) {
116 this.rxBytes += another.rxBytes;
117 this.rxPackets += another.rxPackets;
118 this.txBytes += another.txBytes;
119 this.txPackets += another.txPackets;
120 this.operations += another.operations;
121 }
122
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700123 @Override
124 public String toString() {
125 final StringBuilder builder = new StringBuilder();
126 builder.append("iface=").append(iface);
127 builder.append(" uid=").append(uid);
128 builder.append(" set=").append(setToString(set));
129 builder.append(" tag=").append(tagToString(tag));
130 builder.append(" rxBytes=").append(rxBytes);
131 builder.append(" rxPackets=").append(rxPackets);
132 builder.append(" txBytes=").append(txBytes);
133 builder.append(" txPackets=").append(txPackets);
134 builder.append(" operations=").append(operations);
135 return builder.toString();
136 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700137 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700138
Jeff Sharkey4a971222011-06-11 22:16:55 -0700139 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700140 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700141 this.size = 0;
142 this.iface = new String[initialSize];
143 this.uid = new int[initialSize];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700144 this.set = new int[initialSize];
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700145 this.tag = new int[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700146 this.rxBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700147 this.rxPackets = new long[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700148 this.txBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700149 this.txPackets = new long[initialSize];
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700150 this.operations = new long[initialSize];
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700151 }
152
153 public NetworkStats(Parcel parcel) {
154 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700155 size = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700156 iface = parcel.createStringArray();
157 uid = parcel.createIntArray();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700158 set = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700159 tag = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700160 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700161 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700162 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700163 txPackets = parcel.createLongArray();
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700164 operations = parcel.createLongArray();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700165 }
166
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700167 @Override
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700168 public void writeToParcel(Parcel dest, int flags) {
169 dest.writeLong(elapsedRealtime);
170 dest.writeInt(size);
171 dest.writeStringArray(iface);
172 dest.writeIntArray(uid);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700173 dest.writeIntArray(set);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700174 dest.writeIntArray(tag);
175 dest.writeLongArray(rxBytes);
176 dest.writeLongArray(rxPackets);
177 dest.writeLongArray(txBytes);
178 dest.writeLongArray(txPackets);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700179 dest.writeLongArray(operations);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700180 }
181
Jeff Sharkey4abb1b82011-11-08 17:35:28 -0800182 @Override
183 public NetworkStats clone() {
184 final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
185 NetworkStats.Entry entry = null;
186 for (int i = 0; i < size; i++) {
187 entry = getValues(i, entry);
188 clone.addValues(entry);
189 }
190 return clone;
191 }
192
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700193 // @VisibleForTesting
194 public NetworkStats addIfaceValues(
195 String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
196 return addValues(
197 iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700198 }
199
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700200 // @VisibleForTesting
201 public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
202 long rxPackets, long txBytes, long txPackets, long operations) {
203 return addValues(new Entry(
204 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700205 }
206
207 /**
208 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
209 * object can be recycled across multiple calls.
210 */
211 public NetworkStats addValues(Entry entry) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700212 if (size >= this.iface.length) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700213 final int newLength = Math.max(iface.length, 10) * 3 / 2;
214 iface = Arrays.copyOf(iface, newLength);
215 uid = Arrays.copyOf(uid, newLength);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700216 set = Arrays.copyOf(set, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700217 tag = Arrays.copyOf(tag, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700218 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700219 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700220 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700221 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700222 operations = Arrays.copyOf(operations, newLength);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700223 }
224
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700225 iface[size] = entry.iface;
226 uid[size] = entry.uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700227 set[size] = entry.set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700228 tag[size] = entry.tag;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700229 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700230 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700231 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700232 txPackets[size] = entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700233 operations[size] = entry.operations;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700234 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700235
Jeff Sharkey4a971222011-06-11 22:16:55 -0700236 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700237 }
238
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700239 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700240 * Return specific stats entry.
241 */
242 public Entry getValues(int i, Entry recycle) {
243 final Entry entry = recycle != null ? recycle : new Entry();
244 entry.iface = iface[i];
245 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700246 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700247 entry.tag = tag[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700248 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700249 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700250 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700251 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700252 entry.operations = operations[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700253 return entry;
254 }
255
256 public long getElapsedRealtime() {
257 return elapsedRealtime;
258 }
259
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700260 /**
261 * Return age of this {@link NetworkStats} object with respect to
262 * {@link SystemClock#elapsedRealtime()}.
263 */
264 public long getElapsedRealtimeAge() {
265 return SystemClock.elapsedRealtime() - elapsedRealtime;
266 }
267
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700268 public int size() {
269 return size;
270 }
271
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700272 // @VisibleForTesting
273 public int internalSize() {
274 return iface.length;
275 }
276
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700277 @Deprecated
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700278 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700279 long txBytes, long txPackets, long operations) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700280 return combineValues(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700281 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations);
282 }
283
284 public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes,
285 long rxPackets, long txBytes, long txPackets, long operations) {
286 return combineValues(new Entry(
287 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700288 }
289
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700290 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700291 * Combine given values with an existing row, or create a new row if
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700292 * {@link #findIndex(String, int, int, int)} is unable to find match. Can
293 * also be used to subtract values from existing rows.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700294 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700295 public NetworkStats combineValues(Entry entry) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700296 final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700297 if (i == -1) {
298 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700299 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700300 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700301 rxBytes[i] += entry.rxBytes;
302 rxPackets[i] += entry.rxPackets;
303 txBytes[i] += entry.txBytes;
304 txPackets[i] += entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700305 operations[i] += entry.operations;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700306 }
307 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700308 }
309
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700310 /**
Jeff Sharkey905b5892011-09-30 15:19:49 -0700311 * Combine all values from another {@link NetworkStats} into this object.
312 */
313 public void combineAllValues(NetworkStats another) {
314 NetworkStats.Entry entry = null;
315 for (int i = 0; i < another.size; i++) {
316 entry = another.getValues(i, entry);
317 combineValues(entry);
318 }
319 }
320
321 /**
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700322 * Find first stats index that matches the requested parameters.
323 */
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700324 public int findIndex(String iface, int uid, int set, int tag) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700325 for (int i = 0; i < size; i++) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700326 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
327 && Objects.equal(iface, this.iface[i])) {
328 return i;
329 }
330 }
331 return -1;
332 }
333
334 /**
335 * Find first stats index that matches the requested parameters, starting
336 * search around the hinted index as an optimization.
337 */
338 // @VisibleForTesting
339 public int findIndexHinted(String iface, int uid, int set, int tag, int hintIndex) {
340 for (int offset = 0; offset < size; offset++) {
341 final int halfOffset = offset / 2;
342
343 // search outwards from hint index, alternating forward and backward
344 final int i;
345 if (offset % 2 == 0) {
346 i = (hintIndex + halfOffset) % size;
347 } else {
348 i = (size + hintIndex - halfOffset - 1) % size;
349 }
350
351 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
352 && Objects.equal(iface, this.iface[i])) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700353 return i;
354 }
355 }
356 return -1;
357 }
358
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700359 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700360 * Splice in {@link #operations} from the given {@link NetworkStats} based
361 * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
362 * since operation counts are at data layer.
363 */
364 public void spliceOperationsFrom(NetworkStats stats) {
365 for (int i = 0; i < size; i++) {
Jeff Sharkey21a54782012-04-09 10:27:55 -0700366 final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700367 if (j == -1) {
368 operations[i] = 0;
369 } else {
370 operations[i] = stats.operations[j];
371 }
372 }
373 }
374
375 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700376 * Return list of unique interfaces known by this data structure.
377 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700378 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700379 final HashSet<String> ifaces = new HashSet<String>();
380 for (String iface : this.iface) {
381 if (iface != IFACE_ALL) {
382 ifaces.add(iface);
383 }
384 }
385 return ifaces.toArray(new String[ifaces.size()]);
386 }
387
388 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700389 * Return list of unique UIDs known by this data structure.
390 */
391 public int[] getUniqueUids() {
392 final SparseBooleanArray uids = new SparseBooleanArray();
393 for (int uid : this.uid) {
394 uids.put(uid, true);
395 }
396
397 final int size = uids.size();
398 final int[] result = new int[size];
399 for (int i = 0; i < size; i++) {
400 result[i] = uids.keyAt(i);
401 }
402 return result;
403 }
404
405 /**
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700406 * Return total bytes represented by this snapshot object, usually used when
407 * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
408 */
409 public long getTotalBytes() {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700410 final Entry entry = getTotal(null);
411 return entry.rxBytes + entry.txBytes;
412 }
413
414 /**
415 * Return total of all fields represented by this snapshot object.
416 */
417 public Entry getTotal(Entry recycle) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800418 return getTotal(recycle, null, UID_ALL, 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 #uid}.
424 */
425 public Entry getTotal(Entry recycle, int limitUid) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800426 return getTotal(recycle, null, limitUid, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700427 }
428
429 /**
430 * Return total of all fields represented by this snapshot object matching
431 * the requested {@link #iface}.
432 */
433 public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800434 return getTotal(recycle, limitIface, UID_ALL, false);
435 }
436
437 public Entry getTotalIncludingTags(Entry recycle) {
438 return getTotal(recycle, null, UID_ALL, true);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700439 }
440
441 /**
442 * Return total of all fields represented by this snapshot object matching
443 * the requested {@link #iface} and {@link #uid}.
444 *
445 * @param limitIface Set of {@link #iface} to include in total; or {@code
446 * null} to include all ifaces.
447 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800448 private Entry getTotal(
449 Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags) {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700450 final Entry entry = recycle != null ? recycle : new Entry();
451
452 entry.iface = IFACE_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700453 entry.uid = limitUid;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700454 entry.set = SET_ALL;
455 entry.tag = TAG_NONE;
456 entry.rxBytes = 0;
457 entry.rxPackets = 0;
458 entry.txBytes = 0;
459 entry.txPackets = 0;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700460 entry.operations = 0;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700461
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700462 for (int i = 0; i < size; i++) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700463 final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]);
464 final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i]));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700465
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700466 if (matchesUid && matchesIface) {
467 // skip specific tags, since already counted in TAG_NONE
Jeff Sharkey63abc372012-01-11 18:38:16 -0800468 if (tag[i] != TAG_NONE && !includeTags) continue;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700469
470 entry.rxBytes += rxBytes[i];
471 entry.rxPackets += rxPackets[i];
472 entry.txBytes += txBytes[i];
473 entry.txPackets += txPackets[i];
474 entry.operations += operations[i];
475 }
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700476 }
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700477 return entry;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700478 }
479
480 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700481 * Subtract the given {@link NetworkStats}, effectively leaving the delta
482 * between two snapshots in time. Assumes that statistics rows collect over
483 * time, and that none of them have disappeared.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700484 */
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800485 public NetworkStats subtract(NetworkStats right) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800486 return subtract(this, right, null, null);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800487 }
488
489 /**
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800490 * Subtract the two given {@link NetworkStats} objects, returning the delta
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800491 * between two snapshots in time. Assumes that statistics rows collect over
492 * time, and that none of them have disappeared.
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800493 * <p>
494 * If counters have rolled backwards, they are clamped to {@code 0} and
495 * reported to the given {@link NonMonotonicObserver}.
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800496 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800497 public static <C> NetworkStats subtract(
498 NetworkStats left, NetworkStats right, NonMonotonicObserver<C> observer, C cookie) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800499 long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700500 if (deltaRealtime < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800501 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800502 observer.foundNonMonotonic(left, -1, right, -1, cookie);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800503 }
504 deltaRealtime = 0;
Jeff Sharkey75279902011-05-24 18:39:45 -0700505 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700506
Jeff Sharkey75279902011-05-24 18:39:45 -0700507 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700508 final Entry entry = new Entry();
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800509 final NetworkStats result = new NetworkStats(deltaRealtime, left.size);
510 for (int i = 0; i < left.size; i++) {
511 entry.iface = left.iface[i];
512 entry.uid = left.uid[i];
513 entry.set = left.set[i];
514 entry.tag = left.tag[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700515
516 // find remote row that matches, and subtract
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800517 final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag, i);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700518 if (j == -1) {
519 // newly appearing row, return entire value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800520 entry.rxBytes = left.rxBytes[i];
521 entry.rxPackets = left.rxPackets[i];
522 entry.txBytes = left.txBytes[i];
523 entry.txPackets = left.txPackets[i];
524 entry.operations = left.operations[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700525 } else {
526 // existing row, subtract remote value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800527 entry.rxBytes = left.rxBytes[i] - right.rxBytes[j];
528 entry.rxPackets = left.rxPackets[i] - right.rxPackets[j];
529 entry.txBytes = left.txBytes[i] - right.txBytes[j];
530 entry.txPackets = left.txPackets[i] - right.txPackets[j];
531 entry.operations = left.operations[i] - right.operations[j];
Jeff Sharkey163e6442011-10-31 16:37:52 -0700532
533 if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
534 || entry.txPackets < 0 || entry.operations < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800535 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800536 observer.foundNonMonotonic(left, i, right, j, cookie);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800537 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800538 entry.rxBytes = Math.max(entry.rxBytes, 0);
539 entry.rxPackets = Math.max(entry.rxPackets, 0);
540 entry.txBytes = Math.max(entry.txBytes, 0);
541 entry.txPackets = Math.max(entry.txPackets, 0);
542 entry.operations = Math.max(entry.operations, 0);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700543 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700544 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700545
546 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700547 }
548
Jeff Sharkey4a971222011-06-11 22:16:55 -0700549 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700550 }
551
Jeff Sharkey905b5892011-09-30 15:19:49 -0700552 /**
553 * Return total statistics grouped by {@link #iface}; doesn't mutate the
554 * original structure.
555 */
556 public NetworkStats groupedByIface() {
557 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
558
559 final Entry entry = new Entry();
560 entry.uid = UID_ALL;
561 entry.set = SET_ALL;
562 entry.tag = TAG_NONE;
563 entry.operations = 0L;
564
565 for (int i = 0; i < size; i++) {
566 // skip specific tags, since already counted in TAG_NONE
567 if (tag[i] != TAG_NONE) continue;
568
569 entry.iface = iface[i];
570 entry.rxBytes = rxBytes[i];
571 entry.rxPackets = rxPackets[i];
572 entry.txBytes = txBytes[i];
573 entry.txPackets = txPackets[i];
574 stats.combineValues(entry);
575 }
576
577 return stats;
578 }
579
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700580 /**
581 * Return total statistics grouped by {@link #uid}; doesn't mutate the
582 * original structure.
583 */
584 public NetworkStats groupedByUid() {
585 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
586
587 final Entry entry = new Entry();
588 entry.iface = IFACE_ALL;
589 entry.set = SET_ALL;
590 entry.tag = TAG_NONE;
591
592 for (int i = 0; i < size; i++) {
593 // skip specific tags, since already counted in TAG_NONE
594 if (tag[i] != TAG_NONE) continue;
595
596 entry.uid = uid[i];
597 entry.rxBytes = rxBytes[i];
598 entry.rxPackets = rxPackets[i];
599 entry.txBytes = txBytes[i];
600 entry.txPackets = txPackets[i];
601 entry.operations = operations[i];
602 stats.combineValues(entry);
603 }
604
605 return stats;
606 }
607
Jeff Sharkey163e6442011-10-31 16:37:52 -0700608 /**
609 * Return all rows except those attributed to the requested UID; doesn't
610 * mutate the original structure.
611 */
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700612 public NetworkStats withoutUids(int[] uids) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700613 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
614
615 Entry entry = new Entry();
616 for (int i = 0; i < size; i++) {
617 entry = getValues(i, entry);
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700618 if (!ArrayUtils.contains(uids, entry.uid)) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700619 stats.addValues(entry);
620 }
621 }
622
623 return stats;
624 }
625
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700626 public void dump(String prefix, PrintWriter pw) {
627 pw.print(prefix);
628 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700629 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700630 pw.print(prefix);
Jeff Sharkey3359aca2011-11-08 18:08:48 -0800631 pw.print(" ["); pw.print(i); pw.print("]");
632 pw.print(" iface="); pw.print(iface[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700633 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700634 pw.print(" set="); pw.print(setToString(set[i]));
635 pw.print(" tag="); pw.print(tagToString(tag[i]));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700636 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700637 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700638 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700639 pw.print(" txPackets="); pw.print(txPackets[i]);
640 pw.print(" operations="); pw.println(operations[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700641 }
642 }
643
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700644 /**
645 * Return text description of {@link #set} value.
646 */
647 public static String setToString(int set) {
648 switch (set) {
649 case SET_ALL:
650 return "ALL";
651 case SET_DEFAULT:
652 return "DEFAULT";
653 case SET_FOREGROUND:
654 return "FOREGROUND";
655 default:
656 return "UNKNOWN";
657 }
658 }
659
660 /**
661 * Return text description of {@link #tag} value.
662 */
663 public static String tagToString(int tag) {
664 return "0x" + Integer.toHexString(tag);
665 }
666
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700667 @Override
668 public String toString() {
669 final CharArrayWriter writer = new CharArrayWriter();
670 dump("", new PrintWriter(writer));
671 return writer.toString();
672 }
673
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700674 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700675 public int describeContents() {
676 return 0;
677 }
678
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700679 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700680 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700681 public NetworkStats createFromParcel(Parcel in) {
682 return new NetworkStats(in);
683 }
684
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700685 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700686 public NetworkStats[] newArray(int size) {
687 return new NetworkStats[size];
688 }
689 };
Jeff Sharkey163e6442011-10-31 16:37:52 -0700690
Jeff Sharkey63abc372012-01-11 18:38:16 -0800691 public interface NonMonotonicObserver<C> {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800692 public void foundNonMonotonic(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800693 NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700694 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700695}