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.HashMap;
25 import java.util.List;
28 import android.content.Context;
29 import android.content.pm.ApplicationInfo;
30 import android.telephony.SignalStrength;
31 import android.text.format.DateFormat;
32 import android.util.Printer;
33 import android.util.SparseArray;
34 import android.util.SparseIntArray;
35 import android.util.TimeUtils;
36 import android.view.Display;
37 import com.android.internal.os.BatterySipper;
38 import com.android.internal.os.BatteryStatsHelper;
41 * A class providing access to battery usage statistics, including information on
42 * wakelocks, processes, packages, and services. All times are represented in microseconds
43 * except where indicated otherwise.
46 public abstract class BatteryStats implements Parcelable {
48 private static final boolean LOCAL_LOGV = false;
51 public static final String SERVICE_NAME = "batterystats";
54 * A constant indicating a partial wake lock timer.
56 public static final int WAKE_TYPE_PARTIAL = 0;
59 * A constant indicating a full wake lock timer.
61 public static final int WAKE_TYPE_FULL = 1;
64 * A constant indicating a window wake lock timer.
66 public static final int WAKE_TYPE_WINDOW = 2;
69 * A constant indicating a sensor timer.
71 public static final int SENSOR = 3;
74 * A constant indicating a a wifi running timer
76 public static final int WIFI_RUNNING = 4;
79 * A constant indicating a full wifi lock timer
81 public static final int FULL_WIFI_LOCK = 5;
84 * A constant indicating a wifi scan
86 public static final int WIFI_SCAN = 6;
89 * A constant indicating a wifi multicast timer
91 public static final int WIFI_MULTICAST_ENABLED = 7;
94 * A constant indicating an audio turn on timer
96 public static final int AUDIO_TURNED_ON = 7;
99 * A constant indicating a video turn on timer
101 public static final int VIDEO_TURNED_ON = 8;
104 * A constant indicating a vibrator on timer
106 public static final int VIBRATOR_ON = 9;
109 * A constant indicating a foreground activity timer
111 public static final int FOREGROUND_ACTIVITY = 10;
114 * A constant indicating a wifi batched scan is active
116 public static final int WIFI_BATCHED_SCAN = 11;
119 * A constant indicating a process state timer
121 public static final int PROCESS_STATE = 12;
124 * A constant indicating a sync timer
126 public static final int SYNC = 13;
129 * A constant indicating a job timer
131 public static final int JOB = 14;
134 * Include all of the data in the stats, including previously saved data.
136 public static final int STATS_SINCE_CHARGED = 0;
139 * Include only the current run in the stats.
141 public static final int STATS_CURRENT = 1;
144 * Include only the run since the last time the device was unplugged in the stats.
146 public static final int STATS_SINCE_UNPLUGGED = 2;
148 // NOTE: Update this list if you add/change any stats above.
149 // These characters are supposed to represent "total", "last", "current",
150 // and "unplugged". They were shortened for efficiency sake.
151 private static final String[] STAT_NAMES = { "l", "c", "u" };
154 * Bump the version on this if the checkin format changes.
156 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
158 private static final long BYTES_PER_KB = 1024;
159 private static final long BYTES_PER_MB = 1048576; // 1024^2
160 private static final long BYTES_PER_GB = 1073741824; //1024^3
162 private static final String VERSION_DATA = "vers";
163 private static final String UID_DATA = "uid";
164 private static final String APK_DATA = "apk";
165 private static final String PROCESS_DATA = "pr";
166 private static final String SENSOR_DATA = "sr";
167 private static final String VIBRATOR_DATA = "vib";
168 private static final String FOREGROUND_DATA = "fg";
169 private static final String STATE_TIME_DATA = "st";
170 private static final String WAKELOCK_DATA = "wl";
171 private static final String SYNC_DATA = "sy";
172 private static final String JOB_DATA = "jb";
173 private static final String KERNEL_WAKELOCK_DATA = "kwl";
174 private static final String WAKEUP_REASON_DATA = "wr";
175 private static final String NETWORK_DATA = "nt";
176 private static final String USER_ACTIVITY_DATA = "ua";
177 private static final String BATTERY_DATA = "bt";
178 private static final String BATTERY_DISCHARGE_DATA = "dc";
179 private static final String BATTERY_LEVEL_DATA = "lv";
180 private static final String WIFI_DATA = "wfl";
181 private static final String MISC_DATA = "m";
182 private static final String GLOBAL_NETWORK_DATA = "gn";
183 private static final String HISTORY_STRING_POOL = "hsp";
184 private static final String HISTORY_DATA = "h";
185 private static final String SCREEN_BRIGHTNESS_DATA = "br";
186 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
187 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
188 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
189 private static final String DATA_CONNECTION_TIME_DATA = "dct";
190 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
191 private static final String WIFI_STATE_TIME_DATA = "wst";
192 private static final String WIFI_STATE_COUNT_DATA = "wsc";
193 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
194 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
195 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
196 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
197 private static final String BLUETOOTH_STATE_TIME_DATA = "bst";
198 private static final String BLUETOOTH_STATE_COUNT_DATA = "bsc";
199 private static final String POWER_USE_SUMMARY_DATA = "pws";
200 private static final String POWER_USE_ITEM_DATA = "pwi";
201 private static final String DISCHARGE_STEP_DATA = "dsd";
202 private static final String CHARGE_STEP_DATA = "csd";
203 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
204 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
206 private final StringBuilder mFormatBuilder = new StringBuilder(32);
207 private final Formatter mFormatter = new Formatter(mFormatBuilder);
210 * State for keeping track of counting information.
212 public static abstract class Counter {
215 * Returns the count associated with this Counter for the
216 * selected type of statistics.
218 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
220 public abstract int getCountLocked(int which);
223 * Temporary for debugging.
225 public abstract void logState(Printer pw, String prefix);
229 * State for keeping track of long counting information.
231 public static abstract class LongCounter {
234 * Returns the count associated with this Counter for the
235 * selected type of statistics.
237 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
239 public abstract long getCountLocked(int which);
242 * Temporary for debugging.
244 public abstract void logState(Printer pw, String prefix);
248 * State for keeping track of timing information.
250 public static abstract class Timer {
253 * Returns the count associated with this Timer for the
254 * selected type of statistics.
256 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
258 public abstract int getCountLocked(int which);
261 * Returns the total time in microseconds associated with this Timer for the
262 * selected type of statistics.
264 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
265 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
266 * @return a time in microseconds
268 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
271 * Temporary for debugging.
273 public abstract void logState(Printer pw, String prefix);
277 * The statistics associated with a particular uid.
279 public static abstract class Uid {
282 * Returns a mapping containing wakelock statistics.
284 * @return a Map from Strings to Uid.Wakelock objects.
286 public abstract Map<String, ? extends Wakelock> getWakelockStats();
289 * Returns a mapping containing sync statistics.
291 * @return a Map from Strings to Timer objects.
293 public abstract Map<String, ? extends Timer> getSyncStats();
296 * Returns a mapping containing scheduled job statistics.
298 * @return a Map from Strings to Timer objects.
300 public abstract Map<String, ? extends Timer> getJobStats();
303 * The statistics associated with a particular wake lock.
305 public static abstract class Wakelock {
306 public abstract Timer getWakeTime(int type);
310 * Returns a mapping containing sensor statistics.
312 * @return a Map from Integer sensor ids to Uid.Sensor objects.
314 public abstract SparseArray<? extends Sensor> getSensorStats();
317 * Returns a mapping containing active process data.
319 public abstract SparseArray<? extends Pid> getPidStats();
322 * Returns a mapping containing process statistics.
324 * @return a Map from Strings to Uid.Proc objects.
326 public abstract Map<String, ? extends Proc> getProcessStats();
329 * Returns a mapping containing package statistics.
331 * @return a Map from Strings to Uid.Pkg objects.
333 public abstract Map<String, ? extends Pkg> getPackageStats();
338 public abstract int getUid();
340 public abstract void noteWifiRunningLocked(long elapsedRealtime);
341 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
342 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
343 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
344 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
345 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
346 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
347 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
348 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
349 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
350 public abstract void noteActivityResumedLocked(long elapsedRealtime);
351 public abstract void noteActivityPausedLocked(long elapsedRealtime);
352 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
353 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
354 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
355 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
356 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
357 public abstract long getAudioTurnedOnTime(long elapsedRealtimeUs, int which);
358 public abstract long getVideoTurnedOnTime(long elapsedRealtimeUs, int which);
359 public abstract Timer getForegroundActivityTimer();
361 // Time this uid has any processes in foreground state.
362 public static final int PROCESS_STATE_FOREGROUND = 0;
363 // Time this uid has any process in active state (not cached).
364 public static final int PROCESS_STATE_ACTIVE = 1;
365 // Time this uid has any processes running at all.
366 public static final int PROCESS_STATE_RUNNING = 2;
367 // Total number of process states we track.
368 public static final int NUM_PROCESS_STATE = 3;
370 static final String[] PROCESS_STATE_NAMES = {
371 "Foreground", "Active", "Running"
374 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
376 public abstract Timer getVibratorOnTimer();
378 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
381 * Note that these must match the constants in android.os.PowerManager.
382 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
385 static final String[] USER_ACTIVITY_TYPES = {
386 "other", "button", "touch"
389 public static final int NUM_USER_ACTIVITY_TYPES = 3;
391 public abstract void noteUserActivityLocked(int type);
392 public abstract boolean hasUserActivity();
393 public abstract int getUserActivityCount(int type, int which);
395 public abstract boolean hasNetworkActivity();
396 public abstract long getNetworkActivityBytes(int type, int which);
397 public abstract long getNetworkActivityPackets(int type, int which);
398 public abstract long getMobileRadioActiveTime(int which);
399 public abstract int getMobileRadioActiveCount(int which);
401 public static abstract class Sensor {
403 * FIXME: it's not correct to use this magic value because it
404 * could clash with a sensor handle (which are defined by
405 * the sensor HAL, and therefore out of our control
407 // Magic sensor number for the GPS.
408 public static final int GPS = -10000;
410 public abstract int getHandle();
412 public abstract Timer getSensorTime();
416 public int mWakeNesting;
417 public long mWakeSumMs;
418 public long mWakeStartMs;
422 * The statistics associated with a particular process.
424 public static abstract class Proc {
426 public static class ExcessivePower {
427 public static final int TYPE_WAKE = 1;
428 public static final int TYPE_CPU = 2;
431 public long overTime;
432 public long usedTime;
436 * Returns true if this process is still active in the battery stats.
438 public abstract boolean isActive();
441 * Returns the total time (in 1/100 sec) spent executing in user code.
443 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
445 public abstract long getUserTime(int which);
448 * Returns the total time (in 1/100 sec) spent executing in system code.
450 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
452 public abstract long getSystemTime(int which);
455 * Returns the number of times the process has been started.
457 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
459 public abstract int getStarts(int which);
462 * Returns the cpu time spent in microseconds while the process was in the foreground.
463 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
464 * @return foreground cpu time in microseconds
466 public abstract long getForegroundTime(int which);
469 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
470 * @param speedStep the index of the CPU speed. This is not the actual speed of the
472 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
473 * @see BatteryStats#getCpuSpeedSteps()
475 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
477 public abstract int countExcessivePowers();
479 public abstract ExcessivePower getExcessivePower(int i);
483 * The statistics associated with a particular package.
485 public static abstract class Pkg {
488 * Returns the number of times this package has done something that could wake up the
491 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
493 public abstract int getWakeups(int which);
496 * Returns a mapping containing service statistics.
498 public abstract Map<String, ? extends Serv> getServiceStats();
501 * The statistics associated with a particular service.
503 public abstract class Serv {
506 * Returns the amount of time spent started.
508 * @param batteryUptime elapsed uptime on battery in microseconds.
509 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
512 public abstract long getStartTime(long batteryUptime, int which);
515 * Returns the total number of times startService() has been called.
517 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
519 public abstract int getStarts(int which);
522 * Returns the total number times the service has been launched.
524 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
526 public abstract int getLaunches(int which);
531 public final static class HistoryTag {
532 public String string;
537 public void setTo(HistoryTag o) {
543 public void setTo(String _string, int _uid) {
549 public void writeToParcel(Parcel dest, int flags) {
550 dest.writeString(string);
554 public void readFromParcel(Parcel src) {
555 string = src.readString();
561 public boolean equals(Object o) {
562 if (this == o) return true;
563 if (o == null || getClass() != o.getClass()) return false;
565 HistoryTag that = (HistoryTag) o;
567 if (uid != that.uid) return false;
568 if (!string.equals(that.string)) return false;
574 public int hashCode() {
575 int result = string.hashCode();
576 result = 31 * result + uid;
581 public final static class HistoryItem implements Parcelable {
582 public HistoryItem next;
584 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
587 public static final byte CMD_UPDATE = 0; // These can be written as deltas
588 public static final byte CMD_NULL = -1;
589 public static final byte CMD_START = 4;
590 public static final byte CMD_CURRENT_TIME = 5;
591 public static final byte CMD_OVERFLOW = 6;
592 public static final byte CMD_RESET = 7;
594 public byte cmd = CMD_NULL;
597 * Return whether the command code is a delta data update.
599 public boolean isDeltaData() {
600 return cmd == CMD_UPDATE;
603 public byte batteryLevel;
604 public byte batteryStatus;
605 public byte batteryHealth;
606 public byte batteryPlugType;
608 public short batteryTemperature;
609 public char batteryVoltage;
611 // Constants from SCREEN_BRIGHTNESS_*
612 public static final int STATE_BRIGHTNESS_SHIFT = 0;
613 public static final int STATE_BRIGHTNESS_MASK = 0x7;
614 // Constants from SIGNAL_STRENGTH_*
615 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
616 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
617 // Constants from ServiceState.STATE_*
618 public static final int STATE_PHONE_STATE_SHIFT = 6;
619 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
620 // Constants from DATA_CONNECTION_*
621 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
622 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
624 // These states always appear directly in the first int token
625 // of a delta change; they should be ones that change relatively
627 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
628 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
629 public static final int STATE_GPS_ON_FLAG = 1<<29;
630 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
631 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
632 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
633 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
634 // These are on the lower bits used for the command; if they change
635 // we need to write another int of data.
636 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
637 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
638 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
639 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
640 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
641 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
642 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
644 public static final int MOST_INTERESTING_STATES =
645 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
646 | STATE_PHONE_IN_CALL_FLAG | STATE_BLUETOOTH_ON_FLAG;
650 // Constants from WIFI_SUPPL_STATE_*
651 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
652 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
653 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
654 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
655 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
656 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
658 public static final int STATE2_LOW_POWER_FLAG = 1<<31;
659 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
660 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
661 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
662 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
664 public static final int MOST_INTERESTING_STATES2 =
665 STATE2_LOW_POWER_FLAG | STATE2_WIFI_ON_FLAG;
669 // The wake lock that was acquired at this point.
670 public HistoryTag wakelockTag;
672 // Kernel wakeup reason at this point.
673 public HistoryTag wakeReasonTag;
675 public static final int EVENT_FLAG_START = 0x8000;
676 public static final int EVENT_FLAG_FINISH = 0x4000;
678 // No event in this item.
679 public static final int EVENT_NONE = 0x0000;
680 // Event is about a process that is running.
681 public static final int EVENT_PROC = 0x0001;
682 // Event is about an application package that is in the foreground.
683 public static final int EVENT_FOREGROUND = 0x0002;
684 // Event is about an application package that is at the top of the screen.
685 public static final int EVENT_TOP = 0x0003;
686 // Event is about active sync operations.
687 public static final int EVENT_SYNC = 0x0004;
688 // Events for all additional wake locks aquired/release within a wake block.
689 // These are not generated by default.
690 public static final int EVENT_WAKE_LOCK = 0x0005;
691 // Event is about an application executing a scheduled job.
692 public static final int EVENT_JOB = 0x0006;
693 // Events for users running.
694 public static final int EVENT_USER_RUNNING = 0x0007;
695 // Events for foreground user.
696 public static final int EVENT_USER_FOREGROUND = 0x0008;
697 // Number of event types.
698 public static final int EVENT_COUNT = 0x0009;
699 // Mask to extract out only the type part of the event.
700 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
702 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
703 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
704 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
705 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
706 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
707 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
708 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
709 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
710 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
711 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
712 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
713 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
714 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
715 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
716 public static final int EVENT_USER_FOREGROUND_START =
717 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
718 public static final int EVENT_USER_FOREGROUND_FINISH =
719 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
722 public int eventCode;
723 public HistoryTag eventTag;
725 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
726 public long currentTime;
728 // Meta-data when reading.
729 public int numReadInts;
731 // Pre-allocated objects.
732 public final HistoryTag localWakelockTag = new HistoryTag();
733 public final HistoryTag localWakeReasonTag = new HistoryTag();
734 public final HistoryTag localEventTag = new HistoryTag();
736 public HistoryItem() {
739 public HistoryItem(long time, Parcel src) {
745 public int describeContents() {
749 public void writeToParcel(Parcel dest, int flags) {
750 dest.writeLong(time);
751 int bat = (((int)cmd)&0xff)
752 | ((((int)batteryLevel)<<8)&0xff00)
753 | ((((int)batteryStatus)<<16)&0xf0000)
754 | ((((int)batteryHealth)<<20)&0xf00000)
755 | ((((int)batteryPlugType)<<24)&0xf000000)
756 | (wakelockTag != null ? 0x10000000 : 0)
757 | (wakeReasonTag != null ? 0x20000000 : 0)
758 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
760 bat = (((int)batteryTemperature)&0xffff)
761 | ((((int)batteryVoltage)<<16)&0xffff0000);
763 dest.writeInt(states);
764 dest.writeInt(states2);
765 if (wakelockTag != null) {
766 wakelockTag.writeToParcel(dest, flags);
768 if (wakeReasonTag != null) {
769 wakeReasonTag.writeToParcel(dest, flags);
771 if (eventCode != EVENT_NONE) {
772 dest.writeInt(eventCode);
773 eventTag.writeToParcel(dest, flags);
775 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
776 dest.writeLong(currentTime);
780 public void readFromParcel(Parcel src) {
781 int start = src.dataPosition();
782 int bat = src.readInt();
783 cmd = (byte)(bat&0xff);
784 batteryLevel = (byte)((bat>>8)&0xff);
785 batteryStatus = (byte)((bat>>16)&0xf);
786 batteryHealth = (byte)((bat>>20)&0xf);
787 batteryPlugType = (byte)((bat>>24)&0xf);
788 int bat2 = src.readInt();
789 batteryTemperature = (short)(bat2&0xffff);
790 batteryVoltage = (char)((bat2>>16)&0xffff);
791 states = src.readInt();
792 states2 = src.readInt();
793 if ((bat&0x10000000) != 0) {
794 wakelockTag = localWakelockTag;
795 wakelockTag.readFromParcel(src);
799 if ((bat&0x20000000) != 0) {
800 wakeReasonTag = localWakeReasonTag;
801 wakeReasonTag.readFromParcel(src);
803 wakeReasonTag = null;
805 if ((bat&0x40000000) != 0) {
806 eventCode = src.readInt();
807 eventTag = localEventTag;
808 eventTag.readFromParcel(src);
810 eventCode = EVENT_NONE;
813 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
814 currentTime = src.readLong();
818 numReadInts += (src.dataPosition()-start)/4;
821 public void clear() {
828 batteryTemperature = 0;
833 wakeReasonTag = null;
834 eventCode = EVENT_NONE;
838 public void setTo(HistoryItem o) {
844 public void setTo(long time, byte cmd, HistoryItem o) {
850 private void setToCommon(HistoryItem o) {
851 batteryLevel = o.batteryLevel;
852 batteryStatus = o.batteryStatus;
853 batteryHealth = o.batteryHealth;
854 batteryPlugType = o.batteryPlugType;
855 batteryTemperature = o.batteryTemperature;
856 batteryVoltage = o.batteryVoltage;
859 if (o.wakelockTag != null) {
860 wakelockTag = localWakelockTag;
861 wakelockTag.setTo(o.wakelockTag);
865 if (o.wakeReasonTag != null) {
866 wakeReasonTag = localWakeReasonTag;
867 wakeReasonTag.setTo(o.wakeReasonTag);
869 wakeReasonTag = null;
871 eventCode = o.eventCode;
872 if (o.eventTag != null) {
873 eventTag = localEventTag;
874 eventTag.setTo(o.eventTag);
878 currentTime = o.currentTime;
881 public boolean sameNonEvent(HistoryItem o) {
882 return batteryLevel == o.batteryLevel
883 && batteryStatus == o.batteryStatus
884 && batteryHealth == o.batteryHealth
885 && batteryPlugType == o.batteryPlugType
886 && batteryTemperature == o.batteryTemperature
887 && batteryVoltage == o.batteryVoltage
888 && states == o.states
889 && states2 == o.states2
890 && currentTime == o.currentTime;
893 public boolean same(HistoryItem o) {
894 if (!sameNonEvent(o) || eventCode != o.eventCode) {
897 if (wakelockTag != o.wakelockTag) {
898 if (wakelockTag == null || o.wakelockTag == null) {
901 if (!wakelockTag.equals(o.wakelockTag)) {
905 if (wakeReasonTag != o.wakeReasonTag) {
906 if (wakeReasonTag == null || o.wakeReasonTag == null) {
909 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
913 if (eventTag != o.eventTag) {
914 if (eventTag == null || o.eventTag == null) {
917 if (!eventTag.equals(o.eventTag)) {
925 public final static class HistoryEventTracker {
926 private final HashMap<String, SparseIntArray>[] mActiveEvents
927 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
929 public boolean updateState(int code, String name, int uid, int poolIdx) {
930 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
931 int idx = code&HistoryItem.EVENT_TYPE_MASK;
932 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
933 if (active == null) {
934 active = new HashMap<String, SparseIntArray>();
935 mActiveEvents[idx] = active;
937 SparseIntArray uids = active.get(name);
939 uids = new SparseIntArray();
940 active.put(name, uids);
942 if (uids.indexOfKey(uid) >= 0) {
943 // Already set, nothing to do!
946 uids.put(uid, poolIdx);
947 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
948 int idx = code&HistoryItem.EVENT_TYPE_MASK;
949 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
950 if (active == null) {
951 // not currently active, nothing to do.
954 SparseIntArray uids = active.get(name);
956 // not currently active, nothing to do.
959 idx = uids.indexOfKey(uid);
961 // not currently active, nothing to do.
965 if (uids.size() <= 0) {
972 public void removeEvents(int code) {
973 int idx = code&HistoryItem.EVENT_TYPE_MASK;
974 mActiveEvents[idx] = null;
977 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
978 return mActiveEvents[code];
982 public static final class BitDescription {
983 public final int mask;
984 public final int shift;
985 public final String name;
986 public final String shortName;
987 public final String[] values;
988 public final String[] shortValues;
990 public BitDescription(int mask, String name, String shortName) {
994 this.shortName = shortName;
996 this.shortValues = null;
999 public BitDescription(int mask, int shift, String name, String shortName,
1000 String[] values, String[] shortValues) {
1004 this.shortName = shortName;
1005 this.values = values;
1006 this.shortValues = shortValues;
1011 * Don't allow any more batching in to the current history event. This
1012 * is called when printing partial histories, so to ensure that the next
1013 * history event will go in to a new batch after what was printed in the
1014 * last partial history.
1016 public abstract void commitCurrentHistoryBatchLocked();
1018 public abstract int getHistoryTotalSize();
1020 public abstract int getHistoryUsedSize();
1022 public abstract boolean startIteratingHistoryLocked();
1024 public abstract int getHistoryStringPoolSize();
1026 public abstract int getHistoryStringPoolBytes();
1028 public abstract String getHistoryTagPoolString(int index);
1030 public abstract int getHistoryTagPoolUid(int index);
1032 public abstract boolean getNextHistoryLocked(HistoryItem out);
1034 public abstract void finishIteratingHistoryLocked();
1036 public abstract boolean startIteratingOldHistoryLocked();
1038 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1040 public abstract void finishIteratingOldHistoryLocked();
1043 * Return the base time offset for the battery history.
1045 public abstract long getHistoryBaseTime();
1048 * Returns the number of times the device has been started.
1050 public abstract int getStartCount();
1053 * Returns the time in microseconds that the screen has been on while the device was
1054 * running on battery.
1058 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
1061 * Returns the number of times the screen was turned on.
1065 public abstract int getScreenOnCount(int which);
1067 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1069 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1070 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1071 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1072 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1073 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1075 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1076 "dark", "dim", "medium", "light", "bright"
1079 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1080 "0", "1", "2", "3", "4"
1083 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
1086 * Returns the time in microseconds that the screen has been on with
1087 * the given brightness
1091 public abstract long getScreenBrightnessTime(int brightnessBin,
1092 long elapsedRealtimeUs, int which);
1095 * Returns the time in microseconds that low power mode has been enabled while the device was
1096 * running on battery.
1100 public abstract long getLowPowerModeEnabledTime(long elapsedRealtimeUs, int which);
1103 * Returns the number of times that low power mode was enabled.
1107 public abstract int getLowPowerModeEnabledCount(int which);
1110 * Returns the time in microseconds that the phone has been on while the device was
1111 * running on battery.
1115 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
1118 * Returns the number of times a phone call was activated.
1122 public abstract int getPhoneOnCount(int which);
1125 * Returns the time in microseconds that the phone has been running with
1126 * the given signal strength.
1130 public abstract long getPhoneSignalStrengthTime(int strengthBin,
1131 long elapsedRealtimeUs, int which);
1134 * Returns the time in microseconds that the phone has been trying to
1139 public abstract long getPhoneSignalScanningTime(
1140 long elapsedRealtimeUs, int which);
1143 * Returns the number of times the phone has entered the given signal strength.
1147 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1150 * Returns the time in microseconds that the mobile network has been active
1151 * (in a high power state).
1155 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
1158 * Returns the number of times that the mobile network has transitioned to the
1163 public abstract int getMobileRadioActiveCount(int which);
1166 * Returns the time in microseconds that is the difference between the mobile radio
1167 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1172 public abstract long getMobileRadioActiveAdjustedTime(int which);
1175 * Returns the time in microseconds that the mobile network has been active
1176 * (in a high power state) but not being able to blame on an app.
1180 public abstract long getMobileRadioActiveUnknownTime(int which);
1183 * Return count of number of times radio was up that could not be blamed on apps.
1187 public abstract int getMobileRadioActiveUnknownCount(int which);
1189 public static final int DATA_CONNECTION_NONE = 0;
1190 public static final int DATA_CONNECTION_GPRS = 1;
1191 public static final int DATA_CONNECTION_EDGE = 2;
1192 public static final int DATA_CONNECTION_UMTS = 3;
1193 public static final int DATA_CONNECTION_CDMA = 4;
1194 public static final int DATA_CONNECTION_EVDO_0 = 5;
1195 public static final int DATA_CONNECTION_EVDO_A = 6;
1196 public static final int DATA_CONNECTION_1xRTT = 7;
1197 public static final int DATA_CONNECTION_HSDPA = 8;
1198 public static final int DATA_CONNECTION_HSUPA = 9;
1199 public static final int DATA_CONNECTION_HSPA = 10;
1200 public static final int DATA_CONNECTION_IDEN = 11;
1201 public static final int DATA_CONNECTION_EVDO_B = 12;
1202 public static final int DATA_CONNECTION_LTE = 13;
1203 public static final int DATA_CONNECTION_EHRPD = 14;
1204 public static final int DATA_CONNECTION_HSPAP = 15;
1205 public static final int DATA_CONNECTION_OTHER = 16;
1207 static final String[] DATA_CONNECTION_NAMES = {
1208 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
1209 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
1210 "ehrpd", "hspap", "other"
1213 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
1216 * Returns the time in microseconds that the phone has been running with
1217 * the given data connection.
1221 public abstract long getPhoneDataConnectionTime(int dataType,
1222 long elapsedRealtimeUs, int which);
1225 * Returns the number of times the phone has entered the given data
1230 public abstract int getPhoneDataConnectionCount(int dataType, int which);
1232 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1233 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1234 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1235 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1236 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1237 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1238 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1239 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1240 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1241 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1242 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1243 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1244 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1246 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1248 static final String[] WIFI_SUPPL_STATE_NAMES = {
1249 "invalid", "disconn", "disabled", "inactive", "scanning",
1250 "authenticating", "associating", "associated", "4-way-handshake",
1251 "group-handshake", "completed", "dormant", "uninit"
1254 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1255 "inv", "dsc", "dis", "inact", "scan",
1256 "auth", "ascing", "asced", "4-way",
1257 "group", "compl", "dorm", "uninit"
1260 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1261 = new BitDescription[] {
1262 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
1263 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1264 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
1265 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
1266 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1267 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1268 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
1269 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
1270 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
1271 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
1272 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1273 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
1274 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1275 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
1276 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1277 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1278 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1279 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1280 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1281 new String[] {"in", "out", "emergency", "off"},
1282 new String[] {"in", "out", "em", "off"}),
1283 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
1284 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
1285 SignalStrength.SIGNAL_STRENGTH_NAMES,
1286 new String[] { "0", "1", "2", "3", "4" }),
1287 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1288 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1289 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
1292 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1293 = new BitDescription[] {
1294 new BitDescription(HistoryItem.STATE2_LOW_POWER_FLAG, "low_power", "lp"),
1295 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
1296 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
1297 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
1298 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
1299 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
1300 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
1301 new String[] { "0", "1", "2", "3", "4" },
1302 new String[] { "0", "1", "2", "3", "4" }),
1303 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
1304 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
1305 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
1308 public static final String[] HISTORY_EVENT_NAMES = new String[] {
1309 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg"
1312 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
1313 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf"
1317 * Returns the time in microseconds that wifi has been on while the device was
1318 * running on battery.
1322 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
1325 * Returns the time in microseconds that wifi has been on and the driver has
1326 * been in the running state while the device was running on battery.
1330 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
1332 public static final int WIFI_STATE_OFF = 0;
1333 public static final int WIFI_STATE_OFF_SCANNING = 1;
1334 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1335 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1336 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1337 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1338 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1339 public static final int WIFI_STATE_SOFT_AP = 7;
1341 static final String[] WIFI_STATE_NAMES = {
1342 "off", "scanning", "no_net", "disconn",
1343 "sta", "p2p", "sta_p2p", "soft_ap"
1346 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1349 * Returns the time in microseconds that WiFi has been running in the given state.
1353 public abstract long getWifiStateTime(int wifiState,
1354 long elapsedRealtimeUs, int which);
1357 * Returns the number of times that WiFi has entered the given state.
1361 public abstract int getWifiStateCount(int wifiState, int which);
1364 * Returns the time in microseconds that the wifi supplicant has been
1369 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
1372 * Returns the number of times that the wifi supplicant has transitioned
1377 public abstract int getWifiSupplStateCount(int state, int which);
1379 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
1382 * Returns the time in microseconds that WIFI has been running with
1383 * the given signal strength.
1387 public abstract long getWifiSignalStrengthTime(int strengthBin,
1388 long elapsedRealtimeUs, int which);
1391 * Returns the number of times WIFI has entered the given signal strength.
1395 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
1398 * Returns the time in microseconds that bluetooth has been on while the device was
1399 * running on battery.
1403 public abstract long getBluetoothOnTime(long elapsedRealtimeUs, int which);
1405 public abstract int getBluetoothPingCount();
1407 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1408 public static final int BLUETOOTH_STATE_LOW = 1;
1409 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1410 public static final int BLUETOOTH_STATE_HIGH = 3;
1412 static final String[] BLUETOOTH_STATE_NAMES = {
1413 "inactive", "low", "med", "high"
1416 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
1419 * Returns the time in microseconds that Bluetooth has been running in the
1420 * given active state.
1424 public abstract long getBluetoothStateTime(int bluetoothState,
1425 long elapsedRealtimeUs, int which);
1428 * Returns the number of times that Bluetooth has entered the given active state.
1432 public abstract int getBluetoothStateCount(int bluetoothState, int which);
1435 * Returns the time in microseconds that the flashlight has been on while the device was
1436 * running on battery.
1440 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
1443 * Returns the number of times that the flashlight has been turned on while the device was
1444 * running on battery.
1448 public abstract long getFlashlightOnCount(int which);
1450 public static final int NETWORK_MOBILE_RX_DATA = 0;
1451 public static final int NETWORK_MOBILE_TX_DATA = 1;
1452 public static final int NETWORK_WIFI_RX_DATA = 2;
1453 public static final int NETWORK_WIFI_TX_DATA = 3;
1455 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
1457 public abstract long getNetworkActivityBytes(int type, int which);
1458 public abstract long getNetworkActivityPackets(int type, int which);
1461 * Return the wall clock time when battery stats data collection started.
1463 public abstract long getStartClockTime();
1466 * Return platform version tag that we were running in when the battery stats started.
1468 public abstract String getStartPlatformVersion();
1471 * Return platform version tag that we were running in when the battery stats ended.
1473 public abstract String getEndPlatformVersion();
1476 * Return the internal version code of the parcelled format.
1478 public abstract int getParcelVersion();
1481 * Return whether we are currently running on battery.
1483 public abstract boolean getIsOnBattery();
1486 * Returns a SparseArray containing the statistics for each uid.
1488 public abstract SparseArray<? extends Uid> getUidStats();
1491 * Returns the current battery uptime in microseconds.
1493 * @param curTime the amount of elapsed realtime in microseconds.
1495 public abstract long getBatteryUptime(long curTime);
1498 * Returns the current battery realtime in microseconds.
1500 * @param curTime the amount of elapsed realtime in microseconds.
1502 public abstract long getBatteryRealtime(long curTime);
1505 * Returns the battery percentage level at the last time the device was unplugged from power, or
1506 * the last time it booted on battery power.
1508 public abstract int getDischargeStartLevel();
1511 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1512 * returns the level at the last plug event.
1514 public abstract int getDischargeCurrentLevel();
1517 * Get the amount the battery has discharged since the stats were
1518 * last reset after charging, as a lower-end approximation.
1520 public abstract int getLowDischargeAmountSinceCharge();
1523 * Get the amount the battery has discharged since the stats were
1524 * last reset after charging, as an upper-end approximation.
1526 public abstract int getHighDischargeAmountSinceCharge();
1529 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
1531 public abstract int getDischargeAmount(int which);
1534 * Get the amount the battery has discharged while the screen was on,
1535 * since the last time power was unplugged.
1537 public abstract int getDischargeAmountScreenOn();
1540 * Get the amount the battery has discharged while the screen was on,
1541 * since the last time the device was charged.
1543 public abstract int getDischargeAmountScreenOnSinceCharge();
1546 * Get the amount the battery has discharged while the screen was off,
1547 * since the last time power was unplugged.
1549 public abstract int getDischargeAmountScreenOff();
1552 * Get the amount the battery has discharged while the screen was off,
1553 * since the last time the device was charged.
1555 public abstract int getDischargeAmountScreenOffSinceCharge();
1558 * Returns the total, last, or current battery uptime in microseconds.
1560 * @param curTime the elapsed realtime in microseconds.
1561 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1563 public abstract long computeBatteryUptime(long curTime, int which);
1566 * Returns the total, last, or current battery realtime in microseconds.
1568 * @param curTime the current elapsed realtime in microseconds.
1569 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1571 public abstract long computeBatteryRealtime(long curTime, int which);
1574 * Returns the total, last, or current battery screen off uptime in microseconds.
1576 * @param curTime the elapsed realtime in microseconds.
1577 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1579 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
1582 * Returns the total, last, or current battery screen off realtime in microseconds.
1584 * @param curTime the current elapsed realtime in microseconds.
1585 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1587 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
1590 * Returns the total, last, or current uptime in microseconds.
1592 * @param curTime the current elapsed realtime in microseconds.
1593 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1595 public abstract long computeUptime(long curTime, int which);
1598 * Returns the total, last, or current realtime in microseconds.
1600 * @param curTime the current elapsed realtime in microseconds.
1601 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1603 public abstract long computeRealtime(long curTime, int which);
1606 * Compute an approximation for how much run time (in microseconds) is remaining on
1607 * the battery. Returns -1 if no time can be computed: either there is not
1608 * enough current data to make a decision, or the battery is currently
1611 * @param curTime The current elepsed realtime in microseconds.
1613 public abstract long computeBatteryTimeRemaining(long curTime);
1615 // The part of a step duration that is the actual time.
1616 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
1618 // Bits in a step duration that are the new battery level we are at.
1619 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
1620 public static final long STEP_LEVEL_LEVEL_SHIFT = 40;
1622 // Bits in a step duration that are the initial mode we were in at that step.
1623 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
1624 public static final long STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
1626 // Bits in a step duration that indicate which modes changed during that step.
1627 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
1628 public static final long STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
1630 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
1631 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
1633 // Step duration mode: power save is on.
1634 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
1637 * Return the historical number of discharge steps we currently have.
1639 public abstract int getNumDischargeStepDurations();
1642 * Return the array of discharge step durations; the number of valid
1643 * items in it is returned by {@link #getNumDischargeStepDurations()}.
1644 * These values are in milliseconds.
1646 public abstract long[] getDischargeStepDurationsArray();
1649 * Compute an approximation for how much time (in microseconds) remains until the battery
1650 * is fully charged. Returns -1 if no time can be computed: either there is not
1651 * enough current data to make a decision, or the battery is currently
1654 * @param curTime The current elepsed realtime in microseconds.
1656 public abstract long computeChargeTimeRemaining(long curTime);
1659 * Return the historical number of charge steps we currently have.
1661 public abstract int getNumChargeStepDurations();
1664 * Return the array of charge step durations; the number of valid
1665 * items in it is returned by {@link #getNumChargeStepDurations()}.
1666 * These values are in milliseconds.
1668 public abstract long[] getChargeStepDurationsArray();
1670 public abstract Map<String, ? extends LongCounter> getWakeupReasonStats();
1672 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
1674 /** Returns the number of different speeds that the CPU can run at */
1675 public abstract int getCpuSpeedSteps();
1677 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1679 private final static void formatTimeRaw(StringBuilder out, long seconds) {
1680 long days = seconds / (60 * 60 * 24);
1685 long used = days * 60 * 60 * 24;
1687 long hours = (seconds - used) / (60 * 60);
1688 if (hours != 0 || used != 0) {
1692 used += hours * 60 * 60;
1694 long mins = (seconds-used) / 60;
1695 if (mins != 0 || used != 0) {
1701 if (seconds != 0 || used != 0) {
1702 out.append(seconds-used);
1707 public final static void formatTime(StringBuilder sb, long time) {
1708 long sec = time / 100;
1709 formatTimeRaw(sb, sec);
1710 sb.append((time - (sec * 100)) * 10);
1714 public final static void formatTimeMs(StringBuilder sb, long time) {
1715 long sec = time / 1000;
1716 formatTimeRaw(sb, sec);
1717 sb.append(time - (sec * 1000));
1721 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
1722 long sec = time / 1000;
1723 formatTimeRaw(sb, sec);
1724 sb.append(time - (sec * 1000));
1728 public final String formatRatioLocked(long num, long den) {
1732 float perc = ((float)num) / ((float)den) * 100;
1733 mFormatBuilder.setLength(0);
1734 mFormatter.format("%.1f%%", perc);
1735 return mFormatBuilder.toString();
1738 final String formatBytesLocked(long bytes) {
1739 mFormatBuilder.setLength(0);
1741 if (bytes < BYTES_PER_KB) {
1743 } else if (bytes < BYTES_PER_MB) {
1744 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1745 return mFormatBuilder.toString();
1746 } else if (bytes < BYTES_PER_GB){
1747 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1748 return mFormatBuilder.toString();
1750 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1751 return mFormatBuilder.toString();
1755 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
1756 if (timer != null) {
1757 // Convert from microseconds to milliseconds with rounding
1758 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
1759 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1760 return totalTimeMillis;
1767 * @param sb a StringBuilder object.
1768 * @param timer a Timer object contining the wakelock times.
1769 * @param elapsedRealtimeUs the current on-battery time in microseconds.
1770 * @param name the name of the wakelock.
1771 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1772 * @param linePrefix a String to be prepended to each line of output.
1773 * @return the line prefix
1775 private static final String printWakeLock(StringBuilder sb, Timer timer,
1776 long elapsedRealtimeUs, String name, int which, String linePrefix) {
1778 if (timer != null) {
1779 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
1781 int count = timer.getCountLocked(which);
1782 if (totalTimeMillis != 0) {
1783 sb.append(linePrefix);
1784 formatTimeMs(sb, totalTimeMillis);
1791 sb.append(" times)");
1799 * Checkin version of wakelock printer. Prints simple comma-separated list.
1801 * @param sb a StringBuilder object.
1802 * @param timer a Timer object contining the wakelock times.
1803 * @param elapsedRealtimeUs the current time in microseconds.
1804 * @param name the name of the wakelock.
1805 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
1806 * @param linePrefix a String to be prepended to each line of output.
1807 * @return the line prefix
1809 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
1810 long elapsedRealtimeUs, String name, int which, String linePrefix) {
1811 long totalTimeMicros = 0;
1813 if (timer != null) {
1814 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
1815 count = timer.getCountLocked(which);
1817 sb.append(linePrefix);
1818 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1820 sb.append(name != null ? name + "," : "");
1826 * Dump a comma-separated line of values for terse checkin mode.
1828 * @param pw the PageWriter to dump log to
1829 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1830 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1831 * @param args type-dependent data arguments
1833 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1835 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1836 pw.print(uid); pw.print(',');
1837 pw.print(category); pw.print(',');
1840 for (Object arg : args) {
1848 * Temporary for settings.
1850 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
1851 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
1855 * Checkin server version of dump to produce more compact, computer-readable log.
1857 * NOTE: all times are expressed in 'ms'.
1859 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
1861 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1862 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1863 final long batteryUptime = getBatteryUptime(rawUptime);
1864 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1865 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1866 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1867 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1869 final long totalRealtime = computeRealtime(rawRealtime, which);
1870 final long totalUptime = computeUptime(rawUptime, which);
1871 final long screenOnTime = getScreenOnTime(rawRealtime, which);
1872 final long interactiveTime = getInteractiveTime(rawRealtime, which);
1873 final long lowPowerModeEnabledTime = getLowPowerModeEnabledTime(rawRealtime, which);
1874 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1875 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1876 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1877 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
1879 StringBuilder sb = new StringBuilder(128);
1881 SparseArray<? extends Uid> uidStats = getUidStats();
1882 final int NU = uidStats.size();
1884 String category = STAT_NAMES[which];
1886 // Dump "battery" stat
1887 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
1888 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
1889 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
1890 totalRealtime / 1000, totalUptime / 1000,
1891 getStartClockTime(),
1892 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
1894 // Calculate wakelock times across all uids.
1895 long fullWakeLockTimeTotal = 0;
1896 long partialWakeLockTimeTotal = 0;
1898 for (int iu = 0; iu < NU; iu++) {
1899 Uid u = uidStats.valueAt(iu);
1901 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1902 if (wakelocks.size() > 0) {
1903 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1904 : wakelocks.entrySet()) {
1905 Uid.Wakelock wl = ent.getValue();
1907 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1908 if (fullWakeTimer != null) {
1909 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
1913 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1914 if (partialWakeTimer != null) {
1915 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
1916 rawRealtime, which);
1922 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1923 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1924 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1925 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1926 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1927 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1928 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1929 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1931 // Dump network stats
1932 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1933 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1934 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1937 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
1938 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
1939 wifiRunningTime / 1000, bluetoothOnTime / 1000,
1940 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1941 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
1942 0 /*legacy input event count*/, getMobileRadioActiveTime(rawRealtime, which) / 1000,
1943 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
1944 lowPowerModeEnabledTime / 1000);
1946 // Dump screen brightness stats
1947 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1948 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1949 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
1951 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
1953 // Dump signal strength stats
1954 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1955 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
1956 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
1958 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
1959 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
1960 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
1961 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
1962 args[i] = getPhoneSignalStrengthCount(i, which);
1964 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
1966 // Dump network type stats
1967 args = new Object[NUM_DATA_CONNECTION_TYPES];
1968 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1969 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
1971 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1972 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1973 args[i] = getPhoneDataConnectionCount(i, which);
1975 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
1977 // Dump wifi state stats
1978 args = new Object[NUM_WIFI_STATES];
1979 for (int i=0; i<NUM_WIFI_STATES; i++) {
1980 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
1982 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
1983 for (int i=0; i<NUM_WIFI_STATES; i++) {
1984 args[i] = getWifiStateCount(i, which);
1986 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
1988 // Dump wifi suppl state stats
1989 args = new Object[NUM_WIFI_SUPPL_STATES];
1990 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
1991 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
1993 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
1994 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
1995 args[i] = getWifiSupplStateCount(i, which);
1997 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
1999 // Dump wifi signal strength stats
2000 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
2001 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2002 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
2004 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
2005 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2006 args[i] = getWifiSignalStrengthCount(i, which);
2008 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
2010 // Dump bluetooth state stats
2011 args = new Object[NUM_BLUETOOTH_STATES];
2012 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
2013 args[i] = getBluetoothStateTime(i, rawRealtime, which) / 1000;
2015 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
2016 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
2017 args[i] = getBluetoothStateCount(i, which);
2019 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
2021 if (which == STATS_SINCE_UNPLUGGED) {
2022 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
2023 getDischargeCurrentLevel());
2026 if (which == STATS_SINCE_UNPLUGGED) {
2027 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2028 getDischargeStartLevel()-getDischargeCurrentLevel(),
2029 getDischargeStartLevel()-getDischargeCurrentLevel(),
2030 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
2032 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
2033 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
2034 getDischargeAmountScreenOnSinceCharge(),
2035 getDischargeAmountScreenOffSinceCharge());
2039 Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
2040 if (kernelWakelocks.size() > 0) {
2041 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
2043 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
2044 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
2048 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
2049 if (wakeupReasons.size() > 0) {
2050 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
2051 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
2052 "\"" + ent.getKey() + "\"", ent.getValue().getCountLocked(which));
2057 BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
2058 helper.create(this);
2059 helper.refreshStats(which, UserHandle.USER_ALL);
2060 List<BatterySipper> sippers = helper.getUsageList();
2061 if (sippers != null && sippers.size() > 0) {
2062 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
2063 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
2064 BatteryStatsHelper.makemAh(helper.getComputedPower()),
2065 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
2066 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
2067 for (int i=0; i<sippers.size(); i++) {
2068 BatterySipper bs = sippers.get(i);
2071 switch (bs.drainType) {
2094 uid = bs.uidObj.getUid();
2098 uid = UserHandle.getUid(bs.userId, 0);
2110 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
2111 BatteryStatsHelper.makemAh(bs.value));
2115 for (int iu = 0; iu < NU; iu++) {
2116 final int uid = uidStats.keyAt(iu);
2117 if (reqUid >= 0 && uid != reqUid) {
2120 Uid u = uidStats.valueAt(iu);
2121 // Dump Network stats per uid, if any
2122 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2123 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2124 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2125 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2126 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2127 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2128 long mobileActiveTime = u.getMobileRadioActiveTime(which);
2129 int mobileActiveCount = u.getMobileRadioActiveCount(which);
2130 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2131 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
2132 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2133 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2134 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
2136 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
2137 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
2138 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
2139 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
2140 wifiBytesRx, wifiBytesTx,
2141 mobilePacketsRx, mobilePacketsTx,
2142 wifiPacketsRx, wifiPacketsTx,
2143 mobileActiveTime, mobileActiveCount);
2146 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
2147 || uidWifiRunningTime != 0) {
2148 dumpLine(pw, uid, category, WIFI_DATA,
2149 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
2152 if (u.hasUserActivity()) {
2153 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
2154 boolean hasData = false;
2155 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
2156 int val = u.getUserActivityCount(i, which);
2158 if (val != 0) hasData = true;
2161 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
2165 Map<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
2166 if (wakelocks.size() > 0) {
2167 for (Map.Entry<String, ? extends Uid.Wakelock> ent : wakelocks.entrySet()) {
2168 Uid.Wakelock wl = ent.getValue();
2169 String linePrefix = "";
2171 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
2172 rawRealtime, "f", which, linePrefix);
2173 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
2174 rawRealtime, "p", which, linePrefix);
2175 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
2176 rawRealtime, "w", which, linePrefix);
2178 // Only log if we had at lease one wakelock...
2179 if (sb.length() > 0) {
2180 String name = ent.getKey();
2181 if (name.indexOf(',') >= 0) {
2182 name = name.replace(',', '_');
2184 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
2189 Map<String, ? extends Timer> syncs = u.getSyncStats();
2190 if (syncs.size() > 0) {
2191 for (Map.Entry<String, ? extends Timer> ent : syncs.entrySet()) {
2192 Timer timer = ent.getValue();
2193 // Convert from microseconds to milliseconds with rounding
2194 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2195 int count = timer.getCountLocked(which);
2196 if (totalTime != 0) {
2197 dumpLine(pw, uid, category, SYNC_DATA, ent.getKey(), totalTime, count);
2202 Map<String, ? extends Timer> jobs = u.getJobStats();
2203 if (jobs.size() > 0) {
2204 for (Map.Entry<String, ? extends Timer> ent : jobs.entrySet()) {
2205 Timer timer = ent.getValue();
2206 // Convert from microseconds to milliseconds with rounding
2207 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2208 int count = timer.getCountLocked(which);
2209 if (totalTime != 0) {
2210 dumpLine(pw, uid, category, JOB_DATA, ent.getKey(), totalTime, count);
2215 SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2216 int NSE = sensors.size();
2217 for (int ise=0; ise<NSE; ise++) {
2218 Uid.Sensor se = sensors.valueAt(ise);
2219 int sensorNumber = sensors.keyAt(ise);
2220 Timer timer = se.getSensorTime();
2221 if (timer != null) {
2222 // Convert from microseconds to milliseconds with rounding
2223 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2224 int count = timer.getCountLocked(which);
2225 if (totalTime != 0) {
2226 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
2231 Timer vibTimer = u.getVibratorOnTimer();
2232 if (vibTimer != null) {
2233 // Convert from microseconds to milliseconds with rounding
2234 long totalTime = (vibTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2235 int count = vibTimer.getCountLocked(which);
2236 if (totalTime != 0) {
2237 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
2241 Timer fgTimer = u.getForegroundActivityTimer();
2242 if (fgTimer != null) {
2243 // Convert from microseconds to milliseconds with rounding
2244 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
2245 int count = fgTimer.getCountLocked(which);
2246 if (totalTime != 0) {
2247 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
2251 Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
2252 long totalStateTime = 0;
2253 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
2254 totalStateTime += u.getProcessStateTime(ips, rawRealtime, which);
2255 stateTimes[ips] = (totalStateTime + 500) / 1000;
2257 if (totalStateTime > 0) {
2258 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
2261 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2262 if (processStats.size() > 0) {
2263 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2264 : processStats.entrySet()) {
2265 Uid.Proc ps = ent.getValue();
2267 final long userMillis = ps.getUserTime(which) * 10;
2268 final long systemMillis = ps.getSystemTime(which) * 10;
2269 final long foregroundMillis = ps.getForegroundTime(which) * 10;
2270 final long starts = ps.getStarts(which);
2272 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
2274 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
2275 systemMillis, foregroundMillis, starts);
2280 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2281 if (packageStats.size() > 0) {
2282 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2283 : packageStats.entrySet()) {
2285 Uid.Pkg ps = ent.getValue();
2286 int wakeups = ps.getWakeups(which);
2287 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2288 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2289 : serviceStats.entrySet()) {
2290 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2291 long startTime = ss.getStartTime(batteryUptime, which);
2292 int starts = ss.getStarts(which);
2293 int launches = ss.getLaunches(which);
2294 if (startTime != 0 || starts != 0 || launches != 0) {
2295 dumpLine(pw, uid, category, APK_DATA,
2296 wakeups, // wakeup alarms
2297 ent.getKey(), // Apk
2298 sent.getKey(), // service
2299 startTime / 1000, // time spent started, in ms
2309 static final class TimerEntry {
2312 final BatteryStats.Timer mTimer;
2314 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
2322 private void printmAh(PrintWriter printer, double power) {
2323 printer.print(BatteryStatsHelper.makemAh(power));
2327 * Temporary for settings.
2329 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
2331 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2334 @SuppressWarnings("unused")
2335 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
2336 int reqUid, boolean wifiOnly) {
2337 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2338 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2339 final long batteryUptime = getBatteryUptime(rawUptime);
2341 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2342 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
2343 final long totalRealtime = computeRealtime(rawRealtime, which);
2344 final long totalUptime = computeUptime(rawUptime, which);
2345 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2346 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2348 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
2349 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
2351 StringBuilder sb = new StringBuilder(128);
2353 SparseArray<? extends Uid> uidStats = getUidStats();
2354 final int NU = uidStats.size();
2358 sb.append(" Time on battery: ");
2359 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
2360 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
2361 sb.append(") realtime, ");
2362 formatTimeMs(sb, whichBatteryUptime / 1000);
2363 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
2364 sb.append(") uptime");
2365 pw.println(sb.toString());
2368 sb.append(" Time on battery screen off: ");
2369 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
2370 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
2371 sb.append(") realtime, ");
2372 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
2374 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
2375 sb.append(") uptime");
2376 pw.println(sb.toString());
2379 sb.append(" Total run time: ");
2380 formatTimeMs(sb, totalRealtime / 1000);
2381 sb.append("realtime, ");
2382 formatTimeMs(sb, totalUptime / 1000);
2383 sb.append("uptime");
2384 pw.println(sb.toString());
2385 if (batteryTimeRemaining >= 0) {
2388 sb.append(" Battery time remaining: ");
2389 formatTimeMs(sb, batteryTimeRemaining / 1000);
2390 pw.println(sb.toString());
2392 if (chargeTimeRemaining >= 0) {
2395 sb.append(" Charge time remaining: ");
2396 formatTimeMs(sb, chargeTimeRemaining / 1000);
2397 pw.println(sb.toString());
2399 pw.print(" Start clock time: ");
2400 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
2402 final long screenOnTime = getScreenOnTime(rawRealtime, which);
2403 final long interactiveTime = getInteractiveTime(rawRealtime, which);
2404 final long lowPowerModeEnabledTime = getLowPowerModeEnabledTime(rawRealtime, which);
2405 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
2406 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
2407 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
2408 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
2411 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
2412 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
2413 sb.append(") "); sb.append(getScreenOnCount(which));
2414 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
2415 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
2417 pw.println(sb.toString());
2420 sb.append(" Screen brightnesses:");
2421 boolean didOne = false;
2422 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
2423 final long time = getScreenBrightnessTime(i, rawRealtime, which);
2430 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
2432 formatTimeMs(sb, time/1000);
2434 sb.append(formatRatioLocked(time, screenOnTime));
2437 if (!didOne) sb.append(" (no activity)");
2438 pw.println(sb.toString());
2439 if (lowPowerModeEnabledTime != 0) {
2442 sb.append(" Low power mode enabled: ");
2443 formatTimeMs(sb, lowPowerModeEnabledTime / 1000);
2445 sb.append(formatRatioLocked(lowPowerModeEnabledTime, whichBatteryRealtime));
2447 pw.println(sb.toString());
2449 if (phoneOnTime != 0) {
2452 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
2453 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
2454 sb.append(") "); sb.append(getPhoneOnCount(which));
2457 // Calculate wakelock times across all uids.
2458 long fullWakeLockTimeTotalMicros = 0;
2459 long partialWakeLockTimeTotalMicros = 0;
2461 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
2463 for (int iu = 0; iu < NU; iu++) {
2464 Uid u = uidStats.valueAt(iu);
2466 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2467 if (wakelocks.size() > 0) {
2468 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2469 : wakelocks.entrySet()) {
2470 Uid.Wakelock wl = ent.getValue();
2472 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2473 if (fullWakeTimer != null) {
2474 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
2475 rawRealtime, which);
2478 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2479 if (partialWakeTimer != null) {
2480 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
2481 rawRealtime, which);
2482 if (totalTimeMicros > 0) {
2484 // Only show the ordered list of all wake
2485 // locks if the caller is not asking for data
2486 // about a specific uid.
2487 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
2488 partialWakeTimer, totalTimeMicros));
2490 partialWakeLockTimeTotalMicros += totalTimeMicros;
2497 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2498 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2499 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2500 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2501 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2502 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2503 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2504 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
2506 if (fullWakeLockTimeTotalMicros != 0) {
2509 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
2510 (fullWakeLockTimeTotalMicros + 500) / 1000);
2511 pw.println(sb.toString());
2514 if (partialWakeLockTimeTotalMicros != 0) {
2517 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
2518 (partialWakeLockTimeTotalMicros + 500) / 1000);
2519 pw.println(sb.toString());
2523 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
2524 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
2525 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
2526 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
2529 sb.append(" Phone signal levels:");
2531 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
2532 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
2539 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
2541 formatTimeMs(sb, time/1000);
2543 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2545 sb.append(getPhoneSignalStrengthCount(i, which));
2548 if (!didOne) sb.append(" (no activity)");
2549 pw.println(sb.toString());
2553 sb.append(" Signal scanning time: ");
2554 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
2555 pw.println(sb.toString());
2559 sb.append(" Radio types:");
2561 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
2562 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
2569 sb.append(DATA_CONNECTION_NAMES[i]);
2571 formatTimeMs(sb, time/1000);
2573 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2575 sb.append(getPhoneDataConnectionCount(i, which));
2578 if (!didOne) sb.append(" (no activity)");
2579 pw.println(sb.toString());
2583 sb.append(" Mobile radio active time: ");
2584 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
2585 formatTimeMs(sb, mobileActiveTime / 1000);
2586 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
2587 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
2589 pw.println(sb.toString());
2591 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
2592 if (mobileActiveUnknownTime != 0) {
2595 sb.append(" Mobile radio active unknown time: ");
2596 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
2598 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
2599 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
2601 pw.println(sb.toString());
2604 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
2605 if (mobileActiveAdjustedTime != 0) {
2608 sb.append(" Mobile radio active adjusted time: ");
2609 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
2611 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
2613 pw.println(sb.toString());
2617 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
2618 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
2619 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
2620 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
2623 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
2624 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
2625 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2626 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
2628 pw.println(sb.toString());
2632 sb.append(" Wifi states:");
2634 for (int i=0; i<NUM_WIFI_STATES; i++) {
2635 final long time = getWifiStateTime(i, rawRealtime, which);
2641 sb.append(WIFI_STATE_NAMES[i]);
2643 formatTimeMs(sb, time/1000);
2645 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2647 sb.append(getWifiStateCount(i, which));
2650 if (!didOne) sb.append(" (no activity)");
2651 pw.println(sb.toString());
2655 sb.append(" Wifi supplicant states:");
2657 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
2658 final long time = getWifiSupplStateTime(i, rawRealtime, which);
2664 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
2666 formatTimeMs(sb, time/1000);
2668 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2670 sb.append(getWifiSupplStateCount(i, which));
2673 if (!didOne) sb.append(" (no activity)");
2674 pw.println(sb.toString());
2678 sb.append(" Wifi signal levels:");
2680 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
2681 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
2688 sb.append("level(");
2691 formatTimeMs(sb, time/1000);
2693 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2695 sb.append(getWifiSignalStrengthCount(i, which));
2698 if (!didOne) sb.append(" (no activity)");
2699 pw.println(sb.toString());
2703 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
2704 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2706 pw.println(sb.toString());
2710 sb.append(" Bluetooth states:");
2712 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
2713 final long time = getBluetoothStateTime(i, rawRealtime, which);
2719 sb.append(BLUETOOTH_STATE_NAMES[i]);
2721 formatTimeMs(sb, time/1000);
2723 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2725 sb.append(getPhoneDataConnectionCount(i, which));
2728 if (!didOne) sb.append(" (no activity)");
2729 pw.println(sb.toString());
2733 if (which == STATS_SINCE_UNPLUGGED) {
2734 if (getIsOnBattery()) {
2735 pw.print(prefix); pw.println(" Device is currently unplugged");
2736 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2737 pw.println(getDischargeStartLevel());
2738 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2739 pw.println(getDischargeCurrentLevel());
2741 pw.print(prefix); pw.println(" Device is currently plugged into power");
2742 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2743 pw.println(getDischargeStartLevel());
2744 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2745 pw.println(getDischargeCurrentLevel());
2747 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2748 pw.println(getDischargeAmountScreenOn());
2749 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2750 pw.println(getDischargeAmountScreenOff());
2753 pw.print(prefix); pw.println(" Device battery use since last full charge");
2754 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2755 pw.println(getLowDischargeAmountSinceCharge());
2756 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2757 pw.println(getHighDischargeAmountSinceCharge());
2758 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2759 pw.println(getDischargeAmountScreenOnSinceCharge());
2760 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2761 pw.println(getDischargeAmountScreenOffSinceCharge());
2765 BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
2766 helper.create(this);
2767 helper.refreshStats(which, UserHandle.USER_ALL);
2768 List<BatterySipper> sippers = helper.getUsageList();
2769 if (sippers != null && sippers.size() > 0) {
2770 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2771 pw.print(prefix); pw.print(" Capacity: ");
2772 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
2773 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
2774 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
2775 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
2776 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
2779 for (int i=0; i<sippers.size(); i++) {
2780 BatterySipper bs = sippers.get(i);
2781 switch (bs.drainType) {
2783 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2787 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2791 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2795 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2799 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2803 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2807 pw.print(prefix); pw.print(" Flashlight: "); printmAh(pw, bs.value);
2811 pw.print(prefix); pw.print(" Uid ");
2812 UserHandle.formatUid(pw, bs.uidObj.getUid());
2813 pw.print(": "); printmAh(pw, bs.value); pw.println();
2816 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2817 pw.print(": "); printmAh(pw, bs.value); pw.println();
2820 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2824 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2832 sippers = helper.getMobilemsppList();
2833 if (sippers != null && sippers.size() > 0) {
2834 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
2836 for (int i=0; i<sippers.size(); i++) {
2837 BatterySipper bs = sippers.get(i);
2839 sb.append(prefix); sb.append(" Uid ");
2840 UserHandle.formatUid(sb, bs.uidObj.getUid());
2841 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
2842 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
2843 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
2844 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
2845 pw.println(sb.toString());
2846 totalTime += bs.mobileActive;
2850 sb.append(" TOTAL TIME: ");
2851 formatTimeMs(sb, totalTime);
2852 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
2854 pw.println(sb.toString());
2858 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
2860 public int compare(TimerEntry lhs, TimerEntry rhs) {
2861 long lhsTime = lhs.mTime;
2862 long rhsTime = rhs.mTime;
2863 if (lhsTime < rhsTime) {
2866 if (lhsTime > rhsTime) {
2874 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
2875 if (kernelWakelocks.size() > 0) {
2876 final ArrayList<TimerEntry> ktimers = new ArrayList<TimerEntry>();
2877 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
2878 BatteryStats.Timer timer = ent.getValue();
2879 long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
2880 if (totalTimeMillis > 0) {
2881 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
2884 if (ktimers.size() > 0) {
2885 Collections.sort(ktimers, timerComparator);
2886 pw.print(prefix); pw.println(" All kernel wake locks:");
2887 for (int i=0; i<ktimers.size(); i++) {
2888 TimerEntry timer = ktimers.get(i);
2889 String linePrefix = ": ";
2892 sb.append(" Kernel Wake lock ");
2893 sb.append(timer.mName);
2894 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
2896 if (!linePrefix.equals(": ")) {
2897 sb.append(" realtime");
2898 // Only print out wake locks that were held
2899 pw.println(sb.toString());
2906 if (timers.size() > 0) {
2907 Collections.sort(timers, timerComparator);
2908 pw.print(prefix); pw.println(" All partial wake locks:");
2909 for (int i=0; i<timers.size(); i++) {
2910 TimerEntry timer = timers.get(i);
2912 sb.append(" Wake lock ");
2913 UserHandle.formatUid(sb, timer.mId);
2915 sb.append(timer.mName);
2916 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
2917 sb.append(" realtime");
2918 pw.println(sb.toString());
2924 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
2925 if (wakeupReasons.size() > 0) {
2926 pw.print(prefix); pw.println(" All wakeup reasons:");
2927 final ArrayList<TimerEntry> reasons = new ArrayList<TimerEntry>();
2928 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
2929 BatteryStats.LongCounter counter = ent.getValue();
2930 reasons.add(new TimerEntry(ent.getKey(), 0, null,
2931 ent.getValue().getCountLocked(which)));
2933 Collections.sort(reasons, timerComparator);
2934 for (int i=0; i<reasons.size(); i++) {
2935 TimerEntry timer = reasons.get(i);
2936 String linePrefix = ": ";
2939 sb.append(" Wakeup reason ");
2940 sb.append(timer.mName);
2942 formatTimeMs(sb, timer.mTime);
2943 sb.append("realtime");
2944 pw.println(sb.toString());
2950 for (int iu=0; iu<NU; iu++) {
2951 final int uid = uidStats.keyAt(iu);
2952 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
2956 Uid u = uidStats.valueAt(iu);
2960 UserHandle.formatUid(pw, uid);
2962 boolean uidActivity = false;
2964 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2965 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2966 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2967 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2968 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2969 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2970 long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
2971 int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
2972 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2973 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
2974 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2975 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2976 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
2978 if (mobileRxBytes > 0 || mobileTxBytes > 0
2979 || mobileRxPackets > 0 || mobileTxPackets > 0) {
2980 pw.print(prefix); pw.print(" Mobile network: ");
2981 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
2982 pw.print(formatBytesLocked(mobileTxBytes));
2983 pw.print(" sent (packets "); pw.print(mobileRxPackets);
2984 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
2986 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
2988 sb.append(prefix); sb.append(" Mobile radio active: ");
2989 formatTimeMs(sb, uidMobileActiveTime / 1000);
2991 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
2992 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
2993 long packets = mobileRxPackets + mobileTxPackets;
2998 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
3000 pw.println(sb.toString());
3003 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
3004 pw.print(prefix); pw.print(" Wi-Fi network: ");
3005 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
3006 pw.print(formatBytesLocked(wifiTxBytes));
3007 pw.print(" sent (packets "); pw.print(wifiRxPackets);
3008 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
3011 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
3012 || uidWifiRunningTime != 0) {
3014 sb.append(prefix); sb.append(" Wifi Running: ");
3015 formatTimeMs(sb, uidWifiRunningTime / 1000);
3016 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
3017 whichBatteryRealtime)); sb.append(")\n");
3018 sb.append(prefix); sb.append(" Full Wifi Lock: ");
3019 formatTimeMs(sb, fullWifiLockOnTime / 1000);
3020 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
3021 whichBatteryRealtime)); sb.append(")\n");
3022 sb.append(prefix); sb.append(" Wifi Scan: ");
3023 formatTimeMs(sb, wifiScanTime / 1000);
3024 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
3025 whichBatteryRealtime)); sb.append(")");
3026 pw.println(sb.toString());
3029 if (u.hasUserActivity()) {
3030 boolean hasData = false;
3031 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3032 int val = u.getUserActivityCount(i, which);
3036 sb.append(" User activity: ");
3043 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
3047 pw.println(sb.toString());
3051 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
3052 if (wakelocks.size() > 0) {
3053 long totalFull = 0, totalPartial = 0, totalWindow = 0;
3055 for (Map.Entry<String, ? extends Uid.Wakelock> ent : wakelocks.entrySet()) {
3056 Uid.Wakelock wl = ent.getValue();
3057 String linePrefix = ": ";
3060 sb.append(" Wake lock ");
3061 sb.append(ent.getKey());
3062 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
3063 "full", which, linePrefix);
3064 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
3065 "partial", which, linePrefix);
3066 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
3067 "window", which, linePrefix);
3068 if (true || !linePrefix.equals(": ")) {
3069 sb.append(" realtime");
3070 // Only print out wake locks that were held
3071 pw.println(sb.toString());
3075 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
3076 rawRealtime, which);
3077 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
3078 rawRealtime, which);
3079 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
3080 rawRealtime, which);
3083 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
3086 sb.append(" TOTAL wake: ");
3087 boolean needComma = false;
3088 if (totalFull != 0) {
3090 formatTimeMs(sb, totalFull);
3093 if (totalPartial != 0) {
3098 formatTimeMs(sb, totalPartial);
3099 sb.append("partial");
3101 if (totalWindow != 0) {
3106 formatTimeMs(sb, totalWindow);
3107 sb.append("window");
3109 sb.append(" realtime");
3110 pw.println(sb.toString());
3115 Map<String, ? extends Timer> syncs = u.getSyncStats();
3116 if (syncs.size() > 0) {
3117 for (Map.Entry<String, ? extends Timer> ent : syncs.entrySet()) {
3118 Timer timer = ent.getValue();
3119 // Convert from microseconds to milliseconds with rounding
3120 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3121 int count = timer.getCountLocked(which);
3124 sb.append(" Sync ");
3125 sb.append(ent.getKey());
3127 if (totalTime != 0) {
3128 formatTimeMs(sb, totalTime);
3129 sb.append("realtime (");
3131 sb.append(" times)");
3133 sb.append("(not used)");
3135 pw.println(sb.toString());
3140 Map<String, ? extends Timer> jobs = u.getJobStats();
3141 if (syncs.size() > 0) {
3142 for (Map.Entry<String, ? extends Timer> ent : jobs.entrySet()) {
3143 Timer timer = ent.getValue();
3144 // Convert from microseconds to milliseconds with rounding
3145 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3146 int count = timer.getCountLocked(which);
3150 sb.append(ent.getKey());
3152 if (totalTime != 0) {
3153 formatTimeMs(sb, totalTime);
3154 sb.append("realtime (");
3156 sb.append(" times)");
3158 sb.append("(not used)");
3160 pw.println(sb.toString());
3165 SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3166 int NSE = sensors.size();
3167 for (int ise=0; ise<NSE; ise++) {
3168 Uid.Sensor se = sensors.valueAt(ise);
3169 int sensorNumber = sensors.keyAt(ise);
3172 sb.append(" Sensor ");
3173 int handle = se.getHandle();
3174 if (handle == Uid.Sensor.GPS) {
3181 Timer timer = se.getSensorTime();
3182 if (timer != null) {
3183 // Convert from microseconds to milliseconds with rounding
3184 long totalTime = (timer.getTotalTimeLocked(
3185 rawRealtime, which) + 500) / 1000;
3186 int count = timer.getCountLocked(which);
3188 if (totalTime != 0) {
3189 formatTimeMs(sb, totalTime);
3190 sb.append("realtime (");
3192 sb.append(" times)");
3194 sb.append("(not used)");
3197 sb.append("(not used)");
3200 pw.println(sb.toString());
3204 Timer vibTimer = u.getVibratorOnTimer();
3205 if (vibTimer != null) {
3206 // Convert from microseconds to milliseconds with rounding
3207 long totalTime = (vibTimer.getTotalTimeLocked(
3208 rawRealtime, which) + 500) / 1000;
3209 int count = vibTimer.getCountLocked(which);
3211 if (totalTime != 0) {
3214 sb.append(" Vibrator: ");
3215 formatTimeMs(sb, totalTime);
3216 sb.append("realtime (");
3218 sb.append(" times)");
3219 pw.println(sb.toString());
3224 Timer fgTimer = u.getForegroundActivityTimer();
3225 if (fgTimer != null) {
3226 // Convert from microseconds to milliseconds with rounding
3227 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3228 int count = fgTimer.getCountLocked(which);
3229 if (totalTime != 0) {
3232 sb.append(" Foreground activities: ");
3233 formatTimeMs(sb, totalTime);
3234 sb.append("realtime (");
3236 sb.append(" times)");
3237 pw.println(sb.toString());
3242 long totalStateTime = 0;
3243 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
3244 long time = u.getProcessStateTime(ips, rawRealtime, which);
3246 totalStateTime += time;
3250 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
3251 sb.append(" for: ");
3252 formatTimeMs(sb, (totalStateTime + 500) / 1000);
3253 pw.println(sb.toString());
3258 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
3259 if (processStats.size() > 0) {
3260 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
3261 : processStats.entrySet()) {
3262 Uid.Proc ps = ent.getValue();
3265 long foregroundTime;
3269 userTime = ps.getUserTime(which);
3270 systemTime = ps.getSystemTime(which);
3271 foregroundTime = ps.getForegroundTime(which);
3272 starts = ps.getStarts(which);
3273 numExcessive = which == STATS_SINCE_CHARGED
3274 ? ps.countExcessivePowers() : 0;
3276 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
3277 || numExcessive != 0) {
3279 sb.append(prefix); sb.append(" Proc ");
3280 sb.append(ent.getKey()); sb.append(":\n");
3281 sb.append(prefix); sb.append(" CPU: ");
3282 formatTime(sb, userTime); sb.append("usr + ");
3283 formatTime(sb, systemTime); sb.append("krn ; ");
3284 formatTime(sb, foregroundTime); sb.append("fg");
3286 sb.append("\n"); sb.append(prefix); sb.append(" ");
3287 sb.append(starts); sb.append(" proc starts");
3289 pw.println(sb.toString());
3290 for (int e=0; e<numExcessive; e++) {
3291 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
3293 pw.print(prefix); pw.print(" * Killed for ");
3294 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
3295 pw.print("wake lock");
3296 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
3299 pw.print("unknown");
3302 TimeUtils.formatDuration(ew.usedTime, pw);
3304 TimeUtils.formatDuration(ew.overTime, pw);
3305 if (ew.overTime != 0) {
3307 pw.print((ew.usedTime*100)/ew.overTime);
3317 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
3318 if (packageStats.size() > 0) {
3319 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
3320 : packageStats.entrySet()) {
3321 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
3322 boolean apkActivity = false;
3323 Uid.Pkg ps = ent.getValue();
3324 int wakeups = ps.getWakeups(which);
3326 pw.print(prefix); pw.print(" ");
3327 pw.print(wakeups); pw.println(" wakeup alarms");
3330 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3331 if (serviceStats.size() > 0) {
3332 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
3333 : serviceStats.entrySet()) {
3334 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
3335 long startTime = ss.getStartTime(batteryUptime, which);
3336 int starts = ss.getStarts(which);
3337 int launches = ss.getLaunches(which);
3338 if (startTime != 0 || starts != 0 || launches != 0) {
3340 sb.append(prefix); sb.append(" Service ");
3341 sb.append(sent.getKey()); sb.append(":\n");
3342 sb.append(prefix); sb.append(" Created for: ");
3343 formatTimeMs(sb, startTime / 1000);
3344 sb.append("uptime\n");
3345 sb.append(prefix); sb.append(" Starts: ");
3347 sb.append(", launches: "); sb.append(launches);
3348 pw.println(sb.toString());
3354 pw.print(prefix); pw.println(" (nothing executed)");
3360 pw.print(prefix); pw.println(" (nothing executed)");
3365 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
3366 BitDescription[] descriptions, boolean longNames) {
3367 int diff = oldval ^ newval;
3368 if (diff == 0) return;
3369 boolean didWake = false;
3370 for (int i=0; i<descriptions.length; i++) {
3371 BitDescription bd = descriptions[i];
3372 if ((diff&bd.mask) != 0) {
3373 pw.print(longNames ? " " : ",");
3375 pw.print((newval&bd.mask) != 0 ? "+" : "-");
3376 pw.print(longNames ? bd.name : bd.shortName);
3377 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
3381 UserHandle.formatUid(pw, wakelockTag.uid);
3383 pw.print(wakelockTag.string);
3386 pw.print(wakelockTag.poolIdx);
3390 pw.print(longNames ? bd.name : bd.shortName);
3392 int val = (newval&bd.mask)>>bd.shift;
3393 if (bd.values != null && val >= 0 && val < bd.values.length) {
3394 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
3401 if (!didWake && wakelockTag != null) {
3402 pw.print(longNames ? " wake_lock=" : ",w=");
3404 UserHandle.formatUid(pw, wakelockTag.uid);
3406 pw.print(wakelockTag.string);
3409 pw.print(wakelockTag.poolIdx);
3414 public void prepareForDumpLocked() {
3417 public static class HistoryPrinter {
3429 oldState = oldState2 = 0;
3438 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
3442 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
3444 pw.print(rec.numReadInts);
3447 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3448 pw.print(HISTORY_DATA); pw.print(',');
3450 pw.print(rec.time - baseTime);
3452 pw.print(rec.time - lastTime);
3454 lastTime = rec.time;
3456 if (rec.cmd == HistoryItem.CMD_START) {
3460 pw.println("START");
3462 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
3463 || rec.cmd == HistoryItem.CMD_RESET) {
3467 if (rec.cmd == HistoryItem.CMD_RESET) {
3473 pw.println(rec.currentTime);
3476 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
3477 rec.currentTime).toString());
3479 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
3483 pw.println("*OVERFLOW*");
3486 if (rec.batteryLevel < 10) pw.print("00");
3487 else if (rec.batteryLevel < 100) pw.print("0");
3488 pw.print(rec.batteryLevel);
3491 if (rec.states < 0) ;
3492 else if (rec.states < 0x10) pw.print("0000000");
3493 else if (rec.states < 0x100) pw.print("000000");
3494 else if (rec.states < 0x1000) pw.print("00000");
3495 else if (rec.states < 0x10000) pw.print("0000");
3496 else if (rec.states < 0x100000) pw.print("000");
3497 else if (rec.states < 0x1000000) pw.print("00");
3498 else if (rec.states < 0x10000000) pw.print("0");
3499 pw.print(Integer.toHexString(rec.states));
3502 if (oldLevel != rec.batteryLevel) {
3503 oldLevel = rec.batteryLevel;
3504 pw.print(",Bl="); pw.print(rec.batteryLevel);
3507 if (oldStatus != rec.batteryStatus) {
3508 oldStatus = rec.batteryStatus;
3509 pw.print(checkin ? ",Bs=" : " status=");
3510 switch (oldStatus) {
3511 case BatteryManager.BATTERY_STATUS_UNKNOWN:
3512 pw.print(checkin ? "?" : "unknown");
3514 case BatteryManager.BATTERY_STATUS_CHARGING:
3515 pw.print(checkin ? "c" : "charging");
3517 case BatteryManager.BATTERY_STATUS_DISCHARGING:
3518 pw.print(checkin ? "d" : "discharging");
3520 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
3521 pw.print(checkin ? "n" : "not-charging");
3523 case BatteryManager.BATTERY_STATUS_FULL:
3524 pw.print(checkin ? "f" : "full");
3527 pw.print(oldStatus);
3531 if (oldHealth != rec.batteryHealth) {
3532 oldHealth = rec.batteryHealth;
3533 pw.print(checkin ? ",Bh=" : " health=");
3534 switch (oldHealth) {
3535 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
3536 pw.print(checkin ? "?" : "unknown");
3538 case BatteryManager.BATTERY_HEALTH_GOOD:
3539 pw.print(checkin ? "g" : "good");
3541 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
3542 pw.print(checkin ? "h" : "overheat");
3544 case BatteryManager.BATTERY_HEALTH_DEAD:
3545 pw.print(checkin ? "d" : "dead");
3547 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
3548 pw.print(checkin ? "v" : "over-voltage");
3550 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
3551 pw.print(checkin ? "f" : "failure");
3553 case BatteryManager.BATTERY_HEALTH_COLD:
3554 pw.print(checkin ? "c" : "cold");
3557 pw.print(oldHealth);
3561 if (oldPlug != rec.batteryPlugType) {
3562 oldPlug = rec.batteryPlugType;
3563 pw.print(checkin ? ",Bp=" : " plug=");
3566 pw.print(checkin ? "n" : "none");
3568 case BatteryManager.BATTERY_PLUGGED_AC:
3569 pw.print(checkin ? "a" : "ac");
3571 case BatteryManager.BATTERY_PLUGGED_USB:
3572 pw.print(checkin ? "u" : "usb");
3574 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
3575 pw.print(checkin ? "w" : "wireless");
3582 if (oldTemp != rec.batteryTemperature) {
3583 oldTemp = rec.batteryTemperature;
3584 pw.print(checkin ? ",Bt=" : " temp=");
3587 if (oldVolt != rec.batteryVoltage) {
3588 oldVolt = rec.batteryVoltage;
3589 pw.print(checkin ? ",Bv=" : " volt=");
3592 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
3593 HISTORY_STATE_DESCRIPTIONS, !checkin);
3594 printBitDescriptions(pw, oldState2, rec.states2, null,
3595 HISTORY_STATE2_DESCRIPTIONS, !checkin);
3596 if (rec.wakeReasonTag != null) {
3599 pw.print(rec.wakeReasonTag.poolIdx);
3601 pw.print(" wake_reason=");
3602 pw.print(rec.wakeReasonTag.uid);
3604 pw.print(rec.wakeReasonTag.string);
3608 if (rec.eventCode != HistoryItem.EVENT_NONE) {
3609 pw.print(checkin ? "," : " ");
3610 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
3612 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
3615 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
3616 : HISTORY_EVENT_NAMES;
3617 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
3618 | HistoryItem.EVENT_FLAG_FINISH);
3619 if (idx >= 0 && idx < eventNames.length) {
3620 pw.print(eventNames[idx]);
3622 pw.print(checkin ? "Ev" : "event");
3627 pw.print(rec.eventTag.poolIdx);
3629 UserHandle.formatUid(pw, rec.eventTag.uid);
3631 pw.print(rec.eventTag.string);
3636 oldState = rec.states;
3637 oldState2 = rec.states2;
3642 private void printSizeValue(PrintWriter pw, long size) {
3643 float result = size;
3645 if (result >= 10*1024) {
3647 result = result / 1024;
3649 if (result >= 10*1024) {
3651 result = result / 1024;
3653 if (result >= 10*1024) {
3655 result = result / 1024;
3657 if (result >= 10*1024) {
3659 result = result / 1024;
3661 if (result >= 10*1024) {
3663 result = result / 1024;
3665 pw.print((int)result);
3669 private static boolean dumpDurationSteps(PrintWriter pw, String header, long[] steps,
3670 int count, boolean checkin) {
3677 String[] lineArgs = new String[4];
3678 for (int i=0; i<count; i++) {
3679 long duration = steps[i] & STEP_LEVEL_TIME_MASK;
3680 int level = (int)((steps[i] & STEP_LEVEL_LEVEL_MASK)
3681 >> STEP_LEVEL_LEVEL_SHIFT);
3682 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
3683 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
3684 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
3685 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
3687 lineArgs[0] = Long.toString(duration);
3688 lineArgs[1] = Integer.toString(level);
3689 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
3690 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
3691 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
3692 case Display.STATE_ON: lineArgs[2] = "s+"; break;
3693 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
3694 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
3695 default: lineArgs[1] = "?"; break;
3700 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
3701 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
3705 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
3707 pw.print(" #"); pw.print(i); pw.print(": ");
3708 TimeUtils.formatDuration(duration, pw);
3709 pw.print(" to "); pw.print(level);
3710 boolean haveModes = false;
3711 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
3713 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
3714 case Display.STATE_OFF: pw.print("screen-off"); break;
3715 case Display.STATE_ON: pw.print("screen-on"); break;
3716 case Display.STATE_DOZE: pw.print("screen-doze"); break;
3717 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
3718 default: lineArgs[1] = "screen-?"; break;
3722 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
3723 pw.print(haveModes ? ", " : " (");
3724 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
3725 ? "power-save-on" : "power-save-off");
3737 public static final int DUMP_UNPLUGGED_ONLY = 1<<0;
3738 public static final int DUMP_CHARGED_ONLY = 1<<1;
3739 public static final int DUMP_HISTORY_ONLY = 1<<2;
3740 public static final int DUMP_INCLUDE_HISTORY = 1<<3;
3741 public static final int DUMP_VERBOSE = 1<<4;
3742 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<5;
3744 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
3745 final HistoryPrinter hprinter = new HistoryPrinter();
3746 final HistoryItem rec = new HistoryItem();
3749 boolean printed = false;
3750 HistoryEventTracker tracker = null;
3751 while (getNextHistoryLocked(rec)) {
3752 lastTime = rec.time;
3754 baseTime = lastTime;
3756 if (rec.time >= histStart) {
3757 if (histStart >= 0 && !printed) {
3758 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
3759 || rec.cmd == HistoryItem.CMD_RESET
3760 || rec.cmd == HistoryItem.CMD_START) {
3762 hprinter.printNextItem(pw, rec, baseTime, checkin,
3763 (flags&DUMP_VERBOSE) != 0);
3764 rec.cmd = HistoryItem.CMD_UPDATE;
3765 } else if (rec.currentTime != 0) {
3768 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
3769 hprinter.printNextItem(pw, rec, baseTime, checkin,
3770 (flags&DUMP_VERBOSE) != 0);
3773 if (tracker != null) {
3774 if (rec.cmd != HistoryItem.CMD_UPDATE) {
3775 hprinter.printNextItem(pw, rec, baseTime, checkin,
3776 (flags&DUMP_VERBOSE) != 0);
3777 rec.cmd = HistoryItem.CMD_UPDATE;
3779 int oldEventCode = rec.eventCode;
3780 HistoryTag oldEventTag = rec.eventTag;
3781 rec.eventTag = new HistoryTag();
3782 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
3783 HashMap<String, SparseIntArray> active
3784 = tracker.getStateForEvent(i);
3785 if (active == null) {
3788 for (HashMap.Entry<String, SparseIntArray> ent
3789 : active.entrySet()) {
3790 SparseIntArray uids = ent.getValue();
3791 for (int j=0; j<uids.size(); j++) {
3793 rec.eventTag.string = ent.getKey();
3794 rec.eventTag.uid = uids.keyAt(j);
3795 rec.eventTag.poolIdx = uids.valueAt(j);
3796 hprinter.printNextItem(pw, rec, baseTime, checkin,
3797 (flags&DUMP_VERBOSE) != 0);
3798 rec.wakeReasonTag = null;
3799 rec.wakelockTag = null;
3803 rec.eventCode = oldEventCode;
3804 rec.eventTag = oldEventTag;
3808 hprinter.printNextItem(pw, rec, baseTime, checkin,
3809 (flags&DUMP_VERBOSE) != 0);
3810 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
3811 // This is an attempt to aggregate the previous state and generate
3812 //Â fake events to reflect that state at the point where we start
3813 // printing real events. It doesn't really work right, so is turned off.
3814 if (tracker == null) {
3815 tracker = new HistoryEventTracker();
3817 tracker.updateState(rec.eventCode, rec.eventTag.string,
3818 rec.eventTag.uid, rec.eventTag.poolIdx);
3821 if (histStart >= 0) {
3822 commitCurrentHistoryBatchLocked();
3823 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
3828 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
3830 * @param pw a Printer to receive the dump output.
3832 @SuppressWarnings("unused")
3833 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
3834 prepareForDumpLocked();
3836 final boolean filtering =
3837 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
3839 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
3840 final long historyTotalSize = getHistoryTotalSize();
3841 final long historyUsedSize = getHistoryUsedSize();
3842 if (startIteratingHistoryLocked()) {
3844 pw.print("Battery History (");
3845 pw.print((100*historyUsedSize)/historyTotalSize);
3846 pw.print("% used, ");
3847 printSizeValue(pw, historyUsedSize);
3848 pw.print(" used of ");
3849 printSizeValue(pw, historyTotalSize);
3851 pw.print(getHistoryStringPoolSize());
3852 pw.print(" strings using ");
3853 printSizeValue(pw, getHistoryStringPoolBytes());
3855 dumpHistoryLocked(pw, flags, histStart, false);
3858 finishIteratingHistoryLocked();
3862 if (startIteratingOldHistoryLocked()) {
3864 final HistoryItem rec = new HistoryItem();
3865 pw.println("Old battery History:");
3866 HistoryPrinter hprinter = new HistoryPrinter();
3868 while (getNextOldHistoryLocked(rec)) {
3870 baseTime = rec.time;
3872 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
3876 finishIteratingOldHistoryLocked();
3881 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
3886 SparseArray<? extends Uid> uidStats = getUidStats();
3887 final int NU = uidStats.size();
3888 boolean didPid = false;
3889 long nowRealtime = SystemClock.elapsedRealtime();
3890 for (int i=0; i<NU; i++) {
3891 Uid uid = uidStats.valueAt(i);
3892 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
3894 for (int j=0; j<pids.size(); j++) {
3895 Uid.Pid pid = pids.valueAt(j);
3897 pw.println("Per-PID Stats:");
3900 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
3901 ? (nowRealtime - pid.mWakeStartMs) : 0);
3902 pw.print(" PID "); pw.print(pids.keyAt(j));
3903 pw.print(" wake time: ");
3904 TimeUtils.formatDuration(time, pw);
3914 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
3915 if (dumpDurationSteps(pw, "Discharge step durations:", getDischargeStepDurationsArray(),
3916 getNumDischargeStepDurations(), false)) {
3917 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
3918 if (timeRemaining >= 0) {
3919 pw.print(" Estimated discharge time remaining: ");
3920 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3925 if (dumpDurationSteps(pw, "Charge step durations:", getChargeStepDurationsArray(),
3926 getNumChargeStepDurations(), false)) {
3927 long timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
3928 if (timeRemaining >= 0) {
3929 pw.print(" Estimated charge time remaining: ");
3930 TimeUtils.formatDuration(timeRemaining / 1000, pw);
3935 pw.println("Statistics since last charge:");
3936 pw.println(" System starts: " + getStartCount()
3937 + ", currently on battery: " + getIsOnBattery());
3938 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
3939 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
3942 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
3943 pw.println("Statistics since last unplugged:");
3944 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid,
3945 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
3949 @SuppressWarnings("unused")
3950 public void dumpCheckinLocked(Context context, PrintWriter pw,
3951 List<ApplicationInfo> apps, int flags, long histStart) {
3952 prepareForDumpLocked();
3954 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
3955 "10", getParcelVersion(), getStartPlatformVersion(), getEndPlatformVersion());
3957 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3959 final boolean filtering =
3960 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
3962 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
3963 if (startIteratingHistoryLocked()) {
3965 for (int i=0; i<getHistoryStringPoolSize(); i++) {
3966 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3967 pw.print(HISTORY_STRING_POOL); pw.print(',');
3970 pw.print(getHistoryTagPoolUid(i));
3972 String str = getHistoryTagPoolString(i);
3973 str = str.replace("\\", "\\\\");
3974 str = str.replace("\"", "\\\"");
3979 dumpHistoryLocked(pw, flags, histStart, true);
3981 finishIteratingHistoryLocked();
3986 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
3991 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
3992 for (int i=0; i<apps.size(); i++) {
3993 ApplicationInfo ai = apps.get(i);
3994 ArrayList<String> pkgs = uids.get(ai.uid);
3996 pkgs = new ArrayList<String>();
3997 uids.put(ai.uid, pkgs);
3999 pkgs.add(ai.packageName);
4001 SparseArray<? extends Uid> uidStats = getUidStats();
4002 final int NU = uidStats.size();
4003 String[] lineArgs = new String[2];
4004 for (int i=0; i<NU; i++) {
4005 int uid = uidStats.keyAt(i);
4006 ArrayList<String> pkgs = uids.get(uid);
4008 for (int j=0; j<pkgs.size(); j++) {
4009 lineArgs[0] = Integer.toString(uid);
4010 lineArgs[1] = pkgs.get(j);
4011 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
4012 (Object[])lineArgs);
4017 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
4018 dumpDurationSteps(pw, DISCHARGE_STEP_DATA, getDischargeStepDurationsArray(),
4019 getNumDischargeStepDurations(), true);
4020 String[] lineArgs = new String[1];
4021 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime());
4022 if (timeRemaining >= 0) {
4023 lineArgs[0] = Long.toString(timeRemaining);
4024 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
4025 (Object[])lineArgs);
4027 dumpDurationSteps(pw, CHARGE_STEP_DATA, getChargeStepDurationsArray(),
4028 getNumChargeStepDurations(), true);
4029 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime());
4030 if (timeRemaining >= 0) {
4031 lineArgs[0] = Long.toString(timeRemaining);
4032 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
4033 (Object[])lineArgs);
4035 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
4036 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
4038 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
4039 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1,
4040 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);