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