blob: 87beb95479c5f9efe4f228c145f3056024237ebe [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";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700158 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700159 private static final String NETWORK_DATA = "nt";
160 private static final String USER_ACTIVITY_DATA = "ua";
161 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800162 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700163 private static final String BATTERY_LEVEL_DATA = "lv";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700164 private static final String WIFI_DATA = "wfl";
Evan Millare84de8d2009-04-02 22:16:12 -0700165 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800166 private static final String GLOBAL_NETWORK_DATA = "gn";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800167 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700168 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700169 private static final String SCREEN_BRIGHTNESS_DATA = "br";
170 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700171 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700172 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
173 private static final String DATA_CONNECTION_TIME_DATA = "dct";
174 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800175 private static final String WIFI_STATE_TIME_DATA = "wst";
176 private static final String WIFI_STATE_COUNT_DATA = "wsc";
177 private static final String BLUETOOTH_STATE_TIME_DATA = "bst";
178 private static final String BLUETOOTH_STATE_COUNT_DATA = "bsc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800179 private static final String POWER_USE_SUMMARY_DATA = "pws";
180 private static final String POWER_USE_ITEM_DATA = "pwi";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700182 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 private final Formatter mFormatter = new Formatter(mFormatBuilder);
184
185 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700186 * State for keeping track of counting information.
187 */
188 public static abstract class Counter {
189
190 /**
191 * Returns the count associated with this Counter for the
192 * selected type of statistics.
193 *
194 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
195 */
Evan Millarc64edde2009-04-18 12:26:32 -0700196 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700197
198 /**
199 * Temporary for debugging.
200 */
201 public abstract void logState(Printer pw, String prefix);
202 }
203
204 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700205 * State for keeping track of long counting information.
206 */
207 public static abstract class LongCounter {
208
209 /**
210 * Returns the count associated with this Counter for the
211 * selected type of statistics.
212 *
213 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
214 */
215 public abstract long getCountLocked(int which);
216
217 /**
218 * Temporary for debugging.
219 */
220 public abstract void logState(Printer pw, String prefix);
221 }
222
223 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * State for keeping track of timing information.
225 */
226 public static abstract class Timer {
227
228 /**
229 * Returns the count associated with this Timer for the
230 * selected type of statistics.
231 *
232 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
233 */
Evan Millarc64edde2009-04-18 12:26:32 -0700234 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235
236 /**
237 * Returns the total time in microseconds associated with this Timer for the
238 * selected type of statistics.
239 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800240 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
242 * @return a time in microseconds
243 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800244 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 /**
247 * Temporary for debugging.
248 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700249 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 }
251
252 /**
253 * The statistics associated with a particular uid.
254 */
255 public static abstract class Uid {
256
257 /**
258 * Returns a mapping containing wakelock statistics.
259 *
260 * @return a Map from Strings to Uid.Wakelock objects.
261 */
262 public abstract Map<String, ? extends Wakelock> getWakelockStats();
263
264 /**
265 * The statistics associated with a particular wake lock.
266 */
267 public static abstract class Wakelock {
268 public abstract Timer getWakeTime(int type);
269 }
270
271 /**
272 * Returns a mapping containing sensor statistics.
273 *
274 * @return a Map from Integer sensor ids to Uid.Sensor objects.
275 */
276 public abstract Map<Integer, ? extends Sensor> getSensorStats();
277
278 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700279 * Returns a mapping containing active process data.
280 */
281 public abstract SparseArray<? extends Pid> getPidStats();
282
283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * Returns a mapping containing process statistics.
285 *
286 * @return a Map from Strings to Uid.Proc objects.
287 */
288 public abstract Map<String, ? extends Proc> getProcessStats();
289
290 /**
291 * Returns a mapping containing package statistics.
292 *
293 * @return a Map from Strings to Uid.Pkg objects.
294 */
295 public abstract Map<String, ? extends Pkg> getPackageStats();
296
297 /**
298 * {@hide}
299 */
300 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700301
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800302 public abstract void noteWifiRunningLocked(long elapsedRealtime);
303 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
304 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
305 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
306 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
307 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
308 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
309 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
310 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
311 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
312 public abstract void noteAudioTurnedOnLocked(long elapsedRealtime);
313 public abstract void noteAudioTurnedOffLocked(long elapsedRealtime);
314 public abstract void noteVideoTurnedOnLocked(long elapsedRealtime);
315 public abstract void noteVideoTurnedOffLocked(long elapsedRealtime);
316 public abstract void noteActivityResumedLocked(long elapsedRealtime);
317 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800318 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
319 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
320 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
321 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
322 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
323 public abstract long getAudioTurnedOnTime(long elapsedRealtimeUs, int which);
324 public abstract long getVideoTurnedOnTime(long elapsedRealtimeUs, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700325 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800326 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
Robert Greenwalta029ea12013-09-25 16:38:12 -0700328 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
329
Dianne Hackborn617f8772009-03-31 15:04:46 -0700330 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700331 * Note that these must match the constants in android.os.PowerManager.
332 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
333 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700334 */
335 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700336 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700337 };
338
Jeff Browndf693de2012-07-27 12:03:38 -0700339 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700340
Dianne Hackborn617f8772009-03-31 15:04:46 -0700341 public abstract void noteUserActivityLocked(int type);
342 public abstract boolean hasUserActivity();
343 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700344
345 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800346 public abstract long getNetworkActivityBytes(int type, int which);
347 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800348 public abstract long getMobileRadioActiveTime(int which);
349 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800352 /*
353 * FIXME: it's not correct to use this magic value because it
354 * could clash with a sensor handle (which are defined by
355 * the sensor HAL, and therefore out of our control
356 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 // Magic sensor number for the GPS.
358 public static final int GPS = -10000;
359
360 public abstract int getHandle();
361
362 public abstract Timer getSensorTime();
363 }
364
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700365 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800366 public int mWakeNesting;
367 public long mWakeSumMs;
368 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700369 }
370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 /**
372 * The statistics associated with a particular process.
373 */
374 public static abstract class Proc {
375
Dianne Hackborn287952c2010-09-22 22:34:31 -0700376 public static class ExcessivePower {
377 public static final int TYPE_WAKE = 1;
378 public static final int TYPE_CPU = 2;
379
380 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700381 public long overTime;
382 public long usedTime;
383 }
384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800386 * Returns true if this process is still active in the battery stats.
387 */
388 public abstract boolean isActive();
389
390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 * Returns the total time (in 1/100 sec) spent executing in user code.
392 *
393 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
394 */
395 public abstract long getUserTime(int which);
396
397 /**
398 * Returns the total time (in 1/100 sec) spent executing in system code.
399 *
400 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
401 */
402 public abstract long getSystemTime(int which);
403
404 /**
405 * Returns the number of times the process has been started.
406 *
407 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
408 */
409 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700410
411 /**
412 * Returns the cpu time spent in microseconds while the process was in the foreground.
413 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
414 * @return foreground cpu time in microseconds
415 */
416 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700417
418 /**
419 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
420 * @param speedStep the index of the CPU speed. This is not the actual speed of the
421 * CPU.
422 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
423 * @see BatteryStats#getCpuSpeedSteps()
424 */
425 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700426
Dianne Hackborn287952c2010-09-22 22:34:31 -0700427 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700428
Dianne Hackborn287952c2010-09-22 22:34:31 -0700429 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 }
431
432 /**
433 * The statistics associated with a particular package.
434 */
435 public static abstract class Pkg {
436
437 /**
438 * Returns the number of times this package has done something that could wake up the
439 * device from sleep.
440 *
441 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
442 */
443 public abstract int getWakeups(int which);
444
445 /**
446 * Returns a mapping containing service statistics.
447 */
448 public abstract Map<String, ? extends Serv> getServiceStats();
449
450 /**
451 * The statistics associated with a particular service.
452 */
453 public abstract class Serv {
454
455 /**
456 * Returns the amount of time spent started.
457 *
458 * @param batteryUptime elapsed uptime on battery in microseconds.
459 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
460 * @return
461 */
462 public abstract long getStartTime(long batteryUptime, int which);
463
464 /**
465 * Returns the total number of times startService() has been called.
466 *
467 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
468 */
469 public abstract int getStarts(int which);
470
471 /**
472 * Returns the total number times the service has been launched.
473 *
474 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
475 */
476 public abstract int getLaunches(int which);
477 }
478 }
479 }
480
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800481 public final static class HistoryTag {
482 public String string;
483 public int uid;
484
485 public int poolIdx;
486
487 public void setTo(HistoryTag o) {
488 string = o.string;
489 uid = o.uid;
490 poolIdx = o.poolIdx;
491 }
492
493 public void setTo(String _string, int _uid) {
494 string = _string;
495 uid = _uid;
496 poolIdx = -1;
497 }
498
499 public void writeToParcel(Parcel dest, int flags) {
500 dest.writeString(string);
501 dest.writeInt(uid);
502 }
503
504 public void readFromParcel(Parcel src) {
505 string = src.readString();
506 uid = src.readInt();
507 poolIdx = -1;
508 }
509
510 @Override
511 public boolean equals(Object o) {
512 if (this == o) return true;
513 if (o == null || getClass() != o.getClass()) return false;
514
515 HistoryTag that = (HistoryTag) o;
516
517 if (uid != that.uid) return false;
518 if (!string.equals(that.string)) return false;
519
520 return true;
521 }
522
523 @Override
524 public int hashCode() {
525 int result = string.hashCode();
526 result = 31 * result + uid;
527 return result;
528 }
529 }
530
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700531 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700532 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700533
534 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800535
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800536 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800537 public static final byte CMD_NULL = -1;
538 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800539 public static final byte CMD_CURRENT_TIME = 5;
540 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800541
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700542 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700543
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800544 /**
545 * Return whether the command code is a delta data update.
546 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800547 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800548 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800549 }
550
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700551 public byte batteryLevel;
552 public byte batteryStatus;
553 public byte batteryHealth;
554 public byte batteryPlugType;
555
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900556 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700557 public char batteryVoltage;
558
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700559 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700560 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800561 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700562 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800563 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
564 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700565 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800566 public static final int STATE_PHONE_STATE_SHIFT = 6;
567 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700568 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800569 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800570 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800571
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700572 // These states always appear directly in the first int token
573 // of a delta change; they should be ones that change relatively
574 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700575 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
576 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800577 public static final int STATE_GPS_ON_FLAG = 1<<29;
578 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800579 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800580 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800581 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800582 public static final int STATE_WIFI_RUNNING_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700583 // These are on the lower bits used for the command; if they change
584 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700585 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700586 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700587 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700588 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
589 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
590 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
591 public static final int STATE_WIFI_ON_FLAG = 1<<17;
592 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700593
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700594 public static final int MOST_INTERESTING_STATES =
595 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
596 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
597
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700598 public int states;
599
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700600 public static final int STATE2_VIDEO_ON_FLAG = 1<<0;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700601 public int states2;
602
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800603 // The wake lock that was acquired at this point.
604 public HistoryTag wakelockTag;
605
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800606 // Kernel wakeup reason at this point.
607 public HistoryTag wakeReasonTag;
608
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800609 public static final int EVENT_FLAG_START = 0x8000;
610 public static final int EVENT_FLAG_FINISH = 0x4000;
611
612 // No event in this item.
613 public static final int EVENT_NONE = 0x0000;
614 // Event is about a process that is running.
615 public static final int EVENT_PROC = 0x0001;
616 // Event is about an application package that is in the foreground.
617 public static final int EVENT_FOREGROUND = 0x0002;
618 // Event is about an application package that is at the top of the screen.
619 public static final int EVENT_TOP = 0x0003;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800620 // Event is about an application package that is at the top of the screen.
621 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800622 // Number of event types.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800623 public static final int EVENT_COUNT = 0x0005;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800624
625 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
626 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
627 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
628 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
629 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
630 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800631 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
632 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800633
634 // For CMD_EVENT.
635 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800636 public HistoryTag eventTag;
637
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800638 // Only set for CMD_CURRENT_TIME.
639 public long currentTime;
640
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800641 // Meta-data when reading.
642 public int numReadInts;
643
644 // Pre-allocated objects.
645 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800646 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800647 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800648
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700649 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700650 }
651
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700652 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700653 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800654 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700655 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700656 }
657
658 public int describeContents() {
659 return 0;
660 }
661
662 public void writeToParcel(Parcel dest, int flags) {
663 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700664 int bat = (((int)cmd)&0xff)
665 | ((((int)batteryLevel)<<8)&0xff00)
666 | ((((int)batteryStatus)<<16)&0xf0000)
667 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800668 | ((((int)batteryPlugType)<<24)&0xf000000)
669 | (wakelockTag != null ? 0x10000000 : 0)
670 | (wakeReasonTag != null ? 0x20000000 : 0)
671 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700672 dest.writeInt(bat);
673 bat = (((int)batteryTemperature)&0xffff)
674 | ((((int)batteryVoltage)<<16)&0xffff0000);
675 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700676 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700677 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800678 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800679 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800680 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800681 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800682 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800683 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800684 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800685 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800686 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800687 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800688 if (cmd == CMD_CURRENT_TIME) {
689 dest.writeLong(currentTime);
690 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700691 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700692
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800693 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800694 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700695 int bat = src.readInt();
696 cmd = (byte)(bat&0xff);
697 batteryLevel = (byte)((bat>>8)&0xff);
698 batteryStatus = (byte)((bat>>16)&0xf);
699 batteryHealth = (byte)((bat>>20)&0xf);
700 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800701 int bat2 = src.readInt();
702 batteryTemperature = (short)(bat2&0xffff);
703 batteryVoltage = (char)((bat2>>16)&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700704 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700705 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800706 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800707 wakelockTag = localWakelockTag;
708 wakelockTag.readFromParcel(src);
709 } else {
710 wakelockTag = null;
711 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800712 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800713 wakeReasonTag = localWakeReasonTag;
714 wakeReasonTag.readFromParcel(src);
715 } else {
716 wakeReasonTag = null;
717 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800718 if ((bat&0x40000000) != 0) {
719 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800720 eventTag = localEventTag;
721 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800722 } else {
723 eventCode = EVENT_NONE;
724 eventTag = null;
725 }
726 if (cmd == CMD_CURRENT_TIME) {
727 currentTime = src.readLong();
728 } else {
729 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700730 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800731 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700732 }
733
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700734 public void clear() {
735 time = 0;
736 cmd = CMD_NULL;
737 batteryLevel = 0;
738 batteryStatus = 0;
739 batteryHealth = 0;
740 batteryPlugType = 0;
741 batteryTemperature = 0;
742 batteryVoltage = 0;
743 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700744 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800745 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800746 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800747 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800748 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700749 }
750
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700751 public void setTo(HistoryItem o) {
752 time = o.time;
753 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800754 setToCommon(o);
755 }
756
757 public void setTo(long time, byte cmd, HistoryItem o) {
758 this.time = time;
759 this.cmd = cmd;
760 setToCommon(o);
761 }
762
763 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700764 batteryLevel = o.batteryLevel;
765 batteryStatus = o.batteryStatus;
766 batteryHealth = o.batteryHealth;
767 batteryPlugType = o.batteryPlugType;
768 batteryTemperature = o.batteryTemperature;
769 batteryVoltage = o.batteryVoltage;
770 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700771 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800772 if (o.wakelockTag != null) {
773 wakelockTag = localWakelockTag;
774 wakelockTag.setTo(o.wakelockTag);
775 } else {
776 wakelockTag = null;
777 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800778 if (o.wakeReasonTag != null) {
779 wakeReasonTag = localWakeReasonTag;
780 wakeReasonTag.setTo(o.wakeReasonTag);
781 } else {
782 wakeReasonTag = null;
783 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800784 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800785 if (o.eventTag != null) {
786 eventTag = localEventTag;
787 eventTag.setTo(o.eventTag);
788 } else {
789 eventTag = null;
790 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800791 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700792 }
793
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800794 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700795 return batteryLevel == o.batteryLevel
796 && batteryStatus == o.batteryStatus
797 && batteryHealth == o.batteryHealth
798 && batteryPlugType == o.batteryPlugType
799 && batteryTemperature == o.batteryTemperature
800 && batteryVoltage == o.batteryVoltage
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800801 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700802 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800803 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700804 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800805
806 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800807 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800808 return false;
809 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800810 if (wakelockTag != o.wakelockTag) {
811 if (wakelockTag == null || o.wakelockTag == null) {
812 return false;
813 }
814 if (!wakelockTag.equals(o.wakelockTag)) {
815 return false;
816 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800817 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -0800818 if (wakeReasonTag != o.wakeReasonTag) {
819 if (wakeReasonTag == null || o.wakeReasonTag == null) {
820 return false;
821 }
822 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
823 return false;
824 }
825 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800826 if (eventTag != o.eventTag) {
827 if (eventTag == null || o.eventTag == null) {
828 return false;
829 }
830 if (!eventTag.equals(o.eventTag)) {
831 return false;
832 }
833 }
834 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800835 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700836 }
837
838 public static final class BitDescription {
839 public final int mask;
840 public final int shift;
841 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800842 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700843 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800844 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700845
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800846 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700847 this.mask = mask;
848 this.shift = -1;
849 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800850 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700851 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800852 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700853 }
854
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800855 public BitDescription(int mask, int shift, String name, String shortName,
856 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700857 this.mask = mask;
858 this.shift = shift;
859 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800860 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700861 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800862 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700863 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700864 }
865
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800866 public abstract int getHistoryTotalSize();
867
868 public abstract int getHistoryUsedSize();
869
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700870 public abstract boolean startIteratingHistoryLocked();
871
Dianne Hackborn099bc622014-01-22 13:39:16 -0800872 public abstract int getHistoryStringPoolSize();
873
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800874 public abstract int getHistoryStringPoolBytes();
875
876 public abstract String getHistoryTagPoolString(int index);
877
878 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -0800879
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700880 public abstract boolean getNextHistoryLocked(HistoryItem out);
881
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700882 public abstract void finishIteratingHistoryLocked();
883
884 public abstract boolean startIteratingOldHistoryLocked();
885
886 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
887
888 public abstract void finishIteratingOldHistoryLocked();
889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700891 * Return the base time offset for the battery history.
892 */
893 public abstract long getHistoryBaseTime();
894
895 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 * Returns the number of times the device has been started.
897 */
898 public abstract int getStartCount();
899
900 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700901 * 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 -0800902 * running on battery.
903 *
904 * {@hide}
905 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800906 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800908 /**
909 * Returns the number of times the screen was turned on.
910 *
911 * {@hide}
912 */
913 public abstract int getScreenOnCount(int which);
914
Dianne Hackborn617f8772009-03-31 15:04:46 -0700915 public static final int SCREEN_BRIGHTNESS_DARK = 0;
916 public static final int SCREEN_BRIGHTNESS_DIM = 1;
917 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
918 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
919 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
920
921 static final String[] SCREEN_BRIGHTNESS_NAMES = {
922 "dark", "dim", "medium", "light", "bright"
923 };
924
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800925 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
926 "0", "1", "2", "3", "4"
927 };
928
Dianne Hackborn617f8772009-03-31 15:04:46 -0700929 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
930
931 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700932 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700933 * the given brightness
934 *
935 * {@hide}
936 */
937 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800938 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700939
940 public abstract int getInputEventCount(int which);
941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700943 * 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 -0800944 * running on battery.
945 *
946 * {@hide}
947 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800948 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700949
950 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800951 * Returns the number of times a phone call was activated.
952 *
953 * {@hide}
954 */
955 public abstract int getPhoneOnCount(int which);
956
957 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700958 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700959 * the given signal strength.
960 *
961 * {@hide}
962 */
963 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800964 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700965
Dianne Hackborn617f8772009-03-31 15:04:46 -0700966 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700967 * Returns the time in microseconds that the phone has been trying to
968 * acquire a signal.
969 *
970 * {@hide}
971 */
972 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800973 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -0700974
975 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700976 * Returns the number of times the phone has entered the given signal strength.
977 *
978 * {@hide}
979 */
980 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
981
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800982 /**
983 * Returns the time in microseconds that the mobile network has been active
984 * (in a high power state).
985 *
986 * {@hide}
987 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800988 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800989
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800990 /**
991 * Returns the number of times that the mobile network has transitioned to the
992 * active state.
993 *
994 * {@hide}
995 */
996 public abstract int getMobileRadioActiveCount(int which);
997
998 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700999 * Returns the time in microseconds that is the difference between the mobile radio
1000 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1001 * from the radio.
1002 *
1003 * {@hide}
1004 */
1005 public abstract long getMobileRadioActiveAdjustedTime(int which);
1006
1007 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001008 * Returns the time in microseconds that the mobile network has been active
1009 * (in a high power state) but not being able to blame on an app.
1010 *
1011 * {@hide}
1012 */
1013 public abstract long getMobileRadioActiveUnknownTime(int which);
1014
1015 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001016 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001017 *
1018 * {@hide}
1019 */
1020 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001021
Dianne Hackborn627bba72009-03-24 22:32:56 -07001022 public static final int DATA_CONNECTION_NONE = 0;
1023 public static final int DATA_CONNECTION_GPRS = 1;
1024 public static final int DATA_CONNECTION_EDGE = 2;
1025 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001026 public static final int DATA_CONNECTION_CDMA = 4;
1027 public static final int DATA_CONNECTION_EVDO_0 = 5;
1028 public static final int DATA_CONNECTION_EVDO_A = 6;
1029 public static final int DATA_CONNECTION_1xRTT = 7;
1030 public static final int DATA_CONNECTION_HSDPA = 8;
1031 public static final int DATA_CONNECTION_HSUPA = 9;
1032 public static final int DATA_CONNECTION_HSPA = 10;
1033 public static final int DATA_CONNECTION_IDEN = 11;
1034 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001035 public static final int DATA_CONNECTION_LTE = 13;
1036 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001037 public static final int DATA_CONNECTION_HSPAP = 15;
1038 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001039
Dianne Hackborn627bba72009-03-24 22:32:56 -07001040 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001041 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001042 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001043 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001044 };
1045
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001046 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001047
1048 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001049 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001050 * the given data connection.
1051 *
1052 * {@hide}
1053 */
1054 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001055 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001058 * Returns the number of times the phone has entered the given data
1059 * connection type.
1060 *
1061 * {@hide}
1062 */
1063 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001064
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001065 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
1066 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001067 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001068 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
1069 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001070 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001071 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
1072 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
1073 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001074 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001075 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
1076 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001077 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001078 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
1079 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
1080 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
1081 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
1082 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
1083 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
1084 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
1085 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
1086 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
1087 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
1088 new String[] {"in", "out", "emergency", "off"},
1089 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001090 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001091 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
1092 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
1093 "0", "1", "2", "3", "4"
1094 }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001095 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
1096 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
1097 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001098 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07001099
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001100 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
1101 = new BitDescription[] {
1102 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
1103 };
1104
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001105 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001106 "null", "proc", "fg", "top", "sync"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001107 };
1108
1109 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001110 "Enl", "Epr", "Efg", "Etp", "Esy"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001111 };
1112
Dianne Hackborn617f8772009-03-31 15:04:46 -07001113 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001114 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001115 * running on battery.
1116 *
1117 * {@hide}
1118 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001119 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001120
1121 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001122 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001123 * been in the running state while the device was running on battery.
1124 *
1125 * {@hide}
1126 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001127 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001128
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001129 public static final int WIFI_STATE_OFF = 0;
1130 public static final int WIFI_STATE_OFF_SCANNING = 1;
1131 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1132 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1133 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1134 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1135 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1136 public static final int WIFI_STATE_SOFT_AP = 7;
1137
1138 static final String[] WIFI_STATE_NAMES = {
1139 "off", "scanning", "no_net", "disconn",
1140 "sta", "p2p", "sta_p2p", "soft_ap"
1141 };
1142
1143 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1144
1145 /**
1146 * Returns the time in microseconds that WiFi has been running in the given state.
1147 *
1148 * {@hide}
1149 */
1150 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001151 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001152
1153 /**
1154 * Returns the number of times that WiFi has entered the given state.
1155 *
1156 * {@hide}
1157 */
1158 public abstract int getWifiStateCount(int wifiState, int which);
1159
The Android Open Source Project10592532009-03-18 17:39:46 -07001160 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001161 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001162 * running on battery.
1163 *
1164 * {@hide}
1165 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001166 public abstract long getBluetoothOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001167
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001168 public abstract int getBluetoothPingCount();
1169
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001170 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1171 public static final int BLUETOOTH_STATE_LOW = 1;
1172 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1173 public static final int BLUETOOTH_STATE_HIGH = 3;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001174
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001175 static final String[] BLUETOOTH_STATE_NAMES = {
1176 "inactive", "low", "med", "high"
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001177 };
1178
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001179 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001180
1181 /**
1182 * Returns the time in microseconds that Bluetooth has been running in the
1183 * given active state.
1184 *
1185 * {@hide}
1186 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001187 public abstract long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001188 long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001189
1190 /**
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001191 * Returns the number of times that Bluetooth has entered the given active state.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001192 *
1193 * {@hide}
1194 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001195 public abstract int getBluetoothStateCount(int bluetoothState, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001196
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001197 public static final int NETWORK_MOBILE_RX_DATA = 0;
1198 public static final int NETWORK_MOBILE_TX_DATA = 1;
1199 public static final int NETWORK_WIFI_RX_DATA = 2;
1200 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001201
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001202 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001203
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001204 public abstract long getNetworkActivityBytes(int type, int which);
1205 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001206
The Android Open Source Project10592532009-03-18 17:39:46 -07001207 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001208 * Return the wall clock time when battery stats data collection started.
1209 */
1210 public abstract long getStartClockTime();
1211
1212 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 * Return whether we are currently running on battery.
1214 */
1215 public abstract boolean getIsOnBattery();
1216
1217 /**
1218 * Returns a SparseArray containing the statistics for each uid.
1219 */
1220 public abstract SparseArray<? extends Uid> getUidStats();
1221
1222 /**
1223 * Returns the current battery uptime in microseconds.
1224 *
1225 * @param curTime the amount of elapsed realtime in microseconds.
1226 */
1227 public abstract long getBatteryUptime(long curTime);
1228
1229 /**
1230 * Returns the current battery realtime in microseconds.
1231 *
1232 * @param curTime the amount of elapsed realtime in microseconds.
1233 */
1234 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001235
1236 /**
Evan Millar633a1742009-04-02 16:36:33 -07001237 * Returns the battery percentage level at the last time the device was unplugged from power, or
1238 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07001239 */
Evan Millar633a1742009-04-02 16:36:33 -07001240 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07001241
1242 /**
Evan Millar633a1742009-04-02 16:36:33 -07001243 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1244 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07001245 */
Evan Millar633a1742009-04-02 16:36:33 -07001246 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247
1248 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001249 * Get the amount the battery has discharged since the stats were
1250 * last reset after charging, as a lower-end approximation.
1251 */
1252 public abstract int getLowDischargeAmountSinceCharge();
1253
1254 /**
1255 * Get the amount the battery has discharged since the stats were
1256 * last reset after charging, as an upper-end approximation.
1257 */
1258 public abstract int getHighDischargeAmountSinceCharge();
1259
1260 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07001261 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
1262 */
1263 public abstract int getDischargeAmount(int which);
1264
1265 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001266 * Get the amount the battery has discharged while the screen was on,
1267 * since the last time power was unplugged.
1268 */
1269 public abstract int getDischargeAmountScreenOn();
1270
1271 /**
1272 * Get the amount the battery has discharged while the screen was on,
1273 * since the last time the device was charged.
1274 */
1275 public abstract int getDischargeAmountScreenOnSinceCharge();
1276
1277 /**
1278 * Get the amount the battery has discharged while the screen was off,
1279 * since the last time power was unplugged.
1280 */
1281 public abstract int getDischargeAmountScreenOff();
1282
1283 /**
1284 * Get the amount the battery has discharged while the screen was off,
1285 * since the last time the device was charged.
1286 */
1287 public abstract int getDischargeAmountScreenOffSinceCharge();
1288
1289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 * Returns the total, last, or current battery uptime in microseconds.
1291 *
1292 * @param curTime the elapsed realtime in microseconds.
1293 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1294 */
1295 public abstract long computeBatteryUptime(long curTime, int which);
1296
1297 /**
1298 * Returns the total, last, or current battery realtime in microseconds.
1299 *
1300 * @param curTime the current elapsed realtime in microseconds.
1301 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1302 */
1303 public abstract long computeBatteryRealtime(long curTime, int which);
1304
1305 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001306 * Returns the total, last, or current battery screen off uptime in microseconds.
1307 *
1308 * @param curTime the elapsed realtime in microseconds.
1309 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1310 */
1311 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
1312
1313 /**
1314 * Returns the total, last, or current battery screen off realtime in microseconds.
1315 *
1316 * @param curTime the current elapsed realtime in microseconds.
1317 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1318 */
1319 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
1320
1321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 * Returns the total, last, or current uptime in microseconds.
1323 *
1324 * @param curTime the current elapsed realtime in microseconds.
1325 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1326 */
1327 public abstract long computeUptime(long curTime, int which);
1328
1329 /**
1330 * Returns the total, last, or current realtime in microseconds.
1331 * *
1332 * @param curTime the current elapsed realtime in microseconds.
1333 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1334 */
1335 public abstract long computeRealtime(long curTime, int which);
Evan Millarc64edde2009-04-18 12:26:32 -07001336
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001337 public abstract Map<String, ? extends LongCounter> getWakeupReasonStats();
1338
Evan Millarc64edde2009-04-18 12:26:32 -07001339 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340
Amith Yamasanie43530a2009-08-21 13:11:37 -07001341 /** Returns the number of different speeds that the CPU can run at */
1342 public abstract int getCpuSpeedSteps();
1343
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001344 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1345
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001346 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 long days = seconds / (60 * 60 * 24);
1348 if (days != 0) {
1349 out.append(days);
1350 out.append("d ");
1351 }
1352 long used = days * 60 * 60 * 24;
1353
1354 long hours = (seconds - used) / (60 * 60);
1355 if (hours != 0 || used != 0) {
1356 out.append(hours);
1357 out.append("h ");
1358 }
1359 used += hours * 60 * 60;
1360
1361 long mins = (seconds-used) / 60;
1362 if (mins != 0 || used != 0) {
1363 out.append(mins);
1364 out.append("m ");
1365 }
1366 used += mins * 60;
1367
1368 if (seconds != 0 || used != 0) {
1369 out.append(seconds-used);
1370 out.append("s ");
1371 }
1372 }
1373
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001374 public final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001376 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 sb.append((time - (sec * 100)) * 10);
1378 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
1380
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001381 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001383 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 sb.append(time - (sec * 1000));
1385 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 }
1387
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001388 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001389 long sec = time / 1000;
1390 formatTimeRaw(sb, sec);
1391 sb.append(time - (sec * 1000));
1392 sb.append("ms");
1393 }
1394
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001395 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001397 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
1399 float perc = ((float)num) / ((float)den) * 100;
1400 mFormatBuilder.setLength(0);
1401 mFormatter.format("%.1f%%", perc);
1402 return mFormatBuilder.toString();
1403 }
1404
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001405 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07001406 mFormatBuilder.setLength(0);
1407
1408 if (bytes < BYTES_PER_KB) {
1409 return bytes + "B";
1410 } else if (bytes < BYTES_PER_MB) {
1411 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1412 return mFormatBuilder.toString();
1413 } else if (bytes < BYTES_PER_GB){
1414 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1415 return mFormatBuilder.toString();
1416 } else {
1417 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1418 return mFormatBuilder.toString();
1419 }
1420 }
1421
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001422 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001423 if (timer != null) {
1424 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001425 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001426 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1427 return totalTimeMillis;
1428 }
1429 return 0;
1430 }
1431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 /**
1433 *
1434 * @param sb a StringBuilder object.
1435 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001436 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 * @param name the name of the wakelock.
1438 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1439 * @param linePrefix a String to be prepended to each line of output.
1440 * @return the line prefix
1441 */
1442 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001443 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444
1445 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001446 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447
Evan Millarc64edde2009-04-18 12:26:32 -07001448 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 if (totalTimeMillis != 0) {
1450 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001451 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001452 if (name != null) {
1453 sb.append(name);
1454 sb.append(' ');
1455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 sb.append('(');
1457 sb.append(count);
1458 sb.append(" times)");
1459 return ", ";
1460 }
1461 }
1462 return linePrefix;
1463 }
1464
1465 /**
1466 * Checkin version of wakelock printer. Prints simple comma-separated list.
1467 *
1468 * @param sb a StringBuilder object.
1469 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001470 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 * @param name the name of the wakelock.
1472 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1473 * @param linePrefix a String to be prepended to each line of output.
1474 * @return the line prefix
1475 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001476 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
1477 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 long totalTimeMicros = 0;
1479 int count = 0;
1480 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001481 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07001482 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 }
1484 sb.append(linePrefix);
1485 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1486 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001487 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 sb.append(count);
1489 return ",";
1490 }
1491
1492 /**
1493 * Dump a comma-separated line of values for terse checkin mode.
1494 *
1495 * @param pw the PageWriter to dump log to
1496 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1497 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1498 * @param args type-dependent data arguments
1499 */
1500 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1501 Object... args ) {
1502 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1503 pw.print(uid); pw.print(',');
1504 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001505 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506
1507 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001508 pw.print(',');
1509 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001511 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
1513
1514 /**
1515 * Checkin server version of dump to produce more compact, computer-readable log.
1516 *
1517 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001519 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1521 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1522 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1524 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001525 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1526 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1527 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 final long totalRealtime = computeRealtime(rawRealtime, which);
1529 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001530 final long screenOnTime = getScreenOnTime(rawRealtime, which);
1531 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1532 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1533 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1534 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535
1536 StringBuilder sb = new StringBuilder(128);
1537
Evan Millar22ac0432009-03-31 11:33:18 -07001538 SparseArray<? extends Uid> uidStats = getUidStats();
1539 final int NU = uidStats.size();
1540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 // Dump "battery" stat
1544 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001545 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001546 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001547 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001548 getStartClockTime(),
1549 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001551 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001552 long fullWakeLockTimeTotal = 0;
1553 long partialWakeLockTimeTotal = 0;
1554
1555 for (int iu = 0; iu < NU; iu++) {
1556 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001557
Evan Millar22ac0432009-03-31 11:33:18 -07001558 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1559 if (wakelocks.size() > 0) {
1560 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1561 : wakelocks.entrySet()) {
1562 Uid.Wakelock wl = ent.getValue();
1563
1564 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1565 if (fullWakeTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001566 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
1567 which);
Evan Millar22ac0432009-03-31 11:33:18 -07001568 }
1569
1570 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1571 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001572 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001573 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001574 }
1575 }
1576 }
1577 }
1578
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001579 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1580 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1581 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1582 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1583 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1584 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1585 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1586 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1587
1588 // Dump network stats
1589 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1590 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1591 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 // Dump misc stats
1594 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001595 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001596 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001597 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001598 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001599 getInputEventCount(which), getMobileRadioActiveTime(rawRealtime, which),
1600 getMobileRadioActiveAdjustedTime(which));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001601
1602 // Dump screen brightness stats
1603 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1604 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001605 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001606 }
1607 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001608
Dianne Hackborn627bba72009-03-24 22:32:56 -07001609 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001610 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1611 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001612 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001613 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001614 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001615 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001616 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001617 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001618 args[i] = getPhoneSignalStrengthCount(i, which);
1619 }
1620 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001621
Dianne Hackborn627bba72009-03-24 22:32:56 -07001622 // Dump network type stats
1623 args = new Object[NUM_DATA_CONNECTION_TYPES];
1624 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001625 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001626 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001627 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1628 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1629 args[i] = getPhoneDataConnectionCount(i, which);
1630 }
1631 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001632
1633 // Dump wifi state stats
1634 args = new Object[NUM_WIFI_STATES];
1635 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001636 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001637 }
1638 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
1639 for (int i=0; i<NUM_WIFI_STATES; i++) {
1640 args[i] = getWifiStateCount(i, which);
1641 }
1642 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
1643
1644 // Dump bluetooth state stats
1645 args = new Object[NUM_BLUETOOTH_STATES];
1646 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001647 args[i] = getBluetoothStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001648 }
1649 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
1650 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1651 args[i] = getBluetoothStateCount(i, which);
1652 }
1653 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
1654
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001655 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001656 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001657 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001660 if (which == STATS_SINCE_UNPLUGGED) {
1661 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1662 getDischargeStartLevel()-getDischargeCurrentLevel(),
1663 getDischargeStartLevel()-getDischargeCurrentLevel(),
1664 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1665 } else {
1666 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1667 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1668 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1669 }
1670
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001671 if (reqUid < 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001672 Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001673 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001674 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001675 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001676 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001677 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001678 sb.toString());
1679 }
Evan Millarc64edde2009-04-18 12:26:32 -07001680 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001681 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
1682 if (wakeupReasons.size() > 0) {
1683 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
1684 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
1685 "\"" + ent.getKey() + "\"", ent.getValue().getCountLocked(which));
1686 }
1687 }
Evan Millarc64edde2009-04-18 12:26:32 -07001688 }
1689
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001690 BatteryStatsHelper helper = new BatteryStatsHelper(context);
1691 helper.create(this);
1692 helper.refreshStats(which, UserHandle.USER_ALL);
1693 List<BatterySipper> sippers = helper.getUsageList();
1694 if (sippers != null && sippers.size() > 0) {
1695 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1696 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001697 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001698 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1699 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1700 for (int i=0; i<sippers.size(); i++) {
1701 BatterySipper bs = sippers.get(i);
1702 int uid = 0;
1703 String label;
1704 switch (bs.drainType) {
1705 case IDLE:
1706 label="idle";
1707 break;
1708 case CELL:
1709 label="cell";
1710 break;
1711 case PHONE:
1712 label="phone";
1713 break;
1714 case WIFI:
1715 label="wifi";
1716 break;
1717 case BLUETOOTH:
1718 label="blue";
1719 break;
1720 case SCREEN:
1721 label="scrn";
1722 break;
1723 case APP:
1724 uid = bs.uidObj.getUid();
1725 label = "uid";
1726 break;
1727 case USER:
1728 uid = UserHandle.getUid(bs.userId, 0);
1729 label = "user";
1730 break;
1731 case UNACCOUNTED:
1732 label = "unacc";
1733 break;
1734 case OVERCOUNTED:
1735 label = "over";
1736 break;
1737 default:
1738 label = "???";
1739 }
1740 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1741 BatteryStatsHelper.makemAh(bs.value));
1742 }
1743 }
1744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 for (int iu = 0; iu < NU; iu++) {
1746 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001747 if (reqUid >= 0 && uid != reqUid) {
1748 continue;
1749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 Uid u = uidStats.valueAt(iu);
1751 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001752 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1753 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1754 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1755 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1756 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1757 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001758 long mobileActiveTime = u.getMobileRadioActiveTime(which);
1759 int mobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001760 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1761 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001762 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
1763 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
1764 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001765
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001766 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1767 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001768 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001769 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1770 wifiBytesRx, wifiBytesTx,
1771 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001772 wifiPacketsRx, wifiPacketsTx,
1773 mobileActiveTime, mobileActiveCount);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001774 }
1775
Nick Pelly6ccaa542012-06-15 15:22:47 -07001776 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001777 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001778 dumpLine(pw, uid, category, WIFI_DATA,
1779 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781
Dianne Hackborn617f8772009-03-31 15:04:46 -07001782 if (u.hasUserActivity()) {
1783 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1784 boolean hasData = false;
1785 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1786 int val = u.getUserActivityCount(i, which);
1787 args[i] = val;
1788 if (val != 0) hasData = true;
1789 }
1790 if (hasData) {
1791 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1792 }
1793 }
1794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1796 if (wakelocks.size() > 0) {
1797 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1798 : wakelocks.entrySet()) {
1799 Uid.Wakelock wl = ent.getValue();
1800 String linePrefix = "";
1801 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001802 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001803 rawRealtime, "f", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001804 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001805 rawRealtime, "p", which, linePrefix);
Evan Millarc64edde2009-04-18 12:26:32 -07001806 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001807 rawRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808
1809 // Only log if we had at lease one wakelock...
1810 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001811 String name = ent.getKey();
1812 if (name.indexOf(',') >= 0) {
1813 name = name.replace(',', '_');
1814 }
1815 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
1817 }
1818 }
1819
1820 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1821 if (sensors.size() > 0) {
1822 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1823 : sensors.entrySet()) {
1824 Uid.Sensor se = ent.getValue();
1825 int sensorNumber = ent.getKey();
1826 Timer timer = se.getSensorTime();
1827 if (timer != null) {
1828 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001829 long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07001830 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 if (totalTime != 0) {
1832 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1833 }
1834 }
1835 }
1836 }
1837
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001838 Timer vibTimer = u.getVibratorOnTimer();
1839 if (vibTimer != null) {
1840 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001841 long totalTime = (vibTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001842 int count = vibTimer.getCountLocked(which);
1843 if (totalTime != 0) {
1844 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1845 }
1846 }
1847
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001848 Timer fgTimer = u.getForegroundActivityTimer();
1849 if (fgTimer != null) {
1850 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001851 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001852 int count = fgTimer.getCountLocked(which);
1853 if (totalTime != 0) {
1854 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1855 }
1856 }
1857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1859 if (processStats.size() > 0) {
1860 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1861 : processStats.entrySet()) {
1862 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001863
1864 final long userMillis = ps.getUserTime(which) * 10;
1865 final long systemMillis = ps.getSystemTime(which) * 10;
1866 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1867 final long starts = ps.getStarts(which);
1868
1869 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1870 || starts != 0) {
1871 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1872 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 }
1874 }
1875 }
1876
1877 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1878 if (packageStats.size() > 0) {
1879 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1880 : packageStats.entrySet()) {
1881
1882 Uid.Pkg ps = ent.getValue();
1883 int wakeups = ps.getWakeups(which);
1884 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1885 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1886 : serviceStats.entrySet()) {
1887 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1888 long startTime = ss.getStartTime(batteryUptime, which);
1889 int starts = ss.getStarts(which);
1890 int launches = ss.getLaunches(which);
1891 if (startTime != 0 || starts != 0 || launches != 0) {
1892 dumpLine(pw, uid, category, APK_DATA,
1893 wakeups, // wakeup alarms
1894 ent.getKey(), // Apk
1895 sent.getKey(), // service
1896 startTime / 1000, // time spent started, in ms
1897 starts,
1898 launches);
1899 }
1900 }
1901 }
1902 }
1903 }
1904 }
1905
Dianne Hackborn81038902012-11-26 17:04:09 -08001906 static final class TimerEntry {
1907 final String mName;
1908 final int mId;
1909 final BatteryStats.Timer mTimer;
1910 final long mTime;
1911 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1912 mName = name;
1913 mId = id;
1914 mTimer = timer;
1915 mTime = time;
1916 }
1917 }
1918
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001919 private void printmAh(PrintWriter printer, double power) {
1920 printer.print(BatteryStatsHelper.makemAh(power));
1921 }
1922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001924 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
1925 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1927 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1928 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929
1930 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1931 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1932 final long totalRealtime = computeRealtime(rawRealtime, which);
1933 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001934 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
1935 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
1936 which);
1937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001939
1940 SparseArray<? extends Uid> uidStats = getUidStats();
1941 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001943 sb.setLength(0);
1944 sb.append(prefix);
1945 sb.append(" Time on battery: ");
1946 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1947 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1948 sb.append(") realtime, ");
1949 formatTimeMs(sb, whichBatteryUptime / 1000);
1950 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1951 sb.append(") uptime");
1952 pw.println(sb.toString());
1953 sb.setLength(0);
1954 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001955 sb.append(" Time on battery screen off: ");
1956 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
1957 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
1958 sb.append(") realtime, ");
1959 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
1960 sb.append("(");
1961 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
1962 sb.append(") uptime");
1963 pw.println(sb.toString());
1964 sb.setLength(0);
1965 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001966 sb.append(" Total run time: ");
1967 formatTimeMs(sb, totalRealtime / 1000);
1968 sb.append("realtime, ");
1969 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001970 sb.append("uptime");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001971 pw.println(sb.toString());
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001972 pw.print(" Start clock time: ");
1973 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
1974
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001975 final long screenOnTime = getScreenOnTime(rawRealtime, which);
1976 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
1977 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
1978 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
1979 final long bluetoothOnTime = getBluetoothOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001980 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001981 sb.append(prefix);
1982 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
1983 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001984 sb.append(") "); sb.append(getScreenOnCount(which));
1985 sb.append("x, Input events: "); sb.append(getInputEventCount(which));
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001986 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001987 if (phoneOnTime != 0) {
1988 sb.setLength(0);
1989 sb.append(prefix);
1990 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
1991 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
1992 sb.append(") "); sb.append(getPhoneOnCount(which));
1993 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001994 sb.setLength(0);
1995 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001996 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07001997 boolean didOne = false;
1998 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001999 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002000 if (time == 0) {
2001 continue;
2002 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002003 sb.append("\n ");
2004 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002005 didOne = true;
2006 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
2007 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002008 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002009 sb.append("(");
2010 sb.append(formatRatioLocked(time, screenOnTime));
2011 sb.append(")");
2012 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002013 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002014 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07002015
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002016 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002017 long fullWakeLockTimeTotalMicros = 0;
2018 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08002019
Dianne Hackborn81038902012-11-26 17:04:09 -08002020 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
2021
Evan Millar22ac0432009-03-31 11:33:18 -07002022 for (int iu = 0; iu < NU; iu++) {
2023 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002024
Evan Millar22ac0432009-03-31 11:33:18 -07002025 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2026 if (wakelocks.size() > 0) {
2027 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2028 : wakelocks.entrySet()) {
2029 Uid.Wakelock wl = ent.getValue();
2030
2031 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
2032 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07002033 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002034 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07002035 }
2036
2037 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
2038 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08002039 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002040 rawRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08002041 if (totalTimeMicros > 0) {
2042 if (reqUid < 0) {
2043 // Only show the ordered list of all wake
2044 // locks if the caller is not asking for data
2045 // about a specific uid.
2046 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
2047 partialWakeTimer, totalTimeMicros));
2048 }
2049 partialWakeLockTimeTotalMicros += totalTimeMicros;
2050 }
Evan Millar22ac0432009-03-31 11:33:18 -07002051 }
2052 }
2053 }
2054 }
2055
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002056 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2057 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2058 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2059 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2060 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2061 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2062 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2063 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
2064
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002065 if (fullWakeLockTimeTotalMicros != 0) {
2066 sb.setLength(0);
2067 sb.append(prefix);
2068 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
2069 (fullWakeLockTimeTotalMicros + 500) / 1000);
2070 pw.println(sb.toString());
2071 }
2072
2073 if (partialWakeLockTimeTotalMicros != 0) {
2074 sb.setLength(0);
2075 sb.append(prefix);
2076 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
2077 (partialWakeLockTimeTotalMicros + 500) / 1000);
2078 pw.println(sb.toString());
2079 }
2080
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002081 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002082 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
2083 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
2084 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
2085 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002086 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002087 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002088 sb.append(" Signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07002089 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08002090 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002091 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002092 if (time == 0) {
2093 continue;
2094 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002095 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002096 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002097 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08002098 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002099 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002100 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002101 sb.append("(");
2102 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002103 sb.append(") ");
2104 sb.append(getPhoneSignalStrengthCount(i, which));
2105 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002106 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002107 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002108 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07002109
2110 sb.setLength(0);
2111 sb.append(prefix);
2112 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002113 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07002114 pw.println(sb.toString());
2115
Dianne Hackborn627bba72009-03-24 22:32:56 -07002116 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002117 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002118 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002119 didOne = false;
2120 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002121 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002122 if (time == 0) {
2123 continue;
2124 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002125 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002126 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002127 didOne = true;
2128 sb.append(DATA_CONNECTION_NAMES[i]);
2129 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002130 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002131 sb.append("(");
2132 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07002133 sb.append(") ");
2134 sb.append(getPhoneDataConnectionCount(i, which));
2135 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002136 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002137 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07002138 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002139
2140 sb.setLength(0);
2141 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002142 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002143 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002144 formatTimeMs(sb, mobileActiveTime / 1000);
2145 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
2146 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
2147 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07002148 pw.println(sb.toString());
2149
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002150 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
2151 if (mobileActiveUnknownTime != 0) {
2152 sb.setLength(0);
2153 sb.append(prefix);
2154 sb.append(" Mobile radio active unknown time: ");
2155 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
2156 sb.append("(");
2157 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
2158 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
2159 sb.append("x");
2160 pw.println(sb.toString());
2161 }
2162
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002163 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
2164 if (mobileActiveAdjustedTime != 0) {
2165 sb.setLength(0);
2166 sb.append(prefix);
2167 sb.append(" Mobile radio active adjusted time: ");
2168 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
2169 sb.append("(");
2170 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
2171 sb.append(")");
2172 pw.println(sb.toString());
2173 }
2174
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002175 pw.print(prefix);
2176 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
2177 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
2178 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
2179 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002180 sb.setLength(0);
2181 sb.append(prefix);
2182 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
2183 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
2184 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2185 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002186 sb.append(")");
2187 pw.println(sb.toString());
2188
2189 sb.setLength(0);
2190 sb.append(prefix);
2191 sb.append(" Wifi states:");
2192 didOne = false;
2193 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002194 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002195 if (time == 0) {
2196 continue;
2197 }
2198 sb.append("\n ");
2199 didOne = true;
2200 sb.append(WIFI_STATE_NAMES[i]);
2201 sb.append(" ");
2202 formatTimeMs(sb, time/1000);
2203 sb.append("(");
2204 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2205 sb.append(") ");
2206 sb.append(getPhoneDataConnectionCount(i, which));
2207 sb.append("x");
2208 }
2209 if (!didOne) sb.append(" (no activity)");
2210 pw.println(sb.toString());
2211
2212 sb.setLength(0);
2213 sb.append(prefix);
2214 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002215 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2216 sb.append(")");
2217 pw.println(sb.toString());
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002218
2219 sb.setLength(0);
2220 sb.append(prefix);
2221 sb.append(" Bluetooth states:");
2222 didOne = false;
2223 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002224 final long time = getBluetoothStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002225 if (time == 0) {
2226 continue;
2227 }
2228 sb.append("\n ");
2229 didOne = true;
2230 sb.append(BLUETOOTH_STATE_NAMES[i]);
2231 sb.append(" ");
2232 formatTimeMs(sb, time/1000);
2233 sb.append("(");
2234 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2235 sb.append(") ");
2236 sb.append(getPhoneDataConnectionCount(i, which));
2237 sb.append("x");
2238 }
2239 if (!didOne) sb.append(" (no activity)");
2240 pw.println(sb.toString());
2241
2242 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002243
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002244 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002245 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002246 pw.print(prefix); pw.println(" Device is currently unplugged");
2247 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2248 pw.println(getDischargeStartLevel());
2249 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2250 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07002251 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002252 pw.print(prefix); pw.println(" Device is currently plugged into power");
2253 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2254 pw.println(getDischargeStartLevel());
2255 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2256 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002257 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002258 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2259 pw.println(getDischargeAmountScreenOn());
2260 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2261 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07002262 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002263 } else {
2264 pw.print(prefix); pw.println(" Device battery use since last full charge");
2265 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2266 pw.println(getLowDischargeAmountSinceCharge());
2267 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2268 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002269 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2270 pw.println(getDischargeAmountScreenOnSinceCharge());
2271 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2272 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08002273 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002274 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002275
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002276 BatteryStatsHelper helper = new BatteryStatsHelper(context);
2277 helper.create(this);
2278 helper.refreshStats(which, UserHandle.USER_ALL);
2279 List<BatterySipper> sippers = helper.getUsageList();
2280 if (sippers != null && sippers.size() > 0) {
2281 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2282 pw.print(prefix); pw.print(" Capacity: ");
2283 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08002284 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002285 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
2286 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
2287 pw.println();
2288 for (int i=0; i<sippers.size(); i++) {
2289 BatterySipper bs = sippers.get(i);
2290 switch (bs.drainType) {
2291 case IDLE:
2292 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2293 pw.println();
2294 break;
2295 case CELL:
2296 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2297 pw.println();
2298 break;
2299 case PHONE:
2300 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2301 pw.println();
2302 break;
2303 case WIFI:
2304 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2305 pw.println();
2306 break;
2307 case BLUETOOTH:
2308 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2309 pw.println();
2310 break;
2311 case SCREEN:
2312 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2313 pw.println();
2314 break;
2315 case APP:
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002316 pw.print(prefix); pw.print(" Uid ");
2317 UserHandle.formatUid(pw, bs.uidObj.getUid());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002318 pw.print(": "); printmAh(pw, bs.value); pw.println();
2319 break;
2320 case USER:
2321 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2322 pw.print(": "); printmAh(pw, bs.value); pw.println();
2323 break;
2324 case UNACCOUNTED:
2325 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2326 pw.println();
2327 break;
2328 case OVERCOUNTED:
2329 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2330 pw.println();
2331 break;
2332 }
2333 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08002334 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002335 }
2336
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002337 sippers = helper.getMobilemsppList();
2338 if (sippers != null && sippers.size() > 0) {
2339 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002340 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002341 for (int i=0; i<sippers.size(); i++) {
2342 BatterySipper bs = sippers.get(i);
2343 sb.setLength(0);
2344 sb.append(prefix); sb.append(" Uid ");
2345 UserHandle.formatUid(sb, bs.uidObj.getUid());
2346 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
2347 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
2348 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002349 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002350 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002351 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002352 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002353 sb.setLength(0);
2354 sb.append(prefix);
2355 sb.append(" TOTAL TIME: ");
2356 formatTimeMs(sb, totalTime);
2357 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
2358 sb.append(")");
2359 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002360 pw.println();
2361 }
2362
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002363 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
2364 @Override
2365 public int compare(TimerEntry lhs, TimerEntry rhs) {
2366 long lhsTime = lhs.mTime;
2367 long rhsTime = rhs.mTime;
2368 if (lhsTime < rhsTime) {
2369 return 1;
2370 }
2371 if (lhsTime > rhsTime) {
2372 return -1;
2373 }
2374 return 0;
2375 }
2376 };
2377
2378 if (reqUid < 0) {
2379 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
2380 if (kernelWakelocks.size() > 0) {
2381 final ArrayList<TimerEntry> ktimers = new ArrayList<TimerEntry>();
2382 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
2383 BatteryStats.Timer timer = ent.getValue();
2384 long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
2385 if (totalTimeMillis > 0) {
2386 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
2387 }
2388 }
2389 if (ktimers.size() > 0) {
2390 Collections.sort(ktimers, timerComparator);
2391 pw.print(prefix); pw.println(" All kernel wake locks:");
2392 for (int i=0; i<ktimers.size(); i++) {
2393 TimerEntry timer = ktimers.get(i);
2394 String linePrefix = ": ";
2395 sb.setLength(0);
2396 sb.append(prefix);
2397 sb.append(" Kernel Wake lock ");
2398 sb.append(timer.mName);
2399 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
2400 which, linePrefix);
2401 if (!linePrefix.equals(": ")) {
2402 sb.append(" realtime");
2403 // Only print out wake locks that were held
2404 pw.println(sb.toString());
2405 }
2406 }
2407 pw.println();
2408 }
2409 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002410
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002411 if (timers.size() > 0) {
2412 Collections.sort(timers, timerComparator);
2413 pw.print(prefix); pw.println(" All partial wake locks:");
2414 for (int i=0; i<timers.size(); i++) {
2415 TimerEntry timer = timers.get(i);
2416 sb.setLength(0);
2417 sb.append(" Wake lock ");
2418 UserHandle.formatUid(sb, timer.mId);
2419 sb.append(" ");
2420 sb.append(timer.mName);
2421 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
2422 sb.append(" realtime");
2423 pw.println(sb.toString());
2424 }
2425 timers.clear();
2426 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08002427 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002428
2429 Map<String, ? extends LongCounter> wakeupReasons = getWakeupReasonStats();
2430 if (wakeupReasons.size() > 0) {
2431 pw.print(prefix); pw.println(" All wakeup reasons:");
2432 final ArrayList<TimerEntry> reasons = new ArrayList<TimerEntry>();
2433 for (Map.Entry<String, ? extends LongCounter> ent : wakeupReasons.entrySet()) {
2434 BatteryStats.LongCounter counter = ent.getValue();
2435 reasons.add(new TimerEntry(ent.getKey(), 0, null,
2436 ent.getValue().getCountLocked(which)));
2437 }
2438 Collections.sort(reasons, timerComparator);
2439 for (int i=0; i<reasons.size(); i++) {
2440 TimerEntry timer = reasons.get(i);
2441 String linePrefix = ": ";
2442 sb.setLength(0);
2443 sb.append(prefix);
2444 sb.append(" Wakeup reason ");
2445 sb.append(timer.mName);
2446 sb.append(": ");
2447 formatTimeMs(sb, timer.mTime);
2448 sb.append("realtime");
2449 pw.println(sb.toString());
2450 }
2451 pw.println();
2452 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002453 }
Evan Millar22ac0432009-03-31 11:33:18 -07002454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 for (int iu=0; iu<NU; iu++) {
2456 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08002457 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002458 continue;
2459 }
2460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002462
2463 pw.print(prefix);
2464 pw.print(" ");
2465 UserHandle.formatUid(pw, uid);
2466 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002468
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002469 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2470 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2471 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2472 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2473 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2474 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002475 long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
2476 int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002477 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2478 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002479 long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
2480 long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
2481 long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002482
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002483 if (mobileRxBytes > 0 || mobileTxBytes > 0
2484 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002485 pw.print(prefix); pw.print(" Mobile network: ");
2486 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002487 pw.print(formatBytesLocked(mobileTxBytes));
2488 pw.print(" sent (packets "); pw.print(mobileRxPackets);
2489 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002491 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
2492 sb.setLength(0);
2493 sb.append(prefix); sb.append(" Mobile radio active: ");
2494 formatTimeMs(sb, uidMobileActiveTime / 1000);
2495 sb.append("(");
2496 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
2497 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
2498 long packets = mobileRxPackets + mobileTxPackets;
2499 if (packets == 0) {
2500 packets = 1;
2501 }
2502 sb.append(" @ ");
2503 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
2504 sb.append(" mspp");
2505 pw.println(sb.toString());
2506 }
2507
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002508 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002509 pw.print(prefix); pw.print(" Wi-Fi network: ");
2510 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002511 pw.print(formatBytesLocked(wifiTxBytes));
2512 pw.print(" sent (packets "); pw.print(wifiRxPackets);
2513 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002514 }
2515
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002516 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
2517 || uidWifiRunningTime != 0) {
2518 sb.setLength(0);
2519 sb.append(prefix); sb.append(" Wifi Running: ");
2520 formatTimeMs(sb, uidWifiRunningTime / 1000);
2521 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
2522 whichBatteryRealtime)); sb.append(")\n");
2523 sb.append(prefix); sb.append(" Full Wifi Lock: ");
2524 formatTimeMs(sb, fullWifiLockOnTime / 1000);
2525 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
2526 whichBatteryRealtime)); sb.append(")\n");
2527 sb.append(prefix); sb.append(" Wifi Scan: ");
2528 formatTimeMs(sb, wifiScanTime / 1000);
2529 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
2530 whichBatteryRealtime)); sb.append(")");
2531 pw.println(sb.toString());
2532 }
2533
Dianne Hackborn617f8772009-03-31 15:04:46 -07002534 if (u.hasUserActivity()) {
2535 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07002536 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002537 int val = u.getUserActivityCount(i, which);
2538 if (val != 0) {
2539 if (!hasData) {
2540 sb.setLength(0);
2541 sb.append(" User activity: ");
2542 hasData = true;
2543 } else {
2544 sb.append(", ");
2545 }
2546 sb.append(val);
2547 sb.append(" ");
2548 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
2549 }
2550 }
2551 if (hasData) {
2552 pw.println(sb.toString());
2553 }
2554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555
2556 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2557 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002558 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2559 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2561 : wakelocks.entrySet()) {
2562 Uid.Wakelock wl = ent.getValue();
2563 String linePrefix = ": ";
2564 sb.setLength(0);
2565 sb.append(prefix);
2566 sb.append(" Wake lock ");
2567 sb.append(ent.getKey());
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002568 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 "full", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002570 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 "partial", which, linePrefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002572 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 "window", which, linePrefix);
2574 if (!linePrefix.equals(": ")) {
2575 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002576 // Only print out wake locks that were held
2577 pw.println(sb.toString());
2578 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002579 count++;
2580 }
2581 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002582 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002583 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002584 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002585 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002586 rawRealtime, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002587 }
2588 if (count > 1) {
2589 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2590 sb.setLength(0);
2591 sb.append(prefix);
2592 sb.append(" TOTAL wake: ");
2593 boolean needComma = false;
2594 if (totalFull != 0) {
2595 needComma = true;
2596 formatTimeMs(sb, totalFull);
2597 sb.append("full");
2598 }
2599 if (totalPartial != 0) {
2600 if (needComma) {
2601 sb.append(", ");
2602 }
2603 needComma = true;
2604 formatTimeMs(sb, totalPartial);
2605 sb.append("partial");
2606 }
2607 if (totalWindow != 0) {
2608 if (needComma) {
2609 sb.append(", ");
2610 }
2611 needComma = true;
2612 formatTimeMs(sb, totalWindow);
2613 sb.append("window");
2614 }
2615 sb.append(" realtime");
2616 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 }
2619 }
2620
2621 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2622 if (sensors.size() > 0) {
2623 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2624 : sensors.entrySet()) {
2625 Uid.Sensor se = ent.getValue();
2626 int sensorNumber = ent.getKey();
2627 sb.setLength(0);
2628 sb.append(prefix);
2629 sb.append(" Sensor ");
2630 int handle = se.getHandle();
2631 if (handle == Uid.Sensor.GPS) {
2632 sb.append("GPS");
2633 } else {
2634 sb.append(handle);
2635 }
2636 sb.append(": ");
2637
2638 Timer timer = se.getSensorTime();
2639 if (timer != null) {
2640 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002641 long totalTime = (timer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002642 rawRealtime, which) + 500) / 1000;
Evan Millarc64edde2009-04-18 12:26:32 -07002643 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 //timer.logState();
2645 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002646 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 sb.append("realtime (");
2648 sb.append(count);
2649 sb.append(" times)");
2650 } else {
2651 sb.append("(not used)");
2652 }
2653 } else {
2654 sb.append("(not used)");
2655 }
2656
2657 pw.println(sb.toString());
2658 uidActivity = true;
2659 }
2660 }
2661
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002662 Timer vibTimer = u.getVibratorOnTimer();
2663 if (vibTimer != null) {
2664 // Convert from microseconds to milliseconds with rounding
2665 long totalTime = (vibTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002666 rawRealtime, which) + 500) / 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002667 int count = vibTimer.getCountLocked(which);
2668 //timer.logState();
2669 if (totalTime != 0) {
2670 sb.setLength(0);
2671 sb.append(prefix);
2672 sb.append(" Vibrator: ");
2673 formatTimeMs(sb, totalTime);
2674 sb.append("realtime (");
2675 sb.append(count);
2676 sb.append(" times)");
2677 pw.println(sb.toString());
2678 uidActivity = true;
2679 }
2680 }
2681
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002682 Timer fgTimer = u.getForegroundActivityTimer();
2683 if (fgTimer != null) {
2684 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002685 long totalTime = (fgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002686 int count = fgTimer.getCountLocked(which);
2687 if (totalTime != 0) {
2688 sb.setLength(0);
2689 sb.append(prefix);
2690 sb.append(" Foreground activities: ");
2691 formatTimeMs(sb, totalTime);
2692 sb.append("realtime (");
2693 sb.append(count);
2694 sb.append(" times)");
2695 pw.println(sb.toString());
2696 uidActivity = true;
2697 }
2698 }
2699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2701 if (processStats.size() > 0) {
2702 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2703 : processStats.entrySet()) {
2704 Uid.Proc ps = ent.getValue();
2705 long userTime;
2706 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002707 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002709 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710
2711 userTime = ps.getUserTime(which);
2712 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002713 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002715 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002716 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002718 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002719 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002720 sb.setLength(0);
2721 sb.append(prefix); sb.append(" Proc ");
2722 sb.append(ent.getKey()); sb.append(":\n");
2723 sb.append(prefix); sb.append(" CPU: ");
2724 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002725 formatTime(sb, systemTime); sb.append("krn ; ");
2726 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002727 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002728 sb.append("\n"); sb.append(prefix); sb.append(" ");
2729 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002730 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002731 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002732 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002733 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002734 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002735 pw.print(prefix); pw.print(" * Killed for ");
2736 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2737 pw.print("wake lock");
2738 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2739 pw.print("cpu");
2740 } else {
2741 pw.print("unknown");
2742 }
2743 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002744 TimeUtils.formatDuration(ew.usedTime, pw);
2745 pw.print(" over ");
2746 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002747 if (ew.overTime != 0) {
2748 pw.print(" (");
2749 pw.print((ew.usedTime*100)/ew.overTime);
2750 pw.println("%)");
2751 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002752 }
2753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 uidActivity = true;
2755 }
2756 }
2757 }
2758
2759 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2760 if (packageStats.size() > 0) {
2761 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2762 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002763 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 boolean apkActivity = false;
2765 Uid.Pkg ps = ent.getValue();
2766 int wakeups = ps.getWakeups(which);
2767 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002768 pw.print(prefix); pw.print(" ");
2769 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 apkActivity = true;
2771 }
2772 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2773 if (serviceStats.size() > 0) {
2774 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2775 : serviceStats.entrySet()) {
2776 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2777 long startTime = ss.getStartTime(batteryUptime, which);
2778 int starts = ss.getStarts(which);
2779 int launches = ss.getLaunches(which);
2780 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002781 sb.setLength(0);
2782 sb.append(prefix); sb.append(" Service ");
2783 sb.append(sent.getKey()); sb.append(":\n");
2784 sb.append(prefix); sb.append(" Created for: ");
2785 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002786 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002787 sb.append(prefix); sb.append(" Starts: ");
2788 sb.append(starts);
2789 sb.append(", launches: "); sb.append(launches);
2790 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 apkActivity = true;
2792 }
2793 }
2794 }
2795 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002796 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 }
2798 uidActivity = true;
2799 }
2800 }
2801 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002802 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 }
2804 }
2805 }
2806
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002807 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002808 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002809 int diff = oldval ^ newval;
2810 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002811 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002812 for (int i=0; i<descriptions.length; i++) {
2813 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002814 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002815 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002816 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002817 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002818 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002819 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
2820 didWake = true;
2821 pw.print("=");
2822 if (longNames) {
2823 UserHandle.formatUid(pw, wakelockTag.uid);
2824 pw.print(":\"");
2825 pw.print(wakelockTag.string);
2826 pw.print("\"");
2827 } else {
2828 pw.print(wakelockTag.poolIdx);
2829 }
2830 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002831 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002832 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002833 pw.print("=");
2834 int val = (newval&bd.mask)>>bd.shift;
2835 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002836 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002837 } else {
2838 pw.print(val);
2839 }
2840 }
2841 }
2842 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002843 if (!didWake && wakelockTag != null) {
2844 pw.print(longNames ? "wake_lock=" : "w=");
2845 if (longNames) {
2846 UserHandle.formatUid(pw, wakelockTag.uid);
2847 pw.print(":\"");
2848 pw.print(wakelockTag.string);
2849 pw.print("\"");
2850 } else {
2851 pw.print(wakelockTag.poolIdx);
2852 }
2853 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002854 }
2855
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002856 public void prepareForDumpLocked() {
2857 }
2858
2859 public static class HistoryPrinter {
2860 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002861 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002862 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002863 int oldStatus = -1;
2864 int oldHealth = -1;
2865 int oldPlug = -1;
2866 int oldTemp = -1;
2867 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002868 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002869
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002870 public void printNextItem(PrintWriter pw, HistoryItem rec, long now, boolean checkin,
2871 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002872 if (!checkin) {
2873 pw.print(" ");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002874 if (now >= 0) {
2875 TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
2876 } else {
2877 TimeUtils.formatDuration(rec.time, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
2878 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002879 pw.print(" (");
2880 pw.print(rec.numReadInts);
2881 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002882 } else {
2883 if (lastTime < 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002884 if (now >= 0) {
2885 pw.print("@");
2886 pw.print(rec.time-now);
2887 } else {
2888 pw.print(rec.time);
2889 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002890 } else {
2891 pw.print(rec.time-lastTime);
2892 }
2893 lastTime = rec.time;
2894 }
2895 if (rec.cmd == HistoryItem.CMD_START) {
2896 if (checkin) {
2897 pw.print(":");
2898 }
2899 pw.println("START");
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002900 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME) {
2901 if (checkin) {
2902 pw.print(":");
2903 }
2904 pw.print("TIME:");
2905 if (checkin) {
2906 pw.println(rec.currentTime);
2907 } else {
2908 pw.print(" ");
2909 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
2910 rec.currentTime).toString());
2911 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002912 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2913 if (checkin) {
2914 pw.print(":");
2915 }
2916 pw.println("*OVERFLOW*");
2917 } else {
2918 if (!checkin) {
2919 if (rec.batteryLevel < 10) pw.print("00");
2920 else if (rec.batteryLevel < 100) pw.print("0");
2921 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002922 if (verbose) {
2923 pw.print(" ");
2924 if (rec.states < 0) ;
2925 else if (rec.states < 0x10) pw.print("0000000");
2926 else if (rec.states < 0x100) pw.print("000000");
2927 else if (rec.states < 0x1000) pw.print("00000");
2928 else if (rec.states < 0x10000) pw.print("0000");
2929 else if (rec.states < 0x100000) pw.print("000");
2930 else if (rec.states < 0x1000000) pw.print("00");
2931 else if (rec.states < 0x10000000) pw.print("0");
2932 pw.print(Integer.toHexString(rec.states));
2933 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002934 } else {
2935 if (oldLevel != rec.batteryLevel) {
2936 oldLevel = rec.batteryLevel;
2937 pw.print(",Bl="); pw.print(rec.batteryLevel);
2938 }
2939 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002940 if (oldStatus != rec.batteryStatus) {
2941 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002942 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002943 switch (oldStatus) {
2944 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002945 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002946 break;
2947 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002948 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002949 break;
2950 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002951 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002952 break;
2953 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002954 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002955 break;
2956 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002957 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002958 break;
2959 default:
2960 pw.print(oldStatus);
2961 break;
2962 }
2963 }
2964 if (oldHealth != rec.batteryHealth) {
2965 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002966 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002967 switch (oldHealth) {
2968 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002969 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002970 break;
2971 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002972 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002973 break;
2974 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002975 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002976 break;
2977 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002978 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002979 break;
2980 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002981 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002982 break;
2983 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002984 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002985 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002986 case BatteryManager.BATTERY_HEALTH_COLD:
2987 pw.print(checkin ? "c" : "cold");
2988 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002989 default:
2990 pw.print(oldHealth);
2991 break;
2992 }
2993 }
2994 if (oldPlug != rec.batteryPlugType) {
2995 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002996 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002997 switch (oldPlug) {
2998 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002999 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003000 break;
3001 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003002 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003003 break;
3004 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003005 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003006 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003007 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003008 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07003009 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003010 default:
3011 pw.print(oldPlug);
3012 break;
3013 }
3014 }
3015 if (oldTemp != rec.batteryTemperature) {
3016 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003017 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003018 pw.print(oldTemp);
3019 }
3020 if (oldVolt != rec.batteryVoltage) {
3021 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003022 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003023 pw.print(oldVolt);
3024 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003025 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003026 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003027 printBitDescriptions(pw, oldState2, rec.states2, null,
3028 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003029 if (rec.wakeReasonTag != null) {
3030 if (checkin) {
3031 pw.print(",Wr=");
3032 pw.print(rec.wakeReasonTag.poolIdx);
3033 } else {
3034 pw.print(" wake_reason=");
3035 pw.print(rec.wakeReasonTag.uid);
3036 pw.print(":\"");
3037 pw.print(rec.wakeReasonTag.string);
3038 pw.print("\"");
3039 }
3040 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003041 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003042 pw.print(checkin ? "," : " ");
3043 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
3044 pw.print("+");
3045 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
3046 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003047 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003048 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
3049 : HISTORY_EVENT_NAMES;
3050 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
3051 | HistoryItem.EVENT_FLAG_FINISH);
3052 if (idx >= 0 && idx < eventNames.length) {
3053 pw.print(eventNames[idx]);
3054 } else {
3055 pw.print(checkin ? "Ev" : "event");
3056 pw.print(idx);
3057 }
3058 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003059 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003060 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003061 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003062 UserHandle.formatUid(pw, rec.eventTag.uid);
3063 pw.print(":\"");
3064 pw.print(rec.eventTag.string);
3065 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08003066 }
3067 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003068 pw.println();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003069 oldState = rec.states;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003070 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003071 }
3072 }
3073
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003074 private void printSizeValue(PrintWriter pw, long size) {
3075 float result = size;
3076 String suffix = "";
3077 if (result >= 10*1024) {
3078 suffix = "KB";
3079 result = result / 1024;
3080 }
3081 if (result >= 10*1024) {
3082 suffix = "MB";
3083 result = result / 1024;
3084 }
3085 if (result >= 10*1024) {
3086 suffix = "GB";
3087 result = result / 1024;
3088 }
3089 if (result >= 10*1024) {
3090 suffix = "TB";
3091 result = result / 1024;
3092 }
3093 if (result >= 10*1024) {
3094 suffix = "PB";
3095 result = result / 1024;
3096 }
3097 pw.print((int)result);
3098 pw.print(suffix);
3099 }
3100
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003101 public static final int DUMP_UNPLUGGED_ONLY = 1<<0;
3102 public static final int DUMP_CHARGED_ONLY = 1<<1;
3103 public static final int DUMP_HISTORY_ONLY = 1<<2;
3104 public static final int DUMP_INCLUDE_HISTORY = 1<<3;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003105 public static final int DUMP_VERBOSE = 1<<4;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 /**
3108 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
3109 *
3110 * @param pw a Printer to receive the dump output.
3111 */
3112 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003113 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003114 prepareForDumpLocked();
3115
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003116 final boolean filtering =
3117 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003118
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003119 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
3120 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3121
3122 final HistoryItem rec = new HistoryItem();
3123 final long historyTotalSize = getHistoryTotalSize();
3124 final long historyUsedSize = getHistoryUsedSize();
3125 if (startIteratingHistoryLocked()) {
3126 try {
3127 pw.print("Battery History (");
3128 pw.print((100*historyUsedSize)/historyTotalSize);
3129 pw.print("% used, ");
3130 printSizeValue(pw, historyUsedSize);
3131 pw.print(" used of ");
3132 printSizeValue(pw, historyTotalSize);
3133 pw.print(", ");
3134 pw.print(getHistoryStringPoolSize());
3135 pw.print(" strings using ");
3136 printSizeValue(pw, getHistoryStringPoolBytes());
3137 pw.println("):");
3138 HistoryPrinter hprinter = new HistoryPrinter();
3139 long lastTime = -1;
3140 while (getNextHistoryLocked(rec)) {
3141 lastTime = rec.time;
3142 if (rec.time >= histStart) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003143 hprinter.printNextItem(pw, rec, histStart >= 0 ? -1 : now, false,
3144 (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003145 }
3146 }
3147 if (histStart >= 0) {
3148 pw.print(" NEXT: "); pw.println(lastTime+1);
3149 }
3150 pw.println();
3151 } finally {
3152 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003153 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003154 }
3155
3156 if (startIteratingOldHistoryLocked()) {
3157 try {
3158 pw.println("Old battery History:");
3159 HistoryPrinter hprinter = new HistoryPrinter();
3160 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003161 hprinter.printNextItem(pw, rec, now, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003162 }
3163 pw.println();
3164 } finally {
3165 finishIteratingOldHistoryLocked();
3166 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003167 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003168 }
3169
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003170 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003171 return;
3172 }
3173
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003174 if (!filtering) {
3175 SparseArray<? extends Uid> uidStats = getUidStats();
3176 final int NU = uidStats.size();
3177 boolean didPid = false;
3178 long nowRealtime = SystemClock.elapsedRealtime();
3179 for (int i=0; i<NU; i++) {
3180 Uid uid = uidStats.valueAt(i);
3181 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
3182 if (pids != null) {
3183 for (int j=0; j<pids.size(); j++) {
3184 Uid.Pid pid = pids.valueAt(j);
3185 if (!didPid) {
3186 pw.println("Per-PID Stats:");
3187 didPid = true;
3188 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003189 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
3190 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003191 pw.print(" PID "); pw.print(pids.keyAt(j));
3192 pw.print(" wake time: ");
3193 TimeUtils.formatDuration(time, pw);
3194 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003195 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07003196 }
3197 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003198 if (didPid) {
3199 pw.println("");
3200 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003201 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003202
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003203 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003204 pw.println("Statistics since last charge:");
3205 pw.println(" System starts: " + getStartCount()
3206 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003207 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07003208 pw.println("");
3209 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003210 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
3211 pw.println("Statistics since last unplugged:");
3212 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
3213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 }
3215
3216 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003217 public void dumpCheckinLocked(Context context, PrintWriter pw,
3218 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003219 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003221 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
3222
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003223 final boolean filtering =
3224 (flags&(DUMP_HISTORY_ONLY|DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) != 0;
3225
3226 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07003227 final HistoryItem rec = new HistoryItem();
3228 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003229 try {
3230 for (int i=0; i<getHistoryStringPoolSize(); i++) {
3231 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3232 pw.print(HISTORY_STRING_POOL); pw.print(',');
3233 pw.print(i);
3234 pw.print(',');
3235 pw.print(getHistoryTagPoolString(i));
3236 pw.print(',');
3237 pw.print(getHistoryTagPoolUid(i));
3238 pw.println();
3239 }
3240 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003241 long lastTime = -1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003242 while (getNextHistoryLocked(rec)) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003243 lastTime = rec.time;
3244 if (rec.time >= histStart) {
3245 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
3246 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003247 hprinter.printNextItem(pw, rec, histStart >= 0 ? -1 : now, true, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003248 }
3249 }
3250 if (histStart >= 0) {
3251 pw.print("NEXT: "); pw.println(lastTime+1);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003252 }
3253 } finally {
3254 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003255 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003256 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07003257 }
3258
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003259 if (filtering && (flags&(DUMP_UNPLUGGED_ONLY|DUMP_CHARGED_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003260 return;
3261 }
3262
Dianne Hackborne4a59512010-12-07 11:08:07 -08003263 if (apps != null) {
3264 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
3265 for (int i=0; i<apps.size(); i++) {
3266 ApplicationInfo ai = apps.get(i);
3267 ArrayList<String> pkgs = uids.get(ai.uid);
3268 if (pkgs == null) {
3269 pkgs = new ArrayList<String>();
3270 uids.put(ai.uid, pkgs);
3271 }
3272 pkgs.add(ai.packageName);
3273 }
3274 SparseArray<? extends Uid> uidStats = getUidStats();
3275 final int NU = uidStats.size();
3276 String[] lineArgs = new String[2];
3277 for (int i=0; i<NU; i++) {
3278 int uid = uidStats.keyAt(i);
3279 ArrayList<String> pkgs = uids.get(uid);
3280 if (pkgs != null) {
3281 for (int j=0; j<pkgs.size(); j++) {
3282 lineArgs[0] = Integer.toString(uid);
3283 lineArgs[1] = pkgs.get(j);
3284 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
3285 (Object[])lineArgs);
3286 }
3287 }
3288 }
3289 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003290 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003291 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003292 }
3293 if (!filtering || (flags&DUMP_UNPLUGGED_ONLY) != 0) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003294 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 }
3296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297}