BatteryStats.java revision 32907cfb38bda2d3c052cf5139c5b592678fedbb
1/* 2 * Copyright (C) 2008 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.os; 18 19import java.io.PrintWriter; 20import java.util.Formatter; 21import java.util.Map; 22 23import android.util.Log; 24import android.util.Printer; 25import android.util.SparseArray; 26 27/** 28 * A class providing access to battery usage statistics, including information on 29 * wakelocks, processes, packages, and services. All times are represented in microseconds 30 * except where indicated otherwise. 31 * @hide 32 */ 33public abstract class BatteryStats implements Parcelable { 34 35 private static final boolean LOCAL_LOGV = false; 36 37 /** 38 * A constant indicating a partial wake lock timer. 39 */ 40 public static final int WAKE_TYPE_PARTIAL = 0; 41 42 /** 43 * A constant indicating a full wake lock timer. 44 */ 45 public static final int WAKE_TYPE_FULL = 1; 46 47 /** 48 * A constant indicating a window wake lock timer. 49 */ 50 public static final int WAKE_TYPE_WINDOW = 2; 51 52 /** 53 * A constant indicating a sensor timer. 54 */ 55 public static final int SENSOR = 3; 56 57 /** 58 * A constant indicating a a wifi turn on timer 59 */ 60 public static final int WIFI_TURNED_ON = 4; 61 62 /** 63 * A constant indicating a full wifi lock timer 64 */ 65 public static final int FULL_WIFI_LOCK = 5; 66 67 /** 68 * A constant indicating a scan wifi lock timer 69 */ 70 public static final int SCAN_WIFI_LOCK = 6; 71 72 /** 73 * A constant indicating a wifi multicast timer 74 */ 75 public static final int WIFI_MULTICAST_ENABLED = 7; 76 77 /** 78 * A constant indicating an audio turn on timer 79 */ 80 public static final int AUDIO_TURNED_ON = 7; 81 82 /** 83 * A constant indicating a video turn on timer 84 */ 85 public static final int VIDEO_TURNED_ON = 8; 86 87 /** 88 * Include all of the data in the stats, including previously saved data. 89 */ 90 public static final int STATS_TOTAL = 0; 91 92 /** 93 * Include only the last run in the stats. 94 */ 95 public static final int STATS_LAST = 1; 96 97 /** 98 * Include only the current run in the stats. 99 */ 100 public static final int STATS_CURRENT = 2; 101 102 /** 103 * Include only the run since the last time the device was unplugged in the stats. 104 */ 105 public static final int STATS_UNPLUGGED = 3; 106 107 // NOTE: Update this list if you add/change any stats above. 108 // These characters are supposed to represent "total", "last", "current", 109 // and "unplugged". They were shortened for effeciency sake. 110 private static final String[] STAT_NAMES = { "t", "l", "c", "u" }; 111 112 /** 113 * Bump the version on this if the checkin format changes. 114 */ 115 private static final int BATTERY_STATS_CHECKIN_VERSION = 5; 116 117 private static final long BYTES_PER_KB = 1024; 118 private static final long BYTES_PER_MB = 1048576; // 1024^2 119 private static final long BYTES_PER_GB = 1073741824; //1024^3 120 121 122 private static final String APK_DATA = "apk"; 123 private static final String PROCESS_DATA = "pr"; 124 private static final String SENSOR_DATA = "sr"; 125 private static final String WAKELOCK_DATA = "wl"; 126 private static final String KERNEL_WAKELOCK_DATA = "kwl"; 127 private static final String NETWORK_DATA = "nt"; 128 private static final String USER_ACTIVITY_DATA = "ua"; 129 private static final String BATTERY_DATA = "bt"; 130 private static final String BATTERY_LEVEL_DATA = "lv"; 131 private static final String WIFI_LOCK_DATA = "wfl"; 132 private static final String MISC_DATA = "m"; 133 private static final String SCREEN_BRIGHTNESS_DATA = "br"; 134 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt"; 135 private static final String SIGNAL_SCANNING_TIME_DATA = "sst"; 136 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc"; 137 private static final String DATA_CONNECTION_TIME_DATA = "dct"; 138 private static final String DATA_CONNECTION_COUNT_DATA = "dcc"; 139 140 private final StringBuilder mFormatBuilder = new StringBuilder(32); 141 private final Formatter mFormatter = new Formatter(mFormatBuilder); 142 143 /** 144 * State for keeping track of counting information. 145 */ 146 public static abstract class Counter { 147 148 /** 149 * Returns the count associated with this Counter for the 150 * selected type of statistics. 151 * 152 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT 153 */ 154 public abstract int getCountLocked(int which); 155 156 /** 157 * Temporary for debugging. 158 */ 159 public abstract void logState(Printer pw, String prefix); 160 } 161 162 /** 163 * State for keeping track of timing information. 164 */ 165 public static abstract class Timer { 166 167 /** 168 * Returns the count associated with this Timer for the 169 * selected type of statistics. 170 * 171 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT 172 */ 173 public abstract int getCountLocked(int which); 174 175 /** 176 * Returns the total time in microseconds associated with this Timer for the 177 * selected type of statistics. 178 * 179 * @param batteryRealtime system realtime on battery in microseconds 180 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT 181 * @return a time in microseconds 182 */ 183 public abstract long getTotalTimeLocked(long batteryRealtime, int which); 184 185 /** 186 * Temporary for debugging. 187 */ 188 public abstract void logState(Printer pw, String prefix); 189 } 190 191 /** 192 * The statistics associated with a particular uid. 193 */ 194 public static abstract class Uid { 195 196 /** 197 * Returns a mapping containing wakelock statistics. 198 * 199 * @return a Map from Strings to Uid.Wakelock objects. 200 */ 201 public abstract Map<String, ? extends Wakelock> getWakelockStats(); 202 203 /** 204 * The statistics associated with a particular wake lock. 205 */ 206 public static abstract class Wakelock { 207 public abstract Timer getWakeTime(int type); 208 } 209 210 /** 211 * Returns a mapping containing sensor statistics. 212 * 213 * @return a Map from Integer sensor ids to Uid.Sensor objects. 214 */ 215 public abstract Map<Integer, ? extends Sensor> getSensorStats(); 216 217 /** 218 * Returns a mapping containing process statistics. 219 * 220 * @return a Map from Strings to Uid.Proc objects. 221 */ 222 public abstract Map<String, ? extends Proc> getProcessStats(); 223 224 /** 225 * Returns a mapping containing package statistics. 226 * 227 * @return a Map from Strings to Uid.Pkg objects. 228 */ 229 public abstract Map<String, ? extends Pkg> getPackageStats(); 230 231 /** 232 * {@hide} 233 */ 234 public abstract int getUid(); 235 236 /** 237 * {@hide} 238 */ 239 public abstract long getTcpBytesReceived(int which); 240 241 /** 242 * {@hide} 243 */ 244 public abstract long getTcpBytesSent(int which); 245 246 public abstract void noteWifiTurnedOnLocked(); 247 public abstract void noteWifiTurnedOffLocked(); 248 public abstract void noteFullWifiLockAcquiredLocked(); 249 public abstract void noteFullWifiLockReleasedLocked(); 250 public abstract void noteScanWifiLockAcquiredLocked(); 251 public abstract void noteScanWifiLockReleasedLocked(); 252 public abstract void noteWifiMulticastEnabledLocked(); 253 public abstract void noteWifiMulticastDisabledLocked(); 254 public abstract void noteAudioTurnedOnLocked(); 255 public abstract void noteAudioTurnedOffLocked(); 256 public abstract void noteVideoTurnedOnLocked(); 257 public abstract void noteVideoTurnedOffLocked(); 258 public abstract long getWifiTurnedOnTime(long batteryRealtime, int which); 259 public abstract long getFullWifiLockTime(long batteryRealtime, int which); 260 public abstract long getScanWifiLockTime(long batteryRealtime, int which); 261 public abstract long getWifiMulticastTime(long batteryRealtime, 262 int which); 263 public abstract long getAudioTurnedOnTime(long batteryRealtime, int which); 264 public abstract long getVideoTurnedOnTime(long batteryRealtime, int which); 265 266 /** 267 * Note that these must match the constants in android.os.LocalPowerManager. 268 */ 269 static final String[] USER_ACTIVITY_TYPES = { 270 "other", "cheek", "touch", "long_touch", "touch_up", "button", "unknown" 271 }; 272 273 public static final int NUM_USER_ACTIVITY_TYPES = 7; 274 275 public abstract void noteUserActivityLocked(int type); 276 public abstract boolean hasUserActivity(); 277 public abstract int getUserActivityCount(int type, int which); 278 279 public static abstract class Sensor { 280 // Magic sensor number for the GPS. 281 public static final int GPS = -10000; 282 283 public abstract int getHandle(); 284 285 public abstract Timer getSensorTime(); 286 } 287 288 /** 289 * The statistics associated with a particular process. 290 */ 291 public static abstract class Proc { 292 293 /** 294 * Returns the total time (in 1/100 sec) spent executing in user code. 295 * 296 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 297 */ 298 public abstract long getUserTime(int which); 299 300 /** 301 * Returns the total time (in 1/100 sec) spent executing in system code. 302 * 303 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 304 */ 305 public abstract long getSystemTime(int which); 306 307 /** 308 * Returns the number of times the process has been started. 309 * 310 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 311 */ 312 public abstract int getStarts(int which); 313 314 /** 315 * Returns the cpu time spent in microseconds while the process was in the foreground. 316 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED 317 * @return foreground cpu time in microseconds 318 */ 319 public abstract long getForegroundTime(int which); 320 321 /** 322 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed. 323 * @param speedStep the index of the CPU speed. This is not the actual speed of the 324 * CPU. 325 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED 326 * @see BatteryStats#getCpuSpeedSteps() 327 */ 328 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which); 329 } 330 331 /** 332 * The statistics associated with a particular package. 333 */ 334 public static abstract class Pkg { 335 336 /** 337 * Returns the number of times this package has done something that could wake up the 338 * device from sleep. 339 * 340 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 341 */ 342 public abstract int getWakeups(int which); 343 344 /** 345 * Returns a mapping containing service statistics. 346 */ 347 public abstract Map<String, ? extends Serv> getServiceStats(); 348 349 /** 350 * The statistics associated with a particular service. 351 */ 352 public abstract class Serv { 353 354 /** 355 * Returns the amount of time spent started. 356 * 357 * @param batteryUptime elapsed uptime on battery in microseconds. 358 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 359 * @return 360 */ 361 public abstract long getStartTime(long batteryUptime, int which); 362 363 /** 364 * Returns the total number of times startService() has been called. 365 * 366 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 367 */ 368 public abstract int getStarts(int which); 369 370 /** 371 * Returns the total number times the service has been launched. 372 * 373 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 374 */ 375 public abstract int getLaunches(int which); 376 } 377 } 378 } 379 380 public final class BatteryHistoryRecord implements Parcelable { 381 public BatteryHistoryRecord next; 382 383 public long time; 384 public byte batteryLevel; 385 386 public static final int STATE_SCREEN_MASK = 0x000000f; 387 public static final int STATE_SCREEN_SHIFT = 0; 388 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x00000f0; 389 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 4; 390 public static final int STATE_PHONE_STATE_MASK = 0x0000f00; 391 public static final int STATE_PHONE_STATE_SHIFT = 8; 392 public static final int STATE_DATA_CONNECTION_MASK = 0x000f000; 393 public static final int STATE_DATA_CONNECTION_SHIFT = 12; 394 395 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<30; 396 public static final int STATE_SCREEN_ON_FLAG = 1<<29; 397 public static final int STATE_GPS_ON_FLAG = 1<<28; 398 public static final int STATE_PHONE_ON_FLAG = 1<<27; 399 public static final int STATE_WIFI_ON_FLAG = 1<<26; 400 public static final int STATE_WIFI_RUNNING_FLAG = 1<<25; 401 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<24; 402 public static final int STATE_WIFI_SCAN_LOCK_FLAG = 1<<23; 403 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<22; 404 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<21; 405 public static final int STATE_AUDIO_ON_FLAG = 1<<20; 406 public static final int STATE_VIDEO_ON_FLAG = 1<<19; 407 408 public int states; 409 410 public BatteryHistoryRecord() { 411 } 412 413 public BatteryHistoryRecord(long time, Parcel src) { 414 this.time = time; 415 batteryLevel = (byte)src.readInt(); 416 states = src.readInt(); 417 } 418 419 public int describeContents() { 420 return 0; 421 } 422 423 public void writeToParcel(Parcel dest, int flags) { 424 dest.writeLong(time); 425 dest.writeInt(batteryLevel); 426 dest.writeInt(states); 427 } 428 } 429 430 /** 431 * Return the current history of battery state changes. 432 */ 433 public abstract BatteryHistoryRecord getHistory(); 434 435 /** 436 * Returns the number of times the device has been started. 437 */ 438 public abstract int getStartCount(); 439 440 /** 441 * Returns the time in microseconds that the screen has been on while the device was 442 * running on battery. 443 * 444 * {@hide} 445 */ 446 public abstract long getScreenOnTime(long batteryRealtime, int which); 447 448 public static final int SCREEN_BRIGHTNESS_DARK = 0; 449 public static final int SCREEN_BRIGHTNESS_DIM = 1; 450 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2; 451 public static final int SCREEN_BRIGHTNESS_LIGHT = 3; 452 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4; 453 454 static final String[] SCREEN_BRIGHTNESS_NAMES = { 455 "dark", "dim", "medium", "light", "bright" 456 }; 457 458 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5; 459 460 /** 461 * Returns the time in microseconds that the screen has been on with 462 * the given brightness 463 * 464 * {@hide} 465 */ 466 public abstract long getScreenBrightnessTime(int brightnessBin, 467 long batteryRealtime, int which); 468 469 public abstract int getInputEventCount(int which); 470 471 /** 472 * Returns the time in microseconds that the phone has been on while the device was 473 * running on battery. 474 * 475 * {@hide} 476 */ 477 public abstract long getPhoneOnTime(long batteryRealtime, int which); 478 479 public static final int SIGNAL_STRENGTH_NONE_OR_UNKNOWN = 0; 480 public static final int SIGNAL_STRENGTH_POOR = 1; 481 public static final int SIGNAL_STRENGTH_MODERATE = 2; 482 public static final int SIGNAL_STRENGTH_GOOD = 3; 483 public static final int SIGNAL_STRENGTH_GREAT = 4; 484 485 static final String[] SIGNAL_STRENGTH_NAMES = { 486 "none", "poor", "moderate", "good", "great" 487 }; 488 489 public static final int NUM_SIGNAL_STRENGTH_BINS = 5; 490 491 /** 492 * Returns the time in microseconds that the phone has been running with 493 * the given signal strength. 494 * 495 * {@hide} 496 */ 497 public abstract long getPhoneSignalStrengthTime(int strengthBin, 498 long batteryRealtime, int which); 499 500 /** 501 * Returns the time in microseconds that the phone has been trying to 502 * acquire a signal. 503 * 504 * {@hide} 505 */ 506 public abstract long getPhoneSignalScanningTime( 507 long batteryRealtime, int which); 508 509 /** 510 * Returns the number of times the phone has entered the given signal strength. 511 * 512 * {@hide} 513 */ 514 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which); 515 516 public static final int DATA_CONNECTION_NONE = 0; 517 public static final int DATA_CONNECTION_GPRS = 1; 518 public static final int DATA_CONNECTION_EDGE = 2; 519 public static final int DATA_CONNECTION_UMTS = 3; 520 public static final int DATA_CONNECTION_OTHER = 4; 521 522 static final String[] DATA_CONNECTION_NAMES = { 523 "none", "gprs", "edge", "umts", "other" 524 }; 525 526 public static final int NUM_DATA_CONNECTION_TYPES = 5; 527 528 /** 529 * Returns the time in microseconds that the phone has been running with 530 * the given data connection. 531 * 532 * {@hide} 533 */ 534 public abstract long getPhoneDataConnectionTime(int dataType, 535 long batteryRealtime, int which); 536 537 /** 538 * Returns the number of times the phone has entered the given data 539 * connection type. 540 * 541 * {@hide} 542 */ 543 public abstract int getPhoneDataConnectionCount(int dataType, int which); 544 545 /** 546 * Returns the time in microseconds that wifi has been on while the device was 547 * running on battery. 548 * 549 * {@hide} 550 */ 551 public abstract long getWifiOnTime(long batteryRealtime, int which); 552 553 /** 554 * Returns the time in microseconds that wifi has been on and the driver has 555 * been in the running state while the device was running on battery. 556 * 557 * {@hide} 558 */ 559 public abstract long getWifiRunningTime(long batteryRealtime, int which); 560 561 /** 562 * Returns the time in microseconds that bluetooth has been on while the device was 563 * running on battery. 564 * 565 * {@hide} 566 */ 567 public abstract long getBluetoothOnTime(long batteryRealtime, int which); 568 569 /** 570 * Return whether we are currently running on battery. 571 */ 572 public abstract boolean getIsOnBattery(); 573 574 /** 575 * Returns a SparseArray containing the statistics for each uid. 576 */ 577 public abstract SparseArray<? extends Uid> getUidStats(); 578 579 /** 580 * Returns the current battery uptime in microseconds. 581 * 582 * @param curTime the amount of elapsed realtime in microseconds. 583 */ 584 public abstract long getBatteryUptime(long curTime); 585 586 /** 587 * @deprecated use getRadioDataUptime 588 */ 589 public long getRadioDataUptimeMs() { 590 return getRadioDataUptime() / 1000; 591 } 592 593 /** 594 * Returns the time that the radio was on for data transfers. 595 * @return the uptime in microseconds while unplugged 596 */ 597 public abstract long getRadioDataUptime(); 598 599 /** 600 * Returns the current battery realtime in microseconds. 601 * 602 * @param curTime the amount of elapsed realtime in microseconds. 603 */ 604 public abstract long getBatteryRealtime(long curTime); 605 606 /** 607 * Returns the battery percentage level at the last time the device was unplugged from power, or 608 * the last time it booted on battery power. 609 */ 610 public abstract int getDischargeStartLevel(); 611 612 /** 613 * Returns the current battery percentage level if we are in a discharge cycle, otherwise 614 * returns the level at the last plug event. 615 */ 616 public abstract int getDischargeCurrentLevel(); 617 618 /** 619 * Returns the total, last, or current battery uptime in microseconds. 620 * 621 * @param curTime the elapsed realtime in microseconds. 622 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 623 */ 624 public abstract long computeBatteryUptime(long curTime, int which); 625 626 /** 627 * Returns the total, last, or current battery realtime in microseconds. 628 * 629 * @param curTime the current elapsed realtime in microseconds. 630 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 631 */ 632 public abstract long computeBatteryRealtime(long curTime, int which); 633 634 /** 635 * Returns the total, last, or current uptime in microseconds. 636 * 637 * @param curTime the current elapsed realtime in microseconds. 638 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 639 */ 640 public abstract long computeUptime(long curTime, int which); 641 642 /** 643 * Returns the total, last, or current realtime in microseconds. 644 * * 645 * @param curTime the current elapsed realtime in microseconds. 646 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 647 */ 648 public abstract long computeRealtime(long curTime, int which); 649 650 public abstract Map<String, ? extends Timer> getKernelWakelockStats(); 651 652 /** Returns the number of different speeds that the CPU can run at */ 653 public abstract int getCpuSpeedSteps(); 654 655 private final static void formatTimeRaw(StringBuilder out, long seconds) { 656 long days = seconds / (60 * 60 * 24); 657 if (days != 0) { 658 out.append(days); 659 out.append("d "); 660 } 661 long used = days * 60 * 60 * 24; 662 663 long hours = (seconds - used) / (60 * 60); 664 if (hours != 0 || used != 0) { 665 out.append(hours); 666 out.append("h "); 667 } 668 used += hours * 60 * 60; 669 670 long mins = (seconds-used) / 60; 671 if (mins != 0 || used != 0) { 672 out.append(mins); 673 out.append("m "); 674 } 675 used += mins * 60; 676 677 if (seconds != 0 || used != 0) { 678 out.append(seconds-used); 679 out.append("s "); 680 } 681 } 682 683 private final static void formatTime(StringBuilder sb, long time) { 684 long sec = time / 100; 685 formatTimeRaw(sb, sec); 686 sb.append((time - (sec * 100)) * 10); 687 sb.append("ms "); 688 } 689 690 private final static void formatTimeMs(StringBuilder sb, long time) { 691 long sec = time / 1000; 692 formatTimeRaw(sb, sec); 693 sb.append(time - (sec * 1000)); 694 sb.append("ms "); 695 } 696 697 private final String formatRatioLocked(long num, long den) { 698 if (den == 0L) { 699 return "---%"; 700 } 701 float perc = ((float)num) / ((float)den) * 100; 702 mFormatBuilder.setLength(0); 703 mFormatter.format("%.1f%%", perc); 704 return mFormatBuilder.toString(); 705 } 706 707 private final String formatBytesLocked(long bytes) { 708 mFormatBuilder.setLength(0); 709 710 if (bytes < BYTES_PER_KB) { 711 return bytes + "B"; 712 } else if (bytes < BYTES_PER_MB) { 713 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB); 714 return mFormatBuilder.toString(); 715 } else if (bytes < BYTES_PER_GB){ 716 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB); 717 return mFormatBuilder.toString(); 718 } else { 719 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB); 720 return mFormatBuilder.toString(); 721 } 722 } 723 724 /** 725 * 726 * @param sb a StringBuilder object. 727 * @param timer a Timer object contining the wakelock times. 728 * @param batteryRealtime the current on-battery time in microseconds. 729 * @param name the name of the wakelock. 730 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 731 * @param linePrefix a String to be prepended to each line of output. 732 * @return the line prefix 733 */ 734 private static final String printWakeLock(StringBuilder sb, Timer timer, 735 long batteryRealtime, String name, int which, String linePrefix) { 736 737 if (timer != null) { 738 // Convert from microseconds to milliseconds with rounding 739 long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which); 740 long totalTimeMillis = (totalTimeMicros + 500) / 1000; 741 742 int count = timer.getCountLocked(which); 743 if (totalTimeMillis != 0) { 744 sb.append(linePrefix); 745 formatTimeMs(sb, totalTimeMillis); 746 if (name != null) sb.append(name); 747 sb.append(' '); 748 sb.append('('); 749 sb.append(count); 750 sb.append(" times)"); 751 return ", "; 752 } 753 } 754 return linePrefix; 755 } 756 757 /** 758 * Checkin version of wakelock printer. Prints simple comma-separated list. 759 * 760 * @param sb a StringBuilder object. 761 * @param timer a Timer object contining the wakelock times. 762 * @param now the current time in microseconds. 763 * @param name the name of the wakelock. 764 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 765 * @param linePrefix a String to be prepended to each line of output. 766 * @return the line prefix 767 */ 768 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer, long now, 769 String name, int which, String linePrefix) { 770 long totalTimeMicros = 0; 771 int count = 0; 772 if (timer != null) { 773 totalTimeMicros = timer.getTotalTimeLocked(now, which); 774 count = timer.getCountLocked(which); 775 } 776 sb.append(linePrefix); 777 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding 778 sb.append(','); 779 sb.append(name != null ? name + "," : ""); 780 sb.append(count); 781 return ","; 782 } 783 784 /** 785 * Dump a comma-separated line of values for terse checkin mode. 786 * 787 * @param pw the PageWriter to dump log to 788 * @param category category of data (e.g. "total", "last", "unplugged", "current" ) 789 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network") 790 * @param args type-dependent data arguments 791 */ 792 private static final void dumpLine(PrintWriter pw, int uid, String category, String type, 793 Object... args ) { 794 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(','); 795 pw.print(uid); pw.print(','); 796 pw.print(category); pw.print(','); 797 pw.print(type); 798 799 for (Object arg : args) { 800 pw.print(','); 801 pw.print(arg); 802 } 803 pw.print('\n'); 804 } 805 806 /** 807 * Checkin server version of dump to produce more compact, computer-readable log. 808 * 809 * NOTE: all times are expressed in 'ms'. 810 */ 811 public final void dumpCheckinLocked(PrintWriter pw, int which, int reqUid) { 812 final long rawUptime = SystemClock.uptimeMillis() * 1000; 813 final long rawRealtime = SystemClock.elapsedRealtime() * 1000; 814 final long batteryUptime = getBatteryUptime(rawUptime); 815 final long batteryRealtime = getBatteryRealtime(rawRealtime); 816 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which); 817 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which); 818 final long totalRealtime = computeRealtime(rawRealtime, which); 819 final long totalUptime = computeUptime(rawUptime, which); 820 final long screenOnTime = getScreenOnTime(batteryRealtime, which); 821 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which); 822 final long wifiOnTime = getWifiOnTime(batteryRealtime, which); 823 final long wifiRunningTime = getWifiRunningTime(batteryRealtime, which); 824 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which); 825 826 StringBuilder sb = new StringBuilder(128); 827 828 SparseArray<? extends Uid> uidStats = getUidStats(); 829 final int NU = uidStats.size(); 830 831 String category = STAT_NAMES[which]; 832 833 // Dump "battery" stat 834 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA, 835 which == STATS_TOTAL ? getStartCount() : "N/A", 836 whichBatteryRealtime / 1000, whichBatteryUptime / 1000, 837 totalRealtime / 1000, totalUptime / 1000); 838 839 // Calculate total network and wakelock times across all uids. 840 long rxTotal = 0; 841 long txTotal = 0; 842 long fullWakeLockTimeTotal = 0; 843 long partialWakeLockTimeTotal = 0; 844 845 for (int iu = 0; iu < NU; iu++) { 846 Uid u = uidStats.valueAt(iu); 847 rxTotal += u.getTcpBytesReceived(which); 848 txTotal += u.getTcpBytesSent(which); 849 850 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats(); 851 if (wakelocks.size() > 0) { 852 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent 853 : wakelocks.entrySet()) { 854 Uid.Wakelock wl = ent.getValue(); 855 856 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL); 857 if (fullWakeTimer != null) { 858 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(batteryRealtime, which); 859 } 860 861 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL); 862 if (partialWakeTimer != null) { 863 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked( 864 batteryRealtime, which); 865 } 866 } 867 } 868 } 869 870 // Dump misc stats 871 dumpLine(pw, 0 /* uid */, category, MISC_DATA, 872 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000, 873 wifiRunningTime / 1000, bluetoothOnTime / 1000, rxTotal, txTotal, 874 fullWakeLockTimeTotal, partialWakeLockTimeTotal, 875 getInputEventCount(which)); 876 877 // Dump screen brightness stats 878 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS]; 879 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { 880 args[i] = getScreenBrightnessTime(i, batteryRealtime, which) / 1000; 881 } 882 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args); 883 884 // Dump signal strength stats 885 args = new Object[NUM_SIGNAL_STRENGTH_BINS]; 886 for (int i=0; i<NUM_SIGNAL_STRENGTH_BINS; i++) { 887 args[i] = getPhoneSignalStrengthTime(i, batteryRealtime, which) / 1000; 888 } 889 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args); 890 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA, 891 getPhoneSignalScanningTime(batteryRealtime, which) / 1000); 892 for (int i=0; i<NUM_SIGNAL_STRENGTH_BINS; i++) { 893 args[i] = getPhoneSignalStrengthCount(i, which); 894 } 895 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args); 896 897 // Dump network type stats 898 args = new Object[NUM_DATA_CONNECTION_TYPES]; 899 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { 900 args[i] = getPhoneDataConnectionTime(i, batteryRealtime, which) / 1000; 901 } 902 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args); 903 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { 904 args[i] = getPhoneDataConnectionCount(i, which); 905 } 906 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args); 907 908 if (which == STATS_UNPLUGGED) { 909 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(), 910 getDischargeCurrentLevel()); 911 } 912 913 if (reqUid < 0) { 914 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats(); 915 if (kernelWakelocks.size() > 0) { 916 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) { 917 sb.setLength(0); 918 printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, ""); 919 920 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(), 921 sb.toString()); 922 } 923 } 924 } 925 926 for (int iu = 0; iu < NU; iu++) { 927 final int uid = uidStats.keyAt(iu); 928 if (reqUid >= 0 && uid != reqUid) { 929 continue; 930 } 931 Uid u = uidStats.valueAt(iu); 932 // Dump Network stats per uid, if any 933 long rx = u.getTcpBytesReceived(which); 934 long tx = u.getTcpBytesSent(which); 935 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which); 936 long scanWifiLockOnTime = u.getScanWifiLockTime(batteryRealtime, which); 937 long wifiTurnedOnTime = u.getWifiTurnedOnTime(batteryRealtime, which); 938 939 if (rx > 0 || tx > 0) dumpLine(pw, uid, category, NETWORK_DATA, rx, tx); 940 941 if (fullWifiLockOnTime != 0 || scanWifiLockOnTime != 0 942 || wifiTurnedOnTime != 0) { 943 dumpLine(pw, uid, category, WIFI_LOCK_DATA, 944 fullWifiLockOnTime, scanWifiLockOnTime, wifiTurnedOnTime); 945 } 946 947 if (u.hasUserActivity()) { 948 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES]; 949 boolean hasData = false; 950 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) { 951 int val = u.getUserActivityCount(i, which); 952 args[i] = val; 953 if (val != 0) hasData = true; 954 } 955 if (hasData) { 956 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args); 957 } 958 } 959 960 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats(); 961 if (wakelocks.size() > 0) { 962 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent 963 : wakelocks.entrySet()) { 964 Uid.Wakelock wl = ent.getValue(); 965 String linePrefix = ""; 966 sb.setLength(0); 967 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL), 968 batteryRealtime, "f", which, linePrefix); 969 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), 970 batteryRealtime, "p", which, linePrefix); 971 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), 972 batteryRealtime, "w", which, linePrefix); 973 974 // Only log if we had at lease one wakelock... 975 if (sb.length() > 0) { 976 dumpLine(pw, uid, category, WAKELOCK_DATA, ent.getKey(), sb.toString()); 977 } 978 } 979 } 980 981 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats(); 982 if (sensors.size() > 0) { 983 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent 984 : sensors.entrySet()) { 985 Uid.Sensor se = ent.getValue(); 986 int sensorNumber = ent.getKey(); 987 Timer timer = se.getSensorTime(); 988 if (timer != null) { 989 // Convert from microseconds to milliseconds with rounding 990 long totalTime = (timer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000; 991 int count = timer.getCountLocked(which); 992 if (totalTime != 0) { 993 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count); 994 } 995 } 996 } 997 } 998 999 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats(); 1000 if (processStats.size() > 0) { 1001 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent 1002 : processStats.entrySet()) { 1003 Uid.Proc ps = ent.getValue(); 1004 1005 long userTime = ps.getUserTime(which); 1006 long systemTime = ps.getSystemTime(which); 1007 int starts = ps.getStarts(which); 1008 1009 if (userTime != 0 || systemTime != 0 || starts != 0) { 1010 dumpLine(pw, uid, category, PROCESS_DATA, 1011 ent.getKey(), // proc 1012 userTime * 10, // cpu time in ms 1013 systemTime * 10, // user time in ms 1014 starts); // process starts 1015 } 1016 } 1017 } 1018 1019 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats(); 1020 if (packageStats.size() > 0) { 1021 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent 1022 : packageStats.entrySet()) { 1023 1024 Uid.Pkg ps = ent.getValue(); 1025 int wakeups = ps.getWakeups(which); 1026 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats(); 1027 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent 1028 : serviceStats.entrySet()) { 1029 BatteryStats.Uid.Pkg.Serv ss = sent.getValue(); 1030 long startTime = ss.getStartTime(batteryUptime, which); 1031 int starts = ss.getStarts(which); 1032 int launches = ss.getLaunches(which); 1033 if (startTime != 0 || starts != 0 || launches != 0) { 1034 dumpLine(pw, uid, category, APK_DATA, 1035 wakeups, // wakeup alarms 1036 ent.getKey(), // Apk 1037 sent.getKey(), // service 1038 startTime / 1000, // time spent started, in ms 1039 starts, 1040 launches); 1041 } 1042 } 1043 } 1044 } 1045 } 1046 } 1047 1048 @SuppressWarnings("unused") 1049 public final void dumpLocked(PrintWriter pw, String prefix, int which, int reqUid) { 1050 final long rawUptime = SystemClock.uptimeMillis() * 1000; 1051 final long rawRealtime = SystemClock.elapsedRealtime() * 1000; 1052 final long batteryUptime = getBatteryUptime(rawUptime); 1053 final long batteryRealtime = getBatteryRealtime(rawRealtime); 1054 1055 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which); 1056 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which); 1057 final long totalRealtime = computeRealtime(rawRealtime, which); 1058 final long totalUptime = computeUptime(rawUptime, which); 1059 1060 StringBuilder sb = new StringBuilder(128); 1061 1062 SparseArray<? extends Uid> uidStats = getUidStats(); 1063 final int NU = uidStats.size(); 1064 1065 sb.setLength(0); 1066 sb.append(prefix); 1067 sb.append(" Time on battery: "); 1068 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("("); 1069 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime)); 1070 sb.append(") realtime, "); 1071 formatTimeMs(sb, whichBatteryUptime / 1000); 1072 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime)); 1073 sb.append(") uptime"); 1074 pw.println(sb.toString()); 1075 sb.setLength(0); 1076 sb.append(prefix); 1077 sb.append(" Total run time: "); 1078 formatTimeMs(sb, totalRealtime / 1000); 1079 sb.append("realtime, "); 1080 formatTimeMs(sb, totalUptime / 1000); 1081 sb.append("uptime, "); 1082 pw.println(sb.toString()); 1083 1084 final long screenOnTime = getScreenOnTime(batteryRealtime, which); 1085 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which); 1086 final long wifiRunningTime = getWifiRunningTime(batteryRealtime, which); 1087 final long wifiOnTime = getWifiOnTime(batteryRealtime, which); 1088 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which); 1089 sb.setLength(0); 1090 sb.append(prefix); 1091 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000); 1092 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime)); 1093 sb.append("), Input events: "); sb.append(getInputEventCount(which)); 1094 sb.append(", Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000); 1095 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime)); 1096 sb.append(")"); 1097 pw.println(sb.toString()); 1098 sb.setLength(0); 1099 sb.append(prefix); 1100 sb.append(" Screen brightnesses: "); 1101 boolean didOne = false; 1102 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { 1103 final long time = getScreenBrightnessTime(i, batteryRealtime, which); 1104 if (time == 0) { 1105 continue; 1106 } 1107 if (didOne) sb.append(", "); 1108 didOne = true; 1109 sb.append(SCREEN_BRIGHTNESS_NAMES[i]); 1110 sb.append(" "); 1111 formatTimeMs(sb, time/1000); 1112 sb.append("("); 1113 sb.append(formatRatioLocked(time, screenOnTime)); 1114 sb.append(")"); 1115 } 1116 if (!didOne) sb.append("No activity"); 1117 pw.println(sb.toString()); 1118 1119 // Calculate total network and wakelock times across all uids. 1120 long rxTotal = 0; 1121 long txTotal = 0; 1122 long fullWakeLockTimeTotalMicros = 0; 1123 long partialWakeLockTimeTotalMicros = 0; 1124 1125 if (reqUid < 0) { 1126 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats(); 1127 if (kernelWakelocks.size() > 0) { 1128 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) { 1129 1130 String linePrefix = ": "; 1131 sb.setLength(0); 1132 sb.append(prefix); 1133 sb.append(" Kernel Wake lock "); 1134 sb.append(ent.getKey()); 1135 linePrefix = printWakeLock(sb, ent.getValue(), batteryRealtime, null, which, 1136 linePrefix); 1137 if (!linePrefix.equals(": ")) { 1138 sb.append(" realtime"); 1139 } else { 1140 sb.append(": (nothing executed)"); 1141 } 1142 pw.println(sb.toString()); 1143 } 1144 } 1145 } 1146 1147 for (int iu = 0; iu < NU; iu++) { 1148 Uid u = uidStats.valueAt(iu); 1149 rxTotal += u.getTcpBytesReceived(which); 1150 txTotal += u.getTcpBytesSent(which); 1151 1152 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats(); 1153 if (wakelocks.size() > 0) { 1154 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent 1155 : wakelocks.entrySet()) { 1156 Uid.Wakelock wl = ent.getValue(); 1157 1158 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL); 1159 if (fullWakeTimer != null) { 1160 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked( 1161 batteryRealtime, which); 1162 } 1163 1164 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL); 1165 if (partialWakeTimer != null) { 1166 partialWakeLockTimeTotalMicros += partialWakeTimer.getTotalTimeLocked( 1167 batteryRealtime, which); 1168 } 1169 } 1170 } 1171 } 1172 1173 pw.print(prefix); 1174 pw.print(" Total received: "); pw.print(formatBytesLocked(rxTotal)); 1175 pw.print(", Total sent: "); pw.println(formatBytesLocked(txTotal)); 1176 sb.setLength(0); 1177 sb.append(prefix); 1178 sb.append(" Total full wakelock time: "); formatTimeMs(sb, 1179 (fullWakeLockTimeTotalMicros + 500) / 1000); 1180 sb.append(", Total partial waklock time: "); formatTimeMs(sb, 1181 (partialWakeLockTimeTotalMicros + 500) / 1000); 1182 pw.println(sb.toString()); 1183 1184 sb.setLength(0); 1185 sb.append(prefix); 1186 sb.append(" Signal levels: "); 1187 didOne = false; 1188 for (int i=0; i<NUM_SIGNAL_STRENGTH_BINS; i++) { 1189 final long time = getPhoneSignalStrengthTime(i, batteryRealtime, which); 1190 if (time == 0) { 1191 continue; 1192 } 1193 if (didOne) sb.append(", "); 1194 didOne = true; 1195 sb.append(SIGNAL_STRENGTH_NAMES[i]); 1196 sb.append(" "); 1197 formatTimeMs(sb, time/1000); 1198 sb.append("("); 1199 sb.append(formatRatioLocked(time, whichBatteryRealtime)); 1200 sb.append(") "); 1201 sb.append(getPhoneSignalStrengthCount(i, which)); 1202 sb.append("x"); 1203 } 1204 if (!didOne) sb.append("No activity"); 1205 pw.println(sb.toString()); 1206 1207 sb.setLength(0); 1208 sb.append(prefix); 1209 sb.append(" Signal scanning time: "); 1210 formatTimeMs(sb, getPhoneSignalScanningTime(batteryRealtime, which) / 1000); 1211 pw.println(sb.toString()); 1212 1213 sb.setLength(0); 1214 sb.append(prefix); 1215 sb.append(" Radio types: "); 1216 didOne = false; 1217 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { 1218 final long time = getPhoneDataConnectionTime(i, batteryRealtime, which); 1219 if (time == 0) { 1220 continue; 1221 } 1222 if (didOne) sb.append(", "); 1223 didOne = true; 1224 sb.append(DATA_CONNECTION_NAMES[i]); 1225 sb.append(" "); 1226 formatTimeMs(sb, time/1000); 1227 sb.append("("); 1228 sb.append(formatRatioLocked(time, whichBatteryRealtime)); 1229 sb.append(") "); 1230 sb.append(getPhoneDataConnectionCount(i, which)); 1231 sb.append("x"); 1232 } 1233 if (!didOne) sb.append("No activity"); 1234 pw.println(sb.toString()); 1235 1236 sb.setLength(0); 1237 sb.append(prefix); 1238 sb.append(" Radio data uptime when unplugged: "); 1239 sb.append(getRadioDataUptime() / 1000); 1240 sb.append(" ms"); 1241 pw.println(sb.toString()); 1242 1243 sb.setLength(0); 1244 sb.append(prefix); 1245 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000); 1246 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime)); 1247 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000); 1248 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime)); 1249 sb.append("), Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000); 1250 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime)); 1251 sb.append(")"); 1252 pw.println(sb.toString()); 1253 1254 pw.println(" "); 1255 1256 if (which == STATS_UNPLUGGED) { 1257 if (getIsOnBattery()) { 1258 pw.print(prefix); pw.println(" Device is currently unplugged"); 1259 pw.print(prefix); pw.print(" Discharge cycle start level: "); 1260 pw.println(getDischargeStartLevel()); 1261 pw.print(prefix); pw.print(" Discharge cycle current level: "); 1262 pw.println(getDischargeCurrentLevel()); 1263 } else { 1264 pw.print(prefix); pw.println(" Device is currently plugged into power"); 1265 pw.print(prefix); pw.print(" Last discharge cycle start level: "); 1266 pw.println(getDischargeStartLevel()); 1267 pw.print(prefix); pw.print(" Last discharge cycle end level: "); 1268 pw.println(getDischargeCurrentLevel()); 1269 } 1270 pw.println(" "); 1271 } 1272 1273 1274 for (int iu=0; iu<NU; iu++) { 1275 final int uid = uidStats.keyAt(iu); 1276 if (reqUid >= 0 && uid != reqUid) { 1277 continue; 1278 } 1279 1280 Uid u = uidStats.valueAt(iu); 1281 1282 pw.println(prefix + " #" + uid + ":"); 1283 boolean uidActivity = false; 1284 1285 long tcpReceived = u.getTcpBytesReceived(which); 1286 long tcpSent = u.getTcpBytesSent(which); 1287 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which); 1288 long scanWifiLockOnTime = u.getScanWifiLockTime(batteryRealtime, which); 1289 long wifiTurnedOnTime = u.getWifiTurnedOnTime(batteryRealtime, which); 1290 1291 if (tcpReceived != 0 || tcpSent != 0) { 1292 pw.print(prefix); pw.print(" Network: "); 1293 pw.print(formatBytesLocked(tcpReceived)); pw.print(" received, "); 1294 pw.print(formatBytesLocked(tcpSent)); pw.println(" sent"); 1295 } 1296 1297 if (u.hasUserActivity()) { 1298 boolean hasData = false; 1299 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { 1300 int val = u.getUserActivityCount(i, which); 1301 if (val != 0) { 1302 if (!hasData) { 1303 sb.setLength(0); 1304 sb.append(" User activity: "); 1305 hasData = true; 1306 } else { 1307 sb.append(", "); 1308 } 1309 sb.append(val); 1310 sb.append(" "); 1311 sb.append(Uid.USER_ACTIVITY_TYPES[i]); 1312 } 1313 } 1314 if (hasData) { 1315 pw.println(sb.toString()); 1316 } 1317 } 1318 1319 if (fullWifiLockOnTime != 0 || scanWifiLockOnTime != 0 1320 || wifiTurnedOnTime != 0) { 1321 sb.setLength(0); 1322 sb.append(prefix); sb.append(" Turned Wifi On: "); 1323 formatTimeMs(sb, wifiTurnedOnTime / 1000); 1324 sb.append("("); sb.append(formatRatioLocked(wifiTurnedOnTime, 1325 whichBatteryRealtime)); sb.append(")\n"); 1326 sb.append(prefix); sb.append(" Full Wifi Lock: "); 1327 formatTimeMs(sb, fullWifiLockOnTime / 1000); 1328 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime, 1329 whichBatteryRealtime)); sb.append(")\n"); 1330 sb.append(prefix); sb.append(" Scan Wifi Lock: "); 1331 formatTimeMs(sb, scanWifiLockOnTime / 1000); 1332 sb.append("("); sb.append(formatRatioLocked(scanWifiLockOnTime, 1333 whichBatteryRealtime)); sb.append(")"); 1334 pw.println(sb.toString()); 1335 } 1336 1337 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats(); 1338 if (wakelocks.size() > 0) { 1339 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent 1340 : wakelocks.entrySet()) { 1341 Uid.Wakelock wl = ent.getValue(); 1342 String linePrefix = ": "; 1343 sb.setLength(0); 1344 sb.append(prefix); 1345 sb.append(" Wake lock "); 1346 sb.append(ent.getKey()); 1347 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), batteryRealtime, 1348 "full", which, linePrefix); 1349 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), batteryRealtime, 1350 "partial", which, linePrefix); 1351 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), batteryRealtime, 1352 "window", which, linePrefix); 1353 if (!linePrefix.equals(": ")) { 1354 sb.append(" realtime"); 1355 } else { 1356 sb.append(": (nothing executed)"); 1357 } 1358 pw.println(sb.toString()); 1359 uidActivity = true; 1360 } 1361 } 1362 1363 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats(); 1364 if (sensors.size() > 0) { 1365 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent 1366 : sensors.entrySet()) { 1367 Uid.Sensor se = ent.getValue(); 1368 int sensorNumber = ent.getKey(); 1369 sb.setLength(0); 1370 sb.append(prefix); 1371 sb.append(" Sensor "); 1372 int handle = se.getHandle(); 1373 if (handle == Uid.Sensor.GPS) { 1374 sb.append("GPS"); 1375 } else { 1376 sb.append(handle); 1377 } 1378 sb.append(": "); 1379 1380 Timer timer = se.getSensorTime(); 1381 if (timer != null) { 1382 // Convert from microseconds to milliseconds with rounding 1383 long totalTime = (timer.getTotalTimeLocked( 1384 batteryRealtime, which) + 500) / 1000; 1385 int count = timer.getCountLocked(which); 1386 //timer.logState(); 1387 if (totalTime != 0) { 1388 formatTimeMs(sb, totalTime); 1389 sb.append("realtime ("); 1390 sb.append(count); 1391 sb.append(" times)"); 1392 } else { 1393 sb.append("(not used)"); 1394 } 1395 } else { 1396 sb.append("(not used)"); 1397 } 1398 1399 pw.println(sb.toString()); 1400 uidActivity = true; 1401 } 1402 } 1403 1404 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats(); 1405 if (processStats.size() > 0) { 1406 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent 1407 : processStats.entrySet()) { 1408 Uid.Proc ps = ent.getValue(); 1409 long userTime; 1410 long systemTime; 1411 int starts; 1412 1413 userTime = ps.getUserTime(which); 1414 systemTime = ps.getSystemTime(which); 1415 starts = ps.getStarts(which); 1416 1417 if (userTime != 0 || systemTime != 0 || starts != 0) { 1418 sb.setLength(0); 1419 sb.append(prefix); sb.append(" Proc "); 1420 sb.append(ent.getKey()); sb.append(":\n"); 1421 sb.append(prefix); sb.append(" CPU: "); 1422 formatTime(sb, userTime); sb.append("usr + "); 1423 formatTime(sb, systemTime); sb.append("krn\n"); 1424 sb.append(prefix); sb.append(" "); sb.append(starts); 1425 sb.append(" proc starts"); 1426 pw.println(sb.toString()); 1427 uidActivity = true; 1428 } 1429 } 1430 } 1431 1432 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats(); 1433 if (packageStats.size() > 0) { 1434 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent 1435 : packageStats.entrySet()) { 1436 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":"); 1437 boolean apkActivity = false; 1438 Uid.Pkg ps = ent.getValue(); 1439 int wakeups = ps.getWakeups(which); 1440 if (wakeups != 0) { 1441 pw.print(prefix); pw.print(" "); 1442 pw.print(wakeups); pw.println(" wakeup alarms"); 1443 apkActivity = true; 1444 } 1445 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats(); 1446 if (serviceStats.size() > 0) { 1447 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent 1448 : serviceStats.entrySet()) { 1449 BatteryStats.Uid.Pkg.Serv ss = sent.getValue(); 1450 long startTime = ss.getStartTime(batteryUptime, which); 1451 int starts = ss.getStarts(which); 1452 int launches = ss.getLaunches(which); 1453 if (startTime != 0 || starts != 0 || launches != 0) { 1454 sb.setLength(0); 1455 sb.append(prefix); sb.append(" Service "); 1456 sb.append(sent.getKey()); sb.append(":\n"); 1457 sb.append(prefix); sb.append(" Created for: "); 1458 formatTimeMs(sb, startTime / 1000); 1459 sb.append(" uptime\n"); 1460 sb.append(prefix); sb.append(" Starts: "); 1461 sb.append(starts); 1462 sb.append(", launches: "); sb.append(launches); 1463 pw.println(sb.toString()); 1464 apkActivity = true; 1465 } 1466 } 1467 } 1468 if (!apkActivity) { 1469 pw.print(prefix); pw.println(" (nothing executed)"); 1470 } 1471 uidActivity = true; 1472 } 1473 } 1474 if (!uidActivity) { 1475 pw.print(prefix); pw.println(" (nothing executed)"); 1476 } 1477 } 1478 } 1479 1480 /** 1481 * Dumps a human-readable summary of the battery statistics to the given PrintWriter. 1482 * 1483 * @param pw a Printer to receive the dump output. 1484 */ 1485 @SuppressWarnings("unused") 1486 public void dumpLocked(PrintWriter pw) { 1487 BatteryHistoryRecord rec = getHistory(); 1488 if (rec != null) { 1489 pw.println("Battery History:"); 1490 while (rec != null) { 1491 pw.print(" "); 1492 pw.print(rec.time); 1493 pw.print(" "); 1494 pw.print(rec.batteryLevel); 1495 pw.print(" "); 1496 pw.println(Integer.toHexString(rec.states)); 1497 rec = rec.next; 1498 } 1499 } 1500 1501 pw.println("Total Statistics (Current and Historic):"); 1502 pw.println(" System starts: " + getStartCount() 1503 + ", currently on battery: " + getIsOnBattery()); 1504 dumpLocked(pw, "", STATS_TOTAL, -1); 1505 pw.println(""); 1506 pw.println("Last Run Statistics (Previous run of system):"); 1507 dumpLocked(pw, "", STATS_LAST, -1); 1508 pw.println(""); 1509 pw.println("Current Battery Statistics (Currently running system):"); 1510 dumpLocked(pw, "", STATS_CURRENT, -1); 1511 pw.println(""); 1512 pw.println("Unplugged Statistics (Since last unplugged from power):"); 1513 dumpLocked(pw, "", STATS_UNPLUGGED, -1); 1514 } 1515 1516 @SuppressWarnings("unused") 1517 public void dumpCheckinLocked(PrintWriter pw, String[] args) { 1518 boolean isUnpluggedOnly = false; 1519 1520 for (String arg : args) { 1521 if ("-u".equals(arg)) { 1522 if (LOCAL_LOGV) Log.v("BatteryStats", "Dumping unplugged data"); 1523 isUnpluggedOnly = true; 1524 } 1525 } 1526 1527 if (isUnpluggedOnly) { 1528 dumpCheckinLocked(pw, STATS_UNPLUGGED, -1); 1529 } 1530 else { 1531 dumpCheckinLocked(pw, STATS_TOTAL, -1); 1532 dumpCheckinLocked(pw, STATS_LAST, -1); 1533 dumpCheckinLocked(pw, STATS_UNPLUGGED, -1); 1534 dumpCheckinLocked(pw, STATS_CURRENT, -1); 1535 } 1536 } 1537 1538} 1539