blob: 235f24cd3602f8f9dcf3cf33a401c1bdb683900c [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 Hackborn37de0982014-05-09 09:32:18 -070024import java.util.HashMap;
Dianne Hackborne4a59512010-12-07 11:08:07 -080025import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import java.util.Map;
27
Dianne Hackborna7c837f2014-01-15 16:20:44 -080028import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080029import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080030import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080031import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070032import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070033import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070034import android.util.MutableBoolean;
35import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.util.Printer;
37import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070038import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070039import android.util.TimeUtils;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070040import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080041
Dianne Hackborna7c837f2014-01-15 16:20:44 -080042import com.android.internal.os.BatterySipper;
43import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45/**
46 * A class providing access to battery usage statistics, including information on
47 * wakelocks, processes, packages, and services. All times are represented in microseconds
48 * except where indicated otherwise.
49 * @hide
50 */
51public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070052 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070055
56 /** @hide */
57 public static final String SERVICE_NAME = "batterystats";
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 /**
60 * A constant indicating a partial wake lock timer.
61 */
62 public static final int WAKE_TYPE_PARTIAL = 0;
63
64 /**
65 * A constant indicating a full wake lock timer.
66 */
67 public static final int WAKE_TYPE_FULL = 1;
68
69 /**
70 * A constant indicating a window wake lock timer.
71 */
72 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 /**
75 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 */
77 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070078
79 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070080 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070081 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070082 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070083
84 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070085 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070086 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070087 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070088
89 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070090 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070091 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070092 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
Dianne Hackborn62793e42015-03-09 11:15:41 -070094 /**
95 * A constant indicating a wifi multicast timer
96 */
97 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -070098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700100 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700101 */
102 public static final int VIDEO_TURNED_ON = 8;
103
104 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800105 * A constant indicating a vibrator on timer
106 */
107 public static final int VIBRATOR_ON = 9;
108
109 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700110 * A constant indicating a foreground activity timer
111 */
112 public static final int FOREGROUND_ACTIVITY = 10;
113
114 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700115 * A constant indicating a wifi batched scan is active
116 */
117 public static final int WIFI_BATCHED_SCAN = 11;
118
119 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700120 * A constant indicating a process state timer
121 */
122 public static final int PROCESS_STATE = 12;
123
124 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700125 * A constant indicating a sync timer
126 */
127 public static final int SYNC = 13;
128
129 /**
130 * A constant indicating a job timer
131 */
132 public static final int JOB = 14;
133
134 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800135 * A constant indicating an audio turn on timer
136 */
137 public static final int AUDIO_TURNED_ON = 15;
138
139 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700140 * A constant indicating a flashlight turn on timer
141 */
142 public static final int FLASHLIGHT_TURNED_ON = 16;
143
144 /**
145 * A constant indicating a camera turn on timer
146 */
147 public static final int CAMERA_TURNED_ON = 17;
148
149 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700150 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700151 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700152 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700153
154 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800155 * A constant indicating a bluetooth scan timer.
156 */
157 public static final int BLUETOOTH_SCAN_ON = 19;
158
159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 * Include all of the data in the stats, including previously saved data.
161 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700162 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 * Include only the current run in the stats.
166 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700167 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
169 /**
170 * Include only the run since the last time the device was unplugged in the stats.
171 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700172 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700173
174 // NOTE: Update this list if you add/change any stats above.
175 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700176 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700177 private static final String[] STAT_NAMES = { "l", "c", "u" };
178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700180 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700181 *
182 * New in version 19:
183 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800184 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700185 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700186 */
Kweku Adams47db5a82016-12-09 19:04:50 -0800187 static final String CHECKIN_VERSION = "20";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700188
189 /**
190 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700192 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700193
Evan Millar22ac0432009-03-31 11:33:18 -0700194 private static final long BYTES_PER_KB = 1024;
195 private static final long BYTES_PER_MB = 1048576; // 1024^2
196 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700198 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800199 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700200 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700202 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700203 private static final String CPU_DATA = "cpu";
Evan Millare84de8d2009-04-02 22:16:12 -0700204 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800205 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700206 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700207 private static final String STATE_TIME_DATA = "st";
Evan Millare84de8d2009-04-02 22:16:12 -0700208 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700209 private static final String SYNC_DATA = "sy";
210 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700211 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700212 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700213 private static final String NETWORK_DATA = "nt";
214 private static final String USER_ACTIVITY_DATA = "ua";
215 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800216 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700217 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700218 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700219 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800220 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
221 private static final String WIFI_CONTROLLER_DATA = "wfcd";
222 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
223 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700224 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700225 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800226 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800227 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
228 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800229 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700230 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700231 private static final String SCREEN_BRIGHTNESS_DATA = "br";
232 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700233 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700234 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
235 private static final String DATA_CONNECTION_TIME_DATA = "dct";
236 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800237 private static final String WIFI_STATE_TIME_DATA = "wst";
238 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700239 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
240 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
241 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
242 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800243 private static final String POWER_USE_SUMMARY_DATA = "pws";
244 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700245 private static final String DISCHARGE_STEP_DATA = "dsd";
246 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700247 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
248 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700249 private static final String FLASHLIGHT_DATA = "fla";
250 private static final String CAMERA_DATA = "cam";
251 private static final String VIDEO_DATA = "vid";
252 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Adam Lesinski010bf372016-04-11 12:18:18 -0700254 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
255
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700256 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 private final Formatter mFormatter = new Formatter(mFormatBuilder);
258
259 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700260 * State for keeping track of counting information.
261 */
262 public static abstract class Counter {
263
264 /**
265 * Returns the count associated with this Counter for the
266 * selected type of statistics.
267 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700268 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700269 */
Evan Millarc64edde2009-04-18 12:26:32 -0700270 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700271
272 /**
273 * Temporary for debugging.
274 */
275 public abstract void logState(Printer pw, String prefix);
276 }
277
278 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700279 * State for keeping track of long counting information.
280 */
281 public static abstract class LongCounter {
282
283 /**
284 * Returns the count associated with this Counter for the
285 * selected type of statistics.
286 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700287 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700288 */
289 public abstract long getCountLocked(int which);
290
291 /**
292 * Temporary for debugging.
293 */
294 public abstract void logState(Printer pw, String prefix);
295 }
296
297 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800298 * Container class that aggregates counters for transmit, receive, and idle state of a
299 * radio controller.
300 */
301 public static abstract class ControllerActivityCounter {
302 /**
303 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
304 * idle state.
305 */
306 public abstract LongCounter getIdleTimeCounter();
307
308 /**
309 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
310 * receive state.
311 */
312 public abstract LongCounter getRxTimeCounter();
313
314 /**
315 * An array of {@link LongCounter}, representing various transmit levels, where each level
316 * may draw a different amount of power. The levels themselves are controller-specific.
317 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
318 * various transmit level states.
319 */
320 public abstract LongCounter[] getTxTimeCounters();
321
322 /**
323 * @return a non-null {@link LongCounter} representing the power consumed by the controller
324 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
325 * yield a value of 0 if the device doesn't support power calculations.
326 */
327 public abstract LongCounter getPowerCounter();
328 }
329
330 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 * State for keeping track of timing information.
332 */
333 public static abstract class Timer {
334
335 /**
336 * Returns the count associated with this Timer for the
337 * selected type of statistics.
338 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700339 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 */
Evan Millarc64edde2009-04-18 12:26:32 -0700341 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342
343 /**
344 * Returns the total time in microseconds associated with this Timer for the
345 * selected type of statistics.
346 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800347 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700348 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * @return a time in microseconds
350 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800351 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700354 * Returns the total time in microseconds associated with this Timer since the
355 * 'mark' was last set.
356 *
357 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
358 * @return a time in microseconds
359 */
360 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
361
362 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700363 * Returns the max duration if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800364 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700365
366 */
367 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
368 return -1;
369 }
370
371 /**
372 * Returns the current time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800373 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700374 */
375 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
376 return -1;
377 }
378
379 /**
Bookatz867c0d72017-03-07 18:23:42 -0800380 * Returns the current time the timer has been active, if it is being tracked.
381 *
382 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
383 * been on since reset.
384 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
385 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
386 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
387 * the actual total time.
388 * Not all Timer subclasses track the max, total, current durations.
389 */
390 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
391 return -1;
392 }
393
394 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700395 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
396 * used, for example, for tracking background usage. Secondary timers are never pooled.
397 *
398 * Not all Timer subclasses have a secondary timer; those that don't return null.
399 */
400 public Timer getSubTimer() {
401 return null;
402 }
403
404 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700405 * Returns whether the timer is currently running. Some types of timers
406 * (e.g. BatchTimers) don't know whether the event is currently active,
407 * and report false.
408 */
409 public boolean isRunningLocked() {
410 return false;
411 }
412
413 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 * Temporary for debugging.
415 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700416 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418
419 /**
420 * The statistics associated with a particular uid.
421 */
422 public static abstract class Uid {
423
424 /**
425 * Returns a mapping containing wakelock statistics.
426 *
427 * @return a Map from Strings to Uid.Wakelock objects.
428 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700429 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430
431 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700432 * Returns a mapping containing sync statistics.
433 *
434 * @return a Map from Strings to Timer objects.
435 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700436 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700437
438 /**
439 * Returns a mapping containing scheduled job statistics.
440 *
441 * @return a Map from Strings to Timer objects.
442 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700443 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700444
445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 * The statistics associated with a particular wake lock.
447 */
448 public static abstract class Wakelock {
449 public abstract Timer getWakeTime(int type);
450 }
451
452 /**
453 * Returns a mapping containing sensor statistics.
454 *
455 * @return a Map from Integer sensor ids to Uid.Sensor objects.
456 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700457 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458
459 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700460 * Returns a mapping containing active process data.
461 */
462 public abstract SparseArray<? extends Pid> getPidStats();
463
464 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 * Returns a mapping containing process statistics.
466 *
467 * @return a Map from Strings to Uid.Proc objects.
468 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700469 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470
471 /**
472 * Returns a mapping containing package statistics.
473 *
474 * @return a Map from Strings to Uid.Pkg objects.
475 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700476 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700477
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800478 public abstract ControllerActivityCounter getWifiControllerActivity();
479 public abstract ControllerActivityCounter getBluetoothControllerActivity();
480 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800481
482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 * {@hide}
484 */
485 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700486
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800487 public abstract void noteWifiRunningLocked(long elapsedRealtime);
488 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
489 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
490 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
491 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
492 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
493 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
494 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
495 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
496 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800497 public abstract void noteActivityResumedLocked(long elapsedRealtime);
498 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800499 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
500 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
501 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700502 public abstract int getWifiScanCount(int which);
Bookatz867c0d72017-03-07 18:23:42 -0800503 public abstract int getWifiScanBackgroundCount(int which);
504 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
505 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800506 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700507 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800508 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700509 public abstract Timer getAudioTurnedOnTimer();
510 public abstract Timer getVideoTurnedOnTimer();
511 public abstract Timer getFlashlightTurnedOnTimer();
512 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700513 public abstract Timer getForegroundActivityTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800514 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800515 public abstract Timer getBluetoothScanBackgroundTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700516
Dianne Hackborna0200e32016-03-30 18:01:41 -0700517 // Note: the following times are disjoint. They can be added together to find the
518 // total time a uid has had any processes running at all.
519
520 /**
521 * Time this uid has any processes in the top state (or above such as persistent).
522 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800523 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700524 /**
525 * Time this uid has any process with a started out bound foreground service, but
526 * none in the "top" state.
527 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800528 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700529 /**
530 * Time this uid has any process that is top while the device is sleeping, but none
531 * in the "foreground service" or better state.
532 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800533 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700534 /**
535 * Time this uid has any process in an active foreground state, but none in the
536 * "top sleeping" or better state.
537 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800538 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700539 /**
540 * Time this uid has any process in an active background state, but none in the
541 * "foreground" or better state.
542 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800543 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700544 /**
545 * Time this uid has any processes that are sitting around cached, not in one of the
546 * other active states.
547 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800548 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700549 /**
550 * Total number of process states we track.
551 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800552 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700553
554 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800555 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700556 };
557
558 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800559 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700560
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800561 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562
Robert Greenwalta029ea12013-09-25 16:38:12 -0700563 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
564
Dianne Hackborn617f8772009-03-31 15:04:46 -0700565 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700566 * Note that these must match the constants in android.os.PowerManager.
567 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
568 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700569 */
570 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700571 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700572 };
573
Phil Weaverda80d672016-03-15 16:25:46 -0700574 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700575
Dianne Hackborn617f8772009-03-31 15:04:46 -0700576 public abstract void noteUserActivityLocked(int type);
577 public abstract boolean hasUserActivity();
578 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700579
580 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800581 public abstract long getNetworkActivityBytes(int type, int which);
582 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800583 public abstract long getMobileRadioActiveTime(int which);
584 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700585
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700586 /**
587 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
588 */
589 public abstract long getUserCpuTimeUs(int which);
590
591 /**
592 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
593 */
594 public abstract long getSystemCpuTimeUs(int which);
595
596 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700597 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
598 * given CPU cluster.
599 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700600 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700601 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700602 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
603 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700604 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700605 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700606
Adam Lesinski5f056f62016-07-14 16:56:08 -0700607 /**
608 * Returns the number of times this UID woke up the Application Processor to
609 * process a mobile radio packet.
610 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
611 */
612 public abstract long getMobileRadioApWakeupCount(int which);
613
614 /**
615 * Returns the number of times this UID woke up the Application Processor to
616 * process a WiFi packet.
617 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
618 */
619 public abstract long getWifiRadioApWakeupCount(int which);
620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800622 /*
623 * FIXME: it's not correct to use this magic value because it
624 * could clash with a sensor handle (which are defined by
625 * the sensor HAL, and therefore out of our control
626 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 // Magic sensor number for the GPS.
628 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800633
Bookatz867c0d72017-03-07 18:23:42 -0800634 /** Returns a Timer for sensor usage when app is in the background. */
635 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
637
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700638 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800639 public int mWakeNesting;
640 public long mWakeSumMs;
641 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700642 }
643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 /**
645 * The statistics associated with a particular process.
646 */
647 public static abstract class Proc {
648
Dianne Hackborn287952c2010-09-22 22:34:31 -0700649 public static class ExcessivePower {
650 public static final int TYPE_WAKE = 1;
651 public static final int TYPE_CPU = 2;
652
653 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700654 public long overTime;
655 public long usedTime;
656 }
657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800659 * Returns true if this process is still active in the battery stats.
660 */
661 public abstract boolean isActive();
662
663 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700664 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700666 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 */
668 public abstract long getUserTime(int which);
669
670 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700671 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700673 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 */
675 public abstract long getSystemTime(int which);
676
677 /**
678 * Returns the number of times the process has been started.
679 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700680 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 */
682 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700683
684 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800685 * Returns the number of times the process has crashed.
686 *
687 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
688 */
689 public abstract int getNumCrashes(int which);
690
691 /**
692 * Returns the number of times the process has ANRed.
693 *
694 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
695 */
696 public abstract int getNumAnrs(int which);
697
698 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700699 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700700 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700701 * @return foreground cpu time in microseconds
702 */
703 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700704
Dianne Hackborn287952c2010-09-22 22:34:31 -0700705 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700706
Dianne Hackborn287952c2010-09-22 22:34:31 -0700707 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 }
709
710 /**
711 * The statistics associated with a particular package.
712 */
713 public static abstract class Pkg {
714
715 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700716 * Returns information about all wakeup alarms that have been triggered for this
717 * package. The mapping keys are tag names for the alarms, the counter contains
718 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700720 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721
722 /**
723 * Returns a mapping containing service statistics.
724 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700725 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
727 /**
728 * The statistics associated with a particular service.
729 */
Joe Onoratoabded112016-02-08 16:49:39 -0800730 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731
732 /**
733 * Returns the amount of time spent started.
734 *
735 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700736 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 * @return
738 */
739 public abstract long getStartTime(long batteryUptime, int which);
740
741 /**
742 * Returns the total number of times startService() has been called.
743 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700744 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 */
746 public abstract int getStarts(int which);
747
748 /**
749 * Returns the total number times the service has been launched.
750 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700751 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 */
753 public abstract int getLaunches(int which);
754 }
755 }
756 }
757
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800758 public static final class LevelStepTracker {
759 public long mLastStepTime = -1;
760 public int mNumStepDurations;
761 public final long[] mStepDurations;
762
763 public LevelStepTracker(int maxLevelSteps) {
764 mStepDurations = new long[maxLevelSteps];
765 }
766
767 public LevelStepTracker(int numSteps, long[] steps) {
768 mNumStepDurations = numSteps;
769 mStepDurations = new long[numSteps];
770 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
771 }
772
773 public long getDurationAt(int index) {
774 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
775 }
776
777 public int getLevelAt(int index) {
778 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
779 >> STEP_LEVEL_LEVEL_SHIFT);
780 }
781
782 public int getInitModeAt(int index) {
783 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
784 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
785 }
786
787 public int getModModeAt(int index) {
788 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
789 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
790 }
791
792 private void appendHex(long val, int topOffset, StringBuilder out) {
793 boolean hasData = false;
794 while (topOffset >= 0) {
795 int digit = (int)( (val>>topOffset) & 0xf );
796 topOffset -= 4;
797 if (!hasData && digit == 0) {
798 continue;
799 }
800 hasData = true;
801 if (digit >= 0 && digit <= 9) {
802 out.append((char)('0' + digit));
803 } else {
804 out.append((char)('a' + digit - 10));
805 }
806 }
807 }
808
809 public void encodeEntryAt(int index, StringBuilder out) {
810 long item = mStepDurations[index];
811 long duration = item & STEP_LEVEL_TIME_MASK;
812 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
813 >> STEP_LEVEL_LEVEL_SHIFT);
814 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
815 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
816 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
817 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
818 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
819 case Display.STATE_OFF: out.append('f'); break;
820 case Display.STATE_ON: out.append('o'); break;
821 case Display.STATE_DOZE: out.append('d'); break;
822 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
823 }
824 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
825 out.append('p');
826 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700827 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
828 out.append('i');
829 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800830 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
831 case Display.STATE_OFF: out.append('F'); break;
832 case Display.STATE_ON: out.append('O'); break;
833 case Display.STATE_DOZE: out.append('D'); break;
834 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
835 }
836 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
837 out.append('P');
838 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700839 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
840 out.append('I');
841 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800842 out.append('-');
843 appendHex(level, 4, out);
844 out.append('-');
845 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
846 }
847
848 public void decodeEntryAt(int index, String value) {
849 final int N = value.length();
850 int i = 0;
851 char c;
852 long out = 0;
853 while (i < N && (c=value.charAt(i)) != '-') {
854 i++;
855 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800856 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800857 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800858 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800859 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800860 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800861 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800862 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
863 << STEP_LEVEL_INITIAL_MODE_SHIFT);
864 break;
865 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
866 << STEP_LEVEL_INITIAL_MODE_SHIFT);
867 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700868 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
869 << STEP_LEVEL_INITIAL_MODE_SHIFT);
870 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800871 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
872 break;
873 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
874 break;
875 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
876 break;
877 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
878 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
879 break;
880 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
881 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800882 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700883 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
884 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
885 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800886 }
887 }
888 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800889 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800890 while (i < N && (c=value.charAt(i)) != '-') {
891 i++;
892 level <<= 4;
893 if (c >= '0' && c <= '9') {
894 level += c - '0';
895 } else if (c >= 'a' && c <= 'f') {
896 level += c - 'a' + 10;
897 } else if (c >= 'A' && c <= 'F') {
898 level += c - 'A' + 10;
899 }
900 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800901 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800902 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
903 long duration = 0;
904 while (i < N && (c=value.charAt(i)) != '-') {
905 i++;
906 duration <<= 4;
907 if (c >= '0' && c <= '9') {
908 duration += c - '0';
909 } else if (c >= 'a' && c <= 'f') {
910 duration += c - 'a' + 10;
911 } else if (c >= 'A' && c <= 'F') {
912 duration += c - 'A' + 10;
913 }
914 }
915 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
916 }
917
918 public void init() {
919 mLastStepTime = -1;
920 mNumStepDurations = 0;
921 }
922
923 public void clearTime() {
924 mLastStepTime = -1;
925 }
926
927 public long computeTimePerLevel() {
928 final long[] steps = mStepDurations;
929 final int numSteps = mNumStepDurations;
930
931 // For now we'll do a simple average across all steps.
932 if (numSteps <= 0) {
933 return -1;
934 }
935 long total = 0;
936 for (int i=0; i<numSteps; i++) {
937 total += steps[i] & STEP_LEVEL_TIME_MASK;
938 }
939 return total / numSteps;
940 /*
941 long[] buckets = new long[numSteps];
942 int numBuckets = 0;
943 int numToAverage = 4;
944 int i = 0;
945 while (i < numSteps) {
946 long totalTime = 0;
947 int num = 0;
948 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
949 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
950 num++;
951 }
952 buckets[numBuckets] = totalTime / num;
953 numBuckets++;
954 numToAverage *= 2;
955 i += num;
956 }
957 if (numBuckets < 1) {
958 return -1;
959 }
960 long averageTime = buckets[numBuckets-1];
961 for (i=numBuckets-2; i>=0; i--) {
962 averageTime = (averageTime + buckets[i]) / 2;
963 }
964 return averageTime;
965 */
966 }
967
968 public long computeTimeEstimate(long modesOfInterest, long modeValues,
969 int[] outNumOfInterest) {
970 final long[] steps = mStepDurations;
971 final int count = mNumStepDurations;
972 if (count <= 0) {
973 return -1;
974 }
975 long total = 0;
976 int numOfInterest = 0;
977 for (int i=0; i<count; i++) {
978 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
979 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
980 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
981 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
982 // If the modes of interest didn't change during this step period...
983 if ((modMode&modesOfInterest) == 0) {
984 // And the mode values during this period match those we are measuring...
985 if ((initMode&modesOfInterest) == modeValues) {
986 // Then this can be used to estimate the total time!
987 numOfInterest++;
988 total += steps[i] & STEP_LEVEL_TIME_MASK;
989 }
990 }
991 }
992 if (numOfInterest <= 0) {
993 return -1;
994 }
995
996 if (outNumOfInterest != null) {
997 outNumOfInterest[0] = numOfInterest;
998 }
999
1000 // The estimated time is the average time we spend in each level, multipled
1001 // by 100 -- the total number of battery levels
1002 return (total / numOfInterest) * 100;
1003 }
1004
1005 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1006 int stepCount = mNumStepDurations;
1007 final long lastStepTime = mLastStepTime;
1008 if (lastStepTime >= 0 && numStepLevels > 0) {
1009 final long[] steps = mStepDurations;
1010 long duration = elapsedRealtime - lastStepTime;
1011 for (int i=0; i<numStepLevels; i++) {
1012 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1013 long thisDuration = duration / (numStepLevels-i);
1014 duration -= thisDuration;
1015 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1016 thisDuration = STEP_LEVEL_TIME_MASK;
1017 }
1018 steps[0] = thisDuration | modeBits;
1019 }
1020 stepCount += numStepLevels;
1021 if (stepCount > steps.length) {
1022 stepCount = steps.length;
1023 }
1024 }
1025 mNumStepDurations = stepCount;
1026 mLastStepTime = elapsedRealtime;
1027 }
1028
1029 public void readFromParcel(Parcel in) {
1030 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001031 if (N > mStepDurations.length) {
1032 throw new ParcelFormatException("more step durations than available: " + N);
1033 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001034 mNumStepDurations = N;
1035 for (int i=0; i<N; i++) {
1036 mStepDurations[i] = in.readLong();
1037 }
1038 }
1039
1040 public void writeToParcel(Parcel out) {
1041 final int N = mNumStepDurations;
1042 out.writeInt(N);
1043 for (int i=0; i<N; i++) {
1044 out.writeLong(mStepDurations[i]);
1045 }
1046 }
1047 }
1048
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001049 public static final class PackageChange {
1050 public String mPackageName;
1051 public boolean mUpdate;
1052 public int mVersionCode;
1053 }
1054
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001055 public static final class DailyItem {
1056 public long mStartTime;
1057 public long mEndTime;
1058 public LevelStepTracker mDischargeSteps;
1059 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001060 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001061 }
1062
1063 public abstract DailyItem getDailyItemLocked(int daysAgo);
1064
1065 public abstract long getCurrentDailyStartTime();
1066
1067 public abstract long getNextMinDailyDeadline();
1068
1069 public abstract long getNextMaxDailyDeadline();
1070
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001071 public final static class HistoryTag {
1072 public String string;
1073 public int uid;
1074
1075 public int poolIdx;
1076
1077 public void setTo(HistoryTag o) {
1078 string = o.string;
1079 uid = o.uid;
1080 poolIdx = o.poolIdx;
1081 }
1082
1083 public void setTo(String _string, int _uid) {
1084 string = _string;
1085 uid = _uid;
1086 poolIdx = -1;
1087 }
1088
1089 public void writeToParcel(Parcel dest, int flags) {
1090 dest.writeString(string);
1091 dest.writeInt(uid);
1092 }
1093
1094 public void readFromParcel(Parcel src) {
1095 string = src.readString();
1096 uid = src.readInt();
1097 poolIdx = -1;
1098 }
1099
1100 @Override
1101 public boolean equals(Object o) {
1102 if (this == o) return true;
1103 if (o == null || getClass() != o.getClass()) return false;
1104
1105 HistoryTag that = (HistoryTag) o;
1106
1107 if (uid != that.uid) return false;
1108 if (!string.equals(that.string)) return false;
1109
1110 return true;
1111 }
1112
1113 @Override
1114 public int hashCode() {
1115 int result = string.hashCode();
1116 result = 31 * result + uid;
1117 return result;
1118 }
1119 }
1120
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001121 /**
1122 * Optional detailed information that can go into a history step. This is typically
1123 * generated each time the battery level changes.
1124 */
1125 public final static class HistoryStepDetails {
1126 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1127 public int userTime;
1128 public int systemTime;
1129
1130 // Top three apps using CPU in the last step, with times in 1/100 second.
1131 public int appCpuUid1;
1132 public int appCpuUTime1;
1133 public int appCpuSTime1;
1134 public int appCpuUid2;
1135 public int appCpuUTime2;
1136 public int appCpuSTime2;
1137 public int appCpuUid3;
1138 public int appCpuUTime3;
1139 public int appCpuSTime3;
1140
1141 // Information from /proc/stat
1142 public int statUserTime;
1143 public int statSystemTime;
1144 public int statIOWaitTime;
1145 public int statIrqTime;
1146 public int statSoftIrqTime;
1147 public int statIdlTime;
1148
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001149 // Platform-level low power state stats
1150 public String statPlatformIdleState;
1151
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001152 public HistoryStepDetails() {
1153 clear();
1154 }
1155
1156 public void clear() {
1157 userTime = systemTime = 0;
1158 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1159 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1160 = appCpuUTime3 = appCpuSTime3 = 0;
1161 }
1162
1163 public void writeToParcel(Parcel out) {
1164 out.writeInt(userTime);
1165 out.writeInt(systemTime);
1166 out.writeInt(appCpuUid1);
1167 out.writeInt(appCpuUTime1);
1168 out.writeInt(appCpuSTime1);
1169 out.writeInt(appCpuUid2);
1170 out.writeInt(appCpuUTime2);
1171 out.writeInt(appCpuSTime2);
1172 out.writeInt(appCpuUid3);
1173 out.writeInt(appCpuUTime3);
1174 out.writeInt(appCpuSTime3);
1175 out.writeInt(statUserTime);
1176 out.writeInt(statSystemTime);
1177 out.writeInt(statIOWaitTime);
1178 out.writeInt(statIrqTime);
1179 out.writeInt(statSoftIrqTime);
1180 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001181 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001182 }
1183
1184 public void readFromParcel(Parcel in) {
1185 userTime = in.readInt();
1186 systemTime = in.readInt();
1187 appCpuUid1 = in.readInt();
1188 appCpuUTime1 = in.readInt();
1189 appCpuSTime1 = in.readInt();
1190 appCpuUid2 = in.readInt();
1191 appCpuUTime2 = in.readInt();
1192 appCpuSTime2 = in.readInt();
1193 appCpuUid3 = in.readInt();
1194 appCpuUTime3 = in.readInt();
1195 appCpuSTime3 = in.readInt();
1196 statUserTime = in.readInt();
1197 statSystemTime = in.readInt();
1198 statIOWaitTime = in.readInt();
1199 statIrqTime = in.readInt();
1200 statSoftIrqTime = in.readInt();
1201 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001202 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001203 }
1204 }
1205
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001206 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001207 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001208
1209 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001210 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001211
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001212 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001213 public static final byte CMD_NULL = -1;
1214 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001215 public static final byte CMD_CURRENT_TIME = 5;
1216 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001217 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001218 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001219
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001220 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001221
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001222 /**
1223 * Return whether the command code is a delta data update.
1224 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001225 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001226 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001227 }
1228
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001229 public byte batteryLevel;
1230 public byte batteryStatus;
1231 public byte batteryHealth;
1232 public byte batteryPlugType;
1233
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001234 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001235 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001236
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001237 // The charge of the battery in micro-Ampere-hours.
1238 public int batteryChargeUAh;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001239
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001240 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001241 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001242 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001243 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001244 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1245 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001246 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001247 public static final int STATE_PHONE_STATE_SHIFT = 6;
1248 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001249 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001250 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001251 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001252
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001253 // These states always appear directly in the first int token
1254 // of a delta change; they should be ones that change relatively
1255 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001256 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1257 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001258 public static final int STATE_GPS_ON_FLAG = 1<<29;
1259 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001260 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001261 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001262 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001263 // Do not use, this is used for coulomb delta count.
1264 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001265 // These are on the lower bits used for the command; if they change
1266 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001267 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001268 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001269 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001270 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1271 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1272 // empty slot
1273 // empty slot
1274 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001275
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001276 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001277 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1278
1279 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001280
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001281 public int states;
1282
Dianne Hackborn3251b902014-06-20 14:40:53 -07001283 // Constants from WIFI_SUPPL_STATE_*
1284 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1285 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1286 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1287 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1288 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1289 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1290
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001291 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001292 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1293 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1294 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001295 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001296 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1297 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1298 public static final int STATE2_CHARGING_FLAG = 1<<24;
1299 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1300 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1301 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001302 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001303
1304 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001305 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001306 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1307
1308 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001309
Dianne Hackborn40c87252014-03-19 16:55:40 -07001310 public int states2;
1311
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001312 // The wake lock that was acquired at this point.
1313 public HistoryTag wakelockTag;
1314
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001315 // Kernel wakeup reason at this point.
1316 public HistoryTag wakeReasonTag;
1317
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001318 // Non-null when there is more detailed information at this step.
1319 public HistoryStepDetails stepDetails;
1320
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001321 public static final int EVENT_FLAG_START = 0x8000;
1322 public static final int EVENT_FLAG_FINISH = 0x4000;
1323
1324 // No event in this item.
1325 public static final int EVENT_NONE = 0x0000;
1326 // Event is about a process that is running.
1327 public static final int EVENT_PROC = 0x0001;
1328 // Event is about an application package that is in the foreground.
1329 public static final int EVENT_FOREGROUND = 0x0002;
1330 // Event is about an application package that is at the top of the screen.
1331 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001332 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001333 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001334 // Events for all additional wake locks aquired/release within a wake block.
1335 // These are not generated by default.
1336 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001337 // Event is about an application executing a scheduled job.
1338 public static final int EVENT_JOB = 0x0006;
1339 // Events for users running.
1340 public static final int EVENT_USER_RUNNING = 0x0007;
1341 // Events for foreground user.
1342 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001343 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001344 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001345 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001346 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001347 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001348 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001349 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001350 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001351 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001352 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001353 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001354 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001355 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001356 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001357 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001358 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001359 // Event for a package being on the temporary whitelist.
1360 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001361 // Event for the screen waking up.
1362 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001363 // Event for the UID that woke up the application processor.
1364 // Used for wakeups coming from WiFi, modem, etc.
1365 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001366 // Event for reporting that a specific partial wake lock has been held for a long duration.
1367 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001368
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001369 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001370 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001371 // Mask to extract out only the type part of the event.
1372 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001373
1374 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1375 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1376 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1377 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1378 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1379 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001380 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1381 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001382 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1383 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001384 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1385 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1386 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1387 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1388 public static final int EVENT_USER_FOREGROUND_START =
1389 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1390 public static final int EVENT_USER_FOREGROUND_FINISH =
1391 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001392 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1393 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001394 public static final int EVENT_TEMP_WHITELIST_START =
1395 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1396 public static final int EVENT_TEMP_WHITELIST_FINISH =
1397 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001398 public static final int EVENT_LONG_WAKE_LOCK_START =
1399 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1400 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1401 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001402
1403 // For CMD_EVENT.
1404 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001405 public HistoryTag eventTag;
1406
Dianne Hackborn9a755432014-05-15 17:05:22 -07001407 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001408 public long currentTime;
1409
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001410 // Meta-data when reading.
1411 public int numReadInts;
1412
1413 // Pre-allocated objects.
1414 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001415 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001416 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001417
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001418 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001419 }
1420
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001421 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001422 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001423 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001424 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001425 }
1426
1427 public int describeContents() {
1428 return 0;
1429 }
1430
1431 public void writeToParcel(Parcel dest, int flags) {
1432 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001433 int bat = (((int)cmd)&0xff)
1434 | ((((int)batteryLevel)<<8)&0xff00)
1435 | ((((int)batteryStatus)<<16)&0xf0000)
1436 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001437 | ((((int)batteryPlugType)<<24)&0xf000000)
1438 | (wakelockTag != null ? 0x10000000 : 0)
1439 | (wakeReasonTag != null ? 0x20000000 : 0)
1440 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001441 dest.writeInt(bat);
1442 bat = (((int)batteryTemperature)&0xffff)
1443 | ((((int)batteryVoltage)<<16)&0xffff0000);
1444 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001445 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001446 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001447 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001448 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001449 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001450 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001451 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001452 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001453 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001454 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001455 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001456 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001457 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001458 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001459 dest.writeLong(currentTime);
1460 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001461 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001462
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001463 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001464 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001465 int bat = src.readInt();
1466 cmd = (byte)(bat&0xff);
1467 batteryLevel = (byte)((bat>>8)&0xff);
1468 batteryStatus = (byte)((bat>>16)&0xf);
1469 batteryHealth = (byte)((bat>>20)&0xf);
1470 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001471 int bat2 = src.readInt();
1472 batteryTemperature = (short)(bat2&0xffff);
1473 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001474 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001475 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001476 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001477 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001478 wakelockTag = localWakelockTag;
1479 wakelockTag.readFromParcel(src);
1480 } else {
1481 wakelockTag = null;
1482 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001483 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001484 wakeReasonTag = localWakeReasonTag;
1485 wakeReasonTag.readFromParcel(src);
1486 } else {
1487 wakeReasonTag = null;
1488 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001489 if ((bat&0x40000000) != 0) {
1490 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001491 eventTag = localEventTag;
1492 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001493 } else {
1494 eventCode = EVENT_NONE;
1495 eventTag = null;
1496 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001497 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001498 currentTime = src.readLong();
1499 } else {
1500 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001501 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001502 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001503 }
1504
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001505 public void clear() {
1506 time = 0;
1507 cmd = CMD_NULL;
1508 batteryLevel = 0;
1509 batteryStatus = 0;
1510 batteryHealth = 0;
1511 batteryPlugType = 0;
1512 batteryTemperature = 0;
1513 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001514 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001515 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001516 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001517 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001518 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001519 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001520 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001521 }
1522
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001523 public void setTo(HistoryItem o) {
1524 time = o.time;
1525 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001526 setToCommon(o);
1527 }
1528
1529 public void setTo(long time, byte cmd, HistoryItem o) {
1530 this.time = time;
1531 this.cmd = cmd;
1532 setToCommon(o);
1533 }
1534
1535 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001536 batteryLevel = o.batteryLevel;
1537 batteryStatus = o.batteryStatus;
1538 batteryHealth = o.batteryHealth;
1539 batteryPlugType = o.batteryPlugType;
1540 batteryTemperature = o.batteryTemperature;
1541 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001542 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001543 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001544 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001545 if (o.wakelockTag != null) {
1546 wakelockTag = localWakelockTag;
1547 wakelockTag.setTo(o.wakelockTag);
1548 } else {
1549 wakelockTag = null;
1550 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001551 if (o.wakeReasonTag != null) {
1552 wakeReasonTag = localWakeReasonTag;
1553 wakeReasonTag.setTo(o.wakeReasonTag);
1554 } else {
1555 wakeReasonTag = null;
1556 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001557 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001558 if (o.eventTag != null) {
1559 eventTag = localEventTag;
1560 eventTag.setTo(o.eventTag);
1561 } else {
1562 eventTag = null;
1563 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001564 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001565 }
1566
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001567 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001568 return batteryLevel == o.batteryLevel
1569 && batteryStatus == o.batteryStatus
1570 && batteryHealth == o.batteryHealth
1571 && batteryPlugType == o.batteryPlugType
1572 && batteryTemperature == o.batteryTemperature
1573 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001574 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001575 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001576 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001577 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001578 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001579
1580 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001581 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001582 return false;
1583 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001584 if (wakelockTag != o.wakelockTag) {
1585 if (wakelockTag == null || o.wakelockTag == null) {
1586 return false;
1587 }
1588 if (!wakelockTag.equals(o.wakelockTag)) {
1589 return false;
1590 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001591 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001592 if (wakeReasonTag != o.wakeReasonTag) {
1593 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1594 return false;
1595 }
1596 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1597 return false;
1598 }
1599 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001600 if (eventTag != o.eventTag) {
1601 if (eventTag == null || o.eventTag == null) {
1602 return false;
1603 }
1604 if (!eventTag.equals(o.eventTag)) {
1605 return false;
1606 }
1607 }
1608 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001609 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001610 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001611
1612 public final static class HistoryEventTracker {
1613 private final HashMap<String, SparseIntArray>[] mActiveEvents
1614 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1615
1616 public boolean updateState(int code, String name, int uid, int poolIdx) {
1617 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1618 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1619 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1620 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001621 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001622 mActiveEvents[idx] = active;
1623 }
1624 SparseIntArray uids = active.get(name);
1625 if (uids == null) {
1626 uids = new SparseIntArray();
1627 active.put(name, uids);
1628 }
1629 if (uids.indexOfKey(uid) >= 0) {
1630 // Already set, nothing to do!
1631 return false;
1632 }
1633 uids.put(uid, poolIdx);
1634 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1635 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1636 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1637 if (active == null) {
1638 // not currently active, nothing to do.
1639 return false;
1640 }
1641 SparseIntArray uids = active.get(name);
1642 if (uids == null) {
1643 // not currently active, nothing to do.
1644 return false;
1645 }
1646 idx = uids.indexOfKey(uid);
1647 if (idx < 0) {
1648 // not currently active, nothing to do.
1649 return false;
1650 }
1651 uids.removeAt(idx);
1652 if (uids.size() <= 0) {
1653 active.remove(name);
1654 }
1655 }
1656 return true;
1657 }
1658
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001659 public void removeEvents(int code) {
1660 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1661 mActiveEvents[idx] = null;
1662 }
1663
Dianne Hackborn37de0982014-05-09 09:32:18 -07001664 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1665 return mActiveEvents[code];
1666 }
1667 }
1668
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001669 public static final class BitDescription {
1670 public final int mask;
1671 public final int shift;
1672 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001673 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001674 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001675 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001676
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001677 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001678 this.mask = mask;
1679 this.shift = -1;
1680 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001681 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001682 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001683 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001684 }
1685
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001686 public BitDescription(int mask, int shift, String name, String shortName,
1687 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001688 this.mask = mask;
1689 this.shift = shift;
1690 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001691 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001692 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001693 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001694 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001695 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001696
Dianne Hackbornfc064132014-06-02 12:42:12 -07001697 /**
1698 * Don't allow any more batching in to the current history event. This
1699 * is called when printing partial histories, so to ensure that the next
1700 * history event will go in to a new batch after what was printed in the
1701 * last partial history.
1702 */
1703 public abstract void commitCurrentHistoryBatchLocked();
1704
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001705 public abstract int getHistoryTotalSize();
1706
1707 public abstract int getHistoryUsedSize();
1708
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001709 public abstract boolean startIteratingHistoryLocked();
1710
Dianne Hackborn099bc622014-01-22 13:39:16 -08001711 public abstract int getHistoryStringPoolSize();
1712
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001713 public abstract int getHistoryStringPoolBytes();
1714
1715 public abstract String getHistoryTagPoolString(int index);
1716
1717 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001718
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001719 public abstract boolean getNextHistoryLocked(HistoryItem out);
1720
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001721 public abstract void finishIteratingHistoryLocked();
1722
1723 public abstract boolean startIteratingOldHistoryLocked();
1724
1725 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1726
1727 public abstract void finishIteratingOldHistoryLocked();
1728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001730 * Return the base time offset for the battery history.
1731 */
1732 public abstract long getHistoryBaseTime();
1733
1734 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 * Returns the number of times the device has been started.
1736 */
1737 public abstract int getStartCount();
1738
1739 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001740 * 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 -08001741 * running on battery.
1742 *
1743 * {@hide}
1744 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001745 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001747 /**
1748 * Returns the number of times the screen was turned on.
1749 *
1750 * {@hide}
1751 */
1752 public abstract int getScreenOnCount(int which);
1753
Jeff Browne95c3cd2014-05-02 16:59:26 -07001754 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1755
Dianne Hackborn617f8772009-03-31 15:04:46 -07001756 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1757 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1758 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1759 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1760 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1761
1762 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1763 "dark", "dim", "medium", "light", "bright"
1764 };
1765
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001766 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1767 "0", "1", "2", "3", "4"
1768 };
1769
Dianne Hackborn617f8772009-03-31 15:04:46 -07001770 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001771
Dianne Hackborn617f8772009-03-31 15:04:46 -07001772 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001773 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001774 * the given brightness
1775 *
1776 * {@hide}
1777 */
1778 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001779 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001782 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001783 * running on battery.
1784 *
1785 * {@hide}
1786 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001787 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001788
1789 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001790 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001791 *
1792 * {@hide}
1793 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001794 public abstract int getPowerSaveModeEnabledCount(int which);
1795
1796 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001797 * Constant for device idle mode: not active.
1798 */
1799 public static final int DEVICE_IDLE_MODE_OFF = 0;
1800
1801 /**
1802 * Constant for device idle mode: active in lightweight mode.
1803 */
1804 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1805
1806 /**
1807 * Constant for device idle mode: active in full mode.
1808 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001809 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001810
1811 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001812 * Returns the time in microseconds that device has been in idle mode while
1813 * running on battery.
1814 *
1815 * {@hide}
1816 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001817 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001818
1819 /**
1820 * Returns the number of times that the devie has gone in to idle mode.
1821 *
1822 * {@hide}
1823 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001824 public abstract int getDeviceIdleModeCount(int mode, int which);
1825
1826 /**
1827 * Return the longest duration we spent in a particular device idle mode (fully in the
1828 * mode, not in idle maintenance etc).
1829 */
1830 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001831
1832 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001833 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001834 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001835 * counts all of the time that we consider the device to be idle, whether or not
1836 * it is currently in the actual device idle mode.
1837 *
1838 * {@hide}
1839 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001840 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001841
1842 /**
1843 * Returns the number of times that the devie has started idling.
1844 *
1845 * {@hide}
1846 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001847 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001848
1849 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001850 * Returns the number of times that connectivity state changed.
1851 *
1852 * {@hide}
1853 */
1854 public abstract int getNumConnectivityChange(int which);
1855
1856 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001857 * 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 -08001858 * running on battery.
1859 *
1860 * {@hide}
1861 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001862 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001863
1864 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001865 * Returns the number of times a phone call was activated.
1866 *
1867 * {@hide}
1868 */
1869 public abstract int getPhoneOnCount(int which);
1870
1871 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001872 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001873 * the given signal strength.
1874 *
1875 * {@hide}
1876 */
1877 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001878 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001879
Dianne Hackborn617f8772009-03-31 15:04:46 -07001880 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001881 * Returns the time in microseconds that the phone has been trying to
1882 * acquire a signal.
1883 *
1884 * {@hide}
1885 */
1886 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001887 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001888
1889 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001890 * Returns the number of times the phone has entered the given signal strength.
1891 *
1892 * {@hide}
1893 */
1894 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1895
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001896 /**
1897 * Returns the time in microseconds that the mobile network has been active
1898 * (in a high power state).
1899 *
1900 * {@hide}
1901 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001902 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001903
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001904 /**
1905 * Returns the number of times that the mobile network has transitioned to the
1906 * active state.
1907 *
1908 * {@hide}
1909 */
1910 public abstract int getMobileRadioActiveCount(int which);
1911
1912 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001913 * Returns the time in microseconds that is the difference between the mobile radio
1914 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1915 * from the radio.
1916 *
1917 * {@hide}
1918 */
1919 public abstract long getMobileRadioActiveAdjustedTime(int which);
1920
1921 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001922 * Returns the time in microseconds that the mobile network has been active
1923 * (in a high power state) but not being able to blame on an app.
1924 *
1925 * {@hide}
1926 */
1927 public abstract long getMobileRadioActiveUnknownTime(int which);
1928
1929 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001930 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001931 *
1932 * {@hide}
1933 */
1934 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001935
Dianne Hackborn627bba72009-03-24 22:32:56 -07001936 public static final int DATA_CONNECTION_NONE = 0;
1937 public static final int DATA_CONNECTION_GPRS = 1;
1938 public static final int DATA_CONNECTION_EDGE = 2;
1939 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001940 public static final int DATA_CONNECTION_CDMA = 4;
1941 public static final int DATA_CONNECTION_EVDO_0 = 5;
1942 public static final int DATA_CONNECTION_EVDO_A = 6;
1943 public static final int DATA_CONNECTION_1xRTT = 7;
1944 public static final int DATA_CONNECTION_HSDPA = 8;
1945 public static final int DATA_CONNECTION_HSUPA = 9;
1946 public static final int DATA_CONNECTION_HSPA = 10;
1947 public static final int DATA_CONNECTION_IDEN = 11;
1948 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001949 public static final int DATA_CONNECTION_LTE = 13;
1950 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001951 public static final int DATA_CONNECTION_HSPAP = 15;
1952 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001953
Dianne Hackborn627bba72009-03-24 22:32:56 -07001954 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001955 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001956 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001957 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001958 };
1959
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001960 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001961
1962 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001963 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001964 * the given data connection.
1965 *
1966 * {@hide}
1967 */
1968 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001969 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001972 * Returns the number of times the phone has entered the given data
1973 * connection type.
1974 *
1975 * {@hide}
1976 */
1977 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001978
Dianne Hackborn3251b902014-06-20 14:40:53 -07001979 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1980 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1981 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1982 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1983 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1984 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1985 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1986 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1987 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1988 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1989 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1990 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1991 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1992
1993 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1994
1995 static final String[] WIFI_SUPPL_STATE_NAMES = {
1996 "invalid", "disconn", "disabled", "inactive", "scanning",
1997 "authenticating", "associating", "associated", "4-way-handshake",
1998 "group-handshake", "completed", "dormant", "uninit"
1999 };
2000
2001 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2002 "inv", "dsc", "dis", "inact", "scan",
2003 "auth", "ascing", "asced", "4-way",
2004 "group", "compl", "dorm", "uninit"
2005 };
2006
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002007 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2008 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002009 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002010 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2011 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002012 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002013 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2014 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2015 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002016 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002017 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002018 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002019 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002020 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2021 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002022 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2023 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2024 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2025 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2026 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2027 new String[] {"in", "out", "emergency", "off"},
2028 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002029 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2030 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2031 SignalStrength.SIGNAL_STRENGTH_NAMES,
2032 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002033 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2034 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2035 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002036 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002037
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002038 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2039 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002040 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002041 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002042 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002043 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002044 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002045 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2046 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2047 new String[] { "off", "light", "full", "???" },
2048 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002049 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2050 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2051 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002052 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2053 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2054 new String[] { "0", "1", "2", "3", "4" },
2055 new String[] { "0", "1", "2", "3", "4" }),
2056 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2057 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2058 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002059 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002060 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002061 };
2062
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002063 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002064 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002065 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2066 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002067 };
2068
2069 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002070 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002071 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002072 "Esw", "Ewa", "Elw", "Eec"
2073 };
2074
2075 @FunctionalInterface
2076 public interface IntToString {
2077 String applyAsString(int val);
2078 }
2079
2080 private static final IntToString sUidToString = UserHandle::formatUid;
2081 private static final IntToString sIntToString = Integer::toString;
2082
2083 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2084 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2085 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2086 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2087 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002088 };
2089
Dianne Hackborn617f8772009-03-31 15:04:46 -07002090 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002091 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002092 * running on battery.
2093 *
2094 * {@hide}
2095 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002096 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002097
2098 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002099 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002100 * been in the running state while the device was running on battery.
2101 *
2102 * {@hide}
2103 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002104 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002105
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002106 public static final int WIFI_STATE_OFF = 0;
2107 public static final int WIFI_STATE_OFF_SCANNING = 1;
2108 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2109 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2110 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2111 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2112 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2113 public static final int WIFI_STATE_SOFT_AP = 7;
2114
2115 static final String[] WIFI_STATE_NAMES = {
2116 "off", "scanning", "no_net", "disconn",
2117 "sta", "p2p", "sta_p2p", "soft_ap"
2118 };
2119
2120 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2121
2122 /**
2123 * Returns the time in microseconds that WiFi has been running in the given state.
2124 *
2125 * {@hide}
2126 */
2127 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002128 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002129
2130 /**
2131 * Returns the number of times that WiFi has entered the given state.
2132 *
2133 * {@hide}
2134 */
2135 public abstract int getWifiStateCount(int wifiState, int which);
2136
The Android Open Source Project10592532009-03-18 17:39:46 -07002137 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002138 * Returns the time in microseconds that the wifi supplicant has been
2139 * in a given state.
2140 *
2141 * {@hide}
2142 */
2143 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2144
2145 /**
2146 * Returns the number of times that the wifi supplicant has transitioned
2147 * to a given state.
2148 *
2149 * {@hide}
2150 */
2151 public abstract int getWifiSupplStateCount(int state, int which);
2152
2153 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2154
2155 /**
2156 * Returns the time in microseconds that WIFI has been running with
2157 * the given signal strength.
2158 *
2159 * {@hide}
2160 */
2161 public abstract long getWifiSignalStrengthTime(int strengthBin,
2162 long elapsedRealtimeUs, int which);
2163
2164 /**
2165 * Returns the number of times WIFI has entered the given signal strength.
2166 *
2167 * {@hide}
2168 */
2169 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2170
2171 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002172 * Returns the time in microseconds that the flashlight has been on while the device was
2173 * running on battery.
2174 *
2175 * {@hide}
2176 */
2177 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2178
2179 /**
2180 * Returns the number of times that the flashlight has been turned on while the device was
2181 * running on battery.
2182 *
2183 * {@hide}
2184 */
2185 public abstract long getFlashlightOnCount(int which);
2186
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002187 /**
2188 * Returns the time in microseconds that the camera has been on while the device was
2189 * running on battery.
2190 *
2191 * {@hide}
2192 */
2193 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2194
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002195 /**
2196 * Returns the time in microseconds that bluetooth scans were running while the device was
2197 * on battery.
2198 *
2199 * {@hide}
2200 */
2201 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002202
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002203 public static final int NETWORK_MOBILE_RX_DATA = 0;
2204 public static final int NETWORK_MOBILE_TX_DATA = 1;
2205 public static final int NETWORK_WIFI_RX_DATA = 2;
2206 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002207 public static final int NETWORK_BT_RX_DATA = 4;
2208 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002209 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2210 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2211 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2212 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2213 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002214
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002215 public abstract long getNetworkActivityBytes(int type, int which);
2216 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002217
Adam Lesinskie08af192015-03-25 16:42:59 -07002218 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002219 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002220 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002221 * actual power data.
2222 */
2223 public abstract boolean hasWifiActivityReporting();
2224
2225 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002226 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2227 * in various radio controller states, such as transmit, receive, and idle.
2228 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002229 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002230 public abstract ControllerActivityCounter getWifiControllerActivity();
2231
2232 /**
2233 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2234 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2235 * actual power data.
2236 */
2237 public abstract boolean hasBluetoothActivityReporting();
2238
2239 /**
2240 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2241 * in various radio controller states, such as transmit, receive, and idle.
2242 * @return non-null {@link ControllerActivityCounter}
2243 */
2244 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2245
2246 /**
2247 * Returns true if the BatteryStats object has detailed modem power reports.
2248 * When true, calling {@link #getModemControllerActivity()} will yield the
2249 * actual power data.
2250 */
2251 public abstract boolean hasModemActivityReporting();
2252
2253 /**
2254 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2255 * in various radio controller states, such as transmit, receive, and idle.
2256 * @return non-null {@link ControllerActivityCounter}
2257 */
2258 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002259
The Android Open Source Project10592532009-03-18 17:39:46 -07002260 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002261 * Return the wall clock time when battery stats data collection started.
2262 */
2263 public abstract long getStartClockTime();
2264
2265 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002266 * Return platform version tag that we were running in when the battery stats started.
2267 */
2268 public abstract String getStartPlatformVersion();
2269
2270 /**
2271 * Return platform version tag that we were running in when the battery stats ended.
2272 */
2273 public abstract String getEndPlatformVersion();
2274
2275 /**
2276 * Return the internal version code of the parcelled format.
2277 */
2278 public abstract int getParcelVersion();
2279
2280 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 * Return whether we are currently running on battery.
2282 */
2283 public abstract boolean getIsOnBattery();
2284
2285 /**
2286 * Returns a SparseArray containing the statistics for each uid.
2287 */
2288 public abstract SparseArray<? extends Uid> getUidStats();
2289
2290 /**
2291 * Returns the current battery uptime in microseconds.
2292 *
2293 * @param curTime the amount of elapsed realtime in microseconds.
2294 */
2295 public abstract long getBatteryUptime(long curTime);
2296
2297 /**
2298 * Returns the current battery realtime in microseconds.
2299 *
2300 * @param curTime the amount of elapsed realtime in microseconds.
2301 */
2302 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002303
2304 /**
Evan Millar633a1742009-04-02 16:36:33 -07002305 * Returns the battery percentage level at the last time the device was unplugged from power, or
2306 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002307 */
Evan Millar633a1742009-04-02 16:36:33 -07002308 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002309
2310 /**
Evan Millar633a1742009-04-02 16:36:33 -07002311 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2312 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002313 */
Evan Millar633a1742009-04-02 16:36:33 -07002314 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315
2316 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002317 * Get the amount the battery has discharged since the stats were
2318 * last reset after charging, as a lower-end approximation.
2319 */
2320 public abstract int getLowDischargeAmountSinceCharge();
2321
2322 /**
2323 * Get the amount the battery has discharged since the stats were
2324 * last reset after charging, as an upper-end approximation.
2325 */
2326 public abstract int getHighDischargeAmountSinceCharge();
2327
2328 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002329 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2330 */
2331 public abstract int getDischargeAmount(int which);
2332
2333 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002334 * Get the amount the battery has discharged while the screen was on,
2335 * since the last time power was unplugged.
2336 */
2337 public abstract int getDischargeAmountScreenOn();
2338
2339 /**
2340 * Get the amount the battery has discharged while the screen was on,
2341 * since the last time the device was charged.
2342 */
2343 public abstract int getDischargeAmountScreenOnSinceCharge();
2344
2345 /**
2346 * Get the amount the battery has discharged while the screen was off,
2347 * since the last time power was unplugged.
2348 */
2349 public abstract int getDischargeAmountScreenOff();
2350
2351 /**
2352 * Get the amount the battery has discharged while the screen was off,
2353 * since the last time the device was charged.
2354 */
2355 public abstract int getDischargeAmountScreenOffSinceCharge();
2356
2357 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 * Returns the total, last, or current battery uptime in microseconds.
2359 *
2360 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002361 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 */
2363 public abstract long computeBatteryUptime(long curTime, int which);
2364
2365 /**
2366 * Returns the total, last, or current battery realtime in microseconds.
2367 *
2368 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002369 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 */
2371 public abstract long computeBatteryRealtime(long curTime, int which);
2372
2373 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002374 * Returns the total, last, or current battery screen off uptime in microseconds.
2375 *
2376 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002377 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002378 */
2379 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2380
2381 /**
2382 * Returns the total, last, or current battery screen off realtime in microseconds.
2383 *
2384 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002385 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002386 */
2387 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2388
2389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 * Returns the total, last, or current uptime in microseconds.
2391 *
2392 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002393 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 */
2395 public abstract long computeUptime(long curTime, int which);
2396
2397 /**
2398 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002399 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002401 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 */
2403 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002404
2405 /**
2406 * Compute an approximation for how much run time (in microseconds) is remaining on
2407 * the battery. Returns -1 if no time can be computed: either there is not
2408 * enough current data to make a decision, or the battery is currently
2409 * charging.
2410 *
2411 * @param curTime The current elepsed realtime in microseconds.
2412 */
2413 public abstract long computeBatteryTimeRemaining(long curTime);
2414
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002415 // The part of a step duration that is the actual time.
2416 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2417
2418 // Bits in a step duration that are the new battery level we are at.
2419 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002420 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002421
2422 // Bits in a step duration that are the initial mode we were in at that step.
2423 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002424 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002425
2426 // Bits in a step duration that indicate which modes changed during that step.
2427 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002428 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002429
2430 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2431 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2432
Santos Cordone94f0502017-02-24 12:31:20 -08002433 // The largest value for screen state that is tracked in battery states. Any values above
2434 // this should be mapped back to one of the tracked values before being tracked here.
2435 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2436
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002437 // Step duration mode: power save is on.
2438 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2439
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002440 // Step duration mode: device is currently in idle mode.
2441 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2442
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002443 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2444 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002445 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2446 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002447 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2448 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2449 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2450 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2451 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002452 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2453 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002454 };
2455 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2456 (Display.STATE_OFF-1),
2457 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002458 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002459 (Display.STATE_ON-1),
2460 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2461 (Display.STATE_DOZE-1),
2462 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2463 (Display.STATE_DOZE_SUSPEND-1),
2464 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002465 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002466 };
2467 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2468 "screen off",
2469 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002470 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002471 "screen on",
2472 "screen on power save",
2473 "screen doze",
2474 "screen doze power save",
2475 "screen doze-suspend",
2476 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002477 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002478 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002479
2480 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002481 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2482 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2483 * a coulomb counter.
2484 */
2485 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2486
2487 /**
2488 * Return the counter keeping track of the amount of battery discharge measured in
2489 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2490 * a coulomb counter.
2491 */
2492 public abstract LongCounter getDischargeCoulombCounter();
2493
2494 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002495 * Returns the estimated real battery capacity, which may be less than the capacity
2496 * declared by the PowerProfile.
2497 * @return The estimated battery capacity in mAh.
2498 */
2499 public abstract int getEstimatedBatteryCapacity();
2500
2501 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002502 * @return The minimum learned battery capacity in uAh.
2503 */
2504 public abstract int getMinLearnedBatteryCapacity();
2505
2506 /**
2507 * @return The maximum learned battery capacity in uAh.
2508 */
2509 public abstract int getMaxLearnedBatteryCapacity() ;
2510
2511 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002512 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002513 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002514 public abstract LevelStepTracker getDischargeLevelStepTracker();
2515
2516 /**
2517 * Return the array of daily discharge step durations.
2518 */
2519 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002520
2521 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002522 * Compute an approximation for how much time (in microseconds) remains until the battery
2523 * is fully charged. Returns -1 if no time can be computed: either there is not
2524 * enough current data to make a decision, or the battery is currently
2525 * discharging.
2526 *
2527 * @param curTime The current elepsed realtime in microseconds.
2528 */
2529 public abstract long computeChargeTimeRemaining(long curTime);
2530
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002531 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002532 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002533 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002534 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002535
2536 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002537 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002538 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002539 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002540
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002541 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2542
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002543 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002544
Evan Millarc64edde2009-04-18 12:26:32 -07002545 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546
James Carr2dd7e5e2016-07-20 18:48:39 -07002547 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2548
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002549 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2550
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002551 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 long days = seconds / (60 * 60 * 24);
2553 if (days != 0) {
2554 out.append(days);
2555 out.append("d ");
2556 }
2557 long used = days * 60 * 60 * 24;
2558
2559 long hours = (seconds - used) / (60 * 60);
2560 if (hours != 0 || used != 0) {
2561 out.append(hours);
2562 out.append("h ");
2563 }
2564 used += hours * 60 * 60;
2565
2566 long mins = (seconds-used) / 60;
2567 if (mins != 0 || used != 0) {
2568 out.append(mins);
2569 out.append("m ");
2570 }
2571 used += mins * 60;
2572
2573 if (seconds != 0 || used != 0) {
2574 out.append(seconds-used);
2575 out.append("s ");
2576 }
2577 }
2578
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002579 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002581 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 sb.append(time - (sec * 1000));
2583 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 }
2585
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002586 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002587 long sec = time / 1000;
2588 formatTimeRaw(sb, sec);
2589 sb.append(time - (sec * 1000));
2590 sb.append("ms");
2591 }
2592
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002593 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002595 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 }
2597 float perc = ((float)num) / ((float)den) * 100;
2598 mFormatBuilder.setLength(0);
2599 mFormatter.format("%.1f%%", perc);
2600 return mFormatBuilder.toString();
2601 }
2602
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002603 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002604 mFormatBuilder.setLength(0);
2605
2606 if (bytes < BYTES_PER_KB) {
2607 return bytes + "B";
2608 } else if (bytes < BYTES_PER_MB) {
2609 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2610 return mFormatBuilder.toString();
2611 } else if (bytes < BYTES_PER_GB){
2612 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2613 return mFormatBuilder.toString();
2614 } else {
2615 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2616 return mFormatBuilder.toString();
2617 }
2618 }
2619
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002620 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002621 if (timer != null) {
2622 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002623 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002624 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2625 return totalTimeMillis;
2626 }
2627 return 0;
2628 }
2629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 /**
2631 *
2632 * @param sb a StringBuilder object.
2633 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002634 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002636 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 * @param linePrefix a String to be prepended to each line of output.
2638 * @return the line prefix
2639 */
2640 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002641 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002644 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002645
Evan Millarc64edde2009-04-18 12:26:32 -07002646 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 if (totalTimeMillis != 0) {
2648 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002649 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002650 if (name != null) {
2651 sb.append(name);
2652 sb.append(' ');
2653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 sb.append('(');
2655 sb.append(count);
2656 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002657 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2658 if (maxDurationMs >= 0) {
2659 sb.append(" max=");
2660 sb.append(maxDurationMs);
2661 }
2662 if (timer.isRunningLocked()) {
2663 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2664 if (currentMs >= 0) {
2665 sb.append(" (running for ");
2666 sb.append(currentMs);
2667 sb.append("ms)");
2668 } else {
2669 sb.append(" (running)");
2670 }
2671 }
2672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 return ", ";
2674 }
2675 }
2676 return linePrefix;
2677 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002678
2679 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002680 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002681 *
2682 * @param pw a PrintWriter object to print to.
2683 * @param sb a StringBuilder object.
2684 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002685 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002686 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2687 * @param prefix a String to be prepended to each line of output.
2688 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002689 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002690 */
2691 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002692 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002693 if (timer != null) {
2694 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002695 final long totalTimeMs = (timer.getTotalTimeLocked(
2696 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002697 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002698 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002699 sb.setLength(0);
2700 sb.append(prefix);
2701 sb.append(" ");
2702 sb.append(type);
2703 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002704 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002705 sb.append("realtime (");
2706 sb.append(count);
2707 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002708 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2709 if (maxDurationMs >= 0) {
2710 sb.append(" max=");
2711 sb.append(maxDurationMs);
2712 }
2713 if (timer.isRunningLocked()) {
2714 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2715 if (currentMs >= 0) {
2716 sb.append(" (running for ");
2717 sb.append(currentMs);
2718 sb.append("ms)");
2719 } else {
2720 sb.append(" (running)");
2721 }
2722 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002723 pw.println(sb.toString());
2724 return true;
2725 }
2726 }
2727 return false;
2728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729
2730 /**
2731 * Checkin version of wakelock printer. Prints simple comma-separated list.
2732 *
2733 * @param sb a StringBuilder object.
2734 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002735 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002737 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 * @param linePrefix a String to be prepended to each line of output.
2739 * @return the line prefix
2740 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002741 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2742 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 long totalTimeMicros = 0;
2744 int count = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002745 long max = -1;
2746 long current = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002748 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002749 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002750 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2751 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 }
2753 sb.append(linePrefix);
2754 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2755 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002756 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002758 sb.append(',');
2759 sb.append(current);
2760 sb.append(',');
2761 sb.append(max);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 return ",";
2763 }
2764
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002765 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2766 String type) {
2767 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2768 pw.print(',');
2769 pw.print(uid);
2770 pw.print(',');
2771 pw.print(category);
2772 pw.print(',');
2773 pw.print(type);
2774 }
2775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 /**
2777 * Dump a comma-separated line of values for terse checkin mode.
2778 *
2779 * @param pw the PageWriter to dump log to
2780 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2781 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2782 * @param args type-dependent data arguments
2783 */
2784 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2785 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002786 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002787 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002788 pw.print(',');
2789 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002791 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002793
2794 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002795 * Dump a given timer stat for terse checkin mode.
2796 *
2797 * @param pw the PageWriter to dump log to
2798 * @param uid the UID to log
2799 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2800 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2801 * @param timer a {@link Timer} to dump stats for
2802 * @param rawRealtime the current elapsed realtime of the system in microseconds
2803 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2804 */
2805 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2806 Timer timer, long rawRealtime, int which) {
2807 if (timer != null) {
2808 // Convert from microseconds to milliseconds with rounding
2809 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2810 / 1000;
2811 final int count = timer.getCountLocked(which);
2812 if (totalTime != 0) {
2813 dumpLine(pw, uid, category, type, totalTime, count);
2814 }
2815 }
2816 }
2817
2818 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002819 * Checks if the ControllerActivityCounter has any data worth dumping.
2820 */
2821 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2822 if (counter == null) {
2823 return false;
2824 }
2825
2826 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2827 || counter.getRxTimeCounter().getCountLocked(which) != 0
2828 || counter.getPowerCounter().getCountLocked(which) != 0) {
2829 return true;
2830 }
2831
2832 for (LongCounter c : counter.getTxTimeCounters()) {
2833 if (c.getCountLocked(which) != 0) {
2834 return true;
2835 }
2836 }
2837 return false;
2838 }
2839
2840 /**
2841 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2842 * The order of the arguments in the final check in line is:
2843 *
2844 * idle, rx, power, tx...
2845 *
2846 * where tx... is one or more transmit level times.
2847 */
2848 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2849 String type,
2850 ControllerActivityCounter counter,
2851 int which) {
2852 if (!controllerActivityHasData(counter, which)) {
2853 return;
2854 }
2855
2856 dumpLineHeader(pw, uid, category, type);
2857 pw.print(",");
2858 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2859 pw.print(",");
2860 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2861 pw.print(",");
2862 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2863 for (LongCounter c : counter.getTxTimeCounters()) {
2864 pw.print(",");
2865 pw.print(c.getCountLocked(which));
2866 }
2867 pw.println();
2868 }
2869
2870 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2871 String prefix, String controllerName,
2872 ControllerActivityCounter counter,
2873 int which) {
2874 if (controllerActivityHasData(counter, which)) {
2875 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2876 }
2877 }
2878
2879 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2880 String controllerName,
2881 ControllerActivityCounter counter, int which) {
2882 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2883 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2884 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2885 long totalTxTimeMs = 0;
2886 for (LongCounter txState : counter.getTxTimeCounters()) {
2887 totalTxTimeMs += txState.getCountLocked(which);
2888 }
2889
2890 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2891
2892 sb.setLength(0);
2893 sb.append(prefix);
2894 sb.append(" ");
2895 sb.append(controllerName);
2896 sb.append(" Idle time: ");
2897 formatTimeMs(sb, idleTimeMs);
2898 sb.append("(");
2899 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2900 sb.append(")");
2901 pw.println(sb.toString());
2902
2903 sb.setLength(0);
2904 sb.append(prefix);
2905 sb.append(" ");
2906 sb.append(controllerName);
2907 sb.append(" Rx time: ");
2908 formatTimeMs(sb, rxTimeMs);
2909 sb.append("(");
2910 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2911 sb.append(")");
2912 pw.println(sb.toString());
2913
2914 sb.setLength(0);
2915 sb.append(prefix);
2916 sb.append(" ");
2917 sb.append(controllerName);
2918 sb.append(" Tx time: ");
2919 formatTimeMs(sb, totalTxTimeMs);
2920 sb.append("(");
2921 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2922 sb.append(")");
2923 pw.println(sb.toString());
2924
2925 final int numTxLvls = counter.getTxTimeCounters().length;
2926 if (numTxLvls > 1) {
2927 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2928 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2929 sb.setLength(0);
2930 sb.append(prefix);
2931 sb.append(" [");
2932 sb.append(lvl);
2933 sb.append("] ");
2934 formatTimeMs(sb, txLvlTimeMs);
2935 sb.append("(");
2936 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2937 sb.append(")");
2938 pw.println(sb.toString());
2939 }
2940 }
2941
2942 sb.setLength(0);
2943 sb.append(prefix);
2944 sb.append(" ");
2945 sb.append(controllerName);
2946 sb.append(" Power drain: ").append(
2947 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2948 sb.append("mAh");
2949 pw.println(sb.toString());
2950 }
2951
2952 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002953 * Temporary for settings.
2954 */
2955 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2956 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2957 }
2958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 /**
2960 * Checkin server version of dump to produce more compact, computer-readable log.
2961 *
2962 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002964 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2965 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2967 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2968 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2970 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002971 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2972 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2973 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 final long totalRealtime = computeRealtime(rawRealtime, which);
2975 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002976 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002977 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002978 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002979 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2980 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002981 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002982 rawRealtime, which);
2983 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2984 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002985 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002986 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002987 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002988 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002989 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
2990 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
2991 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002992
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002993 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002995 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002996 final int NU = uidStats.size();
2997
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002998 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003001 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003002 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003003 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003004 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003005 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003006 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003007 getEstimatedBatteryCapacity(),
3008 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07003009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003011 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003012 long fullWakeLockTimeTotal = 0;
3013 long partialWakeLockTimeTotal = 0;
3014
3015 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003016 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003017
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003018 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3019 = u.getWakelockStats();
3020 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3021 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003022
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003023 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3024 if (fullWakeTimer != null) {
3025 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3026 which);
3027 }
3028
3029 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3030 if (partialWakeTimer != null) {
3031 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3032 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003033 }
3034 }
3035 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003036
3037 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003038 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3039 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3040 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3041 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3042 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3043 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3044 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3045 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003046 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3047 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003048 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3049 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003050 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3051 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003052
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003053 // Dump Modem controller stats
3054 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3055 getModemControllerActivity(), which);
3056
Adam Lesinskie283d332015-04-16 12:29:25 -07003057 // Dump Wifi controller stats
3058 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3059 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003060 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003061 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003062
3063 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3064 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003065
3066 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003067 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3068 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 // Dump misc stats
3071 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003072 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003073 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003074 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003075 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003076 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003077 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3078 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003079 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003080 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3081 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3082 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3083 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003084 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003085
3086 // Dump screen brightness stats
3087 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3088 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003089 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003090 }
3091 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07003092
Dianne Hackborn627bba72009-03-24 22:32:56 -07003093 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003094 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3095 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003096 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003097 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003098 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003099 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003100 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003101 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003102 args[i] = getPhoneSignalStrengthCount(i, which);
3103 }
3104 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003105
Dianne Hackborn627bba72009-03-24 22:32:56 -07003106 // Dump network type stats
3107 args = new Object[NUM_DATA_CONNECTION_TYPES];
3108 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003109 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003110 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003111 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3112 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3113 args[i] = getPhoneDataConnectionCount(i, which);
3114 }
3115 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003116
3117 // Dump wifi state stats
3118 args = new Object[NUM_WIFI_STATES];
3119 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003120 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003121 }
3122 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3123 for (int i=0; i<NUM_WIFI_STATES; i++) {
3124 args[i] = getWifiStateCount(i, which);
3125 }
3126 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3127
Dianne Hackborn3251b902014-06-20 14:40:53 -07003128 // Dump wifi suppl state stats
3129 args = new Object[NUM_WIFI_SUPPL_STATES];
3130 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3131 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3132 }
3133 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3134 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3135 args[i] = getWifiSupplStateCount(i, which);
3136 }
3137 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3138
3139 // Dump wifi signal strength stats
3140 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3141 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3142 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3143 }
3144 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3145 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3146 args[i] = getWifiSignalStrengthCount(i, which);
3147 }
3148 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3149
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003150 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003151 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003152 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003155 if (which == STATS_SINCE_UNPLUGGED) {
3156 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3157 getDischargeStartLevel()-getDischargeCurrentLevel(),
3158 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003159 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3160 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003161 } else {
3162 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3163 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003164 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003165 getDischargeAmountScreenOffSinceCharge(),
3166 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003167 }
3168
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003169 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003170 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003171 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003172 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003173 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003174 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003175 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3176 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003177 }
Evan Millarc64edde2009-04-18 12:26:32 -07003178 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003179 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003180 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003181 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3182 // Not doing the regular wake lock formatting to remain compatible
3183 // with the old checkin format.
3184 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3185 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003186 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003187 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003188 }
3189 }
Evan Millarc64edde2009-04-18 12:26:32 -07003190 }
3191
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003192 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003193 helper.create(this);
3194 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003195 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003196 if (sippers != null && sippers.size() > 0) {
3197 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3198 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003199 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003200 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3201 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3202 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003203 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003204 int uid = 0;
3205 String label;
3206 switch (bs.drainType) {
3207 case IDLE:
3208 label="idle";
3209 break;
3210 case CELL:
3211 label="cell";
3212 break;
3213 case PHONE:
3214 label="phone";
3215 break;
3216 case WIFI:
3217 label="wifi";
3218 break;
3219 case BLUETOOTH:
3220 label="blue";
3221 break;
3222 case SCREEN:
3223 label="scrn";
3224 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003225 case FLASHLIGHT:
3226 label="flashlight";
3227 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003228 case APP:
3229 uid = bs.uidObj.getUid();
3230 label = "uid";
3231 break;
3232 case USER:
3233 uid = UserHandle.getUid(bs.userId, 0);
3234 label = "user";
3235 break;
3236 case UNACCOUNTED:
3237 label = "unacc";
3238 break;
3239 case OVERCOUNTED:
3240 label = "over";
3241 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003242 case CAMERA:
3243 label = "camera";
3244 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003245 default:
3246 label = "???";
3247 }
3248 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003249 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003250 }
3251 }
3252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 for (int iu = 0; iu < NU; iu++) {
3254 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003255 if (reqUid >= 0 && uid != reqUid) {
3256 continue;
3257 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003258 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003261 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3262 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3263 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3264 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3265 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3266 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3267 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3268 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003269 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003270 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3271 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003272 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003273 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3274 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003275 // Background data transfers
3276 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3277 which);
3278 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3279 which);
3280 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3281 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3282 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3283 which);
3284 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3285 which);
3286 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3287 which);
3288 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3289 which);
3290
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003291 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3292 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003293 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003294 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3295 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3296 || wifiBytesBgTx > 0
3297 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3298 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003299 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3300 wifiBytesRx, wifiBytesTx,
3301 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003302 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003303 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003304 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3305 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3306 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3307 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003308 }
3309
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003310 // Dump modem controller data, per UID.
3311 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3312 u.getModemControllerActivity(), which);
3313
3314 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003315 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3316 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3317 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003318 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3319 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003320 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3321 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3322 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003323 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003324 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003325 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3326 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003327 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3328 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003329 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003330 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003333 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3334 u.getWifiControllerActivity(), which);
3335
Bookatz867c0d72017-03-07 18:23:42 -08003336 final Timer bleTimer = u.getBluetoothScanTimer();
3337 if (bleTimer != null) {
3338 // Convert from microseconds to milliseconds with rounding
3339 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3340 / 1000;
3341 if (totalTime != 0) {
3342 final int count = bleTimer.getCountLocked(which);
3343 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3344 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
3345 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3346 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3347 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3348 final long actualTimeBg = bleTimerBg != null ?
3349 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3350 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
3351 countBg, actualTime, actualTimeBg);
3352 }
3353 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003354
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003355 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3356 u.getBluetoothControllerActivity(), which);
3357
Dianne Hackborn617f8772009-03-31 15:04:46 -07003358 if (u.hasUserActivity()) {
3359 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3360 boolean hasData = false;
3361 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3362 int val = u.getUserActivityCount(i, which);
3363 args[i] = val;
3364 if (val != 0) hasData = true;
3365 }
3366 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003367 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003368 }
3369 }
3370
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003371 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3372 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3373 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3374 String linePrefix = "";
3375 sb.setLength(0);
3376 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3377 rawRealtime, "f", which, linePrefix);
3378 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3379 rawRealtime, "p", which, linePrefix);
3380 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3381 rawRealtime, "w", which, linePrefix);
3382
3383 // Only log if we had at lease one wakelock...
3384 if (sb.length() > 0) {
3385 String name = wakelocks.keyAt(iw);
3386 if (name.indexOf(',') >= 0) {
3387 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003389 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 }
3391 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003392
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003393 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3394 for (int isy=syncs.size()-1; isy>=0; isy--) {
3395 final Timer timer = syncs.valueAt(isy);
3396 // Convert from microseconds to milliseconds with rounding
3397 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3398 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003399 final Timer bgTimer = timer.getSubTimer();
3400 final long bgTime = bgTimer != null ?
3401 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
3402 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003403 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003404 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003405 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003406 }
3407 }
3408
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003409 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3410 for (int ij=jobs.size()-1; ij>=0; ij--) {
3411 final Timer timer = jobs.valueAt(ij);
3412 // Convert from microseconds to milliseconds with rounding
3413 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3414 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003415 final Timer bgTimer = timer.getSubTimer();
3416 final long bgTime = bgTimer != null ?
3417 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
3418 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003419 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003420 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003421 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003422 }
3423 }
3424
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003425 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3426 rawRealtime, which);
3427 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3428 rawRealtime, which);
3429 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3430 rawRealtime, which);
3431 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3432 rawRealtime, which);
3433
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003434 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3435 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003436 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003437 final Uid.Sensor se = sensors.valueAt(ise);
3438 final int sensorNumber = sensors.keyAt(ise);
3439 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003440 if (timer != null) {
3441 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003442 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3443 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003444 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003445 final int count = timer.getCountLocked(which);
3446 final Timer bgTimer = se.getSensorBackgroundTime();
3447 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
3448 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3449 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3450 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3451 final long bgActualTime = bgTimer != null ?
3452 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3453 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3454 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 }
3457 }
3458
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003459 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3460 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003461
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003462 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3463 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003464
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003465 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003466 long totalStateTime = 0;
3467 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003468 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3469 totalStateTime += time;
3470 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003471 }
3472 if (totalStateTime > 0) {
3473 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3474 }
3475
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003476 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3477 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003478 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003479 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003480 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003481 }
3482
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003483 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3484 = u.getProcessStats();
3485 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3486 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003487
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003488 final long userMillis = ps.getUserTime(which);
3489 final long systemMillis = ps.getSystemTime(which);
3490 final long foregroundMillis = ps.getForegroundTime(which);
3491 final int starts = ps.getStarts(which);
3492 final int numCrashes = ps.getNumCrashes(which);
3493 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003494
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003495 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3496 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003497 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3498 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 }
3500 }
3501
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003502 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3503 = u.getPackageStats();
3504 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3505 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3506 int wakeups = 0;
3507 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3508 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003509 int count = alarms.valueAt(iwa).getCountLocked(which);
3510 wakeups += count;
3511 String name = alarms.keyAt(iwa).replace(',', '_');
3512 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003513 }
3514 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3515 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3516 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3517 final long startTime = ss.getStartTime(batteryUptime, which);
3518 final int starts = ss.getStarts(which);
3519 final int launches = ss.getLaunches(which);
3520 if (startTime != 0 || starts != 0 || launches != 0) {
3521 dumpLine(pw, uid, category, APK_DATA,
3522 wakeups, // wakeup alarms
3523 packageStats.keyAt(ipkg), // Apk
3524 serviceStats.keyAt(isvc), // service
3525 startTime / 1000, // time spent started, in ms
3526 starts,
3527 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 }
3529 }
3530 }
3531 }
3532 }
3533
Dianne Hackborn81038902012-11-26 17:04:09 -08003534 static final class TimerEntry {
3535 final String mName;
3536 final int mId;
3537 final BatteryStats.Timer mTimer;
3538 final long mTime;
3539 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3540 mName = name;
3541 mId = id;
3542 mTimer = timer;
3543 mTime = time;
3544 }
3545 }
3546
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003547 private void printmAh(PrintWriter printer, double power) {
3548 printer.print(BatteryStatsHelper.makemAh(power));
3549 }
3550
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003551 private void printmAh(StringBuilder sb, double power) {
3552 sb.append(BatteryStatsHelper.makemAh(power));
3553 }
3554
Dianne Hackbornd953c532014-08-16 18:17:38 -07003555 /**
3556 * Temporary for settings.
3557 */
3558 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3559 int reqUid) {
3560 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3561 }
3562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003564 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003565 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3567 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3568 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569
3570 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3571 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3572 final long totalRealtime = computeRealtime(rawRealtime, which);
3573 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003574 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3575 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3576 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003577 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3578 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003579
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003580 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003581
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003582 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003583 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003585 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3586 if (estimatedBatteryCapacity > 0) {
3587 sb.setLength(0);
3588 sb.append(prefix);
3589 sb.append(" Estimated battery capacity: ");
3590 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3591 sb.append(" mAh");
3592 pw.println(sb.toString());
3593 }
3594
Jocelyn Dangc627d102017-04-14 13:15:14 -07003595 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
3596 if (minLearnedBatteryCapacity > 0) {
3597 sb.setLength(0);
3598 sb.append(prefix);
3599 sb.append(" Min learned battery capacity: ");
3600 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
3601 sb.append(" mAh");
3602 pw.println(sb.toString());
3603 }
3604 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
3605 if (maxLearnedBatteryCapacity > 0) {
3606 sb.setLength(0);
3607 sb.append(prefix);
3608 sb.append(" Max learned battery capacity: ");
3609 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
3610 sb.append(" mAh");
3611 pw.println(sb.toString());
3612 }
3613
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003614 sb.setLength(0);
3615 sb.append(prefix);
3616 sb.append(" Time on battery: ");
3617 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3618 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3619 sb.append(") realtime, ");
3620 formatTimeMs(sb, whichBatteryUptime / 1000);
3621 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3622 sb.append(") uptime");
3623 pw.println(sb.toString());
3624 sb.setLength(0);
3625 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003626 sb.append(" Time on battery screen off: ");
3627 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3628 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3629 sb.append(") realtime, ");
3630 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3631 sb.append("(");
3632 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3633 sb.append(") uptime");
3634 pw.println(sb.toString());
3635 sb.setLength(0);
3636 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003637 sb.append(" Total run time: ");
3638 formatTimeMs(sb, totalRealtime / 1000);
3639 sb.append("realtime, ");
3640 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003641 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003642 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003643 if (batteryTimeRemaining >= 0) {
3644 sb.setLength(0);
3645 sb.append(prefix);
3646 sb.append(" Battery time remaining: ");
3647 formatTimeMs(sb, batteryTimeRemaining / 1000);
3648 pw.println(sb.toString());
3649 }
3650 if (chargeTimeRemaining >= 0) {
3651 sb.setLength(0);
3652 sb.append(prefix);
3653 sb.append(" Charge time remaining: ");
3654 formatTimeMs(sb, chargeTimeRemaining / 1000);
3655 pw.println(sb.toString());
3656 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003657
3658 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3659 final long dischargeCount = dischargeCounter.getCountLocked(which);
3660 if (dischargeCount >= 0) {
3661 sb.setLength(0);
3662 sb.append(prefix);
3663 sb.append(" Discharge: ");
3664 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3665 sb.append(" mAh");
3666 pw.println(sb.toString());
3667 }
3668
3669 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3670 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3671 if (dischargeScreenOffCount >= 0) {
3672 sb.setLength(0);
3673 sb.append(prefix);
3674 sb.append(" Screen off discharge: ");
3675 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3676 sb.append(" mAh");
3677 pw.println(sb.toString());
3678 }
3679
3680 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3681 if (dischargeScreenOnCount >= 0) {
3682 sb.setLength(0);
3683 sb.append(prefix);
3684 sb.append(" Screen on discharge: ");
3685 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3686 sb.append(" mAh");
3687 pw.println(sb.toString());
3688 }
3689
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003690 pw.print(" Start clock time: ");
3691 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3692
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003693 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003694 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003695 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003696 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3697 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003698 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003699 rawRealtime, which);
3700 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3701 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003702 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003703 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003704 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3705 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3706 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003707 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003708 sb.append(prefix);
3709 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3710 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003711 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003712 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3713 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003714 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003715 pw.println(sb.toString());
3716 sb.setLength(0);
3717 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003718 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003719 boolean didOne = false;
3720 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003721 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003722 if (time == 0) {
3723 continue;
3724 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003725 sb.append("\n ");
3726 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003727 didOne = true;
3728 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3729 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003730 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003731 sb.append("(");
3732 sb.append(formatRatioLocked(time, screenOnTime));
3733 sb.append(")");
3734 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003735 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003736 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003737 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003738 sb.setLength(0);
3739 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003740 sb.append(" Power save mode enabled: ");
3741 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003742 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003743 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003744 sb.append(")");
3745 pw.println(sb.toString());
3746 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003747 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003748 sb.setLength(0);
3749 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003750 sb.append(" Device light idling: ");
3751 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003752 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003753 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3754 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003755 sb.append("x");
3756 pw.println(sb.toString());
3757 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003758 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003759 sb.setLength(0);
3760 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003761 sb.append(" Idle mode light time: ");
3762 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003763 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003764 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3765 sb.append(") ");
3766 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003767 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003768 sb.append(" -- longest ");
3769 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3770 pw.println(sb.toString());
3771 }
3772 if (deviceIdlingTime != 0) {
3773 sb.setLength(0);
3774 sb.append(prefix);
3775 sb.append(" Device full idling: ");
3776 formatTimeMs(sb, deviceIdlingTime / 1000);
3777 sb.append("(");
3778 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003779 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003780 sb.append("x");
3781 pw.println(sb.toString());
3782 }
3783 if (deviceIdleModeFullTime != 0) {
3784 sb.setLength(0);
3785 sb.append(prefix);
3786 sb.append(" Idle mode full time: ");
3787 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3788 sb.append("(");
3789 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3790 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003791 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003792 sb.append("x");
3793 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003794 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003795 pw.println(sb.toString());
3796 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003797 if (phoneOnTime != 0) {
3798 sb.setLength(0);
3799 sb.append(prefix);
3800 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3801 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003802 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003803 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003804 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003805 if (connChanges != 0) {
3806 pw.print(prefix);
3807 pw.print(" Connectivity changes: "); pw.println(connChanges);
3808 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003809
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003810 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003811 long fullWakeLockTimeTotalMicros = 0;
3812 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003813
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003814 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003815
Evan Millar22ac0432009-03-31 11:33:18 -07003816 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003817 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003818
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003819 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3820 = u.getWakelockStats();
3821 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3822 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003823
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003824 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3825 if (fullWakeTimer != null) {
3826 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3827 rawRealtime, which);
3828 }
3829
3830 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3831 if (partialWakeTimer != null) {
3832 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3833 rawRealtime, which);
3834 if (totalTimeMicros > 0) {
3835 if (reqUid < 0) {
3836 // Only show the ordered list of all wake
3837 // locks if the caller is not asking for data
3838 // about a specific uid.
3839 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3840 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003841 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003842 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003843 }
3844 }
3845 }
3846 }
3847
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003848 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3849 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3850 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3851 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3852 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3853 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3854 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3855 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003856 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3857 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003858
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003859 if (fullWakeLockTimeTotalMicros != 0) {
3860 sb.setLength(0);
3861 sb.append(prefix);
3862 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3863 (fullWakeLockTimeTotalMicros + 500) / 1000);
3864 pw.println(sb.toString());
3865 }
3866
3867 if (partialWakeLockTimeTotalMicros != 0) {
3868 sb.setLength(0);
3869 sb.append(prefix);
3870 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3871 (partialWakeLockTimeTotalMicros + 500) / 1000);
3872 pw.println(sb.toString());
3873 }
3874
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003875 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003876 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3877 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3878 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3879 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003880 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003881 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003882 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003883 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003884 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003885 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003886 if (time == 0) {
3887 continue;
3888 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003889 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003890 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003891 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003892 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003893 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003894 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003895 sb.append("(");
3896 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003897 sb.append(") ");
3898 sb.append(getPhoneSignalStrengthCount(i, which));
3899 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003900 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003901 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003902 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003903
3904 sb.setLength(0);
3905 sb.append(prefix);
3906 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003907 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003908 pw.println(sb.toString());
3909
Dianne Hackborn627bba72009-03-24 22:32:56 -07003910 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003911 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003912 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003913 didOne = false;
3914 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003915 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003916 if (time == 0) {
3917 continue;
3918 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003919 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003920 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003921 didOne = true;
3922 sb.append(DATA_CONNECTION_NAMES[i]);
3923 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003924 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003925 sb.append("(");
3926 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003927 sb.append(") ");
3928 sb.append(getPhoneDataConnectionCount(i, which));
3929 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003930 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003931 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003932 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003933
3934 sb.setLength(0);
3935 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003936 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003937 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003938 formatTimeMs(sb, mobileActiveTime / 1000);
3939 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3940 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3941 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003942 pw.println(sb.toString());
3943
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003944 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3945 if (mobileActiveUnknownTime != 0) {
3946 sb.setLength(0);
3947 sb.append(prefix);
3948 sb.append(" Mobile radio active unknown time: ");
3949 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3950 sb.append("(");
3951 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3952 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3953 sb.append("x");
3954 pw.println(sb.toString());
3955 }
3956
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003957 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3958 if (mobileActiveAdjustedTime != 0) {
3959 sb.setLength(0);
3960 sb.append(prefix);
3961 sb.append(" Mobile radio active adjusted time: ");
3962 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3963 sb.append("(");
3964 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3965 sb.append(")");
3966 pw.println(sb.toString());
3967 }
3968
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003969 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3970
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003971 pw.print(prefix);
3972 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3973 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3974 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3975 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003976 sb.setLength(0);
3977 sb.append(prefix);
3978 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3979 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3980 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3981 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003982 sb.append(")");
3983 pw.println(sb.toString());
3984
3985 sb.setLength(0);
3986 sb.append(prefix);
3987 sb.append(" Wifi states:");
3988 didOne = false;
3989 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003990 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003991 if (time == 0) {
3992 continue;
3993 }
3994 sb.append("\n ");
3995 didOne = true;
3996 sb.append(WIFI_STATE_NAMES[i]);
3997 sb.append(" ");
3998 formatTimeMs(sb, time/1000);
3999 sb.append("(");
4000 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4001 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004002 sb.append(getWifiStateCount(i, which));
4003 sb.append("x");
4004 }
4005 if (!didOne) sb.append(" (no activity)");
4006 pw.println(sb.toString());
4007
4008 sb.setLength(0);
4009 sb.append(prefix);
4010 sb.append(" Wifi supplicant states:");
4011 didOne = false;
4012 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4013 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4014 if (time == 0) {
4015 continue;
4016 }
4017 sb.append("\n ");
4018 didOne = true;
4019 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4020 sb.append(" ");
4021 formatTimeMs(sb, time/1000);
4022 sb.append("(");
4023 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4024 sb.append(") ");
4025 sb.append(getWifiSupplStateCount(i, which));
4026 sb.append("x");
4027 }
4028 if (!didOne) sb.append(" (no activity)");
4029 pw.println(sb.toString());
4030
4031 sb.setLength(0);
4032 sb.append(prefix);
4033 sb.append(" Wifi signal levels:");
4034 didOne = false;
4035 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4036 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4037 if (time == 0) {
4038 continue;
4039 }
4040 sb.append("\n ");
4041 sb.append(prefix);
4042 didOne = true;
4043 sb.append("level(");
4044 sb.append(i);
4045 sb.append(") ");
4046 formatTimeMs(sb, time/1000);
4047 sb.append("(");
4048 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4049 sb.append(") ");
4050 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004051 sb.append("x");
4052 }
4053 if (!didOne) sb.append(" (no activity)");
4054 pw.println(sb.toString());
4055
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004056 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004057
Adam Lesinski50e47602015-12-04 17:04:54 -08004058 pw.print(prefix);
4059 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4060 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4061
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004062 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4063 sb.setLength(0);
4064 sb.append(prefix);
4065 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4066 pw.println(sb.toString());
4067
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004068 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4069 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004070
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004071 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004072
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004073 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004074 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004075 pw.print(prefix); pw.println(" Device is currently unplugged");
4076 pw.print(prefix); pw.print(" Discharge cycle start level: ");
4077 pw.println(getDischargeStartLevel());
4078 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4079 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004080 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004081 pw.print(prefix); pw.println(" Device is currently plugged into power");
4082 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
4083 pw.println(getDischargeStartLevel());
4084 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
4085 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004086 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004087 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4088 pw.println(getDischargeAmountScreenOn());
4089 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4090 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004091 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004092 } else {
4093 pw.print(prefix); pw.println(" Device battery use since last full charge");
4094 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4095 pw.println(getLowDischargeAmountSinceCharge());
4096 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4097 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004098 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4099 pw.println(getDischargeAmountScreenOnSinceCharge());
4100 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4101 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004102 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004103 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004104
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004105 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004106 helper.create(this);
4107 helper.refreshStats(which, UserHandle.USER_ALL);
4108 List<BatterySipper> sippers = helper.getUsageList();
4109 if (sippers != null && sippers.size() > 0) {
4110 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4111 pw.print(prefix); pw.print(" Capacity: ");
4112 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004113 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004114 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4115 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4116 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4117 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004118 pw.println();
4119 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004120 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004121 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004122 switch (bs.drainType) {
4123 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004124 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004125 break;
4126 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004127 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004128 break;
4129 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004130 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004131 break;
4132 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004133 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004134 break;
4135 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004136 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004137 break;
4138 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004139 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004140 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004141 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004142 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004143 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004144 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004145 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004146 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004147 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004148 break;
4149 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004150 pw.print(" User "); pw.print(bs.userId);
4151 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004152 break;
4153 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004154 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004155 break;
4156 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004157 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004158 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004159 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004160 pw.print(" Camera: ");
4161 break;
4162 default:
4163 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004164 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004165 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004166 printmAh(pw, bs.totalPowerMah);
4167
Adam Lesinski57123002015-06-12 16:12:07 -07004168 if (bs.usagePowerMah != bs.totalPowerMah) {
4169 // If the usage (generic power) isn't the whole amount, we list out
4170 // what components are involved in the calculation.
4171
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004172 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004173 if (bs.usagePowerMah != 0) {
4174 pw.print(" usage=");
4175 printmAh(pw, bs.usagePowerMah);
4176 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004177 if (bs.cpuPowerMah != 0) {
4178 pw.print(" cpu=");
4179 printmAh(pw, bs.cpuPowerMah);
4180 }
4181 if (bs.wakeLockPowerMah != 0) {
4182 pw.print(" wake=");
4183 printmAh(pw, bs.wakeLockPowerMah);
4184 }
4185 if (bs.mobileRadioPowerMah != 0) {
4186 pw.print(" radio=");
4187 printmAh(pw, bs.mobileRadioPowerMah);
4188 }
4189 if (bs.wifiPowerMah != 0) {
4190 pw.print(" wifi=");
4191 printmAh(pw, bs.wifiPowerMah);
4192 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004193 if (bs.bluetoothPowerMah != 0) {
4194 pw.print(" bt=");
4195 printmAh(pw, bs.bluetoothPowerMah);
4196 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004197 if (bs.gpsPowerMah != 0) {
4198 pw.print(" gps=");
4199 printmAh(pw, bs.gpsPowerMah);
4200 }
4201 if (bs.sensorPowerMah != 0) {
4202 pw.print(" sensor=");
4203 printmAh(pw, bs.sensorPowerMah);
4204 }
4205 if (bs.cameraPowerMah != 0) {
4206 pw.print(" camera=");
4207 printmAh(pw, bs.cameraPowerMah);
4208 }
4209 if (bs.flashlightPowerMah != 0) {
4210 pw.print(" flash=");
4211 printmAh(pw, bs.flashlightPowerMah);
4212 }
4213 pw.print(" )");
4214 }
4215 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004216 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004217 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004218 }
4219
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004220 sippers = helper.getMobilemsppList();
4221 if (sippers != null && sippers.size() > 0) {
4222 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004223 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004224 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004225 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004226 sb.setLength(0);
4227 sb.append(prefix); sb.append(" Uid ");
4228 UserHandle.formatUid(sb, bs.uidObj.getUid());
4229 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4230 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4231 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004232 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004233 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004234 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004235 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004236 sb.setLength(0);
4237 sb.append(prefix);
4238 sb.append(" TOTAL TIME: ");
4239 formatTimeMs(sb, totalTime);
4240 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4241 sb.append(")");
4242 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004243 pw.println();
4244 }
4245
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004246 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4247 @Override
4248 public int compare(TimerEntry lhs, TimerEntry rhs) {
4249 long lhsTime = lhs.mTime;
4250 long rhsTime = rhs.mTime;
4251 if (lhsTime < rhsTime) {
4252 return 1;
4253 }
4254 if (lhsTime > rhsTime) {
4255 return -1;
4256 }
4257 return 0;
4258 }
4259 };
4260
4261 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004262 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4263 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004264 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004265 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4266 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4267 : kernelWakelocks.entrySet()) {
4268 final BatteryStats.Timer timer = ent.getValue();
4269 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004270 if (totalTimeMillis > 0) {
4271 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4272 }
4273 }
4274 if (ktimers.size() > 0) {
4275 Collections.sort(ktimers, timerComparator);
4276 pw.print(prefix); pw.println(" All kernel wake locks:");
4277 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004278 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004279 String linePrefix = ": ";
4280 sb.setLength(0);
4281 sb.append(prefix);
4282 sb.append(" Kernel Wake lock ");
4283 sb.append(timer.mName);
4284 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4285 which, linePrefix);
4286 if (!linePrefix.equals(": ")) {
4287 sb.append(" realtime");
4288 // Only print out wake locks that were held
4289 pw.println(sb.toString());
4290 }
4291 }
4292 pw.println();
4293 }
4294 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004295
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004296 if (timers.size() > 0) {
4297 Collections.sort(timers, timerComparator);
4298 pw.print(prefix); pw.println(" All partial wake locks:");
4299 for (int i=0; i<timers.size(); i++) {
4300 TimerEntry timer = timers.get(i);
4301 sb.setLength(0);
4302 sb.append(" Wake lock ");
4303 UserHandle.formatUid(sb, timer.mId);
4304 sb.append(" ");
4305 sb.append(timer.mName);
4306 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4307 sb.append(" realtime");
4308 pw.println(sb.toString());
4309 }
4310 timers.clear();
4311 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004312 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004313
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004314 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004315 if (wakeupReasons.size() > 0) {
4316 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004317 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004318 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004319 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004320 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4321 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004322 }
4323 Collections.sort(reasons, timerComparator);
4324 for (int i=0; i<reasons.size(); i++) {
4325 TimerEntry timer = reasons.get(i);
4326 String linePrefix = ": ";
4327 sb.setLength(0);
4328 sb.append(prefix);
4329 sb.append(" Wakeup reason ");
4330 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004331 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4332 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004333 pw.println(sb.toString());
4334 }
4335 pw.println();
4336 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004337 }
Evan Millar22ac0432009-03-31 11:33:18 -07004338
James Carr2dd7e5e2016-07-20 18:48:39 -07004339 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4340 pw.println("Memory Stats");
4341 for (int i = 0; i < mMemoryStats.size(); i++) {
4342 sb.setLength(0);
4343 sb.append("Bandwidth ");
4344 sb.append(mMemoryStats.keyAt(i));
4345 sb.append(" Time ");
4346 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4347 pw.println(sb.toString());
4348 }
4349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 for (int iu=0; iu<NU; iu++) {
4351 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004352 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004353 continue;
4354 }
4355
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004356 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004357
4358 pw.print(prefix);
4359 pw.print(" ");
4360 UserHandle.formatUid(pw, uid);
4361 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004362 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004363
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004364 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4365 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4366 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4367 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004368 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4369 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4370
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004371 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4372 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004373 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4374 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004375
4376 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4377 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4378
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004379 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4380 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4381 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004382 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4383 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4384 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4385 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004386 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004387
Adam Lesinski5f056f62016-07-14 16:56:08 -07004388 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4389 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4390
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004391 if (mobileRxBytes > 0 || mobileTxBytes > 0
4392 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004393 pw.print(prefix); pw.print(" Mobile network: ");
4394 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004395 pw.print(formatBytesLocked(mobileTxBytes));
4396 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4397 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004399 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4400 sb.setLength(0);
4401 sb.append(prefix); sb.append(" Mobile radio active: ");
4402 formatTimeMs(sb, uidMobileActiveTime / 1000);
4403 sb.append("(");
4404 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4405 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4406 long packets = mobileRxPackets + mobileTxPackets;
4407 if (packets == 0) {
4408 packets = 1;
4409 }
4410 sb.append(" @ ");
4411 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4412 sb.append(" mspp");
4413 pw.println(sb.toString());
4414 }
4415
Adam Lesinski5f056f62016-07-14 16:56:08 -07004416 if (mobileWakeup > 0) {
4417 sb.setLength(0);
4418 sb.append(prefix);
4419 sb.append(" Mobile radio AP wakeups: ");
4420 sb.append(mobileWakeup);
4421 pw.println(sb.toString());
4422 }
4423
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004424 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4425 u.getModemControllerActivity(), which);
4426
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004427 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004428 pw.print(prefix); pw.print(" Wi-Fi network: ");
4429 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004430 pw.print(formatBytesLocked(wifiTxBytes));
4431 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4432 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004433 }
4434
Dianne Hackborn62793e42015-03-09 11:15:41 -07004435 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004436 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004437 || uidWifiRunningTime != 0) {
4438 sb.setLength(0);
4439 sb.append(prefix); sb.append(" Wifi Running: ");
4440 formatTimeMs(sb, uidWifiRunningTime / 1000);
4441 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4442 whichBatteryRealtime)); sb.append(")\n");
4443 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4444 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4445 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4446 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004447 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004448 formatTimeMs(sb, wifiScanTime / 1000);
4449 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004450 whichBatteryRealtime)); sb.append(") ");
4451 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004452 sb.append("x\n");
4453 // actual and background times are unpooled and since reset (regardless of 'which')
4454 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4455 formatTimeMs(sb, wifiScanActualTime / 1000);
4456 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4457 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4458 sb.append(") ");
4459 sb.append(wifiScanCount);
4460 sb.append("x\n");
4461 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4462 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4463 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4464 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4465 sb.append(") ");
4466 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004467 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004468 pw.println(sb.toString());
4469 }
4470
Adam Lesinski5f056f62016-07-14 16:56:08 -07004471 if (wifiWakeup > 0) {
4472 sb.setLength(0);
4473 sb.append(prefix);
4474 sb.append(" WiFi AP wakeups: ");
4475 sb.append(wifiWakeup);
4476 pw.println(sb.toString());
4477 }
4478
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004479 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4480 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004481
Adam Lesinski50e47602015-12-04 17:04:54 -08004482 if (btRxBytes > 0 || btTxBytes > 0) {
4483 pw.print(prefix); pw.print(" Bluetooth network: ");
4484 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4485 pw.print(formatBytesLocked(btTxBytes));
4486 pw.println(" sent");
4487 }
4488
Bookatz867c0d72017-03-07 18:23:42 -08004489 final Timer bleTimer = u.getBluetoothScanTimer();
4490 if (bleTimer != null) {
4491 // Convert from microseconds to milliseconds with rounding
4492 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4493 / 1000;
4494 if (totalTimeMs != 0) {
4495 final int count = bleTimer.getCountLocked(which);
4496 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4497 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
4498 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4499 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4500 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4501 final long actualTimeMsBg = bleTimerBg != null ?
4502 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4503
4504 sb.setLength(0);
4505 sb.append(prefix);
4506 sb.append(" ");
4507 sb.append("Bluetooth Scan");
4508 sb.append(": ");
4509 if (actualTimeMs != totalTimeMs) {
4510 formatTimeMs(sb, totalTimeMs);
4511 sb.append("blamed realtime, ");
4512 }
4513 formatTimeMs(sb, actualTimeMs); // since reset, regardless of 'which'
4514 sb.append("realtime (");
4515 sb.append(count);
4516 sb.append(" times)");
4517 if (bleTimer.isRunningLocked()) {
4518 sb.append(" (running)");
4519 }
4520 if (actualTimeMsBg != 0 || countBg > 0) {
4521 sb.append(", ");
4522 formatTimeMs(sb, actualTimeMsBg); // since reset, regardless of 'which'
4523 sb.append("background (");
4524 sb.append(countBg);
4525 sb.append(" times)");
4526 }
4527 pw.println(sb.toString());
4528 uidActivity = true;
4529 }
4530 }
4531
4532
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004533
Dianne Hackborn617f8772009-03-31 15:04:46 -07004534 if (u.hasUserActivity()) {
4535 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004536 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004537 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004538 if (val != 0) {
4539 if (!hasData) {
4540 sb.setLength(0);
4541 sb.append(" User activity: ");
4542 hasData = true;
4543 } else {
4544 sb.append(", ");
4545 }
4546 sb.append(val);
4547 sb.append(" ");
4548 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4549 }
4550 }
4551 if (hasData) {
4552 pw.println(sb.toString());
4553 }
4554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004555
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004556 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4557 = u.getWakelockStats();
4558 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004559 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004560 int countWakelock = 0;
4561 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4562 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4563 String linePrefix = ": ";
4564 sb.setLength(0);
4565 sb.append(prefix);
4566 sb.append(" Wake lock ");
4567 sb.append(wakelocks.keyAt(iw));
4568 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4569 "full", which, linePrefix);
4570 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4571 "partial", which, linePrefix);
4572 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4573 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004574 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4575 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004576 sb.append(" realtime");
4577 pw.println(sb.toString());
4578 uidActivity = true;
4579 countWakelock++;
4580
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004581 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4582 rawRealtime, which);
4583 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4584 rawRealtime, which);
4585 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4586 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004587 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004588 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004589 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004590 if (countWakelock > 1) {
4591 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4592 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004593 sb.setLength(0);
4594 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004595 sb.append(" TOTAL wake: ");
4596 boolean needComma = false;
4597 if (totalFullWakelock != 0) {
4598 needComma = true;
4599 formatTimeMs(sb, totalFullWakelock);
4600 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004601 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004602 if (totalPartialWakelock != 0) {
4603 if (needComma) {
4604 sb.append(", ");
4605 }
4606 needComma = true;
4607 formatTimeMs(sb, totalPartialWakelock);
4608 sb.append("partial");
4609 }
4610 if (totalWindowWakelock != 0) {
4611 if (needComma) {
4612 sb.append(", ");
4613 }
4614 needComma = true;
4615 formatTimeMs(sb, totalWindowWakelock);
4616 sb.append("window");
4617 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004618 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004619 if (needComma) {
4620 sb.append(",");
4621 }
4622 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004623 formatTimeMs(sb, totalDrawWakelock);
4624 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004625 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004626 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004627 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004628 }
4629 }
4630
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004631 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4632 for (int isy=syncs.size()-1; isy>=0; isy--) {
4633 final Timer timer = syncs.valueAt(isy);
4634 // Convert from microseconds to milliseconds with rounding
4635 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4636 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004637 final Timer bgTimer = timer.getSubTimer();
4638 final long bgTime = bgTimer != null ?
4639 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4640 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004641 sb.setLength(0);
4642 sb.append(prefix);
4643 sb.append(" Sync ");
4644 sb.append(syncs.keyAt(isy));
4645 sb.append(": ");
4646 if (totalTime != 0) {
4647 formatTimeMs(sb, totalTime);
4648 sb.append("realtime (");
4649 sb.append(count);
4650 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07004651 if (bgTime > 0) {
4652 sb.append(", ");
4653 formatTimeMs(sb, bgTime);
4654 sb.append("background (");
4655 sb.append(bgCount);
4656 sb.append(" times)");
4657 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004658 } else {
4659 sb.append("(not used)");
4660 }
4661 pw.println(sb.toString());
4662 uidActivity = true;
4663 }
4664
4665 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4666 for (int ij=jobs.size()-1; ij>=0; ij--) {
4667 final Timer timer = jobs.valueAt(ij);
4668 // Convert from microseconds to milliseconds with rounding
4669 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4670 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004671 final Timer bgTimer = timer.getSubTimer();
4672 final long bgTime = bgTimer != null ?
4673 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4674 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004675 sb.setLength(0);
4676 sb.append(prefix);
4677 sb.append(" Job ");
4678 sb.append(jobs.keyAt(ij));
4679 sb.append(": ");
4680 if (totalTime != 0) {
4681 formatTimeMs(sb, totalTime);
4682 sb.append("realtime (");
4683 sb.append(count);
4684 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07004685 if (bgTime > 0) {
4686 sb.append(", ");
4687 formatTimeMs(sb, bgTime);
4688 sb.append("background (");
4689 sb.append(bgCount);
4690 sb.append(" times)");
4691 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004692 } else {
4693 sb.append("(not used)");
4694 }
4695 pw.println(sb.toString());
4696 uidActivity = true;
4697 }
4698
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004699 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4700 prefix, "Flashlight");
4701 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4702 prefix, "Camera");
4703 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4704 prefix, "Video");
4705 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4706 prefix, "Audio");
4707
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004708 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4709 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004710 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004711 final Uid.Sensor se = sensors.valueAt(ise);
4712 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004713 sb.setLength(0);
4714 sb.append(prefix);
4715 sb.append(" Sensor ");
4716 int handle = se.getHandle();
4717 if (handle == Uid.Sensor.GPS) {
4718 sb.append("GPS");
4719 } else {
4720 sb.append(handle);
4721 }
4722 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004724 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004725 if (timer != null) {
4726 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08004727 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4728 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004729 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08004730 final Timer bgTimer = se.getSensorBackgroundTime();
4731 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
4732 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4733 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4734 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4735 final long bgActualTime = bgTimer != null ?
4736 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4737
Dianne Hackborn61659e52014-07-09 16:13:01 -07004738 //timer.logState();
4739 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004740 if (actualTime != totalTime) {
4741 formatTimeMs(sb, totalTime);
4742 sb.append("blamed realtime, ");
4743 }
4744
4745 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07004746 sb.append("realtime (");
4747 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08004748 sb.append(" times)");
4749
4750 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004751 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08004752 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
4753 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004754 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08004755 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 } else {
4758 sb.append("(not used)");
4759 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004760 } else {
4761 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004762 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004763
4764 pw.println(sb.toString());
4765 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004766 }
4767
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004768 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4769 "Vibrator");
4770 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4771 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004772
Dianne Hackborn61659e52014-07-09 16:13:01 -07004773 long totalStateTime = 0;
4774 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4775 long time = u.getProcessStateTime(ips, rawRealtime, which);
4776 if (time > 0) {
4777 totalStateTime += time;
4778 sb.setLength(0);
4779 sb.append(prefix);
4780 sb.append(" ");
4781 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4782 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004783 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004784 pw.println(sb.toString());
4785 uidActivity = true;
4786 }
4787 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004788 if (totalStateTime > 0) {
4789 sb.setLength(0);
4790 sb.append(prefix);
4791 sb.append(" Total running: ");
4792 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4793 pw.println(sb.toString());
4794 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004795
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004796 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4797 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004798 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004799 sb.setLength(0);
4800 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004801 sb.append(" Total cpu time: u=");
4802 formatTimeMs(sb, userCpuTimeUs / 1000);
4803 sb.append("s=");
4804 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004805 pw.println(sb.toString());
4806 }
4807
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004808 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4809 = u.getProcessStats();
4810 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4811 final Uid.Proc ps = processStats.valueAt(ipr);
4812 long userTime;
4813 long systemTime;
4814 long foregroundTime;
4815 int starts;
4816 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004817
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004818 userTime = ps.getUserTime(which);
4819 systemTime = ps.getSystemTime(which);
4820 foregroundTime = ps.getForegroundTime(which);
4821 starts = ps.getStarts(which);
4822 final int numCrashes = ps.getNumCrashes(which);
4823 final int numAnrs = ps.getNumAnrs(which);
4824 numExcessive = which == STATS_SINCE_CHARGED
4825 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004827 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4828 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4829 sb.setLength(0);
4830 sb.append(prefix); sb.append(" Proc ");
4831 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4832 sb.append(prefix); sb.append(" CPU: ");
4833 formatTimeMs(sb, userTime); sb.append("usr + ");
4834 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4835 formatTimeMs(sb, foregroundTime); sb.append("fg");
4836 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4837 sb.append("\n"); sb.append(prefix); sb.append(" ");
4838 boolean hasOne = false;
4839 if (starts != 0) {
4840 hasOne = true;
4841 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004842 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004843 if (numCrashes != 0) {
4844 if (hasOne) {
4845 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004846 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004847 hasOne = true;
4848 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004849 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004850 if (numAnrs != 0) {
4851 if (hasOne) {
4852 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004854 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004855 }
4856 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004857 pw.println(sb.toString());
4858 for (int e=0; e<numExcessive; e++) {
4859 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4860 if (ew != null) {
4861 pw.print(prefix); pw.print(" * Killed for ");
4862 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4863 pw.print("wake lock");
4864 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4865 pw.print("cpu");
4866 } else {
4867 pw.print("unknown");
4868 }
4869 pw.print(" use: ");
4870 TimeUtils.formatDuration(ew.usedTime, pw);
4871 pw.print(" over ");
4872 TimeUtils.formatDuration(ew.overTime, pw);
4873 if (ew.overTime != 0) {
4874 pw.print(" (");
4875 pw.print((ew.usedTime*100)/ew.overTime);
4876 pw.println("%)");
4877 }
4878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004879 }
4880 uidActivity = true;
4881 }
4882 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004883
4884 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4885 = u.getPackageStats();
4886 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4887 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4888 pw.println(":");
4889 boolean apkActivity = false;
4890 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4891 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4892 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4893 pw.print(prefix); pw.print(" Wakeup alarm ");
4894 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4895 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4896 pw.println(" times");
4897 apkActivity = true;
4898 }
4899 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4900 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4901 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4902 final long startTime = ss.getStartTime(batteryUptime, which);
4903 final int starts = ss.getStarts(which);
4904 final int launches = ss.getLaunches(which);
4905 if (startTime != 0 || starts != 0 || launches != 0) {
4906 sb.setLength(0);
4907 sb.append(prefix); sb.append(" Service ");
4908 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4909 sb.append(prefix); sb.append(" Created for: ");
4910 formatTimeMs(sb, startTime / 1000);
4911 sb.append("uptime\n");
4912 sb.append(prefix); sb.append(" Starts: ");
4913 sb.append(starts);
4914 sb.append(", launches: "); sb.append(launches);
4915 pw.println(sb.toString());
4916 apkActivity = true;
4917 }
4918 }
4919 if (!apkActivity) {
4920 pw.print(prefix); pw.println(" (nothing executed)");
4921 }
4922 uidActivity = true;
4923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004925 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 }
4927 }
4928 }
4929
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004930 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004931 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004932 int diff = oldval ^ newval;
4933 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004934 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004935 for (int i=0; i<descriptions.length; i++) {
4936 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004937 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004938 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004939 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004940 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004941 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004942 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4943 didWake = true;
4944 pw.print("=");
4945 if (longNames) {
4946 UserHandle.formatUid(pw, wakelockTag.uid);
4947 pw.print(":\"");
4948 pw.print(wakelockTag.string);
4949 pw.print("\"");
4950 } else {
4951 pw.print(wakelockTag.poolIdx);
4952 }
4953 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004954 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004955 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004956 pw.print("=");
4957 int val = (newval&bd.mask)>>bd.shift;
4958 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004959 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004960 } else {
4961 pw.print(val);
4962 }
4963 }
4964 }
4965 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004966 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004967 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004968 if (longNames) {
4969 UserHandle.formatUid(pw, wakelockTag.uid);
4970 pw.print(":\"");
4971 pw.print(wakelockTag.string);
4972 pw.print("\"");
4973 } else {
4974 pw.print(wakelockTag.poolIdx);
4975 }
4976 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004977 }
4978
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004979 public void prepareForDumpLocked() {
4980 }
4981
4982 public static class HistoryPrinter {
4983 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004984 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004985 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004986 int oldStatus = -1;
4987 int oldHealth = -1;
4988 int oldPlug = -1;
4989 int oldTemp = -1;
4990 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004991 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004992 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004993
Dianne Hackborn3251b902014-06-20 14:40:53 -07004994 void reset() {
4995 oldState = oldState2 = 0;
4996 oldLevel = -1;
4997 oldStatus = -1;
4998 oldHealth = -1;
4999 oldPlug = -1;
5000 oldTemp = -1;
5001 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005002 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005003 }
5004
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005005 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005006 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005007 if (!checkin) {
5008 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005009 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005010 pw.print(" (");
5011 pw.print(rec.numReadInts);
5012 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005013 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005014 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5015 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005016 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005017 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005018 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005019 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005020 }
5021 lastTime = rec.time;
5022 }
5023 if (rec.cmd == HistoryItem.CMD_START) {
5024 if (checkin) {
5025 pw.print(":");
5026 }
5027 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005028 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005029 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5030 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005031 if (checkin) {
5032 pw.print(":");
5033 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005034 if (rec.cmd == HistoryItem.CMD_RESET) {
5035 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005036 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005037 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005038 pw.print("TIME:");
5039 if (checkin) {
5040 pw.println(rec.currentTime);
5041 } else {
5042 pw.print(" ");
5043 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5044 rec.currentTime).toString());
5045 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005046 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5047 if (checkin) {
5048 pw.print(":");
5049 }
5050 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005051 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5052 if (checkin) {
5053 pw.print(":");
5054 }
5055 pw.println("*OVERFLOW*");
5056 } else {
5057 if (!checkin) {
5058 if (rec.batteryLevel < 10) pw.print("00");
5059 else if (rec.batteryLevel < 100) pw.print("0");
5060 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005061 if (verbose) {
5062 pw.print(" ");
5063 if (rec.states < 0) ;
5064 else if (rec.states < 0x10) pw.print("0000000");
5065 else if (rec.states < 0x100) pw.print("000000");
5066 else if (rec.states < 0x1000) pw.print("00000");
5067 else if (rec.states < 0x10000) pw.print("0000");
5068 else if (rec.states < 0x100000) pw.print("000");
5069 else if (rec.states < 0x1000000) pw.print("00");
5070 else if (rec.states < 0x10000000) pw.print("0");
5071 pw.print(Integer.toHexString(rec.states));
5072 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005073 } else {
5074 if (oldLevel != rec.batteryLevel) {
5075 oldLevel = rec.batteryLevel;
5076 pw.print(",Bl="); pw.print(rec.batteryLevel);
5077 }
5078 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005079 if (oldStatus != rec.batteryStatus) {
5080 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005081 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005082 switch (oldStatus) {
5083 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005084 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005085 break;
5086 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005087 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005088 break;
5089 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005090 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005091 break;
5092 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005093 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005094 break;
5095 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005096 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005097 break;
5098 default:
5099 pw.print(oldStatus);
5100 break;
5101 }
5102 }
5103 if (oldHealth != rec.batteryHealth) {
5104 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005105 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005106 switch (oldHealth) {
5107 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005108 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005109 break;
5110 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005111 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005112 break;
5113 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005114 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005115 break;
5116 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005117 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005118 break;
5119 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005120 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005121 break;
5122 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005123 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005124 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005125 case BatteryManager.BATTERY_HEALTH_COLD:
5126 pw.print(checkin ? "c" : "cold");
5127 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005128 default:
5129 pw.print(oldHealth);
5130 break;
5131 }
5132 }
5133 if (oldPlug != rec.batteryPlugType) {
5134 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005135 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005136 switch (oldPlug) {
5137 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005138 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005139 break;
5140 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005141 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005142 break;
5143 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005144 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005145 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005146 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005147 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005148 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005149 default:
5150 pw.print(oldPlug);
5151 break;
5152 }
5153 }
5154 if (oldTemp != rec.batteryTemperature) {
5155 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005156 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005157 pw.print(oldTemp);
5158 }
5159 if (oldVolt != rec.batteryVoltage) {
5160 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005161 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005162 pw.print(oldVolt);
5163 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005164 final int chargeMAh = rec.batteryChargeUAh / 1000;
5165 if (oldChargeMAh != chargeMAh) {
5166 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005167 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005168 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005169 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005170 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005171 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005172 printBitDescriptions(pw, oldState2, rec.states2, null,
5173 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005174 if (rec.wakeReasonTag != null) {
5175 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005176 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005177 pw.print(rec.wakeReasonTag.poolIdx);
5178 } else {
5179 pw.print(" wake_reason=");
5180 pw.print(rec.wakeReasonTag.uid);
5181 pw.print(":\"");
5182 pw.print(rec.wakeReasonTag.string);
5183 pw.print("\"");
5184 }
5185 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005186 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005187 pw.print(checkin ? "," : " ");
5188 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5189 pw.print("+");
5190 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5191 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005192 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005193 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5194 : HISTORY_EVENT_NAMES;
5195 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5196 | HistoryItem.EVENT_FLAG_FINISH);
5197 if (idx >= 0 && idx < eventNames.length) {
5198 pw.print(eventNames[idx]);
5199 } else {
5200 pw.print(checkin ? "Ev" : "event");
5201 pw.print(idx);
5202 }
5203 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005204 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005205 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005206 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005207 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5208 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005209 pw.print(":\"");
5210 pw.print(rec.eventTag.string);
5211 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005212 }
5213 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005214 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005215 if (rec.stepDetails != null) {
5216 if (!checkin) {
5217 pw.print(" Details: cpu=");
5218 pw.print(rec.stepDetails.userTime);
5219 pw.print("u+");
5220 pw.print(rec.stepDetails.systemTime);
5221 pw.print("s");
5222 if (rec.stepDetails.appCpuUid1 >= 0) {
5223 pw.print(" (");
5224 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5225 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5226 if (rec.stepDetails.appCpuUid2 >= 0) {
5227 pw.print(", ");
5228 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5229 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5230 }
5231 if (rec.stepDetails.appCpuUid3 >= 0) {
5232 pw.print(", ");
5233 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5234 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5235 }
5236 pw.print(')');
5237 }
5238 pw.println();
5239 pw.print(" /proc/stat=");
5240 pw.print(rec.stepDetails.statUserTime);
5241 pw.print(" usr, ");
5242 pw.print(rec.stepDetails.statSystemTime);
5243 pw.print(" sys, ");
5244 pw.print(rec.stepDetails.statIOWaitTime);
5245 pw.print(" io, ");
5246 pw.print(rec.stepDetails.statIrqTime);
5247 pw.print(" irq, ");
5248 pw.print(rec.stepDetails.statSoftIrqTime);
5249 pw.print(" sirq, ");
5250 pw.print(rec.stepDetails.statIdlTime);
5251 pw.print(" idle");
5252 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5253 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5254 + rec.stepDetails.statSoftIrqTime;
5255 int total = totalRun + rec.stepDetails.statIdlTime;
5256 if (total > 0) {
5257 pw.print(" (");
5258 float perc = ((float)totalRun) / ((float)total) * 100;
5259 pw.print(String.format("%.1f%%", perc));
5260 pw.print(" of ");
5261 StringBuilder sb = new StringBuilder(64);
5262 formatTimeMsNoSpace(sb, total*10);
5263 pw.print(sb);
5264 pw.print(")");
5265 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005266 pw.print(", PlatformIdleStat ");
5267 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005268 pw.println();
5269 } else {
5270 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5271 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5272 pw.print(rec.stepDetails.userTime);
5273 pw.print(":");
5274 pw.print(rec.stepDetails.systemTime);
5275 if (rec.stepDetails.appCpuUid1 >= 0) {
5276 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5277 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5278 if (rec.stepDetails.appCpuUid2 >= 0) {
5279 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5280 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5281 }
5282 if (rec.stepDetails.appCpuUid3 >= 0) {
5283 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5284 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5285 }
5286 }
5287 pw.println();
5288 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5289 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5290 pw.print(rec.stepDetails.statUserTime);
5291 pw.print(',');
5292 pw.print(rec.stepDetails.statSystemTime);
5293 pw.print(',');
5294 pw.print(rec.stepDetails.statIOWaitTime);
5295 pw.print(',');
5296 pw.print(rec.stepDetails.statIrqTime);
5297 pw.print(',');
5298 pw.print(rec.stepDetails.statSoftIrqTime);
5299 pw.print(',');
5300 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005301 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005302 if (rec.stepDetails.statPlatformIdleState != null) {
5303 pw.print(rec.stepDetails.statPlatformIdleState);
5304 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005305 pw.println();
5306 }
5307 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005308 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005309 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005310 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005311 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005312
5313 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5314 UserHandle.formatUid(pw, uid);
5315 pw.print("=");
5316 pw.print(utime);
5317 pw.print("u+");
5318 pw.print(stime);
5319 pw.print("s");
5320 }
5321
5322 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5323 pw.print('/');
5324 pw.print(uid);
5325 pw.print(":");
5326 pw.print(utime);
5327 pw.print(":");
5328 pw.print(stime);
5329 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005330 }
5331
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005332 private void printSizeValue(PrintWriter pw, long size) {
5333 float result = size;
5334 String suffix = "";
5335 if (result >= 10*1024) {
5336 suffix = "KB";
5337 result = result / 1024;
5338 }
5339 if (result >= 10*1024) {
5340 suffix = "MB";
5341 result = result / 1024;
5342 }
5343 if (result >= 10*1024) {
5344 suffix = "GB";
5345 result = result / 1024;
5346 }
5347 if (result >= 10*1024) {
5348 suffix = "TB";
5349 result = result / 1024;
5350 }
5351 if (result >= 10*1024) {
5352 suffix = "PB";
5353 result = result / 1024;
5354 }
5355 pw.print((int)result);
5356 pw.print(suffix);
5357 }
5358
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005359 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5360 String label3, long estimatedTime) {
5361 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005362 return false;
5363 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005364 pw.print(label1);
5365 pw.print(label2);
5366 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005367 StringBuilder sb = new StringBuilder(64);
5368 formatTimeMs(sb, estimatedTime);
5369 pw.print(sb);
5370 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005371 return true;
5372 }
5373
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005374 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5375 LevelStepTracker steps, boolean checkin) {
5376 if (steps == null) {
5377 return false;
5378 }
5379 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005380 if (count <= 0) {
5381 return false;
5382 }
5383 if (!checkin) {
5384 pw.println(header);
5385 }
Kweku Adams030980a2015-04-01 16:07:48 -07005386 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005387 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005388 long duration = steps.getDurationAt(i);
5389 int level = steps.getLevelAt(i);
5390 long initMode = steps.getInitModeAt(i);
5391 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005392 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005393 lineArgs[0] = Long.toString(duration);
5394 lineArgs[1] = Integer.toString(level);
5395 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5396 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5397 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5398 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5399 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5400 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005401 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005402 }
5403 } else {
5404 lineArgs[2] = "";
5405 }
5406 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5407 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5408 } else {
5409 lineArgs[3] = "";
5410 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005411 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005412 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005413 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005414 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005415 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005416 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5417 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005418 pw.print(prefix);
5419 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005420 TimeUtils.formatDuration(duration, pw);
5421 pw.print(" to "); pw.print(level);
5422 boolean haveModes = false;
5423 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5424 pw.print(" (");
5425 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5426 case Display.STATE_OFF: pw.print("screen-off"); break;
5427 case Display.STATE_ON: pw.print("screen-on"); break;
5428 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5429 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005430 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005431 }
5432 haveModes = true;
5433 }
5434 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5435 pw.print(haveModes ? ", " : " (");
5436 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5437 ? "power-save-on" : "power-save-off");
5438 haveModes = true;
5439 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005440 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5441 pw.print(haveModes ? ", " : " (");
5442 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5443 ? "device-idle-on" : "device-idle-off");
5444 haveModes = true;
5445 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005446 if (haveModes) {
5447 pw.print(")");
5448 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005449 pw.println();
5450 }
5451 }
5452 return true;
5453 }
5454
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005455 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005456 public static final int DUMP_DAILY_ONLY = 1<<2;
5457 public static final int DUMP_HISTORY_ONLY = 1<<3;
5458 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5459 public static final int DUMP_VERBOSE = 1<<5;
5460 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005461
Dianne Hackborn37de0982014-05-09 09:32:18 -07005462 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5463 final HistoryPrinter hprinter = new HistoryPrinter();
5464 final HistoryItem rec = new HistoryItem();
5465 long lastTime = -1;
5466 long baseTime = -1;
5467 boolean printed = false;
5468 HistoryEventTracker tracker = null;
5469 while (getNextHistoryLocked(rec)) {
5470 lastTime = rec.time;
5471 if (baseTime < 0) {
5472 baseTime = lastTime;
5473 }
5474 if (rec.time >= histStart) {
5475 if (histStart >= 0 && !printed) {
5476 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005477 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005478 || rec.cmd == HistoryItem.CMD_START
5479 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005480 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005481 hprinter.printNextItem(pw, rec, baseTime, checkin,
5482 (flags&DUMP_VERBOSE) != 0);
5483 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005484 } else if (rec.currentTime != 0) {
5485 printed = true;
5486 byte cmd = rec.cmd;
5487 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005488 hprinter.printNextItem(pw, rec, baseTime, checkin,
5489 (flags&DUMP_VERBOSE) != 0);
5490 rec.cmd = cmd;
5491 }
5492 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005493 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5494 hprinter.printNextItem(pw, rec, baseTime, checkin,
5495 (flags&DUMP_VERBOSE) != 0);
5496 rec.cmd = HistoryItem.CMD_UPDATE;
5497 }
5498 int oldEventCode = rec.eventCode;
5499 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005500 rec.eventTag = new HistoryTag();
5501 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5502 HashMap<String, SparseIntArray> active
5503 = tracker.getStateForEvent(i);
5504 if (active == null) {
5505 continue;
5506 }
5507 for (HashMap.Entry<String, SparseIntArray> ent
5508 : active.entrySet()) {
5509 SparseIntArray uids = ent.getValue();
5510 for (int j=0; j<uids.size(); j++) {
5511 rec.eventCode = i;
5512 rec.eventTag.string = ent.getKey();
5513 rec.eventTag.uid = uids.keyAt(j);
5514 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005515 hprinter.printNextItem(pw, rec, baseTime, checkin,
5516 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005517 rec.wakeReasonTag = null;
5518 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005519 }
5520 }
5521 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005522 rec.eventCode = oldEventCode;
5523 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005524 tracker = null;
5525 }
5526 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005527 hprinter.printNextItem(pw, rec, baseTime, checkin,
5528 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005529 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5530 // This is an attempt to aggregate the previous state and generate
5531 // fake events to reflect that state at the point where we start
5532 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005533 if (tracker == null) {
5534 tracker = new HistoryEventTracker();
5535 }
5536 tracker.updateState(rec.eventCode, rec.eventTag.string,
5537 rec.eventTag.uid, rec.eventTag.poolIdx);
5538 }
5539 }
5540 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005541 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005542 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5543 }
5544 }
5545
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005546 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5547 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5548 if (steps == null) {
5549 return;
5550 }
5551 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5552 if (timeRemaining >= 0) {
5553 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5554 tmpSb.setLength(0);
5555 formatTimeMs(tmpSb, timeRemaining);
5556 pw.print(tmpSb);
5557 pw.print(" (from "); pw.print(tmpOutInt[0]);
5558 pw.println(" steps)");
5559 }
5560 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5561 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5562 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5563 if (estimatedTime > 0) {
5564 pw.print(prefix); pw.print(label); pw.print(" ");
5565 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5566 pw.print(" time: ");
5567 tmpSb.setLength(0);
5568 formatTimeMs(tmpSb, estimatedTime);
5569 pw.print(tmpSb);
5570 pw.print(" (from "); pw.print(tmpOutInt[0]);
5571 pw.println(" steps)");
5572 }
5573 }
5574 }
5575
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005576 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5577 ArrayList<PackageChange> changes) {
5578 if (changes == null) {
5579 return;
5580 }
5581 pw.print(prefix); pw.println("Package changes:");
5582 for (int i=0; i<changes.size(); i++) {
5583 PackageChange pc = changes.get(i);
5584 if (pc.mUpdate) {
5585 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5586 pw.print(" vers="); pw.println(pc.mVersionCode);
5587 } else {
5588 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5589 }
5590 }
5591 }
5592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005593 /**
5594 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5595 *
5596 * @param pw a Printer to receive the dump output.
5597 */
5598 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005599 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005600 prepareForDumpLocked();
5601
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005602 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005603 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005604
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005605 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005606 final long historyTotalSize = getHistoryTotalSize();
5607 final long historyUsedSize = getHistoryUsedSize();
5608 if (startIteratingHistoryLocked()) {
5609 try {
5610 pw.print("Battery History (");
5611 pw.print((100*historyUsedSize)/historyTotalSize);
5612 pw.print("% used, ");
5613 printSizeValue(pw, historyUsedSize);
5614 pw.print(" used of ");
5615 printSizeValue(pw, historyTotalSize);
5616 pw.print(", ");
5617 pw.print(getHistoryStringPoolSize());
5618 pw.print(" strings using ");
5619 printSizeValue(pw, getHistoryStringPoolBytes());
5620 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005621 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005622 pw.println();
5623 } finally {
5624 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005625 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005626 }
5627
5628 if (startIteratingOldHistoryLocked()) {
5629 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005630 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005631 pw.println("Old battery History:");
5632 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005633 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005634 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005635 if (baseTime < 0) {
5636 baseTime = rec.time;
5637 }
5638 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005639 }
5640 pw.println();
5641 } finally {
5642 finishIteratingOldHistoryLocked();
5643 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005644 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005645 }
5646
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005647 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005648 return;
5649 }
5650
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005651 if (!filtering) {
5652 SparseArray<? extends Uid> uidStats = getUidStats();
5653 final int NU = uidStats.size();
5654 boolean didPid = false;
5655 long nowRealtime = SystemClock.elapsedRealtime();
5656 for (int i=0; i<NU; i++) {
5657 Uid uid = uidStats.valueAt(i);
5658 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5659 if (pids != null) {
5660 for (int j=0; j<pids.size(); j++) {
5661 Uid.Pid pid = pids.valueAt(j);
5662 if (!didPid) {
5663 pw.println("Per-PID Stats:");
5664 didPid = true;
5665 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005666 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5667 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005668 pw.print(" PID "); pw.print(pids.keyAt(j));
5669 pw.print(" wake time: ");
5670 TimeUtils.formatDuration(time, pw);
5671 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005672 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005673 }
5674 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005675 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005676 pw.println();
5677 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005678 }
5679
5680 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005681 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5682 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005683 long timeRemaining = computeBatteryTimeRemaining(
5684 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005685 if (timeRemaining >= 0) {
5686 pw.print(" Estimated discharge time remaining: ");
5687 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5688 pw.println();
5689 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005690 final LevelStepTracker steps = getDischargeLevelStepTracker();
5691 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5692 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5693 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5694 STEP_LEVEL_MODE_VALUES[i], null));
5695 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005696 pw.println();
5697 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005698 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5699 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005700 long timeRemaining = computeChargeTimeRemaining(
5701 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005702 if (timeRemaining >= 0) {
5703 pw.print(" Estimated charge time remaining: ");
5704 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5705 pw.println();
5706 }
5707 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005708 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005709 }
5710 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5711 pw.println("Daily stats:");
5712 pw.print(" Current start time: ");
5713 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5714 getCurrentDailyStartTime()).toString());
5715 pw.print(" Next min deadline: ");
5716 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5717 getNextMinDailyDeadline()).toString());
5718 pw.print(" Next max deadline: ");
5719 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5720 getNextMaxDailyDeadline()).toString());
5721 StringBuilder sb = new StringBuilder(64);
5722 int[] outInt = new int[1];
5723 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5724 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005725 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5726 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005727 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005728 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5729 dsteps, false)) {
5730 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5731 sb, outInt);
5732 }
5733 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5734 csteps, false)) {
5735 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5736 sb, outInt);
5737 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005738 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005739 } else {
5740 pw.println(" Current daily steps:");
5741 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5742 sb, outInt);
5743 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5744 sb, outInt);
5745 }
5746 }
5747 DailyItem dit;
5748 int curIndex = 0;
5749 while ((dit=getDailyItemLocked(curIndex)) != null) {
5750 curIndex++;
5751 if ((flags&DUMP_DAILY_ONLY) != 0) {
5752 pw.println();
5753 }
5754 pw.print(" Daily from ");
5755 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5756 pw.print(" to ");
5757 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5758 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005759 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005760 if (dumpDurationSteps(pw, " ",
5761 " Discharge step durations:", dit.mDischargeSteps, false)) {
5762 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5763 sb, outInt);
5764 }
5765 if (dumpDurationSteps(pw, " ",
5766 " Charge step durations:", dit.mChargeSteps, false)) {
5767 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5768 sb, outInt);
5769 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005770 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005771 } else {
5772 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5773 sb, outInt);
5774 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5775 sb, outInt);
5776 }
5777 }
5778 pw.println();
5779 }
5780 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005781 pw.println("Statistics since last charge:");
5782 pw.println(" System starts: " + getStartCount()
5783 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005784 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5785 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005786 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 }
5789
5790 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005791 public void dumpCheckinLocked(Context context, PrintWriter pw,
5792 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005793 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005794
5795 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005796 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5797 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005798
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005799 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5800
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005801 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005802 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005803
5804 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005805 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005806 try {
5807 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5808 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5809 pw.print(HISTORY_STRING_POOL); pw.print(',');
5810 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005811 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005812 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005813 pw.print(",\"");
5814 String str = getHistoryTagPoolString(i);
5815 str = str.replace("\\", "\\\\");
5816 str = str.replace("\"", "\\\"");
5817 pw.print(str);
5818 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005819 pw.println();
5820 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005821 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005822 } finally {
5823 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005824 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005825 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005826 }
5827
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005828 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005829 return;
5830 }
5831
Dianne Hackborne4a59512010-12-07 11:08:07 -08005832 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005833 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005834 for (int i=0; i<apps.size(); i++) {
5835 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005836 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
5837 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005838 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005839 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
5840 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005841 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005842 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005843 }
5844 SparseArray<? extends Uid> uidStats = getUidStats();
5845 final int NU = uidStats.size();
5846 String[] lineArgs = new String[2];
5847 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005848 int uid = UserHandle.getAppId(uidStats.keyAt(i));
5849 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
5850 if (pkgs != null && !pkgs.second.value) {
5851 pkgs.second.value = true;
5852 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005853 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005854 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005855 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5856 (Object[])lineArgs);
5857 }
5858 }
5859 }
5860 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005861 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005862 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005863 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07005864 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005865 if (timeRemaining >= 0) {
5866 lineArgs[0] = Long.toString(timeRemaining);
5867 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5868 (Object[])lineArgs);
5869 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005870 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07005871 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005872 if (timeRemaining >= 0) {
5873 lineArgs[0] = Long.toString(timeRemaining);
5874 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5875 (Object[])lineArgs);
5876 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005877 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5878 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005881}