blob: 2afea1fbc87d8b419681476cbfa90486d46e6d37 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
19import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080020import java.util.ArrayList;
Dianne Hackborn81038902012-11-26 17:04:09 -080021import java.util.Collections;
22import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.Formatter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080024import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import java.util.Map;
26
Dianne Hackborna7c837f2014-01-15 16:20:44 -080027import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080028import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080029import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080030import android.text.format.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.Printer;
32import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070033import android.util.TimeUtils;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080034import com.android.internal.os.BatterySipper;
35import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
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;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070046
47 /** @hide */
48 public static final String SERVICE_NAME = "batterystats";
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 /**
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.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 */
68 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070069
70 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070071 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070072 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070073 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070074
75 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070076 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070077 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070078 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070079
80 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070081 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070082 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070083 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Robert Greenwalt5347bd42009-05-13 15:10:16 -070085 /**
86 * A constant indicating a wifi multicast timer
Robert Greenwalt5347bd42009-05-13 15:10:16 -070087 */
88 public static final int WIFI_MULTICAST_ENABLED = 7;
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070091 * A constant indicating an audio turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070092 */
93 public static final int AUDIO_TURNED_ON = 7;
94
95 /**
96 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070097 */
98 public static final int VIDEO_TURNED_ON = 8;
99
100 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800101 * A constant indicating a vibrator on timer
102 */
103 public static final int VIBRATOR_ON = 9;
104
105 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700106 * A constant indicating a foreground activity timer
107 */
108 public static final int FOREGROUND_ACTIVITY = 10;
109
110 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700111 * A constant indicating a wifi batched scan is active
112 */
113 public static final int WIFI_BATCHED_SCAN = 11;
114
115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * Include all of the data in the stats, including previously saved data.
117 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700118 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
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 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700133 public static final int STATS_SINCE_UNPLUGGED = 3;
Evan Millare84de8d2009-04-02 22:16:12 -0700134
135 // NOTE: Update this list if you add/change any stats above.
136 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700137 // and "unplugged". They were shortened for efficiency sake.
Evan Millare84de8d2009-04-02 22:16:12 -0700138 private static final String[] STAT_NAMES = { "t", "l", "c", "u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
140 /**
141 * Bump the version on this if the checkin format changes.
142 */
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700143 private static final int BATTERY_STATS_CHECKIN_VERSION = 7;
Evan Millar22ac0432009-03-31 11:33:18 -0700144
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Dianne Hackborne4a59512010-12-07 11:08:07 -0800150 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700152 private static final String PROCESS_DATA = "pr";
153 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800154 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700155 private static final String FOREGROUND_DATA = "fg";
Evan Millare84de8d2009-04-02 22:16:12 -0700156 private static final String WAKELOCK_DATA = "wl";
Evan Millarc64edde2009-04-18 12:26:32 -0700157 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Evan Millare84de8d2009-04-02 22:16:12 -0700158 private static final String NETWORK_DATA = "nt";
159 private static final String USER_ACTIVITY_DATA = "ua";
160 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800161 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700162 private static final String BATTERY_LEVEL_DATA = "lv";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700163 private static final String WIFI_DATA = "wfl";
Evan Millare84de8d2009-04-02 22:16:12 -0700164 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800165 private static final String GLOBAL_NETWORK_DATA = "gn";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800166 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700167 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700168 private static final String SCREEN_BRIGHTNESS_DATA = "br";
169 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700170 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700171 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";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800174 private static final String POWER_USE_SUMMARY_DATA = "pws";
175 private static final String POWER_USE_ITEM_DATA = "pwi";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700177 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 private final Formatter mFormatter = new Formatter(mFormatBuilder);
179
180 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700181 * 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 */
Evan Millarc64edde2009-04-18 12:26:32 -0700191 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700192
193 /**
194 * Temporary for debugging.
195 */
196 public abstract void logState(Printer pw, String prefix);
197 }
198
199 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * 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 */
Evan Millarc64edde2009-04-18 12:26:32 -0700210 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211
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 */
Evan Millarc64edde2009-04-18 12:26:32 -0700220 public abstract long getTotalTimeLocked(long batteryRealtime, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 /**
223 * Temporary for debugging.
224 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700225 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 }
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 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700255 * Returns a mapping containing active process data.
256 */
257 public abstract SparseArray<? extends Pid> getPidStats();
258
259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * 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();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700277
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700278 public abstract void noteWifiRunningLocked();
279 public abstract void noteWifiStoppedLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700280 public abstract void noteFullWifiLockAcquiredLocked();
281 public abstract void noteFullWifiLockReleasedLocked();
Nick Pelly6ccaa542012-06-15 15:22:47 -0700282 public abstract void noteWifiScanStartedLocked();
283 public abstract void noteWifiScanStoppedLocked();
Robert Greenwalta029ea12013-09-25 16:38:12 -0700284 public abstract void noteWifiBatchedScanStartedLocked(int csph);
285 public abstract void noteWifiBatchedScanStoppedLocked();
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700286 public abstract void noteWifiMulticastEnabledLocked();
287 public abstract void noteWifiMulticastDisabledLocked();
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700288 public abstract void noteAudioTurnedOnLocked();
289 public abstract void noteAudioTurnedOffLocked();
290 public abstract void noteVideoTurnedOnLocked();
291 public abstract void noteVideoTurnedOffLocked();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700292 public abstract void noteActivityResumedLocked();
293 public abstract void noteActivityPausedLocked();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700294 public abstract long getWifiRunningTime(long batteryRealtime, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -0700295 public abstract long getFullWifiLockTime(long batteryRealtime, int which);
Nick Pelly6ccaa542012-06-15 15:22:47 -0700296 public abstract long getWifiScanTime(long batteryRealtime, int which);
Robert Greenwalta029ea12013-09-25 16:38:12 -0700297 public abstract long getWifiBatchedScanTime(int csphBin, long batteryRealtime, int which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700298 public abstract long getWifiMulticastTime(long batteryRealtime,
299 int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700300 public abstract long getAudioTurnedOnTime(long batteryRealtime, int which);
301 public abstract long getVideoTurnedOnTime(long batteryRealtime, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700302 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800303 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304
Robert Greenwalta029ea12013-09-25 16:38:12 -0700305 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
306
Dianne Hackborn617f8772009-03-31 15:04:46 -0700307 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700308 * 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.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700311 */
312 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700313 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700314 };
315
Jeff Browndf693de2012-07-27 12:03:38 -0700316 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700317
Dianne Hackborn617f8772009-03-31 15:04:46 -0700318 public abstract void noteUserActivityLocked(int type);
319 public abstract boolean hasUserActivity();
320 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700321
322 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800323 public abstract long getNetworkActivityBytes(int type, int which);
324 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800327 /*
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 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 // 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
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700340 public class Pid {
341 public long mWakeSum;
342 public long mWakeStart;
343 }
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
346 * The statistics associated with a particular process.
347 */
348 public static abstract class Proc {
349
Dianne Hackborn287952c2010-09-22 22:34:31 -0700350 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;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700355 public long overTime;
356 public long usedTime;
357 }
358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800360 * Returns true if this process is still active in the battery stats.
361 */
362 public abstract boolean isActive();
363
364 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 * 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);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700384
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);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700391
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);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700400
Dianne Hackborn287952c2010-09-22 22:34:31 -0700401 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700402
Dianne Hackborn287952c2010-09-22 22:34:31 -0700403 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 }
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
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700455 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700456 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700457
458 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800459
460 // The command codes 0-3 can be written with delta updates; all others require
461 // that a full entry be written.
462 public static final byte CMD_UPDATE = 0;
463 public static final byte CMD_EVENT = 1;
464 public static final byte CMD_NULL = -1;
465 public static final byte CMD_START = 4;
466 public static final byte CMD_OVERFLOW = 5;
467
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700468 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700469
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800470 /**
471 * Return whether the command code is a delta data update.
472 */
473 public static boolean isDeltaData(byte cmd) {
474 return cmd >= 0 && cmd <= 3;
475 }
476
477 /**
478 * Return whether the command code is a delta data update.
479 */
480 public boolean isDeltaData() {
481 return cmd >= 0 && cmd <= 3;
482 }
483
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700484 public byte batteryLevel;
485 public byte batteryStatus;
486 public byte batteryHealth;
487 public byte batteryPlugType;
488
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900489 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700490 public char batteryVoltage;
491
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700492 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700493 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800494 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700495 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800496 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
497 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700498 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800499 public static final int STATE_PHONE_STATE_SHIFT = 6;
500 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700501 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800502 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
503 public static final int STATE_DATA_CONNECTION_MASK = 0x1f;
504
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700505 // These states always appear directly in the first int token
506 // of a delta change; they should be ones that change relatively
507 // frequently.
508 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
509 public static final int STATE_SENSOR_ON_FLAG = 1<<29;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700510 public static final int STATE_GPS_ON_FLAG = 1<<28;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700511 public static final int STATE_PHONE_SCANNING_FLAG = 1<<27;
512 public static final int STATE_WIFI_RUNNING_FLAG = 1<<26;
513 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<25;
Nick Pelly6ccaa542012-06-15 15:22:47 -0700514 public static final int STATE_WIFI_SCAN_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700515 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<23;
516 // These are on the lower bits used for the command; if they change
517 // we need to write another int of data.
518 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
519 public static final int STATE_VIDEO_ON_FLAG = 1<<21;
520 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
521 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
522 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
523 public static final int STATE_WIFI_ON_FLAG = 1<<17;
524 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700525
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700526 public static final int MOST_INTERESTING_STATES =
527 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
528 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
529
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700530 public int states;
531
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800532 public static final int EVENT_NONE = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800533 public static final int EVENT_PROC_STARTED = 1;
534 public static final int EVENT_PROC_FINISHED = 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800535
536 // For CMD_EVENT.
537 public int eventCode;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800538 public String eventName;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800539 public int eventNameIdx; // only filled in when iterating.
540 public int eventUid;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800541
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700542 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700543 }
544
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700545 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700546 this.time = time;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700547 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700548 }
549
550 public int describeContents() {
551 return 0;
552 }
553
554 public void writeToParcel(Parcel dest, int flags) {
555 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700556 int bat = (((int)cmd)&0xff)
557 | ((((int)batteryLevel)<<8)&0xff00)
558 | ((((int)batteryStatus)<<16)&0xf0000)
559 | ((((int)batteryHealth)<<20)&0xf00000)
560 | ((((int)batteryPlugType)<<24)&0xf000000);
561 dest.writeInt(bat);
562 bat = (((int)batteryTemperature)&0xffff)
563 | ((((int)batteryVoltage)<<16)&0xffff0000);
564 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700565 dest.writeInt(states);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800566 if (cmd == CMD_EVENT) {
567 dest.writeInt(eventCode);
568 dest.writeInt(eventUid);
569 dest.writeString(eventName);
570 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700571 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700572
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800573 public void readFromParcel(Parcel src) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700574 int bat = src.readInt();
575 cmd = (byte)(bat&0xff);
576 batteryLevel = (byte)((bat>>8)&0xff);
577 batteryStatus = (byte)((bat>>16)&0xf);
578 batteryHealth = (byte)((bat>>20)&0xf);
579 batteryPlugType = (byte)((bat>>24)&0xf);
580 bat = src.readInt();
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900581 batteryTemperature = (short)(bat&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700582 batteryVoltage = (char)((bat>>16)&0xffff);
583 states = src.readInt();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800584 if (cmd == CMD_EVENT) {
585 eventCode = src.readInt();
586 eventUid = src.readInt();
587 eventName = src.readString();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800588 eventNameIdx = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700589 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800590 eventCode = EVENT_NONE;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700591 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700592 }
593
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700594 public void clear() {
595 time = 0;
596 cmd = CMD_NULL;
597 batteryLevel = 0;
598 batteryStatus = 0;
599 batteryHealth = 0;
600 batteryPlugType = 0;
601 batteryTemperature = 0;
602 batteryVoltage = 0;
603 states = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800604 eventCode = EVENT_NONE;
605 eventUid = 0;
606 eventName = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700607 }
608
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700609 public void setTo(HistoryItem o) {
610 time = o.time;
611 cmd = o.cmd;
612 batteryLevel = o.batteryLevel;
613 batteryStatus = o.batteryStatus;
614 batteryHealth = o.batteryHealth;
615 batteryPlugType = o.batteryPlugType;
616 batteryTemperature = o.batteryTemperature;
617 batteryVoltage = o.batteryVoltage;
618 states = o.states;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800619 eventCode = o.eventCode;
620 eventUid = o.eventUid;
621 eventName = o.eventName;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800622 eventNameIdx = o.eventNameIdx;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700623 }
624
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800625 public void setTo(long time, byte cmd, int eventCode, int eventUid, String eventName,
626 HistoryItem o) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700627 this.time = time;
628 this.cmd = cmd;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800629 this.eventCode = eventCode;
630 this.eventUid = eventUid;
631 this.eventName = eventName;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800632 this.eventNameIdx = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700633 batteryLevel = o.batteryLevel;
634 batteryStatus = o.batteryStatus;
635 batteryHealth = o.batteryHealth;
636 batteryPlugType = o.batteryPlugType;
637 batteryTemperature = o.batteryTemperature;
638 batteryVoltage = o.batteryVoltage;
639 states = o.states;
640 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700641
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800642 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700643 return batteryLevel == o.batteryLevel
644 && batteryStatus == o.batteryStatus
645 && batteryHealth == o.batteryHealth
646 && batteryPlugType == o.batteryPlugType
647 && batteryTemperature == o.batteryTemperature
648 && batteryVoltage == o.batteryVoltage
649 && states == o.states;
650 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800651
652 public boolean same(HistoryItem o) {
653 if (!sameNonEvent(o) || eventCode != o.eventCode || eventUid != o.eventUid) {
654 return false;
655 }
656 if (eventName == o.eventName) {
657 return true;
658 }
659 return eventName != null && o.eventName != null && eventName.equals(o.eventName);
660 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700661 }
662
663 public static final class BitDescription {
664 public final int mask;
665 public final int shift;
666 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800667 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700668 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800669 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700670
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800671 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700672 this.mask = mask;
673 this.shift = -1;
674 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800675 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700676 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800677 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700678 }
679
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800680 public BitDescription(int mask, int shift, String name, String shortName,
681 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700682 this.mask = mask;
683 this.shift = shift;
684 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800685 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700686 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800687 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700688 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700689 }
690
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700691 public abstract boolean startIteratingHistoryLocked();
692
Dianne Hackborn099bc622014-01-22 13:39:16 -0800693 public abstract int getHistoryStringPoolSize();
694
695 public abstract String getHistoryStringPoolItem(int index);
696
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700697 public abstract boolean getNextHistoryLocked(HistoryItem out);
698
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700699 public abstract void finishIteratingHistoryLocked();
700
701 public abstract boolean startIteratingOldHistoryLocked();
702
703 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
704
705 public abstract void finishIteratingOldHistoryLocked();
706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700708 * Return the base time offset for the battery history.
709 */
710 public abstract long getHistoryBaseTime();
711
712 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 * Returns the number of times the device has been started.
714 */
715 public abstract int getStartCount();
716
717 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700718 * Returns the time in microseconds that the screen has been on while the device was
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 * running on battery.
720 *
721 * {@hide}
722 */
723 public abstract long getScreenOnTime(long batteryRealtime, int which);
724
Dianne Hackborn617f8772009-03-31 15:04:46 -0700725 public static final int SCREEN_BRIGHTNESS_DARK = 0;
726 public static final int SCREEN_BRIGHTNESS_DIM = 1;
727 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
728 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
729 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
730
731 static final String[] SCREEN_BRIGHTNESS_NAMES = {
732 "dark", "dim", "medium", "light", "bright"
733 };
734
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800735 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
736 "0", "1", "2", "3", "4"
737 };
738
Dianne Hackborn617f8772009-03-31 15:04:46 -0700739 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
740
741 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700742 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700743 * the given brightness
744 *
745 * {@hide}
746 */
747 public abstract long getScreenBrightnessTime(int brightnessBin,
748 long batteryRealtime, int which);
749
750 public abstract int getInputEventCount(int which);
751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700753 * Returns the time in microseconds that the phone has been on while the device was
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 * running on battery.
755 *
756 * {@hide}
757 */
758 public abstract long getPhoneOnTime(long batteryRealtime, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700759
760 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700761 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700762 * the given signal strength.
763 *
764 * {@hide}
765 */
766 public abstract long getPhoneSignalStrengthTime(int strengthBin,
767 long batteryRealtime, int which);
768
Dianne Hackborn617f8772009-03-31 15:04:46 -0700769 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700770 * Returns the time in microseconds that the phone has been trying to
771 * acquire a signal.
772 *
773 * {@hide}
774 */
775 public abstract long getPhoneSignalScanningTime(
776 long batteryRealtime, int which);
777
778 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700779 * Returns the number of times the phone has entered the given signal strength.
780 *
781 * {@hide}
782 */
783 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
784
Dianne Hackborn627bba72009-03-24 22:32:56 -0700785 public static final int DATA_CONNECTION_NONE = 0;
786 public static final int DATA_CONNECTION_GPRS = 1;
787 public static final int DATA_CONNECTION_EDGE = 2;
788 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700789 public static final int DATA_CONNECTION_CDMA = 4;
790 public static final int DATA_CONNECTION_EVDO_0 = 5;
791 public static final int DATA_CONNECTION_EVDO_A = 6;
792 public static final int DATA_CONNECTION_1xRTT = 7;
793 public static final int DATA_CONNECTION_HSDPA = 8;
794 public static final int DATA_CONNECTION_HSUPA = 9;
795 public static final int DATA_CONNECTION_HSPA = 10;
796 public static final int DATA_CONNECTION_IDEN = 11;
797 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700798 public static final int DATA_CONNECTION_LTE = 13;
799 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -0800800 public static final int DATA_CONNECTION_HSPAP = 15;
801 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700802
Dianne Hackborn627bba72009-03-24 22:32:56 -0700803 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700804 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -0700805 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -0800806 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -0700807 };
808
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700809 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -0700810
811 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700812 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700813 * the given data connection.
814 *
815 * {@hide}
816 */
817 public abstract long getPhoneDataConnectionTime(int dataType,
818 long batteryRealtime, int which);
819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700821 * Returns the number of times the phone has entered the given data
822 * connection type.
823 *
824 * {@hide}
825 */
826 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700827
828 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
829 = new BitDescription[] {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800830 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
831 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
832 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
833 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
834 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
835 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
836 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
837 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
838 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
839 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
840 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
841 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
842 new BitDescription(HistoryItem.STATE_VIDEO_ON_FLAG, "video", "v"),
843 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
844 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700845 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800846 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
847 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700848 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800849 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
850 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
851 "0", "1", "2", "3", "4"
852 }),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700853 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800854 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
855 new String[] {"in", "out", "emergency", "off"},
856 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700857 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800858 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
859 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700860 };
Dianne Hackborn617f8772009-03-31 15:04:46 -0700861
862 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700863 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -0700864 * running on battery.
865 *
866 * {@hide}
867 */
868 public abstract long getWifiOnTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700869
870 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700871 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700872 * been in the running state while the device was running on battery.
873 *
874 * {@hide}
875 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700876 public abstract long getGlobalWifiRunningTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700877
The Android Open Source Project10592532009-03-18 17:39:46 -0700878 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700879 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -0700880 * running on battery.
881 *
882 * {@hide}
883 */
884 public abstract long getBluetoothOnTime(long batteryRealtime, int which);
885
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800886 public abstract int getBluetoothPingCount();
887
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800888 public static final int NETWORK_MOBILE_RX_DATA = 0;
889 public static final int NETWORK_MOBILE_TX_DATA = 1;
890 public static final int NETWORK_WIFI_RX_DATA = 2;
891 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700892
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800893 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700894
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800895 public abstract long getNetworkActivityBytes(int type, int which);
896 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700897
The Android Open Source Project10592532009-03-18 17:39:46 -0700898 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800899 * Return the wall clock time when battery stats data collection started.
900 */
901 public abstract long getStartClockTime();
902
903 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 * Return whether we are currently running on battery.
905 */
906 public abstract boolean getIsOnBattery();
907
908 /**
909 * Returns a SparseArray containing the statistics for each uid.
910 */
911 public abstract SparseArray<? extends Uid> getUidStats();
912
913 /**
914 * Returns the current battery uptime in microseconds.
915 *
916 * @param curTime the amount of elapsed realtime in microseconds.
917 */
918 public abstract long getBatteryUptime(long curTime);
919
920 /**
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700921 * @deprecated use getRadioDataUptime
922 */
923 public long getRadioDataUptimeMs() {
924 return getRadioDataUptime() / 1000;
925 }
926
927 /**
928 * Returns the time that the radio was on for data transfers.
929 * @return the uptime in microseconds while unplugged
930 */
931 public abstract long getRadioDataUptime();
932
933 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 * Returns the current battery realtime in microseconds.
935 *
936 * @param curTime the amount of elapsed realtime in microseconds.
937 */
938 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -0700939
940 /**
Evan Millar633a1742009-04-02 16:36:33 -0700941 * Returns the battery percentage level at the last time the device was unplugged from power, or
942 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -0700943 */
Evan Millar633a1742009-04-02 16:36:33 -0700944 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -0700945
946 /**
Evan Millar633a1742009-04-02 16:36:33 -0700947 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
948 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -0700949 */
Evan Millar633a1742009-04-02 16:36:33 -0700950 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951
952 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700953 * Get the amount the battery has discharged since the stats were
954 * last reset after charging, as a lower-end approximation.
955 */
956 public abstract int getLowDischargeAmountSinceCharge();
957
958 /**
959 * Get the amount the battery has discharged since the stats were
960 * last reset after charging, as an upper-end approximation.
961 */
962 public abstract int getHighDischargeAmountSinceCharge();
963
964 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800965 * Get the amount the battery has discharged while the screen was on,
966 * since the last time power was unplugged.
967 */
968 public abstract int getDischargeAmountScreenOn();
969
970 /**
971 * Get the amount the battery has discharged while the screen was on,
972 * since the last time the device was charged.
973 */
974 public abstract int getDischargeAmountScreenOnSinceCharge();
975
976 /**
977 * Get the amount the battery has discharged while the screen was off,
978 * since the last time power was unplugged.
979 */
980 public abstract int getDischargeAmountScreenOff();
981
982 /**
983 * Get the amount the battery has discharged while the screen was off,
984 * since the last time the device was charged.
985 */
986 public abstract int getDischargeAmountScreenOffSinceCharge();
987
988 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 * Returns the total, last, or current battery uptime in microseconds.
990 *
991 * @param curTime the elapsed realtime in microseconds.
992 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
993 */
994 public abstract long computeBatteryUptime(long curTime, int which);
995
996 /**
997 * Returns the total, last, or current battery realtime in microseconds.
998 *
999 * @param curTime the current elapsed realtime in microseconds.
1000 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1001 */
1002 public abstract long computeBatteryRealtime(long curTime, int which);
1003
1004 /**
1005 * Returns the total, last, or current uptime in microseconds.
1006 *
1007 * @param curTime the current elapsed realtime in microseconds.
1008 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1009 */
1010 public abstract long computeUptime(long curTime, int which);
1011
1012 /**
1013 * Returns the total, last, or current realtime in microseconds.
1014 * *
1015 * @param curTime the current elapsed realtime in microseconds.
1016 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1017 */
1018 public abstract long computeRealtime(long curTime, int which);
Evan Millarc64edde2009-04-18 12:26:32 -07001019
1020 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021
Amith Yamasanie43530a2009-08-21 13:11:37 -07001022 /** Returns the number of different speeds that the CPU can run at */
1023 public abstract int getCpuSpeedSteps();
1024
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001025 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1026
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001027 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 long days = seconds / (60 * 60 * 24);
1029 if (days != 0) {
1030 out.append(days);
1031 out.append("d ");
1032 }
1033 long used = days * 60 * 60 * 24;
1034
1035 long hours = (seconds - used) / (60 * 60);
1036 if (hours != 0 || used != 0) {
1037 out.append(hours);
1038 out.append("h ");
1039 }
1040 used += hours * 60 * 60;
1041
1042 long mins = (seconds-used) / 60;
1043 if (mins != 0 || used != 0) {
1044 out.append(mins);
1045 out.append("m ");
1046 }
1047 used += mins * 60;
1048
1049 if (seconds != 0 || used != 0) {
1050 out.append(seconds-used);
1051 out.append("s ");
1052 }
1053 }
1054
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001055 private final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001057 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 sb.append((time - (sec * 100)) * 10);
1059 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
1061
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001062 private final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001064 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 sb.append(time - (sec * 1000));
1066 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
1068
1069 private final String formatRatioLocked(long num, long den) {
1070 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001071 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073 float perc = ((float)num) / ((float)den) * 100;
1074 mFormatBuilder.setLength(0);
1075 mFormatter.format("%.1f%%", perc);
1076 return mFormatBuilder.toString();
1077 }
1078
Evan Millar22ac0432009-03-31 11:33:18 -07001079 private final String formatBytesLocked(long bytes) {
1080 mFormatBuilder.setLength(0);
1081
1082 if (bytes < BYTES_PER_KB) {
1083 return bytes + "B";
1084 } else if (bytes < BYTES_PER_MB) {
1085 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1086 return mFormatBuilder.toString();
1087 } else if (bytes < BYTES_PER_GB){
1088 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1089 return mFormatBuilder.toString();
1090 } else {
1091 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1092 return mFormatBuilder.toString();
1093 }
1094 }
1095
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001096 private static long computeWakeLock(Timer timer, long batteryRealtime, int which) {
1097 if (timer != null) {
1098 // Convert from microseconds to milliseconds with rounding
1099 long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which);
1100 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1101 return totalTimeMillis;
1102 }
1103 return 0;
1104 }
1105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 /**
1107 *
1108 * @param sb a StringBuilder object.
1109 * @param timer a Timer object contining the wakelock times.
1110 * @param batteryRealtime the current on-battery time in microseconds.
1111 * @param name the name of the wakelock.
1112 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1113 * @param linePrefix a String to be prepended to each line of output.
1114 * @return the line prefix
1115 */
1116 private static final String printWakeLock(StringBuilder sb, Timer timer,
1117 long batteryRealtime, String name, int which, String linePrefix) {
1118
1119 if (timer != null) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001120 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121
Evan Millarc64edde2009-04-18 12:26:32 -07001122 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 if (totalTimeMillis != 0) {
1124 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001125 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001126 if (name != null) {
1127 sb.append(name);
1128 sb.append(' ');
1129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 sb.append('(');
1131 sb.append(count);
1132 sb.append(" times)");
1133 return ", ";
1134 }
1135 }
1136 return linePrefix;
1137 }
1138
1139 /**
1140 * Checkin version of wakelock printer. Prints simple comma-separated list.
1141 *
1142 * @param sb a StringBuilder object.
1143 * @param timer a Timer object contining the wakelock times.
1144 * @param now the current time in microseconds.
1145 * @param name the name of the wakelock.
1146 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1147 * @param linePrefix a String to be prepended to each line of output.
1148 * @return the line prefix
1149 */
1150 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer, long now,
Evan Millarc64edde2009-04-18 12:26:32 -07001151 String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 long totalTimeMicros = 0;
1153 int count = 0;
1154 if (timer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001155 totalTimeMicros = timer.getTotalTimeLocked(now, which);
1156 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158 sb.append(linePrefix);
1159 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1160 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001161 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 sb.append(count);
1163 return ",";
1164 }
1165
1166 /**
1167 * Dump a comma-separated line of values for terse checkin mode.
1168 *
1169 * @param pw the PageWriter to dump log to
1170 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1171 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1172 * @param args type-dependent data arguments
1173 */
1174 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1175 Object... args ) {
1176 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1177 pw.print(uid); pw.print(',');
1178 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001179 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180
1181 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001182 pw.print(',');
1183 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001185 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187
1188 /**
1189 * Checkin server version of dump to produce more compact, computer-readable log.
1190 *
1191 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001193 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1195 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1196 final long batteryUptime = getBatteryUptime(rawUptime);
1197 final long batteryRealtime = getBatteryRealtime(rawRealtime);
1198 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1199 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1200 final long totalRealtime = computeRealtime(rawRealtime, which);
1201 final long totalUptime = computeUptime(rawUptime, which);
1202 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1203 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001204 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001205 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001206 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207
1208 StringBuilder sb = new StringBuilder(128);
1209
Evan Millar22ac0432009-03-31 11:33:18 -07001210 SparseArray<? extends Uid> uidStats = getUidStats();
1211 final int NU = uidStats.size();
1212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 // Dump "battery" stat
1216 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001217 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001218 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001219 totalRealtime / 1000, totalUptime / 1000,
1220 getStartClockTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001222 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001223 long fullWakeLockTimeTotal = 0;
1224 long partialWakeLockTimeTotal = 0;
1225
1226 for (int iu = 0; iu < NU; iu++) {
1227 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001228
Evan Millar22ac0432009-03-31 11:33:18 -07001229 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1230 if (wakelocks.size() > 0) {
1231 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1232 : wakelocks.entrySet()) {
1233 Uid.Wakelock wl = ent.getValue();
1234
1235 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1236 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001237 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(batteryRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001238 }
1239
1240 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1241 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001242 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001243 batteryRealtime, which);
1244 }
1245 }
1246 }
1247 }
1248
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001249 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1250 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1251 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1252 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1253 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1254 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1255 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1256 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1257
1258 // Dump network stats
1259 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1260 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1261 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 // Dump misc stats
1264 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001265 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001266 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001267 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001268 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
1269 getInputEventCount(which));
1270
1271 // Dump screen brightness stats
1272 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1273 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1274 args[i] = getScreenBrightnessTime(i, batteryRealtime, which) / 1000;
1275 }
1276 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001277
Dianne Hackborn627bba72009-03-24 22:32:56 -07001278 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001279 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1280 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001281 args[i] = getPhoneSignalStrengthTime(i, batteryRealtime, which) / 1000;
1282 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001283 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001284 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
1285 getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001286 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001287 args[i] = getPhoneSignalStrengthCount(i, which);
1288 }
1289 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001290
1291 // Dump network type stats
1292 args = new Object[NUM_DATA_CONNECTION_TYPES];
1293 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1294 args[i] = getPhoneDataConnectionTime(i, batteryRealtime, which) / 1000;
1295 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001296 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1297 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1298 args[i] = getPhoneDataConnectionCount(i, which);
1299 }
1300 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001301
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001302 if (which == STATS_SINCE_UNPLUGGED) {
Evan Millare84de8d2009-04-02 22:16:12 -07001303 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001304 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001307 if (which == STATS_SINCE_UNPLUGGED) {
1308 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1309 getDischargeStartLevel()-getDischargeCurrentLevel(),
1310 getDischargeStartLevel()-getDischargeCurrentLevel(),
1311 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1312 } else {
1313 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1314 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1315 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1316 }
1317
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001318 if (reqUid < 0) {
1319 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1320 if (kernelWakelocks.size() > 0) {
1321 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1322 sb.setLength(0);
1323 printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, "");
1324
1325 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
1326 sb.toString());
1327 }
Evan Millarc64edde2009-04-18 12:26:32 -07001328 }
1329 }
1330
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001331 BatteryStatsHelper helper = new BatteryStatsHelper(context);
1332 helper.create(this);
1333 helper.refreshStats(which, UserHandle.USER_ALL);
1334 List<BatterySipper> sippers = helper.getUsageList();
1335 if (sippers != null && sippers.size() > 0) {
1336 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1337 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001338 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001339 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1340 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1341 for (int i=0; i<sippers.size(); i++) {
1342 BatterySipper bs = sippers.get(i);
1343 int uid = 0;
1344 String label;
1345 switch (bs.drainType) {
1346 case IDLE:
1347 label="idle";
1348 break;
1349 case CELL:
1350 label="cell";
1351 break;
1352 case PHONE:
1353 label="phone";
1354 break;
1355 case WIFI:
1356 label="wifi";
1357 break;
1358 case BLUETOOTH:
1359 label="blue";
1360 break;
1361 case SCREEN:
1362 label="scrn";
1363 break;
1364 case APP:
1365 uid = bs.uidObj.getUid();
1366 label = "uid";
1367 break;
1368 case USER:
1369 uid = UserHandle.getUid(bs.userId, 0);
1370 label = "user";
1371 break;
1372 case UNACCOUNTED:
1373 label = "unacc";
1374 break;
1375 case OVERCOUNTED:
1376 label = "over";
1377 break;
1378 default:
1379 label = "???";
1380 }
1381 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1382 BatteryStatsHelper.makemAh(bs.value));
1383 }
1384 }
1385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 for (int iu = 0; iu < NU; iu++) {
1387 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001388 if (reqUid >= 0 && uid != reqUid) {
1389 continue;
1390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 Uid u = uidStats.valueAt(iu);
1392 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001393 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1394 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1395 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1396 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1397 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1398 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1399 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1400 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001401 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001402 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001403 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001404
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001405 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1406 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
1407 || wifiPacketsTx > 0) {
1408 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1409 wifiBytesRx, wifiBytesTx,
1410 mobilePacketsRx, mobilePacketsTx,
1411 wifiPacketsRx, wifiPacketsTx);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001412 }
1413
Nick Pelly6ccaa542012-06-15 15:22:47 -07001414 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001415 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001416 dumpLine(pw, uid, category, WIFI_DATA,
1417 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419
Dianne Hackborn617f8772009-03-31 15:04:46 -07001420 if (u.hasUserActivity()) {
1421 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1422 boolean hasData = false;
1423 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1424 int val = u.getUserActivityCount(i, which);
1425 args[i] = val;
1426 if (val != 0) hasData = true;
1427 }
1428 if (hasData) {
1429 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1430 }
1431 }
1432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1434 if (wakelocks.size() > 0) {
1435 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1436 : wakelocks.entrySet()) {
1437 Uid.Wakelock wl = ent.getValue();
1438 String linePrefix = "";
1439 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001440 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
1441 batteryRealtime, "f", which, linePrefix);
1442 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
1443 batteryRealtime, "p", which, linePrefix);
1444 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
1445 batteryRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446
1447 // Only log if we had at lease one wakelock...
1448 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001449 String name = ent.getKey();
1450 if (name.indexOf(',') >= 0) {
1451 name = name.replace(',', '_');
1452 }
1453 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
1455 }
1456 }
1457
1458 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1459 if (sensors.size() > 0) {
1460 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1461 : sensors.entrySet()) {
1462 Uid.Sensor se = ent.getValue();
1463 int sensorNumber = ent.getKey();
1464 Timer timer = se.getSensorTime();
1465 if (timer != null) {
1466 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07001467 long totalTime = (timer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1468 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 if (totalTime != 0) {
1470 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1471 }
1472 }
1473 }
1474 }
1475
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001476 Timer vibTimer = u.getVibratorOnTimer();
1477 if (vibTimer != null) {
1478 // Convert from microseconds to milliseconds with rounding
1479 long totalTime = (vibTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1480 int count = vibTimer.getCountLocked(which);
1481 if (totalTime != 0) {
1482 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1483 }
1484 }
1485
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001486 Timer fgTimer = u.getForegroundActivityTimer();
1487 if (fgTimer != null) {
1488 // Convert from microseconds to milliseconds with rounding
1489 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1490 int count = fgTimer.getCountLocked(which);
1491 if (totalTime != 0) {
1492 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1493 }
1494 }
1495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1497 if (processStats.size() > 0) {
1498 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1499 : processStats.entrySet()) {
1500 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001501
1502 final long userMillis = ps.getUserTime(which) * 10;
1503 final long systemMillis = ps.getSystemTime(which) * 10;
1504 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1505 final long starts = ps.getStarts(which);
1506
1507 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1508 || starts != 0) {
1509 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1510 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
1512 }
1513 }
1514
1515 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1516 if (packageStats.size() > 0) {
1517 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1518 : packageStats.entrySet()) {
1519
1520 Uid.Pkg ps = ent.getValue();
1521 int wakeups = ps.getWakeups(which);
1522 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1523 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1524 : serviceStats.entrySet()) {
1525 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1526 long startTime = ss.getStartTime(batteryUptime, which);
1527 int starts = ss.getStarts(which);
1528 int launches = ss.getLaunches(which);
1529 if (startTime != 0 || starts != 0 || launches != 0) {
1530 dumpLine(pw, uid, category, APK_DATA,
1531 wakeups, // wakeup alarms
1532 ent.getKey(), // Apk
1533 sent.getKey(), // service
1534 startTime / 1000, // time spent started, in ms
1535 starts,
1536 launches);
1537 }
1538 }
1539 }
1540 }
1541 }
1542 }
1543
Dianne Hackborn81038902012-11-26 17:04:09 -08001544 static final class TimerEntry {
1545 final String mName;
1546 final int mId;
1547 final BatteryStats.Timer mTimer;
1548 final long mTime;
1549 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1550 mName = name;
1551 mId = id;
1552 mTimer = timer;
1553 mTime = time;
1554 }
1555 }
1556
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001557 private void printmAh(PrintWriter printer, double power) {
1558 printer.print(BatteryStatsHelper.makemAh(power));
1559 }
1560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001562 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
1563 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1565 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1566 final long batteryUptime = getBatteryUptime(rawUptime);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001567 final long batteryRealtime = getBatteryRealtime(rawRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568
1569 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1570 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1571 final long totalRealtime = computeRealtime(rawRealtime, which);
1572 final long totalUptime = computeUptime(rawUptime, which);
1573
1574 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001575
1576 SparseArray<? extends Uid> uidStats = getUidStats();
1577 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001579 sb.setLength(0);
1580 sb.append(prefix);
1581 sb.append(" Time on battery: ");
1582 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1583 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1584 sb.append(") realtime, ");
1585 formatTimeMs(sb, whichBatteryUptime / 1000);
1586 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1587 sb.append(") uptime");
1588 pw.println(sb.toString());
1589 sb.setLength(0);
1590 sb.append(prefix);
1591 sb.append(" Total run time: ");
1592 formatTimeMs(sb, totalRealtime / 1000);
1593 sb.append("realtime, ");
1594 formatTimeMs(sb, totalUptime / 1000);
1595 sb.append("uptime, ");
1596 pw.println(sb.toString());
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001597 pw.print(" Start clock time: ");
1598 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
1599
The Android Open Source Project10592532009-03-18 17:39:46 -07001600 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1601 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001602 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001603 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
1604 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001605 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001606 sb.append(prefix);
1607 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
1608 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
1609 sb.append("), Input events: "); sb.append(getInputEventCount(which));
1610 sb.append(", Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
1611 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
1612 sb.append(")");
1613 pw.println(sb.toString());
1614 sb.setLength(0);
1615 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001616 sb.append(" Screen brightnesses: ");
1617 boolean didOne = false;
1618 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1619 final long time = getScreenBrightnessTime(i, batteryRealtime, which);
1620 if (time == 0) {
1621 continue;
1622 }
1623 if (didOne) sb.append(", ");
1624 didOne = true;
1625 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
1626 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001627 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001628 sb.append("(");
1629 sb.append(formatRatioLocked(time, screenOnTime));
1630 sb.append(")");
1631 }
1632 if (!didOne) sb.append("No activity");
1633 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07001634
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001635 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001636 long fullWakeLockTimeTotalMicros = 0;
1637 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08001638
1639 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
1640 @Override
1641 public int compare(TimerEntry lhs, TimerEntry rhs) {
1642 long lhsTime = lhs.mTime;
1643 long rhsTime = rhs.mTime;
1644 if (lhsTime < rhsTime) {
1645 return 1;
1646 }
1647 if (lhsTime > rhsTime) {
1648 return -1;
1649 }
1650 return 0;
1651 }
1652 };
1653
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001654 if (reqUid < 0) {
1655 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1656 if (kernelWakelocks.size() > 0) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001657 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001658 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001659 BatteryStats.Timer timer = ent.getValue();
1660 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
1661 if (totalTimeMillis > 0) {
1662 timers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
1663 }
1664 }
1665 Collections.sort(timers, timerComparator);
1666 for (int i=0; i<timers.size(); i++) {
1667 TimerEntry timer = timers.get(i);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001668 String linePrefix = ": ";
1669 sb.setLength(0);
1670 sb.append(prefix);
1671 sb.append(" Kernel Wake lock ");
Dianne Hackborn81038902012-11-26 17:04:09 -08001672 sb.append(timer.mName);
1673 linePrefix = printWakeLock(sb, timer.mTimer, batteryRealtime, null,
1674 which, linePrefix);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001675 if (!linePrefix.equals(": ")) {
1676 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05001677 // Only print out wake locks that were held
1678 pw.println(sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001679 }
Evan Millarc64edde2009-04-18 12:26:32 -07001680 }
Evan Millarc64edde2009-04-18 12:26:32 -07001681 }
1682 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001683
1684 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1685
Evan Millar22ac0432009-03-31 11:33:18 -07001686 for (int iu = 0; iu < NU; iu++) {
1687 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001688
Evan Millar22ac0432009-03-31 11:33:18 -07001689 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1690 if (wakelocks.size() > 0) {
1691 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1692 : wakelocks.entrySet()) {
1693 Uid.Wakelock wl = ent.getValue();
1694
1695 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1696 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001697 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001698 batteryRealtime, which);
1699 }
1700
1701 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1702 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001703 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001704 batteryRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08001705 if (totalTimeMicros > 0) {
1706 if (reqUid < 0) {
1707 // Only show the ordered list of all wake
1708 // locks if the caller is not asking for data
1709 // about a specific uid.
1710 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
1711 partialWakeTimer, totalTimeMicros));
1712 }
1713 partialWakeLockTimeTotalMicros += totalTimeMicros;
1714 }
Evan Millar22ac0432009-03-31 11:33:18 -07001715 }
1716 }
1717 }
1718 }
1719
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001720 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1721 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1722 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1723 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1724 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1725 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1726 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1727 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1728
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001729 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001730 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
1731 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
1732 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
1733 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001734 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001735 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
1736 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
1737 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
1738 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001739 sb.setLength(0);
1740 sb.append(prefix);
1741 sb.append(" Total full wakelock time: "); formatTimeMs(sb,
1742 (fullWakeLockTimeTotalMicros + 500) / 1000);
Dianne Hackborn81038902012-11-26 17:04:09 -08001743 sb.append(", Total partial wakelock time: "); formatTimeMs(sb,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001744 (partialWakeLockTimeTotalMicros + 500) / 1000);
1745 pw.println(sb.toString());
Evan Millar22ac0432009-03-31 11:33:18 -07001746
Dianne Hackborn627bba72009-03-24 22:32:56 -07001747 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001748 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001749 sb.append(" Signal levels: ");
1750 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08001751 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001752 final long time = getPhoneSignalStrengthTime(i, batteryRealtime, which);
1753 if (time == 0) {
1754 continue;
1755 }
1756 if (didOne) sb.append(", ");
1757 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08001758 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001759 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001760 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001761 sb.append("(");
1762 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001763 sb.append(") ");
1764 sb.append(getPhoneSignalStrengthCount(i, which));
1765 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001766 }
1767 if (!didOne) sb.append("No activity");
1768 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07001769
1770 sb.setLength(0);
1771 sb.append(prefix);
1772 sb.append(" Signal scanning time: ");
1773 formatTimeMs(sb, getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
1774 pw.println(sb.toString());
1775
Dianne Hackborn627bba72009-03-24 22:32:56 -07001776 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001777 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001778 sb.append(" Radio types: ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001779 didOne = false;
1780 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1781 final long time = getPhoneDataConnectionTime(i, batteryRealtime, which);
1782 if (time == 0) {
1783 continue;
1784 }
1785 if (didOne) sb.append(", ");
1786 didOne = true;
1787 sb.append(DATA_CONNECTION_NAMES[i]);
1788 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001789 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001790 sb.append("(");
1791 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001792 sb.append(") ");
1793 sb.append(getPhoneDataConnectionCount(i, which));
1794 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001795 }
1796 if (!didOne) sb.append("No activity");
1797 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001798
1799 sb.setLength(0);
1800 sb.append(prefix);
1801 sb.append(" Radio data uptime when unplugged: ");
1802 sb.append(getRadioDataUptime() / 1000);
1803 sb.append(" ms");
1804 pw.println(sb.toString());
1805
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001806 sb.setLength(0);
1807 sb.append(prefix);
1808 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
1809 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
1810 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
1811 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
1812 sb.append("), Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
1813 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
1814 sb.append(")");
1815 pw.println(sb.toString());
Dianne Hackborn617f8772009-03-31 15:04:46 -07001816
The Android Open Source Project10592532009-03-18 17:39:46 -07001817 pw.println(" ");
1818
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001819 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001820 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001821 pw.print(prefix); pw.println(" Device is currently unplugged");
1822 pw.print(prefix); pw.print(" Discharge cycle start level: ");
1823 pw.println(getDischargeStartLevel());
1824 pw.print(prefix); pw.print(" Discharge cycle current level: ");
1825 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07001826 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001827 pw.print(prefix); pw.println(" Device is currently plugged into power");
1828 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
1829 pw.println(getDischargeStartLevel());
1830 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
1831 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001832 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001833 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1834 pw.println(getDischargeAmountScreenOn());
1835 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1836 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07001837 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001838 } else {
1839 pw.print(prefix); pw.println(" Device battery use since last full charge");
1840 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
1841 pw.println(getLowDischargeAmountSinceCharge());
1842 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
1843 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001844 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
1845 pw.println(getDischargeAmountScreenOnSinceCharge());
1846 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
1847 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08001848 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07001849 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001850
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001851 BatteryStatsHelper helper = new BatteryStatsHelper(context);
1852 helper.create(this);
1853 helper.refreshStats(which, UserHandle.USER_ALL);
1854 List<BatterySipper> sippers = helper.getUsageList();
1855 if (sippers != null && sippers.size() > 0) {
1856 pw.print(prefix); pw.println(" Estimated power use (mAh):");
1857 pw.print(prefix); pw.print(" Capacity: ");
1858 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08001859 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001860 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
1861 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
1862 pw.println();
1863 for (int i=0; i<sippers.size(); i++) {
1864 BatterySipper bs = sippers.get(i);
1865 switch (bs.drainType) {
1866 case IDLE:
1867 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
1868 pw.println();
1869 break;
1870 case CELL:
1871 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
1872 pw.println();
1873 break;
1874 case PHONE:
1875 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
1876 pw.println();
1877 break;
1878 case WIFI:
1879 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
1880 pw.println();
1881 break;
1882 case BLUETOOTH:
1883 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
1884 pw.println();
1885 break;
1886 case SCREEN:
1887 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
1888 pw.println();
1889 break;
1890 case APP:
1891 pw.print(prefix); pw.print(" Uid "); pw.print(bs.uidObj.getUid());
1892 pw.print(": "); printmAh(pw, bs.value); pw.println();
1893 break;
1894 case USER:
1895 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
1896 pw.print(": "); printmAh(pw, bs.value); pw.println();
1897 break;
1898 case UNACCOUNTED:
1899 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
1900 pw.println();
1901 break;
1902 case OVERCOUNTED:
1903 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
1904 pw.println();
1905 break;
1906 }
1907 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08001908 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001909 }
1910
Dianne Hackborn81038902012-11-26 17:04:09 -08001911 if (timers.size() > 0) {
1912 Collections.sort(timers, timerComparator);
1913 pw.print(prefix); pw.println(" All partial wake locks:");
1914 for (int i=0; i<timers.size(); i++) {
1915 TimerEntry timer = timers.get(i);
1916 sb.setLength(0);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07001917 sb.append(" Wake lock ");
1918 UserHandle.formatUid(sb, timer.mId);
Dianne Hackborn81038902012-11-26 17:04:09 -08001919 sb.append(" ");
1920 sb.append(timer.mName);
1921 printWakeLock(sb, timer.mTimer, batteryRealtime, null, which, ": ");
1922 sb.append(" realtime");
1923 pw.println(sb.toString());
1924 }
1925 timers.clear();
1926 pw.println();
1927 }
Evan Millar22ac0432009-03-31 11:33:18 -07001928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 for (int iu=0; iu<NU; iu++) {
1930 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08001931 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001932 continue;
1933 }
1934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07001936
1937 pw.print(prefix);
1938 pw.print(" ");
1939 UserHandle.formatUid(pw, uid);
1940 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 boolean uidActivity = false;
1942
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001943 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1944 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1945 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1946 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1947 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1948 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1949 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1950 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001951 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001952 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001953 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001954
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001955 if (mobileRxBytes > 0 || mobileTxBytes > 0
1956 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001957 pw.print(prefix); pw.print(" Mobile network: ");
1958 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001959 pw.print(formatBytesLocked(mobileTxBytes));
1960 pw.print(" sent (packets "); pw.print(mobileRxPackets);
1961 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001963 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001964 pw.print(prefix); pw.print(" Wi-Fi network: ");
1965 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001966 pw.print(formatBytesLocked(wifiTxBytes));
1967 pw.print(" sent (packets "); pw.print(wifiRxPackets);
1968 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001969 }
1970
Dianne Hackborn617f8772009-03-31 15:04:46 -07001971 if (u.hasUserActivity()) {
1972 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07001973 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001974 int val = u.getUserActivityCount(i, which);
1975 if (val != 0) {
1976 if (!hasData) {
1977 sb.setLength(0);
1978 sb.append(" User activity: ");
1979 hasData = true;
1980 } else {
1981 sb.append(", ");
1982 }
1983 sb.append(val);
1984 sb.append(" ");
1985 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
1986 }
1987 }
1988 if (hasData) {
1989 pw.println(sb.toString());
1990 }
1991 }
1992
Nick Pelly6ccaa542012-06-15 15:22:47 -07001993 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001994 || uidWifiRunningTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001995 sb.setLength(0);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001996 sb.append(prefix); sb.append(" Wifi Running: ");
1997 formatTimeMs(sb, uidWifiRunningTime / 1000);
1998 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001999 whichBatteryRealtime)); sb.append(")\n");
2000 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Nick Pelly6ccaa542012-06-15 15:22:47 -07002001 formatTimeMs(sb, fullWifiLockOnTime / 1000);
2002 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002003 whichBatteryRealtime)); sb.append(")\n");
Nick Pelly6ccaa542012-06-15 15:22:47 -07002004 sb.append(prefix); sb.append(" Wifi Scan: ");
2005 formatTimeMs(sb, wifiScanTime / 1000);
2006 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002007 whichBatteryRealtime)); sb.append(")");
2008 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07002009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010
2011 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2012 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002013 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2014 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2016 : wakelocks.entrySet()) {
2017 Uid.Wakelock wl = ent.getValue();
2018 String linePrefix = ": ";
2019 sb.setLength(0);
2020 sb.append(prefix);
2021 sb.append(" Wake lock ");
2022 sb.append(ent.getKey());
2023 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), batteryRealtime,
2024 "full", which, linePrefix);
2025 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), batteryRealtime,
2026 "partial", which, linePrefix);
2027 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), batteryRealtime,
2028 "window", which, linePrefix);
2029 if (!linePrefix.equals(": ")) {
2030 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002031 // Only print out wake locks that were held
2032 pw.println(sb.toString());
2033 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002034 count++;
2035 }
2036 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
2037 batteryRealtime, which);
2038 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
2039 batteryRealtime, which);
2040 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
2041 batteryRealtime, which);
2042 }
2043 if (count > 1) {
2044 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2045 sb.setLength(0);
2046 sb.append(prefix);
2047 sb.append(" TOTAL wake: ");
2048 boolean needComma = false;
2049 if (totalFull != 0) {
2050 needComma = true;
2051 formatTimeMs(sb, totalFull);
2052 sb.append("full");
2053 }
2054 if (totalPartial != 0) {
2055 if (needComma) {
2056 sb.append(", ");
2057 }
2058 needComma = true;
2059 formatTimeMs(sb, totalPartial);
2060 sb.append("partial");
2061 }
2062 if (totalWindow != 0) {
2063 if (needComma) {
2064 sb.append(", ");
2065 }
2066 needComma = true;
2067 formatTimeMs(sb, totalWindow);
2068 sb.append("window");
2069 }
2070 sb.append(" realtime");
2071 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 }
2074 }
2075
2076 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2077 if (sensors.size() > 0) {
2078 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2079 : sensors.entrySet()) {
2080 Uid.Sensor se = ent.getValue();
2081 int sensorNumber = ent.getKey();
2082 sb.setLength(0);
2083 sb.append(prefix);
2084 sb.append(" Sensor ");
2085 int handle = se.getHandle();
2086 if (handle == Uid.Sensor.GPS) {
2087 sb.append("GPS");
2088 } else {
2089 sb.append(handle);
2090 }
2091 sb.append(": ");
2092
2093 Timer timer = se.getSensorTime();
2094 if (timer != null) {
2095 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002096 long totalTime = (timer.getTotalTimeLocked(
2097 batteryRealtime, which) + 500) / 1000;
2098 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 //timer.logState();
2100 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002101 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 sb.append("realtime (");
2103 sb.append(count);
2104 sb.append(" times)");
2105 } else {
2106 sb.append("(not used)");
2107 }
2108 } else {
2109 sb.append("(not used)");
2110 }
2111
2112 pw.println(sb.toString());
2113 uidActivity = true;
2114 }
2115 }
2116
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002117 Timer vibTimer = u.getVibratorOnTimer();
2118 if (vibTimer != null) {
2119 // Convert from microseconds to milliseconds with rounding
2120 long totalTime = (vibTimer.getTotalTimeLocked(
2121 batteryRealtime, which) + 500) / 1000;
2122 int count = vibTimer.getCountLocked(which);
2123 //timer.logState();
2124 if (totalTime != 0) {
2125 sb.setLength(0);
2126 sb.append(prefix);
2127 sb.append(" Vibrator: ");
2128 formatTimeMs(sb, totalTime);
2129 sb.append("realtime (");
2130 sb.append(count);
2131 sb.append(" times)");
2132 pw.println(sb.toString());
2133 uidActivity = true;
2134 }
2135 }
2136
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002137 Timer fgTimer = u.getForegroundActivityTimer();
2138 if (fgTimer != null) {
2139 // Convert from microseconds to milliseconds with rounding
2140 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
2141 int count = fgTimer.getCountLocked(which);
2142 if (totalTime != 0) {
2143 sb.setLength(0);
2144 sb.append(prefix);
2145 sb.append(" Foreground activities: ");
2146 formatTimeMs(sb, totalTime);
2147 sb.append("realtime (");
2148 sb.append(count);
2149 sb.append(" times)");
2150 pw.println(sb.toString());
2151 uidActivity = true;
2152 }
2153 }
2154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2156 if (processStats.size() > 0) {
2157 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2158 : processStats.entrySet()) {
2159 Uid.Proc ps = ent.getValue();
2160 long userTime;
2161 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002162 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002164 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165
2166 userTime = ps.getUserTime(which);
2167 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002168 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002170 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002171 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002173 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002174 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002175 sb.setLength(0);
2176 sb.append(prefix); sb.append(" Proc ");
2177 sb.append(ent.getKey()); sb.append(":\n");
2178 sb.append(prefix); sb.append(" CPU: ");
2179 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002180 formatTime(sb, systemTime); sb.append("krn ; ");
2181 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002182 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002183 sb.append("\n"); sb.append(prefix); sb.append(" ");
2184 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002185 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002186 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002187 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002188 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002189 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002190 pw.print(prefix); pw.print(" * Killed for ");
2191 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2192 pw.print("wake lock");
2193 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2194 pw.print("cpu");
2195 } else {
2196 pw.print("unknown");
2197 }
2198 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002199 TimeUtils.formatDuration(ew.usedTime, pw);
2200 pw.print(" over ");
2201 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002202 if (ew.overTime != 0) {
2203 pw.print(" (");
2204 pw.print((ew.usedTime*100)/ew.overTime);
2205 pw.println("%)");
2206 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002207 }
2208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 uidActivity = true;
2210 }
2211 }
2212 }
2213
2214 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2215 if (packageStats.size() > 0) {
2216 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2217 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002218 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 boolean apkActivity = false;
2220 Uid.Pkg ps = ent.getValue();
2221 int wakeups = ps.getWakeups(which);
2222 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002223 pw.print(prefix); pw.print(" ");
2224 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 apkActivity = true;
2226 }
2227 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2228 if (serviceStats.size() > 0) {
2229 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2230 : serviceStats.entrySet()) {
2231 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2232 long startTime = ss.getStartTime(batteryUptime, which);
2233 int starts = ss.getStarts(which);
2234 int launches = ss.getLaunches(which);
2235 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002236 sb.setLength(0);
2237 sb.append(prefix); sb.append(" Service ");
2238 sb.append(sent.getKey()); sb.append(":\n");
2239 sb.append(prefix); sb.append(" Created for: ");
2240 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002241 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002242 sb.append(prefix); sb.append(" Starts: ");
2243 sb.append(starts);
2244 sb.append(", launches: "); sb.append(launches);
2245 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 apkActivity = true;
2247 }
2248 }
2249 }
2250 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002251 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 }
2253 uidActivity = true;
2254 }
2255 }
2256 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002257 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 }
2259 }
2260 }
2261
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002262 static void printBitDescriptions(PrintWriter pw, int oldval, int newval,
2263 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002264 int diff = oldval ^ newval;
2265 if (diff == 0) return;
2266 for (int i=0; i<descriptions.length; i++) {
2267 BitDescription bd = descriptions[i];
Dianne Hackborn099bc622014-01-22 13:39:16 -08002268 int mask = bd.mask;
2269 if (bd.shift > 0) {
2270 mask <<= bd.shift;
2271 }
2272 if ((diff&mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002273 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002274 if (bd.shift < 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002275 pw.print((newval&mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002276 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002277 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002278 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002279 pw.print("=");
2280 int val = (newval&bd.mask)>>bd.shift;
2281 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002282 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002283 } else {
2284 pw.print(val);
2285 }
2286 }
2287 }
2288 }
2289 }
2290
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002291 public void prepareForDumpLocked() {
2292 }
2293
2294 public static class HistoryPrinter {
2295 int oldState = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002296 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002297 int oldStatus = -1;
2298 int oldHealth = -1;
2299 int oldPlug = -1;
2300 int oldTemp = -1;
2301 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002302 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002303
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002304 public void printNextItem(PrintWriter pw, HistoryItem rec, long now, boolean checkin) {
2305 if (!checkin) {
2306 pw.print(" ");
2307 TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002308 pw.print(" ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002309 } else {
2310 if (lastTime < 0) {
2311 pw.print("@");
2312 pw.print(rec.time-now);
2313 } else {
2314 pw.print(rec.time-lastTime);
2315 }
2316 lastTime = rec.time;
2317 }
2318 if (rec.cmd == HistoryItem.CMD_START) {
2319 if (checkin) {
2320 pw.print(":");
2321 }
2322 pw.println("START");
2323 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2324 if (checkin) {
2325 pw.print(":");
2326 }
2327 pw.println("*OVERFLOW*");
2328 } else {
2329 if (!checkin) {
2330 if (rec.batteryLevel < 10) pw.print("00");
2331 else if (rec.batteryLevel < 100) pw.print("0");
2332 pw.print(rec.batteryLevel);
2333 pw.print(" ");
2334 if (rec.states < 0x10) pw.print("0000000");
2335 else if (rec.states < 0x100) pw.print("000000");
2336 else if (rec.states < 0x1000) pw.print("00000");
2337 else if (rec.states < 0x10000) pw.print("0000");
2338 else if (rec.states < 0x100000) pw.print("000");
2339 else if (rec.states < 0x1000000) pw.print("00");
2340 else if (rec.states < 0x10000000) pw.print("0");
2341 pw.print(Integer.toHexString(rec.states));
2342 } else {
2343 if (oldLevel != rec.batteryLevel) {
2344 oldLevel = rec.batteryLevel;
2345 pw.print(",Bl="); pw.print(rec.batteryLevel);
2346 }
2347 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002348 if (oldStatus != rec.batteryStatus) {
2349 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002350 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002351 switch (oldStatus) {
2352 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002353 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002354 break;
2355 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002356 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002357 break;
2358 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002359 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002360 break;
2361 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002362 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002363 break;
2364 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002365 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002366 break;
2367 default:
2368 pw.print(oldStatus);
2369 break;
2370 }
2371 }
2372 if (oldHealth != rec.batteryHealth) {
2373 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002374 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002375 switch (oldHealth) {
2376 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002377 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002378 break;
2379 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002380 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002381 break;
2382 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002383 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002384 break;
2385 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002386 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002387 break;
2388 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002389 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002390 break;
2391 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002392 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002393 break;
2394 default:
2395 pw.print(oldHealth);
2396 break;
2397 }
2398 }
2399 if (oldPlug != rec.batteryPlugType) {
2400 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002401 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002402 switch (oldPlug) {
2403 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002404 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002405 break;
2406 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002407 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002408 break;
2409 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002410 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002411 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002412 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002413 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002414 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002415 default:
2416 pw.print(oldPlug);
2417 break;
2418 }
2419 }
2420 if (oldTemp != rec.batteryTemperature) {
2421 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002422 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002423 pw.print(oldTemp);
2424 }
2425 if (oldVolt != rec.batteryVoltage) {
2426 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002427 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002428 pw.print(oldVolt);
2429 }
2430 printBitDescriptions(pw, oldState, rec.states,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002431 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002432 if (rec.eventCode != HistoryItem.EVENT_NONE) {
2433 switch (rec.eventCode) {
2434 case HistoryItem.EVENT_PROC_STARTED:
2435 pw.print(checkin ? ",PrSt=" : " procstart=");
2436 break;
2437 case HistoryItem.EVENT_PROC_FINISHED:
2438 pw.print(checkin ? ",PrFn=" : " procfin=");
2439 break;
2440 default:
2441 if (checkin) {
2442 pw.print(",?cmd_");
2443 pw.print(rec.eventCode);
2444 pw.print("=");
2445 } else {
2446 pw.print(" cmd_");
2447 pw.print(rec.eventCode);
2448 pw.print("=");
2449 }
2450 break;
2451 }
2452 if (checkin) {
2453 pw.print(rec.eventUid);
2454 } else {
2455 UserHandle.formatUid(pw, rec.eventUid);
2456 }
2457 pw.print(":");
2458 if (checkin) {
2459 pw.print(rec.eventNameIdx);
2460 } else {
2461 pw.print(rec.eventName);
2462 }
2463 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002464 pw.println();
2465 }
2466 oldState = rec.states;
2467 }
2468 }
2469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 /**
2471 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
2472 *
2473 * @param pw a Printer to receive the dump output.
2474 */
2475 @SuppressWarnings("unused")
Dianne Hackborn099bc622014-01-22 13:39:16 -08002476 public void dumpLocked(Context context, PrintWriter pw, boolean isUnpluggedOnly, int reqUid,
2477 boolean historyOnly) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002478 prepareForDumpLocked();
2479
2480 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2481
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002482 final HistoryItem rec = new HistoryItem();
2483 if (startIteratingHistoryLocked()) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002484 pw.println("Battery History:");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002485 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002486 while (getNextHistoryLocked(rec)) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002487 hprinter.printNextItem(pw, rec, now, false);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002488 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002489 finishIteratingHistoryLocked();
2490 pw.println("");
2491 }
2492
2493 if (startIteratingOldHistoryLocked()) {
2494 pw.println("Old battery History:");
2495 HistoryPrinter hprinter = new HistoryPrinter();
2496 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002497 hprinter.printNextItem(pw, rec, now, false);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002498 }
2499 finishIteratingOldHistoryLocked();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002500 pw.println("");
2501 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08002502
2503 if (historyOnly) {
2504 return;
2505 }
2506
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002507 SparseArray<? extends Uid> uidStats = getUidStats();
2508 final int NU = uidStats.size();
2509 boolean didPid = false;
2510 long nowRealtime = SystemClock.elapsedRealtime();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002511 for (int i=0; i<NU; i++) {
2512 Uid uid = uidStats.valueAt(i);
2513 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
2514 if (pids != null) {
2515 for (int j=0; j<pids.size(); j++) {
2516 Uid.Pid pid = pids.valueAt(j);
2517 if (!didPid) {
2518 pw.println("Per-PID Stats:");
2519 didPid = true;
2520 }
2521 long time = pid.mWakeSum + (pid.mWakeStart != 0
2522 ? (nowRealtime - pid.mWakeStart) : 0);
2523 pw.print(" PID "); pw.print(pids.keyAt(j));
2524 pw.print(" wake time: ");
2525 TimeUtils.formatDuration(time, pw);
2526 pw.println("");
2527 }
2528 }
2529 }
2530 if (didPid) {
2531 pw.println("");
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002532 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002533
2534 if (!isUnpluggedOnly) {
2535 pw.println("Statistics since last charge:");
2536 pw.println(" System starts: " + getStartCount()
2537 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002538 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002539 pw.println("");
2540 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002541 pw.println("Statistics since last unplugged:");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002542 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 }
2544
2545 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002546 public void dumpCheckinLocked(Context context,
Dianne Hackborn49021f52013-09-04 18:03:40 -07002547 PrintWriter pw, List<ApplicationInfo> apps, boolean isUnpluggedOnly,
Dianne Hackborn099bc622014-01-22 13:39:16 -08002548 boolean includeHistory, boolean historyOnly) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002549 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002551 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2552
Dianne Hackborn099bc622014-01-22 13:39:16 -08002553 if (includeHistory || historyOnly) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07002554 final HistoryItem rec = new HistoryItem();
2555 if (startIteratingHistoryLocked()) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002556 for (int i=0; i<getHistoryStringPoolSize(); i++) {
2557 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
2558 pw.print(HISTORY_STRING_POOL); pw.print(',');
2559 pw.print(i);
2560 pw.print(',');
2561 pw.print(getHistoryStringPoolItem(i));
2562 pw.println();
2563 }
Dianne Hackborn49021f52013-09-04 18:03:40 -07002564 HistoryPrinter hprinter = new HistoryPrinter();
2565 while (getNextHistoryLocked(rec)) {
2566 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
Dianne Hackborn49021f52013-09-04 18:03:40 -07002567 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002568 hprinter.printNextItem(pw, rec, now, true);
Dianne Hackborn49021f52013-09-04 18:03:40 -07002569 }
2570 finishIteratingHistoryLocked();
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002571 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002572 }
2573
Dianne Hackborn099bc622014-01-22 13:39:16 -08002574 if (historyOnly) {
2575 return;
2576 }
2577
Dianne Hackborne4a59512010-12-07 11:08:07 -08002578 if (apps != null) {
2579 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
2580 for (int i=0; i<apps.size(); i++) {
2581 ApplicationInfo ai = apps.get(i);
2582 ArrayList<String> pkgs = uids.get(ai.uid);
2583 if (pkgs == null) {
2584 pkgs = new ArrayList<String>();
2585 uids.put(ai.uid, pkgs);
2586 }
2587 pkgs.add(ai.packageName);
2588 }
2589 SparseArray<? extends Uid> uidStats = getUidStats();
2590 final int NU = uidStats.size();
2591 String[] lineArgs = new String[2];
2592 for (int i=0; i<NU; i++) {
2593 int uid = uidStats.keyAt(i);
2594 ArrayList<String> pkgs = uids.get(uid);
2595 if (pkgs != null) {
2596 for (int j=0; j<pkgs.size(); j++) {
2597 lineArgs[0] = Integer.toString(uid);
2598 lineArgs[1] = pkgs.get(j);
2599 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
2600 (Object[])lineArgs);
2601 }
2602 }
2603 }
2604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 if (isUnpluggedOnly) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002606 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 }
2608 else {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002609 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
2610 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 }
2612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613}