2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import java.io.PrintWriter;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.Formatter;
24 import java.util.List;
27 import android.content.pm.ApplicationInfo;
28 import android.telephony.SignalStrength;
29 import android.util.Log;
30 import android.util.Printer;
31 import android.util.Slog;
32 import android.util.SparseArray;
33 import android.util.TimeUtils;
36 * A class providing access to battery usage statistics, including information on
37 * wakelocks, processes, packages, and services. All times are represented in microseconds
38 * except where indicated otherwise.
41 public abstract class BatteryStats implements Parcelable {
43 private static final boolean LOCAL_LOGV = false;
46 public static final String SERVICE_NAME = "batterystats";
49 * A constant indicating a partial wake lock timer.
51 public static final int WAKE_TYPE_PARTIAL = 0;
54 * A constant indicating a full wake lock timer.
56 public static final int WAKE_TYPE_FULL = 1;
59 * A constant indicating a window wake lock timer.
61 public static final int WAKE_TYPE_WINDOW = 2;
64 * A constant indicating a sensor timer.
66 public static final int SENSOR = 3;
69 * A constant indicating a a wifi running timer
71 public static final int WIFI_RUNNING = 4;
74 * A constant indicating a full wifi lock timer
76 public static final int FULL_WIFI_LOCK = 5;
79 * A constant indicating a wifi scan
81 public static final int WIFI_SCAN = 6;
84 * A constant indicating a wifi multicast timer
86 public static final int WIFI_MULTICAST_ENABLED = 7;
89 * A constant indicating an audio turn on timer
91 public static final int AUDIO_TURNED_ON = 7;
94 * A constant indicating a video turn on timer
96 public static final int VIDEO_TURNED_ON = 8;
99 * A constant indicating a vibrator on timer
101 public static final int VIBRATOR_ON = 9;
104 * A constant indicating a foreground activity timer
106 public static final int FOREGROUND_ACTIVITY = 10;
109 * A constant indicating a wifi batched scan is active
111 public static final int WIFI_BATCHED_SCAN = 11;
114 * Include all of the data in the stats, including previously saved data.
116 public static final int STATS_SINCE_CHARGED = 0;
119 * Include only the last run in the stats.
121 public static final int STATS_LAST = 1;
124 * Include only the current run in the stats.
126 public static final int STATS_CURRENT = 2;
129 * Include only the run since the last time the device was unplugged in the stats.
131 public static final int STATS_SINCE_UNPLUGGED = 3;
133 // NOTE: Update this list if you add/change any stats above.
134 // These characters are supposed to represent "total", "last", "current",
135 // and "unplugged". They were shortened for efficiency sake.
136 private static final String[] STAT_NAMES = { "t", "l", "c", "u" };
139 * Bump the version on this if the checkin format changes.
141 private static final int BATTERY_STATS_CHECKIN_VERSION = 7;
143 private static final long BYTES_PER_KB = 1024;
144 private static final long BYTES_PER_MB = 1048576; // 1024^2
145 private static final long BYTES_PER_GB = 1073741824; //1024^3
148 private static final String UID_DATA = "uid";
149 private static final String APK_DATA = "apk";
150 private static final String PROCESS_DATA = "pr";
151 private static final String SENSOR_DATA = "sr";
152 private static final String VIBRATOR_DATA = "vib";
153 private static final String FOREGROUND_DATA = "fg";
154 private static final String WAKELOCK_DATA = "wl";
155 private static final String KERNEL_WAKELOCK_DATA = "kwl";
156 private static final String NETWORK_DATA = "nt";
157 private static final String USER_ACTIVITY_DATA = "ua";
158 private static final String BATTERY_DATA = "bt";
159 private static final String BATTERY_DISCHARGE_DATA = "dc";
160 private static final String BATTERY_LEVEL_DATA = "lv";
161 private static final String WIFI_DATA = "wfl";
162 private static final String MISC_DATA = "m";
163 private static final String HISTORY_DATA = "h";
164 private static final String SCREEN_BRIGHTNESS_DATA = "br";
165 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
166 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
167 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
168 private static final String DATA_CONNECTION_TIME_DATA = "dct";
169 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
171 private final StringBuilder mFormatBuilder = new StringBuilder(32);
172 private final Formatter mFormatter = new Formatter(mFormatBuilder);
175 * State for keeping track of counting information.
177 public static abstract class Counter {
180 * Returns the count associated with this Counter for the
181 * selected type of statistics.
183 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
185 public abstract int getCountLocked(int which);
188 * Temporary for debugging.
190 public abstract void logState(Printer pw, String prefix);
194 * State for keeping track of timing information.
196 public static abstract class Timer {
199 * Returns the count associated with this Timer for the
200 * selected type of statistics.
202 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
204 public abstract int getCountLocked(int which);
207 * Returns the total time in microseconds associated with this Timer for the
208 * selected type of statistics.
210 * @param batteryRealtime system realtime on battery in microseconds
211 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
212 * @return a time in microseconds
214 public abstract long getTotalTimeLocked(long batteryRealtime, int which);
217 * Temporary for debugging.
219 public abstract void logState(Printer pw, String prefix);
223 * The statistics associated with a particular uid.
225 public static abstract class Uid {
228 * Returns a mapping containing wakelock statistics.
230 * @return a Map from Strings to Uid.Wakelock objects.
232 public abstract Map<String, ? extends Wakelock> getWakelockStats();
235 * The statistics associated with a particular wake lock.
237 public static abstract class Wakelock {
238 public abstract Timer getWakeTime(int type);
242 * Returns a mapping containing sensor statistics.
244 * @return a Map from Integer sensor ids to Uid.Sensor objects.
246 public abstract Map<Integer, ? extends Sensor> getSensorStats();
249 * Returns a mapping containing active process data.
251 public abstract SparseArray<? extends Pid> getPidStats();
254 * Returns a mapping containing process statistics.
256 * @return a Map from Strings to Uid.Proc objects.
258 public abstract Map<String, ? extends Proc> getProcessStats();
261 * Returns a mapping containing package statistics.
263 * @return a Map from Strings to Uid.Pkg objects.
265 public abstract Map<String, ? extends Pkg> getPackageStats();
270 public abstract int getUid();
272 public abstract void noteWifiRunningLocked();
273 public abstract void noteWifiStoppedLocked();
274 public abstract void noteFullWifiLockAcquiredLocked();
275 public abstract void noteFullWifiLockReleasedLocked();
276 public abstract void noteWifiScanStartedLocked();
277 public abstract void noteWifiScanStoppedLocked();
278 public abstract void noteWifiBatchedScanStartedLocked(int csph);
279 public abstract void noteWifiBatchedScanStoppedLocked();
280 public abstract void noteWifiMulticastEnabledLocked();
281 public abstract void noteWifiMulticastDisabledLocked();
282 public abstract void noteAudioTurnedOnLocked();
283 public abstract void noteAudioTurnedOffLocked();
284 public abstract void noteVideoTurnedOnLocked();
285 public abstract void noteVideoTurnedOffLocked();
286 public abstract void noteActivityResumedLocked();
287 public abstract void noteActivityPausedLocked();
288 public abstract long getWifiRunningTime(long batteryRealtime, int which);
289 public abstract long getFullWifiLockTime(long batteryRealtime, int which);
290 public abstract long getWifiScanTime(long batteryRealtime, int which);
291 public abstract long getWifiBatchedScanTime(int csphBin, long batteryRealtime, int which);
292 public abstract long getWifiMulticastTime(long batteryRealtime,
294 public abstract long getAudioTurnedOnTime(long batteryRealtime, int which);
295 public abstract long getVideoTurnedOnTime(long batteryRealtime, int which);
296 public abstract Timer getForegroundActivityTimer();
297 public abstract Timer getVibratorOnTimer();
299 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
302 * Note that these must match the constants in android.os.PowerManager.
303 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
306 static final String[] USER_ACTIVITY_TYPES = {
307 "other", "button", "touch"
310 public static final int NUM_USER_ACTIVITY_TYPES = 3;
312 public abstract void noteUserActivityLocked(int type);
313 public abstract boolean hasUserActivity();
314 public abstract int getUserActivityCount(int type, int which);
316 public abstract boolean hasNetworkActivity();
317 public abstract long getNetworkActivityCount(int type, int which);
319 public static abstract class Sensor {
321 * FIXME: it's not correct to use this magic value because it
322 * could clash with a sensor handle (which are defined by
323 * the sensor HAL, and therefore out of our control
325 // Magic sensor number for the GPS.
326 public static final int GPS = -10000;
328 public abstract int getHandle();
330 public abstract Timer getSensorTime();
334 public long mWakeSum;
335 public long mWakeStart;
339 * The statistics associated with a particular process.
341 public static abstract class Proc {
343 public static class ExcessivePower {
344 public static final int TYPE_WAKE = 1;
345 public static final int TYPE_CPU = 2;
348 public long overTime;
349 public long usedTime;
353 * Returns the total time (in 1/100 sec) spent executing in user code.
355 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
357 public abstract long getUserTime(int which);
360 * Returns the total time (in 1/100 sec) spent executing in system code.
362 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
364 public abstract long getSystemTime(int which);
367 * Returns the number of times the process has been started.
369 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
371 public abstract int getStarts(int which);
374 * Returns the cpu time spent in microseconds while the process was in the foreground.
375 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
376 * @return foreground cpu time in microseconds
378 public abstract long getForegroundTime(int which);
381 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
382 * @param speedStep the index of the CPU speed. This is not the actual speed of the
384 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
385 * @see BatteryStats#getCpuSpeedSteps()
387 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
389 public abstract int countExcessivePowers();
391 public abstract ExcessivePower getExcessivePower(int i);
395 * The statistics associated with a particular package.
397 public static abstract class Pkg {
400 * Returns the number of times this package has done something that could wake up the
403 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
405 public abstract int getWakeups(int which);
408 * Returns a mapping containing service statistics.
410 public abstract Map<String, ? extends Serv> getServiceStats();
413 * The statistics associated with a particular service.
415 public abstract class Serv {
418 * Returns the amount of time spent started.
420 * @param batteryUptime elapsed uptime on battery in microseconds.
421 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
424 public abstract long getStartTime(long batteryUptime, int which);
427 * Returns the total number of times startService() has been called.
429 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
431 public abstract int getStarts(int which);
434 * Returns the total number times the service has been launched.
436 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
438 public abstract int getLaunches(int which);
443 public final static class HistoryItem implements Parcelable {
444 static final String TAG = "HistoryItem";
445 static final boolean DEBUG = false;
447 public HistoryItem next;
451 public static final byte CMD_NULL = 0;
452 public static final byte CMD_UPDATE = 1;
453 public static final byte CMD_START = 2;
454 public static final byte CMD_OVERFLOW = 3;
456 public byte cmd = CMD_NULL;
458 public byte batteryLevel;
459 public byte batteryStatus;
460 public byte batteryHealth;
461 public byte batteryPlugType;
463 public short batteryTemperature;
464 public char batteryVoltage;
466 // Constants from SCREEN_BRIGHTNESS_*
467 public static final int STATE_BRIGHTNESS_MASK = 0x0000000f;
468 public static final int STATE_BRIGHTNESS_SHIFT = 0;
469 // Constants from SIGNAL_STRENGTH_*
470 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x000000f0;
471 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 4;
472 // Constants from ServiceState.STATE_*
473 public static final int STATE_PHONE_STATE_MASK = 0x00000f00;
474 public static final int STATE_PHONE_STATE_SHIFT = 8;
475 // Constants from DATA_CONNECTION_*
476 public static final int STATE_DATA_CONNECTION_MASK = 0x0000f000;
477 public static final int STATE_DATA_CONNECTION_SHIFT = 12;
479 // These states always appear directly in the first int token
480 // of a delta change; they should be ones that change relatively
482 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
483 public static final int STATE_SENSOR_ON_FLAG = 1<<29;
484 public static final int STATE_GPS_ON_FLAG = 1<<28;
485 public static final int STATE_PHONE_SCANNING_FLAG = 1<<27;
486 public static final int STATE_WIFI_RUNNING_FLAG = 1<<26;
487 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<25;
488 public static final int STATE_WIFI_SCAN_FLAG = 1<<24;
489 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<23;
490 // These are on the lower bits used for the command; if they change
491 // we need to write another int of data.
492 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
493 public static final int STATE_VIDEO_ON_FLAG = 1<<21;
494 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
495 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
496 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
497 public static final int STATE_WIFI_ON_FLAG = 1<<17;
498 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
500 public static final int MOST_INTERESTING_STATES =
501 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
502 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
506 public HistoryItem() {
509 public HistoryItem(long time, Parcel src) {
514 public int describeContents() {
518 public void writeToParcel(Parcel dest, int flags) {
519 dest.writeLong(time);
520 int bat = (((int)cmd)&0xff)
521 | ((((int)batteryLevel)<<8)&0xff00)
522 | ((((int)batteryStatus)<<16)&0xf0000)
523 | ((((int)batteryHealth)<<20)&0xf00000)
524 | ((((int)batteryPlugType)<<24)&0xf000000);
526 bat = (((int)batteryTemperature)&0xffff)
527 | ((((int)batteryVoltage)<<16)&0xffff0000);
529 dest.writeInt(states);
532 private void readFromParcel(Parcel src) {
533 int bat = src.readInt();
534 cmd = (byte)(bat&0xff);
535 batteryLevel = (byte)((bat>>8)&0xff);
536 batteryStatus = (byte)((bat>>16)&0xf);
537 batteryHealth = (byte)((bat>>20)&0xf);
538 batteryPlugType = (byte)((bat>>24)&0xf);
540 batteryTemperature = (short)(bat&0xffff);
541 batteryVoltage = (char)((bat>>16)&0xffff);
542 states = src.readInt();
545 // Part of initial delta int that specifies the time delta.
546 static final int DELTA_TIME_MASK = 0x3ffff;
547 static final int DELTA_TIME_ABS = 0x3fffd; // Following is an entire abs update.
548 static final int DELTA_TIME_INT = 0x3fffe; // The delta is a following int
549 static final int DELTA_TIME_LONG = 0x3ffff; // The delta is a following long
550 // Part of initial delta int holding the command code.
551 static final int DELTA_CMD_MASK = 0x3;
552 static final int DELTA_CMD_SHIFT = 18;
553 // Flag in delta int: a new battery level int follows.
554 static final int DELTA_BATTERY_LEVEL_FLAG = 1<<20;
555 // Flag in delta int: a new full state and battery status int follows.
556 static final int DELTA_STATE_FLAG = 1<<21;
557 static final int DELTA_STATE_MASK = 0xffc00000;
559 public void writeDelta(Parcel dest, HistoryItem last) {
560 if (last == null || last.cmd != CMD_UPDATE) {
561 dest.writeInt(DELTA_TIME_ABS);
562 writeToParcel(dest, 0);
566 final long deltaTime = time - last.time;
567 final int lastBatteryLevelInt = last.buildBatteryLevelInt();
568 final int lastStateInt = last.buildStateInt();
571 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
572 deltaTimeToken = DELTA_TIME_LONG;
573 } else if (deltaTime >= DELTA_TIME_ABS) {
574 deltaTimeToken = DELTA_TIME_INT;
576 deltaTimeToken = (int)deltaTime;
578 int firstToken = deltaTimeToken
579 | (cmd<<DELTA_CMD_SHIFT)
580 | (states&DELTA_STATE_MASK);
581 final int batteryLevelInt = buildBatteryLevelInt();
582 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
583 if (batteryLevelIntChanged) {
584 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
586 final int stateInt = buildStateInt();
587 final boolean stateIntChanged = stateInt != lastStateInt;
588 if (stateIntChanged) {
589 firstToken |= DELTA_STATE_FLAG;
591 dest.writeInt(firstToken);
592 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
593 + " deltaTime=" + deltaTime);
595 if (deltaTimeToken >= DELTA_TIME_INT) {
596 if (deltaTimeToken == DELTA_TIME_INT) {
597 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
598 dest.writeInt((int)deltaTime);
600 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
601 dest.writeLong(deltaTime);
604 if (batteryLevelIntChanged) {
605 dest.writeInt(batteryLevelInt);
606 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
607 + Integer.toHexString(batteryLevelInt)
608 + " batteryLevel=" + batteryLevel
609 + " batteryTemp=" + batteryTemperature
610 + " batteryVolt=" + (int)batteryVoltage);
612 if (stateIntChanged) {
613 dest.writeInt(stateInt);
614 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
615 + Integer.toHexString(stateInt)
616 + " batteryStatus=" + batteryStatus
617 + " batteryHealth=" + batteryHealth
618 + " batteryPlugType=" + batteryPlugType
619 + " states=0x" + Integer.toHexString(states));
623 private int buildBatteryLevelInt() {
624 return ((((int)batteryLevel)<<25)&0xfe000000)
625 | ((((int)batteryTemperature)<<14)&0x01ffc000)
626 | (((int)batteryVoltage)&0x00003fff);
629 private int buildStateInt() {
630 return ((((int)batteryStatus)<<28)&0xf0000000)
631 | ((((int)batteryHealth)<<24)&0x0f000000)
632 | ((((int)batteryPlugType)<<22)&0x00c00000)
633 | (states&(~DELTA_STATE_MASK));
636 public void readDelta(Parcel src) {
637 int firstToken = src.readInt();
638 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
639 cmd = (byte)((firstToken>>DELTA_CMD_SHIFT)&DELTA_CMD_MASK);
640 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
641 + " deltaTimeToken=" + deltaTimeToken);
643 if (deltaTimeToken < DELTA_TIME_ABS) {
644 time += deltaTimeToken;
645 } else if (deltaTimeToken == DELTA_TIME_ABS) {
646 time = src.readLong();
649 } else if (deltaTimeToken == DELTA_TIME_INT) {
650 int delta = src.readInt();
652 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + time);
654 long delta = src.readLong();
655 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + time);
659 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
660 int batteryLevelInt = src.readInt();
661 batteryLevel = (byte)((batteryLevelInt>>25)&0x7f);
662 batteryTemperature = (short)((batteryLevelInt<<7)>>21);
663 batteryVoltage = (char)(batteryLevelInt&0x3fff);
664 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
665 + Integer.toHexString(batteryLevelInt)
666 + " batteryLevel=" + batteryLevel
667 + " batteryTemp=" + batteryTemperature
668 + " batteryVolt=" + (int)batteryVoltage);
671 if ((firstToken&DELTA_STATE_FLAG) != 0) {
672 int stateInt = src.readInt();
673 states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~DELTA_STATE_MASK));
674 batteryStatus = (byte)((stateInt>>28)&0xf);
675 batteryHealth = (byte)((stateInt>>24)&0xf);
676 batteryPlugType = (byte)((stateInt>>22)&0x3);
677 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
678 + Integer.toHexString(stateInt)
679 + " batteryStatus=" + batteryStatus
680 + " batteryHealth=" + batteryHealth
681 + " batteryPlugType=" + batteryPlugType
682 + " states=0x" + Integer.toHexString(states));
684 states = (firstToken&DELTA_STATE_MASK) | (states&(~DELTA_STATE_MASK));
688 public void clear() {
695 batteryTemperature = 0;
700 public void setTo(HistoryItem o) {
703 batteryLevel = o.batteryLevel;
704 batteryStatus = o.batteryStatus;
705 batteryHealth = o.batteryHealth;
706 batteryPlugType = o.batteryPlugType;
707 batteryTemperature = o.batteryTemperature;
708 batteryVoltage = o.batteryVoltage;
712 public void setTo(long time, byte cmd, HistoryItem o) {
715 batteryLevel = o.batteryLevel;
716 batteryStatus = o.batteryStatus;
717 batteryHealth = o.batteryHealth;
718 batteryPlugType = o.batteryPlugType;
719 batteryTemperature = o.batteryTemperature;
720 batteryVoltage = o.batteryVoltage;
724 public boolean same(HistoryItem o) {
725 return batteryLevel == o.batteryLevel
726 && batteryStatus == o.batteryStatus
727 && batteryHealth == o.batteryHealth
728 && batteryPlugType == o.batteryPlugType
729 && batteryTemperature == o.batteryTemperature
730 && batteryVoltage == o.batteryVoltage
731 && states == o.states;
735 public static final class BitDescription {
736 public final int mask;
737 public final int shift;
738 public final String name;
739 public final String[] values;
741 public BitDescription(int mask, String name) {
748 public BitDescription(int mask, int shift, String name, String[] values) {
752 this.values = values;
756 public abstract boolean startIteratingHistoryLocked();
758 public abstract boolean getNextHistoryLocked(HistoryItem out);
760 public abstract void finishIteratingHistoryLocked();
762 public abstract boolean startIteratingOldHistoryLocked();
764 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
766 public abstract void finishIteratingOldHistoryLocked();
769 * Return the base time offset for the battery history.
771 public abstract long getHistoryBaseTime();
774 * Returns the number of times the device has been started.
776 public abstract int getStartCount();
779 * Returns the time in microseconds that the screen has been on while the device was
780 * running on battery.
784 public abstract long getScreenOnTime(long batteryRealtime, int which);
786 public abstract long getInteractiveTime(long batteryRealtime, int which);
788 public static final int SCREEN_BRIGHTNESS_DARK = 0;
789 public static final int SCREEN_BRIGHTNESS_DIM = 1;
790 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
791 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
792 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
794 static final String[] SCREEN_BRIGHTNESS_NAMES = {
795 "dark", "dim", "medium", "light", "bright"
798 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
801 * Returns the time in microseconds that the screen has been on with
802 * the given brightness
806 public abstract long getScreenBrightnessTime(int brightnessBin,
807 long batteryRealtime, int which);
810 * Returns the time in microseconds that the phone has been on while the device was
811 * running on battery.
815 public abstract long getPhoneOnTime(long batteryRealtime, int which);
818 * Returns the time in microseconds that the phone has been running with
819 * the given signal strength.
823 public abstract long getPhoneSignalStrengthTime(int strengthBin,
824 long batteryRealtime, int which);
827 * Returns the time in microseconds that the phone has been trying to
832 public abstract long getPhoneSignalScanningTime(
833 long batteryRealtime, int which);
836 * Returns the number of times the phone has entered the given signal strength.
840 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
842 public static final int DATA_CONNECTION_NONE = 0;
843 public static final int DATA_CONNECTION_GPRS = 1;
844 public static final int DATA_CONNECTION_EDGE = 2;
845 public static final int DATA_CONNECTION_UMTS = 3;
846 public static final int DATA_CONNECTION_CDMA = 4;
847 public static final int DATA_CONNECTION_EVDO_0 = 5;
848 public static final int DATA_CONNECTION_EVDO_A = 6;
849 public static final int DATA_CONNECTION_1xRTT = 7;
850 public static final int DATA_CONNECTION_HSDPA = 8;
851 public static final int DATA_CONNECTION_HSUPA = 9;
852 public static final int DATA_CONNECTION_HSPA = 10;
853 public static final int DATA_CONNECTION_IDEN = 11;
854 public static final int DATA_CONNECTION_EVDO_B = 12;
855 public static final int DATA_CONNECTION_LTE = 13;
856 public static final int DATA_CONNECTION_EHRPD = 14;
857 public static final int DATA_CONNECTION_HSPAP = 15;
858 public static final int DATA_CONNECTION_OTHER = 16;
860 static final String[] DATA_CONNECTION_NAMES = {
861 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
862 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
863 "ehrpd", "hspap", "other"
866 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
869 * Returns the time in microseconds that the phone has been running with
870 * the given data connection.
874 public abstract long getPhoneDataConnectionTime(int dataType,
875 long batteryRealtime, int which);
878 * Returns the number of times the phone has entered the given data
883 public abstract int getPhoneDataConnectionCount(int dataType, int which);
885 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
886 = new BitDescription[] {
887 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged"),
888 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen"),
889 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps"),
890 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call"),
891 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning"),
892 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi"),
893 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running"),
894 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock"),
895 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan"),
896 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast"),
897 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth"),
898 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio"),
899 new BitDescription(HistoryItem.STATE_VIDEO_ON_FLAG, "video"),
900 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock"),
901 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor"),
902 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
903 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness",
904 SCREEN_BRIGHTNESS_NAMES),
905 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
906 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength",
907 SignalStrength.SIGNAL_STRENGTH_NAMES),
908 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
909 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state",
910 new String[] {"in", "out", "emergency", "off"}),
911 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
912 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn",
913 DATA_CONNECTION_NAMES),
917 * Returns the time in microseconds that wifi has been on while the device was
918 * running on battery.
922 public abstract long getWifiOnTime(long batteryRealtime, int which);
925 * Returns the time in microseconds that wifi has been on and the driver has
926 * been in the running state while the device was running on battery.
930 public abstract long getGlobalWifiRunningTime(long batteryRealtime, int which);
933 * Returns the time in microseconds that bluetooth has been on while the device was
934 * running on battery.
938 public abstract long getBluetoothOnTime(long batteryRealtime, int which);
940 public static final int NETWORK_MOBILE_RX_BYTES = 0;
941 public static final int NETWORK_MOBILE_TX_BYTES = 1;
942 public static final int NETWORK_WIFI_RX_BYTES = 2;
943 public static final int NETWORK_WIFI_TX_BYTES = 3;
945 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_BYTES + 1;
947 public abstract long getNetworkActivityCount(int type, int which);
950 * Return whether we are currently running on battery.
952 public abstract boolean getIsOnBattery();
955 * Returns a SparseArray containing the statistics for each uid.
957 public abstract SparseArray<? extends Uid> getUidStats();
960 * Returns the current battery uptime in microseconds.
962 * @param curTime the amount of elapsed realtime in microseconds.
964 public abstract long getBatteryUptime(long curTime);
967 * @deprecated use getRadioDataUptime
969 public long getRadioDataUptimeMs() {
970 return getRadioDataUptime() / 1000;
974 * Returns the time that the radio was on for data transfers.
975 * @return the uptime in microseconds while unplugged
977 public abstract long getRadioDataUptime();
980 * Returns the current battery realtime in microseconds.
982 * @param curTime the amount of elapsed realtime in microseconds.
984 public abstract long getBatteryRealtime(long curTime);
987 * Returns the battery percentage level at the last time the device was unplugged from power, or
988 * the last time it booted on battery power.
990 public abstract int getDischargeStartLevel();
993 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
994 * returns the level at the last plug event.
996 public abstract int getDischargeCurrentLevel();
999 * Get the amount the battery has discharged since the stats were
1000 * last reset after charging, as a lower-end approximation.
1002 public abstract int getLowDischargeAmountSinceCharge();
1005 * Get the amount the battery has discharged since the stats were
1006 * last reset after charging, as an upper-end approximation.
1008 public abstract int getHighDischargeAmountSinceCharge();
1011 * Get the amount the battery has discharged while the screen was on,
1012 * since the last time power was unplugged.
1014 public abstract int getDischargeAmountScreenOn();
1017 * Get the amount the battery has discharged while the screen was on,
1018 * since the last time the device was charged.
1020 public abstract int getDischargeAmountScreenOnSinceCharge();
1023 * Get the amount the battery has discharged while the screen was off,
1024 * since the last time power was unplugged.
1026 public abstract int getDischargeAmountScreenOff();
1029 * Get the amount the battery has discharged while the screen was off,
1030 * since the last time the device was charged.
1032 public abstract int getDischargeAmountScreenOffSinceCharge();
1035 * Returns the total, last, or current battery uptime in microseconds.
1037 * @param curTime the elapsed realtime in microseconds.
1038 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1040 public abstract long computeBatteryUptime(long curTime, int which);
1043 * Returns the total, last, or current battery realtime in microseconds.
1045 * @param curTime the current elapsed realtime in microseconds.
1046 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1048 public abstract long computeBatteryRealtime(long curTime, int which);
1051 * Returns the total, last, or current uptime in microseconds.
1053 * @param curTime the current elapsed realtime in microseconds.
1054 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1056 public abstract long computeUptime(long curTime, int which);
1059 * Returns the total, last, or current realtime in microseconds.
1061 * @param curTime the current elapsed realtime in microseconds.
1062 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1064 public abstract long computeRealtime(long curTime, int which);
1066 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
1068 /** Returns the number of different speeds that the CPU can run at */
1069 public abstract int getCpuSpeedSteps();
1071 private final static void formatTimeRaw(StringBuilder out, long seconds) {
1072 long days = seconds / (60 * 60 * 24);
1077 long used = days * 60 * 60 * 24;
1079 long hours = (seconds - used) / (60 * 60);
1080 if (hours != 0 || used != 0) {
1084 used += hours * 60 * 60;
1086 long mins = (seconds-used) / 60;
1087 if (mins != 0 || used != 0) {
1093 if (seconds != 0 || used != 0) {
1094 out.append(seconds-used);
1099 private final static void formatTime(StringBuilder sb, long time) {
1100 long sec = time / 100;
1101 formatTimeRaw(sb, sec);
1102 sb.append((time - (sec * 100)) * 10);
1106 private final static void formatTimeMs(StringBuilder sb, long time) {
1107 long sec = time / 1000;
1108 formatTimeRaw(sb, sec);
1109 sb.append(time - (sec * 1000));
1113 private final String formatRatioLocked(long num, long den) {
1117 float perc = ((float)num) / ((float)den) * 100;
1118 mFormatBuilder.setLength(0);
1119 mFormatter.format("%.1f%%", perc);
1120 return mFormatBuilder.toString();
1123 private final String formatBytesLocked(long bytes) {
1124 mFormatBuilder.setLength(0);
1126 if (bytes < BYTES_PER_KB) {
1128 } else if (bytes < BYTES_PER_MB) {
1129 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1130 return mFormatBuilder.toString();
1131 } else if (bytes < BYTES_PER_GB){
1132 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1133 return mFormatBuilder.toString();
1135 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1136 return mFormatBuilder.toString();
1140 private static long computeWakeLock(Timer timer, long batteryRealtime, int which) {
1141 if (timer != null) {
1142 // Convert from microseconds to milliseconds with rounding
1143 long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which);
1144 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1145 return totalTimeMillis;
1152 * @param sb a StringBuilder object.
1153 * @param timer a Timer object contining the wakelock times.
1154 * @param batteryRealtime the current on-battery time in microseconds.
1155 * @param name the name of the wakelock.
1156 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1157 * @param linePrefix a String to be prepended to each line of output.
1158 * @return the line prefix
1160 private static final String printWakeLock(StringBuilder sb, Timer timer,
1161 long batteryRealtime, String name, int which, String linePrefix) {
1163 if (timer != null) {
1164 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
1166 int count = timer.getCountLocked(which);
1167 if (totalTimeMillis != 0) {
1168 sb.append(linePrefix);
1169 formatTimeMs(sb, totalTimeMillis);
1176 sb.append(" times)");
1184 * Checkin version of wakelock printer. Prints simple comma-separated list.
1186 * @param sb a StringBuilder object.
1187 * @param timer a Timer object contining the wakelock times.
1188 * @param now the current time in microseconds.
1189 * @param name the name of the wakelock.
1190 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1191 * @param linePrefix a String to be prepended to each line of output.
1192 * @return the line prefix
1194 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer, long now,
1195 String name, int which, String linePrefix) {
1196 long totalTimeMicros = 0;
1198 if (timer != null) {
1199 totalTimeMicros = timer.getTotalTimeLocked(now, which);
1200 count = timer.getCountLocked(which);
1202 sb.append(linePrefix);
1203 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1205 sb.append(name != null ? name + "," : "");
1211 * Dump a comma-separated line of values for terse checkin mode.
1213 * @param pw the PageWriter to dump log to
1214 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1215 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1216 * @param args type-dependent data arguments
1218 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1220 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1221 pw.print(uid); pw.print(',');
1222 pw.print(category); pw.print(',');
1225 for (Object arg : args) {
1233 * Checkin server version of dump to produce more compact, computer-readable log.
1235 * NOTE: all times are expressed in 'ms'.
1237 public final void dumpCheckinLocked(PrintWriter pw, int which, int reqUid) {
1238 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1239 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1240 final long batteryUptime = getBatteryUptime(rawUptime);
1241 final long batteryRealtime = getBatteryRealtime(rawRealtime);
1242 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1243 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1244 final long totalRealtime = computeRealtime(rawRealtime, which);
1245 final long totalUptime = computeUptime(rawUptime, which);
1246 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1247 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
1248 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
1249 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
1250 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
1252 StringBuilder sb = new StringBuilder(128);
1254 SparseArray<? extends Uid> uidStats = getUidStats();
1255 final int NU = uidStats.size();
1257 String category = STAT_NAMES[which];
1259 // Dump "battery" stat
1260 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
1261 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
1262 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
1263 totalRealtime / 1000, totalUptime / 1000);
1265 // Calculate total network and wakelock times across all uids.
1266 long mobileRxTotal = 0;
1267 long mobileTxTotal = 0;
1268 long wifiRxTotal = 0;
1269 long wifiTxTotal = 0;
1270 long fullWakeLockTimeTotal = 0;
1271 long partialWakeLockTimeTotal = 0;
1273 for (int iu = 0; iu < NU; iu++) {
1274 Uid u = uidStats.valueAt(iu);
1275 mobileRxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1276 mobileTxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1277 wifiRxTotal += u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1278 wifiTxTotal += u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
1280 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1281 if (wakelocks.size() > 0) {
1282 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1283 : wakelocks.entrySet()) {
1284 Uid.Wakelock wl = ent.getValue();
1286 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1287 if (fullWakeTimer != null) {
1288 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(batteryRealtime, which);
1291 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1292 if (partialWakeTimer != null) {
1293 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
1294 batteryRealtime, which);
1301 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
1302 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
1303 wifiRunningTime / 1000, bluetoothOnTime / 1000,
1304 mobileRxTotal, mobileTxTotal, wifiRxTotal, wifiTxTotal,
1305 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
1306 0 /*legacy input event count*/);
1308 // Dump screen brightness stats
1309 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1310 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1311 args[i] = getScreenBrightnessTime(i, batteryRealtime, which) / 1000;
1313 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
1315 // Dump signal strength stats
1316 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1317 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
1318 args[i] = getPhoneSignalStrengthTime(i, batteryRealtime, which) / 1000;
1320 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
1321 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
1322 getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
1323 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
1324 args[i] = getPhoneSignalStrengthCount(i, which);
1326 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
1328 // Dump network type stats
1329 args = new Object[NUM_DATA_CONNECTION_TYPES];
1330 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1331 args[i] = getPhoneDataConnectionTime(i, batteryRealtime, which) / 1000;
1333 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1334 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1335 args[i] = getPhoneDataConnectionCount(i, which);
1337 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
1339 if (which == STATS_SINCE_UNPLUGGED) {
1340 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
1341 getDischargeCurrentLevel());
1344 if (which == STATS_SINCE_UNPLUGGED) {
1345 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1346 getDischargeStartLevel()-getDischargeCurrentLevel(),
1347 getDischargeStartLevel()-getDischargeCurrentLevel(),
1348 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1350 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1351 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1352 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1356 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1357 if (kernelWakelocks.size() > 0) {
1358 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1360 printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, "");
1362 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
1368 for (int iu = 0; iu < NU; iu++) {
1369 final int uid = uidStats.keyAt(iu);
1370 if (reqUid >= 0 && uid != reqUid) {
1373 Uid u = uidStats.valueAt(iu);
1374 // Dump Network stats per uid, if any
1375 long mobileRx = u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1376 long mobileTx = u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1377 long wifiRx = u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1378 long wifiTx = u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
1379 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
1380 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
1381 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
1383 if (mobileRx > 0 || mobileTx > 0 || wifiRx > 0 || wifiTx > 0) {
1384 dumpLine(pw, uid, category, NETWORK_DATA, mobileRx, mobileTx, wifiRx, wifiTx);
1387 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
1388 || uidWifiRunningTime != 0) {
1389 dumpLine(pw, uid, category, WIFI_DATA,
1390 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
1393 if (u.hasUserActivity()) {
1394 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1395 boolean hasData = false;
1396 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1397 int val = u.getUserActivityCount(i, which);
1399 if (val != 0) hasData = true;
1402 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1406 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1407 if (wakelocks.size() > 0) {
1408 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1409 : wakelocks.entrySet()) {
1410 Uid.Wakelock wl = ent.getValue();
1411 String linePrefix = "";
1413 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
1414 batteryRealtime, "f", which, linePrefix);
1415 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
1416 batteryRealtime, "p", which, linePrefix);
1417 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
1418 batteryRealtime, "w", which, linePrefix);
1420 // Only log if we had at lease one wakelock...
1421 if (sb.length() > 0) {
1422 String name = ent.getKey();
1423 if (name.indexOf(',') >= 0) {
1424 name = name.replace(',', '_');
1426 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
1431 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1432 if (sensors.size() > 0) {
1433 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1434 : sensors.entrySet()) {
1435 Uid.Sensor se = ent.getValue();
1436 int sensorNumber = ent.getKey();
1437 Timer timer = se.getSensorTime();
1438 if (timer != null) {
1439 // Convert from microseconds to milliseconds with rounding
1440 long totalTime = (timer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1441 int count = timer.getCountLocked(which);
1442 if (totalTime != 0) {
1443 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1449 Timer vibTimer = u.getVibratorOnTimer();
1450 if (vibTimer != null) {
1451 // Convert from microseconds to milliseconds with rounding
1452 long totalTime = (vibTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1453 int count = vibTimer.getCountLocked(which);
1454 if (totalTime != 0) {
1455 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1459 Timer fgTimer = u.getForegroundActivityTimer();
1460 if (fgTimer != null) {
1461 // Convert from microseconds to milliseconds with rounding
1462 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1463 int count = fgTimer.getCountLocked(which);
1464 if (totalTime != 0) {
1465 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1469 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1470 if (processStats.size() > 0) {
1471 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1472 : processStats.entrySet()) {
1473 Uid.Proc ps = ent.getValue();
1475 final long userMillis = ps.getUserTime(which) * 10;
1476 final long systemMillis = ps.getSystemTime(which) * 10;
1477 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1478 final long starts = ps.getStarts(which);
1480 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1482 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1483 systemMillis, foregroundMillis, starts);
1488 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1489 if (packageStats.size() > 0) {
1490 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1491 : packageStats.entrySet()) {
1493 Uid.Pkg ps = ent.getValue();
1494 int wakeups = ps.getWakeups(which);
1495 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1496 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1497 : serviceStats.entrySet()) {
1498 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1499 long startTime = ss.getStartTime(batteryUptime, which);
1500 int starts = ss.getStarts(which);
1501 int launches = ss.getLaunches(which);
1502 if (startTime != 0 || starts != 0 || launches != 0) {
1503 dumpLine(pw, uid, category, APK_DATA,
1504 wakeups, // wakeup alarms
1505 ent.getKey(), // Apk
1506 sent.getKey(), // service
1507 startTime / 1000, // time spent started, in ms
1517 static final class TimerEntry {
1520 final BatteryStats.Timer mTimer;
1522 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1530 @SuppressWarnings("unused")
1531 public final void dumpLocked(PrintWriter pw, String prefix, final int which, int reqUid) {
1532 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1533 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1534 final long batteryUptime = getBatteryUptime(rawUptime);
1535 final long batteryRealtime = getBatteryRealtime(rawRealtime);
1537 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1538 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1539 final long totalRealtime = computeRealtime(rawRealtime, which);
1540 final long totalUptime = computeUptime(rawUptime, which);
1542 StringBuilder sb = new StringBuilder(128);
1544 SparseArray<? extends Uid> uidStats = getUidStats();
1545 final int NU = uidStats.size();
1549 sb.append(" Time on battery: ");
1550 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1551 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1552 sb.append(") realtime, ");
1553 formatTimeMs(sb, whichBatteryUptime / 1000);
1554 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1555 sb.append(") uptime");
1556 pw.println(sb.toString());
1559 sb.append(" Total run time: ");
1560 formatTimeMs(sb, totalRealtime / 1000);
1561 sb.append("realtime, ");
1562 formatTimeMs(sb, totalUptime / 1000);
1563 sb.append("uptime, ");
1564 pw.println(sb.toString());
1566 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1567 final long interactiveTime = getInteractiveTime(batteryRealtime, which);
1568 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
1569 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
1570 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
1571 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
1574 sb.append(" Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
1575 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
1577 pw.println(sb.toString());
1580 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
1581 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
1582 sb.append("), Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
1583 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
1585 pw.println(sb.toString());
1588 sb.append(" Screen brightnesses: ");
1589 boolean didOne = false;
1590 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1591 final long time = getScreenBrightnessTime(i, batteryRealtime, which);
1595 if (didOne) sb.append(", ");
1597 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
1599 formatTimeMs(sb, time/1000);
1601 sb.append(formatRatioLocked(time, screenOnTime));
1604 if (!didOne) sb.append("No activity");
1605 pw.println(sb.toString());
1607 // Calculate total network and wakelock times across all uids.
1608 long mobileRxTotal = 0;
1609 long mobileTxTotal = 0;
1610 long wifiRxTotal = 0;
1611 long wifiTxTotal = 0;
1612 long fullWakeLockTimeTotalMicros = 0;
1613 long partialWakeLockTimeTotalMicros = 0;
1615 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
1617 public int compare(TimerEntry lhs, TimerEntry rhs) {
1618 long lhsTime = lhs.mTime;
1619 long rhsTime = rhs.mTime;
1620 if (lhsTime < rhsTime) {
1623 if (lhsTime > rhsTime) {
1631 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1632 if (kernelWakelocks.size() > 0) {
1633 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1634 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1635 BatteryStats.Timer timer = ent.getValue();
1636 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
1637 if (totalTimeMillis > 0) {
1638 timers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
1641 Collections.sort(timers, timerComparator);
1642 for (int i=0; i<timers.size(); i++) {
1643 TimerEntry timer = timers.get(i);
1644 String linePrefix = ": ";
1647 sb.append(" Kernel Wake lock ");
1648 sb.append(timer.mName);
1649 linePrefix = printWakeLock(sb, timer.mTimer, batteryRealtime, null,
1651 if (!linePrefix.equals(": ")) {
1652 sb.append(" realtime");
1653 // Only print out wake locks that were held
1654 pw.println(sb.toString());
1660 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1662 for (int iu = 0; iu < NU; iu++) {
1663 Uid u = uidStats.valueAt(iu);
1664 mobileRxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1665 mobileTxTotal += u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1666 wifiRxTotal += u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1667 wifiTxTotal += u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
1669 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1670 if (wakelocks.size() > 0) {
1671 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1672 : wakelocks.entrySet()) {
1673 Uid.Wakelock wl = ent.getValue();
1675 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1676 if (fullWakeTimer != null) {
1677 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
1678 batteryRealtime, which);
1681 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1682 if (partialWakeTimer != null) {
1683 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
1684 batteryRealtime, which);
1685 if (totalTimeMicros > 0) {
1687 // Only show the ordered list of all wake
1688 // locks if the caller is not asking for data
1689 // about a specific uid.
1690 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
1691 partialWakeTimer, totalTimeMicros));
1693 partialWakeLockTimeTotalMicros += totalTimeMicros;
1701 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotal));
1702 pw.print(", Total sent: "); pw.println(formatBytesLocked(mobileTxTotal));
1704 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotal));
1705 pw.print(", Total sent: "); pw.println(formatBytesLocked(wifiTxTotal));
1708 sb.append(" Total full wakelock time: "); formatTimeMs(sb,
1709 (fullWakeLockTimeTotalMicros + 500) / 1000);
1710 sb.append(", Total partial wakelock time: "); formatTimeMs(sb,
1711 (partialWakeLockTimeTotalMicros + 500) / 1000);
1712 pw.println(sb.toString());
1716 sb.append(" Signal levels: ");
1718 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
1719 final long time = getPhoneSignalStrengthTime(i, batteryRealtime, which);
1723 if (didOne) sb.append(", ");
1725 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
1727 formatTimeMs(sb, time/1000);
1729 sb.append(formatRatioLocked(time, whichBatteryRealtime));
1731 sb.append(getPhoneSignalStrengthCount(i, which));
1734 if (!didOne) sb.append("No activity");
1735 pw.println(sb.toString());
1739 sb.append(" Signal scanning time: ");
1740 formatTimeMs(sb, getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
1741 pw.println(sb.toString());
1745 sb.append(" Radio types: ");
1747 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1748 final long time = getPhoneDataConnectionTime(i, batteryRealtime, which);
1752 if (didOne) sb.append(", ");
1754 sb.append(DATA_CONNECTION_NAMES[i]);
1756 formatTimeMs(sb, time/1000);
1758 sb.append(formatRatioLocked(time, whichBatteryRealtime));
1760 sb.append(getPhoneDataConnectionCount(i, which));
1763 if (!didOne) sb.append("No activity");
1764 pw.println(sb.toString());
1768 sb.append(" Radio data uptime when unplugged: ");
1769 sb.append(getRadioDataUptime() / 1000);
1771 pw.println(sb.toString());
1775 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
1776 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
1777 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
1778 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
1779 sb.append("), Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
1780 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
1782 pw.println(sb.toString());
1786 if (which == STATS_SINCE_UNPLUGGED) {
1787 if (getIsOnBattery()) {
1788 pw.print(prefix); pw.println(" Device is currently unplugged");
1789 pw.print(prefix); pw.print(" Discharge cycle start level: ");
1790 pw.println(getDischargeStartLevel());
1791 pw.print(prefix); pw.print(" Discharge cycle current level: ");
1792 pw.println(getDischargeCurrentLevel());
1794 pw.print(prefix); pw.println(" Device is currently plugged into power");
1795 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
1796 pw.println(getDischargeStartLevel());
1797 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
1798 pw.println(getDischargeCurrentLevel());
1800 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1801 pw.println(getDischargeAmountScreenOn());
1802 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1803 pw.println(getDischargeAmountScreenOff());
1806 pw.print(prefix); pw.println(" Device battery use since last full charge");
1807 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
1808 pw.println(getLowDischargeAmountSinceCharge());
1809 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
1810 pw.println(getHighDischargeAmountSinceCharge());
1811 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1812 pw.println(getDischargeAmountScreenOnSinceCharge());
1813 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1814 pw.println(getDischargeAmountScreenOffSinceCharge());
1818 if (timers.size() > 0) {
1819 Collections.sort(timers, timerComparator);
1820 pw.print(prefix); pw.println(" All partial wake locks:");
1821 for (int i=0; i<timers.size(); i++) {
1822 TimerEntry timer = timers.get(i);
1824 sb.append(" Wake lock ");
1825 UserHandle.formatUid(sb, timer.mId);
1827 sb.append(timer.mName);
1828 printWakeLock(sb, timer.mTimer, batteryRealtime, null, which, ": ");
1829 sb.append(" realtime");
1830 pw.println(sb.toString());
1836 for (int iu=0; iu<NU; iu++) {
1837 final int uid = uidStats.keyAt(iu);
1838 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
1842 Uid u = uidStats.valueAt(iu);
1846 UserHandle.formatUid(pw, uid);
1848 boolean uidActivity = false;
1850 long mobileRxBytes = u.getNetworkActivityCount(NETWORK_MOBILE_RX_BYTES, which);
1851 long mobileTxBytes = u.getNetworkActivityCount(NETWORK_MOBILE_TX_BYTES, which);
1852 long wifiRxBytes = u.getNetworkActivityCount(NETWORK_WIFI_RX_BYTES, which);
1853 long wifiTxBytes = u.getNetworkActivityCount(NETWORK_WIFI_TX_BYTES, which);
1854 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
1855 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
1856 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
1858 if (mobileRxBytes > 0 || mobileTxBytes > 0) {
1859 pw.print(prefix); pw.print(" Mobile network: ");
1860 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
1861 pw.print(formatBytesLocked(mobileTxBytes)); pw.println(" sent");
1863 if (wifiRxBytes > 0 || wifiTxBytes > 0) {
1864 pw.print(prefix); pw.print(" Wi-Fi network: ");
1865 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
1866 pw.print(formatBytesLocked(wifiTxBytes)); pw.println(" sent");
1869 if (u.hasUserActivity()) {
1870 boolean hasData = false;
1871 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1872 int val = u.getUserActivityCount(i, which);
1876 sb.append(" User activity: ");
1883 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
1887 pw.println(sb.toString());
1891 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
1892 || uidWifiRunningTime != 0) {
1894 sb.append(prefix); sb.append(" Wifi Running: ");
1895 formatTimeMs(sb, uidWifiRunningTime / 1000);
1896 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
1897 whichBatteryRealtime)); sb.append(")\n");
1898 sb.append(prefix); sb.append(" Full Wifi Lock: ");
1899 formatTimeMs(sb, fullWifiLockOnTime / 1000);
1900 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
1901 whichBatteryRealtime)); sb.append(")\n");
1902 sb.append(prefix); sb.append(" Wifi Scan: ");
1903 formatTimeMs(sb, wifiScanTime / 1000);
1904 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
1905 whichBatteryRealtime)); sb.append(")");
1906 pw.println(sb.toString());
1909 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1910 if (wakelocks.size() > 0) {
1911 long totalFull = 0, totalPartial = 0, totalWindow = 0;
1913 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1914 : wakelocks.entrySet()) {
1915 Uid.Wakelock wl = ent.getValue();
1916 String linePrefix = ": ";
1919 sb.append(" Wake lock ");
1920 sb.append(ent.getKey());
1921 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), batteryRealtime,
1922 "full", which, linePrefix);
1923 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), batteryRealtime,
1924 "partial", which, linePrefix);
1925 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), batteryRealtime,
1926 "window", which, linePrefix);
1927 if (!linePrefix.equals(": ")) {
1928 sb.append(" realtime");
1929 // Only print out wake locks that were held
1930 pw.println(sb.toString());
1934 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
1935 batteryRealtime, which);
1936 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
1937 batteryRealtime, which);
1938 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
1939 batteryRealtime, which);
1942 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
1945 sb.append(" TOTAL wake: ");
1946 boolean needComma = false;
1947 if (totalFull != 0) {
1949 formatTimeMs(sb, totalFull);
1952 if (totalPartial != 0) {
1957 formatTimeMs(sb, totalPartial);
1958 sb.append("partial");
1960 if (totalWindow != 0) {
1965 formatTimeMs(sb, totalWindow);
1966 sb.append("window");
1968 sb.append(" realtime");
1969 pw.println(sb.toString());
1974 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1975 if (sensors.size() > 0) {
1976 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1977 : sensors.entrySet()) {
1978 Uid.Sensor se = ent.getValue();
1979 int sensorNumber = ent.getKey();
1982 sb.append(" Sensor ");
1983 int handle = se.getHandle();
1984 if (handle == Uid.Sensor.GPS) {
1991 Timer timer = se.getSensorTime();
1992 if (timer != null) {
1993 // Convert from microseconds to milliseconds with rounding
1994 long totalTime = (timer.getTotalTimeLocked(
1995 batteryRealtime, which) + 500) / 1000;
1996 int count = timer.getCountLocked(which);
1998 if (totalTime != 0) {
1999 formatTimeMs(sb, totalTime);
2000 sb.append("realtime (");
2002 sb.append(" times)");
2004 sb.append("(not used)");
2007 sb.append("(not used)");
2010 pw.println(sb.toString());
2015 Timer vibTimer = u.getVibratorOnTimer();
2016 if (vibTimer != null) {
2017 // Convert from microseconds to milliseconds with rounding
2018 long totalTime = (vibTimer.getTotalTimeLocked(
2019 batteryRealtime, which) + 500) / 1000;
2020 int count = vibTimer.getCountLocked(which);
2022 if (totalTime != 0) {
2025 sb.append(" Vibrator: ");
2026 formatTimeMs(sb, totalTime);
2027 sb.append("realtime (");
2029 sb.append(" times)");
2030 pw.println(sb.toString());
2035 Timer fgTimer = u.getForegroundActivityTimer();
2036 if (fgTimer != null) {
2037 // Convert from microseconds to milliseconds with rounding
2038 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
2039 int count = fgTimer.getCountLocked(which);
2040 if (totalTime != 0) {
2043 sb.append(" Foreground activities: ");
2044 formatTimeMs(sb, totalTime);
2045 sb.append("realtime (");
2047 sb.append(" times)");
2048 pw.println(sb.toString());
2053 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2054 if (processStats.size() > 0) {
2055 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2056 : processStats.entrySet()) {
2057 Uid.Proc ps = ent.getValue();
2060 long foregroundTime;
2064 userTime = ps.getUserTime(which);
2065 systemTime = ps.getSystemTime(which);
2066 foregroundTime = ps.getForegroundTime(which);
2067 starts = ps.getStarts(which);
2068 numExcessive = which == STATS_SINCE_CHARGED
2069 ? ps.countExcessivePowers() : 0;
2071 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
2072 || numExcessive != 0) {
2074 sb.append(prefix); sb.append(" Proc ");
2075 sb.append(ent.getKey()); sb.append(":\n");
2076 sb.append(prefix); sb.append(" CPU: ");
2077 formatTime(sb, userTime); sb.append("usr + ");
2078 formatTime(sb, systemTime); sb.append("krn ; ");
2079 formatTime(sb, foregroundTime); sb.append("fg");
2081 sb.append("\n"); sb.append(prefix); sb.append(" ");
2082 sb.append(starts); sb.append(" proc starts");
2084 pw.println(sb.toString());
2085 for (int e=0; e<numExcessive; e++) {
2086 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
2088 pw.print(prefix); pw.print(" * Killed for ");
2089 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2090 pw.print("wake lock");
2091 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2094 pw.print("unknown");
2097 TimeUtils.formatDuration(ew.usedTime, pw);
2099 TimeUtils.formatDuration(ew.overTime, pw);
2100 if (ew.overTime != 0) {
2102 pw.print((ew.usedTime*100)/ew.overTime);
2112 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2113 if (packageStats.size() > 0) {
2114 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2115 : packageStats.entrySet()) {
2116 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
2117 boolean apkActivity = false;
2118 Uid.Pkg ps = ent.getValue();
2119 int wakeups = ps.getWakeups(which);
2121 pw.print(prefix); pw.print(" ");
2122 pw.print(wakeups); pw.println(" wakeup alarms");
2125 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2126 if (serviceStats.size() > 0) {
2127 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2128 : serviceStats.entrySet()) {
2129 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2130 long startTime = ss.getStartTime(batteryUptime, which);
2131 int starts = ss.getStarts(which);
2132 int launches = ss.getLaunches(which);
2133 if (startTime != 0 || starts != 0 || launches != 0) {
2135 sb.append(prefix); sb.append(" Service ");
2136 sb.append(sent.getKey()); sb.append(":\n");
2137 sb.append(prefix); sb.append(" Created for: ");
2138 formatTimeMs(sb, startTime / 1000);
2139 sb.append("uptime\n");
2140 sb.append(prefix); sb.append(" Starts: ");
2142 sb.append(", launches: "); sb.append(launches);
2143 pw.println(sb.toString());
2149 pw.print(prefix); pw.println(" (nothing executed)");
2155 pw.print(prefix); pw.println(" (nothing executed)");
2160 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, BitDescription[] descriptions) {
2161 int diff = oldval ^ newval;
2162 if (diff == 0) return;
2163 for (int i=0; i<descriptions.length; i++) {
2164 BitDescription bd = descriptions[i];
2165 if ((diff&bd.mask) != 0) {
2167 pw.print((newval&bd.mask) != 0 ? " +" : " -");
2173 int val = (newval&bd.mask)>>bd.shift;
2174 if (bd.values != null && val >= 0 && val < bd.values.length) {
2175 pw.print(bd.values[val]);
2184 public void prepareForDumpLocked() {
2187 public static class HistoryPrinter {
2195 public void printNextItem(PrintWriter pw, HistoryItem rec, long now) {
2197 TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
2199 if (rec.cmd == HistoryItem.CMD_START) {
2200 pw.println(" START");
2201 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2202 pw.println(" *OVERFLOW*");
2204 if (rec.batteryLevel < 10) pw.print("00");
2205 else if (rec.batteryLevel < 100) pw.print("0");
2206 pw.print(rec.batteryLevel);
2208 if (rec.states < 0x10) pw.print("0000000");
2209 else if (rec.states < 0x100) pw.print("000000");
2210 else if (rec.states < 0x1000) pw.print("00000");
2211 else if (rec.states < 0x10000) pw.print("0000");
2212 else if (rec.states < 0x100000) pw.print("000");
2213 else if (rec.states < 0x1000000) pw.print("00");
2214 else if (rec.states < 0x10000000) pw.print("0");
2215 pw.print(Integer.toHexString(rec.states));
2216 if (oldStatus != rec.batteryStatus) {
2217 oldStatus = rec.batteryStatus;
2218 pw.print(" status=");
2219 switch (oldStatus) {
2220 case BatteryManager.BATTERY_STATUS_UNKNOWN:
2221 pw.print("unknown");
2223 case BatteryManager.BATTERY_STATUS_CHARGING:
2224 pw.print("charging");
2226 case BatteryManager.BATTERY_STATUS_DISCHARGING:
2227 pw.print("discharging");
2229 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
2230 pw.print("not-charging");
2232 case BatteryManager.BATTERY_STATUS_FULL:
2236 pw.print(oldStatus);
2240 if (oldHealth != rec.batteryHealth) {
2241 oldHealth = rec.batteryHealth;
2242 pw.print(" health=");
2243 switch (oldHealth) {
2244 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
2245 pw.print("unknown");
2247 case BatteryManager.BATTERY_HEALTH_GOOD:
2250 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
2251 pw.print("overheat");
2253 case BatteryManager.BATTERY_HEALTH_DEAD:
2256 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
2257 pw.print("over-voltage");
2259 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
2260 pw.print("failure");
2263 pw.print(oldHealth);
2267 if (oldPlug != rec.batteryPlugType) {
2268 oldPlug = rec.batteryPlugType;
2274 case BatteryManager.BATTERY_PLUGGED_AC:
2277 case BatteryManager.BATTERY_PLUGGED_USB:
2280 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
2281 pw.print("wireless");
2288 if (oldTemp != rec.batteryTemperature) {
2289 oldTemp = rec.batteryTemperature;
2293 if (oldVolt != rec.batteryVoltage) {
2294 oldVolt = rec.batteryVoltage;
2298 printBitDescriptions(pw, oldState, rec.states,
2299 HISTORY_STATE_DESCRIPTIONS);
2302 oldState = rec.states;
2305 public void printNextItemCheckin(PrintWriter pw, HistoryItem rec, long now) {
2306 pw.print(rec.time-now);
2308 if (rec.cmd == HistoryItem.CMD_START) {
2310 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2311 pw.print("overflow");
2313 pw.print(rec.batteryLevel);
2315 pw.print(rec.states);
2317 pw.print(rec.batteryStatus);
2319 pw.print(rec.batteryHealth);
2321 pw.print(rec.batteryPlugType);
2323 pw.print((int)rec.batteryTemperature);
2325 pw.print((int)rec.batteryVoltage);
2331 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
2333 * @param pw a Printer to receive the dump output.
2335 @SuppressWarnings("unused")
2336 public void dumpLocked(PrintWriter pw, boolean isUnpluggedOnly, int reqUid) {
2337 prepareForDumpLocked();
2339 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2341 final HistoryItem rec = new HistoryItem();
2342 if (startIteratingHistoryLocked()) {
2343 pw.println("Battery History:");
2344 HistoryPrinter hprinter = new HistoryPrinter();
2345 while (getNextHistoryLocked(rec)) {
2346 hprinter.printNextItem(pw, rec, now);
2348 finishIteratingHistoryLocked();
2352 if (startIteratingOldHistoryLocked()) {
2353 pw.println("Old battery History:");
2354 HistoryPrinter hprinter = new HistoryPrinter();
2355 while (getNextOldHistoryLocked(rec)) {
2356 hprinter.printNextItem(pw, rec, now);
2358 finishIteratingOldHistoryLocked();
2362 SparseArray<? extends Uid> uidStats = getUidStats();
2363 final int NU = uidStats.size();
2364 boolean didPid = false;
2365 long nowRealtime = SystemClock.elapsedRealtime();
2366 for (int i=0; i<NU; i++) {
2367 Uid uid = uidStats.valueAt(i);
2368 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
2370 for (int j=0; j<pids.size(); j++) {
2371 Uid.Pid pid = pids.valueAt(j);
2373 pw.println("Per-PID Stats:");
2376 long time = pid.mWakeSum + (pid.mWakeStart != 0
2377 ? (nowRealtime - pid.mWakeStart) : 0);
2378 pw.print(" PID "); pw.print(pids.keyAt(j));
2379 pw.print(" wake time: ");
2380 TimeUtils.formatDuration(time, pw);
2389 if (!isUnpluggedOnly) {
2390 pw.println("Statistics since last charge:");
2391 pw.println(" System starts: " + getStartCount()
2392 + ", currently on battery: " + getIsOnBattery());
2393 dumpLocked(pw, "", STATS_SINCE_CHARGED, reqUid);
2396 pw.println("Statistics since last unplugged:");
2397 dumpLocked(pw, "", STATS_SINCE_UNPLUGGED, reqUid);
2400 @SuppressWarnings("unused")
2401 public void dumpCheckinLocked(
2402 PrintWriter pw, List<ApplicationInfo> apps, boolean isUnpluggedOnly,
2403 boolean includeHistory) {
2404 prepareForDumpLocked();
2406 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2408 if (includeHistory) {
2409 final HistoryItem rec = new HistoryItem();
2410 if (startIteratingHistoryLocked()) {
2411 HistoryPrinter hprinter = new HistoryPrinter();
2412 while (getNextHistoryLocked(rec)) {
2413 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
2414 pw.print(0); pw.print(',');
2415 pw.print(HISTORY_DATA); pw.print(',');
2416 hprinter.printNextItemCheckin(pw, rec, now);
2419 finishIteratingHistoryLocked();
2424 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
2425 for (int i=0; i<apps.size(); i++) {
2426 ApplicationInfo ai = apps.get(i);
2427 ArrayList<String> pkgs = uids.get(ai.uid);
2429 pkgs = new ArrayList<String>();
2430 uids.put(ai.uid, pkgs);
2432 pkgs.add(ai.packageName);
2434 SparseArray<? extends Uid> uidStats = getUidStats();
2435 final int NU = uidStats.size();
2436 String[] lineArgs = new String[2];
2437 for (int i=0; i<NU; i++) {
2438 int uid = uidStats.keyAt(i);
2439 ArrayList<String> pkgs = uids.get(uid);
2441 for (int j=0; j<pkgs.size(); j++) {
2442 lineArgs[0] = Integer.toString(uid);
2443 lineArgs[1] = pkgs.get(j);
2444 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
2445 (Object[])lineArgs);
2450 if (isUnpluggedOnly) {
2451 dumpCheckinLocked(pw, STATS_SINCE_UNPLUGGED, -1);
2454 dumpCheckinLocked(pw, STATS_SINCE_CHARGED, -1);
2455 dumpCheckinLocked(pw, STATS_SINCE_UNPLUGGED, -1);