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