blob: fbff7d89fcc23c8d57a198ec1a8656183bbed7be [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 Sharkey9a13f362011-04-26 16:25:36 -070046 /**
47 * {@link SystemClock#elapsedRealtime()} timestamp when this data was
48 * generated.
49 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070050 private final long elapsedRealtime;
51 private int size;
52 private String[] iface;
53 private int[] uid;
54 private int[] tag;
55 private long[] rxBytes;
56 private long[] rxPackets;
57 private long[] txBytes;
58 private long[] txPackets;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070059
60 public static class Entry {
61 public String iface;
62 public int uid;
63 public int tag;
64 public long rxBytes;
65 public long rxPackets;
66 public long txBytes;
67 public long txPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070068
69 public Entry() {
70 }
71
72 public Entry(String iface, int uid, int tag, long rxBytes, long rxPackets, long txBytes,
73 long txPackets) {
74 this.iface = iface;
75 this.uid = uid;
76 this.tag = tag;
77 this.rxBytes = rxBytes;
78 this.rxPackets = rxPackets;
79 this.txBytes = txBytes;
80 this.txPackets = txPackets;
81 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070082 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -070083
Jeff Sharkey4a971222011-06-11 22:16:55 -070084 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -070085 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -070086 this.size = 0;
87 this.iface = new String[initialSize];
88 this.uid = new int[initialSize];
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070089 this.tag = new int[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070090 this.rxBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070091 this.rxPackets = new long[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070092 this.txBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070093 this.txPackets = new long[initialSize];
Jeff Sharkey9a13f362011-04-26 16:25:36 -070094 }
95
96 public NetworkStats(Parcel parcel) {
97 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -070098 size = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -070099 iface = parcel.createStringArray();
100 uid = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700101 tag = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700102 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700103 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700104 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700105 txPackets = parcel.createLongArray();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700106 }
107
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700108 public NetworkStats addValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
109 long txBytes, long txPackets) {
110 return addValues(new Entry(iface, uid, tag, rxBytes, rxPackets, txBytes, txPackets));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700111 }
112
113 /**
114 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
115 * object can be recycled across multiple calls.
116 */
117 public NetworkStats addValues(Entry entry) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700118 if (size >= this.iface.length) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700119 final int newLength = Math.max(iface.length, 10) * 3 / 2;
120 iface = Arrays.copyOf(iface, newLength);
121 uid = Arrays.copyOf(uid, newLength);
122 tag = Arrays.copyOf(tag, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700123 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700124 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700125 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700126 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700127 }
128
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700129 iface[size] = entry.iface;
130 uid[size] = entry.uid;
131 tag[size] = entry.tag;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700132 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700133 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700134 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700135 txPackets[size] = entry.txPackets;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700136 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700137
Jeff Sharkey4a971222011-06-11 22:16:55 -0700138 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700139 }
140
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700141 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700142 * Return specific stats entry.
143 */
144 public Entry getValues(int i, Entry recycle) {
145 final Entry entry = recycle != null ? recycle : new Entry();
146 entry.iface = iface[i];
147 entry.uid = uid[i];
148 entry.tag = tag[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700149 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700150 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700151 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700152 entry.txPackets = txPackets[i];
153 return entry;
154 }
155
156 public long getElapsedRealtime() {
157 return elapsedRealtime;
158 }
159
160 public int size() {
161 return size;
162 }
163
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700164 // @VisibleForTesting
165 public int internalSize() {
166 return iface.length;
167 }
168
169 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
170 long txBytes, long txPackets) {
171 return combineValues(new Entry(iface, uid, tag, rxBytes, rxPackets, txBytes, txPackets));
172 }
173
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700174 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700175 * Combine given values with an existing row, or create a new row if
176 * {@link #findIndex(String, int, int)} is unable to find match. Can also be
177 * used to subtract values from existing rows.
178 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700179 public NetworkStats combineValues(Entry entry) {
180 final int i = findIndex(entry.iface, entry.uid, entry.tag);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700181 if (i == -1) {
182 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700183 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700184 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700185 rxBytes[i] += entry.rxBytes;
186 rxPackets[i] += entry.rxPackets;
187 txBytes[i] += entry.txBytes;
188 txPackets[i] += entry.txPackets;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700189 }
190 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700191 }
192
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700193 /**
194 * Find first stats index that matches the requested parameters.
195 */
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700196 public int findIndex(String iface, int uid, int tag) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700197 for (int i = 0; i < size; i++) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700198 if (equal(iface, this.iface[i]) && uid == this.uid[i] && tag == this.tag[i]) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700199 return i;
200 }
201 }
202 return -1;
203 }
204
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700205 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700206 * Return list of unique interfaces known by this data structure.
207 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700208 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700209 final HashSet<String> ifaces = new HashSet<String>();
210 for (String iface : this.iface) {
211 if (iface != IFACE_ALL) {
212 ifaces.add(iface);
213 }
214 }
215 return ifaces.toArray(new String[ifaces.size()]);
216 }
217
218 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700219 * Return list of unique UIDs known by this data structure.
220 */
221 public int[] getUniqueUids() {
222 final SparseBooleanArray uids = new SparseBooleanArray();
223 for (int uid : this.uid) {
224 uids.put(uid, true);
225 }
226
227 final int size = uids.size();
228 final int[] result = new int[size];
229 for (int i = 0; i < size; i++) {
230 result[i] = uids.keyAt(i);
231 }
232 return result;
233 }
234
235 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700236 * Subtract the given {@link NetworkStats}, effectively leaving the delta
237 * between two snapshots in time. Assumes that statistics rows collect over
238 * time, and that none of them have disappeared.
Jeff Sharkey75279902011-05-24 18:39:45 -0700239 *
Jeff Sharkey3f391352011-06-05 17:42:53 -0700240 * @throws IllegalArgumentException when given {@link NetworkStats} is
241 * non-monotonic.
242 */
243 public NetworkStats subtract(NetworkStats value) {
244 return subtract(value, true, false);
245 }
246
247 /**
248 * Subtract the given {@link NetworkStats}, effectively leaving the delta
249 * between two snapshots in time. Assumes that statistics rows collect over
250 * time, and that none of them have disappeared.
251 * <p>
252 * Instead of throwing when counters are non-monotonic, this variant clamps
253 * results to never be negative.
254 */
255 public NetworkStats subtractClamped(NetworkStats value) {
256 return subtract(value, false, true);
257 }
258
259 /**
260 * Subtract the given {@link NetworkStats}, effectively leaving the delta
261 * between two snapshots in time. Assumes that statistics rows collect over
262 * time, and that none of them have disappeared.
263 *
Jeff Sharkey75279902011-05-24 18:39:45 -0700264 * @param enforceMonotonic Validate that incoming value is strictly
265 * monotonic compared to this object.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700266 * @param clampNegative Instead of throwing like {@code enforceMonotonic},
267 * clamp resulting counters at 0 to prevent negative values.
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700268 */
Jeff Sharkey3f391352011-06-05 17:42:53 -0700269 private NetworkStats subtract(
270 NetworkStats value, boolean enforceMonotonic, boolean clampNegative) {
Jeff Sharkey75279902011-05-24 18:39:45 -0700271 final long deltaRealtime = this.elapsedRealtime - value.elapsedRealtime;
272 if (enforceMonotonic && deltaRealtime < 0) {
273 throw new IllegalArgumentException("found non-monotonic realtime");
274 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700275
Jeff Sharkey75279902011-05-24 18:39:45 -0700276 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700277 final Entry entry = new Entry();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700278 final NetworkStats result = new NetworkStats(deltaRealtime, size);
279 for (int i = 0; i < size; i++) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700280 entry.iface = iface[i];
281 entry.uid = uid[i];
282 entry.tag = tag[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700283
284 // find remote row that matches, and subtract
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700285 final int j = value.findIndex(entry.iface, entry.uid, entry.tag);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700286 if (j == -1) {
287 // newly appearing row, return entire value
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700288 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700289 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700290 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700291 entry.txPackets = txPackets[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700292 } else {
293 // existing row, subtract remote value
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700294 entry.rxBytes = rxBytes[i] - value.rxBytes[j];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700295 entry.rxPackets = rxPackets[i] - value.rxPackets[j];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700296 entry.txBytes = txBytes[i] - value.txBytes[j];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700297 entry.txPackets = txPackets[i] - value.txPackets[j];
298 if (enforceMonotonic
299 && (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
300 || entry.txPackets < 0)) {
Jeff Sharkey75279902011-05-24 18:39:45 -0700301 throw new IllegalArgumentException("found non-monotonic values");
302 }
Jeff Sharkey3f391352011-06-05 17:42:53 -0700303 if (clampNegative) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700304 entry.rxBytes = Math.max(0, entry.rxBytes);
305 entry.rxPackets = Math.max(0, entry.rxPackets);
306 entry.txBytes = Math.max(0, entry.txBytes);
307 entry.txPackets = Math.max(0, entry.txPackets);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700308 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700309 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700310
311 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700312 }
313
Jeff Sharkey4a971222011-06-11 22:16:55 -0700314 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700315 }
316
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700317 private static boolean equal(Object a, Object b) {
318 return a == b || (a != null && a.equals(b));
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700319 }
320
321 public void dump(String prefix, PrintWriter pw) {
322 pw.print(prefix);
323 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700324 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700325 pw.print(prefix);
326 pw.print(" iface="); pw.print(iface[i]);
327 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700328 pw.print(" tag="); pw.print(tag[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700329 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700330 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700331 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700332 pw.print(" txPackets="); pw.println(txPackets[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700333 }
334 }
335
336 @Override
337 public String toString() {
338 final CharArrayWriter writer = new CharArrayWriter();
339 dump("", new PrintWriter(writer));
340 return writer.toString();
341 }
342
343 /** {@inheritDoc} */
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700344 public int describeContents() {
345 return 0;
346 }
347
348 /** {@inheritDoc} */
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700349 public void writeToParcel(Parcel dest, int flags) {
350 dest.writeLong(elapsedRealtime);
Jeff Sharkey4a971222011-06-11 22:16:55 -0700351 dest.writeInt(size);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700352 dest.writeStringArray(iface);
353 dest.writeIntArray(uid);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700354 dest.writeIntArray(tag);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700355 dest.writeLongArray(rxBytes);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700356 dest.writeLongArray(rxPackets);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700357 dest.writeLongArray(txBytes);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700358 dest.writeLongArray(txPackets);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700359 }
360
361 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
362 public NetworkStats createFromParcel(Parcel in) {
363 return new NetworkStats(in);
364 }
365
366 public NetworkStats[] newArray(int size) {
367 return new NetworkStats[size];
368 }
369 };
370}