blob: e5549753cc0529f6c2b9b8ed6ed289b90e5c6a9a [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 Sharkey47eb1022011-08-25 17:48:52 -070022import android.util.Log;
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -070023import android.util.SparseBooleanArray;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070024
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 Sharkey47eb1022011-08-25 17:48:52 -070042 private static final String TAG = "NetworkStats";
43
Jeff Sharkey75279902011-05-24 18:39:45 -070044 /** {@link #iface} value when interface details unavailable. */
Jeff Sharkey9a13f362011-04-26 16:25:36 -070045 public static final String IFACE_ALL = null;
Jeff Sharkey75279902011-05-24 18:39:45 -070046 /** {@link #uid} value when UID details unavailable. */
47 public static final int UID_ALL = -1;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070048 /** {@link #set} value when all sets combined. */
49 public static final int SET_ALL = -1;
50 /** {@link #set} value where background data is accounted. */
51 public static final int SET_DEFAULT = 0;
52 /** {@link #set} value where foreground data is accounted. */
53 public static final int SET_FOREGROUND = 1;
54 /** {@link #tag} value for total data across all tags. */
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070055 public static final int TAG_NONE = 0;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070056
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
106 @Override
107 public String toString() {
108 final StringBuilder builder = new StringBuilder();
109 builder.append("iface=").append(iface);
110 builder.append(" uid=").append(uid);
111 builder.append(" set=").append(setToString(set));
112 builder.append(" tag=").append(tagToString(tag));
113 builder.append(" rxBytes=").append(rxBytes);
114 builder.append(" rxPackets=").append(rxPackets);
115 builder.append(" txBytes=").append(txBytes);
116 builder.append(" txPackets=").append(txPackets);
117 builder.append(" operations=").append(operations);
118 return builder.toString();
119 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700120 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700121
Jeff Sharkey4a971222011-06-11 22:16:55 -0700122 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700123 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700124 this.size = 0;
125 this.iface = new String[initialSize];
126 this.uid = new int[initialSize];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700127 this.set = new int[initialSize];
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700128 this.tag = new int[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700129 this.rxBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700130 this.rxPackets = new long[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700131 this.txBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700132 this.txPackets = new long[initialSize];
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700133 this.operations = new long[initialSize];
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700134 }
135
136 public NetworkStats(Parcel parcel) {
137 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700138 size = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700139 iface = parcel.createStringArray();
140 uid = parcel.createIntArray();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700141 set = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700142 tag = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700143 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700144 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700145 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700146 txPackets = parcel.createLongArray();
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700147 operations = parcel.createLongArray();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700148 }
149
150 /** {@inheritDoc} */
151 public void writeToParcel(Parcel dest, int flags) {
152 dest.writeLong(elapsedRealtime);
153 dest.writeInt(size);
154 dest.writeStringArray(iface);
155 dest.writeIntArray(uid);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700156 dest.writeIntArray(set);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700157 dest.writeIntArray(tag);
158 dest.writeLongArray(rxBytes);
159 dest.writeLongArray(rxPackets);
160 dest.writeLongArray(txBytes);
161 dest.writeLongArray(txPackets);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700162 dest.writeLongArray(operations);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700163 }
164
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700165 // @VisibleForTesting
166 public NetworkStats addIfaceValues(
167 String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
168 return addValues(
169 iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700170 }
171
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700172 // @VisibleForTesting
173 public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
174 long rxPackets, long txBytes, long txPackets, long operations) {
175 return addValues(new Entry(
176 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700177 }
178
179 /**
180 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
181 * object can be recycled across multiple calls.
182 */
183 public NetworkStats addValues(Entry entry) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700184 if (size >= this.iface.length) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700185 final int newLength = Math.max(iface.length, 10) * 3 / 2;
186 iface = Arrays.copyOf(iface, newLength);
187 uid = Arrays.copyOf(uid, newLength);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700188 set = Arrays.copyOf(set, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700189 tag = Arrays.copyOf(tag, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700190 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700191 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700192 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700193 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700194 operations = Arrays.copyOf(operations, newLength);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700195 }
196
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700197 iface[size] = entry.iface;
198 uid[size] = entry.uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700199 set[size] = entry.set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700200 tag[size] = entry.tag;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700201 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700202 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700203 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700204 txPackets[size] = entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700205 operations[size] = entry.operations;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700206 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700207
Jeff Sharkey4a971222011-06-11 22:16:55 -0700208 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700209 }
210
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700211 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700212 * Return specific stats entry.
213 */
214 public Entry getValues(int i, Entry recycle) {
215 final Entry entry = recycle != null ? recycle : new Entry();
216 entry.iface = iface[i];
217 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700218 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700219 entry.tag = tag[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700220 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700221 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700222 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700223 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700224 entry.operations = operations[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700225 return entry;
226 }
227
228 public long getElapsedRealtime() {
229 return elapsedRealtime;
230 }
231
232 public int size() {
233 return size;
234 }
235
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700236 // @VisibleForTesting
237 public int internalSize() {
238 return iface.length;
239 }
240
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700241 @Deprecated
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700242 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700243 long txBytes, long txPackets, long operations) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700244 return combineValues(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700245 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations);
246 }
247
248 public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes,
249 long rxPackets, long txBytes, long txPackets, long operations) {
250 return combineValues(new Entry(
251 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700252 }
253
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700254 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700255 * Combine given values with an existing row, or create a new row if
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700256 * {@link #findIndex(String, int, int, int)} is unable to find match. Can
257 * also be used to subtract values from existing rows.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700258 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700259 public NetworkStats combineValues(Entry entry) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700260 final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700261 if (i == -1) {
262 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700263 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700264 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700265 rxBytes[i] += entry.rxBytes;
266 rxPackets[i] += entry.rxPackets;
267 txBytes[i] += entry.txBytes;
268 txPackets[i] += entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700269 operations[i] += entry.operations;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700270 }
271 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700272 }
273
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700274 /**
Jeff Sharkey905b5892011-09-30 15:19:49 -0700275 * Combine all values from another {@link NetworkStats} into this object.
276 */
277 public void combineAllValues(NetworkStats another) {
278 NetworkStats.Entry entry = null;
279 for (int i = 0; i < another.size; i++) {
280 entry = another.getValues(i, entry);
281 combineValues(entry);
282 }
283 }
284
285 /**
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700286 * Find first stats index that matches the requested parameters.
287 */
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700288 public int findIndex(String iface, int uid, int set, int tag) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700289 for (int i = 0; i < size; i++) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700290 if (Objects.equal(iface, this.iface[i]) && uid == this.uid[i] && set == this.set[i]
291 && tag == this.tag[i]) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700292 return i;
293 }
294 }
295 return -1;
296 }
297
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700298 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700299 * Splice in {@link #operations} from the given {@link NetworkStats} based
300 * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
301 * since operation counts are at data layer.
302 */
303 public void spliceOperationsFrom(NetworkStats stats) {
304 for (int i = 0; i < size; i++) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700305 final int j = stats.findIndex(IFACE_ALL, uid[i], set[i], tag[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700306 if (j == -1) {
307 operations[i] = 0;
308 } else {
309 operations[i] = stats.operations[j];
310 }
311 }
312 }
313
314 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700315 * Return list of unique interfaces known by this data structure.
316 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700317 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700318 final HashSet<String> ifaces = new HashSet<String>();
319 for (String iface : this.iface) {
320 if (iface != IFACE_ALL) {
321 ifaces.add(iface);
322 }
323 }
324 return ifaces.toArray(new String[ifaces.size()]);
325 }
326
327 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700328 * Return list of unique UIDs known by this data structure.
329 */
330 public int[] getUniqueUids() {
331 final SparseBooleanArray uids = new SparseBooleanArray();
332 for (int uid : this.uid) {
333 uids.put(uid, true);
334 }
335
336 final int size = uids.size();
337 final int[] result = new int[size];
338 for (int i = 0; i < size; i++) {
339 result[i] = uids.keyAt(i);
340 }
341 return result;
342 }
343
344 /**
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700345 * Return total bytes represented by this snapshot object, usually used when
346 * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
347 */
348 public long getTotalBytes() {
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700349 final Entry entry = getTotal(null);
350 return entry.rxBytes + entry.txBytes;
351 }
352
353 /**
354 * Return total of all fields represented by this snapshot object.
355 */
356 public Entry getTotal(Entry recycle) {
357 final Entry entry = recycle != null ? recycle : new Entry();
358
359 entry.iface = IFACE_ALL;
360 entry.uid = UID_ALL;
361 entry.set = SET_ALL;
362 entry.tag = TAG_NONE;
363 entry.rxBytes = 0;
364 entry.rxPackets = 0;
365 entry.txBytes = 0;
366 entry.txPackets = 0;
367
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700368 for (int i = 0; i < size; i++) {
369 // skip specific tags, since already counted in TAG_NONE
370 if (tag[i] != TAG_NONE) continue;
371
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700372 entry.rxBytes += rxBytes[i];
373 entry.rxPackets += rxPackets[i];
374 entry.txBytes += txBytes[i];
375 entry.txPackets += txPackets[i];
376 entry.operations += operations[i];
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700377 }
Jeff Sharkey07b0dd92011-09-01 13:06:19 -0700378 return entry;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700379 }
380
381 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700382 * Subtract the given {@link NetworkStats}, effectively leaving the delta
383 * between two snapshots in time. Assumes that statistics rows collect over
384 * time, and that none of them have disappeared.
Jeff Sharkey75279902011-05-24 18:39:45 -0700385 *
Jeff Sharkey3f391352011-06-05 17:42:53 -0700386 * @throws IllegalArgumentException when given {@link NetworkStats} is
387 * non-monotonic.
388 */
389 public NetworkStats subtract(NetworkStats value) {
390 return subtract(value, true, false);
391 }
392
393 /**
394 * Subtract the given {@link NetworkStats}, effectively leaving the delta
395 * between two snapshots in time. Assumes that statistics rows collect over
396 * time, and that none of them have disappeared.
397 * <p>
398 * Instead of throwing when counters are non-monotonic, this variant clamps
399 * results to never be negative.
400 */
401 public NetworkStats subtractClamped(NetworkStats value) {
402 return subtract(value, false, true);
403 }
404
405 /**
406 * Subtract the given {@link NetworkStats}, effectively leaving the delta
407 * between two snapshots in time. Assumes that statistics rows collect over
408 * time, and that none of them have disappeared.
409 *
Jeff Sharkey75279902011-05-24 18:39:45 -0700410 * @param enforceMonotonic Validate that incoming value is strictly
411 * monotonic compared to this object.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700412 * @param clampNegative Instead of throwing like {@code enforceMonotonic},
413 * clamp resulting counters at 0 to prevent negative values.
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700414 */
Jeff Sharkey3f391352011-06-05 17:42:53 -0700415 private NetworkStats subtract(
416 NetworkStats value, boolean enforceMonotonic, boolean clampNegative) {
Jeff Sharkey75279902011-05-24 18:39:45 -0700417 final long deltaRealtime = this.elapsedRealtime - value.elapsedRealtime;
418 if (enforceMonotonic && deltaRealtime < 0) {
419 throw new IllegalArgumentException("found non-monotonic realtime");
420 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700421
Jeff Sharkey75279902011-05-24 18:39:45 -0700422 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700423 final Entry entry = new Entry();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700424 final NetworkStats result = new NetworkStats(deltaRealtime, size);
425 for (int i = 0; i < size; i++) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700426 entry.iface = iface[i];
427 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700428 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700429 entry.tag = tag[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700430
431 // find remote row that matches, and subtract
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700432 final int j = value.findIndex(entry.iface, entry.uid, entry.set, entry.tag);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700433 if (j == -1) {
434 // newly appearing row, return entire value
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700435 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700436 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700437 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700438 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700439 entry.operations = operations[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700440 } else {
441 // existing row, subtract remote value
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700442 entry.rxBytes = rxBytes[i] - value.rxBytes[j];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700443 entry.rxPackets = rxPackets[i] - value.rxPackets[j];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700444 entry.txBytes = txBytes[i] - value.txBytes[j];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700445 entry.txPackets = txPackets[i] - value.txPackets[j];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700446 entry.operations = operations[i] - value.operations[j];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700447 if (enforceMonotonic
448 && (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700449 || entry.txPackets < 0 || entry.operations < 0)) {
Jeff Sharkey47eb1022011-08-25 17:48:52 -0700450 Log.v(TAG, "lhs=" + this);
451 Log.v(TAG, "rhs=" + value);
452 throw new IllegalArgumentException(
453 "found non-monotonic values at lhs[" + i + "] - rhs[" + j + "]");
Jeff Sharkey75279902011-05-24 18:39:45 -0700454 }
Jeff Sharkey3f391352011-06-05 17:42:53 -0700455 if (clampNegative) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700456 entry.rxBytes = Math.max(0, entry.rxBytes);
457 entry.rxPackets = Math.max(0, entry.rxPackets);
458 entry.txBytes = Math.max(0, entry.txBytes);
459 entry.txPackets = Math.max(0, entry.txPackets);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700460 entry.operations = Math.max(0, entry.operations);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700461 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700462 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700463
464 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700465 }
466
Jeff Sharkey4a971222011-06-11 22:16:55 -0700467 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700468 }
469
Jeff Sharkey905b5892011-09-30 15:19:49 -0700470 /**
471 * Return total statistics grouped by {@link #iface}; doesn't mutate the
472 * original structure.
473 */
474 public NetworkStats groupedByIface() {
475 final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
476
477 final Entry entry = new Entry();
478 entry.uid = UID_ALL;
479 entry.set = SET_ALL;
480 entry.tag = TAG_NONE;
481 entry.operations = 0L;
482
483 for (int i = 0; i < size; i++) {
484 // skip specific tags, since already counted in TAG_NONE
485 if (tag[i] != TAG_NONE) continue;
486
487 entry.iface = iface[i];
488 entry.rxBytes = rxBytes[i];
489 entry.rxPackets = rxPackets[i];
490 entry.txBytes = txBytes[i];
491 entry.txPackets = txPackets[i];
492 stats.combineValues(entry);
493 }
494
495 return stats;
496 }
497
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700498 public void dump(String prefix, PrintWriter pw) {
499 pw.print(prefix);
500 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700501 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700502 pw.print(prefix);
503 pw.print(" iface="); pw.print(iface[i]);
504 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700505 pw.print(" set="); pw.print(setToString(set[i]));
506 pw.print(" tag="); pw.print(tagToString(tag[i]));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700507 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700508 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700509 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700510 pw.print(" txPackets="); pw.print(txPackets[i]);
511 pw.print(" operations="); pw.println(operations[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700512 }
513 }
514
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700515 /**
516 * Return text description of {@link #set} value.
517 */
518 public static String setToString(int set) {
519 switch (set) {
520 case SET_ALL:
521 return "ALL";
522 case SET_DEFAULT:
523 return "DEFAULT";
524 case SET_FOREGROUND:
525 return "FOREGROUND";
526 default:
527 return "UNKNOWN";
528 }
529 }
530
531 /**
532 * Return text description of {@link #tag} value.
533 */
534 public static String tagToString(int tag) {
535 return "0x" + Integer.toHexString(tag);
536 }
537
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700538 @Override
539 public String toString() {
540 final CharArrayWriter writer = new CharArrayWriter();
541 dump("", new PrintWriter(writer));
542 return writer.toString();
543 }
544
545 /** {@inheritDoc} */
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700546 public int describeContents() {
547 return 0;
548 }
549
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700550 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
551 public NetworkStats createFromParcel(Parcel in) {
552 return new NetworkStats(in);
553 }
554
555 public NetworkStats[] newArray(int size) {
556 return new NetworkStats[size];
557 }
558 };
559}