BatteryStats.java revision a1f1a3c573acd91024fda0ceb3b921c73b186963
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.ArrayList; 21import java.util.Collections; 22import java.util.Comparator; 23import java.util.Formatter; 24import java.util.List; 25import java.util.Map; 26 27import android.content.Context; 28import android.content.pm.ApplicationInfo; 29import android.telephony.SignalStrength; 30import android.text.format.DateFormat; 31import android.util.Printer; 32import android.util.SparseArray; 33import android.util.TimeUtils; 34import com.android.internal.os.BatterySipper; 35import com.android.internal.os.BatteryStatsHelper; 36 37/** 38 * A class providing access to battery usage statistics, including information on 39 * wakelocks, processes, packages, and services. All times are represented in microseconds 40 * except where indicated otherwise. 41 * @hide 42 */ 43public abstract class BatteryStats implements Parcelable { 44 45 private static final boolean LOCAL_LOGV = false; 46 47 /** @hide */ 48 public static final String SERVICE_NAME = "batterystats"; 49 50 /** 51 * A constant indicating a partial wake lock timer. 52 */ 53 public static final int WAKE_TYPE_PARTIAL = 0; 54 55 /** 56 * A constant indicating a full wake lock timer. 57 */ 58 public static final int WAKE_TYPE_FULL = 1; 59 60 /** 61 * A constant indicating a window wake lock timer. 62 */ 63 public static final int WAKE_TYPE_WINDOW = 2; 64 65 /** 66 * A constant indicating a sensor timer. 67 */ 68 public static final int SENSOR = 3; 69 70 /** 71 * A constant indicating a a wifi running timer 72 */ 73 public static final int WIFI_RUNNING = 4; 74 75 /** 76 * A constant indicating a full wifi lock timer 77 */ 78 public static final int FULL_WIFI_LOCK = 5; 79 80 /** 81 * A constant indicating a wifi scan 82 */ 83 public static final int WIFI_SCAN = 6; 84 85 /** 86 * A constant indicating a wifi multicast timer 87 */ 88 public static final int WIFI_MULTICAST_ENABLED = 7; 89 90 /** 91 * A constant indicating an audio turn on timer 92 */ 93 public static final int AUDIO_TURNED_ON = 7; 94 95 /** 96 * A constant indicating a video turn on timer 97 */ 98 public static final int VIDEO_TURNED_ON = 8; 99 100 /** 101 * A constant indicating a vibrator on timer 102 */ 103 public static final int VIBRATOR_ON = 9; 104 105 /** 106 * A constant indicating a foreground activity timer 107 */ 108 public static final int FOREGROUND_ACTIVITY = 10; 109 110 /** 111 * A constant indicating a wifi batched scan is active 112 */ 113 public static final int WIFI_BATCHED_SCAN = 11; 114 115 /** 116 * Include all of the data in the stats, including previously saved data. 117 */ 118 public static final int STATS_SINCE_CHARGED = 0; 119 120 /** 121 * Include only the last run in the stats. 122 */ 123 public static final int STATS_LAST = 1; 124 125 /** 126 * Include only the current run in the stats. 127 */ 128 public static final int STATS_CURRENT = 2; 129 130 /** 131 * Include only the run since the last time the device was unplugged in the stats. 132 */ 133 public static final int STATS_SINCE_UNPLUGGED = 3; 134 135 // NOTE: Update this list if you add/change any stats above. 136 // These characters are supposed to represent "total", "last", "current", 137 // and "unplugged". They were shortened for efficiency sake. 138 private static final String[] STAT_NAMES = { "t", "l", "c", "u" }; 139 140 /** 141 * Bump the version on this if the checkin format changes. 142 */ 143 private static final int BATTERY_STATS_CHECKIN_VERSION = 7; 144 145 private static final long BYTES_PER_KB = 1024; 146 private static final long BYTES_PER_MB = 1048576; // 1024^2 147 private static final long BYTES_PER_GB = 1073741824; //1024^3 148 149 150 private static final String UID_DATA = "uid"; 151 private static final String APK_DATA = "apk"; 152 private static final String PROCESS_DATA = "pr"; 153 private static final String SENSOR_DATA = "sr"; 154 private static final String VIBRATOR_DATA = "vib"; 155 private static final String FOREGROUND_DATA = "fg"; 156 private static final String WAKELOCK_DATA = "wl"; 157 private static final String KERNEL_WAKELOCK_DATA = "kwl"; 158 private static final String NETWORK_DATA = "nt"; 159 private static final String USER_ACTIVITY_DATA = "ua"; 160 private static final String BATTERY_DATA = "bt"; 161 private static final String BATTERY_DISCHARGE_DATA = "dc"; 162 private static final String BATTERY_LEVEL_DATA = "lv"; 163 private static final String WIFI_DATA = "wfl"; 164 private static final String MISC_DATA = "m"; 165 private static final String GLOBAL_NETWORK_DATA = "gn"; 166 private static final String HISTORY_STRING_POOL = "hsp"; 167 private static final String HISTORY_DATA = "h"; 168 private static final String SCREEN_BRIGHTNESS_DATA = "br"; 169 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt"; 170 private static final String SIGNAL_SCANNING_TIME_DATA = "sst"; 171 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc"; 172 private static final String DATA_CONNECTION_TIME_DATA = "dct"; 173 private static final String DATA_CONNECTION_COUNT_DATA = "dcc"; 174 private static final String WIFI_STATE_TIME_DATA = "wst"; 175 private static final String WIFI_STATE_COUNT_DATA = "wsc"; 176 private static final String BLUETOOTH_STATE_TIME_DATA = "bst"; 177 private static final String BLUETOOTH_STATE_COUNT_DATA = "bsc"; 178 private static final String POWER_USE_SUMMARY_DATA = "pws"; 179 private static final String POWER_USE_ITEM_DATA = "pwi"; 180 181 private final StringBuilder mFormatBuilder = new StringBuilder(32); 182 private final Formatter mFormatter = new Formatter(mFormatBuilder); 183 184 /** 185 * State for keeping track of counting information. 186 */ 187 public static abstract class Counter { 188 189 /** 190 * Returns the count associated with this Counter for the 191 * selected type of statistics. 192 * 193 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT 194 */ 195 public abstract int getCountLocked(int which); 196 197 /** 198 * Temporary for debugging. 199 */ 200 public abstract void logState(Printer pw, String prefix); 201 } 202 203 /** 204 * State for keeping track of timing information. 205 */ 206 public static abstract class Timer { 207 208 /** 209 * Returns the count associated with this Timer for the 210 * selected type of statistics. 211 * 212 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT 213 */ 214 public abstract int getCountLocked(int which); 215 216 /** 217 * Returns the total time in microseconds associated with this Timer for the 218 * selected type of statistics. 219 * 220 * @param batteryRealtime system realtime on battery in microseconds 221 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT 222 * @return a time in microseconds 223 */ 224 public abstract long getTotalTimeLocked(long batteryRealtime, int which); 225 226 /** 227 * Temporary for debugging. 228 */ 229 public abstract void logState(Printer pw, String prefix); 230 } 231 232 /** 233 * The statistics associated with a particular uid. 234 */ 235 public static abstract class Uid { 236 237 /** 238 * Returns a mapping containing wakelock statistics. 239 * 240 * @return a Map from Strings to Uid.Wakelock objects. 241 */ 242 public abstract Map<String, ? extends Wakelock> getWakelockStats(); 243 244 /** 245 * The statistics associated with a particular wake lock. 246 */ 247 public static abstract class Wakelock { 248 public abstract Timer getWakeTime(int type); 249 } 250 251 /** 252 * Returns a mapping containing sensor statistics. 253 * 254 * @return a Map from Integer sensor ids to Uid.Sensor objects. 255 */ 256 public abstract Map<Integer, ? extends Sensor> getSensorStats(); 257 258 /** 259 * Returns a mapping containing active process data. 260 */ 261 public abstract SparseArray<? extends Pid> getPidStats(); 262 263 /** 264 * Returns a mapping containing process statistics. 265 * 266 * @return a Map from Strings to Uid.Proc objects. 267 */ 268 public abstract Map<String, ? extends Proc> getProcessStats(); 269 270 /** 271 * Returns a mapping containing package statistics. 272 * 273 * @return a Map from Strings to Uid.Pkg objects. 274 */ 275 public abstract Map<String, ? extends Pkg> getPackageStats(); 276 277 /** 278 * {@hide} 279 */ 280 public abstract int getUid(); 281 282 public abstract void noteWifiRunningLocked(long elapsedRealtime); 283 public abstract void noteWifiStoppedLocked(long elapsedRealtime); 284 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime); 285 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime); 286 public abstract void noteWifiScanStartedLocked(long elapsedRealtime); 287 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime); 288 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime); 289 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime); 290 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime); 291 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime); 292 public abstract void noteAudioTurnedOnLocked(long elapsedRealtime); 293 public abstract void noteAudioTurnedOffLocked(long elapsedRealtime); 294 public abstract void noteVideoTurnedOnLocked(long elapsedRealtime); 295 public abstract void noteVideoTurnedOffLocked(long elapsedRealtime); 296 public abstract void noteActivityResumedLocked(long elapsedRealtime); 297 public abstract void noteActivityPausedLocked(long elapsedRealtime); 298 public abstract long getWifiRunningTime(long batteryRealtime, int which); 299 public abstract long getFullWifiLockTime(long batteryRealtime, int which); 300 public abstract long getWifiScanTime(long batteryRealtime, int which); 301 public abstract long getWifiBatchedScanTime(int csphBin, long batteryRealtime, int which); 302 public abstract long getWifiMulticastTime(long batteryRealtime, 303 int which); 304 public abstract long getAudioTurnedOnTime(long batteryRealtime, int which); 305 public abstract long getVideoTurnedOnTime(long batteryRealtime, int which); 306 public abstract Timer getForegroundActivityTimer(); 307 public abstract Timer getVibratorOnTimer(); 308 309 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5; 310 311 /** 312 * Note that these must match the constants in android.os.PowerManager. 313 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must 314 * also be bumped. 315 */ 316 static final String[] USER_ACTIVITY_TYPES = { 317 "other", "button", "touch" 318 }; 319 320 public static final int NUM_USER_ACTIVITY_TYPES = 3; 321 322 public abstract void noteUserActivityLocked(int type); 323 public abstract boolean hasUserActivity(); 324 public abstract int getUserActivityCount(int type, int which); 325 326 public abstract boolean hasNetworkActivity(); 327 public abstract long getNetworkActivityBytes(int type, int which); 328 public abstract long getNetworkActivityPackets(int type, int which); 329 330 public static abstract class Sensor { 331 /* 332 * FIXME: it's not correct to use this magic value because it 333 * could clash with a sensor handle (which are defined by 334 * the sensor HAL, and therefore out of our control 335 */ 336 // Magic sensor number for the GPS. 337 public static final int GPS = -10000; 338 339 public abstract int getHandle(); 340 341 public abstract Timer getSensorTime(); 342 } 343 344 public class Pid { 345 public long mWakeSum; 346 public long mWakeStart; 347 } 348 349 /** 350 * The statistics associated with a particular process. 351 */ 352 public static abstract class Proc { 353 354 public static class ExcessivePower { 355 public static final int TYPE_WAKE = 1; 356 public static final int TYPE_CPU = 2; 357 358 public int type; 359 public long overTime; 360 public long usedTime; 361 } 362 363 /** 364 * Returns true if this process is still active in the battery stats. 365 */ 366 public abstract boolean isActive(); 367 368 /** 369 * Returns the total time (in 1/100 sec) spent executing in user code. 370 * 371 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 372 */ 373 public abstract long getUserTime(int which); 374 375 /** 376 * Returns the total time (in 1/100 sec) spent executing in system code. 377 * 378 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 379 */ 380 public abstract long getSystemTime(int which); 381 382 /** 383 * Returns the number of times the process has been started. 384 * 385 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 386 */ 387 public abstract int getStarts(int which); 388 389 /** 390 * Returns the cpu time spent in microseconds while the process was in the foreground. 391 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED 392 * @return foreground cpu time in microseconds 393 */ 394 public abstract long getForegroundTime(int which); 395 396 /** 397 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed. 398 * @param speedStep the index of the CPU speed. This is not the actual speed of the 399 * CPU. 400 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED 401 * @see BatteryStats#getCpuSpeedSteps() 402 */ 403 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which); 404 405 public abstract int countExcessivePowers(); 406 407 public abstract ExcessivePower getExcessivePower(int i); 408 } 409 410 /** 411 * The statistics associated with a particular package. 412 */ 413 public static abstract class Pkg { 414 415 /** 416 * Returns the number of times this package has done something that could wake up the 417 * device from sleep. 418 * 419 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 420 */ 421 public abstract int getWakeups(int which); 422 423 /** 424 * Returns a mapping containing service statistics. 425 */ 426 public abstract Map<String, ? extends Serv> getServiceStats(); 427 428 /** 429 * The statistics associated with a particular service. 430 */ 431 public abstract class Serv { 432 433 /** 434 * Returns the amount of time spent started. 435 * 436 * @param batteryUptime elapsed uptime on battery in microseconds. 437 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 438 * @return 439 */ 440 public abstract long getStartTime(long batteryUptime, int which); 441 442 /** 443 * Returns the total number of times startService() has been called. 444 * 445 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 446 */ 447 public abstract int getStarts(int which); 448 449 /** 450 * Returns the total number times the service has been launched. 451 * 452 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 453 */ 454 public abstract int getLaunches(int which); 455 } 456 } 457 } 458 459 public final static class HistoryTag { 460 public String string; 461 public int uid; 462 463 public int poolIdx; 464 465 public void setTo(HistoryTag o) { 466 string = o.string; 467 uid = o.uid; 468 poolIdx = o.poolIdx; 469 } 470 471 public void setTo(String _string, int _uid) { 472 string = _string; 473 uid = _uid; 474 poolIdx = -1; 475 } 476 477 public void writeToParcel(Parcel dest, int flags) { 478 dest.writeString(string); 479 dest.writeInt(uid); 480 } 481 482 public void readFromParcel(Parcel src) { 483 string = src.readString(); 484 uid = src.readInt(); 485 poolIdx = -1; 486 } 487 488 @Override 489 public boolean equals(Object o) { 490 if (this == o) return true; 491 if (o == null || getClass() != o.getClass()) return false; 492 493 HistoryTag that = (HistoryTag) o; 494 495 if (uid != that.uid) return false; 496 if (!string.equals(that.string)) return false; 497 498 return true; 499 } 500 501 @Override 502 public int hashCode() { 503 int result = string.hashCode(); 504 result = 31 * result + uid; 505 return result; 506 } 507 } 508 509 public final static class HistoryItem implements Parcelable { 510 public HistoryItem next; 511 512 public long time; 513 514 public static final byte CMD_UPDATE = 0; // These can be written as deltas 515 public static final byte CMD_NULL = -1; 516 public static final byte CMD_START = 4; 517 public static final byte CMD_OVERFLOW = 5; 518 519 public byte cmd = CMD_NULL; 520 521 /** 522 * Return whether the command code is a delta data update. 523 */ 524 public boolean isDeltaData() { 525 return cmd == CMD_UPDATE; 526 } 527 528 public byte batteryLevel; 529 public byte batteryStatus; 530 public byte batteryHealth; 531 public byte batteryPlugType; 532 533 public short batteryTemperature; 534 public char batteryVoltage; 535 536 // Constants from SCREEN_BRIGHTNESS_* 537 public static final int STATE_BRIGHTNESS_SHIFT = 0; 538 public static final int STATE_BRIGHTNESS_MASK = 0x7; 539 // Constants from SIGNAL_STRENGTH_* 540 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3; 541 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT; 542 // Constants from ServiceState.STATE_* 543 public static final int STATE_PHONE_STATE_SHIFT = 6; 544 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT; 545 // Constants from DATA_CONNECTION_* 546 public static final int STATE_DATA_CONNECTION_SHIFT = 9; 547 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT; 548 549 // These states always appear directly in the first int token 550 // of a delta change; they should be ones that change relatively 551 // frequently. 552 public static final int STATE_WAKE_LOCK_FLAG = 1<<31; 553 public static final int STATE_SENSOR_ON_FLAG = 1<<30; 554 public static final int STATE_GPS_ON_FLAG = 1<<29; 555 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28; 556 public static final int STATE_WIFI_SCAN_FLAG = 1<<27; 557 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26; 558 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25; 559 public static final int STATE_WIFI_RUNNING_FLAG = 1<<24; 560 // These are on the lower bits used for the command; if they change 561 // we need to write another int of data. 562 public static final int STATE_PHONE_SCANNING_FLAG = 1<<23; 563 public static final int STATE_AUDIO_ON_FLAG = 1<<22; 564 public static final int STATE_VIDEO_ON_FLAG = 1<<21; 565 public static final int STATE_SCREEN_ON_FLAG = 1<<20; 566 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; 567 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18; 568 public static final int STATE_WIFI_ON_FLAG = 1<<17; 569 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16; 570 571 public static final int MOST_INTERESTING_STATES = 572 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG 573 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG; 574 575 public int states; 576 577 // The wake lock that was acquired at this point. 578 public HistoryTag wakelockTag; 579 580 public static final int EVENT_FLAG_START = 0x8000; 581 public static final int EVENT_FLAG_FINISH = 0x4000; 582 583 // No event in this item. 584 public static final int EVENT_NONE = 0x0000; 585 // Event is about a process that is running. 586 public static final int EVENT_PROC = 0x0001; 587 // Event is about an application package that is in the foreground. 588 public static final int EVENT_FOREGROUND = 0x0002; 589 // Event is about an application package that is at the top of the screen. 590 public static final int EVENT_TOP = 0x0003; 591 // Event is about an application package that is at the top of the screen. 592 public static final int EVENT_SYNC = 0x0004; 593 // Number of event types. 594 public static final int EVENT_COUNT = 0x0005; 595 596 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START; 597 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH; 598 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START; 599 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH; 600 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START; 601 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH; 602 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START; 603 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH; 604 605 // For CMD_EVENT. 606 public int eventCode; 607 public HistoryTag eventTag; 608 609 // Meta-data when reading. 610 public int numReadInts; 611 612 // Pre-allocated objects. 613 public final HistoryTag localWakelockTag = new HistoryTag(); 614 public final HistoryTag localEventTag = new HistoryTag(); 615 616 public HistoryItem() { 617 } 618 619 public HistoryItem(long time, Parcel src) { 620 this.time = time; 621 numReadInts = 2; 622 readFromParcel(src); 623 } 624 625 public int describeContents() { 626 return 0; 627 } 628 629 public void writeToParcel(Parcel dest, int flags) { 630 dest.writeLong(time); 631 int bat = (((int)cmd)&0xff) 632 | ((((int)batteryLevel)<<8)&0xff00) 633 | ((((int)batteryStatus)<<16)&0xf0000) 634 | ((((int)batteryHealth)<<20)&0xf00000) 635 | ((((int)batteryPlugType)<<24)&0xf000000); 636 dest.writeInt(bat); 637 bat = (((int)batteryTemperature)&0xffff) 638 | ((((int)batteryVoltage)<<16)&0xffff0000); 639 dest.writeInt(bat); 640 dest.writeInt(states); 641 if (wakelockTag != null) { 642 dest.writeInt(1); 643 wakelockTag.writeToParcel(dest, flags); 644 } else { 645 dest.writeInt(0); 646 } 647 dest.writeInt(eventCode); 648 if (eventCode != EVENT_NONE) { 649 dest.writeInt(eventCode); 650 eventTag.writeToParcel(dest, flags); 651 } 652 } 653 654 public void readFromParcel(Parcel src) { 655 int start = src.dataPosition(); 656 int bat = src.readInt(); 657 cmd = (byte)(bat&0xff); 658 batteryLevel = (byte)((bat>>8)&0xff); 659 batteryStatus = (byte)((bat>>16)&0xf); 660 batteryHealth = (byte)((bat>>20)&0xf); 661 batteryPlugType = (byte)((bat>>24)&0xf); 662 bat = src.readInt(); 663 batteryTemperature = (short)(bat&0xffff); 664 batteryVoltage = (char)((bat>>16)&0xffff); 665 states = src.readInt(); 666 if (src.readInt() != 0) { 667 wakelockTag = localWakelockTag; 668 wakelockTag.readFromParcel(src); 669 } else { 670 wakelockTag = null; 671 } 672 eventCode = src.readInt(); 673 if (eventCode != EVENT_NONE) { 674 eventTag = localEventTag; 675 eventTag.readFromParcel(src); 676 } 677 numReadInts += (src.dataPosition()-start)/4; 678 } 679 680 public void clear() { 681 time = 0; 682 cmd = CMD_NULL; 683 batteryLevel = 0; 684 batteryStatus = 0; 685 batteryHealth = 0; 686 batteryPlugType = 0; 687 batteryTemperature = 0; 688 batteryVoltage = 0; 689 states = 0; 690 wakelockTag = null; 691 eventCode = EVENT_NONE; 692 eventTag = null; 693 } 694 695 public void setTo(HistoryItem o) { 696 time = o.time; 697 cmd = o.cmd; 698 setToCommon(o); 699 } 700 701 public void setTo(long time, byte cmd, HistoryItem o) { 702 this.time = time; 703 this.cmd = cmd; 704 setToCommon(o); 705 } 706 707 private void setToCommon(HistoryItem o) { 708 batteryLevel = o.batteryLevel; 709 batteryStatus = o.batteryStatus; 710 batteryHealth = o.batteryHealth; 711 batteryPlugType = o.batteryPlugType; 712 batteryTemperature = o.batteryTemperature; 713 batteryVoltage = o.batteryVoltage; 714 states = o.states; 715 if (o.wakelockTag != null) { 716 wakelockTag = localWakelockTag; 717 wakelockTag.setTo(o.wakelockTag); 718 } else { 719 wakelockTag = null; 720 } 721 eventCode = o.eventCode; 722 if (o.eventTag != null) { 723 eventTag = localEventTag; 724 eventTag.setTo(o.eventTag); 725 } else { 726 eventTag = null; 727 } 728 } 729 730 public boolean sameNonEvent(HistoryItem o) { 731 return batteryLevel == o.batteryLevel 732 && batteryStatus == o.batteryStatus 733 && batteryHealth == o.batteryHealth 734 && batteryPlugType == o.batteryPlugType 735 && batteryTemperature == o.batteryTemperature 736 && batteryVoltage == o.batteryVoltage 737 && states == o.states; 738 } 739 740 public boolean same(HistoryItem o) { 741 if (!sameNonEvent(o) || eventCode != o.eventCode) { 742 return false; 743 } 744 if (wakelockTag != o.wakelockTag) { 745 if (wakelockTag == null || o.wakelockTag == null) { 746 return false; 747 } 748 if (!wakelockTag.equals(o.wakelockTag)) { 749 return false; 750 } 751 } 752 if (eventTag != o.eventTag) { 753 if (eventTag == null || o.eventTag == null) { 754 return false; 755 } 756 if (!eventTag.equals(o.eventTag)) { 757 return false; 758 } 759 } 760 return true; 761 } 762 } 763 764 public static final class BitDescription { 765 public final int mask; 766 public final int shift; 767 public final String name; 768 public final String shortName; 769 public final String[] values; 770 public final String[] shortValues; 771 772 public BitDescription(int mask, String name, String shortName) { 773 this.mask = mask; 774 this.shift = -1; 775 this.name = name; 776 this.shortName = shortName; 777 this.values = null; 778 this.shortValues = null; 779 } 780 781 public BitDescription(int mask, int shift, String name, String shortName, 782 String[] values, String[] shortValues) { 783 this.mask = mask; 784 this.shift = shift; 785 this.name = name; 786 this.shortName = shortName; 787 this.values = values; 788 this.shortValues = shortValues; 789 } 790 } 791 792 public abstract int getHistoryTotalSize(); 793 794 public abstract int getHistoryUsedSize(); 795 796 public abstract boolean startIteratingHistoryLocked(); 797 798 public abstract int getHistoryStringPoolSize(); 799 800 public abstract int getHistoryStringPoolBytes(); 801 802 public abstract String getHistoryTagPoolString(int index); 803 804 public abstract int getHistoryTagPoolUid(int index); 805 806 public abstract boolean getNextHistoryLocked(HistoryItem out); 807 808 public abstract void finishIteratingHistoryLocked(); 809 810 public abstract boolean startIteratingOldHistoryLocked(); 811 812 public abstract boolean getNextOldHistoryLocked(HistoryItem out); 813 814 public abstract void finishIteratingOldHistoryLocked(); 815 816 /** 817 * Return the base time offset for the battery history. 818 */ 819 public abstract long getHistoryBaseTime(); 820 821 /** 822 * Returns the number of times the device has been started. 823 */ 824 public abstract int getStartCount(); 825 826 /** 827 * Returns the time in microseconds that the screen has been on while the device was 828 * running on battery. 829 * 830 * {@hide} 831 */ 832 public abstract long getScreenOnTime(long batteryRealtime, int which); 833 834 public static final int SCREEN_BRIGHTNESS_DARK = 0; 835 public static final int SCREEN_BRIGHTNESS_DIM = 1; 836 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2; 837 public static final int SCREEN_BRIGHTNESS_LIGHT = 3; 838 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4; 839 840 static final String[] SCREEN_BRIGHTNESS_NAMES = { 841 "dark", "dim", "medium", "light", "bright" 842 }; 843 844 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = { 845 "0", "1", "2", "3", "4" 846 }; 847 848 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5; 849 850 /** 851 * Returns the time in microseconds that the screen has been on with 852 * the given brightness 853 * 854 * {@hide} 855 */ 856 public abstract long getScreenBrightnessTime(int brightnessBin, 857 long batteryRealtime, int which); 858 859 public abstract int getInputEventCount(int which); 860 861 /** 862 * Returns the time in microseconds that the phone has been on while the device was 863 * running on battery. 864 * 865 * {@hide} 866 */ 867 public abstract long getPhoneOnTime(long batteryRealtime, int which); 868 869 /** 870 * Returns the time in microseconds that the phone has been running with 871 * the given signal strength. 872 * 873 * {@hide} 874 */ 875 public abstract long getPhoneSignalStrengthTime(int strengthBin, 876 long batteryRealtime, int which); 877 878 /** 879 * Returns the time in microseconds that the phone has been trying to 880 * acquire a signal. 881 * 882 * {@hide} 883 */ 884 public abstract long getPhoneSignalScanningTime( 885 long batteryRealtime, int which); 886 887 /** 888 * Returns the number of times the phone has entered the given signal strength. 889 * 890 * {@hide} 891 */ 892 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which); 893 894 /** 895 * Returns the time in microseconds that the mobile network has been active 896 * (in a high power state). 897 * 898 * {@hide} 899 */ 900 public abstract long getMobileRadioActiveTime(long batteryRealtime, int which); 901 902 903 public static final int DATA_CONNECTION_NONE = 0; 904 public static final int DATA_CONNECTION_GPRS = 1; 905 public static final int DATA_CONNECTION_EDGE = 2; 906 public static final int DATA_CONNECTION_UMTS = 3; 907 public static final int DATA_CONNECTION_CDMA = 4; 908 public static final int DATA_CONNECTION_EVDO_0 = 5; 909 public static final int DATA_CONNECTION_EVDO_A = 6; 910 public static final int DATA_CONNECTION_1xRTT = 7; 911 public static final int DATA_CONNECTION_HSDPA = 8; 912 public static final int DATA_CONNECTION_HSUPA = 9; 913 public static final int DATA_CONNECTION_HSPA = 10; 914 public static final int DATA_CONNECTION_IDEN = 11; 915 public static final int DATA_CONNECTION_EVDO_B = 12; 916 public static final int DATA_CONNECTION_LTE = 13; 917 public static final int DATA_CONNECTION_EHRPD = 14; 918 public static final int DATA_CONNECTION_HSPAP = 15; 919 public static final int DATA_CONNECTION_OTHER = 16; 920 921 static final String[] DATA_CONNECTION_NAMES = { 922 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A", 923 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte", 924 "ehrpd", "hspap", "other" 925 }; 926 927 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1; 928 929 /** 930 * Returns the time in microseconds that the phone has been running with 931 * the given data connection. 932 * 933 * {@hide} 934 */ 935 public abstract long getPhoneDataConnectionTime(int dataType, 936 long batteryRealtime, int which); 937 938 /** 939 * Returns the number of times the phone has entered the given data 940 * connection type. 941 * 942 * {@hide} 943 */ 944 public abstract int getPhoneDataConnectionCount(int dataType, int which); 945 946 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS 947 = new BitDescription[] { 948 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"), 949 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"), 950 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"), 951 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"), 952 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"), 953 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"), 954 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"), 955 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"), 956 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"), 957 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"), 958 new BitDescription(HistoryItem.STATE_VIDEO_ON_FLAG, "video", "v"), 959 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"), 960 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"), 961 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"), 962 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"), 963 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"), 964 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK, 965 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn", 966 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES), 967 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK, 968 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst", 969 new String[] {"in", "out", "emergency", "off"}, 970 new String[] {"in", "out", "em", "off"}), 971 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK, 972 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss", 973 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] { 974 "0", "1", "2", "3", "4" 975 }), 976 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK, 977 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb", 978 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES), 979 }; 980 981 public static final String[] HISTORY_EVENT_NAMES = new String[] { 982 "null", "proc", "fg", "top", "sync" 983 }; 984 985 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] { 986 "Enl", "Epr", "Efg", "Etp", "Esy" 987 }; 988 989 /** 990 * Returns the time in microseconds that wifi has been on while the device was 991 * running on battery. 992 * 993 * {@hide} 994 */ 995 public abstract long getWifiOnTime(long batteryRealtime, int which); 996 997 /** 998 * Returns the time in microseconds that wifi has been on and the driver has 999 * been in the running state while the device was running on battery. 1000 * 1001 * {@hide} 1002 */ 1003 public abstract long getGlobalWifiRunningTime(long batteryRealtime, int which); 1004 1005 public static final int WIFI_STATE_OFF = 0; 1006 public static final int WIFI_STATE_OFF_SCANNING = 1; 1007 public static final int WIFI_STATE_ON_NO_NETWORKS = 2; 1008 public static final int WIFI_STATE_ON_DISCONNECTED = 3; 1009 public static final int WIFI_STATE_ON_CONNECTED_STA = 4; 1010 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5; 1011 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6; 1012 public static final int WIFI_STATE_SOFT_AP = 7; 1013 1014 static final String[] WIFI_STATE_NAMES = { 1015 "off", "scanning", "no_net", "disconn", 1016 "sta", "p2p", "sta_p2p", "soft_ap" 1017 }; 1018 1019 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1; 1020 1021 /** 1022 * Returns the time in microseconds that WiFi has been running in the given state. 1023 * 1024 * {@hide} 1025 */ 1026 public abstract long getWifiStateTime(int wifiState, 1027 long batteryRealtime, int which); 1028 1029 /** 1030 * Returns the number of times that WiFi has entered the given state. 1031 * 1032 * {@hide} 1033 */ 1034 public abstract int getWifiStateCount(int wifiState, int which); 1035 1036 /** 1037 * Returns the time in microseconds that bluetooth has been on while the device was 1038 * running on battery. 1039 * 1040 * {@hide} 1041 */ 1042 public abstract long getBluetoothOnTime(long batteryRealtime, int which); 1043 1044 public abstract int getBluetoothPingCount(); 1045 1046 public static final int BLUETOOTH_STATE_INACTIVE = 0; 1047 public static final int BLUETOOTH_STATE_LOW = 1; 1048 public static final int BLUETOOTH_STATE_MEDIUM = 2; 1049 public static final int BLUETOOTH_STATE_HIGH = 3; 1050 1051 static final String[] BLUETOOTH_STATE_NAMES = { 1052 "inactive", "low", "med", "high" 1053 }; 1054 1055 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1; 1056 1057 /** 1058 * Returns the time in microseconds that Bluetooth has been running in the 1059 * given active state. 1060 * 1061 * {@hide} 1062 */ 1063 public abstract long getBluetoothStateTime(int bluetoothState, 1064 long batteryRealtime, int which); 1065 1066 /** 1067 * Returns the number of times that Bluetooth has entered the given active state. 1068 * 1069 * {@hide} 1070 */ 1071 public abstract int getBluetoothStateCount(int bluetoothState, int which); 1072 1073 public static final int NETWORK_MOBILE_RX_DATA = 0; 1074 public static final int NETWORK_MOBILE_TX_DATA = 1; 1075 public static final int NETWORK_WIFI_RX_DATA = 2; 1076 public static final int NETWORK_WIFI_TX_DATA = 3; 1077 1078 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1; 1079 1080 public abstract long getNetworkActivityBytes(int type, int which); 1081 public abstract long getNetworkActivityPackets(int type, int which); 1082 1083 /** 1084 * Return the wall clock time when battery stats data collection started. 1085 */ 1086 public abstract long getStartClockTime(); 1087 1088 /** 1089 * Return whether we are currently running on battery. 1090 */ 1091 public abstract boolean getIsOnBattery(); 1092 1093 /** 1094 * Returns a SparseArray containing the statistics for each uid. 1095 */ 1096 public abstract SparseArray<? extends Uid> getUidStats(); 1097 1098 /** 1099 * Returns the current battery uptime in microseconds. 1100 * 1101 * @param curTime the amount of elapsed realtime in microseconds. 1102 */ 1103 public abstract long getBatteryUptime(long curTime); 1104 1105 /** 1106 * Returns the current battery realtime in microseconds. 1107 * 1108 * @param curTime the amount of elapsed realtime in microseconds. 1109 */ 1110 public abstract long getBatteryRealtime(long curTime); 1111 1112 /** 1113 * Returns the battery percentage level at the last time the device was unplugged from power, or 1114 * the last time it booted on battery power. 1115 */ 1116 public abstract int getDischargeStartLevel(); 1117 1118 /** 1119 * Returns the current battery percentage level if we are in a discharge cycle, otherwise 1120 * returns the level at the last plug event. 1121 */ 1122 public abstract int getDischargeCurrentLevel(); 1123 1124 /** 1125 * Get the amount the battery has discharged since the stats were 1126 * last reset after charging, as a lower-end approximation. 1127 */ 1128 public abstract int getLowDischargeAmountSinceCharge(); 1129 1130 /** 1131 * Get the amount the battery has discharged since the stats were 1132 * last reset after charging, as an upper-end approximation. 1133 */ 1134 public abstract int getHighDischargeAmountSinceCharge(); 1135 1136 /** 1137 * Get the amount the battery has discharged while the screen was on, 1138 * since the last time power was unplugged. 1139 */ 1140 public abstract int getDischargeAmountScreenOn(); 1141 1142 /** 1143 * Get the amount the battery has discharged while the screen was on, 1144 * since the last time the device was charged. 1145 */ 1146 public abstract int getDischargeAmountScreenOnSinceCharge(); 1147 1148 /** 1149 * Get the amount the battery has discharged while the screen was off, 1150 * since the last time power was unplugged. 1151 */ 1152 public abstract int getDischargeAmountScreenOff(); 1153 1154 /** 1155 * Get the amount the battery has discharged while the screen was off, 1156 * since the last time the device was charged. 1157 */ 1158 public abstract int getDischargeAmountScreenOffSinceCharge(); 1159 1160 /** 1161 * Returns the total, last, or current battery uptime in microseconds. 1162 * 1163 * @param curTime the elapsed realtime in microseconds. 1164 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 1165 */ 1166 public abstract long computeBatteryUptime(long curTime, int which); 1167 1168 /** 1169 * Returns the total, last, or current battery realtime in microseconds. 1170 * 1171 * @param curTime the current elapsed realtime in microseconds. 1172 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 1173 */ 1174 public abstract long computeBatteryRealtime(long curTime, int which); 1175 1176 /** 1177 * Returns the total, last, or current uptime in microseconds. 1178 * 1179 * @param curTime the current elapsed realtime in microseconds. 1180 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 1181 */ 1182 public abstract long computeUptime(long curTime, int which); 1183 1184 /** 1185 * Returns the total, last, or current realtime in microseconds. 1186 * * 1187 * @param curTime the current elapsed realtime in microseconds. 1188 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 1189 */ 1190 public abstract long computeRealtime(long curTime, int which); 1191 1192 public abstract Map<String, ? extends Timer> getKernelWakelockStats(); 1193 1194 /** Returns the number of different speeds that the CPU can run at */ 1195 public abstract int getCpuSpeedSteps(); 1196 1197 public abstract void writeToParcelWithoutUids(Parcel out, int flags); 1198 1199 private final static void formatTimeRaw(StringBuilder out, long seconds) { 1200 long days = seconds / (60 * 60 * 24); 1201 if (days != 0) { 1202 out.append(days); 1203 out.append("d "); 1204 } 1205 long used = days * 60 * 60 * 24; 1206 1207 long hours = (seconds - used) / (60 * 60); 1208 if (hours != 0 || used != 0) { 1209 out.append(hours); 1210 out.append("h "); 1211 } 1212 used += hours * 60 * 60; 1213 1214 long mins = (seconds-used) / 60; 1215 if (mins != 0 || used != 0) { 1216 out.append(mins); 1217 out.append("m "); 1218 } 1219 used += mins * 60; 1220 1221 if (seconds != 0 || used != 0) { 1222 out.append(seconds-used); 1223 out.append("s "); 1224 } 1225 } 1226 1227 private final static void formatTime(StringBuilder sb, long time) { 1228 long sec = time / 100; 1229 formatTimeRaw(sb, sec); 1230 sb.append((time - (sec * 100)) * 10); 1231 sb.append("ms "); 1232 } 1233 1234 private final static void formatTimeMs(StringBuilder sb, long time) { 1235 long sec = time / 1000; 1236 formatTimeRaw(sb, sec); 1237 sb.append(time - (sec * 1000)); 1238 sb.append("ms "); 1239 } 1240 1241 private final String formatRatioLocked(long num, long den) { 1242 if (den == 0L) { 1243 return "--%"; 1244 } 1245 float perc = ((float)num) / ((float)den) * 100; 1246 mFormatBuilder.setLength(0); 1247 mFormatter.format("%.1f%%", perc); 1248 return mFormatBuilder.toString(); 1249 } 1250 1251 private final String formatBytesLocked(long bytes) { 1252 mFormatBuilder.setLength(0); 1253 1254 if (bytes < BYTES_PER_KB) { 1255 return bytes + "B"; 1256 } else if (bytes < BYTES_PER_MB) { 1257 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB); 1258 return mFormatBuilder.toString(); 1259 } else if (bytes < BYTES_PER_GB){ 1260 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB); 1261 return mFormatBuilder.toString(); 1262 } else { 1263 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB); 1264 return mFormatBuilder.toString(); 1265 } 1266 } 1267 1268 private static long computeWakeLock(Timer timer, long batteryRealtime, int which) { 1269 if (timer != null) { 1270 // Convert from microseconds to milliseconds with rounding 1271 long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which); 1272 long totalTimeMillis = (totalTimeMicros + 500) / 1000; 1273 return totalTimeMillis; 1274 } 1275 return 0; 1276 } 1277 1278 /** 1279 * 1280 * @param sb a StringBuilder object. 1281 * @param timer a Timer object contining the wakelock times. 1282 * @param batteryRealtime the current on-battery time in microseconds. 1283 * @param name the name of the wakelock. 1284 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 1285 * @param linePrefix a String to be prepended to each line of output. 1286 * @return the line prefix 1287 */ 1288 private static final String printWakeLock(StringBuilder sb, Timer timer, 1289 long batteryRealtime, String name, int which, String linePrefix) { 1290 1291 if (timer != null) { 1292 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which); 1293 1294 int count = timer.getCountLocked(which); 1295 if (totalTimeMillis != 0) { 1296 sb.append(linePrefix); 1297 formatTimeMs(sb, totalTimeMillis); 1298 if (name != null) { 1299 sb.append(name); 1300 sb.append(' '); 1301 } 1302 sb.append('('); 1303 sb.append(count); 1304 sb.append(" times)"); 1305 return ", "; 1306 } 1307 } 1308 return linePrefix; 1309 } 1310 1311 /** 1312 * Checkin version of wakelock printer. Prints simple comma-separated list. 1313 * 1314 * @param sb a StringBuilder object. 1315 * @param timer a Timer object contining the wakelock times. 1316 * @param now the current time in microseconds. 1317 * @param name the name of the wakelock. 1318 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT. 1319 * @param linePrefix a String to be prepended to each line of output. 1320 * @return the line prefix 1321 */ 1322 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer, long now, 1323 String name, int which, String linePrefix) { 1324 long totalTimeMicros = 0; 1325 int count = 0; 1326 if (timer != null) { 1327 totalTimeMicros = timer.getTotalTimeLocked(now, which); 1328 count = timer.getCountLocked(which); 1329 } 1330 sb.append(linePrefix); 1331 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding 1332 sb.append(','); 1333 sb.append(name != null ? name + "," : ""); 1334 sb.append(count); 1335 return ","; 1336 } 1337 1338 /** 1339 * Dump a comma-separated line of values for terse checkin mode. 1340 * 1341 * @param pw the PageWriter to dump log to 1342 * @param category category of data (e.g. "total", "last", "unplugged", "current" ) 1343 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network") 1344 * @param args type-dependent data arguments 1345 */ 1346 private static final void dumpLine(PrintWriter pw, int uid, String category, String type, 1347 Object... args ) { 1348 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(','); 1349 pw.print(uid); pw.print(','); 1350 pw.print(category); pw.print(','); 1351 pw.print(type); 1352 1353 for (Object arg : args) { 1354 pw.print(','); 1355 pw.print(arg); 1356 } 1357 pw.println(); 1358 } 1359 1360 /** 1361 * Checkin server version of dump to produce more compact, computer-readable log. 1362 * 1363 * NOTE: all times are expressed in 'ms'. 1364 */ 1365 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) { 1366 final long rawUptime = SystemClock.uptimeMillis() * 1000; 1367 final long rawRealtime = SystemClock.elapsedRealtime() * 1000; 1368 final long batteryUptime = getBatteryUptime(rawUptime); 1369 final long batteryRealtime = getBatteryRealtime(rawRealtime); 1370 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which); 1371 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which); 1372 final long totalRealtime = computeRealtime(rawRealtime, which); 1373 final long totalUptime = computeUptime(rawUptime, which); 1374 final long screenOnTime = getScreenOnTime(batteryRealtime, which); 1375 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which); 1376 final long wifiOnTime = getWifiOnTime(batteryRealtime, which); 1377 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which); 1378 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which); 1379 1380 StringBuilder sb = new StringBuilder(128); 1381 1382 SparseArray<? extends Uid> uidStats = getUidStats(); 1383 final int NU = uidStats.size(); 1384 1385 String category = STAT_NAMES[which]; 1386 1387 // Dump "battery" stat 1388 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA, 1389 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A", 1390 whichBatteryRealtime / 1000, whichBatteryUptime / 1000, 1391 totalRealtime / 1000, totalUptime / 1000, 1392 getStartClockTime()); 1393 1394 // Calculate wakelock times across all uids. 1395 long fullWakeLockTimeTotal = 0; 1396 long partialWakeLockTimeTotal = 0; 1397 1398 for (int iu = 0; iu < NU; iu++) { 1399 Uid u = uidStats.valueAt(iu); 1400 1401 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats(); 1402 if (wakelocks.size() > 0) { 1403 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent 1404 : wakelocks.entrySet()) { 1405 Uid.Wakelock wl = ent.getValue(); 1406 1407 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL); 1408 if (fullWakeTimer != null) { 1409 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(batteryRealtime, which); 1410 } 1411 1412 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL); 1413 if (partialWakeTimer != null) { 1414 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked( 1415 batteryRealtime, which); 1416 } 1417 } 1418 } 1419 } 1420 1421 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which); 1422 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which); 1423 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which); 1424 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which); 1425 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which); 1426 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which); 1427 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which); 1428 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which); 1429 1430 // Dump network stats 1431 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA, 1432 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes, 1433 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets); 1434 1435 // Dump misc stats 1436 dumpLine(pw, 0 /* uid */, category, MISC_DATA, 1437 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000, 1438 wifiRunningTime / 1000, bluetoothOnTime / 1000, 1439 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes, 1440 fullWakeLockTimeTotal, partialWakeLockTimeTotal, 1441 getInputEventCount(which), getMobileRadioActiveTime(batteryRealtime, which)); 1442 1443 // Dump screen brightness stats 1444 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS]; 1445 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { 1446 args[i] = getScreenBrightnessTime(i, batteryRealtime, which) / 1000; 1447 } 1448 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args); 1449 1450 // Dump signal strength stats 1451 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS]; 1452 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) { 1453 args[i] = getPhoneSignalStrengthTime(i, batteryRealtime, which) / 1000; 1454 } 1455 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args); 1456 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA, 1457 getPhoneSignalScanningTime(batteryRealtime, which) / 1000); 1458 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) { 1459 args[i] = getPhoneSignalStrengthCount(i, which); 1460 } 1461 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args); 1462 1463 // Dump network type stats 1464 args = new Object[NUM_DATA_CONNECTION_TYPES]; 1465 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { 1466 args[i] = getPhoneDataConnectionTime(i, batteryRealtime, which) / 1000; 1467 } 1468 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args); 1469 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { 1470 args[i] = getPhoneDataConnectionCount(i, which); 1471 } 1472 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args); 1473 1474 // Dump wifi state stats 1475 args = new Object[NUM_WIFI_STATES]; 1476 for (int i=0; i<NUM_WIFI_STATES; i++) { 1477 args[i] = getWifiStateTime(i, batteryRealtime, which) / 1000; 1478 } 1479 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args); 1480 for (int i=0; i<NUM_WIFI_STATES; i++) { 1481 args[i] = getWifiStateCount(i, which); 1482 } 1483 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args); 1484 1485 // Dump bluetooth state stats 1486 args = new Object[NUM_BLUETOOTH_STATES]; 1487 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) { 1488 args[i] = getBluetoothStateTime(i, batteryRealtime, which) / 1000; 1489 } 1490 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args); 1491 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) { 1492 args[i] = getBluetoothStateCount(i, which); 1493 } 1494 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args); 1495 1496 if (which == STATS_SINCE_UNPLUGGED) { 1497 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(), 1498 getDischargeCurrentLevel()); 1499 } 1500 1501 if (which == STATS_SINCE_UNPLUGGED) { 1502 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA, 1503 getDischargeStartLevel()-getDischargeCurrentLevel(), 1504 getDischargeStartLevel()-getDischargeCurrentLevel(), 1505 getDischargeAmountScreenOn(), getDischargeAmountScreenOff()); 1506 } else { 1507 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA, 1508 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(), 1509 getDischargeAmountScreenOn(), getDischargeAmountScreenOff()); 1510 } 1511 1512 if (reqUid < 0) { 1513 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats(); 1514 if (kernelWakelocks.size() > 0) { 1515 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) { 1516 sb.setLength(0); 1517 printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, ""); 1518 1519 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(), 1520 sb.toString()); 1521 } 1522 } 1523 } 1524 1525 BatteryStatsHelper helper = new BatteryStatsHelper(context); 1526 helper.create(this); 1527 helper.refreshStats(which, UserHandle.USER_ALL); 1528 List<BatterySipper> sippers = helper.getUsageList(); 1529 if (sippers != null && sippers.size() > 0) { 1530 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA, 1531 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()), 1532 BatteryStatsHelper.makemAh(helper.getComputedPower()), 1533 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()), 1534 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower())); 1535 for (int i=0; i<sippers.size(); i++) { 1536 BatterySipper bs = sippers.get(i); 1537 int uid = 0; 1538 String label; 1539 switch (bs.drainType) { 1540 case IDLE: 1541 label="idle"; 1542 break; 1543 case CELL: 1544 label="cell"; 1545 break; 1546 case PHONE: 1547 label="phone"; 1548 break; 1549 case WIFI: 1550 label="wifi"; 1551 break; 1552 case BLUETOOTH: 1553 label="blue"; 1554 break; 1555 case SCREEN: 1556 label="scrn"; 1557 break; 1558 case APP: 1559 uid = bs.uidObj.getUid(); 1560 label = "uid"; 1561 break; 1562 case USER: 1563 uid = UserHandle.getUid(bs.userId, 0); 1564 label = "user"; 1565 break; 1566 case UNACCOUNTED: 1567 label = "unacc"; 1568 break; 1569 case OVERCOUNTED: 1570 label = "over"; 1571 break; 1572 default: 1573 label = "???"; 1574 } 1575 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label, 1576 BatteryStatsHelper.makemAh(bs.value)); 1577 } 1578 } 1579 1580 for (int iu = 0; iu < NU; iu++) { 1581 final int uid = uidStats.keyAt(iu); 1582 if (reqUid >= 0 && uid != reqUid) { 1583 continue; 1584 } 1585 Uid u = uidStats.valueAt(iu); 1586 // Dump Network stats per uid, if any 1587 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which); 1588 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which); 1589 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which); 1590 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which); 1591 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which); 1592 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which); 1593 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which); 1594 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which); 1595 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which); 1596 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which); 1597 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which); 1598 1599 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0 1600 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0 1601 || wifiPacketsTx > 0) { 1602 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx, 1603 wifiBytesRx, wifiBytesTx, 1604 mobilePacketsRx, mobilePacketsTx, 1605 wifiPacketsRx, wifiPacketsTx); 1606 } 1607 1608 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 1609 || uidWifiRunningTime != 0) { 1610 dumpLine(pw, uid, category, WIFI_DATA, 1611 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime); 1612 } 1613 1614 if (u.hasUserActivity()) { 1615 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES]; 1616 boolean hasData = false; 1617 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) { 1618 int val = u.getUserActivityCount(i, which); 1619 args[i] = val; 1620 if (val != 0) hasData = true; 1621 } 1622 if (hasData) { 1623 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args); 1624 } 1625 } 1626 1627 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats(); 1628 if (wakelocks.size() > 0) { 1629 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent 1630 : wakelocks.entrySet()) { 1631 Uid.Wakelock wl = ent.getValue(); 1632 String linePrefix = ""; 1633 sb.setLength(0); 1634 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL), 1635 batteryRealtime, "f", which, linePrefix); 1636 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), 1637 batteryRealtime, "p", which, linePrefix); 1638 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), 1639 batteryRealtime, "w", which, linePrefix); 1640 1641 // Only log if we had at lease one wakelock... 1642 if (sb.length() > 0) { 1643 String name = ent.getKey(); 1644 if (name.indexOf(',') >= 0) { 1645 name = name.replace(',', '_'); 1646 } 1647 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString()); 1648 } 1649 } 1650 } 1651 1652 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats(); 1653 if (sensors.size() > 0) { 1654 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent 1655 : sensors.entrySet()) { 1656 Uid.Sensor se = ent.getValue(); 1657 int sensorNumber = ent.getKey(); 1658 Timer timer = se.getSensorTime(); 1659 if (timer != null) { 1660 // Convert from microseconds to milliseconds with rounding 1661 long totalTime = (timer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000; 1662 int count = timer.getCountLocked(which); 1663 if (totalTime != 0) { 1664 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count); 1665 } 1666 } 1667 } 1668 } 1669 1670 Timer vibTimer = u.getVibratorOnTimer(); 1671 if (vibTimer != null) { 1672 // Convert from microseconds to milliseconds with rounding 1673 long totalTime = (vibTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000; 1674 int count = vibTimer.getCountLocked(which); 1675 if (totalTime != 0) { 1676 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count); 1677 } 1678 } 1679 1680 Timer fgTimer = u.getForegroundActivityTimer(); 1681 if (fgTimer != null) { 1682 // Convert from microseconds to milliseconds with rounding 1683 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000; 1684 int count = fgTimer.getCountLocked(which); 1685 if (totalTime != 0) { 1686 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count); 1687 } 1688 } 1689 1690 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats(); 1691 if (processStats.size() > 0) { 1692 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent 1693 : processStats.entrySet()) { 1694 Uid.Proc ps = ent.getValue(); 1695 1696 final long userMillis = ps.getUserTime(which) * 10; 1697 final long systemMillis = ps.getSystemTime(which) * 10; 1698 final long foregroundMillis = ps.getForegroundTime(which) * 10; 1699 final long starts = ps.getStarts(which); 1700 1701 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0 1702 || starts != 0) { 1703 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis, 1704 systemMillis, foregroundMillis, starts); 1705 } 1706 } 1707 } 1708 1709 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats(); 1710 if (packageStats.size() > 0) { 1711 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent 1712 : packageStats.entrySet()) { 1713 1714 Uid.Pkg ps = ent.getValue(); 1715 int wakeups = ps.getWakeups(which); 1716 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats(); 1717 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent 1718 : serviceStats.entrySet()) { 1719 BatteryStats.Uid.Pkg.Serv ss = sent.getValue(); 1720 long startTime = ss.getStartTime(batteryUptime, which); 1721 int starts = ss.getStarts(which); 1722 int launches = ss.getLaunches(which); 1723 if (startTime != 0 || starts != 0 || launches != 0) { 1724 dumpLine(pw, uid, category, APK_DATA, 1725 wakeups, // wakeup alarms 1726 ent.getKey(), // Apk 1727 sent.getKey(), // service 1728 startTime / 1000, // time spent started, in ms 1729 starts, 1730 launches); 1731 } 1732 } 1733 } 1734 } 1735 } 1736 } 1737 1738 static final class TimerEntry { 1739 final String mName; 1740 final int mId; 1741 final BatteryStats.Timer mTimer; 1742 final long mTime; 1743 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) { 1744 mName = name; 1745 mId = id; 1746 mTimer = timer; 1747 mTime = time; 1748 } 1749 } 1750 1751 private void printmAh(PrintWriter printer, double power) { 1752 printer.print(BatteryStatsHelper.makemAh(power)); 1753 } 1754 1755 @SuppressWarnings("unused") 1756 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which, 1757 int reqUid) { 1758 final long rawUptime = SystemClock.uptimeMillis() * 1000; 1759 final long rawRealtime = SystemClock.elapsedRealtime() * 1000; 1760 final long batteryUptime = getBatteryUptime(rawUptime); 1761 final long batteryRealtime = getBatteryRealtime(rawRealtime); 1762 1763 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which); 1764 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which); 1765 final long totalRealtime = computeRealtime(rawRealtime, which); 1766 final long totalUptime = computeUptime(rawUptime, which); 1767 1768 StringBuilder sb = new StringBuilder(128); 1769 1770 SparseArray<? extends Uid> uidStats = getUidStats(); 1771 final int NU = uidStats.size(); 1772 1773 sb.setLength(0); 1774 sb.append(prefix); 1775 sb.append(" Time on battery: "); 1776 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("("); 1777 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime)); 1778 sb.append(") realtime, "); 1779 formatTimeMs(sb, whichBatteryUptime / 1000); 1780 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime)); 1781 sb.append(") uptime"); 1782 pw.println(sb.toString()); 1783 sb.setLength(0); 1784 sb.append(prefix); 1785 sb.append(" Total run time: "); 1786 formatTimeMs(sb, totalRealtime / 1000); 1787 sb.append("realtime, "); 1788 formatTimeMs(sb, totalUptime / 1000); 1789 sb.append("uptime, "); 1790 pw.println(sb.toString()); 1791 pw.print(" Start clock time: "); 1792 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString()); 1793 1794 final long screenOnTime = getScreenOnTime(batteryRealtime, which); 1795 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which); 1796 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which); 1797 final long wifiOnTime = getWifiOnTime(batteryRealtime, which); 1798 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which); 1799 sb.setLength(0); 1800 sb.append(prefix); 1801 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000); 1802 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime)); 1803 sb.append("), Input events: "); sb.append(getInputEventCount(which)); 1804 sb.append(", Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000); 1805 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime)); 1806 sb.append(")"); 1807 pw.println(sb.toString()); 1808 sb.setLength(0); 1809 sb.append(prefix); 1810 sb.append(" Screen brightnesses: "); 1811 boolean didOne = false; 1812 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) { 1813 final long time = getScreenBrightnessTime(i, batteryRealtime, which); 1814 if (time == 0) { 1815 continue; 1816 } 1817 if (didOne) sb.append(", "); 1818 didOne = true; 1819 sb.append(SCREEN_BRIGHTNESS_NAMES[i]); 1820 sb.append(" "); 1821 formatTimeMs(sb, time/1000); 1822 sb.append("("); 1823 sb.append(formatRatioLocked(time, screenOnTime)); 1824 sb.append(")"); 1825 } 1826 if (!didOne) sb.append("No activity"); 1827 pw.println(sb.toString()); 1828 1829 // Calculate wakelock times across all uids. 1830 long fullWakeLockTimeTotalMicros = 0; 1831 long partialWakeLockTimeTotalMicros = 0; 1832 1833 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() { 1834 @Override 1835 public int compare(TimerEntry lhs, TimerEntry rhs) { 1836 long lhsTime = lhs.mTime; 1837 long rhsTime = rhs.mTime; 1838 if (lhsTime < rhsTime) { 1839 return 1; 1840 } 1841 if (lhsTime > rhsTime) { 1842 return -1; 1843 } 1844 return 0; 1845 } 1846 }; 1847 1848 if (reqUid < 0) { 1849 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats(); 1850 if (kernelWakelocks.size() > 0) { 1851 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>(); 1852 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) { 1853 BatteryStats.Timer timer = ent.getValue(); 1854 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which); 1855 if (totalTimeMillis > 0) { 1856 timers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis)); 1857 } 1858 } 1859 Collections.sort(timers, timerComparator); 1860 for (int i=0; i<timers.size(); i++) { 1861 TimerEntry timer = timers.get(i); 1862 String linePrefix = ": "; 1863 sb.setLength(0); 1864 sb.append(prefix); 1865 sb.append(" Kernel Wake lock "); 1866 sb.append(timer.mName); 1867 linePrefix = printWakeLock(sb, timer.mTimer, batteryRealtime, null, 1868 which, linePrefix); 1869 if (!linePrefix.equals(": ")) { 1870 sb.append(" realtime"); 1871 // Only print out wake locks that were held 1872 pw.println(sb.toString()); 1873 } 1874 } 1875 } 1876 } 1877 1878 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>(); 1879 1880 for (int iu = 0; iu < NU; iu++) { 1881 Uid u = uidStats.valueAt(iu); 1882 1883 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats(); 1884 if (wakelocks.size() > 0) { 1885 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent 1886 : wakelocks.entrySet()) { 1887 Uid.Wakelock wl = ent.getValue(); 1888 1889 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL); 1890 if (fullWakeTimer != null) { 1891 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked( 1892 batteryRealtime, which); 1893 } 1894 1895 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL); 1896 if (partialWakeTimer != null) { 1897 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked( 1898 batteryRealtime, which); 1899 if (totalTimeMicros > 0) { 1900 if (reqUid < 0) { 1901 // Only show the ordered list of all wake 1902 // locks if the caller is not asking for data 1903 // about a specific uid. 1904 timers.add(new TimerEntry(ent.getKey(), u.getUid(), 1905 partialWakeTimer, totalTimeMicros)); 1906 } 1907 partialWakeLockTimeTotalMicros += totalTimeMicros; 1908 } 1909 } 1910 } 1911 } 1912 } 1913 1914 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which); 1915 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which); 1916 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which); 1917 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which); 1918 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which); 1919 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which); 1920 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which); 1921 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which); 1922 1923 pw.print(prefix); 1924 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes)); 1925 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes)); 1926 pw.print(" (packets received "); pw.print(mobileRxTotalPackets); 1927 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")"); 1928 pw.print(prefix); 1929 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes)); 1930 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes)); 1931 pw.print(" (packets received "); pw.print(wifiRxTotalPackets); 1932 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")"); 1933 sb.setLength(0); 1934 sb.append(prefix); 1935 sb.append(" Total full wakelock time: "); formatTimeMs(sb, 1936 (fullWakeLockTimeTotalMicros + 500) / 1000); 1937 sb.append(", Total partial wakelock time: "); formatTimeMs(sb, 1938 (partialWakeLockTimeTotalMicros + 500) / 1000); 1939 pw.println(sb.toString()); 1940 1941 sb.setLength(0); 1942 sb.append(prefix); 1943 sb.append(" Signal levels:"); 1944 didOne = false; 1945 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) { 1946 final long time = getPhoneSignalStrengthTime(i, batteryRealtime, which); 1947 if (time == 0) { 1948 continue; 1949 } 1950 sb.append("\n "); 1951 didOne = true; 1952 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]); 1953 sb.append(" "); 1954 formatTimeMs(sb, time/1000); 1955 sb.append("("); 1956 sb.append(formatRatioLocked(time, whichBatteryRealtime)); 1957 sb.append(") "); 1958 sb.append(getPhoneSignalStrengthCount(i, which)); 1959 sb.append("x"); 1960 } 1961 if (!didOne) sb.append(" (no activity)"); 1962 pw.println(sb.toString()); 1963 1964 sb.setLength(0); 1965 sb.append(prefix); 1966 sb.append(" Signal scanning time: "); 1967 formatTimeMs(sb, getPhoneSignalScanningTime(batteryRealtime, which) / 1000); 1968 pw.println(sb.toString()); 1969 1970 sb.setLength(0); 1971 sb.append(prefix); 1972 sb.append(" Radio types:"); 1973 didOne = false; 1974 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) { 1975 final long time = getPhoneDataConnectionTime(i, batteryRealtime, which); 1976 if (time == 0) { 1977 continue; 1978 } 1979 sb.append("\n "); 1980 didOne = true; 1981 sb.append(DATA_CONNECTION_NAMES[i]); 1982 sb.append(" "); 1983 formatTimeMs(sb, time/1000); 1984 sb.append("("); 1985 sb.append(formatRatioLocked(time, whichBatteryRealtime)); 1986 sb.append(") "); 1987 sb.append(getPhoneDataConnectionCount(i, which)); 1988 sb.append("x"); 1989 } 1990 if (!didOne) sb.append(" (no activity)"); 1991 pw.println(sb.toString()); 1992 1993 sb.setLength(0); 1994 sb.append(prefix); 1995 sb.append(" Mobile radio active time: "); 1996 formatTimeMs(sb, getMobileRadioActiveTime(batteryRealtime, which) / 1000); 1997 pw.println(sb.toString()); 1998 1999 sb.setLength(0); 2000 sb.append(prefix); 2001 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000); 2002 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime)); 2003 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000); 2004 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime)); 2005 sb.append(")"); 2006 pw.println(sb.toString()); 2007 2008 sb.setLength(0); 2009 sb.append(prefix); 2010 sb.append(" Wifi states:"); 2011 didOne = false; 2012 for (int i=0; i<NUM_WIFI_STATES; i++) { 2013 final long time = getWifiStateTime(i, batteryRealtime, which); 2014 if (time == 0) { 2015 continue; 2016 } 2017 sb.append("\n "); 2018 didOne = true; 2019 sb.append(WIFI_STATE_NAMES[i]); 2020 sb.append(" "); 2021 formatTimeMs(sb, time/1000); 2022 sb.append("("); 2023 sb.append(formatRatioLocked(time, whichBatteryRealtime)); 2024 sb.append(") "); 2025 sb.append(getPhoneDataConnectionCount(i, which)); 2026 sb.append("x"); 2027 } 2028 if (!didOne) sb.append(" (no activity)"); 2029 pw.println(sb.toString()); 2030 2031 sb.setLength(0); 2032 sb.append(prefix); 2033 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000); 2034 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime)); 2035 sb.append(")"); 2036 pw.println(sb.toString()); 2037 2038 sb.setLength(0); 2039 sb.append(prefix); 2040 sb.append(" Bluetooth states:"); 2041 didOne = false; 2042 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) { 2043 final long time = getBluetoothStateTime(i, batteryRealtime, which); 2044 if (time == 0) { 2045 continue; 2046 } 2047 sb.append("\n "); 2048 didOne = true; 2049 sb.append(BLUETOOTH_STATE_NAMES[i]); 2050 sb.append(" "); 2051 formatTimeMs(sb, time/1000); 2052 sb.append("("); 2053 sb.append(formatRatioLocked(time, whichBatteryRealtime)); 2054 sb.append(") "); 2055 sb.append(getPhoneDataConnectionCount(i, which)); 2056 sb.append("x"); 2057 } 2058 if (!didOne) sb.append(" (no activity)"); 2059 pw.println(sb.toString()); 2060 2061 pw.println(); 2062 2063 if (which == STATS_SINCE_UNPLUGGED) { 2064 if (getIsOnBattery()) { 2065 pw.print(prefix); pw.println(" Device is currently unplugged"); 2066 pw.print(prefix); pw.print(" Discharge cycle start level: "); 2067 pw.println(getDischargeStartLevel()); 2068 pw.print(prefix); pw.print(" Discharge cycle current level: "); 2069 pw.println(getDischargeCurrentLevel()); 2070 } else { 2071 pw.print(prefix); pw.println(" Device is currently plugged into power"); 2072 pw.print(prefix); pw.print(" Last discharge cycle start level: "); 2073 pw.println(getDischargeStartLevel()); 2074 pw.print(prefix); pw.print(" Last discharge cycle end level: "); 2075 pw.println(getDischargeCurrentLevel()); 2076 } 2077 pw.print(prefix); pw.print(" Amount discharged while screen on: "); 2078 pw.println(getDischargeAmountScreenOn()); 2079 pw.print(prefix); pw.print(" Amount discharged while screen off: "); 2080 pw.println(getDischargeAmountScreenOff()); 2081 pw.println(" "); 2082 } else { 2083 pw.print(prefix); pw.println(" Device battery use since last full charge"); 2084 pw.print(prefix); pw.print(" Amount discharged (lower bound): "); 2085 pw.println(getLowDischargeAmountSinceCharge()); 2086 pw.print(prefix); pw.print(" Amount discharged (upper bound): "); 2087 pw.println(getHighDischargeAmountSinceCharge()); 2088 pw.print(prefix); pw.print(" Amount discharged while screen on: "); 2089 pw.println(getDischargeAmountScreenOnSinceCharge()); 2090 pw.print(prefix); pw.print(" Amount discharged while screen off: "); 2091 pw.println(getDischargeAmountScreenOffSinceCharge()); 2092 pw.println(); 2093 } 2094 2095 BatteryStatsHelper helper = new BatteryStatsHelper(context); 2096 helper.create(this); 2097 helper.refreshStats(which, UserHandle.USER_ALL); 2098 List<BatterySipper> sippers = helper.getUsageList(); 2099 if (sippers != null && sippers.size() > 0) { 2100 pw.print(prefix); pw.println(" Estimated power use (mAh):"); 2101 pw.print(prefix); pw.print(" Capacity: "); 2102 printmAh(pw, helper.getPowerProfile().getBatteryCapacity()); 2103 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower()); 2104 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower()); 2105 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower()); 2106 pw.println(); 2107 for (int i=0; i<sippers.size(); i++) { 2108 BatterySipper bs = sippers.get(i); 2109 switch (bs.drainType) { 2110 case IDLE: 2111 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value); 2112 pw.println(); 2113 break; 2114 case CELL: 2115 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value); 2116 pw.println(); 2117 break; 2118 case PHONE: 2119 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value); 2120 pw.println(); 2121 break; 2122 case WIFI: 2123 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value); 2124 pw.println(); 2125 break; 2126 case BLUETOOTH: 2127 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value); 2128 pw.println(); 2129 break; 2130 case SCREEN: 2131 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value); 2132 pw.println(); 2133 break; 2134 case APP: 2135 pw.print(prefix); pw.print(" Uid "); pw.print(bs.uidObj.getUid()); 2136 pw.print(": "); printmAh(pw, bs.value); pw.println(); 2137 break; 2138 case USER: 2139 pw.print(prefix); pw.print(" User "); pw.print(bs.userId); 2140 pw.print(": "); printmAh(pw, bs.value); pw.println(); 2141 break; 2142 case UNACCOUNTED: 2143 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value); 2144 pw.println(); 2145 break; 2146 case OVERCOUNTED: 2147 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value); 2148 pw.println(); 2149 break; 2150 } 2151 } 2152 pw.println(); 2153 } 2154 2155 if (timers.size() > 0) { 2156 Collections.sort(timers, timerComparator); 2157 pw.print(prefix); pw.println(" All partial wake locks:"); 2158 for (int i=0; i<timers.size(); i++) { 2159 TimerEntry timer = timers.get(i); 2160 sb.setLength(0); 2161 sb.append(" Wake lock "); 2162 UserHandle.formatUid(sb, timer.mId); 2163 sb.append(" "); 2164 sb.append(timer.mName); 2165 printWakeLock(sb, timer.mTimer, batteryRealtime, null, which, ": "); 2166 sb.append(" realtime"); 2167 pw.println(sb.toString()); 2168 } 2169 timers.clear(); 2170 pw.println(); 2171 } 2172 2173 for (int iu=0; iu<NU; iu++) { 2174 final int uid = uidStats.keyAt(iu); 2175 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) { 2176 continue; 2177 } 2178 2179 Uid u = uidStats.valueAt(iu); 2180 2181 pw.print(prefix); 2182 pw.print(" "); 2183 UserHandle.formatUid(pw, uid); 2184 pw.println(":"); 2185 boolean uidActivity = false; 2186 2187 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which); 2188 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which); 2189 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which); 2190 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which); 2191 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which); 2192 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which); 2193 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which); 2194 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which); 2195 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which); 2196 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which); 2197 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which); 2198 2199 if (mobileRxBytes > 0 || mobileTxBytes > 0 2200 || mobileRxPackets > 0 || mobileTxPackets > 0) { 2201 pw.print(prefix); pw.print(" Mobile network: "); 2202 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, "); 2203 pw.print(formatBytesLocked(mobileTxBytes)); 2204 pw.print(" sent (packets "); pw.print(mobileRxPackets); 2205 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)"); 2206 } 2207 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) { 2208 pw.print(prefix); pw.print(" Wi-Fi network: "); 2209 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, "); 2210 pw.print(formatBytesLocked(wifiTxBytes)); 2211 pw.print(" sent (packets "); pw.print(wifiRxPackets); 2212 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)"); 2213 } 2214 2215 if (u.hasUserActivity()) { 2216 boolean hasData = false; 2217 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) { 2218 int val = u.getUserActivityCount(i, which); 2219 if (val != 0) { 2220 if (!hasData) { 2221 sb.setLength(0); 2222 sb.append(" User activity: "); 2223 hasData = true; 2224 } else { 2225 sb.append(", "); 2226 } 2227 sb.append(val); 2228 sb.append(" "); 2229 sb.append(Uid.USER_ACTIVITY_TYPES[i]); 2230 } 2231 } 2232 if (hasData) { 2233 pw.println(sb.toString()); 2234 } 2235 } 2236 2237 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 2238 || uidWifiRunningTime != 0) { 2239 sb.setLength(0); 2240 sb.append(prefix); sb.append(" Wifi Running: "); 2241 formatTimeMs(sb, uidWifiRunningTime / 1000); 2242 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime, 2243 whichBatteryRealtime)); sb.append(")\n"); 2244 sb.append(prefix); sb.append(" Full Wifi Lock: "); 2245 formatTimeMs(sb, fullWifiLockOnTime / 1000); 2246 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime, 2247 whichBatteryRealtime)); sb.append(")\n"); 2248 sb.append(prefix); sb.append(" Wifi Scan: "); 2249 formatTimeMs(sb, wifiScanTime / 1000); 2250 sb.append("("); sb.append(formatRatioLocked(wifiScanTime, 2251 whichBatteryRealtime)); sb.append(")"); 2252 pw.println(sb.toString()); 2253 } 2254 2255 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats(); 2256 if (wakelocks.size() > 0) { 2257 long totalFull = 0, totalPartial = 0, totalWindow = 0; 2258 int count = 0; 2259 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent 2260 : wakelocks.entrySet()) { 2261 Uid.Wakelock wl = ent.getValue(); 2262 String linePrefix = ": "; 2263 sb.setLength(0); 2264 sb.append(prefix); 2265 sb.append(" Wake lock "); 2266 sb.append(ent.getKey()); 2267 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), batteryRealtime, 2268 "full", which, linePrefix); 2269 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), batteryRealtime, 2270 "partial", which, linePrefix); 2271 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), batteryRealtime, 2272 "window", which, linePrefix); 2273 if (!linePrefix.equals(": ")) { 2274 sb.append(" realtime"); 2275 // Only print out wake locks that were held 2276 pw.println(sb.toString()); 2277 uidActivity = true; 2278 count++; 2279 } 2280 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL), 2281 batteryRealtime, which); 2282 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL), 2283 batteryRealtime, which); 2284 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW), 2285 batteryRealtime, which); 2286 } 2287 if (count > 1) { 2288 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) { 2289 sb.setLength(0); 2290 sb.append(prefix); 2291 sb.append(" TOTAL wake: "); 2292 boolean needComma = false; 2293 if (totalFull != 0) { 2294 needComma = true; 2295 formatTimeMs(sb, totalFull); 2296 sb.append("full"); 2297 } 2298 if (totalPartial != 0) { 2299 if (needComma) { 2300 sb.append(", "); 2301 } 2302 needComma = true; 2303 formatTimeMs(sb, totalPartial); 2304 sb.append("partial"); 2305 } 2306 if (totalWindow != 0) { 2307 if (needComma) { 2308 sb.append(", "); 2309 } 2310 needComma = true; 2311 formatTimeMs(sb, totalWindow); 2312 sb.append("window"); 2313 } 2314 sb.append(" realtime"); 2315 pw.println(sb.toString()); 2316 } 2317 } 2318 } 2319 2320 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats(); 2321 if (sensors.size() > 0) { 2322 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent 2323 : sensors.entrySet()) { 2324 Uid.Sensor se = ent.getValue(); 2325 int sensorNumber = ent.getKey(); 2326 sb.setLength(0); 2327 sb.append(prefix); 2328 sb.append(" Sensor "); 2329 int handle = se.getHandle(); 2330 if (handle == Uid.Sensor.GPS) { 2331 sb.append("GPS"); 2332 } else { 2333 sb.append(handle); 2334 } 2335 sb.append(": "); 2336 2337 Timer timer = se.getSensorTime(); 2338 if (timer != null) { 2339 // Convert from microseconds to milliseconds with rounding 2340 long totalTime = (timer.getTotalTimeLocked( 2341 batteryRealtime, which) + 500) / 1000; 2342 int count = timer.getCountLocked(which); 2343 //timer.logState(); 2344 if (totalTime != 0) { 2345 formatTimeMs(sb, totalTime); 2346 sb.append("realtime ("); 2347 sb.append(count); 2348 sb.append(" times)"); 2349 } else { 2350 sb.append("(not used)"); 2351 } 2352 } else { 2353 sb.append("(not used)"); 2354 } 2355 2356 pw.println(sb.toString()); 2357 uidActivity = true; 2358 } 2359 } 2360 2361 Timer vibTimer = u.getVibratorOnTimer(); 2362 if (vibTimer != null) { 2363 // Convert from microseconds to milliseconds with rounding 2364 long totalTime = (vibTimer.getTotalTimeLocked( 2365 batteryRealtime, which) + 500) / 1000; 2366 int count = vibTimer.getCountLocked(which); 2367 //timer.logState(); 2368 if (totalTime != 0) { 2369 sb.setLength(0); 2370 sb.append(prefix); 2371 sb.append(" Vibrator: "); 2372 formatTimeMs(sb, totalTime); 2373 sb.append("realtime ("); 2374 sb.append(count); 2375 sb.append(" times)"); 2376 pw.println(sb.toString()); 2377 uidActivity = true; 2378 } 2379 } 2380 2381 Timer fgTimer = u.getForegroundActivityTimer(); 2382 if (fgTimer != null) { 2383 // Convert from microseconds to milliseconds with rounding 2384 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000; 2385 int count = fgTimer.getCountLocked(which); 2386 if (totalTime != 0) { 2387 sb.setLength(0); 2388 sb.append(prefix); 2389 sb.append(" Foreground activities: "); 2390 formatTimeMs(sb, totalTime); 2391 sb.append("realtime ("); 2392 sb.append(count); 2393 sb.append(" times)"); 2394 pw.println(sb.toString()); 2395 uidActivity = true; 2396 } 2397 } 2398 2399 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats(); 2400 if (processStats.size() > 0) { 2401 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent 2402 : processStats.entrySet()) { 2403 Uid.Proc ps = ent.getValue(); 2404 long userTime; 2405 long systemTime; 2406 long foregroundTime; 2407 int starts; 2408 int numExcessive; 2409 2410 userTime = ps.getUserTime(which); 2411 systemTime = ps.getSystemTime(which); 2412 foregroundTime = ps.getForegroundTime(which); 2413 starts = ps.getStarts(which); 2414 numExcessive = which == STATS_SINCE_CHARGED 2415 ? ps.countExcessivePowers() : 0; 2416 2417 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0 2418 || numExcessive != 0) { 2419 sb.setLength(0); 2420 sb.append(prefix); sb.append(" Proc "); 2421 sb.append(ent.getKey()); sb.append(":\n"); 2422 sb.append(prefix); sb.append(" CPU: "); 2423 formatTime(sb, userTime); sb.append("usr + "); 2424 formatTime(sb, systemTime); sb.append("krn ; "); 2425 formatTime(sb, foregroundTime); sb.append("fg"); 2426 if (starts != 0) { 2427 sb.append("\n"); sb.append(prefix); sb.append(" "); 2428 sb.append(starts); sb.append(" proc starts"); 2429 } 2430 pw.println(sb.toString()); 2431 for (int e=0; e<numExcessive; e++) { 2432 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e); 2433 if (ew != null) { 2434 pw.print(prefix); pw.print(" * Killed for "); 2435 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) { 2436 pw.print("wake lock"); 2437 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) { 2438 pw.print("cpu"); 2439 } else { 2440 pw.print("unknown"); 2441 } 2442 pw.print(" use: "); 2443 TimeUtils.formatDuration(ew.usedTime, pw); 2444 pw.print(" over "); 2445 TimeUtils.formatDuration(ew.overTime, pw); 2446 if (ew.overTime != 0) { 2447 pw.print(" ("); 2448 pw.print((ew.usedTime*100)/ew.overTime); 2449 pw.println("%)"); 2450 } 2451 } 2452 } 2453 uidActivity = true; 2454 } 2455 } 2456 } 2457 2458 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats(); 2459 if (packageStats.size() > 0) { 2460 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent 2461 : packageStats.entrySet()) { 2462 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":"); 2463 boolean apkActivity = false; 2464 Uid.Pkg ps = ent.getValue(); 2465 int wakeups = ps.getWakeups(which); 2466 if (wakeups != 0) { 2467 pw.print(prefix); pw.print(" "); 2468 pw.print(wakeups); pw.println(" wakeup alarms"); 2469 apkActivity = true; 2470 } 2471 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats(); 2472 if (serviceStats.size() > 0) { 2473 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent 2474 : serviceStats.entrySet()) { 2475 BatteryStats.Uid.Pkg.Serv ss = sent.getValue(); 2476 long startTime = ss.getStartTime(batteryUptime, which); 2477 int starts = ss.getStarts(which); 2478 int launches = ss.getLaunches(which); 2479 if (startTime != 0 || starts != 0 || launches != 0) { 2480 sb.setLength(0); 2481 sb.append(prefix); sb.append(" Service "); 2482 sb.append(sent.getKey()); sb.append(":\n"); 2483 sb.append(prefix); sb.append(" Created for: "); 2484 formatTimeMs(sb, startTime / 1000); 2485 sb.append("uptime\n"); 2486 sb.append(prefix); sb.append(" Starts: "); 2487 sb.append(starts); 2488 sb.append(", launches: "); sb.append(launches); 2489 pw.println(sb.toString()); 2490 apkActivity = true; 2491 } 2492 } 2493 } 2494 if (!apkActivity) { 2495 pw.print(prefix); pw.println(" (nothing executed)"); 2496 } 2497 uidActivity = true; 2498 } 2499 } 2500 if (!uidActivity) { 2501 pw.print(prefix); pw.println(" (nothing executed)"); 2502 } 2503 } 2504 } 2505 2506 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag, 2507 BitDescription[] descriptions, boolean longNames) { 2508 int diff = oldval ^ newval; 2509 if (diff == 0) return; 2510 boolean didWake = false; 2511 for (int i=0; i<descriptions.length; i++) { 2512 BitDescription bd = descriptions[i]; 2513 if ((diff&bd.mask) != 0) { 2514 pw.print(longNames ? " " : ","); 2515 if (bd.shift < 0) { 2516 pw.print((newval&bd.mask) != 0 ? "+" : "-"); 2517 pw.print(longNames ? bd.name : bd.shortName); 2518 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) { 2519 didWake = true; 2520 pw.print("="); 2521 if (longNames) { 2522 UserHandle.formatUid(pw, wakelockTag.uid); 2523 pw.print(":\""); 2524 pw.print(wakelockTag.string); 2525 pw.print("\""); 2526 } else { 2527 pw.print(wakelockTag.poolIdx); 2528 } 2529 } 2530 } else { 2531 pw.print(longNames ? bd.name : bd.shortName); 2532 pw.print("="); 2533 int val = (newval&bd.mask)>>bd.shift; 2534 if (bd.values != null && val >= 0 && val < bd.values.length) { 2535 pw.print(longNames? bd.values[val] : bd.shortValues[val]); 2536 } else { 2537 pw.print(val); 2538 } 2539 } 2540 } 2541 } 2542 if (!didWake && wakelockTag != null) { 2543 pw.print(longNames ? "wake_lock=" : "w="); 2544 if (longNames) { 2545 UserHandle.formatUid(pw, wakelockTag.uid); 2546 pw.print(":\""); 2547 pw.print(wakelockTag.string); 2548 pw.print("\""); 2549 } else { 2550 pw.print(wakelockTag.poolIdx); 2551 } 2552 } 2553 } 2554 2555 public void prepareForDumpLocked() { 2556 } 2557 2558 public static class HistoryPrinter { 2559 int oldState = 0; 2560 int oldLevel = -1; 2561 int oldStatus = -1; 2562 int oldHealth = -1; 2563 int oldPlug = -1; 2564 int oldTemp = -1; 2565 int oldVolt = -1; 2566 long lastTime = -1; 2567 2568 public void printNextItem(PrintWriter pw, HistoryItem rec, long now, boolean checkin) { 2569 if (!checkin) { 2570 pw.print(" "); 2571 TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN); 2572 pw.print(" ("); 2573 pw.print(rec.numReadInts); 2574 pw.print(") "); 2575 } else { 2576 if (lastTime < 0) { 2577 pw.print("@"); 2578 pw.print(rec.time-now); 2579 } else { 2580 pw.print(rec.time-lastTime); 2581 } 2582 lastTime = rec.time; 2583 } 2584 if (rec.cmd == HistoryItem.CMD_START) { 2585 if (checkin) { 2586 pw.print(":"); 2587 } 2588 pw.println("START"); 2589 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) { 2590 if (checkin) { 2591 pw.print(":"); 2592 } 2593 pw.println("*OVERFLOW*"); 2594 } else { 2595 if (!checkin) { 2596 if (rec.batteryLevel < 10) pw.print("00"); 2597 else if (rec.batteryLevel < 100) pw.print("0"); 2598 pw.print(rec.batteryLevel); 2599 pw.print(" "); 2600 if (rec.states < 0) ; 2601 else if (rec.states < 0x10) pw.print("0000000"); 2602 else if (rec.states < 0x100) pw.print("000000"); 2603 else if (rec.states < 0x1000) pw.print("00000"); 2604 else if (rec.states < 0x10000) pw.print("0000"); 2605 else if (rec.states < 0x100000) pw.print("000"); 2606 else if (rec.states < 0x1000000) pw.print("00"); 2607 else if (rec.states < 0x10000000) pw.print("0"); 2608 pw.print(Integer.toHexString(rec.states)); 2609 } else { 2610 if (oldLevel != rec.batteryLevel) { 2611 oldLevel = rec.batteryLevel; 2612 pw.print(",Bl="); pw.print(rec.batteryLevel); 2613 } 2614 } 2615 if (oldStatus != rec.batteryStatus) { 2616 oldStatus = rec.batteryStatus; 2617 pw.print(checkin ? ",Bs=" : " status="); 2618 switch (oldStatus) { 2619 case BatteryManager.BATTERY_STATUS_UNKNOWN: 2620 pw.print(checkin ? "?" : "unknown"); 2621 break; 2622 case BatteryManager.BATTERY_STATUS_CHARGING: 2623 pw.print(checkin ? "c" : "charging"); 2624 break; 2625 case BatteryManager.BATTERY_STATUS_DISCHARGING: 2626 pw.print(checkin ? "d" : "discharging"); 2627 break; 2628 case BatteryManager.BATTERY_STATUS_NOT_CHARGING: 2629 pw.print(checkin ? "n" : "not-charging"); 2630 break; 2631 case BatteryManager.BATTERY_STATUS_FULL: 2632 pw.print(checkin ? "f" : "full"); 2633 break; 2634 default: 2635 pw.print(oldStatus); 2636 break; 2637 } 2638 } 2639 if (oldHealth != rec.batteryHealth) { 2640 oldHealth = rec.batteryHealth; 2641 pw.print(checkin ? ",Bh=" : " health="); 2642 switch (oldHealth) { 2643 case BatteryManager.BATTERY_HEALTH_UNKNOWN: 2644 pw.print(checkin ? "?" : "unknown"); 2645 break; 2646 case BatteryManager.BATTERY_HEALTH_GOOD: 2647 pw.print(checkin ? "g" : "good"); 2648 break; 2649 case BatteryManager.BATTERY_HEALTH_OVERHEAT: 2650 pw.print(checkin ? "h" : "overheat"); 2651 break; 2652 case BatteryManager.BATTERY_HEALTH_DEAD: 2653 pw.print(checkin ? "d" : "dead"); 2654 break; 2655 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: 2656 pw.print(checkin ? "v" : "over-voltage"); 2657 break; 2658 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: 2659 pw.print(checkin ? "f" : "failure"); 2660 break; 2661 case BatteryManager.BATTERY_HEALTH_COLD: 2662 pw.print(checkin ? "c" : "cold"); 2663 break; 2664 default: 2665 pw.print(oldHealth); 2666 break; 2667 } 2668 } 2669 if (oldPlug != rec.batteryPlugType) { 2670 oldPlug = rec.batteryPlugType; 2671 pw.print(checkin ? ",Bp=" : " plug="); 2672 switch (oldPlug) { 2673 case 0: 2674 pw.print(checkin ? "n" : "none"); 2675 break; 2676 case BatteryManager.BATTERY_PLUGGED_AC: 2677 pw.print(checkin ? "a" : "ac"); 2678 break; 2679 case BatteryManager.BATTERY_PLUGGED_USB: 2680 pw.print(checkin ? "u" : "usb"); 2681 break; 2682 case BatteryManager.BATTERY_PLUGGED_WIRELESS: 2683 pw.print(checkin ? "w" : "wireless"); 2684 break; 2685 default: 2686 pw.print(oldPlug); 2687 break; 2688 } 2689 } 2690 if (oldTemp != rec.batteryTemperature) { 2691 oldTemp = rec.batteryTemperature; 2692 pw.print(checkin ? ",Bt=" : " temp="); 2693 pw.print(oldTemp); 2694 } 2695 if (oldVolt != rec.batteryVoltage) { 2696 oldVolt = rec.batteryVoltage; 2697 pw.print(checkin ? ",Bv=" : " volt="); 2698 pw.print(oldVolt); 2699 } 2700 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag, 2701 HISTORY_STATE_DESCRIPTIONS, !checkin); 2702 if (rec.eventCode != HistoryItem.EVENT_NONE) { 2703 pw.print(checkin ? "," : " "); 2704 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) { 2705 pw.print("+"); 2706 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) { 2707 pw.print("-"); 2708 } 2709 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES 2710 : HISTORY_EVENT_NAMES; 2711 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START 2712 | HistoryItem.EVENT_FLAG_FINISH); 2713 if (idx >= 0 && idx < eventNames.length) { 2714 pw.print(eventNames[idx]); 2715 } else { 2716 pw.print(checkin ? "Ev" : "event"); 2717 pw.print(idx); 2718 } 2719 pw.print("="); 2720 if (checkin) { 2721 pw.print(rec.eventTag.poolIdx); 2722 } else { 2723 UserHandle.formatUid(pw, rec.eventTag.uid); 2724 pw.print(":\""); 2725 pw.print(rec.eventTag.string); 2726 pw.print("\""); 2727 } 2728 } 2729 pw.println(); 2730 } 2731 oldState = rec.states; 2732 } 2733 } 2734 2735 private void printSizeValue(PrintWriter pw, long size) { 2736 float result = size; 2737 String suffix = ""; 2738 if (result >= 10*1024) { 2739 suffix = "KB"; 2740 result = result / 1024; 2741 } 2742 if (result >= 10*1024) { 2743 suffix = "MB"; 2744 result = result / 1024; 2745 } 2746 if (result >= 10*1024) { 2747 suffix = "GB"; 2748 result = result / 1024; 2749 } 2750 if (result >= 10*1024) { 2751 suffix = "TB"; 2752 result = result / 1024; 2753 } 2754 if (result >= 10*1024) { 2755 suffix = "PB"; 2756 result = result / 1024; 2757 } 2758 pw.print((int)result); 2759 pw.print(suffix); 2760 } 2761 2762 /** 2763 * Dumps a human-readable summary of the battery statistics to the given PrintWriter. 2764 * 2765 * @param pw a Printer to receive the dump output. 2766 */ 2767 @SuppressWarnings("unused") 2768 public void dumpLocked(Context context, PrintWriter pw, boolean isUnpluggedOnly, int reqUid, 2769 boolean historyOnly) { 2770 prepareForDumpLocked(); 2771 2772 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime(); 2773 2774 final HistoryItem rec = new HistoryItem(); 2775 final long historyTotalSize = getHistoryTotalSize(); 2776 final long historyUsedSize = getHistoryUsedSize(); 2777 if (startIteratingHistoryLocked()) { 2778 try { 2779 pw.print("Battery History ("); 2780 pw.print((100*historyUsedSize)/historyTotalSize); 2781 pw.print("% used, "); 2782 printSizeValue(pw, historyUsedSize); 2783 pw.print(" used of "); 2784 printSizeValue(pw, historyTotalSize); 2785 pw.print(", "); 2786 pw.print(getHistoryStringPoolSize()); 2787 pw.print(" strings using "); 2788 printSizeValue(pw, getHistoryStringPoolBytes()); 2789 pw.println("):"); 2790 HistoryPrinter hprinter = new HistoryPrinter(); 2791 while (getNextHistoryLocked(rec)) { 2792 hprinter.printNextItem(pw, rec, now, false); 2793 } 2794 pw.println(); 2795 } finally { 2796 finishIteratingHistoryLocked(); 2797 } 2798 } 2799 2800 if (startIteratingOldHistoryLocked()) { 2801 try { 2802 pw.println("Old battery History:"); 2803 HistoryPrinter hprinter = new HistoryPrinter(); 2804 while (getNextOldHistoryLocked(rec)) { 2805 hprinter.printNextItem(pw, rec, now, false); 2806 } 2807 pw.println(); 2808 } finally { 2809 finishIteratingOldHistoryLocked(); 2810 } 2811 } 2812 2813 if (historyOnly) { 2814 return; 2815 } 2816 2817 SparseArray<? extends Uid> uidStats = getUidStats(); 2818 final int NU = uidStats.size(); 2819 boolean didPid = false; 2820 long nowRealtime = SystemClock.elapsedRealtime(); 2821 for (int i=0; i<NU; i++) { 2822 Uid uid = uidStats.valueAt(i); 2823 SparseArray<? extends Uid.Pid> pids = uid.getPidStats(); 2824 if (pids != null) { 2825 for (int j=0; j<pids.size(); j++) { 2826 Uid.Pid pid = pids.valueAt(j); 2827 if (!didPid) { 2828 pw.println("Per-PID Stats:"); 2829 didPid = true; 2830 } 2831 long time = pid.mWakeSum + (pid.mWakeStart != 0 2832 ? (nowRealtime - pid.mWakeStart) : 0); 2833 pw.print(" PID "); pw.print(pids.keyAt(j)); 2834 pw.print(" wake time: "); 2835 TimeUtils.formatDuration(time, pw); 2836 pw.println(""); 2837 } 2838 } 2839 } 2840 if (didPid) { 2841 pw.println(""); 2842 } 2843 2844 if (!isUnpluggedOnly) { 2845 pw.println("Statistics since last charge:"); 2846 pw.println(" System starts: " + getStartCount() 2847 + ", currently on battery: " + getIsOnBattery()); 2848 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid); 2849 pw.println(""); 2850 } 2851 pw.println("Statistics since last unplugged:"); 2852 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid); 2853 } 2854 2855 @SuppressWarnings("unused") 2856 public void dumpCheckinLocked(Context context, 2857 PrintWriter pw, List<ApplicationInfo> apps, boolean isUnpluggedOnly, 2858 boolean includeHistory, boolean historyOnly) { 2859 prepareForDumpLocked(); 2860 2861 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime(); 2862 2863 if (includeHistory || historyOnly) { 2864 final HistoryItem rec = new HistoryItem(); 2865 if (startIteratingHistoryLocked()) { 2866 try { 2867 for (int i=0; i<getHistoryStringPoolSize(); i++) { 2868 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(','); 2869 pw.print(HISTORY_STRING_POOL); pw.print(','); 2870 pw.print(i); 2871 pw.print(','); 2872 pw.print(getHistoryTagPoolString(i)); 2873 pw.print(','); 2874 pw.print(getHistoryTagPoolUid(i)); 2875 pw.println(); 2876 } 2877 HistoryPrinter hprinter = new HistoryPrinter(); 2878 while (getNextHistoryLocked(rec)) { 2879 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(','); 2880 pw.print(HISTORY_DATA); pw.print(','); 2881 hprinter.printNextItem(pw, rec, now, true); 2882 } 2883 } finally { 2884 finishIteratingHistoryLocked(); 2885 } 2886 } 2887 } 2888 2889 if (historyOnly) { 2890 return; 2891 } 2892 2893 if (apps != null) { 2894 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>(); 2895 for (int i=0; i<apps.size(); i++) { 2896 ApplicationInfo ai = apps.get(i); 2897 ArrayList<String> pkgs = uids.get(ai.uid); 2898 if (pkgs == null) { 2899 pkgs = new ArrayList<String>(); 2900 uids.put(ai.uid, pkgs); 2901 } 2902 pkgs.add(ai.packageName); 2903 } 2904 SparseArray<? extends Uid> uidStats = getUidStats(); 2905 final int NU = uidStats.size(); 2906 String[] lineArgs = new String[2]; 2907 for (int i=0; i<NU; i++) { 2908 int uid = uidStats.keyAt(i); 2909 ArrayList<String> pkgs = uids.get(uid); 2910 if (pkgs != null) { 2911 for (int j=0; j<pkgs.size(); j++) { 2912 lineArgs[0] = Integer.toString(uid); 2913 lineArgs[1] = pkgs.get(j); 2914 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA, 2915 (Object[])lineArgs); 2916 } 2917 } 2918 } 2919 } 2920 if (isUnpluggedOnly) { 2921 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1); 2922 } 2923 else { 2924 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1); 2925 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1); 2926 } 2927 } 2928} 2929