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 | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 45 | /** {@link #tag} value for without tag. */ |
| 46 | public static final int TAG_NONE = 0; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 47 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 48 | /** |
| 49 | * {@link SystemClock#elapsedRealtime()} timestamp when this data was |
| 50 | * generated. |
| 51 | */ |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 52 | private final long elapsedRealtime; |
| 53 | private int size; |
| 54 | private String[] iface; |
| 55 | private int[] uid; |
| 56 | private int[] tag; |
| 57 | private long[] rxBytes; |
| 58 | private long[] rxPackets; |
| 59 | private long[] txBytes; |
| 60 | private long[] txPackets; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 61 | private int[] operations; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 62 | |
| 63 | public static class Entry { |
| 64 | public String iface; |
| 65 | public int uid; |
| 66 | public int tag; |
| 67 | public long rxBytes; |
| 68 | public long rxPackets; |
| 69 | public long txBytes; |
| 70 | public long txPackets; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 71 | public int operations; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 72 | |
| 73 | public Entry() { |
| 74 | } |
| 75 | |
| 76 | public Entry(String iface, int uid, int tag, long rxBytes, long rxPackets, long txBytes, |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 77 | long txPackets, int operations) { |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 78 | this.iface = iface; |
| 79 | this.uid = uid; |
| 80 | this.tag = tag; |
| 81 | this.rxBytes = rxBytes; |
| 82 | this.rxPackets = rxPackets; |
| 83 | this.txBytes = txBytes; |
| 84 | this.txPackets = txPackets; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 85 | this.operations = operations; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 86 | } |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 87 | } |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 88 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 89 | public NetworkStats(long elapsedRealtime, int initialSize) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 90 | this.elapsedRealtime = elapsedRealtime; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 91 | this.size = 0; |
| 92 | this.iface = new String[initialSize]; |
| 93 | this.uid = new int[initialSize]; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 94 | this.tag = new int[initialSize]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 95 | this.rxBytes = new long[initialSize]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 96 | this.rxPackets = new long[initialSize]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 97 | this.txBytes = new long[initialSize]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 98 | this.txPackets = new long[initialSize]; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 99 | this.operations = new int[initialSize]; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | public NetworkStats(Parcel parcel) { |
| 103 | elapsedRealtime = parcel.readLong(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 104 | size = parcel.readInt(); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 105 | iface = parcel.createStringArray(); |
| 106 | uid = parcel.createIntArray(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 107 | tag = parcel.createIntArray(); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 108 | rxBytes = parcel.createLongArray(); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 109 | rxPackets = parcel.createLongArray(); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 110 | txBytes = parcel.createLongArray(); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 111 | txPackets = parcel.createLongArray(); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 112 | operations = parcel.createIntArray(); |
| 113 | } |
| 114 | |
| 115 | /** {@inheritDoc} */ |
| 116 | public void writeToParcel(Parcel dest, int flags) { |
| 117 | dest.writeLong(elapsedRealtime); |
| 118 | dest.writeInt(size); |
| 119 | dest.writeStringArray(iface); |
| 120 | dest.writeIntArray(uid); |
| 121 | dest.writeIntArray(tag); |
| 122 | dest.writeLongArray(rxBytes); |
| 123 | dest.writeLongArray(rxPackets); |
| 124 | dest.writeLongArray(txBytes); |
| 125 | dest.writeLongArray(txPackets); |
| 126 | dest.writeIntArray(operations); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 129 | public NetworkStats addValues(String iface, int uid, int tag, long rxBytes, long rxPackets, |
| 130 | long txBytes, long txPackets) { |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 131 | return addValues(iface, uid, tag, rxBytes, rxPackets, txBytes, txPackets, 0); |
| 132 | } |
| 133 | |
| 134 | public NetworkStats addValues(String iface, int uid, int tag, long rxBytes, long rxPackets, |
| 135 | long txBytes, long txPackets, int operations) { |
| 136 | return addValues( |
| 137 | new Entry(iface, uid, tag, rxBytes, rxPackets, txBytes, txPackets, operations)); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Add new stats entry, copying from given {@link Entry}. The {@link Entry} |
| 142 | * object can be recycled across multiple calls. |
| 143 | */ |
| 144 | public NetworkStats addValues(Entry entry) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 145 | if (size >= this.iface.length) { |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 146 | final int newLength = Math.max(iface.length, 10) * 3 / 2; |
| 147 | iface = Arrays.copyOf(iface, newLength); |
| 148 | uid = Arrays.copyOf(uid, newLength); |
| 149 | tag = Arrays.copyOf(tag, newLength); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 150 | rxBytes = Arrays.copyOf(rxBytes, newLength); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 151 | rxPackets = Arrays.copyOf(rxPackets, newLength); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 152 | txBytes = Arrays.copyOf(txBytes, newLength); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 153 | txPackets = Arrays.copyOf(txPackets, newLength); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 154 | operations = Arrays.copyOf(operations, newLength); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 157 | iface[size] = entry.iface; |
| 158 | uid[size] = entry.uid; |
| 159 | tag[size] = entry.tag; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 160 | rxBytes[size] = entry.rxBytes; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 161 | rxPackets[size] = entry.rxPackets; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 162 | txBytes[size] = entry.txBytes; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 163 | txPackets[size] = entry.txPackets; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 164 | operations[size] = entry.operations; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 165 | size++; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 166 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 167 | return this; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 170 | /** |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 171 | * Return specific stats entry. |
| 172 | */ |
| 173 | public Entry getValues(int i, Entry recycle) { |
| 174 | final Entry entry = recycle != null ? recycle : new Entry(); |
| 175 | entry.iface = iface[i]; |
| 176 | entry.uid = uid[i]; |
| 177 | entry.tag = tag[i]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 178 | entry.rxBytes = rxBytes[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 179 | entry.rxPackets = rxPackets[i]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 180 | entry.txBytes = txBytes[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 181 | entry.txPackets = txPackets[i]; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 182 | entry.operations = operations[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 183 | return entry; |
| 184 | } |
| 185 | |
| 186 | public long getElapsedRealtime() { |
| 187 | return elapsedRealtime; |
| 188 | } |
| 189 | |
| 190 | public int size() { |
| 191 | return size; |
| 192 | } |
| 193 | |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 194 | // @VisibleForTesting |
| 195 | public int internalSize() { |
| 196 | return iface.length; |
| 197 | } |
| 198 | |
| 199 | public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets, |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 200 | long txBytes, long txPackets, int operations) { |
| 201 | return combineValues( |
| 202 | new Entry(iface, uid, tag, rxBytes, rxPackets, txBytes, txPackets, operations)); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 205 | /** |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 206 | * Combine given values with an existing row, or create a new row if |
| 207 | * {@link #findIndex(String, int, int)} is unable to find match. Can also be |
| 208 | * used to subtract values from existing rows. |
| 209 | */ |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 210 | public NetworkStats combineValues(Entry entry) { |
| 211 | final int i = findIndex(entry.iface, entry.uid, entry.tag); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 212 | if (i == -1) { |
| 213 | // only create new entry when positive contribution |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 214 | addValues(entry); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 215 | } else { |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 216 | rxBytes[i] += entry.rxBytes; |
| 217 | rxPackets[i] += entry.rxPackets; |
| 218 | txBytes[i] += entry.txBytes; |
| 219 | txPackets[i] += entry.txPackets; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 220 | operations[i] += entry.operations; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 221 | } |
| 222 | return this; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 225 | /** |
| 226 | * Find first stats index that matches the requested parameters. |
| 227 | */ |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 228 | public int findIndex(String iface, int uid, int tag) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 229 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 230 | if (Objects.equal(iface, this.iface[i]) && uid == this.uid[i] && tag == this.tag[i]) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 231 | return i; |
| 232 | } |
| 233 | } |
| 234 | return -1; |
| 235 | } |
| 236 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 237 | /** |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 238 | * Splice in {@link #operations} from the given {@link NetworkStats} based |
| 239 | * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface}, |
| 240 | * since operation counts are at data layer. |
| 241 | */ |
| 242 | public void spliceOperationsFrom(NetworkStats stats) { |
| 243 | for (int i = 0; i < size; i++) { |
| 244 | final int j = stats.findIndex(IFACE_ALL, uid[i], tag[i]); |
| 245 | if (j == -1) { |
| 246 | operations[i] = 0; |
| 247 | } else { |
| 248 | operations[i] = stats.operations[j]; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 254 | * Return list of unique interfaces known by this data structure. |
| 255 | */ |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 256 | public String[] getUniqueIfaces() { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 257 | final HashSet<String> ifaces = new HashSet<String>(); |
| 258 | for (String iface : this.iface) { |
| 259 | if (iface != IFACE_ALL) { |
| 260 | ifaces.add(iface); |
| 261 | } |
| 262 | } |
| 263 | return ifaces.toArray(new String[ifaces.size()]); |
| 264 | } |
| 265 | |
| 266 | /** |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 267 | * Return list of unique UIDs known by this data structure. |
| 268 | */ |
| 269 | public int[] getUniqueUids() { |
| 270 | final SparseBooleanArray uids = new SparseBooleanArray(); |
| 271 | for (int uid : this.uid) { |
| 272 | uids.put(uid, true); |
| 273 | } |
| 274 | |
| 275 | final int size = uids.size(); |
| 276 | final int[] result = new int[size]; |
| 277 | for (int i = 0; i < size; i++) { |
| 278 | result[i] = uids.keyAt(i); |
| 279 | } |
| 280 | return result; |
| 281 | } |
| 282 | |
| 283 | /** |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 284 | * Subtract the given {@link NetworkStats}, effectively leaving the delta |
| 285 | * between two snapshots in time. Assumes that statistics rows collect over |
| 286 | * time, and that none of them have disappeared. |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 287 | * |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 288 | * @throws IllegalArgumentException when given {@link NetworkStats} is |
| 289 | * non-monotonic. |
| 290 | */ |
| 291 | public NetworkStats subtract(NetworkStats value) { |
| 292 | return subtract(value, true, false); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Subtract the given {@link NetworkStats}, effectively leaving the delta |
| 297 | * between two snapshots in time. Assumes that statistics rows collect over |
| 298 | * time, and that none of them have disappeared. |
| 299 | * <p> |
| 300 | * Instead of throwing when counters are non-monotonic, this variant clamps |
| 301 | * results to never be negative. |
| 302 | */ |
| 303 | public NetworkStats subtractClamped(NetworkStats value) { |
| 304 | return subtract(value, false, true); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Subtract the given {@link NetworkStats}, effectively leaving the delta |
| 309 | * between two snapshots in time. Assumes that statistics rows collect over |
| 310 | * time, and that none of them have disappeared. |
| 311 | * |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 312 | * @param enforceMonotonic Validate that incoming value is strictly |
| 313 | * monotonic compared to this object. |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 314 | * @param clampNegative Instead of throwing like {@code enforceMonotonic}, |
| 315 | * clamp resulting counters at 0 to prevent negative values. |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 316 | */ |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 317 | private NetworkStats subtract( |
| 318 | NetworkStats value, boolean enforceMonotonic, boolean clampNegative) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 319 | final long deltaRealtime = this.elapsedRealtime - value.elapsedRealtime; |
| 320 | if (enforceMonotonic && deltaRealtime < 0) { |
| 321 | throw new IllegalArgumentException("found non-monotonic realtime"); |
| 322 | } |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 323 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 324 | // result will have our rows, and elapsed time between snapshots |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 325 | final Entry entry = new Entry(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 326 | final NetworkStats result = new NetworkStats(deltaRealtime, size); |
| 327 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 328 | entry.iface = iface[i]; |
| 329 | entry.uid = uid[i]; |
| 330 | entry.tag = tag[i]; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 331 | |
| 332 | // find remote row that matches, and subtract |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 333 | final int j = value.findIndex(entry.iface, entry.uid, entry.tag); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 334 | if (j == -1) { |
| 335 | // newly appearing row, return entire value |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 336 | entry.rxBytes = rxBytes[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 337 | entry.rxPackets = rxPackets[i]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 338 | entry.txBytes = txBytes[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 339 | entry.txPackets = txPackets[i]; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 340 | entry.operations = operations[i]; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 341 | } else { |
| 342 | // existing row, subtract remote value |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 343 | entry.rxBytes = rxBytes[i] - value.rxBytes[j]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 344 | entry.rxPackets = rxPackets[i] - value.rxPackets[j]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 345 | entry.txBytes = txBytes[i] - value.txBytes[j]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 346 | entry.txPackets = txPackets[i] - value.txPackets[j]; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 347 | entry.operations = operations[i] - value.operations[j]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 348 | if (enforceMonotonic |
| 349 | && (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0 |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 350 | || entry.txPackets < 0 || entry.operations < 0)) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 351 | throw new IllegalArgumentException("found non-monotonic values"); |
| 352 | } |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 353 | if (clampNegative) { |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 354 | entry.rxBytes = Math.max(0, entry.rxBytes); |
| 355 | entry.rxPackets = Math.max(0, entry.rxPackets); |
| 356 | entry.txBytes = Math.max(0, entry.txBytes); |
| 357 | entry.txPackets = Math.max(0, entry.txPackets); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 358 | entry.operations = Math.max(0, entry.operations); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 359 | } |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 360 | } |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 361 | |
| 362 | result.addValues(entry); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 365 | return result; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 368 | public void dump(String prefix, PrintWriter pw) { |
| 369 | pw.print(prefix); |
| 370 | pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 371 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 372 | pw.print(prefix); |
| 373 | pw.print(" iface="); pw.print(iface[i]); |
| 374 | pw.print(" uid="); pw.print(uid[i]); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 375 | pw.print(" tag=0x"); pw.print(Integer.toHexString(tag[i])); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 376 | pw.print(" rxBytes="); pw.print(rxBytes[i]); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 377 | pw.print(" rxPackets="); pw.print(rxPackets[i]); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 378 | pw.print(" txBytes="); pw.print(txBytes[i]); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame^] | 379 | pw.print(" txPackets="); pw.print(txPackets[i]); |
| 380 | pw.print(" operations="); pw.println(operations[i]); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | @Override |
| 385 | public String toString() { |
| 386 | final CharArrayWriter writer = new CharArrayWriter(); |
| 387 | dump("", new PrintWriter(writer)); |
| 388 | return writer.toString(); |
| 389 | } |
| 390 | |
| 391 | /** {@inheritDoc} */ |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 392 | public int describeContents() { |
| 393 | return 0; |
| 394 | } |
| 395 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 396 | public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() { |
| 397 | public NetworkStats createFromParcel(Parcel in) { |
| 398 | return new NetworkStats(in); |
| 399 | } |
| 400 | |
| 401 | public NetworkStats[] newArray(int size) { |
| 402 | return new NetworkStats[size]; |
| 403 | } |
| 404 | }; |
| 405 | } |