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