blob: a4c66e4f6452cacad479d59f540c5096ff21d0ce [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
24import java.io.CharArrayWriter;
25import java.io.PrintWriter;
Jeff Sharkey4a971222011-06-11 22:16:55 -070026import java.util.Arrays;
Jeff Sharkey75279902011-05-24 18:39:45 -070027import java.util.HashSet;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070028
29/**
Jeff Sharkey75279902011-05-24 18:39:45 -070030 * Collection of active network statistics. Can contain summary details across
31 * all interfaces, or details with per-UID granularity. Internally stores data
32 * as a large table, closely matching {@code /proc/} data format. This structure
33 * optimizes for rapid in-memory comparison, but consider using
34 * {@link NetworkStatsHistory} when persisting.
Jeff Sharkey9a13f362011-04-26 16:25:36 -070035 *
36 * @hide
37 */
38public class NetworkStats implements Parcelable {
Jeff Sharkey75279902011-05-24 18:39:45 -070039 /** {@link #iface} value when interface details unavailable. */
Jeff Sharkey9a13f362011-04-26 16:25:36 -070040 public static final String IFACE_ALL = null;
Jeff Sharkey75279902011-05-24 18:39:45 -070041 /** {@link #uid} value when UID details unavailable. */
42 public static final int UID_ALL = -1;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070043 /** {@link #tag} value for without tag. */
44 public static final int TAG_NONE = 0;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070045
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070046 // TODO: move public fields to Entry accessors, then undeprecate
47 // TODO: refactor rx/tx to rxBytes/txBytes
48
Jeff Sharkey9a13f362011-04-26 16:25:36 -070049 /**
50 * {@link SystemClock#elapsedRealtime()} timestamp when this data was
51 * generated.
52 */
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070053 @Deprecated
Jeff Sharkey9a13f362011-04-26 16:25:36 -070054 public final long elapsedRealtime;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070055 @Deprecated
Jeff Sharkey4a971222011-06-11 22:16:55 -070056 public int size;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070057 @Deprecated
Jeff Sharkey4a971222011-06-11 22:16:55 -070058 public String[] iface;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070059 @Deprecated
Jeff Sharkey4a971222011-06-11 22:16:55 -070060 public int[] uid;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070061 @Deprecated
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070062 public int[] tag;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070063 @Deprecated
Jeff Sharkey4a971222011-06-11 22:16:55 -070064 public long[] rx;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070065 @Deprecated
66 public long[] rxPackets;
67 @Deprecated
Jeff Sharkey4a971222011-06-11 22:16:55 -070068 public long[] tx;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070069 @Deprecated
70 public long[] txPackets;
71
72 public static class Entry {
73 public String iface;
74 public int uid;
75 public int tag;
76 public long rxBytes;
77 public long rxPackets;
78 public long txBytes;
79 public long txPackets;
80 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -070081
Jeff Sharkey4a971222011-06-11 22:16:55 -070082 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -070083 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -070084 this.size = 0;
85 this.iface = new String[initialSize];
86 this.uid = new int[initialSize];
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070087 this.tag = new int[initialSize];
Jeff Sharkey4a971222011-06-11 22:16:55 -070088 this.rx = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070089 this.rxPackets = new long[initialSize];
Jeff Sharkey4a971222011-06-11 22:16:55 -070090 this.tx = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070091 this.txPackets = new long[initialSize];
Jeff Sharkey9a13f362011-04-26 16:25:36 -070092 }
93
94 public NetworkStats(Parcel parcel) {
95 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -070096 size = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -070097 iface = parcel.createStringArray();
98 uid = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070099 tag = parcel.createIntArray();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700100 rx = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700101 rxPackets = parcel.createLongArray();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700102 tx = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700103 txPackets = parcel.createLongArray();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700104 }
105
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700106 /**
107 * Add new stats entry with given values.
108 */
109 public NetworkStats addEntry(String iface, int uid, int tag, long rx, long tx) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700110 final Entry entry = new Entry();
111 entry.iface = iface;
112 entry.uid = uid;
113 entry.tag = tag;
114 entry.rxBytes = rx;
115 entry.txBytes = tx;
116 return addValues(entry);
117 }
118
119 /**
120 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
121 * object can be recycled across multiple calls.
122 */
123 public NetworkStats addValues(Entry entry) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700124 if (size >= this.iface.length) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700125 final int newLength = Math.max(iface.length, 10) * 3 / 2;
126 iface = Arrays.copyOf(iface, newLength);
127 uid = Arrays.copyOf(uid, newLength);
128 tag = Arrays.copyOf(tag, newLength);
129 rx = Arrays.copyOf(rx, newLength);
130 rxPackets = Arrays.copyOf(rxPackets, newLength);
131 tx = Arrays.copyOf(tx, newLength);
132 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700133 }
134
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700135 iface[size] = entry.iface;
136 uid[size] = entry.uid;
137 tag[size] = entry.tag;
138 rx[size] = entry.rxBytes;
139 rxPackets[size] = entry.rxPackets;
140 tx[size] = entry.txBytes;
141 txPackets[size] = entry.txPackets;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700142 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700143
Jeff Sharkey4a971222011-06-11 22:16:55 -0700144 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700145 }
146
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700147 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700148 * Return specific stats entry.
149 */
150 public Entry getValues(int i, Entry recycle) {
151 final Entry entry = recycle != null ? recycle : new Entry();
152 entry.iface = iface[i];
153 entry.uid = uid[i];
154 entry.tag = tag[i];
155 entry.rxBytes = rx[i];
156 entry.rxPackets = rxPackets[i];
157 entry.txBytes = tx[i];
158 entry.txPackets = txPackets[i];
159 return entry;
160 }
161
162 public long getElapsedRealtime() {
163 return elapsedRealtime;
164 }
165
166 public int size() {
167 return size;
168 }
169
170 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700171 * Combine given values with an existing row, or create a new row if
172 * {@link #findIndex(String, int, int)} is unable to find match. Can also be
173 * used to subtract values from existing rows.
174 */
175 public NetworkStats combineEntry(String iface, int uid, int tag, long rx, long tx) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700176 // TODO: extent to accept rxPackets/txPackets
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700177 final int i = findIndex(iface, uid, tag);
178 if (i == -1) {
179 // only create new entry when positive contribution
180 addEntry(iface, uid, tag, rx, tx);
181 } else {
182 this.rx[i] += rx;
183 this.tx[i] += tx;
184 }
185 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700186 }
187
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700188 /**
189 * Find first stats index that matches the requested parameters.
190 */
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700191 public int findIndex(String iface, int uid, int tag) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700192 for (int i = 0; i < size; i++) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700193 if (equal(iface, this.iface[i]) && uid == this.uid[i] && tag == this.tag[i]) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700194 return i;
195 }
196 }
197 return -1;
198 }
199
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700200 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700201 * Return list of unique interfaces known by this data structure.
202 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700203 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700204 final HashSet<String> ifaces = new HashSet<String>();
205 for (String iface : this.iface) {
206 if (iface != IFACE_ALL) {
207 ifaces.add(iface);
208 }
209 }
210 return ifaces.toArray(new String[ifaces.size()]);
211 }
212
213 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700214 * Return list of unique UIDs known by this data structure.
215 */
216 public int[] getUniqueUids() {
217 final SparseBooleanArray uids = new SparseBooleanArray();
218 for (int uid : this.uid) {
219 uids.put(uid, true);
220 }
221
222 final int size = uids.size();
223 final int[] result = new int[size];
224 for (int i = 0; i < size; i++) {
225 result[i] = uids.keyAt(i);
226 }
227 return result;
228 }
229
230 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700231 * Subtract the given {@link NetworkStats}, effectively leaving the delta
232 * between two snapshots in time. Assumes that statistics rows collect over
233 * time, and that none of them have disappeared.
Jeff Sharkey75279902011-05-24 18:39:45 -0700234 *
Jeff Sharkey3f391352011-06-05 17:42:53 -0700235 * @throws IllegalArgumentException when given {@link NetworkStats} is
236 * non-monotonic.
237 */
238 public NetworkStats subtract(NetworkStats value) {
239 return subtract(value, true, false);
240 }
241
242 /**
243 * Subtract the given {@link NetworkStats}, effectively leaving the delta
244 * between two snapshots in time. Assumes that statistics rows collect over
245 * time, and that none of them have disappeared.
246 * <p>
247 * Instead of throwing when counters are non-monotonic, this variant clamps
248 * results to never be negative.
249 */
250 public NetworkStats subtractClamped(NetworkStats value) {
251 return subtract(value, false, true);
252 }
253
254 /**
255 * Subtract the given {@link NetworkStats}, effectively leaving the delta
256 * between two snapshots in time. Assumes that statistics rows collect over
257 * time, and that none of them have disappeared.
258 *
Jeff Sharkey75279902011-05-24 18:39:45 -0700259 * @param enforceMonotonic Validate that incoming value is strictly
260 * monotonic compared to this object.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700261 * @param clampNegative Instead of throwing like {@code enforceMonotonic},
262 * clamp resulting counters at 0 to prevent negative values.
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700263 */
Jeff Sharkey3f391352011-06-05 17:42:53 -0700264 private NetworkStats subtract(
265 NetworkStats value, boolean enforceMonotonic, boolean clampNegative) {
Jeff Sharkey75279902011-05-24 18:39:45 -0700266 final long deltaRealtime = this.elapsedRealtime - value.elapsedRealtime;
267 if (enforceMonotonic && deltaRealtime < 0) {
268 throw new IllegalArgumentException("found non-monotonic realtime");
269 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700270
Jeff Sharkey75279902011-05-24 18:39:45 -0700271 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700272 final Entry entry = new Entry();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700273 final NetworkStats result = new NetworkStats(deltaRealtime, size);
274 for (int i = 0; i < size; i++) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700275 entry.iface = iface[i];
276 entry.uid = uid[i];
277 entry.tag = tag[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700278
279 // find remote row that matches, and subtract
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700280 final int j = value.findIndex(entry.iface, entry.uid, entry.tag);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700281 if (j == -1) {
282 // newly appearing row, return entire value
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700283 entry.rxBytes = rx[i];
284 entry.rxPackets = rxPackets[i];
285 entry.txBytes = tx[i];
286 entry.txPackets = txPackets[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700287 } else {
288 // existing row, subtract remote value
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700289 entry.rxBytes = rx[i] - value.rx[j];
290 entry.rxPackets = rxPackets[i] - value.rxPackets[j];
291 entry.txBytes = tx[i] - value.tx[j];
292 entry.txPackets = txPackets[i] - value.txPackets[j];
293 if (enforceMonotonic
294 && (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
295 || entry.txPackets < 0)) {
Jeff Sharkey75279902011-05-24 18:39:45 -0700296 throw new IllegalArgumentException("found non-monotonic values");
297 }
Jeff Sharkey3f391352011-06-05 17:42:53 -0700298 if (clampNegative) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700299 entry.rxBytes = Math.max(0, entry.rxBytes);
300 entry.rxPackets = Math.max(0, entry.rxPackets);
301 entry.txBytes = Math.max(0, entry.txBytes);
302 entry.txPackets = Math.max(0, entry.txPackets);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700303 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700304 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700305
306 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700307 }
308
Jeff Sharkey4a971222011-06-11 22:16:55 -0700309 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700310 }
311
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700312 private static boolean equal(Object a, Object b) {
313 return a == b || (a != null && a.equals(b));
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700314 }
315
316 public void dump(String prefix, PrintWriter pw) {
317 pw.print(prefix);
318 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700319 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700320 pw.print(prefix);
321 pw.print(" iface="); pw.print(iface[i]);
322 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700323 pw.print(" tag="); pw.print(tag[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700324 pw.print(" rxBytes="); pw.print(rx[i]);
325 pw.print(" rxPackets="); pw.print(rxPackets[i]);
326 pw.print(" txBytes="); pw.print(tx[i]);
327 pw.print(" txPackets="); pw.println(txPackets[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700328 }
329 }
330
331 @Override
332 public String toString() {
333 final CharArrayWriter writer = new CharArrayWriter();
334 dump("", new PrintWriter(writer));
335 return writer.toString();
336 }
337
338 /** {@inheritDoc} */
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700339 public int describeContents() {
340 return 0;
341 }
342
343 /** {@inheritDoc} */
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700344 public void writeToParcel(Parcel dest, int flags) {
345 dest.writeLong(elapsedRealtime);
Jeff Sharkey4a971222011-06-11 22:16:55 -0700346 dest.writeInt(size);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700347 dest.writeStringArray(iface);
348 dest.writeIntArray(uid);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700349 dest.writeIntArray(tag);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700350 dest.writeLongArray(rx);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700351 dest.writeLongArray(rxPackets);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700352 dest.writeLongArray(tx);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700353 dest.writeLongArray(txPackets);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700354 }
355
356 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
357 public NetworkStats createFromParcel(Parcel in) {
358 return new NetworkStats(in);
359 }
360
361 public NetworkStats[] newArray(int size) {
362 return new NetworkStats[size];
363 }
364 };
365}