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