NetworkStats.java revision b5d55e302d2253e4bfb233ea705caf258cdc4cb9
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.util.Objects;
25
26import java.io.CharArrayWriter;
27import java.io.PrintWriter;
28import java.util.Arrays;
29import java.util.HashSet;
30
31/**
32 * Collection of active network statistics. Can contain summary details across
33 * all interfaces, or details with per-UID granularity. Internally stores data
34 * as a large table, closely matching {@code /proc/} data format. This structure
35 * optimizes for rapid in-memory comparison, but consider using
36 * {@link NetworkStatsHistory} when persisting.
37 *
38 * @hide
39 */
40public class NetworkStats implements Parcelable {
41    /** {@link #iface} value when interface details unavailable. */
42    public static final String IFACE_ALL = null;
43    /** {@link #uid} value when UID details unavailable. */
44    public static final int UID_ALL = -1;
45    /** {@link #set} value when all sets combined. */
46    public static final int SET_ALL = -1;
47    /** {@link #set} value where background data is accounted. */
48    public static final int SET_DEFAULT = 0;
49    /** {@link #set} value where foreground data is accounted. */
50    public static final int SET_FOREGROUND = 1;
51    /** {@link #tag} value for total data across all tags. */
52    public static final int TAG_NONE = 0;
53
54    /**
55     * {@link SystemClock#elapsedRealtime()} timestamp when this data was
56     * generated.
57     */
58    private final long elapsedRealtime;
59    private int size;
60    private String[] iface;
61    private int[] uid;
62    private int[] set;
63    private int[] tag;
64    private long[] rxBytes;
65    private long[] rxPackets;
66    private long[] txBytes;
67    private long[] txPackets;
68    private long[] operations;
69
70    public static class Entry {
71        public String iface;
72        public int uid;
73        public int set;
74        public int tag;
75        public long rxBytes;
76        public long rxPackets;
77        public long txBytes;
78        public long txPackets;
79        public long operations;
80
81        public Entry() {
82            this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
83        }
84
85        public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
86            this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
87                    operations);
88        }
89
90        public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets,
91                long txBytes, long txPackets, long operations) {
92            this.iface = iface;
93            this.uid = uid;
94            this.set = set;
95            this.tag = tag;
96            this.rxBytes = rxBytes;
97            this.rxPackets = rxPackets;
98            this.txBytes = txBytes;
99            this.txPackets = txPackets;
100            this.operations = operations;
101        }
102    }
103
104    public NetworkStats(long elapsedRealtime, int initialSize) {
105        this.elapsedRealtime = elapsedRealtime;
106        this.size = 0;
107        this.iface = new String[initialSize];
108        this.uid = new int[initialSize];
109        this.set = new int[initialSize];
110        this.tag = new int[initialSize];
111        this.rxBytes = new long[initialSize];
112        this.rxPackets = new long[initialSize];
113        this.txBytes = new long[initialSize];
114        this.txPackets = new long[initialSize];
115        this.operations = new long[initialSize];
116    }
117
118    public NetworkStats(Parcel parcel) {
119        elapsedRealtime = parcel.readLong();
120        size = parcel.readInt();
121        iface = parcel.createStringArray();
122        uid = parcel.createIntArray();
123        set = parcel.createIntArray();
124        tag = parcel.createIntArray();
125        rxBytes = parcel.createLongArray();
126        rxPackets = parcel.createLongArray();
127        txBytes = parcel.createLongArray();
128        txPackets = parcel.createLongArray();
129        operations = parcel.createLongArray();
130    }
131
132    /** {@inheritDoc} */
133    public void writeToParcel(Parcel dest, int flags) {
134        dest.writeLong(elapsedRealtime);
135        dest.writeInt(size);
136        dest.writeStringArray(iface);
137        dest.writeIntArray(uid);
138        dest.writeIntArray(set);
139        dest.writeIntArray(tag);
140        dest.writeLongArray(rxBytes);
141        dest.writeLongArray(rxPackets);
142        dest.writeLongArray(txBytes);
143        dest.writeLongArray(txPackets);
144        dest.writeLongArray(operations);
145    }
146
147    // @VisibleForTesting
148    public NetworkStats addIfaceValues(
149            String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
150        return addValues(
151                iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
152    }
153
154    // @VisibleForTesting
155    public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
156            long rxPackets, long txBytes, long txPackets, long operations) {
157        return addValues(new Entry(
158                iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
159    }
160
161    /**
162     * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
163     * object can be recycled across multiple calls.
164     */
165    public NetworkStats addValues(Entry entry) {
166        if (size >= this.iface.length) {
167            final int newLength = Math.max(iface.length, 10) * 3 / 2;
168            iface = Arrays.copyOf(iface, newLength);
169            uid = Arrays.copyOf(uid, newLength);
170            set = Arrays.copyOf(set, newLength);
171            tag = Arrays.copyOf(tag, newLength);
172            rxBytes = Arrays.copyOf(rxBytes, newLength);
173            rxPackets = Arrays.copyOf(rxPackets, newLength);
174            txBytes = Arrays.copyOf(txBytes, newLength);
175            txPackets = Arrays.copyOf(txPackets, newLength);
176            operations = Arrays.copyOf(operations, newLength);
177        }
178
179        iface[size] = entry.iface;
180        uid[size] = entry.uid;
181        set[size] = entry.set;
182        tag[size] = entry.tag;
183        rxBytes[size] = entry.rxBytes;
184        rxPackets[size] = entry.rxPackets;
185        txBytes[size] = entry.txBytes;
186        txPackets[size] = entry.txPackets;
187        operations[size] = entry.operations;
188        size++;
189
190        return this;
191    }
192
193    /**
194     * Return specific stats entry.
195     */
196    public Entry getValues(int i, Entry recycle) {
197        final Entry entry = recycle != null ? recycle : new Entry();
198        entry.iface = iface[i];
199        entry.uid = uid[i];
200        entry.set = set[i];
201        entry.tag = tag[i];
202        entry.rxBytes = rxBytes[i];
203        entry.rxPackets = rxPackets[i];
204        entry.txBytes = txBytes[i];
205        entry.txPackets = txPackets[i];
206        entry.operations = operations[i];
207        return entry;
208    }
209
210    public long getElapsedRealtime() {
211        return elapsedRealtime;
212    }
213
214    public int size() {
215        return size;
216    }
217
218    // @VisibleForTesting
219    public int internalSize() {
220        return iface.length;
221    }
222
223    @Deprecated
224    public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
225            long txBytes, long txPackets, long operations) {
226        return combineValues(
227                iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations);
228    }
229
230    public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes,
231            long rxPackets, long txBytes, long txPackets, long operations) {
232        return combineValues(new Entry(
233                iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
234    }
235
236    /**
237     * Combine given values with an existing row, or create a new row if
238     * {@link #findIndex(String, int, int, int)} is unable to find match. Can
239     * also be used to subtract values from existing rows.
240     */
241    public NetworkStats combineValues(Entry entry) {
242        final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag);
243        if (i == -1) {
244            // only create new entry when positive contribution
245            addValues(entry);
246        } else {
247            rxBytes[i] += entry.rxBytes;
248            rxPackets[i] += entry.rxPackets;
249            txBytes[i] += entry.txBytes;
250            txPackets[i] += entry.txPackets;
251            operations[i] += entry.operations;
252        }
253        return this;
254    }
255
256    /**
257     * Find first stats index that matches the requested parameters.
258     */
259    public int findIndex(String iface, int uid, int set, int tag) {
260        for (int i = 0; i < size; i++) {
261            if (Objects.equal(iface, this.iface[i]) && uid == this.uid[i] && set == this.set[i]
262                    && tag == this.tag[i]) {
263                return i;
264            }
265        }
266        return -1;
267    }
268
269    /**
270     * Splice in {@link #operations} from the given {@link NetworkStats} based
271     * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
272     * since operation counts are at data layer.
273     */
274    public void spliceOperationsFrom(NetworkStats stats) {
275        for (int i = 0; i < size; i++) {
276            final int j = stats.findIndex(IFACE_ALL, uid[i], set[i], tag[i]);
277            if (j == -1) {
278                operations[i] = 0;
279            } else {
280                operations[i] = stats.operations[j];
281            }
282        }
283    }
284
285    /**
286     * Return list of unique interfaces known by this data structure.
287     */
288    public String[] getUniqueIfaces() {
289        final HashSet<String> ifaces = new HashSet<String>();
290        for (String iface : this.iface) {
291            if (iface != IFACE_ALL) {
292                ifaces.add(iface);
293            }
294        }
295        return ifaces.toArray(new String[ifaces.size()]);
296    }
297
298    /**
299     * Return list of unique UIDs known by this data structure.
300     */
301    public int[] getUniqueUids() {
302        final SparseBooleanArray uids = new SparseBooleanArray();
303        for (int uid : this.uid) {
304            uids.put(uid, true);
305        }
306
307        final int size = uids.size();
308        final int[] result = new int[size];
309        for (int i = 0; i < size; i++) {
310            result[i] = uids.keyAt(i);
311        }
312        return result;
313    }
314
315    /**
316     * Subtract the given {@link NetworkStats}, effectively leaving the delta
317     * between two snapshots in time. Assumes that statistics rows collect over
318     * time, and that none of them have disappeared.
319     *
320     * @throws IllegalArgumentException when given {@link NetworkStats} is
321     *             non-monotonic.
322     */
323    public NetworkStats subtract(NetworkStats value) {
324        return subtract(value, true, false);
325    }
326
327    /**
328     * Subtract the given {@link NetworkStats}, effectively leaving the delta
329     * between two snapshots in time. Assumes that statistics rows collect over
330     * time, and that none of them have disappeared.
331     * <p>
332     * Instead of throwing when counters are non-monotonic, this variant clamps
333     * results to never be negative.
334     */
335    public NetworkStats subtractClamped(NetworkStats value) {
336        return subtract(value, false, true);
337    }
338
339    /**
340     * Subtract the given {@link NetworkStats}, effectively leaving the delta
341     * between two snapshots in time. Assumes that statistics rows collect over
342     * time, and that none of them have disappeared.
343     *
344     * @param enforceMonotonic Validate that incoming value is strictly
345     *            monotonic compared to this object.
346     * @param clampNegative Instead of throwing like {@code enforceMonotonic},
347     *            clamp resulting counters at 0 to prevent negative values.
348     */
349    private NetworkStats subtract(
350            NetworkStats value, boolean enforceMonotonic, boolean clampNegative) {
351        final long deltaRealtime = this.elapsedRealtime - value.elapsedRealtime;
352        if (enforceMonotonic && deltaRealtime < 0) {
353            throw new IllegalArgumentException("found non-monotonic realtime");
354        }
355
356        // result will have our rows, and elapsed time between snapshots
357        final Entry entry = new Entry();
358        final NetworkStats result = new NetworkStats(deltaRealtime, size);
359        for (int i = 0; i < size; i++) {
360            entry.iface = iface[i];
361            entry.uid = uid[i];
362            entry.set = set[i];
363            entry.tag = tag[i];
364
365            // find remote row that matches, and subtract
366            final int j = value.findIndex(entry.iface, entry.uid, entry.set, entry.tag);
367            if (j == -1) {
368                // newly appearing row, return entire value
369                entry.rxBytes = rxBytes[i];
370                entry.rxPackets = rxPackets[i];
371                entry.txBytes = txBytes[i];
372                entry.txPackets = txPackets[i];
373                entry.operations = operations[i];
374            } else {
375                // existing row, subtract remote value
376                entry.rxBytes = rxBytes[i] - value.rxBytes[j];
377                entry.rxPackets = rxPackets[i] - value.rxPackets[j];
378                entry.txBytes = txBytes[i] - value.txBytes[j];
379                entry.txPackets = txPackets[i] - value.txPackets[j];
380                entry.operations = operations[i] - value.operations[j];
381                if (enforceMonotonic
382                        && (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
383                                || entry.txPackets < 0 || entry.operations < 0)) {
384                    throw new IllegalArgumentException("found non-monotonic values");
385                }
386                if (clampNegative) {
387                    entry.rxBytes = Math.max(0, entry.rxBytes);
388                    entry.rxPackets = Math.max(0, entry.rxPackets);
389                    entry.txBytes = Math.max(0, entry.txBytes);
390                    entry.txPackets = Math.max(0, entry.txPackets);
391                    entry.operations = Math.max(0, entry.operations);
392                }
393            }
394
395            result.addValues(entry);
396        }
397
398        return result;
399    }
400
401    public void dump(String prefix, PrintWriter pw) {
402        pw.print(prefix);
403        pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
404        for (int i = 0; i < size; i++) {
405            pw.print(prefix);
406            pw.print("  iface="); pw.print(iface[i]);
407            pw.print(" uid="); pw.print(uid[i]);
408            pw.print(" set="); pw.print(setToString(set[i]));
409            pw.print(" tag="); pw.print(tagToString(tag[i]));
410            pw.print(" rxBytes="); pw.print(rxBytes[i]);
411            pw.print(" rxPackets="); pw.print(rxPackets[i]);
412            pw.print(" txBytes="); pw.print(txBytes[i]);
413            pw.print(" txPackets="); pw.print(txPackets[i]);
414            pw.print(" operations="); pw.println(operations[i]);
415        }
416    }
417
418    /**
419     * Return text description of {@link #set} value.
420     */
421    public static String setToString(int set) {
422        switch (set) {
423            case SET_ALL:
424                return "ALL";
425            case SET_DEFAULT:
426                return "DEFAULT";
427            case SET_FOREGROUND:
428                return "FOREGROUND";
429            default:
430                return "UNKNOWN";
431        }
432    }
433
434    /**
435     * Return text description of {@link #tag} value.
436     */
437    public static String tagToString(int tag) {
438        return "0x" + Integer.toHexString(tag);
439    }
440
441    @Override
442    public String toString() {
443        final CharArrayWriter writer = new CharArrayWriter();
444        dump("", new PrintWriter(writer));
445        return writer.toString();
446    }
447
448    /** {@inheritDoc} */
449    public int describeContents() {
450        return 0;
451    }
452
453    public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
454        public NetworkStats createFromParcel(Parcel in) {
455            return new NetworkStats(in);
456        }
457
458        public NetworkStats[] newArray(int size) {
459            return new NetworkStats[size];
460        }
461    };
462}
463