NetworkStats.java revision d0c5b9abed60b7bc056d026bf0f2b2235410fb70
1/*
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;
22import android.util.SparseBooleanArray;
23
24import com.android.internal.annotations.VisibleForTesting;
25import com.android.internal.util.ArrayUtils;
26
27import java.io.CharArrayWriter;
28import java.io.PrintWriter;
29import java.util.Arrays;
30import java.util.HashSet;
31import java.util.Objects;
32
33/**
34 * Collection of active network statistics. Can contain summary details across
35 * all interfaces, or details with per-UID granularity. Internally stores data
36 * as a large table, closely matching {@code /proc/} data format. This structure
37 * optimizes for rapid in-memory comparison, but consider using
38 * {@link NetworkStatsHistory} when persisting.
39 *
40 * @hide
41 */
42public class NetworkStats implements Parcelable {
43    /** {@link #iface} value when interface details unavailable. */
44    public static final String IFACE_ALL = null;
45    /** {@link #uid} value when UID details unavailable. */
46    public static final int UID_ALL = -1;
47    /** {@link #tag} value matching any tag. */
48    public static final int TAG_ALL = -1;
49    /** {@link #set} value when all sets combined. */
50    public static final int SET_ALL = -1;
51    /** {@link #set} value where background data is accounted. */
52    public static final int SET_DEFAULT = 0;
53    /** {@link #set} value where foreground data is accounted. */
54    public static final int SET_FOREGROUND = 1;
55    /** {@link #tag} value for total data across all tags. */
56    public static final int TAG_NONE = 0;
57
58    // TODO: move fields to "mVariable" notation
59
60    /**
61     * {@link SystemClock#elapsedRealtime()} timestamp when this data was
62     * generated.
63     */
64    private long elapsedRealtime;
65    private int size;
66    private int capacity;
67    private String[] iface;
68    private int[] uid;
69    private int[] set;
70    private int[] tag;
71    private long[] rxBytes;
72    private long[] rxPackets;
73    private long[] txBytes;
74    private long[] txPackets;
75    private long[] operations;
76
77    public static class Entry {
78        public String iface;
79        public int uid;
80        public int set;
81        public int tag;
82        public long rxBytes;
83        public long rxPackets;
84        public long txBytes;
85        public long txPackets;
86        public long operations;
87
88        public Entry() {
89            this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
90        }
91
92        public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
93            this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
94                    operations);
95        }
96
97        public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets,
98                long txBytes, long txPackets, long operations) {
99            this.iface = iface;
100            this.uid = uid;
101            this.set = set;
102            this.tag = tag;
103            this.rxBytes = rxBytes;
104            this.rxPackets = rxPackets;
105            this.txBytes = txBytes;
106            this.txPackets = txPackets;
107            this.operations = operations;
108        }
109
110        public boolean isNegative() {
111            return rxBytes < 0 || rxPackets < 0 || txBytes < 0 || txPackets < 0 || operations < 0;
112        }
113
114        public boolean isEmpty() {
115            return rxBytes == 0 && rxPackets == 0 && txBytes == 0 && txPackets == 0
116                    && operations == 0;
117        }
118
119        public void add(Entry another) {
120            this.rxBytes += another.rxBytes;
121            this.rxPackets += another.rxPackets;
122            this.txBytes += another.txBytes;
123            this.txPackets += another.txPackets;
124            this.operations += another.operations;
125        }
126
127        @Override
128        public String toString() {
129            final StringBuilder builder = new StringBuilder();
130            builder.append("iface=").append(iface);
131            builder.append(" uid=").append(uid);
132            builder.append(" set=").append(setToString(set));
133            builder.append(" tag=").append(tagToString(tag));
134            builder.append(" rxBytes=").append(rxBytes);
135            builder.append(" rxPackets=").append(rxPackets);
136            builder.append(" txBytes=").append(txBytes);
137            builder.append(" txPackets=").append(txPackets);
138            builder.append(" operations=").append(operations);
139            return builder.toString();
140        }
141
142        @Override
143        public boolean equals(Object o) {
144            if (o instanceof Entry) {
145                final Entry e = (Entry) o;
146                return uid == e.uid && set == e.set && tag == e.tag && rxBytes == e.rxBytes
147                        && rxPackets == e.rxPackets && txBytes == e.txBytes
148                        && txPackets == e.txPackets && operations == e.operations
149                        && iface.equals(e.iface);
150            }
151            return false;
152        }
153    }
154
155    public NetworkStats(long elapsedRealtime, int initialSize) {
156        this.elapsedRealtime = elapsedRealtime;
157        this.size = 0;
158        if (initialSize >= 0) {
159            this.capacity = initialSize;
160            this.iface = new String[initialSize];
161            this.uid = new int[initialSize];
162            this.set = new int[initialSize];
163            this.tag = new int[initialSize];
164            this.rxBytes = new long[initialSize];
165            this.rxPackets = new long[initialSize];
166            this.txBytes = new long[initialSize];
167            this.txPackets = new long[initialSize];
168            this.operations = new long[initialSize];
169        } else {
170            // Special case for use by NetworkStatsFactory to start out *really* empty.
171            this.capacity = 0;
172        }
173    }
174
175    public NetworkStats(Parcel parcel) {
176        elapsedRealtime = parcel.readLong();
177        size = parcel.readInt();
178        capacity = parcel.readInt();
179        iface = parcel.createStringArray();
180        uid = parcel.createIntArray();
181        set = parcel.createIntArray();
182        tag = parcel.createIntArray();
183        rxBytes = parcel.createLongArray();
184        rxPackets = parcel.createLongArray();
185        txBytes = parcel.createLongArray();
186        txPackets = parcel.createLongArray();
187        operations = parcel.createLongArray();
188    }
189
190    @Override
191    public void writeToParcel(Parcel dest, int flags) {
192        dest.writeLong(elapsedRealtime);
193        dest.writeInt(size);
194        dest.writeInt(capacity);
195        dest.writeStringArray(iface);
196        dest.writeIntArray(uid);
197        dest.writeIntArray(set);
198        dest.writeIntArray(tag);
199        dest.writeLongArray(rxBytes);
200        dest.writeLongArray(rxPackets);
201        dest.writeLongArray(txBytes);
202        dest.writeLongArray(txPackets);
203        dest.writeLongArray(operations);
204    }
205
206    @Override
207    public NetworkStats clone() {
208        final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
209        NetworkStats.Entry entry = null;
210        for (int i = 0; i < size; i++) {
211            entry = getValues(i, entry);
212            clone.addValues(entry);
213        }
214        return clone;
215    }
216
217    @VisibleForTesting
218    public NetworkStats addIfaceValues(
219            String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
220        return addValues(
221                iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
222    }
223
224    @VisibleForTesting
225    public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
226            long rxPackets, long txBytes, long txPackets, long operations) {
227        return addValues(new Entry(
228                iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
229    }
230
231    /**
232     * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
233     * object can be recycled across multiple calls.
234     */
235    public NetworkStats addValues(Entry entry) {
236        if (size >= capacity) {
237            final int newLength = Math.max(size, 10) * 3 / 2;
238            iface = Arrays.copyOf(iface, newLength);
239            uid = Arrays.copyOf(uid, newLength);
240            set = Arrays.copyOf(set, newLength);
241            tag = Arrays.copyOf(tag, newLength);
242            rxBytes = Arrays.copyOf(rxBytes, newLength);
243            rxPackets = Arrays.copyOf(rxPackets, newLength);
244            txBytes = Arrays.copyOf(txBytes, newLength);
245            txPackets = Arrays.copyOf(txPackets, newLength);
246            operations = Arrays.copyOf(operations, newLength);
247            capacity = newLength;
248        }
249
250        iface[size] = entry.iface;
251        uid[size] = entry.uid;
252        set[size] = entry.set;
253        tag[size] = entry.tag;
254        rxBytes[size] = entry.rxBytes;
255        rxPackets[size] = entry.rxPackets;
256        txBytes[size] = entry.txBytes;
257        txPackets[size] = entry.txPackets;
258        operations[size] = entry.operations;
259        size++;
260
261        return this;
262    }
263
264    /**
265     * Return specific stats entry.
266     */
267    public Entry getValues(int i, Entry recycle) {
268        final Entry entry = recycle != null ? recycle : new Entry();
269        entry.iface = iface[i];
270        entry.uid = uid[i];
271        entry.set = set[i];
272        entry.tag = tag[i];
273        entry.rxBytes = rxBytes[i];
274        entry.rxPackets = rxPackets[i];
275        entry.txBytes = txBytes[i];
276        entry.txPackets = txPackets[i];
277        entry.operations = operations[i];
278        return entry;
279    }
280
281    public long getElapsedRealtime() {
282        return elapsedRealtime;
283    }
284
285    public void setElapsedRealtime(long time) {
286        elapsedRealtime = time;
287    }
288
289    /**
290     * Return age of this {@link NetworkStats} object with respect to
291     * {@link SystemClock#elapsedRealtime()}.
292     */
293    public long getElapsedRealtimeAge() {
294        return SystemClock.elapsedRealtime() - elapsedRealtime;
295    }
296
297    public int size() {
298        return size;
299    }
300
301    @VisibleForTesting
302    public int internalSize() {
303        return capacity;
304    }
305
306    @Deprecated
307    public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
308            long txBytes, long txPackets, long operations) {
309        return combineValues(
310                iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations);
311    }
312
313    public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes,
314            long rxPackets, long txBytes, long txPackets, long operations) {
315        return combineValues(new Entry(
316                iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
317    }
318
319    /**
320     * Combine given values with an existing row, or create a new row if
321     * {@link #findIndex(String, int, int, int)} is unable to find match. Can
322     * also be used to subtract values from existing rows.
323     */
324    public NetworkStats combineValues(Entry entry) {
325        final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag);
326        if (i == -1) {
327            // only create new entry when positive contribution
328            addValues(entry);
329        } else {
330            rxBytes[i] += entry.rxBytes;
331            rxPackets[i] += entry.rxPackets;
332            txBytes[i] += entry.txBytes;
333            txPackets[i] += entry.txPackets;
334            operations[i] += entry.operations;
335        }
336        return this;
337    }
338
339    /**
340     * Combine all values from another {@link NetworkStats} into this object.
341     */
342    public void combineAllValues(NetworkStats another) {
343        NetworkStats.Entry entry = null;
344        for (int i = 0; i < another.size; i++) {
345            entry = another.getValues(i, entry);
346            combineValues(entry);
347        }
348    }
349
350    /**
351     * Find first stats index that matches the requested parameters.
352     */
353    public int findIndex(String iface, int uid, int set, int tag) {
354        for (int i = 0; i < size; i++) {
355            if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
356                    && Objects.equals(iface, this.iface[i])) {
357                return i;
358            }
359        }
360        return -1;
361    }
362
363    /**
364     * Find first stats index that matches the requested parameters, starting
365     * search around the hinted index as an optimization.
366     */
367    @VisibleForTesting
368    public int findIndexHinted(String iface, int uid, int set, int tag, int hintIndex) {
369        for (int offset = 0; offset < size; offset++) {
370            final int halfOffset = offset / 2;
371
372            // search outwards from hint index, alternating forward and backward
373            final int i;
374            if (offset % 2 == 0) {
375                i = (hintIndex + halfOffset) % size;
376            } else {
377                i = (size + hintIndex - halfOffset - 1) % size;
378            }
379
380            if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i]
381                    && Objects.equals(iface, this.iface[i])) {
382                return i;
383            }
384        }
385        return -1;
386    }
387
388    /**
389     * Splice in {@link #operations} from the given {@link NetworkStats} based
390     * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
391     * since operation counts are at data layer.
392     */
393    public void spliceOperationsFrom(NetworkStats stats) {
394        for (int i = 0; i < size; i++) {
395            final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i]);
396            if (j == -1) {
397                operations[i] = 0;
398            } else {
399                operations[i] = stats.operations[j];
400            }
401        }
402    }
403
404    /**
405     * Return list of unique interfaces known by this data structure.
406     */
407    public String[] getUniqueIfaces() {
408        final HashSet<String> ifaces = new HashSet<String>();
409        for (String iface : this.iface) {
410            if (iface != IFACE_ALL) {
411                ifaces.add(iface);
412            }
413        }
414        return ifaces.toArray(new String[ifaces.size()]);
415    }
416
417    /**
418     * Return list of unique UIDs known by this data structure.
419     */
420    public int[] getUniqueUids() {
421        final SparseBooleanArray uids = new SparseBooleanArray();
422        for (int uid : this.uid) {
423            uids.put(uid, true);
424        }
425
426        final int size = uids.size();
427        final int[] result = new int[size];
428        for (int i = 0; i < size; i++) {
429            result[i] = uids.keyAt(i);
430        }
431        return result;
432    }
433
434    /**
435     * Return total bytes represented by this snapshot object, usually used when
436     * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
437     */
438    public long getTotalBytes() {
439        final Entry entry = getTotal(null);
440        return entry.rxBytes + entry.txBytes;
441    }
442
443    /**
444     * Return total of all fields represented by this snapshot object.
445     */
446    public Entry getTotal(Entry recycle) {
447        return getTotal(recycle, null, UID_ALL, false);
448    }
449
450    /**
451     * Return total of all fields represented by this snapshot object matching
452     * the requested {@link #uid}.
453     */
454    public Entry getTotal(Entry recycle, int limitUid) {
455        return getTotal(recycle, null, limitUid, false);
456    }
457
458    /**
459     * Return total of all fields represented by this snapshot object matching
460     * the requested {@link #iface}.
461     */
462    public Entry getTotal(Entry recycle, HashSet<String> limitIface) {
463        return getTotal(recycle, limitIface, UID_ALL, false);
464    }
465
466    public Entry getTotalIncludingTags(Entry recycle) {
467        return getTotal(recycle, null, UID_ALL, true);
468    }
469
470    /**
471     * Return total of all fields represented by this snapshot object matching
472     * the requested {@link #iface} and {@link #uid}.
473     *
474     * @param limitIface Set of {@link #iface} to include in total; or {@code
475     *            null} to include all ifaces.
476     */
477    private Entry getTotal(
478            Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags) {
479        final Entry entry = recycle != null ? recycle : new Entry();
480
481        entry.iface = IFACE_ALL;
482        entry.uid = limitUid;
483        entry.set = SET_ALL;
484        entry.tag = TAG_NONE;
485        entry.rxBytes = 0;
486        entry.rxPackets = 0;
487        entry.txBytes = 0;
488        entry.txPackets = 0;
489        entry.operations = 0;
490
491        for (int i = 0; i < size; i++) {
492            final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]);
493            final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i]));
494
495            if (matchesUid && matchesIface) {
496                // skip specific tags, since already counted in TAG_NONE
497                if (tag[i] != TAG_NONE && !includeTags) continue;
498
499                entry.rxBytes += rxBytes[i];
500                entry.rxPackets += rxPackets[i];
501                entry.txBytes += txBytes[i];
502                entry.txPackets += txPackets[i];
503                entry.operations += operations[i];
504            }
505        }
506        return entry;
507    }
508
509    /**
510     * Subtract the given {@link NetworkStats}, effectively leaving the delta
511     * between two snapshots in time. Assumes that statistics rows collect over
512     * time, and that none of them have disappeared.
513     */
514    public NetworkStats subtract(NetworkStats right) {
515        return subtract(this, right, null, null);
516    }
517
518    /**
519     * Subtract the two given {@link NetworkStats} objects, returning the delta
520     * between two snapshots in time. Assumes that statistics rows collect over
521     * time, and that none of them have disappeared.
522     * <p>
523     * If counters have rolled backwards, they are clamped to {@code 0} and
524     * reported to the given {@link NonMonotonicObserver}.
525     */
526    public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
527            NonMonotonicObserver<C> observer, C cookie) {
528        return subtract(left, right, observer, cookie, null);
529    }
530
531    /**
532     * Subtract the two given {@link NetworkStats} objects, returning the delta
533     * between two snapshots in time. Assumes that statistics rows collect over
534     * time, and that none of them have disappeared.
535     * <p>
536     * If counters have rolled backwards, they are clamped to {@code 0} and
537     * reported to the given {@link NonMonotonicObserver}.
538     * <p>
539     * If <var>recycle</var> is supplied, this NetworkStats object will be
540     * reused (and returned) as the result if it is large enough to contain
541     * the data.
542     */
543    public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right,
544            NonMonotonicObserver<C> observer, C cookie, NetworkStats recycle) {
545        long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime;
546        if (deltaRealtime < 0) {
547            if (observer != null) {
548                observer.foundNonMonotonic(left, -1, right, -1, cookie);
549            }
550            deltaRealtime = 0;
551        }
552
553        // result will have our rows, and elapsed time between snapshots
554        final Entry entry = new Entry();
555        final NetworkStats result;
556        if (recycle != null && recycle.capacity >= left.size) {
557            result = recycle;
558            result.size = 0;
559            result.elapsedRealtime = deltaRealtime;
560        } else {
561            result = new NetworkStats(deltaRealtime, left.size);
562        }
563        for (int i = 0; i < left.size; i++) {
564            entry.iface = left.iface[i];
565            entry.uid = left.uid[i];
566            entry.set = left.set[i];
567            entry.tag = left.tag[i];
568
569            // find remote row that matches, and subtract
570            final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag, i);
571            if (j == -1) {
572                // newly appearing row, return entire value
573                entry.rxBytes = left.rxBytes[i];
574                entry.rxPackets = left.rxPackets[i];
575                entry.txBytes = left.txBytes[i];
576                entry.txPackets = left.txPackets[i];
577                entry.operations = left.operations[i];
578            } else {
579                // existing row, subtract remote value
580                entry.rxBytes = left.rxBytes[i] - right.rxBytes[j];
581                entry.rxPackets = left.rxPackets[i] - right.rxPackets[j];
582                entry.txBytes = left.txBytes[i] - right.txBytes[j];
583                entry.txPackets = left.txPackets[i] - right.txPackets[j];
584                entry.operations = left.operations[i] - right.operations[j];
585
586                if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
587                        || entry.txPackets < 0 || entry.operations < 0) {
588                    if (observer != null) {
589                        observer.foundNonMonotonic(left, i, right, j, cookie);
590                    }
591                    entry.rxBytes = Math.max(entry.rxBytes, 0);
592                    entry.rxPackets = Math.max(entry.rxPackets, 0);
593                    entry.txBytes = Math.max(entry.txBytes, 0);
594                    entry.txPackets = Math.max(entry.txPackets, 0);
595                    entry.operations = Math.max(entry.operations, 0);
596                }
597            }
598
599            result.addValues(entry);
600        }
601
602        return result;
603    }
604
605    /**
606     * Return total statistics grouped by {@link #iface}; doesn't mutate the
607     * original structure.
608     */
609    public NetworkStats groupedByIface() {
610        final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
611
612        final Entry entry = new Entry();
613        entry.uid = UID_ALL;
614        entry.set = SET_ALL;
615        entry.tag = TAG_NONE;
616        entry.operations = 0L;
617
618        for (int i = 0; i < size; i++) {
619            // skip specific tags, since already counted in TAG_NONE
620            if (tag[i] != TAG_NONE) continue;
621
622            entry.iface = iface[i];
623            entry.rxBytes = rxBytes[i];
624            entry.rxPackets = rxPackets[i];
625            entry.txBytes = txBytes[i];
626            entry.txPackets = txPackets[i];
627            stats.combineValues(entry);
628        }
629
630        return stats;
631    }
632
633    /**
634     * Return total statistics grouped by {@link #uid}; doesn't mutate the
635     * original structure.
636     */
637    public NetworkStats groupedByUid() {
638        final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
639
640        final Entry entry = new Entry();
641        entry.iface = IFACE_ALL;
642        entry.set = SET_ALL;
643        entry.tag = TAG_NONE;
644
645        for (int i = 0; i < size; i++) {
646            // skip specific tags, since already counted in TAG_NONE
647            if (tag[i] != TAG_NONE) continue;
648
649            entry.uid = uid[i];
650            entry.rxBytes = rxBytes[i];
651            entry.rxPackets = rxPackets[i];
652            entry.txBytes = txBytes[i];
653            entry.txPackets = txPackets[i];
654            entry.operations = operations[i];
655            stats.combineValues(entry);
656        }
657
658        return stats;
659    }
660
661    /**
662     * Return all rows except those attributed to the requested UID; doesn't
663     * mutate the original structure.
664     */
665    public NetworkStats withoutUids(int[] uids) {
666        final NetworkStats stats = new NetworkStats(elapsedRealtime, 10);
667
668        Entry entry = new Entry();
669        for (int i = 0; i < size; i++) {
670            entry = getValues(i, entry);
671            if (!ArrayUtils.contains(uids, entry.uid)) {
672                stats.addValues(entry);
673            }
674        }
675
676        return stats;
677    }
678
679    public void dump(String prefix, PrintWriter pw) {
680        pw.print(prefix);
681        pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
682        for (int i = 0; i < size; i++) {
683            pw.print(prefix);
684            pw.print("  ["); pw.print(i); pw.print("]");
685            pw.print(" iface="); pw.print(iface[i]);
686            pw.print(" uid="); pw.print(uid[i]);
687            pw.print(" set="); pw.print(setToString(set[i]));
688            pw.print(" tag="); pw.print(tagToString(tag[i]));
689            pw.print(" rxBytes="); pw.print(rxBytes[i]);
690            pw.print(" rxPackets="); pw.print(rxPackets[i]);
691            pw.print(" txBytes="); pw.print(txBytes[i]);
692            pw.print(" txPackets="); pw.print(txPackets[i]);
693            pw.print(" operations="); pw.println(operations[i]);
694        }
695    }
696
697    /**
698     * Return text description of {@link #set} value.
699     */
700    public static String setToString(int set) {
701        switch (set) {
702            case SET_ALL:
703                return "ALL";
704            case SET_DEFAULT:
705                return "DEFAULT";
706            case SET_FOREGROUND:
707                return "FOREGROUND";
708            default:
709                return "UNKNOWN";
710        }
711    }
712
713    /**
714     * Return text description of {@link #tag} value.
715     */
716    public static String tagToString(int tag) {
717        return "0x" + Integer.toHexString(tag);
718    }
719
720    @Override
721    public String toString() {
722        final CharArrayWriter writer = new CharArrayWriter();
723        dump("", new PrintWriter(writer));
724        return writer.toString();
725    }
726
727    @Override
728    public int describeContents() {
729        return 0;
730    }
731
732    public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
733        @Override
734        public NetworkStats createFromParcel(Parcel in) {
735            return new NetworkStats(in);
736        }
737
738        @Override
739        public NetworkStats[] newArray(int size) {
740            return new NetworkStats[size];
741        }
742    };
743
744    public interface NonMonotonicObserver<C> {
745        public void foundNonMonotonic(
746                NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie);
747    }
748}
749