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