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