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