blob: 4430e000574181f4b622cd35a560f81ee4d41d94 [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;
22
23import java.io.CharArrayWriter;
24import java.io.PrintWriter;
25
26/**
27 * Collection of network statistics. Can contain summary details across all
28 * interfaces, or details with per-UID granularity. Designed to parcel quickly
29 * across process boundaries.
30 *
31 * @hide
32 */
33public class NetworkStats implements Parcelable {
34 /** {@link #iface} value when entry is summarized over all interfaces. */
35 public static final String IFACE_ALL = null;
36 /** {@link #uid} value when entry is summarized over all UIDs. */
37 public static final int UID_ALL = 0;
38
39 /**
40 * {@link SystemClock#elapsedRealtime()} timestamp when this data was
41 * generated.
42 */
43 public final long elapsedRealtime;
44 public final String[] iface;
45 public final int[] uid;
46 public final long[] rx;
47 public final long[] tx;
48
49 // TODO: add fg/bg stats and tag granularity
50
51 private NetworkStats(long elapsedRealtime, String[] iface, int[] uid, long[] rx, long[] tx) {
52 this.elapsedRealtime = elapsedRealtime;
53 this.iface = iface;
54 this.uid = uid;
55 this.rx = rx;
56 this.tx = tx;
57 }
58
59 public NetworkStats(Parcel parcel) {
60 elapsedRealtime = parcel.readLong();
61 iface = parcel.createStringArray();
62 uid = parcel.createIntArray();
63 rx = parcel.createLongArray();
64 tx = parcel.createLongArray();
65 }
66
67 public static class Builder {
68 private long mElapsedRealtime;
69 private final String[] mIface;
70 private final int[] mUid;
71 private final long[] mRx;
72 private final long[] mTx;
73
74 private int mIndex = 0;
75
76 public Builder(long elapsedRealtime, int size) {
77 mElapsedRealtime = elapsedRealtime;
78 mIface = new String[size];
79 mUid = new int[size];
80 mRx = new long[size];
81 mTx = new long[size];
82 }
83
84 public void addEntry(String iface, int uid, long rx, long tx) {
85 mIface[mIndex] = iface;
86 mUid[mIndex] = uid;
87 mRx[mIndex] = rx;
88 mTx[mIndex] = tx;
89 mIndex++;
90 }
91
92 public NetworkStats build() {
93 if (mIndex != mIface.length) {
94 throw new IllegalArgumentException("unexpected number of entries");
95 }
96 return new NetworkStats(mElapsedRealtime, mIface, mUid, mRx, mTx);
97 }
98 }
99
100 /**
101 * Find first stats index that matches the requested parameters.
102 */
103 public int findIndex(String iface, int uid) {
104 for (int i = 0; i < this.iface.length; i++) {
105 if (equal(iface, this.iface[i]) && uid == this.uid[i]) {
106 return i;
107 }
108 }
109 return -1;
110 }
111
112 private static boolean equal(Object a, Object b) {
113 return a == b || (a != null && a.equals(b));
114 }
115
116 /** {@inheritDoc} */
117 public int describeContents() {
118 return 0;
119 }
120
121 public void dump(String prefix, PrintWriter pw) {
122 pw.print(prefix);
123 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
124 for (int i = 0; i < iface.length; i++) {
125 pw.print(prefix);
126 pw.print(" iface="); pw.print(iface[i]);
127 pw.print(" uid="); pw.print(uid[i]);
128 pw.print(" rx="); pw.print(rx[i]);
129 pw.print(" tx="); pw.println(tx[i]);
130 }
131 }
132
133 @Override
134 public String toString() {
135 final CharArrayWriter writer = new CharArrayWriter();
136 dump("", new PrintWriter(writer));
137 return writer.toString();
138 }
139
140 /** {@inheritDoc} */
141 public void writeToParcel(Parcel dest, int flags) {
142 dest.writeLong(elapsedRealtime);
143 dest.writeStringArray(iface);
144 dest.writeIntArray(uid);
145 dest.writeLongArray(rx);
146 dest.writeLongArray(tx);
147 }
148
149 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
150 public NetworkStats createFromParcel(Parcel in) {
151 return new NetworkStats(in);
152 }
153
154 public NetworkStats[] newArray(int size) {
155 return new NetworkStats[size];
156 }
157 };
158}