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; |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 22 | import android.util.Slog; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 23 | import android.util.SparseBooleanArray; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 24 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 25 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 26 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 27 | |
Jeff Sharkey | 7a8f1a3 | 2014-09-17 09:26:28 -0700 | [diff] [blame] | 28 | import libcore.util.EmptyArray; |
| 29 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 30 | import java.io.CharArrayWriter; |
| 31 | import java.io.PrintWriter; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 32 | import java.util.Arrays; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 33 | import java.util.HashSet; |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 34 | import java.util.Objects; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 37 | * Collection of active network statistics. Can contain summary details across |
| 38 | * all interfaces, or details with per-UID granularity. Internally stores data |
| 39 | * as a large table, closely matching {@code /proc/} data format. This structure |
| 40 | * optimizes for rapid in-memory comparison, but consider using |
| 41 | * {@link NetworkStatsHistory} when persisting. |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 42 | * |
| 43 | * @hide |
| 44 | */ |
| 45 | public class NetworkStats implements Parcelable { |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 46 | private static final String TAG = "NetworkStats"; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 47 | /** {@link #iface} value when interface details unavailable. */ |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 48 | public static final String IFACE_ALL = null; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 49 | /** {@link #uid} value when UID details unavailable. */ |
| 50 | public static final int UID_ALL = -1; |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 51 | /** {@link #tag} value matching any tag. */ |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 52 | // TODO: Rename TAG_ALL to TAG_ANY. |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 53 | public static final int TAG_ALL = -1; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 54 | /** {@link #set} value for all sets combined, not including debug sets. */ |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 55 | public static final int SET_ALL = -1; |
| 56 | /** {@link #set} value where background data is accounted. */ |
| 57 | public static final int SET_DEFAULT = 0; |
| 58 | /** {@link #set} value where foreground data is accounted. */ |
| 59 | public static final int SET_FOREGROUND = 1; |
Wenchao Tong | 98170b0 | 2015-03-17 16:14:23 -0700 | [diff] [blame] | 60 | /** All {@link #set} value greater than SET_DEBUG_START are debug {@link #set} values. */ |
| 61 | public static final int SET_DEBUG_START = 1000; |
| 62 | /** Debug {@link #set} value when the VPN stats are moved in. */ |
| 63 | public static final int SET_DBG_VPN_IN = 1001; |
| 64 | /** Debug {@link #set} value when the VPN stats are moved out of a vpn UID. */ |
| 65 | public static final int SET_DBG_VPN_OUT = 1002; |
| 66 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 67 | /** {@link #tag} value for total data across all tags. */ |
Antonio Cansado | 46c75367 | 2015-12-10 15:57:56 -0800 | [diff] [blame] | 68 | // TODO: Rename TAG_NONE to TAG_ALL. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 69 | public static final int TAG_NONE = 0; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 70 | |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 71 | /** {@link #set} value for all roaming values. */ |
| 72 | public static final int ROAMING_ALL = -1; |
| 73 | /** {@link #set} value where native, non-roaming data is accounted. */ |
Jeff Davidson | 1f7e05e | 2016-03-10 13:21:38 -0800 | [diff] [blame] | 74 | public static final int ROAMING_NO = 0; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 75 | /** {@link #set} value where roaming data is accounted. */ |
Jeff Davidson | 1f7e05e | 2016-03-10 13:21:38 -0800 | [diff] [blame] | 76 | public static final int ROAMING_YES = 1; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 77 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 78 | // TODO: move fields to "mVariable" notation |
| 79 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 80 | /** |
| 81 | * {@link SystemClock#elapsedRealtime()} timestamp when this data was |
| 82 | * generated. |
| 83 | */ |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 84 | private long elapsedRealtime; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 85 | private int size; |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 86 | private int capacity; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 87 | private String[] iface; |
| 88 | private int[] uid; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 89 | private int[] set; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 90 | private int[] tag; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 91 | private int[] roaming; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 92 | private long[] rxBytes; |
| 93 | private long[] rxPackets; |
| 94 | private long[] txBytes; |
| 95 | private long[] txPackets; |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 96 | private long[] operations; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 97 | |
| 98 | public static class Entry { |
| 99 | public String iface; |
| 100 | public int uid; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 101 | public int set; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 102 | public int tag; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 103 | /** |
| 104 | * Note that this is only populated w/ the default value when read from /proc or written |
| 105 | * to disk. We merge in the correct value when reporting this value to clients of |
| 106 | * getSummary(). |
| 107 | */ |
| 108 | public int roaming; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 109 | public long rxBytes; |
| 110 | public long rxPackets; |
| 111 | public long txBytes; |
| 112 | public long txPackets; |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 113 | public long operations; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 114 | |
| 115 | public Entry() { |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 116 | 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] | 117 | } |
| 118 | |
| 119 | public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 120 | this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, |
| 121 | operations); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 124 | public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets, |
| 125 | long txBytes, long txPackets, long operations) { |
Jeff Davidson | 1f7e05e | 2016-03-10 13:21:38 -0800 | [diff] [blame] | 126 | this(iface, uid, set, tag, ROAMING_NO, rxBytes, rxPackets, txBytes, txPackets, |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 127 | operations); |
| 128 | } |
| 129 | |
| 130 | public Entry(String iface, int uid, int set, int tag, int roaming, long rxBytes, |
| 131 | long rxPackets, long txBytes, long txPackets, long operations) { |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 132 | this.iface = iface; |
| 133 | this.uid = uid; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 134 | this.set = set; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 135 | this.tag = tag; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 136 | this.roaming = roaming; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 137 | this.rxBytes = rxBytes; |
| 138 | this.rxPackets = rxPackets; |
| 139 | this.txBytes = txBytes; |
| 140 | this.txPackets = txPackets; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 141 | this.operations = operations; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 142 | } |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 143 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 144 | public boolean isNegative() { |
| 145 | return rxBytes < 0 || rxPackets < 0 || txBytes < 0 || txPackets < 0 || operations < 0; |
| 146 | } |
| 147 | |
| 148 | public boolean isEmpty() { |
| 149 | return rxBytes == 0 && rxPackets == 0 && txBytes == 0 && txPackets == 0 |
| 150 | && operations == 0; |
| 151 | } |
| 152 | |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 153 | public void add(Entry another) { |
| 154 | this.rxBytes += another.rxBytes; |
| 155 | this.rxPackets += another.rxPackets; |
| 156 | this.txBytes += another.txBytes; |
| 157 | this.txPackets += another.txPackets; |
| 158 | this.operations += another.operations; |
| 159 | } |
| 160 | |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 161 | @Override |
| 162 | public String toString() { |
| 163 | final StringBuilder builder = new StringBuilder(); |
| 164 | builder.append("iface=").append(iface); |
| 165 | builder.append(" uid=").append(uid); |
| 166 | builder.append(" set=").append(setToString(set)); |
| 167 | builder.append(" tag=").append(tagToString(tag)); |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 168 | builder.append(" roaming=").append(roamingToString(roaming)); |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 169 | builder.append(" rxBytes=").append(rxBytes); |
| 170 | builder.append(" rxPackets=").append(rxPackets); |
| 171 | builder.append(" txBytes=").append(txBytes); |
| 172 | builder.append(" txPackets=").append(txPackets); |
| 173 | builder.append(" operations=").append(operations); |
| 174 | return builder.toString(); |
| 175 | } |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 176 | |
| 177 | @Override |
| 178 | public boolean equals(Object o) { |
| 179 | if (o instanceof Entry) { |
| 180 | final Entry e = (Entry) o; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 181 | return uid == e.uid && set == e.set && tag == e.tag && roaming == e.roaming |
| 182 | && rxBytes == e.rxBytes && rxPackets == e.rxPackets && txBytes == e.txBytes |
Jeff Sharkey | 9a2c2a6 | 2013-01-14 16:48:51 -0800 | [diff] [blame] | 183 | && txPackets == e.txPackets && operations == e.operations |
| 184 | && iface.equals(e.iface); |
| 185 | } |
| 186 | return false; |
| 187 | } |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 188 | } |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 189 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 190 | public NetworkStats(long elapsedRealtime, int initialSize) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 191 | this.elapsedRealtime = elapsedRealtime; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 192 | this.size = 0; |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 193 | if (initialSize >= 0) { |
| 194 | this.capacity = initialSize; |
| 195 | this.iface = new String[initialSize]; |
| 196 | this.uid = new int[initialSize]; |
| 197 | this.set = new int[initialSize]; |
| 198 | this.tag = new int[initialSize]; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 199 | this.roaming = new int[initialSize]; |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 200 | this.rxBytes = new long[initialSize]; |
| 201 | this.rxPackets = new long[initialSize]; |
| 202 | this.txBytes = new long[initialSize]; |
| 203 | this.txPackets = new long[initialSize]; |
| 204 | this.operations = new long[initialSize]; |
| 205 | } else { |
| 206 | // Special case for use by NetworkStatsFactory to start out *really* empty. |
| 207 | this.capacity = 0; |
Jeff Sharkey | 7a8f1a3 | 2014-09-17 09:26:28 -0700 | [diff] [blame] | 208 | this.iface = EmptyArray.STRING; |
| 209 | this.uid = EmptyArray.INT; |
| 210 | this.set = EmptyArray.INT; |
| 211 | this.tag = EmptyArray.INT; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 212 | this.roaming = EmptyArray.INT; |
Jeff Sharkey | 7a8f1a3 | 2014-09-17 09:26:28 -0700 | [diff] [blame] | 213 | this.rxBytes = EmptyArray.LONG; |
| 214 | this.rxPackets = EmptyArray.LONG; |
| 215 | this.txBytes = EmptyArray.LONG; |
| 216 | this.txPackets = EmptyArray.LONG; |
| 217 | this.operations = EmptyArray.LONG; |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 218 | } |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | public NetworkStats(Parcel parcel) { |
| 222 | elapsedRealtime = parcel.readLong(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 223 | size = parcel.readInt(); |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 224 | capacity = parcel.readInt(); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 225 | iface = parcel.createStringArray(); |
| 226 | uid = parcel.createIntArray(); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 227 | set = parcel.createIntArray(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 228 | tag = parcel.createIntArray(); |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 229 | roaming = parcel.createIntArray(); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 230 | rxBytes = parcel.createLongArray(); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 231 | rxPackets = parcel.createLongArray(); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 232 | txBytes = parcel.createLongArray(); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 233 | txPackets = parcel.createLongArray(); |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 234 | operations = parcel.createLongArray(); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 237 | @Override |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 238 | public void writeToParcel(Parcel dest, int flags) { |
| 239 | dest.writeLong(elapsedRealtime); |
| 240 | dest.writeInt(size); |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 241 | dest.writeInt(capacity); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 242 | dest.writeStringArray(iface); |
| 243 | dest.writeIntArray(uid); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 244 | dest.writeIntArray(set); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 245 | dest.writeIntArray(tag); |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 246 | dest.writeIntArray(roaming); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 247 | dest.writeLongArray(rxBytes); |
| 248 | dest.writeLongArray(rxPackets); |
| 249 | dest.writeLongArray(txBytes); |
| 250 | dest.writeLongArray(txPackets); |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 251 | dest.writeLongArray(operations); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Jeff Sharkey | 4abb1b8 | 2011-11-08 17:35:28 -0800 | [diff] [blame] | 254 | @Override |
| 255 | public NetworkStats clone() { |
| 256 | final NetworkStats clone = new NetworkStats(elapsedRealtime, size); |
| 257 | NetworkStats.Entry entry = null; |
| 258 | for (int i = 0; i < size; i++) { |
| 259 | entry = getValues(i, entry); |
| 260 | clone.addValues(entry); |
| 261 | } |
| 262 | return clone; |
| 263 | } |
| 264 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 265 | @VisibleForTesting |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 266 | public NetworkStats addIfaceValues( |
| 267 | String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) { |
| 268 | return addValues( |
| 269 | iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 272 | @VisibleForTesting |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 273 | public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes, |
| 274 | long rxPackets, long txBytes, long txPackets, long operations) { |
| 275 | return addValues(new Entry( |
| 276 | iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations)); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 279 | @VisibleForTesting |
| 280 | public NetworkStats addValues(String iface, int uid, int set, int tag, int roaming, |
| 281 | long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { |
| 282 | return addValues(new Entry( |
| 283 | iface, uid, set, tag, roaming, rxBytes, rxPackets, txBytes, txPackets, operations)); |
| 284 | } |
| 285 | |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 286 | /** |
| 287 | * Add new stats entry, copying from given {@link Entry}. The {@link Entry} |
| 288 | * object can be recycled across multiple calls. |
| 289 | */ |
| 290 | public NetworkStats addValues(Entry entry) { |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 291 | if (size >= capacity) { |
| 292 | final int newLength = Math.max(size, 10) * 3 / 2; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 293 | iface = Arrays.copyOf(iface, newLength); |
| 294 | uid = Arrays.copyOf(uid, newLength); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 295 | set = Arrays.copyOf(set, newLength); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 296 | tag = Arrays.copyOf(tag, newLength); |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 297 | roaming = Arrays.copyOf(roaming, newLength); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 298 | rxBytes = Arrays.copyOf(rxBytes, newLength); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 299 | rxPackets = Arrays.copyOf(rxPackets, newLength); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 300 | txBytes = Arrays.copyOf(txBytes, newLength); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 301 | txPackets = Arrays.copyOf(txPackets, newLength); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 302 | operations = Arrays.copyOf(operations, newLength); |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 303 | capacity = newLength; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 306 | iface[size] = entry.iface; |
| 307 | uid[size] = entry.uid; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 308 | set[size] = entry.set; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 309 | tag[size] = entry.tag; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 310 | roaming[size] = entry.roaming; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 311 | rxBytes[size] = entry.rxBytes; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 312 | rxPackets[size] = entry.rxPackets; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 313 | txBytes[size] = entry.txBytes; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 314 | txPackets[size] = entry.txPackets; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 315 | operations[size] = entry.operations; |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 316 | size++; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 317 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 318 | return this; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 321 | /** |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 322 | * Return specific stats entry. |
| 323 | */ |
| 324 | public Entry getValues(int i, Entry recycle) { |
| 325 | final Entry entry = recycle != null ? recycle : new Entry(); |
| 326 | entry.iface = iface[i]; |
| 327 | entry.uid = uid[i]; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 328 | entry.set = set[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 329 | entry.tag = tag[i]; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 330 | entry.roaming = roaming[i]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 331 | entry.rxBytes = rxBytes[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 332 | entry.rxPackets = rxPackets[i]; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 333 | entry.txBytes = txBytes[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 334 | entry.txPackets = txPackets[i]; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 335 | entry.operations = operations[i]; |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 336 | return entry; |
| 337 | } |
| 338 | |
| 339 | public long getElapsedRealtime() { |
| 340 | return elapsedRealtime; |
| 341 | } |
| 342 | |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 343 | public void setElapsedRealtime(long time) { |
| 344 | elapsedRealtime = time; |
| 345 | } |
| 346 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 347 | /** |
| 348 | * Return age of this {@link NetworkStats} object with respect to |
| 349 | * {@link SystemClock#elapsedRealtime()}. |
| 350 | */ |
| 351 | public long getElapsedRealtimeAge() { |
| 352 | return SystemClock.elapsedRealtime() - elapsedRealtime; |
| 353 | } |
| 354 | |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 355 | public int size() { |
| 356 | return size; |
| 357 | } |
| 358 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 359 | @VisibleForTesting |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 360 | public int internalSize() { |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 361 | return capacity; |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 364 | @Deprecated |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 365 | 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] | 366 | long txBytes, long txPackets, long operations) { |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 367 | return combineValues( |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 368 | iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, |
| 369 | txPackets, operations); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 372 | public NetworkStats combineValues(String iface, int uid, int set, int tag, |
| 373 | long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 374 | return combineValues(new Entry( |
| 375 | iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations)); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 378 | /** |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 379 | * Combine given values with an existing row, or create a new row if |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 380 | * {@link #findIndex(String, int, int, int, int)} is unable to find match. Can |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 381 | * also be used to subtract values from existing rows. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 382 | */ |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 383 | public NetworkStats combineValues(Entry entry) { |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 384 | final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag, entry.roaming); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 385 | if (i == -1) { |
| 386 | // only create new entry when positive contribution |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 387 | addValues(entry); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 388 | } else { |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 389 | rxBytes[i] += entry.rxBytes; |
| 390 | rxPackets[i] += entry.rxPackets; |
| 391 | txBytes[i] += entry.txBytes; |
| 392 | txPackets[i] += entry.txPackets; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 393 | operations[i] += entry.operations; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 394 | } |
| 395 | return this; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 398 | /** |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 399 | * Combine all values from another {@link NetworkStats} into this object. |
| 400 | */ |
| 401 | public void combineAllValues(NetworkStats another) { |
| 402 | NetworkStats.Entry entry = null; |
| 403 | for (int i = 0; i < another.size; i++) { |
| 404 | entry = another.getValues(i, entry); |
| 405 | combineValues(entry); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /** |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 410 | * Find first stats index that matches the requested parameters. |
| 411 | */ |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 412 | public int findIndex(String iface, int uid, int set, int tag, int roaming) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 413 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 414 | if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i] |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 415 | && roaming == this.roaming[i] && Objects.equals(iface, this.iface[i])) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 416 | return i; |
| 417 | } |
| 418 | } |
| 419 | return -1; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Find first stats index that matches the requested parameters, starting |
| 424 | * search around the hinted index as an optimization. |
| 425 | */ |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 426 | @VisibleForTesting |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 427 | public int findIndexHinted(String iface, int uid, int set, int tag, int roaming, |
| 428 | int hintIndex) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 429 | for (int offset = 0; offset < size; offset++) { |
| 430 | final int halfOffset = offset / 2; |
| 431 | |
| 432 | // search outwards from hint index, alternating forward and backward |
| 433 | final int i; |
| 434 | if (offset % 2 == 0) { |
| 435 | i = (hintIndex + halfOffset) % size; |
| 436 | } else { |
| 437 | i = (size + hintIndex - halfOffset - 1) % size; |
| 438 | } |
| 439 | |
| 440 | if (uid == this.uid[i] && set == this.set[i] && tag == this.tag[i] |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 441 | && roaming == this.roaming[i] && Objects.equals(iface, this.iface[i])) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 442 | return i; |
| 443 | } |
| 444 | } |
| 445 | return -1; |
| 446 | } |
| 447 | |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 448 | /** |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 449 | * Splice in {@link #operations} from the given {@link NetworkStats} based |
| 450 | * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface}, |
| 451 | * since operation counts are at data layer. |
| 452 | */ |
| 453 | public void spliceOperationsFrom(NetworkStats stats) { |
| 454 | for (int i = 0; i < size; i++) { |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 455 | final int j = stats.findIndex(iface[i], uid[i], set[i], tag[i], roaming[i]); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 456 | if (j == -1) { |
| 457 | operations[i] = 0; |
| 458 | } else { |
| 459 | operations[i] = stats.operations[j]; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /** |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 465 | * Return list of unique interfaces known by this data structure. |
| 466 | */ |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 467 | public String[] getUniqueIfaces() { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 468 | final HashSet<String> ifaces = new HashSet<String>(); |
| 469 | for (String iface : this.iface) { |
| 470 | if (iface != IFACE_ALL) { |
| 471 | ifaces.add(iface); |
| 472 | } |
| 473 | } |
| 474 | return ifaces.toArray(new String[ifaces.size()]); |
| 475 | } |
| 476 | |
| 477 | /** |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 478 | * Return list of unique UIDs known by this data structure. |
| 479 | */ |
| 480 | public int[] getUniqueUids() { |
| 481 | final SparseBooleanArray uids = new SparseBooleanArray(); |
| 482 | for (int uid : this.uid) { |
| 483 | uids.put(uid, true); |
| 484 | } |
| 485 | |
| 486 | final int size = uids.size(); |
| 487 | final int[] result = new int[size]; |
| 488 | for (int i = 0; i < size; i++) { |
| 489 | result[i] = uids.keyAt(i); |
| 490 | } |
| 491 | return result; |
| 492 | } |
| 493 | |
| 494 | /** |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 495 | * Return total bytes represented by this snapshot object, usually used when |
| 496 | * checking if a {@link #subtract(NetworkStats)} delta passes a threshold. |
| 497 | */ |
| 498 | public long getTotalBytes() { |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 499 | final Entry entry = getTotal(null); |
| 500 | return entry.rxBytes + entry.txBytes; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Return total of all fields represented by this snapshot object. |
| 505 | */ |
| 506 | public Entry getTotal(Entry recycle) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 507 | return getTotal(recycle, null, UID_ALL, false); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Return total of all fields represented by this snapshot object matching |
| 512 | * the requested {@link #uid}. |
| 513 | */ |
| 514 | public Entry getTotal(Entry recycle, int limitUid) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 515 | return getTotal(recycle, null, limitUid, false); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Return total of all fields represented by this snapshot object matching |
| 520 | * the requested {@link #iface}. |
| 521 | */ |
| 522 | public Entry getTotal(Entry recycle, HashSet<String> limitIface) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 523 | return getTotal(recycle, limitIface, UID_ALL, false); |
| 524 | } |
| 525 | |
| 526 | public Entry getTotalIncludingTags(Entry recycle) { |
| 527 | return getTotal(recycle, null, UID_ALL, true); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Return total of all fields represented by this snapshot object matching |
| 532 | * the requested {@link #iface} and {@link #uid}. |
| 533 | * |
| 534 | * @param limitIface Set of {@link #iface} to include in total; or {@code |
| 535 | * null} to include all ifaces. |
| 536 | */ |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 537 | private Entry getTotal( |
| 538 | Entry recycle, HashSet<String> limitIface, int limitUid, boolean includeTags) { |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 539 | final Entry entry = recycle != null ? recycle : new Entry(); |
| 540 | |
| 541 | entry.iface = IFACE_ALL; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 542 | entry.uid = limitUid; |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 543 | entry.set = SET_ALL; |
| 544 | entry.tag = TAG_NONE; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 545 | entry.roaming = ROAMING_ALL; |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 546 | entry.rxBytes = 0; |
| 547 | entry.rxPackets = 0; |
| 548 | entry.txBytes = 0; |
| 549 | entry.txPackets = 0; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 550 | entry.operations = 0; |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 551 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 552 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 553 | final boolean matchesUid = (limitUid == UID_ALL) || (limitUid == uid[i]); |
| 554 | final boolean matchesIface = (limitIface == null) || (limitIface.contains(iface[i])); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 555 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 556 | if (matchesUid && matchesIface) { |
| 557 | // skip specific tags, since already counted in TAG_NONE |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 558 | if (tag[i] != TAG_NONE && !includeTags) continue; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 559 | |
| 560 | entry.rxBytes += rxBytes[i]; |
| 561 | entry.rxPackets += rxPackets[i]; |
| 562 | entry.txBytes += txBytes[i]; |
| 563 | entry.txPackets += txPackets[i]; |
| 564 | entry.operations += operations[i]; |
| 565 | } |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 566 | } |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 567 | return entry; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 571 | * Fast path for battery stats. |
| 572 | */ |
| 573 | public long getTotalPackets() { |
| 574 | long total = 0; |
| 575 | for (int i = size-1; i >= 0; i--) { |
| 576 | total += rxPackets[i] + txPackets[i]; |
| 577 | } |
| 578 | return total; |
| 579 | } |
| 580 | |
| 581 | /** |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 582 | * Subtract the given {@link NetworkStats}, effectively leaving the delta |
| 583 | * between two snapshots in time. Assumes that statistics rows collect over |
| 584 | * time, and that none of them have disappeared. |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 585 | */ |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 586 | public NetworkStats subtract(NetworkStats right) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 587 | return subtract(this, right, null, null); |
Jeff Sharkey | d4ef8c8f | 2011-11-10 17:54:23 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /** |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 591 | * Subtract the two given {@link NetworkStats} objects, returning the delta |
Jeff Sharkey | d4ef8c8f | 2011-11-10 17:54:23 -0800 | [diff] [blame] | 592 | * between two snapshots in time. Assumes that statistics rows collect over |
| 593 | * time, and that none of them have disappeared. |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 594 | * <p> |
| 595 | * If counters have rolled backwards, they are clamped to {@code 0} and |
| 596 | * reported to the given {@link NonMonotonicObserver}. |
Jeff Sharkey | d4ef8c8f | 2011-11-10 17:54:23 -0800 | [diff] [blame] | 597 | */ |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 598 | public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right, |
| 599 | NonMonotonicObserver<C> observer, C cookie) { |
| 600 | return subtract(left, right, observer, cookie, null); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Subtract the two given {@link NetworkStats} objects, returning the delta |
| 605 | * between two snapshots in time. Assumes that statistics rows collect over |
| 606 | * time, and that none of them have disappeared. |
| 607 | * <p> |
| 608 | * If counters have rolled backwards, they are clamped to {@code 0} and |
| 609 | * reported to the given {@link NonMonotonicObserver}. |
| 610 | * <p> |
| 611 | * If <var>recycle</var> is supplied, this NetworkStats object will be |
| 612 | * reused (and returned) as the result if it is large enough to contain |
| 613 | * the data. |
| 614 | */ |
| 615 | public static <C> NetworkStats subtract(NetworkStats left, NetworkStats right, |
| 616 | NonMonotonicObserver<C> observer, C cookie, NetworkStats recycle) { |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 617 | long deltaRealtime = left.elapsedRealtime - right.elapsedRealtime; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 618 | if (deltaRealtime < 0) { |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 619 | if (observer != null) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 620 | observer.foundNonMonotonic(left, -1, right, -1, cookie); |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 621 | } |
| 622 | deltaRealtime = 0; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 623 | } |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 624 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 625 | // result will have our rows, and elapsed time between snapshots |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 626 | final Entry entry = new Entry(); |
Dianne Hackborn | d0c5b9a | 2014-02-21 16:19:05 -0800 | [diff] [blame] | 627 | final NetworkStats result; |
| 628 | if (recycle != null && recycle.capacity >= left.size) { |
| 629 | result = recycle; |
| 630 | result.size = 0; |
| 631 | result.elapsedRealtime = deltaRealtime; |
| 632 | } else { |
| 633 | result = new NetworkStats(deltaRealtime, left.size); |
| 634 | } |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 635 | for (int i = 0; i < left.size; i++) { |
| 636 | entry.iface = left.iface[i]; |
| 637 | entry.uid = left.uid[i]; |
| 638 | entry.set = left.set[i]; |
| 639 | entry.tag = left.tag[i]; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 640 | entry.roaming = left.roaming[i]; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 641 | |
| 642 | // find remote row that matches, and subtract |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 643 | final int j = right.findIndexHinted(entry.iface, entry.uid, entry.set, entry.tag, |
| 644 | entry.roaming, i); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 645 | if (j == -1) { |
| 646 | // newly appearing row, return entire value |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 647 | entry.rxBytes = left.rxBytes[i]; |
| 648 | entry.rxPackets = left.rxPackets[i]; |
| 649 | entry.txBytes = left.txBytes[i]; |
| 650 | entry.txPackets = left.txPackets[i]; |
| 651 | entry.operations = left.operations[i]; |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 652 | } else { |
| 653 | // existing row, subtract remote value |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 654 | entry.rxBytes = left.rxBytes[i] - right.rxBytes[j]; |
| 655 | entry.rxPackets = left.rxPackets[i] - right.rxPackets[j]; |
| 656 | entry.txBytes = left.txBytes[i] - right.txBytes[j]; |
| 657 | entry.txPackets = left.txPackets[i] - right.txPackets[j]; |
| 658 | entry.operations = left.operations[i] - right.operations[j]; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 659 | |
| 660 | if (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0 |
| 661 | || entry.txPackets < 0 || entry.operations < 0) { |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 662 | if (observer != null) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 663 | observer.foundNonMonotonic(left, i, right, j, cookie); |
Jeff Sharkey | d4ef8c8f | 2011-11-10 17:54:23 -0800 | [diff] [blame] | 664 | } |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 665 | entry.rxBytes = Math.max(entry.rxBytes, 0); |
| 666 | entry.rxPackets = Math.max(entry.rxPackets, 0); |
| 667 | entry.txBytes = Math.max(entry.txBytes, 0); |
| 668 | entry.txPackets = Math.max(entry.txPackets, 0); |
| 669 | entry.operations = Math.max(entry.operations, 0); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 670 | } |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 671 | } |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 672 | |
| 673 | result.addValues(entry); |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 676 | return result; |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 679 | /** |
| 680 | * Return total statistics grouped by {@link #iface}; doesn't mutate the |
| 681 | * original structure. |
| 682 | */ |
| 683 | public NetworkStats groupedByIface() { |
| 684 | final NetworkStats stats = new NetworkStats(elapsedRealtime, 10); |
| 685 | |
| 686 | final Entry entry = new Entry(); |
| 687 | entry.uid = UID_ALL; |
| 688 | entry.set = SET_ALL; |
| 689 | entry.tag = TAG_NONE; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 690 | entry.roaming = ROAMING_ALL; |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 691 | entry.operations = 0L; |
| 692 | |
| 693 | for (int i = 0; i < size; i++) { |
| 694 | // skip specific tags, since already counted in TAG_NONE |
| 695 | if (tag[i] != TAG_NONE) continue; |
| 696 | |
| 697 | entry.iface = iface[i]; |
| 698 | entry.rxBytes = rxBytes[i]; |
| 699 | entry.rxPackets = rxPackets[i]; |
| 700 | entry.txBytes = txBytes[i]; |
| 701 | entry.txPackets = txPackets[i]; |
| 702 | stats.combineValues(entry); |
| 703 | } |
| 704 | |
| 705 | return stats; |
| 706 | } |
| 707 | |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 708 | /** |
| 709 | * Return total statistics grouped by {@link #uid}; doesn't mutate the |
| 710 | * original structure. |
| 711 | */ |
| 712 | public NetworkStats groupedByUid() { |
| 713 | final NetworkStats stats = new NetworkStats(elapsedRealtime, 10); |
| 714 | |
| 715 | final Entry entry = new Entry(); |
| 716 | entry.iface = IFACE_ALL; |
| 717 | entry.set = SET_ALL; |
| 718 | entry.tag = TAG_NONE; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 719 | entry.roaming = ROAMING_ALL; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 720 | |
| 721 | for (int i = 0; i < size; i++) { |
| 722 | // skip specific tags, since already counted in TAG_NONE |
| 723 | if (tag[i] != TAG_NONE) continue; |
| 724 | |
| 725 | entry.uid = uid[i]; |
| 726 | entry.rxBytes = rxBytes[i]; |
| 727 | entry.rxPackets = rxPackets[i]; |
| 728 | entry.txBytes = txBytes[i]; |
| 729 | entry.txPackets = txPackets[i]; |
| 730 | entry.operations = operations[i]; |
| 731 | stats.combineValues(entry); |
| 732 | } |
| 733 | |
| 734 | return stats; |
| 735 | } |
| 736 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 737 | /** |
| 738 | * Return all rows except those attributed to the requested UID; doesn't |
| 739 | * mutate the original structure. |
| 740 | */ |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 741 | public NetworkStats withoutUids(int[] uids) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 742 | final NetworkStats stats = new NetworkStats(elapsedRealtime, 10); |
| 743 | |
| 744 | Entry entry = new Entry(); |
| 745 | for (int i = 0; i < size; i++) { |
| 746 | entry = getValues(i, entry); |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 747 | if (!ArrayUtils.contains(uids, entry.uid)) { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 748 | stats.addValues(entry); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | return stats; |
| 753 | } |
| 754 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 755 | public void dump(String prefix, PrintWriter pw) { |
| 756 | pw.print(prefix); |
| 757 | pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 758 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 759 | pw.print(prefix); |
Jeff Sharkey | 3359aca | 2011-11-08 18:08:48 -0800 | [diff] [blame] | 760 | pw.print(" ["); pw.print(i); pw.print("]"); |
| 761 | pw.print(" iface="); pw.print(iface[i]); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 762 | pw.print(" uid="); pw.print(uid[i]); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 763 | pw.print(" set="); pw.print(setToString(set[i])); |
| 764 | pw.print(" tag="); pw.print(tagToString(tag[i])); |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 765 | pw.print(" roaming="); pw.print(roamingToString(roaming[i])); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 766 | pw.print(" rxBytes="); pw.print(rxBytes[i]); |
Jeff Sharkey | fd8be3e | 2011-07-11 14:36:15 -0700 | [diff] [blame] | 767 | pw.print(" rxPackets="); pw.print(rxPackets[i]); |
Jeff Sharkey | d37948f | 2011-07-12 13:57:00 -0700 | [diff] [blame] | 768 | pw.print(" txBytes="); pw.print(txBytes[i]); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 769 | pw.print(" txPackets="); pw.print(txPackets[i]); |
| 770 | pw.print(" operations="); pw.println(operations[i]); |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 774 | /** |
| 775 | * Return text description of {@link #set} value. |
| 776 | */ |
| 777 | public static String setToString(int set) { |
| 778 | switch (set) { |
| 779 | case SET_ALL: |
| 780 | return "ALL"; |
| 781 | case SET_DEFAULT: |
| 782 | return "DEFAULT"; |
| 783 | case SET_FOREGROUND: |
| 784 | return "FOREGROUND"; |
Wenchao Tong | 98170b0 | 2015-03-17 16:14:23 -0700 | [diff] [blame] | 785 | case SET_DBG_VPN_IN: |
| 786 | return "DBG_VPN_IN"; |
| 787 | case SET_DBG_VPN_OUT: |
| 788 | return "DBG_VPN_OUT"; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 789 | default: |
| 790 | return "UNKNOWN"; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | /** |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 795 | * Return text description of {@link #set} value. |
| 796 | */ |
| 797 | public static String setToCheckinString(int set) { |
| 798 | switch (set) { |
| 799 | case SET_ALL: |
| 800 | return "all"; |
| 801 | case SET_DEFAULT: |
| 802 | return "def"; |
| 803 | case SET_FOREGROUND: |
| 804 | return "fg"; |
Wenchao Tong | 98170b0 | 2015-03-17 16:14:23 -0700 | [diff] [blame] | 805 | case SET_DBG_VPN_IN: |
| 806 | return "vpnin"; |
| 807 | case SET_DBG_VPN_OUT: |
| 808 | return "vpnout"; |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 809 | default: |
| 810 | return "unk"; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /** |
Wenchao Tong | 98170b0 | 2015-03-17 16:14:23 -0700 | [diff] [blame] | 815 | * @return true if the querySet matches the dataSet. |
| 816 | */ |
| 817 | public static boolean setMatches(int querySet, int dataSet) { |
| 818 | if (querySet == dataSet) { |
| 819 | return true; |
| 820 | } |
| 821 | // SET_ALL matches all non-debugging sets. |
| 822 | return querySet == SET_ALL && dataSet < SET_DEBUG_START; |
| 823 | } |
| 824 | |
| 825 | /** |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 826 | * Return text description of {@link #tag} value. |
| 827 | */ |
| 828 | public static String tagToString(int tag) { |
| 829 | return "0x" + Integer.toHexString(tag); |
| 830 | } |
| 831 | |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 832 | /** |
| 833 | * Return text description of {@link #roaming} value. |
| 834 | */ |
| 835 | public static String roamingToString(int roaming) { |
| 836 | switch (roaming) { |
| 837 | case ROAMING_ALL: |
| 838 | return "ALL"; |
Jeff Davidson | 1f7e05e | 2016-03-10 13:21:38 -0800 | [diff] [blame] | 839 | case ROAMING_NO: |
| 840 | return "NO"; |
| 841 | case ROAMING_YES: |
| 842 | return "YES"; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 843 | default: |
| 844 | return "UNKNOWN"; |
| 845 | } |
| 846 | } |
| 847 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 848 | @Override |
| 849 | public String toString() { |
| 850 | final CharArrayWriter writer = new CharArrayWriter(); |
| 851 | dump("", new PrintWriter(writer)); |
| 852 | return writer.toString(); |
| 853 | } |
| 854 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 855 | @Override |
Jeff Sharkey | eedcb95 | 2011-05-17 14:55:15 -0700 | [diff] [blame] | 856 | public int describeContents() { |
| 857 | return 0; |
| 858 | } |
| 859 | |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 860 | public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() { |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 861 | @Override |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 862 | public NetworkStats createFromParcel(Parcel in) { |
| 863 | return new NetworkStats(in); |
| 864 | } |
| 865 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 866 | @Override |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 867 | public NetworkStats[] newArray(int size) { |
| 868 | return new NetworkStats[size]; |
| 869 | } |
| 870 | }; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 871 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 872 | public interface NonMonotonicObserver<C> { |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 873 | public void foundNonMonotonic( |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 874 | NetworkStats left, int leftIndex, NetworkStats right, int rightIndex, C cookie); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 875 | } |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 876 | |
| 877 | /** |
| 878 | * VPN accounting. Move some VPN's underlying traffic to other UIDs that use tun0 iface. |
| 879 | * |
| 880 | * This method should only be called on delta NetworkStats. Do not call this method on a |
| 881 | * snapshot {@link NetworkStats} object because the tunUid and/or the underlyingIface may |
| 882 | * change over time. |
| 883 | * |
| 884 | * This method performs adjustments for one active VPN package and one VPN iface at a time. |
| 885 | * |
| 886 | * It is possible for the VPN software to use multiple underlying networks. This method |
| 887 | * only migrates traffic for the primary underlying network. |
| 888 | * |
| 889 | * @param tunUid uid of the VPN application |
| 890 | * @param tunIface iface of the vpn tunnel |
| 891 | * @param underlyingIface the primary underlying network iface used by the VPN application |
| 892 | * @return true if it successfully adjusts the accounting for VPN, false otherwise |
| 893 | */ |
| 894 | public boolean migrateTun(int tunUid, String tunIface, String underlyingIface) { |
| 895 | Entry tunIfaceTotal = new Entry(); |
| 896 | Entry underlyingIfaceTotal = new Entry(); |
| 897 | |
| 898 | tunAdjustmentInit(tunUid, tunIface, underlyingIface, tunIfaceTotal, underlyingIfaceTotal); |
| 899 | |
| 900 | // If tunIface < underlyingIface, it leaves the overhead traffic in the VPN app. |
| 901 | // If tunIface > underlyingIface, the VPN app doesn't get credit for data compression. |
| 902 | // Negative stats should be avoided. |
| 903 | Entry pool = tunGetPool(tunIfaceTotal, underlyingIfaceTotal); |
| 904 | if (pool.isEmpty()) { |
| 905 | return true; |
| 906 | } |
Jeremy Joslin | 3f0d75a | 2016-08-08 16:07:37 -0700 | [diff] [blame] | 907 | Entry moved = |
| 908 | addTrafficToApplications(tunUid, tunIface, underlyingIface, tunIfaceTotal, pool); |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 909 | deductTrafficFromVpnApp(tunUid, underlyingIface, moved); |
| 910 | |
| 911 | if (!moved.isEmpty()) { |
| 912 | Slog.wtf(TAG, "Failed to deduct underlying network traffic from VPN package. Moved=" |
| 913 | + moved); |
| 914 | return false; |
| 915 | } |
| 916 | return true; |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * Initializes the data used by the migrateTun() method. |
| 921 | * |
| 922 | * This is the first pass iteration which does the following work: |
Jeremy Joslin | 3f0d75a | 2016-08-08 16:07:37 -0700 | [diff] [blame] | 923 | * (1) Adds up all the traffic through the tunUid's underlyingIface |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 924 | * (both foreground and background). |
Jeremy Joslin | 3f0d75a | 2016-08-08 16:07:37 -0700 | [diff] [blame] | 925 | * (2) Adds up all the traffic through tun0 excluding traffic from the vpn app itself. |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 926 | */ |
| 927 | private void tunAdjustmentInit(int tunUid, String tunIface, String underlyingIface, |
| 928 | Entry tunIfaceTotal, Entry underlyingIfaceTotal) { |
| 929 | Entry recycle = new Entry(); |
| 930 | for (int i = 0; i < size; i++) { |
| 931 | getValues(i, recycle); |
| 932 | if (recycle.uid == UID_ALL) { |
| 933 | throw new IllegalStateException( |
| 934 | "Cannot adjust VPN accounting on an iface aggregated NetworkStats."); |
Wenchao Tong | 98170b0 | 2015-03-17 16:14:23 -0700 | [diff] [blame] | 935 | } if (recycle.set == SET_DBG_VPN_IN || recycle.set == SET_DBG_VPN_OUT) { |
| 936 | throw new IllegalStateException( |
| 937 | "Cannot adjust VPN accounting on a NetworkStats containing SET_DBG_VPN_*"); |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | if (recycle.uid == tunUid && recycle.tag == TAG_NONE |
| 941 | && Objects.equals(underlyingIface, recycle.iface)) { |
| 942 | underlyingIfaceTotal.add(recycle); |
| 943 | } |
| 944 | |
Jeremy Joslin | 3f0d75a | 2016-08-08 16:07:37 -0700 | [diff] [blame] | 945 | if (recycle.uid != tunUid && recycle.tag == TAG_NONE |
| 946 | && Objects.equals(tunIface, recycle.iface)) { |
| 947 | // Add up all tunIface traffic excluding traffic from the vpn app itself. |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 948 | tunIfaceTotal.add(recycle); |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | private static Entry tunGetPool(Entry tunIfaceTotal, Entry underlyingIfaceTotal) { |
| 954 | Entry pool = new Entry(); |
| 955 | pool.rxBytes = Math.min(tunIfaceTotal.rxBytes, underlyingIfaceTotal.rxBytes); |
| 956 | pool.rxPackets = Math.min(tunIfaceTotal.rxPackets, underlyingIfaceTotal.rxPackets); |
| 957 | pool.txBytes = Math.min(tunIfaceTotal.txBytes, underlyingIfaceTotal.txBytes); |
| 958 | pool.txPackets = Math.min(tunIfaceTotal.txPackets, underlyingIfaceTotal.txPackets); |
| 959 | pool.operations = Math.min(tunIfaceTotal.operations, underlyingIfaceTotal.operations); |
| 960 | return pool; |
| 961 | } |
| 962 | |
Jeremy Joslin | 3f0d75a | 2016-08-08 16:07:37 -0700 | [diff] [blame] | 963 | private Entry addTrafficToApplications(int tunUid, String tunIface, String underlyingIface, |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 964 | Entry tunIfaceTotal, Entry pool) { |
| 965 | Entry moved = new Entry(); |
| 966 | Entry tmpEntry = new Entry(); |
| 967 | tmpEntry.iface = underlyingIface; |
| 968 | for (int i = 0; i < size; i++) { |
Jeremy Joslin | 3f0d75a | 2016-08-08 16:07:37 -0700 | [diff] [blame] | 969 | // the vpn app is excluded from the redistribution but all moved traffic will be |
| 970 | // deducted from the vpn app (see deductTrafficFromVpnApp below). |
| 971 | if (Objects.equals(iface[i], tunIface) && uid[i] != tunUid) { |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 972 | if (tunIfaceTotal.rxBytes > 0) { |
| 973 | tmpEntry.rxBytes = pool.rxBytes * rxBytes[i] / tunIfaceTotal.rxBytes; |
| 974 | } else { |
| 975 | tmpEntry.rxBytes = 0; |
| 976 | } |
| 977 | if (tunIfaceTotal.rxPackets > 0) { |
| 978 | tmpEntry.rxPackets = pool.rxPackets * rxPackets[i] / tunIfaceTotal.rxPackets; |
| 979 | } else { |
| 980 | tmpEntry.rxPackets = 0; |
| 981 | } |
| 982 | if (tunIfaceTotal.txBytes > 0) { |
| 983 | tmpEntry.txBytes = pool.txBytes * txBytes[i] / tunIfaceTotal.txBytes; |
| 984 | } else { |
| 985 | tmpEntry.txBytes = 0; |
| 986 | } |
| 987 | if (tunIfaceTotal.txPackets > 0) { |
| 988 | tmpEntry.txPackets = pool.txPackets * txPackets[i] / tunIfaceTotal.txPackets; |
| 989 | } else { |
| 990 | tmpEntry.txPackets = 0; |
| 991 | } |
| 992 | if (tunIfaceTotal.operations > 0) { |
| 993 | tmpEntry.operations = |
| 994 | pool.operations * operations[i] / tunIfaceTotal.operations; |
| 995 | } else { |
| 996 | tmpEntry.operations = 0; |
| 997 | } |
| 998 | tmpEntry.uid = uid[i]; |
| 999 | tmpEntry.tag = tag[i]; |
| 1000 | tmpEntry.set = set[i]; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 1001 | tmpEntry.roaming = roaming[i]; |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 1002 | combineValues(tmpEntry); |
| 1003 | if (tag[i] == TAG_NONE) { |
| 1004 | moved.add(tmpEntry); |
Wenchao Tong | 98170b0 | 2015-03-17 16:14:23 -0700 | [diff] [blame] | 1005 | // Add debug info |
| 1006 | tmpEntry.set = SET_DBG_VPN_IN; |
| 1007 | combineValues(tmpEntry); |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | return moved; |
| 1012 | } |
| 1013 | |
| 1014 | private void deductTrafficFromVpnApp(int tunUid, String underlyingIface, Entry moved) { |
Wenchao Tong | 98170b0 | 2015-03-17 16:14:23 -0700 | [diff] [blame] | 1015 | // Add debug info |
| 1016 | moved.uid = tunUid; |
| 1017 | moved.set = SET_DBG_VPN_OUT; |
| 1018 | moved.tag = TAG_NONE; |
| 1019 | moved.iface = underlyingIface; |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 1020 | moved.roaming = ROAMING_ALL; |
Wenchao Tong | 98170b0 | 2015-03-17 16:14:23 -0700 | [diff] [blame] | 1021 | combineValues(moved); |
| 1022 | |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 1023 | // Caveat: if the vpn software uses tag, the total tagged traffic may be greater than |
| 1024 | // the TAG_NONE traffic. |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 1025 | // |
Jeff Davidson | 1f7e05e | 2016-03-10 13:21:38 -0800 | [diff] [blame] | 1026 | // Relies on the fact that the underlying traffic only has state ROAMING_NO, which |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 1027 | // should be the case as it comes directly from the /proc file. We only blend in the |
| 1028 | // roaming data after applying these adjustments, by checking the NetworkIdentity of the |
| 1029 | // underlying iface. |
| 1030 | int idxVpnBackground = findIndex(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, |
Jeff Davidson | 1f7e05e | 2016-03-10 13:21:38 -0800 | [diff] [blame] | 1031 | ROAMING_NO); |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 1032 | if (idxVpnBackground != -1) { |
| 1033 | tunSubtract(idxVpnBackground, this, moved); |
| 1034 | } |
| 1035 | |
Jeff Davidson | a6a7807 | 2016-01-11 16:02:17 -0800 | [diff] [blame] | 1036 | int idxVpnForeground = findIndex(underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE, |
Jeff Davidson | 1f7e05e | 2016-03-10 13:21:38 -0800 | [diff] [blame] | 1037 | ROAMING_NO); |
Wenchao Tong | 21377c3 | 2015-02-26 18:13:07 -0800 | [diff] [blame] | 1038 | if (idxVpnForeground != -1) { |
| 1039 | tunSubtract(idxVpnForeground, this, moved); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | private static void tunSubtract(int i, NetworkStats left, Entry right) { |
| 1044 | long rxBytes = Math.min(left.rxBytes[i], right.rxBytes); |
| 1045 | left.rxBytes[i] -= rxBytes; |
| 1046 | right.rxBytes -= rxBytes; |
| 1047 | |
| 1048 | long rxPackets = Math.min(left.rxPackets[i], right.rxPackets); |
| 1049 | left.rxPackets[i] -= rxPackets; |
| 1050 | right.rxPackets -= rxPackets; |
| 1051 | |
| 1052 | long txBytes = Math.min(left.txBytes[i], right.txBytes); |
| 1053 | left.txBytes[i] -= txBytes; |
| 1054 | right.txBytes -= txBytes; |
| 1055 | |
| 1056 | long txPackets = Math.min(left.txPackets[i], right.txPackets); |
| 1057 | left.txPackets[i] -= txPackets; |
| 1058 | right.txPackets -= txPackets; |
| 1059 | } |
Jeff Sharkey | 9a13f36 | 2011-04-26 16:25:36 -0700 | [diff] [blame] | 1060 | } |