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