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