blob: 5b883a0741d6de90c330326f4a5015ef7401ca76 [file] [log] [blame]
Jeff Sharkey9a13f362011-04-26 16:25:36 -07001/*
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
17package android.net;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.os.SystemClock;
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -070022import android.util.SparseBooleanArray;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070023
Jeff Sharkeya63ba592011-07-19 23:47:12 -070024import com.android.internal.util.Objects;
25
Jeff Sharkey9a13f362011-04-26 16:25:36 -070026import java.io.CharArrayWriter;
27import java.io.PrintWriter;
Jeff Sharkey4a971222011-06-11 22:16:55 -070028import java.util.Arrays;
Jeff Sharkey75279902011-05-24 18:39:45 -070029import java.util.HashSet;
Jeff Sharkey9a13f362011-04-26 16:25:36 -070030
31/**
Jeff Sharkey75279902011-05-24 18:39:45 -070032 * Collection of active network statistics. Can contain summary details across
33 * all interfaces, or details with per-UID granularity. Internally stores data
34 * as a large table, closely matching {@code /proc/} data format. This structure
35 * optimizes for rapid in-memory comparison, but consider using
36 * {@link NetworkStatsHistory} when persisting.
Jeff Sharkey9a13f362011-04-26 16:25:36 -070037 *
38 * @hide
39 */
40public class NetworkStats implements Parcelable {
Jeff Sharkey75279902011-05-24 18:39:45 -070041 /** {@link #iface} value when interface details unavailable. */
Jeff Sharkey9a13f362011-04-26 16:25:36 -070042 public static final String IFACE_ALL = null;
Jeff Sharkey75279902011-05-24 18:39:45 -070043 /** {@link #uid} value when UID details unavailable. */
44 public static final int UID_ALL = -1;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070045 /** {@link #set} value when all sets combined. */
46 public static final int SET_ALL = -1;
47 /** {@link #set} value where background data is accounted. */
48 public static final int SET_DEFAULT = 0;
49 /** {@link #set} value where foreground data is accounted. */
50 public static final int SET_FOREGROUND = 1;
51 /** {@link #tag} value for total data across all tags. */
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070052 public static final int TAG_NONE = 0;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -070053
Jeff Sharkey9a13f362011-04-26 16:25:36 -070054 /**
55 * {@link SystemClock#elapsedRealtime()} timestamp when this data was
56 * generated.
57 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070058 private final long elapsedRealtime;
59 private int size;
60 private String[] iface;
61 private int[] uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070062 private int[] set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070063 private int[] tag;
64 private long[] rxBytes;
65 private long[] rxPackets;
66 private long[] txBytes;
67 private long[] txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070068 private long[] operations;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070069
70 public static class Entry {
71 public String iface;
72 public int uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070073 public int set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070074 public int tag;
75 public long rxBytes;
76 public long rxPackets;
77 public long txBytes;
78 public long txPackets;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070079 public long operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070080
81 public Entry() {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070082 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0L);
Jeff Sharkey63d27a92011-08-03 17:04:22 -070083 }
84
85 public Entry(long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070086 this(IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
87 operations);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070088 }
89
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070090 public Entry(String iface, int uid, int set, int tag, long rxBytes, long rxPackets,
91 long txBytes, long txPackets, long operations) {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070092 this.iface = iface;
93 this.uid = uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070094 this.set = set;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -070095 this.tag = tag;
96 this.rxBytes = rxBytes;
97 this.rxPackets = rxPackets;
98 this.txBytes = txBytes;
99 this.txPackets = txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700100 this.operations = operations;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700101 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700102 }
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700103
Jeff Sharkey4a971222011-06-11 22:16:55 -0700104 public NetworkStats(long elapsedRealtime, int initialSize) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700105 this.elapsedRealtime = elapsedRealtime;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700106 this.size = 0;
107 this.iface = new String[initialSize];
108 this.uid = new int[initialSize];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700109 this.set = new int[initialSize];
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700110 this.tag = new int[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700111 this.rxBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700112 this.rxPackets = new long[initialSize];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700113 this.txBytes = new long[initialSize];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700114 this.txPackets = new long[initialSize];
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700115 this.operations = new long[initialSize];
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700116 }
117
118 public NetworkStats(Parcel parcel) {
119 elapsedRealtime = parcel.readLong();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700120 size = parcel.readInt();
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700121 iface = parcel.createStringArray();
122 uid = parcel.createIntArray();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700123 set = parcel.createIntArray();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700124 tag = parcel.createIntArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700125 rxBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700126 rxPackets = parcel.createLongArray();
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700127 txBytes = parcel.createLongArray();
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700128 txPackets = parcel.createLongArray();
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700129 operations = parcel.createLongArray();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700130 }
131
132 /** {@inheritDoc} */
133 public void writeToParcel(Parcel dest, int flags) {
134 dest.writeLong(elapsedRealtime);
135 dest.writeInt(size);
136 dest.writeStringArray(iface);
137 dest.writeIntArray(uid);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700138 dest.writeIntArray(set);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700139 dest.writeIntArray(tag);
140 dest.writeLongArray(rxBytes);
141 dest.writeLongArray(rxPackets);
142 dest.writeLongArray(txBytes);
143 dest.writeLongArray(txPackets);
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700144 dest.writeLongArray(operations);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700145 }
146
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700147 // @VisibleForTesting
148 public NetworkStats addIfaceValues(
149 String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {
150 return addValues(
151 iface, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 0L);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700152 }
153
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700154 // @VisibleForTesting
155 public NetworkStats addValues(String iface, int uid, int set, int tag, long rxBytes,
156 long rxPackets, long txBytes, long txPackets, long operations) {
157 return addValues(new Entry(
158 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700159 }
160
161 /**
162 * Add new stats entry, copying from given {@link Entry}. The {@link Entry}
163 * object can be recycled across multiple calls.
164 */
165 public NetworkStats addValues(Entry entry) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700166 if (size >= this.iface.length) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700167 final int newLength = Math.max(iface.length, 10) * 3 / 2;
168 iface = Arrays.copyOf(iface, newLength);
169 uid = Arrays.copyOf(uid, newLength);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700170 set = Arrays.copyOf(set, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700171 tag = Arrays.copyOf(tag, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700172 rxBytes = Arrays.copyOf(rxBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700173 rxPackets = Arrays.copyOf(rxPackets, newLength);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700174 txBytes = Arrays.copyOf(txBytes, newLength);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700175 txPackets = Arrays.copyOf(txPackets, newLength);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700176 operations = Arrays.copyOf(operations, newLength);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700177 }
178
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700179 iface[size] = entry.iface;
180 uid[size] = entry.uid;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700181 set[size] = entry.set;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700182 tag[size] = entry.tag;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700183 rxBytes[size] = entry.rxBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700184 rxPackets[size] = entry.rxPackets;
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700185 txBytes[size] = entry.txBytes;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700186 txPackets[size] = entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700187 operations[size] = entry.operations;
Jeff Sharkey4a971222011-06-11 22:16:55 -0700188 size++;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700189
Jeff Sharkey4a971222011-06-11 22:16:55 -0700190 return this;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700191 }
192
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700193 /**
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700194 * Return specific stats entry.
195 */
196 public Entry getValues(int i, Entry recycle) {
197 final Entry entry = recycle != null ? recycle : new Entry();
198 entry.iface = iface[i];
199 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700200 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700201 entry.tag = tag[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700202 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700203 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700204 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700205 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700206 entry.operations = operations[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700207 return entry;
208 }
209
210 public long getElapsedRealtime() {
211 return elapsedRealtime;
212 }
213
214 public int size() {
215 return size;
216 }
217
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700218 // @VisibleForTesting
219 public int internalSize() {
220 return iface.length;
221 }
222
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700223 @Deprecated
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700224 public NetworkStats combineValues(String iface, int uid, int tag, long rxBytes, long rxPackets,
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700225 long txBytes, long txPackets, long operations) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700226 return combineValues(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700227 iface, uid, SET_DEFAULT, tag, rxBytes, rxPackets, txBytes, txPackets, operations);
228 }
229
230 public NetworkStats combineValues(String iface, int uid, int set, int tag, long rxBytes,
231 long rxPackets, long txBytes, long txPackets, long operations) {
232 return combineValues(new Entry(
233 iface, uid, set, tag, rxBytes, rxPackets, txBytes, txPackets, operations));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700234 }
235
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700236 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700237 * Combine given values with an existing row, or create a new row if
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700238 * {@link #findIndex(String, int, int, int)} is unable to find match. Can
239 * also be used to subtract values from existing rows.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700240 */
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700241 public NetworkStats combineValues(Entry entry) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700242 final int i = findIndex(entry.iface, entry.uid, entry.set, entry.tag);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700243 if (i == -1) {
244 // only create new entry when positive contribution
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700245 addValues(entry);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700246 } else {
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700247 rxBytes[i] += entry.rxBytes;
248 rxPackets[i] += entry.rxPackets;
249 txBytes[i] += entry.txBytes;
250 txPackets[i] += entry.txPackets;
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700251 operations[i] += entry.operations;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700252 }
253 return this;
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700254 }
255
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700256 /**
257 * Find first stats index that matches the requested parameters.
258 */
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700259 public int findIndex(String iface, int uid, int set, int tag) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700260 for (int i = 0; i < size; i++) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700261 if (Objects.equal(iface, this.iface[i]) && uid == this.uid[i] && set == this.set[i]
262 && tag == this.tag[i]) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700263 return i;
264 }
265 }
266 return -1;
267 }
268
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700269 /**
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700270 * Splice in {@link #operations} from the given {@link NetworkStats} based
271 * on matching {@link #uid} and {@link #tag} rows. Ignores {@link #iface},
272 * since operation counts are at data layer.
273 */
274 public void spliceOperationsFrom(NetworkStats stats) {
275 for (int i = 0; i < size; i++) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700276 final int j = stats.findIndex(IFACE_ALL, uid[i], set[i], tag[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700277 if (j == -1) {
278 operations[i] = 0;
279 } else {
280 operations[i] = stats.operations[j];
281 }
282 }
283 }
284
285 /**
Jeff Sharkey75279902011-05-24 18:39:45 -0700286 * Return list of unique interfaces known by this data structure.
287 */
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700288 public String[] getUniqueIfaces() {
Jeff Sharkey75279902011-05-24 18:39:45 -0700289 final HashSet<String> ifaces = new HashSet<String>();
290 for (String iface : this.iface) {
291 if (iface != IFACE_ALL) {
292 ifaces.add(iface);
293 }
294 }
295 return ifaces.toArray(new String[ifaces.size()]);
296 }
297
298 /**
Jeff Sharkey61ee0bb2011-05-29 22:50:42 -0700299 * Return list of unique UIDs known by this data structure.
300 */
301 public int[] getUniqueUids() {
302 final SparseBooleanArray uids = new SparseBooleanArray();
303 for (int uid : this.uid) {
304 uids.put(uid, true);
305 }
306
307 final int size = uids.size();
308 final int[] result = new int[size];
309 for (int i = 0; i < size; i++) {
310 result[i] = uids.keyAt(i);
311 }
312 return result;
313 }
314
315 /**
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700316 * Return total bytes represented by this snapshot object, usually used when
317 * checking if a {@link #subtract(NetworkStats)} delta passes a threshold.
318 */
319 public long getTotalBytes() {
320 long totalBytes = 0;
321 for (int i = 0; i < size; i++) {
322 // skip specific tags, since already counted in TAG_NONE
323 if (tag[i] != TAG_NONE) continue;
324
325 totalBytes += rxBytes[i];
326 totalBytes += txBytes[i];
327 }
328 return totalBytes;
329 }
330
331 /**
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700332 * Subtract the given {@link NetworkStats}, effectively leaving the delta
333 * between two snapshots in time. Assumes that statistics rows collect over
334 * time, and that none of them have disappeared.
Jeff Sharkey75279902011-05-24 18:39:45 -0700335 *
Jeff Sharkey3f391352011-06-05 17:42:53 -0700336 * @throws IllegalArgumentException when given {@link NetworkStats} is
337 * non-monotonic.
338 */
339 public NetworkStats subtract(NetworkStats value) {
340 return subtract(value, true, false);
341 }
342
343 /**
344 * Subtract the given {@link NetworkStats}, effectively leaving the delta
345 * between two snapshots in time. Assumes that statistics rows collect over
346 * time, and that none of them have disappeared.
347 * <p>
348 * Instead of throwing when counters are non-monotonic, this variant clamps
349 * results to never be negative.
350 */
351 public NetworkStats subtractClamped(NetworkStats value) {
352 return subtract(value, false, true);
353 }
354
355 /**
356 * Subtract the given {@link NetworkStats}, effectively leaving the delta
357 * between two snapshots in time. Assumes that statistics rows collect over
358 * time, and that none of them have disappeared.
359 *
Jeff Sharkey75279902011-05-24 18:39:45 -0700360 * @param enforceMonotonic Validate that incoming value is strictly
361 * monotonic compared to this object.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700362 * @param clampNegative Instead of throwing like {@code enforceMonotonic},
363 * clamp resulting counters at 0 to prevent negative values.
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700364 */
Jeff Sharkey3f391352011-06-05 17:42:53 -0700365 private NetworkStats subtract(
366 NetworkStats value, boolean enforceMonotonic, boolean clampNegative) {
Jeff Sharkey75279902011-05-24 18:39:45 -0700367 final long deltaRealtime = this.elapsedRealtime - value.elapsedRealtime;
368 if (enforceMonotonic && deltaRealtime < 0) {
369 throw new IllegalArgumentException("found non-monotonic realtime");
370 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700371
Jeff Sharkey75279902011-05-24 18:39:45 -0700372 // result will have our rows, and elapsed time between snapshots
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700373 final Entry entry = new Entry();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700374 final NetworkStats result = new NetworkStats(deltaRealtime, size);
375 for (int i = 0; i < size; i++) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700376 entry.iface = iface[i];
377 entry.uid = uid[i];
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700378 entry.set = set[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700379 entry.tag = tag[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700380
381 // find remote row that matches, and subtract
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700382 final int j = value.findIndex(entry.iface, entry.uid, entry.set, entry.tag);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700383 if (j == -1) {
384 // newly appearing row, return entire value
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700385 entry.rxBytes = rxBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700386 entry.rxPackets = rxPackets[i];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700387 entry.txBytes = txBytes[i];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700388 entry.txPackets = txPackets[i];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700389 entry.operations = operations[i];
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700390 } else {
391 // existing row, subtract remote value
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700392 entry.rxBytes = rxBytes[i] - value.rxBytes[j];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700393 entry.rxPackets = rxPackets[i] - value.rxPackets[j];
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700394 entry.txBytes = txBytes[i] - value.txBytes[j];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700395 entry.txPackets = txPackets[i] - value.txPackets[j];
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700396 entry.operations = operations[i] - value.operations[j];
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700397 if (enforceMonotonic
398 && (entry.rxBytes < 0 || entry.rxPackets < 0 || entry.txBytes < 0
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700399 || entry.txPackets < 0 || entry.operations < 0)) {
Jeff Sharkey75279902011-05-24 18:39:45 -0700400 throw new IllegalArgumentException("found non-monotonic values");
401 }
Jeff Sharkey3f391352011-06-05 17:42:53 -0700402 if (clampNegative) {
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700403 entry.rxBytes = Math.max(0, entry.rxBytes);
404 entry.rxPackets = Math.max(0, entry.rxPackets);
405 entry.txBytes = Math.max(0, entry.txBytes);
406 entry.txPackets = Math.max(0, entry.txPackets);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700407 entry.operations = Math.max(0, entry.operations);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700408 }
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700409 }
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700410
411 result.addValues(entry);
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700412 }
413
Jeff Sharkey4a971222011-06-11 22:16:55 -0700414 return result;
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700415 }
416
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700417 public void dump(String prefix, PrintWriter pw) {
418 pw.print(prefix);
419 pw.print("NetworkStats: elapsedRealtime="); pw.println(elapsedRealtime);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700420 for (int i = 0; i < size; i++) {
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700421 pw.print(prefix);
422 pw.print(" iface="); pw.print(iface[i]);
423 pw.print(" uid="); pw.print(uid[i]);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700424 pw.print(" set="); pw.print(setToString(set[i]));
425 pw.print(" tag="); pw.print(tagToString(tag[i]));
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700426 pw.print(" rxBytes="); pw.print(rxBytes[i]);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700427 pw.print(" rxPackets="); pw.print(rxPackets[i]);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700428 pw.print(" txBytes="); pw.print(txBytes[i]);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700429 pw.print(" txPackets="); pw.print(txPackets[i]);
430 pw.print(" operations="); pw.println(operations[i]);
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700431 }
432 }
433
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700434 /**
435 * Return text description of {@link #set} value.
436 */
437 public static String setToString(int set) {
438 switch (set) {
439 case SET_ALL:
440 return "ALL";
441 case SET_DEFAULT:
442 return "DEFAULT";
443 case SET_FOREGROUND:
444 return "FOREGROUND";
445 default:
446 return "UNKNOWN";
447 }
448 }
449
450 /**
451 * Return text description of {@link #tag} value.
452 */
453 public static String tagToString(int tag) {
454 return "0x" + Integer.toHexString(tag);
455 }
456
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700457 @Override
458 public String toString() {
459 final CharArrayWriter writer = new CharArrayWriter();
460 dump("", new PrintWriter(writer));
461 return writer.toString();
462 }
463
464 /** {@inheritDoc} */
Jeff Sharkeyeedcb952011-05-17 14:55:15 -0700465 public int describeContents() {
466 return 0;
467 }
468
Jeff Sharkey9a13f362011-04-26 16:25:36 -0700469 public static final Creator<NetworkStats> CREATOR = new Creator<NetworkStats>() {
470 public NetworkStats createFromParcel(Parcel in) {
471 return new NetworkStats(in);
472 }
473
474 public NetworkStats[] newArray(int size) {
475 return new NetworkStats[size];
476 }
477 };
478}