Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 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 | |
| 17 | package android.net; |
| 18 | |
| 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | import android.os.SystemClock; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 22 | import android.util.SparseBooleanArray; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 23 | |
| 24 | import java.io.CharArrayWriter; |
| 25 | import java.io.PrintWriter; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 26 | import java.util.Arrays; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 27 | import java.util.HashSet; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 30 | * 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 Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 35 | * |
| 36 | * @hide |
| 37 | */ |
| 38 | public class NetworkStats implements Parcelable { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 39 | /** {@link #iface} value when interface details unavailable. */ |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 40 | public static final String IFACE_ALL = null; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 41 | /** {@link #uid} value when UID details unavailable. */ |
| 42 | public static final int UID_ALL = -1; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 43 | /** {@link #tag} value for without tag. */ |
| 44 | public static final int TAG_NONE = 0; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 45 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 46 | /** |
| 47 | * {@link SystemClock#elapsedRealtime()} timestamp when this data was |
| 48 | * generated. |
| 49 | */ |
| 50 | public final long elapsedRealtime; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 51 | public int size; |
| 52 | public String[] iface; |
| 53 | public int[] uid; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 54 | public int[] tag; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 55 | public long[] rx; |
| 56 | public long[] tx; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 57 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 58 | public NetworkStats(long elapsedRealtime, int initialSize) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 59 | this.elapsedRealtime = elapsedRealtime; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 60 | this.size = 0; |
| 61 | this.iface = new String[initialSize]; |
| 62 | this.uid = new int[initialSize]; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 63 | this.tag = new int[initialSize]; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 64 | this.rx = new long[initialSize]; |
| 65 | this.tx = new long[initialSize]; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | public NetworkStats(Parcel parcel) { |
| 69 | elapsedRealtime = parcel.readLong(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 70 | size = parcel.readInt(); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 71 | iface = parcel.createStringArray(); |
| 72 | uid = parcel.createIntArray(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 73 | tag = parcel.createIntArray(); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 74 | rx = parcel.createLongArray(); |
| 75 | tx = parcel.createLongArray(); |
| 76 | } |
| 77 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 78 | /** |
| 79 | * Add new stats entry with given values. |
| 80 | */ |
| 81 | public NetworkStats addEntry(String iface, int uid, int tag, long rx, long tx) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 82 | if (size >= this.iface.length) { |
| 83 | final int newLength = Math.max(this.iface.length, 10) * 3 / 2; |
| 84 | this.iface = Arrays.copyOf(this.iface, newLength); |
| 85 | this.uid = Arrays.copyOf(this.uid, newLength); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 86 | this.tag = Arrays.copyOf(this.tag, newLength); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 87 | this.rx = Arrays.copyOf(this.rx, newLength); |
| 88 | this.tx = Arrays.copyOf(this.tx, newLength); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 91 | this.iface[size] = iface; |
| 92 | this.uid[size] = uid; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 93 | this.tag[size] = tag; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 94 | this.rx[size] = rx; |
| 95 | this.tx[size] = tx; |
| 96 | size++; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 97 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 98 | return this; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 101 | /** |
| 102 | * Combine given values with an existing row, or create a new row if |
| 103 | * {@link #findIndex(String, int, int)} is unable to find match. Can also be |
| 104 | * used to subtract values from existing rows. |
| 105 | */ |
| 106 | public NetworkStats combineEntry(String iface, int uid, int tag, long rx, long tx) { |
| 107 | final int i = findIndex(iface, uid, tag); |
| 108 | if (i == -1) { |
| 109 | // only create new entry when positive contribution |
| 110 | addEntry(iface, uid, tag, rx, tx); |
| 111 | } else { |
| 112 | this.rx[i] += rx; |
| 113 | this.tx[i] += tx; |
| 114 | } |
| 115 | return this; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Find first stats index that matches the requested parameters. |
| 120 | */ |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 121 | public int findIndex(String iface, int uid, int tag) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 122 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 123 | if (equal(iface, this.iface[i]) && uid == this.uid[i] && tag == this.tag[i]) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 124 | return i; |
| 125 | } |
| 126 | } |
| 127 | return -1; |
| 128 | } |
| 129 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 130 | /** |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 131 | * Return list of unique interfaces known by this data structure. |
| 132 | */ |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 133 | public String[] getUniqueIfaces() { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 134 | final HashSet<String> ifaces = new HashSet<String>(); |
| 135 | for (String iface : this.iface) { |
| 136 | if (iface != IFACE_ALL) { |
| 137 | ifaces.add(iface); |
| 138 | } |
| 139 | } |
| 140 | return ifaces.toArray(new String[ifaces.size()]); |
| 141 | } |
| 142 | |
| 143 | /** |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 144 | * Return list of unique UIDs known by this data structure. |
| 145 | */ |
| 146 | public int[] getUniqueUids() { |
| 147 | final SparseBooleanArray uids = new SparseBooleanArray(); |
| 148 | for (int uid : this.uid) { |
| 149 | uids.put(uid, true); |
| 150 | } |
| 151 | |
| 152 | final int size = uids.size(); |
| 153 | final int[] result = new int[size]; |
| 154 | for (int i = 0; i < size; i++) { |
| 155 | result[i] = uids.keyAt(i); |
| 156 | } |
| 157 | return result; |
| 158 | } |
| 159 | |
| 160 | /** |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 161 | * Subtract the given {@link NetworkStats}, effectively leaving the delta |
| 162 | * between two snapshots in time. Assumes that statistics rows collect over |
| 163 | * time, and that none of them have disappeared. |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 164 | * |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 165 | * @throws IllegalArgumentException when given {@link NetworkStats} is |
| 166 | * non-monotonic. |
| 167 | */ |
| 168 | public NetworkStats subtract(NetworkStats value) { |
| 169 | return subtract(value, true, false); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Subtract the given {@link NetworkStats}, effectively leaving the delta |
| 174 | * between two snapshots in time. Assumes that statistics rows collect over |
| 175 | * time, and that none of them have disappeared. |
| 176 | * <p> |
| 177 | * Instead of throwing when counters are non-monotonic, this variant clamps |
| 178 | * results to never be negative. |
| 179 | */ |
| 180 | public NetworkStats subtractClamped(NetworkStats value) { |
| 181 | return subtract(value, false, true); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Subtract the given {@link NetworkStats}, effectively leaving the delta |
| 186 | * between two snapshots in time. Assumes that statistics rows collect over |
| 187 | * time, and that none of them have disappeared. |
| 188 | * |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 189 | * @param enforceMonotonic Validate that incoming value is strictly |
| 190 | * monotonic compared to this object. |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 191 | * @param clampNegative Instead of throwing like {@code enforceMonotonic}, |
| 192 | * clamp resulting counters at 0 to prevent negative values. |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 193 | */ |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 194 | private NetworkStats subtract( |
| 195 | NetworkStats value, boolean enforceMonotonic, boolean clampNegative) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 196 | final long deltaRealtime = this.elapsedRealtime - value.elapsedRealtime; |
| 197 | if (enforceMonotonic && deltaRealtime < 0) { |
| 198 | throw new IllegalArgumentException("found non-monotonic realtime"); |
| 199 | } |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 200 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 201 | // result will have our rows, and elapsed time between snapshots |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 202 | final NetworkStats result = new NetworkStats(deltaRealtime, size); |
| 203 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 204 | final String iface = this.iface[i]; |
| 205 | final int uid = this.uid[i]; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 206 | final int tag = this.tag[i]; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 207 | |
| 208 | // find remote row that matches, and subtract |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 209 | final int j = value.findIndex(iface, uid, tag); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 210 | if (j == -1) { |
| 211 | // newly appearing row, return entire value |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 212 | result.addEntry(iface, uid, tag, this.rx[i], this.tx[i]); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 213 | } else { |
| 214 | // existing row, subtract remote value |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 215 | long rx = this.rx[i] - value.rx[j]; |
| 216 | long tx = this.tx[i] - value.tx[j]; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 217 | if (enforceMonotonic && (rx < 0 || tx < 0)) { |
| 218 | throw new IllegalArgumentException("found non-monotonic values"); |
| 219 | } |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 220 | if (clampNegative) { |
| 221 | rx = Math.max(0, rx); |
| 222 | tx = Math.max(0, tx); |
| 223 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 224 | result.addEntry(iface, uid, tag, rx, tx); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 228 | return result; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 231 | private static boolean equal(Object a, Object b) { |
| 232 | return a == b || (a != null && a.equals(b)); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | public void dump(String prefix, PrintWriter pw) { |
| 236 | pw.print(prefix); |
| 237 | pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime); |
| 238 | for (int i = 0; i < iface.length; i++) { |
| 239 | pw.print(prefix); |
| 240 | pw.print(" iface="); pw.print(iface[i]); |
| 241 | pw.print(" uid="); pw.print(uid[i]); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 242 | pw.print(" tag="); pw.print(tag[i]); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 243 | pw.print(" rx="); pw.print(rx[i]); |
| 244 | pw.print(" tx="); pw.println(tx[i]); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public String toString() { |
| 250 | final CharArrayWriter writer = new CharArrayWriter(); |
| 251 | dump("", new PrintWriter(writer)); |
| 252 | return writer.toString(); |
| 253 | } |
| 254 | |
| 255 | /** {@inheritDoc} */ |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 256 | public int describeContents() { |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /** {@inheritDoc} */ |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 261 | public void writeToParcel(Parcel dest, int flags) { |
| 262 | dest.writeLong(elapsedRealtime); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 263 | dest.writeInt(size); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 264 | dest.writeStringArray(iface); |
| 265 | dest.writeIntArray(uid); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 266 | dest.writeIntArray(tag); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 267 | dest.writeLongArray(rx); |
| 268 | dest.writeLongArray(tx); |
| 269 | } |
| 270 | |
| 271 | public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() { |
| 272 | public NetworkStats createFromParcel(Parcel in) { |
| 273 | return new NetworkStats(in); |
| 274 | } |
| 275 | |
| 276 | public NetworkStats[] newArray(int size) { |
| 277 | return new NetworkStats[size]; |
| 278 | } |
| 279 | }; |
| 280 | } |