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 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 19 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 20 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | import android.os.SystemClock; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 24 | import android.util.SparseBooleanArray; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 25 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 26 | import com.android.internal.util.Objects; |
| 27 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 28 | import java.io.CharArrayWriter; |
| 29 | import java.io.PrintWriter; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 30 | import java.util.Arrays; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 31 | import java.util.HashSet; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 34 | * Collection of active network statistics. Can contain summary details across |
| 35 | * all interfaces, or details with per-UID granularity. Internally stores data |
| 36 | * as a large table, closely matching {@code /proc/} data format. This structure |
| 37 | * optimizes for rapid in-memory comparison, but consider using |
| 38 | * {@link NetworkStatsHistory} when persisting. |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 39 | * |
| 40 | * @hide |
| 41 | */ |
| 42 | public class NetworkStats implements Parcelable { |
Jeff Sharkey | 47eb102 | 2011-08-25 17:48:52 -0700 | [diff] [blame] | 43 | private static final String TAG = "NetworkStats"; |
| 44 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 45 | /** {@link #iface} value when interface details unavailable. */ |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 46 | public static final String IFACE_ALL = null; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 47 | /** {@link #uid} value when UID details unavailable. */ |
| 48 | public static final int UID_ALL = -1; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 49 | /** {@link #set} value when all sets combined. */ |
| 50 | public static final int SET_ALL = -1; |
| 51 | /** {@link #set} value where background data is accounted. */ |
| 52 | public static final int SET_DEFAULT = 0; |
| 53 | /** {@link #set} value where foreground data is accounted. */ |
| 54 | public static final int SET_FOREGROUND = 1; |
| 55 | /** {@link #tag} value for total data across all tags. */ |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 56 | public static final int TAG_NONE = 0; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 57 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 58 | // TODO: move fields to "mVariable" notation |
| 59 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 60 | /** |
| 61 | * {@link SystemClock#elapsedRealtime()} timestamp when this data was |
| 62 | * generated. |
| 63 | */ |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 64 | private final long elapsedRealtime; |
| 65 | private int size; |
| 66 | private String[] iface; |
| 67 | private int[] uid; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 68 | private int[] set; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 69 | private int[] tag; |
| 70 | private long[] rxBytes; |
| 71 | private long[] rxPackets; |
| 72 | private long[] txBytes; |
| 73 | private long[] txPackets; |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 74 | private long[] operations; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 75 | |
| 76 | public static class Entry { |
| 77 | public String iface; |
| 78 | public int uid; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 79 | public int set; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 80 | public int tag; |
| 81 | public long rxBytes; |
| 82 | public long rxPackets; |
| 83 | public long txBytes; |
| 84 | public long txPackets; |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 85 | public long operations; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 86 | |
| 87 | public Entry() { |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 88 | this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L); |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 92 | this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, |
| 93 | operations); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 96 | public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets, |
| 97 | long txBytes, long txPackets, long operations) { |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 98 | this.iface = iface; |
| 99 | this.uid = uid; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 100 | this.set = set; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 101 | this.tag = tag; |
| 102 | this.rxBytes = rxBytes; |
| 103 | this.rxPackets = rxPackets; |
| 104 | this.txBytes = txBytes; |
| 105 | this.txPackets = txPackets; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 106 | this.operations = operations; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 107 | } |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 108 | |
| 109 | @Override |
| 110 | public String toString() { |
| 111 | final StringBuilder builder = new StringBuilder(); |
| 112 | builder.append("iface=").append(iface); |
| 113 | builder.append(" uid=").append(uid); |
| 114 | builder.append(" set=").append(setToString(set)); |
| 115 | builder.append(" tag=").append(tagToString(tag)); |
| 116 | builder.append(" rxBytes=").append(rxBytes); |
| 117 | builder.append(" rxPackets=").append(rxPackets); |
| 118 | builder.append(" txBytes=").append(txBytes); |
| 119 | builder.append(" txPackets=").append(txPackets); |
| 120 | builder.append(" operations=").append(operations); |
| 121 | return builder.toString(); |
| 122 | } |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 123 | } |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 124 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 125 | public NetworkStats(long elapsedRealtime, int initialSize) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 126 | this.elapsedRealtime = elapsedRealtime; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 127 | this.size = 0; |
| 128 | this.iface = new String[initialSize]; |
| 129 | this.uid = new int[initialSize]; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 130 | this.set = new int[initialSize]; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 131 | this.tag = new int[initialSize]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 132 | this.rxBytes = new long[initialSize]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 133 | this.rxPackets = new long[initialSize]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 134 | this.txBytes = new long[initialSize]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 135 | this.txPackets = new long[initialSize]; |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 136 | this.operations = new long[initialSize]; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | public NetworkStats(Parcel parcel) { |
| 140 | elapsedRealtime = parcel.readLong(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 141 | size = parcel.readInt(); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 142 | iface = parcel.createStringArray(); |
| 143 | uid = parcel.createIntArray(); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 144 | set = parcel.createIntArray(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 145 | tag = parcel.createIntArray(); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 146 | rxBytes = parcel.createLongArray(); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 147 | rxPackets = parcel.createLongArray(); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 148 | txBytes = parcel.createLongArray(); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 149 | txPackets = parcel.createLongArray(); |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 150 | operations = parcel.createLongArray(); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** {@inheritDoc} */ |
| 154 | public void writeToParcel(Parcel dest, int flags) { |
| 155 | dest.writeLong(elapsedRealtime); |
| 156 | dest.writeInt(size); |
| 157 | dest.writeStringArray(iface); |
| 158 | dest.writeIntArray(uid); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 159 | dest.writeIntArray(set); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 160 | dest.writeIntArray(tag); |
| 161 | dest.writeLongArray(rxBytes); |
| 162 | dest.writeLongArray(rxPackets); |
| 163 | dest.writeLongArray(txBytes); |
| 164 | dest.writeLongArray(txPackets); |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 165 | dest.writeLongArray(operations); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jeff Sharkey | 4abb1b8 | 2011-11-08 17:35:28 -0800 | [diff] [blame^] | 168 | @Override |
| 169 | public NetworkStats clone() { |
| 170 | final NetworkStats clone = new NetworkStats(elapsedRealtime, size); |
| 171 | NetworkStats.Entry entry = null; |
| 172 | for (int i = 0; i < size; i++) { |
| 173 | entry = getValues(i, entry); |
| 174 | clone.addValues(entry); |
| 175 | } |
| 176 | return clone; |
| 177 | } |
| 178 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 179 | // @VisibleForTesting |
| 180 | public NetworkStats addIfaceValues( |
| 181 | String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) { |
| 182 | return addValues( |
| 183 | iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 186 | // @VisibleForTesting |
| 187 | public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes, |
| 188 | long rxPackets, long txBytes, long txPackets, long operations) { |
| 189 | return addValues(new Entry( |
| 190 | iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations)); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Add new stats entry, copying from given {@link Entry}. The {@link Entry} |
| 195 | * object can be recycled across multiple calls. |
| 196 | */ |
| 197 | public NetworkStats addValues(Entry entry) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 198 | if (size >= this.iface.length) { |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 199 | final int newLength = Math.max(iface.length, 10) * 3 / 2; |
| 200 | iface = Arrays.copyOf(iface, newLength); |
| 201 | uid = Arrays.copyOf(uid, newLength); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 202 | set = Arrays.copyOf(set, newLength); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 203 | tag = Arrays.copyOf(tag, newLength); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 204 | rxBytes = Arrays.copyOf(rxBytes, newLength); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 205 | rxPackets = Arrays.copyOf(rxPackets, newLength); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 206 | txBytes = Arrays.copyOf(txBytes, newLength); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 207 | txPackets = Arrays.copyOf(txPackets, newLength); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 208 | operations = Arrays.copyOf(operations, newLength); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 211 | iface[size] = entry.iface; |
| 212 | uid[size] = entry.uid; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 213 | set[size] = entry.set; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 214 | tag[size] = entry.tag; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 215 | rxBytes[size] = entry.rxBytes; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 216 | rxPackets[size] = entry.rxPackets; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 217 | txBytes[size] = entry.txBytes; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 218 | txPackets[size] = entry.txPackets; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 219 | operations[size] = entry.operations; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 220 | size++; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 221 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 222 | return this; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 225 | /** |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 226 | * Return specific stats entry. |
| 227 | */ |
| 228 | public Entry getValues(int i, Entry recycle) { |
| 229 | final Entry entry = recycle != null ? recycle : new Entry(); |
| 230 | entry.iface = iface[i]; |
| 231 | entry.uid = uid[i]; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 232 | entry.set = set[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 233 | entry.tag = tag[i]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 234 | entry.rxBytes = rxBytes[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 235 | entry.rxPackets = rxPackets[i]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 236 | entry.txBytes = txBytes[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 237 | entry.txPackets = txPackets[i]; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 238 | entry.operations = operations[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 239 | return entry; |
| 240 | } |
| 241 | |
| 242 | public long getElapsedRealtime() { |
| 243 | return elapsedRealtime; |
| 244 | } |
| 245 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 246 | /** |
| 247 | * Return age of this {@link NetworkStats} object with respect to |
| 248 | * {@link SystemClock#elapsedRealtime()}. |
| 249 | */ |
| 250 | public long getElapsedRealtimeAge() { |
| 251 | return SystemClock.elapsedRealtime() - elapsedRealtime; |
| 252 | } |
| 253 | |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 254 | public int size() { |
| 255 | return size; |
| 256 | } |
| 257 | |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 258 | // @VisibleForTesting |
| 259 | public int internalSize() { |
| 260 | return iface.length; |
| 261 | } |
| 262 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 263 | @Deprecated |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 264 | public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets, |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 265 | long txBytes, long txPackets, long operations) { |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 266 | return combineValues( |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 267 | iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations); |
| 268 | } |
| 269 | |
| 270 | public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes, |
| 271 | long rxPackets, long txBytes, long txPackets, long operations) { |
| 272 | return combineValues(new Entry( |
| 273 | iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations)); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 276 | /** |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 277 | * Combine given values with an existing row, or create a new row if |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 278 | * {@link #findIndex(String, int, int, int)} is unable to find match. Can |
| 279 | * also be used to subtract values from existing rows. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 280 | */ |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 281 | public NetworkStats combineValues(Entry entry) { |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 282 | final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 283 | if (i == -1) { |
| 284 | // only create new entry when positive contribution |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 285 | addValues(entry); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 286 | } else { |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 287 | rxBytes[i] += entry.rxBytes; |
| 288 | rxPackets[i] += entry.rxPackets; |
| 289 | txBytes[i] += entry.txBytes; |
| 290 | txPackets[i] += entry.txPackets; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 291 | operations[i] += entry.operations; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 292 | } |
| 293 | return this; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 296 | /** |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 297 | * Combine all values from another {@link NetworkStats} into this object. |
| 298 | */ |
| 299 | public void combineAllValues(NetworkStats another) { |
| 300 | NetworkStats.Entry entry = null; |
| 301 | for (int i = 0; i < another.size; i++) { |
| 302 | entry = another.getValues(i, entry); |
| 303 | combineValues(entry); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 308 | * Find first stats index that matches the requested parameters. |
| 309 | */ |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 310 | public int findIndex(String iface, int uid, int set, int tag) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 311 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 312 | if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i] |
| 313 | && Objects.equal(iface, this.iface[i])) { |
| 314 | return i; |
| 315 | } |
| 316 | } |
| 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Find first stats index that matches the requested parameters, starting |
| 322 | * search around the hinted index as an optimization. |
| 323 | */ |
| 324 | // @VisibleForTesting |
| 325 | public int findIndexHinted(String iface, int uid, int set, int tag, int hintIndex) { |
| 326 | for (int offset = 0; offset < size; offset++) { |
| 327 | final int halfOffset = offset / 2; |
| 328 | |
| 329 | // search outwards from hint index, alternating forward and backward |
| 330 | final int i; |
| 331 | if (offset % 2 == 0) { |
| 332 | i = (hintIndex + halfOffset) % size; |
| 333 | } else { |
| 334 | i = (size + hintIndex - halfOffset - 1) % size; |
| 335 | } |
| 336 | |
| 337 | if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i] |
| 338 | && Objects.equal(iface, this.iface[i])) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 339 | return i; |
| 340 | } |
| 341 | } |
| 342 | return -1; |
| 343 | } |
| 344 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 345 | /** |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 346 | * Splice in {@link #operations} from the given {@link NetworkStats} based |
| 347 | * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface}, |
| 348 | * since operation counts are at data layer. |
| 349 | */ |
| 350 | public void spliceOperationsFrom(NetworkStats stats) { |
| 351 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 352 | final int j = stats.findIndex(IFACE_ALL, uid[i], set[i], tag[i]); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 353 | if (j == -1) { |
| 354 | operations[i] = 0; |
| 355 | } else { |
| 356 | operations[i] = stats.operations[j]; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 362 | * Return list of unique interfaces known by this data structure. |
| 363 | */ |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 364 | public String[] getUniqueIfaces() { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 365 | final HashSet<String> ifaces = new HashSet<String>(); |
| 366 | for (String iface : this.iface) { |
| 367 | if (iface != IFACE_ALL) { |
| 368 | ifaces.add(iface); |
| 369 | } |
| 370 | } |
| 371 | return ifaces.toArray(new String[ifaces.size()]); |
| 372 | } |
| 373 | |
| 374 | /** |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 375 | * Return list of unique UIDs known by this data structure. |
| 376 | */ |
| 377 | public int[] getUniqueUids() { |
| 378 | final SparseBooleanArray uids = new SparseBooleanArray(); |
| 379 | for (int uid : this.uid) { |
| 380 | uids.put(uid, true); |
| 381 | } |
| 382 | |
| 383 | final int size = uids.size(); |
| 384 | final int[] result = new int[size]; |
| 385 | for (int i = 0; i < size; i++) { |
| 386 | result[i] = uids.keyAt(i); |
| 387 | } |
| 388 | return result; |
| 389 | } |
| 390 | |
| 391 | /** |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 392 | * Return total bytes represented by this snapshot object, usually used when |
| 393 | * checking if a {@link #subtract(NetworkStats)} delta passes a threshold. |
| 394 | */ |
| 395 | public long getTotalBytes() { |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 396 | final Entry entry = getTotal(null); |
| 397 | return entry.rxBytes + entry.txBytes; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Return total of all fields represented by this snapshot object. |
| 402 | */ |
| 403 | public Entry getTotal(Entry recycle) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 404 | return getTotal(recycle, null, UID_ALL); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Return total of all fields represented by this snapshot object matching |
| 409 | * the requested {@link #uid}. |
| 410 | */ |
| 411 | public Entry getTotal(Entry recycle, int limitUid) { |
| 412 | return getTotal(recycle, null, limitUid); |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Return total of all fields represented by this snapshot object matching |
| 417 | * the requested {@link #iface}. |
| 418 | */ |
| 419 | public Entry getTotal(Entry recycle, HashSet<String> limitIface) { |
| 420 | return getTotal(recycle, limitIface, UID_ALL); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Return total of all fields represented by this snapshot object matching |
| 425 | * the requested {@link #iface} and {@link #uid}. |
| 426 | * |
| 427 | * @param limitIface Set of {@link #iface} to include in total; or {@code |
| 428 | * null} to include all ifaces. |
| 429 | */ |
| 430 | private Entry getTotal(Entry recycle, HashSet<String> limitIface, int limitUid) { |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 431 | final Entry entry = recycle != null ? recycle : new Entry(); |
| 432 | |
| 433 | entry.iface = IFACE_ALL; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 434 | entry.uid = limitUid; |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 435 | entry.set = SET_ALL; |
| 436 | entry.tag = TAG_NONE; |
| 437 | entry.rxBytes = 0; |
| 438 | entry.rxPackets = 0; |
| 439 | entry.txBytes = 0; |
| 440 | entry.txPackets = 0; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 441 | entry.operations = 0; |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 442 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 443 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 444 | final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]); |
| 445 | final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i])); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 446 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 447 | if (matchesUid && matchesIface) { |
| 448 | // skip specific tags, since already counted in TAG_NONE |
| 449 | if (tag[i] != TAG_NONE) continue; |
| 450 | |
| 451 | entry.rxBytes += rxBytes[i]; |
| 452 | entry.rxPackets += rxPackets[i]; |
| 453 | entry.txBytes += txBytes[i]; |
| 454 | entry.txPackets += txPackets[i]; |
| 455 | entry.operations += operations[i]; |
| 456 | } |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 457 | } |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 458 | return entry; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 462 | * Subtract the given {@link NetworkStats}, effectively leaving the delta |
| 463 | * between two snapshots in time. Assumes that statistics rows collect over |
| 464 | * time, and that none of them have disappeared. |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 465 | */ |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 466 | public NetworkStats subtract(NetworkStats value) throws NonMonotonicException { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 467 | final long deltaRealtime = this.elapsedRealtime - value.elapsedRealtime; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 468 | if (deltaRealtime < 0) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 469 | throw new IllegalArgumentException("found non-monotonic realtime"); |
| 470 | } |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 471 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 472 | // result will have our rows, and elapsed time between snapshots |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 473 | final Entry entry = new Entry(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 474 | final NetworkStats result = new NetworkStats(deltaRealtime, size); |
| 475 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 476 | entry.iface = iface[i]; |
| 477 | entry.uid = uid[i]; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 478 | entry.set = set[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 479 | entry.tag = tag[i]; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 480 | |
| 481 | // find remote row that matches, and subtract |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 482 | final int j = value.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag, i); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 483 | if (j == -1) { |
| 484 | // newly appearing row, return entire value |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 485 | entry.rxBytes = rxBytes[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 486 | entry.rxPackets = rxPackets[i]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 487 | entry.txBytes = txBytes[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 488 | entry.txPackets = txPackets[i]; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 489 | entry.operations = operations[i]; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 490 | } else { |
| 491 | // existing row, subtract remote value |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 492 | entry.rxBytes = rxBytes[i] - value.rxBytes[j]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 493 | entry.rxPackets = rxPackets[i] - value.rxPackets[j]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 494 | entry.txBytes = txBytes[i] - value.txBytes[j]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 495 | entry.txPackets = txPackets[i] - value.txPackets[j]; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 496 | entry.operations = operations[i] - value.operations[j]; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 497 | |
| 498 | if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0 |
| 499 | || entry.txPackets < 0 || entry.operations < 0) { |
| 500 | throw new NonMonotonicException(this, i, value, j); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 501 | } |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 502 | } |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 503 | |
| 504 | result.addValues(entry); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 507 | return result; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 510 | /** |
| 511 | * Return total statistics grouped by {@link #iface}; doesn't mutate the |
| 512 | * original structure. |
| 513 | */ |
| 514 | public NetworkStats groupedByIface() { |
| 515 | final NetworkStats stats = new NetworkStats(elapsedRealtime, 10); |
| 516 | |
| 517 | final Entry entry = new Entry(); |
| 518 | entry.uid = UID_ALL; |
| 519 | entry.set = SET_ALL; |
| 520 | entry.tag = TAG_NONE; |
| 521 | entry.operations = 0L; |
| 522 | |
| 523 | for (int i = 0; i < size; i++) { |
| 524 | // skip specific tags, since already counted in TAG_NONE |
| 525 | if (tag[i] != TAG_NONE) continue; |
| 526 | |
| 527 | entry.iface = iface[i]; |
| 528 | entry.rxBytes = rxBytes[i]; |
| 529 | entry.rxPackets = rxPackets[i]; |
| 530 | entry.txBytes = txBytes[i]; |
| 531 | entry.txPackets = txPackets[i]; |
| 532 | stats.combineValues(entry); |
| 533 | } |
| 534 | |
| 535 | return stats; |
| 536 | } |
| 537 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 538 | /** |
| 539 | * Return total statistics grouped by {@link #uid}; doesn't mutate the |
| 540 | * original structure. |
| 541 | */ |
| 542 | public NetworkStats groupedByUid() { |
| 543 | final NetworkStats stats = new NetworkStats(elapsedRealtime, 10); |
| 544 | |
| 545 | final Entry entry = new Entry(); |
| 546 | entry.iface = IFACE_ALL; |
| 547 | entry.set = SET_ALL; |
| 548 | entry.tag = TAG_NONE; |
| 549 | |
| 550 | for (int i = 0; i < size; i++) { |
| 551 | // skip specific tags, since already counted in TAG_NONE |
| 552 | if (tag[i] != TAG_NONE) continue; |
| 553 | |
| 554 | entry.uid = uid[i]; |
| 555 | entry.rxBytes = rxBytes[i]; |
| 556 | entry.rxPackets = rxPackets[i]; |
| 557 | entry.txBytes = txBytes[i]; |
| 558 | entry.txPackets = txPackets[i]; |
| 559 | entry.operations = operations[i]; |
| 560 | stats.combineValues(entry); |
| 561 | } |
| 562 | |
| 563 | return stats; |
| 564 | } |
| 565 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 566 | /** |
| 567 | * Return all rows except those attributed to the requested UID; doesn't |
| 568 | * mutate the original structure. |
| 569 | */ |
| 570 | public NetworkStats withoutUid(int uid) { |
| 571 | final NetworkStats stats = new NetworkStats(elapsedRealtime, 10); |
| 572 | |
| 573 | Entry entry = new Entry(); |
| 574 | for (int i = 0; i < size; i++) { |
| 575 | entry = getValues(i, entry); |
| 576 | if (entry.uid != uid) { |
| 577 | stats.addValues(entry); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | return stats; |
| 582 | } |
| 583 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 584 | public void dump(String prefix, PrintWriter pw) { |
| 585 | pw.print(prefix); |
| 586 | pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 587 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 588 | pw.print(prefix); |
| 589 | pw.print(" iface="); pw.print(iface[i]); |
| 590 | pw.print(" uid="); pw.print(uid[i]); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 591 | pw.print(" set="); pw.print(setToString(set[i])); |
| 592 | pw.print(" tag="); pw.print(tagToString(tag[i])); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 593 | pw.print(" rxBytes="); pw.print(rxBytes[i]); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 594 | pw.print(" rxPackets="); pw.print(rxPackets[i]); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 595 | pw.print(" txBytes="); pw.print(txBytes[i]); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 596 | pw.print(" txPackets="); pw.print(txPackets[i]); |
| 597 | pw.print(" operations="); pw.println(operations[i]); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 601 | /** |
| 602 | * Return text description of {@link #set} value. |
| 603 | */ |
| 604 | public static String setToString(int set) { |
| 605 | switch (set) { |
| 606 | case SET_ALL: |
| 607 | return "ALL"; |
| 608 | case SET_DEFAULT: |
| 609 | return "DEFAULT"; |
| 610 | case SET_FOREGROUND: |
| 611 | return "FOREGROUND"; |
| 612 | default: |
| 613 | return "UNKNOWN"; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Return text description of {@link #tag} value. |
| 619 | */ |
| 620 | public static String tagToString(int tag) { |
| 621 | return "0x" + Integer.toHexString(tag); |
| 622 | } |
| 623 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 624 | @Override |
| 625 | public String toString() { |
| 626 | final CharArrayWriter writer = new CharArrayWriter(); |
| 627 | dump("", new PrintWriter(writer)); |
| 628 | return writer.toString(); |
| 629 | } |
| 630 | |
| 631 | /** {@inheritDoc} */ |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 632 | public int describeContents() { |
| 633 | return 0; |
| 634 | } |
| 635 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 636 | public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() { |
| 637 | public NetworkStats createFromParcel(Parcel in) { |
| 638 | return new NetworkStats(in); |
| 639 | } |
| 640 | |
| 641 | public NetworkStats[] newArray(int size) { |
| 642 | return new NetworkStats[size]; |
| 643 | } |
| 644 | }; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 645 | |
| 646 | public static class NonMonotonicException extends Exception { |
| 647 | public final NetworkStats left; |
| 648 | public final NetworkStats right; |
| 649 | public final int leftIndex; |
| 650 | public final int rightIndex; |
| 651 | |
| 652 | public NonMonotonicException( |
| 653 | NetworkStats left, int leftIndex, NetworkStats right, int rightIndex) { |
| 654 | this.left = checkNotNull(left, "missing left"); |
| 655 | this.right = checkNotNull(right, "missing right"); |
| 656 | this.leftIndex = leftIndex; |
| 657 | this.rightIndex = rightIndex; |
| 658 | } |
| 659 | } |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 660 | } |