blob: 2afe578b6b6ff6daa4d592f6d2e94557f54d7641 [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 -070026
Jeff Sharkey7a8f1a32014-09-17 09:26:28 -070027import libcore.util.EmptyArray;
28
Jeff Sharkey9a13f362011-04-26 16:25:36 -070029import java.io.CharArrayWriter;
30import java.io.PrintWriter;
Jeff Sharkey4a971222011-06-11 22:16:55 -070031import java.util.Arrays;
Jeff Sharkey75279902011-05-24 18:39:45 -070032import java.util.HashSet;
Kenny Roote6585b32013-12-13 12:00:26 -080033import java.util.Objects;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070034
35/**
Jeff Sharkey75279902011-05-24 18:39:45 -070036 * Collection of active network statistics. Can contain summary details across
37 * all interfaces, or details with per-UID granularity. Internally stores data
38 * as a large table, closely matching {@code /proc/} data format. This structure
39 * optimizes for rapid in-memory comparison, but consider using
40 * {@link NetworkStatsHistory} when persisting.
Jeff Sharkey9a13f362011-04-26 16:25:36 -070041 *
42 * @hide
43 */
44public class NetworkStats implements Parcelable {
Jeff Sharkey75279902011-05-24 18:39:45 -070045 /** {@link #iface} value when interface details unavailable. */
Jeff Sharkey9a13f362011-04-26 16:25:36 -070046 public static final String IFACE_ALL = null;
Jeff Sharkey75279902011-05-24 18:39:45 -070047 /** {@link #uid} value when UID details unavailable. */
48 public static final int UID_ALL = -1;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080049 /** {@link #tag} value matching any tag. */
50 public static final int TAG_ALL = -1;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070051 /** {@link #set} value when all sets combined. */
52 public static final int SET_ALL = -1;
53 /** {@link #set} value where background data is accounted. */
54 public static final int SET_DEFAULT = 0;
55 /** {@link #set} value where foreground data is accounted. */
56 public static final int SET_FOREGROUND = 1;
57 /** {@link #tag} value for total data across all tags. */
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070058 public static final int TAG_NONE = 0;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070059
Jeff Sharkey163e6442011-10-31 16:37:52 -070060 // TODO: move fields to "mVariable" notation
61
Jeff Sharkey9a13f362011-04-26 16:25:36 -070062 /**
63 * {@link SystemClock#elapsedRealtime()} timestamp when this data was
64 * generated.
65 */
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080066 private long elapsedRealtime;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070067 private int size;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080068 private int capacity;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070069 private String[] iface;
70 private int[] uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070071 private int[] set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070072 private int[] tag;
73 private long[] rxBytes;
74 private long[] rxPackets;
75 private long[] txBytes;
76 private long[] txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070077 private long[] operations;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070078
79 public static class Entry {
80 public String iface;
81 public int uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070082 public int set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070083 public int tag;
84 public long rxBytes;
85 public long rxPackets;
86 public long txBytes;
87 public long txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070088 public long operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070089
90 public Entry() {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070091 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
Jeff Sharkey63d27a92011-08-03 17:04:22 -070092 }
93
94 public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070095 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
96 operations);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070097 }
98
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070099 public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets,
100 long txBytes, long txPackets, long operations) {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700101 this.iface = iface;
102 this.uid = uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700103 this.set = set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700104 this.tag = tag;
105 this.rxBytes = rxBytes;
106 this.rxPackets = rxPackets;
107 this.txBytes = txBytes;
108 this.txPackets = txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700109 this.operations = operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700110 }
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700111
Jeff Sharkey63abc372012-01-11 18:38:16 -0800112 public boolean isNegative() {
113 return rxBytes < 0 || rxPackets < 0 || txBytes < 0 || txPackets < 0 || operations < 0;
114 }
115
116 public boolean isEmpty() {
117 return rxBytes == 0 && rxPackets == 0 && txBytes == 0 && txPackets == 0
118 && operations == 0;
119 }
120
Jeff Sharkey70c70532012-05-16 14:51:19 -0700121 public void add(Entry another) {
122 this.rxBytes += another.rxBytes;
123 this.rxPackets += another.rxPackets;
124 this.txBytes += another.txBytes;
125 this.txPackets += another.txPackets;
126 this.operations += another.operations;
127 }
128
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700129 @Override
130 public String toString() {
131 final StringBuilder builder = new StringBuilder();
132 builder.append("iface=").append(iface);
133 builder.append(" uid=").append(uid);
134 builder.append(" set=").append(setToString(set));
135 builder.append(" tag=").append(tagToString(tag));
136 builder.append(" rxBytes=").append(rxBytes);
137 builder.append(" rxPackets=").append(rxPackets);
138 builder.append(" txBytes=").append(txBytes);
139 builder.append(" txPackets=").append(txPackets);
140 builder.append(" operations=").append(operations);
141 return builder.toString();
142 }
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -0800143
144 @Override
145 public boolean equals(Object o) {
146 if (o instanceof Entry) {
147 final Entry e = (Entry) o;
148 return uid == e.uid && set == e.set && tag == e.tag && rxBytes == e.rxBytes
149 && rxPackets == e.rxPackets && txBytes == e.txBytes
150 && txPackets == e.txPackets && operations == e.operations
151 && iface.equals(e.iface);
152 }
153 return false;
154 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700155 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700156
Jeff Sharkey4a971222011-06-11 22:16:55 -0700157 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700158 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700159 this.size = 0;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800160 if (initialSize >= 0) {
161 this.capacity = initialSize;
162 this.iface = new String[initialSize];
163 this.uid = new int[initialSize];
164 this.set = new int[initialSize];
165 this.tag = new int[initialSize];
166 this.rxBytes = new long[initialSize];
167 this.rxPackets = new long[initialSize];
168 this.txBytes = new long[initialSize];
169 this.txPackets = new long[initialSize];
170 this.operations = new long[initialSize];
171 } else {
172 // Special case for use by NetworkStatsFactory to start out *really* empty.
173 this.capacity = 0;
Jeff Sharkey7a8f1a32014-09-17 09:26:28 -0700174 this.iface = EmptyArray.STRING;
175 this.uid = EmptyArray.INT;
176 this.set = EmptyArray.INT;
177 this.tag = EmptyArray.INT;
178 this.rxBytes = EmptyArray.LONG;
179 this.rxPackets = EmptyArray.LONG;
180 this.txBytes = EmptyArray.LONG;
181 this.txPackets = EmptyArray.LONG;
182 this.operations = EmptyArray.LONG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800183 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700184 }
185
186 public NetworkStats(Parcel parcel) {
187 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700188 size = parcel.readInt();
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800189 capacity = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700190 iface = parcel.createStringArray();
191 uid = parcel.createIntArray();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700192 set = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700193 tag = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700194 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700195 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700196 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700197 txPackets = parcel.createLongArray();
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700198 operations = parcel.createLongArray();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700199 }
200
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700201 @Override
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700202 public void writeToParcel(Parcel dest, int flags) {
203 dest.writeLong(elapsedRealtime);
204 dest.writeInt(size);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800205 dest.writeInt(capacity);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700206 dest.writeStringArray(iface);
207 dest.writeIntArray(uid);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700208 dest.writeIntArray(set);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700209 dest.writeIntArray(tag);
210 dest.writeLongArray(rxBytes);
211 dest.writeLongArray(rxPackets);
212 dest.writeLongArray(txBytes);
213 dest.writeLongArray(txPackets);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700214 dest.writeLongArray(operations);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700215 }
216
Jeff Sharkey4abb1b82011-11-08 17:35:28 -0800217 @Override
218 public NetworkStats clone() {
219 final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
220 NetworkStats.Entry entry = null;
221 for (int i = 0; i < size; i++) {
222 entry = getValues(i, entry);
223 clone.addValues(entry);
224 }
225 return clone;
226 }
227
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800228 @VisibleForTesting
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700229 public NetworkStats addIfaceValues(
230 String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
231 return addValues(
232 iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700233 }
234
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800235 @VisibleForTesting
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700236 public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
237 long rxPackets, long txBytes, long txPackets, long operations) {
238 return addValues(new Entry(
239 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700240 }
241
242 /**
243 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
244 * object can be recycled across multiple calls.
245 */
246 public NetworkStats addValues(Entry entry) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800247 if (size >= capacity) {
248 final int newLength = Math.max(size, 10) * 3 / 2;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700249 iface = Arrays.copyOf(iface, newLength);
250 uid = Arrays.copyOf(uid, newLength);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700251 set = Arrays.copyOf(set, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700252 tag = Arrays.copyOf(tag, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700253 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700254 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700255 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700256 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700257 operations = Arrays.copyOf(operations, newLength);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800258 capacity = newLength;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700259 }
260
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700261 iface[size] = entry.iface;
262 uid[size] = entry.uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700263 set[size] = entry.set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700264 tag[size] = entry.tag;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700265 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700266 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700267 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700268 txPackets[size] = entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700269 operations[size] = entry.operations;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700270 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700271
Jeff Sharkey4a971222011-06-11 22:16:55 -0700272 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700273 }
274
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700275 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700276 * Return specific stats entry.
277 */
278 public Entry getValues(int i, Entry recycle) {
279 final Entry entry = recycle != null ? recycle : new Entry();
280 entry.iface = iface[i];
281 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700282 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700283 entry.tag = tag[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700284 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700285 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700286 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700287 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700288 entry.operations = operations[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700289 return entry;
290 }
291
292 public long getElapsedRealtime() {
293 return elapsedRealtime;
294 }
295
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800296 public void setElapsedRealtime(long time) {
297 elapsedRealtime = time;
298 }
299
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700300 /**
301 * Return age of this {@link NetworkStats} object with respect to
302 * {@link SystemClock#elapsedRealtime()}.
303 */
304 public long getElapsedRealtimeAge() {
305 return SystemClock.elapsedRealtime() - elapsedRealtime;
306 }
307
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700308 public int size() {
309 return size;
310 }
311
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800312 @VisibleForTesting
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700313 public int internalSize() {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800314 return capacity;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700315 }
316
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700317 @Deprecated
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700318 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700319 long txBytes, long txPackets, long operations) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700320 return combineValues(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700321 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations);
322 }
323
324 public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes,
325 long rxPackets, long txBytes, long txPackets, long operations) {
326 return combineValues(new Entry(
327 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700328 }
329
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700330 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700331 * Combine given values with an existing row, or create a new row if
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700332 * {@link #findIndex(String, int, int, int)} is unable to find match. Can
333 * also be used to subtract values from existing rows.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700334 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700335 public NetworkStats combineValues(Entry entry) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700336 final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700337 if (i == -1) {
338 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700339 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700340 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700341 rxBytes[i] += entry.rxBytes;
342 rxPackets[i] += entry.rxPackets;
343 txBytes[i] += entry.txBytes;
344 txPackets[i] += entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700345 operations[i] += entry.operations;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700346 }
347 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700348 }
349
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700350 /**
Jeff Sharkey905b5892011-09-30 15:19:49 -0700351 * Combine all values from another {@link NetworkStats} into this object.
352 */
353 public void combineAllValues(NetworkStats another) {
354 NetworkStats.Entry entry = null;
355 for (int i = 0; i < another.size; i++) {
356 entry = another.getValues(i, entry);
357 combineValues(entry);
358 }
359 }
360
361 /**
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700362 * Find first stats index that matches the requested parameters.
363 */
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700364 public int findIndex(String iface, int uid, int set, int tag) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700365 for (int i = 0; i < size; i++) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700366 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
Kenny Roote6585b32013-12-13 12:00:26 -0800367 && Objects.equals(iface, this.iface[i])) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700368 return i;
369 }
370 }
371 return -1;
372 }
373
374 /**
375 * Find first stats index that matches the requested parameters, starting
376 * search around the hinted index as an optimization.
377 */
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -0800378 @VisibleForTesting
Jeff Sharkey163e6442011-10-31 16:37:52 -0700379 public int findIndexHinted(String iface, int uid, int set, int tag, int hintIndex) {
380 for (int offset = 0; offset < size; offset++) {
381 final int halfOffset = offset / 2;
382
383 // search outwards from hint index, alternating forward and backward
384 final int i;
385 if (offset % 2 == 0) {
386 i = (hintIndex + halfOffset) % size;
387 } else {
388 i = (size + hintIndex - halfOffset - 1) % size;
389 }
390
391 if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
Kenny Roote6585b32013-12-13 12:00:26 -0800392 && Objects.equals(iface, this.iface[i])) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700393 return i;
394 }
395 }
396 return -1;
397 }
398
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700399 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700400 * Splice in {@link #operations} from the given {@link NetworkStats} based
401 * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
402 * since operation counts are at data layer.
403 */
404 public void spliceOperationsFrom(NetworkStats stats) {
405 for (int i = 0; i < size; i++) {
Jeff Sharkey21a54782012-04-09 10:27:55 -0700406 final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700407 if (j == -1) {
408 operations[i] = 0;
409 } else {
410 operations[i] = stats.operations[j];
411 }
412 }
413 }
414
415 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700416 * Return list of unique interfaces known by this data structure.
417 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700418 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700419 final HashSet<String> ifaces = new HashSet<String>();
420 for (String iface : this.iface) {
421 if (iface != IFACE_ALL) {
422 ifaces.add(iface);
423 }
424 }
425 return ifaces.toArray(new String[ifaces.size()]);
426 }
427
428 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700429 * Return list of unique UIDs known by this data structure.
430 */
431 public int[] getUniqueUids() {
432 final SparseBooleanArray uids = new SparseBooleanArray();
433 for (int uid : this.uid) {
434 uids.put(uid, true);
435 }
436
437 final int size = uids.size();
438 final int[] result = new int[size];
439 for (int i = 0; i < size; i++) {
440 result[i] = uids.keyAt(i);
441 }
442 return result;
443 }
444
445 /**
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700446 * Return total bytes represented by this snapshot object, usually used when
447 * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
448 */
449 public long getTotalBytes() {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700450 final Entry entry = getTotal(null);
451 return entry.rxBytes + entry.txBytes;
452 }
453
454 /**
455 * Return total of all fields represented by this snapshot object.
456 */
457 public Entry getTotal(Entry recycle) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800458 return getTotal(recycle, null, UID_ALL, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700459 }
460
461 /**
462 * Return total of all fields represented by this snapshot object matching
463 * the requested {@link #uid}.
464 */
465 public Entry getTotal(Entry recycle, int limitUid) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800466 return getTotal(recycle, null, limitUid, false);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700467 }
468
469 /**
470 * Return total of all fields represented by this snapshot object matching
471 * the requested {@link #iface}.
472 */
473 public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800474 return getTotal(recycle, limitIface, UID_ALL, false);
475 }
476
477 public Entry getTotalIncludingTags(Entry recycle) {
478 return getTotal(recycle, null, UID_ALL, true);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700479 }
480
481 /**
482 * Return total of all fields represented by this snapshot object matching
483 * the requested {@link #iface} and {@link #uid}.
484 *
485 * @param limitIface Set of {@link #iface} to include in total; or {@code
486 * null} to include all ifaces.
487 */
Jeff Sharkey63abc372012-01-11 18:38:16 -0800488 private Entry getTotal(
489 Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags) {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700490 final Entry entry = recycle != null ? recycle : new Entry();
491
492 entry.iface = IFACE_ALL;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700493 entry.uid = limitUid;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700494 entry.set = SET_ALL;
495 entry.tag = TAG_NONE;
496 entry.rxBytes = 0;
497 entry.rxPackets = 0;
498 entry.txBytes = 0;
499 entry.txPackets = 0;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700500 entry.operations = 0;
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700501
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700502 for (int i = 0; i < size; i++) {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700503 final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]);
504 final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i]));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700505
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700506 if (matchesUid && matchesIface) {
507 // skip specific tags, since already counted in TAG_NONE
Jeff Sharkey63abc372012-01-11 18:38:16 -0800508 if (tag[i] != TAG_NONE && !includeTags) continue;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700509
510 entry.rxBytes += rxBytes[i];
511 entry.rxPackets += rxPackets[i];
512 entry.txBytes += txBytes[i];
513 entry.txPackets += txPackets[i];
514 entry.operations += operations[i];
515 }
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700516 }
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700517 return entry;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700518 }
519
520 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800521 * Fast path for battery stats.
522 */
523 public long getTotalPackets() {
524 long total = 0;
525 for (int i = size-1; i >= 0; i--) {
526 total += rxPackets[i] + txPackets[i];
527 }
528 return total;
529 }
530
531 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700532 * Subtract the given {@link NetworkStats}, effectively leaving the delta
533 * between two snapshots in time. Assumes that statistics rows collect over
534 * time, and that none of them have disappeared.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700535 */
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800536 public NetworkStats subtract(NetworkStats right) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800537 return subtract(this, right, null, null);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800538 }
539
540 /**
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800541 * Subtract the two given {@link NetworkStats} objects, returning the delta
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800542 * between two snapshots in time. Assumes that statistics rows collect over
543 * time, and that none of them have disappeared.
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800544 * <p>
545 * If counters have rolled backwards, they are clamped to {@code 0} and
546 * reported to the given {@link NonMonotonicObserver}.
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800547 */
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800548 public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
549 NonMonotonicObserver<C> observer, C cookie) {
550 return subtract(left, right, observer, cookie, null);
551 }
552
553 /**
554 * Subtract the two given {@link NetworkStats} objects, returning the delta
555 * between two snapshots in time. Assumes that statistics rows collect over
556 * time, and that none of them have disappeared.
557 * <p>
558 * If counters have rolled backwards, they are clamped to {@code 0} and
559 * reported to the given {@link NonMonotonicObserver}.
560 * <p>
561 * If <var>recycle</var> is supplied, this NetworkStats object will be
562 * reused (and returned) as the result if it is large enough to contain
563 * the data.
564 */
565 public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
566 NonMonotonicObserver<C> observer, C cookie, NetworkStats recycle) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800567 long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700568 if (deltaRealtime < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800569 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800570 observer.foundNonMonotonic(left, -1, right, -1, cookie);
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800571 }
572 deltaRealtime = 0;
Jeff Sharkey75279902011-05-24 18:39:45 -0700573 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700574
Jeff Sharkey75279902011-05-24 18:39:45 -0700575 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700576 final Entry entry = new Entry();
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800577 final NetworkStats result;
578 if (recycle != null && recycle.capacity >= left.size) {
579 result = recycle;
580 result.size = 0;
581 result.elapsedRealtime = deltaRealtime;
582 } else {
583 result = new NetworkStats(deltaRealtime, left.size);
584 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800585 for (int i = 0; i < left.size; i++) {
586 entry.iface = left.iface[i];
587 entry.uid = left.uid[i];
588 entry.set = left.set[i];
589 entry.tag = left.tag[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700590
591 // find remote row that matches, and subtract
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800592 final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag, i);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700593 if (j == -1) {
594 // newly appearing row, return entire value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800595 entry.rxBytes = left.rxBytes[i];
596 entry.rxPackets = left.rxPackets[i];
597 entry.txBytes = left.txBytes[i];
598 entry.txPackets = left.txPackets[i];
599 entry.operations = left.operations[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700600 } else {
601 // existing row, subtract remote value
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800602 entry.rxBytes = left.rxBytes[i] - right.rxBytes[j];
603 entry.rxPackets = left.rxPackets[i] - right.rxPackets[j];
604 entry.txBytes = left.txBytes[i] - right.txBytes[j];
605 entry.txPackets = left.txPackets[i] - right.txPackets[j];
606 entry.operations = left.operations[i] - right.operations[j];
Jeff Sharkey163e6442011-10-31 16:37:52 -0700607
608 if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
609 || entry.txPackets < 0 || entry.operations < 0) {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800610 if (observer != null) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800611 observer.foundNonMonotonic(left, i, right, j, cookie);
Jeff Sharkeyd4ef8c8f2011-11-10 17:54:23 -0800612 }
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800613 entry.rxBytes = Math.max(entry.rxBytes, 0);
614 entry.rxPackets = Math.max(entry.rxPackets, 0);
615 entry.txBytes = Math.max(entry.txBytes, 0);
616 entry.txPackets = Math.max(entry.txPackets, 0);
617 entry.operations = Math.max(entry.operations, 0);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700618 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700619 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700620
621 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700622 }
623
Jeff Sharkey4a971222011-06-11 22:16:55 -0700624 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700625 }
626
Jeff Sharkey905b5892011-09-30 15:19:49 -0700627 /**
628 * Return total statistics grouped by {@link #iface}; doesn't mutate the
629 * original structure.
630 */
631 public NetworkStats groupedByIface() {
632 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
633
634 final Entry entry = new Entry();
635 entry.uid = UID_ALL;
636 entry.set = SET_ALL;
637 entry.tag = TAG_NONE;
638 entry.operations = 0L;
639
640 for (int i = 0; i < size; i++) {
641 // skip specific tags, since already counted in TAG_NONE
642 if (tag[i] != TAG_NONE) continue;
643
644 entry.iface = iface[i];
645 entry.rxBytes = rxBytes[i];
646 entry.rxPackets = rxPackets[i];
647 entry.txBytes = txBytes[i];
648 entry.txPackets = txPackets[i];
649 stats.combineValues(entry);
650 }
651
652 return stats;
653 }
654
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700655 /**
656 * Return total statistics grouped by {@link #uid}; doesn't mutate the
657 * original structure.
658 */
659 public NetworkStats groupedByUid() {
660 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
661
662 final Entry entry = new Entry();
663 entry.iface = IFACE_ALL;
664 entry.set = SET_ALL;
665 entry.tag = TAG_NONE;
666
667 for (int i = 0; i < size; i++) {
668 // skip specific tags, since already counted in TAG_NONE
669 if (tag[i] != TAG_NONE) continue;
670
671 entry.uid = uid[i];
672 entry.rxBytes = rxBytes[i];
673 entry.rxPackets = rxPackets[i];
674 entry.txBytes = txBytes[i];
675 entry.txPackets = txPackets[i];
676 entry.operations = operations[i];
677 stats.combineValues(entry);
678 }
679
680 return stats;
681 }
682
Jeff Sharkey163e6442011-10-31 16:37:52 -0700683 /**
684 * Return all rows except those attributed to the requested UID; doesn't
685 * mutate the original structure.
686 */
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700687 public NetworkStats withoutUids(int[] uids) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700688 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
689
690 Entry entry = new Entry();
691 for (int i = 0; i < size; i++) {
692 entry = getValues(i, entry);
Jeff Sharkeydaa57e82012-09-19 14:10:39 -0700693 if (!ArrayUtils.contains(uids, entry.uid)) {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700694 stats.addValues(entry);
695 }
696 }
697
698 return stats;
699 }
700
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700701 public void dump(String prefix, PrintWriter pw) {
702 pw.print(prefix);
703 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700704 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700705 pw.print(prefix);
Jeff Sharkey3359aca2011-11-08 18:08:48 -0800706 pw.print(" ["); pw.print(i); pw.print("]");
707 pw.print(" iface="); pw.print(iface[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700708 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700709 pw.print(" set="); pw.print(setToString(set[i]));
710 pw.print(" tag="); pw.print(tagToString(tag[i]));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700711 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700712 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700713 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700714 pw.print(" txPackets="); pw.print(txPackets[i]);
715 pw.print(" operations="); pw.println(operations[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700716 }
717 }
718
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700719 /**
720 * Return text description of {@link #set} value.
721 */
722 public static String setToString(int set) {
723 switch (set) {
724 case SET_ALL:
725 return "ALL";
726 case SET_DEFAULT:
727 return "DEFAULT";
728 case SET_FOREGROUND:
729 return "FOREGROUND";
730 default:
731 return "UNKNOWN";
732 }
733 }
734
735 /**
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800736 * Return text description of {@link #set} value.
737 */
738 public static String setToCheckinString(int set) {
739 switch (set) {
740 case SET_ALL:
741 return "all";
742 case SET_DEFAULT:
743 return "def";
744 case SET_FOREGROUND:
745 return "fg";
746 default:
747 return "unk";
748 }
749 }
750
751 /**
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700752 * Return text description of {@link #tag} value.
753 */
754 public static String tagToString(int tag) {
755 return "0x" + Integer.toHexString(tag);
756 }
757
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700758 @Override
759 public String toString() {
760 final CharArrayWriter writer = new CharArrayWriter();
761 dump("", new PrintWriter(writer));
762 return writer.toString();
763 }
764
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700765 @Override
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700766 public int describeContents() {
767 return 0;
768 }
769
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700770 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700771 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700772 public NetworkStats createFromParcel(Parcel in) {
773 return new NetworkStats(in);
774 }
775
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700776 @Override
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700777 public NetworkStats[] newArray(int size) {
778 return new NetworkStats[size];
779 }
780 };
Jeff Sharkey163e6442011-10-31 16:37:52 -0700781
Jeff Sharkey63abc372012-01-11 18:38:16 -0800782 public interface NonMonotonicObserver<C> {
Jeff Sharkey5a7bcf32012-01-10 17:24:44 -0800783 public void foundNonMonotonic(
Jeff Sharkey63abc372012-01-11 18:38:16 -0800784 NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700785 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700786}