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