blob: 21b68eac38b77301411caea3eb67350d7bf2dea7 [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();
Bookatz956f36bf2017-04-28 09:48:17 -0700516 public abstract Counter getBluetoothScanResultCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700517
Dianne Hackborna0200e32016-03-30 18:01:41 -0700518 // Note: the following times are disjoint. They can be added together to find the
519 // total time a uid has had any processes running at all.
520
521 /**
522 * Time this uid has any processes in the top state (or above such as persistent).
523 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800524 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700525 /**
526 * Time this uid has any process with a started out bound foreground service, but
527 * none in the "top" state.
528 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800529 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700530 /**
531 * Time this uid has any process that is top while the device is sleeping, but none
532 * in the "foreground service" or better state.
533 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800534 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700535 /**
536 * Time this uid has any process in an active foreground state, but none in the
537 * "top sleeping" or better state.
538 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800539 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700540 /**
541 * Time this uid has any process in an active background state, but none in the
542 * "foreground" or better state.
543 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800544 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700545 /**
546 * Time this uid has any processes that are sitting around cached, not in one of the
547 * other active states.
548 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800549 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700550 /**
551 * Total number of process states we track.
552 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800553 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700554
555 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800556 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700557 };
558
559 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800560 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700561
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800562 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563
Robert Greenwalta029ea12013-09-25 16:38:12 -0700564 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
565
Dianne Hackborn617f8772009-03-31 15:04:46 -0700566 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700567 * Note that these must match the constants in android.os.PowerManager.
568 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
569 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700570 */
571 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700572 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700573 };
574
Phil Weaverda80d672016-03-15 16:25:46 -0700575 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700576
Dianne Hackborn617f8772009-03-31 15:04:46 -0700577 public abstract void noteUserActivityLocked(int type);
578 public abstract boolean hasUserActivity();
579 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700580
581 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800582 public abstract long getNetworkActivityBytes(int type, int which);
583 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800584 public abstract long getMobileRadioActiveTime(int which);
585 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700586
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700587 /**
588 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
589 */
590 public abstract long getUserCpuTimeUs(int which);
591
592 /**
593 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
594 */
595 public abstract long getSystemCpuTimeUs(int which);
596
597 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700598 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
599 * given CPU cluster.
600 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700601 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700602 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700603 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
604 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700605 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700606 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700607
Adam Lesinski5f056f62016-07-14 16:56:08 -0700608 /**
609 * Returns the number of times this UID woke up the Application Processor to
610 * process a mobile radio packet.
611 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
612 */
613 public abstract long getMobileRadioApWakeupCount(int which);
614
615 /**
616 * Returns the number of times this UID woke up the Application Processor to
617 * process a WiFi packet.
618 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
619 */
620 public abstract long getWifiRadioApWakeupCount(int which);
621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800623 /*
624 * FIXME: it's not correct to use this magic value because it
625 * could clash with a sensor handle (which are defined by
626 * the sensor HAL, and therefore out of our control
627 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 // Magic sensor number for the GPS.
629 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800634
Bookatz867c0d72017-03-07 18:23:42 -0800635 /** Returns a Timer for sensor usage when app is in the background. */
636 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
638
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700639 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800640 public int mWakeNesting;
641 public long mWakeSumMs;
642 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700643 }
644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 /**
646 * The statistics associated with a particular process.
647 */
648 public static abstract class Proc {
649
Dianne Hackborn287952c2010-09-22 22:34:31 -0700650 public static class ExcessivePower {
651 public static final int TYPE_WAKE = 1;
652 public static final int TYPE_CPU = 2;
653
654 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700655 public long overTime;
656 public long usedTime;
657 }
658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800660 * Returns true if this process is still active in the battery stats.
661 */
662 public abstract boolean isActive();
663
664 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700665 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700667 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 */
669 public abstract long getUserTime(int which);
670
671 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700672 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700674 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 */
676 public abstract long getSystemTime(int which);
677
678 /**
679 * Returns the number of times the process has been started.
680 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700681 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 */
683 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700684
685 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800686 * Returns the number of times the process has crashed.
687 *
688 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
689 */
690 public abstract int getNumCrashes(int which);
691
692 /**
693 * Returns the number of times the process has ANRed.
694 *
695 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
696 */
697 public abstract int getNumAnrs(int which);
698
699 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700700 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700701 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700702 * @return foreground cpu time in microseconds
703 */
704 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700705
Dianne Hackborn287952c2010-09-22 22:34:31 -0700706 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700707
Dianne Hackborn287952c2010-09-22 22:34:31 -0700708 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710
711 /**
712 * The statistics associated with a particular package.
713 */
714 public static abstract class Pkg {
715
716 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700717 * Returns information about all wakeup alarms that have been triggered for this
718 * package. The mapping keys are tag names for the alarms, the counter contains
719 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700721 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722
723 /**
724 * Returns a mapping containing service statistics.
725 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700726 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727
728 /**
729 * The statistics associated with a particular service.
730 */
Joe Onoratoabded112016-02-08 16:49:39 -0800731 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732
733 /**
734 * Returns the amount of time spent started.
735 *
736 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700737 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 * @return
739 */
740 public abstract long getStartTime(long batteryUptime, int which);
741
742 /**
743 * Returns the total number of times startService() has been called.
744 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700745 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 */
747 public abstract int getStarts(int which);
748
749 /**
750 * Returns the total number times the service has been launched.
751 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700752 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 */
754 public abstract int getLaunches(int which);
755 }
756 }
757 }
758
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800759 public static final class LevelStepTracker {
760 public long mLastStepTime = -1;
761 public int mNumStepDurations;
762 public final long[] mStepDurations;
763
764 public LevelStepTracker(int maxLevelSteps) {
765 mStepDurations = new long[maxLevelSteps];
766 }
767
768 public LevelStepTracker(int numSteps, long[] steps) {
769 mNumStepDurations = numSteps;
770 mStepDurations = new long[numSteps];
771 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
772 }
773
774 public long getDurationAt(int index) {
775 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
776 }
777
778 public int getLevelAt(int index) {
779 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
780 >> STEP_LEVEL_LEVEL_SHIFT);
781 }
782
783 public int getInitModeAt(int index) {
784 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
785 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
786 }
787
788 public int getModModeAt(int index) {
789 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
790 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
791 }
792
793 private void appendHex(long val, int topOffset, StringBuilder out) {
794 boolean hasData = false;
795 while (topOffset >= 0) {
796 int digit = (int)( (val>>topOffset) & 0xf );
797 topOffset -= 4;
798 if (!hasData && digit == 0) {
799 continue;
800 }
801 hasData = true;
802 if (digit >= 0 && digit <= 9) {
803 out.append((char)('0' + digit));
804 } else {
805 out.append((char)('a' + digit - 10));
806 }
807 }
808 }
809
810 public void encodeEntryAt(int index, StringBuilder out) {
811 long item = mStepDurations[index];
812 long duration = item & STEP_LEVEL_TIME_MASK;
813 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
814 >> STEP_LEVEL_LEVEL_SHIFT);
815 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
816 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
817 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
818 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
819 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
820 case Display.STATE_OFF: out.append('f'); break;
821 case Display.STATE_ON: out.append('o'); break;
822 case Display.STATE_DOZE: out.append('d'); break;
823 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
824 }
825 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
826 out.append('p');
827 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700828 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
829 out.append('i');
830 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800831 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
832 case Display.STATE_OFF: out.append('F'); break;
833 case Display.STATE_ON: out.append('O'); break;
834 case Display.STATE_DOZE: out.append('D'); break;
835 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
836 }
837 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
838 out.append('P');
839 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700840 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
841 out.append('I');
842 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800843 out.append('-');
844 appendHex(level, 4, out);
845 out.append('-');
846 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
847 }
848
849 public void decodeEntryAt(int index, String value) {
850 final int N = value.length();
851 int i = 0;
852 char c;
853 long out = 0;
854 while (i < N && (c=value.charAt(i)) != '-') {
855 i++;
856 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800857 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800858 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800859 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800860 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800861 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800862 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800863 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
864 << STEP_LEVEL_INITIAL_MODE_SHIFT);
865 break;
866 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
867 << STEP_LEVEL_INITIAL_MODE_SHIFT);
868 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700869 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
870 << STEP_LEVEL_INITIAL_MODE_SHIFT);
871 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800872 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
873 break;
874 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
875 break;
876 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
877 break;
878 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
879 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
880 break;
881 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
882 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800883 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700884 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
885 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
886 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800887 }
888 }
889 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800890 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800891 while (i < N && (c=value.charAt(i)) != '-') {
892 i++;
893 level <<= 4;
894 if (c >= '0' && c <= '9') {
895 level += c - '0';
896 } else if (c >= 'a' && c <= 'f') {
897 level += c - 'a' + 10;
898 } else if (c >= 'A' && c <= 'F') {
899 level += c - 'A' + 10;
900 }
901 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800902 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800903 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
904 long duration = 0;
905 while (i < N && (c=value.charAt(i)) != '-') {
906 i++;
907 duration <<= 4;
908 if (c >= '0' && c <= '9') {
909 duration += c - '0';
910 } else if (c >= 'a' && c <= 'f') {
911 duration += c - 'a' + 10;
912 } else if (c >= 'A' && c <= 'F') {
913 duration += c - 'A' + 10;
914 }
915 }
916 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
917 }
918
919 public void init() {
920 mLastStepTime = -1;
921 mNumStepDurations = 0;
922 }
923
924 public void clearTime() {
925 mLastStepTime = -1;
926 }
927
928 public long computeTimePerLevel() {
929 final long[] steps = mStepDurations;
930 final int numSteps = mNumStepDurations;
931
932 // For now we'll do a simple average across all steps.
933 if (numSteps <= 0) {
934 return -1;
935 }
936 long total = 0;
937 for (int i=0; i<numSteps; i++) {
938 total += steps[i] & STEP_LEVEL_TIME_MASK;
939 }
940 return total / numSteps;
941 /*
942 long[] buckets = new long[numSteps];
943 int numBuckets = 0;
944 int numToAverage = 4;
945 int i = 0;
946 while (i < numSteps) {
947 long totalTime = 0;
948 int num = 0;
949 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
950 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
951 num++;
952 }
953 buckets[numBuckets] = totalTime / num;
954 numBuckets++;
955 numToAverage *= 2;
956 i += num;
957 }
958 if (numBuckets < 1) {
959 return -1;
960 }
961 long averageTime = buckets[numBuckets-1];
962 for (i=numBuckets-2; i>=0; i--) {
963 averageTime = (averageTime + buckets[i]) / 2;
964 }
965 return averageTime;
966 */
967 }
968
969 public long computeTimeEstimate(long modesOfInterest, long modeValues,
970 int[] outNumOfInterest) {
971 final long[] steps = mStepDurations;
972 final int count = mNumStepDurations;
973 if (count <= 0) {
974 return -1;
975 }
976 long total = 0;
977 int numOfInterest = 0;
978 for (int i=0; i<count; i++) {
979 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
980 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
981 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
982 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
983 // If the modes of interest didn't change during this step period...
984 if ((modMode&modesOfInterest) == 0) {
985 // And the mode values during this period match those we are measuring...
986 if ((initMode&modesOfInterest) == modeValues) {
987 // Then this can be used to estimate the total time!
988 numOfInterest++;
989 total += steps[i] & STEP_LEVEL_TIME_MASK;
990 }
991 }
992 }
993 if (numOfInterest <= 0) {
994 return -1;
995 }
996
997 if (outNumOfInterest != null) {
998 outNumOfInterest[0] = numOfInterest;
999 }
1000
1001 // The estimated time is the average time we spend in each level, multipled
1002 // by 100 -- the total number of battery levels
1003 return (total / numOfInterest) * 100;
1004 }
1005
1006 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1007 int stepCount = mNumStepDurations;
1008 final long lastStepTime = mLastStepTime;
1009 if (lastStepTime >= 0 && numStepLevels > 0) {
1010 final long[] steps = mStepDurations;
1011 long duration = elapsedRealtime - lastStepTime;
1012 for (int i=0; i<numStepLevels; i++) {
1013 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1014 long thisDuration = duration / (numStepLevels-i);
1015 duration -= thisDuration;
1016 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1017 thisDuration = STEP_LEVEL_TIME_MASK;
1018 }
1019 steps[0] = thisDuration | modeBits;
1020 }
1021 stepCount += numStepLevels;
1022 if (stepCount > steps.length) {
1023 stepCount = steps.length;
1024 }
1025 }
1026 mNumStepDurations = stepCount;
1027 mLastStepTime = elapsedRealtime;
1028 }
1029
1030 public void readFromParcel(Parcel in) {
1031 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001032 if (N > mStepDurations.length) {
1033 throw new ParcelFormatException("more step durations than available: " + N);
1034 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001035 mNumStepDurations = N;
1036 for (int i=0; i<N; i++) {
1037 mStepDurations[i] = in.readLong();
1038 }
1039 }
1040
1041 public void writeToParcel(Parcel out) {
1042 final int N = mNumStepDurations;
1043 out.writeInt(N);
1044 for (int i=0; i<N; i++) {
1045 out.writeLong(mStepDurations[i]);
1046 }
1047 }
1048 }
1049
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001050 public static final class PackageChange {
1051 public String mPackageName;
1052 public boolean mUpdate;
1053 public int mVersionCode;
1054 }
1055
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001056 public static final class DailyItem {
1057 public long mStartTime;
1058 public long mEndTime;
1059 public LevelStepTracker mDischargeSteps;
1060 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001061 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001062 }
1063
1064 public abstract DailyItem getDailyItemLocked(int daysAgo);
1065
1066 public abstract long getCurrentDailyStartTime();
1067
1068 public abstract long getNextMinDailyDeadline();
1069
1070 public abstract long getNextMaxDailyDeadline();
1071
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001072 public final static class HistoryTag {
1073 public String string;
1074 public int uid;
1075
1076 public int poolIdx;
1077
1078 public void setTo(HistoryTag o) {
1079 string = o.string;
1080 uid = o.uid;
1081 poolIdx = o.poolIdx;
1082 }
1083
1084 public void setTo(String _string, int _uid) {
1085 string = _string;
1086 uid = _uid;
1087 poolIdx = -1;
1088 }
1089
1090 public void writeToParcel(Parcel dest, int flags) {
1091 dest.writeString(string);
1092 dest.writeInt(uid);
1093 }
1094
1095 public void readFromParcel(Parcel src) {
1096 string = src.readString();
1097 uid = src.readInt();
1098 poolIdx = -1;
1099 }
1100
1101 @Override
1102 public boolean equals(Object o) {
1103 if (this == o) return true;
1104 if (o == null || getClass() != o.getClass()) return false;
1105
1106 HistoryTag that = (HistoryTag) o;
1107
1108 if (uid != that.uid) return false;
1109 if (!string.equals(that.string)) return false;
1110
1111 return true;
1112 }
1113
1114 @Override
1115 public int hashCode() {
1116 int result = string.hashCode();
1117 result = 31 * result + uid;
1118 return result;
1119 }
1120 }
1121
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001122 /**
1123 * Optional detailed information that can go into a history step. This is typically
1124 * generated each time the battery level changes.
1125 */
1126 public final static class HistoryStepDetails {
1127 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1128 public int userTime;
1129 public int systemTime;
1130
1131 // Top three apps using CPU in the last step, with times in 1/100 second.
1132 public int appCpuUid1;
1133 public int appCpuUTime1;
1134 public int appCpuSTime1;
1135 public int appCpuUid2;
1136 public int appCpuUTime2;
1137 public int appCpuSTime2;
1138 public int appCpuUid3;
1139 public int appCpuUTime3;
1140 public int appCpuSTime3;
1141
1142 // Information from /proc/stat
1143 public int statUserTime;
1144 public int statSystemTime;
1145 public int statIOWaitTime;
1146 public int statIrqTime;
1147 public int statSoftIrqTime;
1148 public int statIdlTime;
1149
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001150 // Platform-level low power state stats
1151 public String statPlatformIdleState;
1152
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001153 public HistoryStepDetails() {
1154 clear();
1155 }
1156
1157 public void clear() {
1158 userTime = systemTime = 0;
1159 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1160 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1161 = appCpuUTime3 = appCpuSTime3 = 0;
1162 }
1163
1164 public void writeToParcel(Parcel out) {
1165 out.writeInt(userTime);
1166 out.writeInt(systemTime);
1167 out.writeInt(appCpuUid1);
1168 out.writeInt(appCpuUTime1);
1169 out.writeInt(appCpuSTime1);
1170 out.writeInt(appCpuUid2);
1171 out.writeInt(appCpuUTime2);
1172 out.writeInt(appCpuSTime2);
1173 out.writeInt(appCpuUid3);
1174 out.writeInt(appCpuUTime3);
1175 out.writeInt(appCpuSTime3);
1176 out.writeInt(statUserTime);
1177 out.writeInt(statSystemTime);
1178 out.writeInt(statIOWaitTime);
1179 out.writeInt(statIrqTime);
1180 out.writeInt(statSoftIrqTime);
1181 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001182 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001183 }
1184
1185 public void readFromParcel(Parcel in) {
1186 userTime = in.readInt();
1187 systemTime = in.readInt();
1188 appCpuUid1 = in.readInt();
1189 appCpuUTime1 = in.readInt();
1190 appCpuSTime1 = in.readInt();
1191 appCpuUid2 = in.readInt();
1192 appCpuUTime2 = in.readInt();
1193 appCpuSTime2 = in.readInt();
1194 appCpuUid3 = in.readInt();
1195 appCpuUTime3 = in.readInt();
1196 appCpuSTime3 = in.readInt();
1197 statUserTime = in.readInt();
1198 statSystemTime = in.readInt();
1199 statIOWaitTime = in.readInt();
1200 statIrqTime = in.readInt();
1201 statSoftIrqTime = in.readInt();
1202 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001203 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001204 }
1205 }
1206
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001207 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001208 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001209
1210 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001211 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001212
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001213 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001214 public static final byte CMD_NULL = -1;
1215 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001216 public static final byte CMD_CURRENT_TIME = 5;
1217 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001218 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001219 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001220
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001221 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001222
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001223 /**
1224 * Return whether the command code is a delta data update.
1225 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001226 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001227 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001228 }
1229
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001230 public byte batteryLevel;
1231 public byte batteryStatus;
1232 public byte batteryHealth;
1233 public byte batteryPlugType;
1234
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001235 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001236 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001237
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001238 // The charge of the battery in micro-Ampere-hours.
1239 public int batteryChargeUAh;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001240
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001241 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001242 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001243 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001244 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001245 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1246 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001247 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001248 public static final int STATE_PHONE_STATE_SHIFT = 6;
1249 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001250 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001251 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001252 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001253
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001254 // These states always appear directly in the first int token
1255 // of a delta change; they should be ones that change relatively
1256 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001257 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1258 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001259 public static final int STATE_GPS_ON_FLAG = 1<<29;
1260 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001261 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001262 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001263 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001264 // Do not use, this is used for coulomb delta count.
1265 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001266 // These are on the lower bits used for the command; if they change
1267 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001268 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001269 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001270 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001271 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1272 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1273 // empty slot
1274 // empty slot
1275 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001276
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001277 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001278 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1279
1280 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001281
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001282 public int states;
1283
Dianne Hackborn3251b902014-06-20 14:40:53 -07001284 // Constants from WIFI_SUPPL_STATE_*
1285 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1286 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1287 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1288 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1289 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1290 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1291
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001292 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001293 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1294 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1295 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001296 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001297 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1298 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1299 public static final int STATE2_CHARGING_FLAG = 1<<24;
1300 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1301 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1302 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001303 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001304
1305 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001306 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001307 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1308
1309 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001310
Dianne Hackborn40c87252014-03-19 16:55:40 -07001311 public int states2;
1312
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001313 // The wake lock that was acquired at this point.
1314 public HistoryTag wakelockTag;
1315
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001316 // Kernel wakeup reason at this point.
1317 public HistoryTag wakeReasonTag;
1318
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001319 // Non-null when there is more detailed information at this step.
1320 public HistoryStepDetails stepDetails;
1321
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001322 public static final int EVENT_FLAG_START = 0x8000;
1323 public static final int EVENT_FLAG_FINISH = 0x4000;
1324
1325 // No event in this item.
1326 public static final int EVENT_NONE = 0x0000;
1327 // Event is about a process that is running.
1328 public static final int EVENT_PROC = 0x0001;
1329 // Event is about an application package that is in the foreground.
1330 public static final int EVENT_FOREGROUND = 0x0002;
1331 // Event is about an application package that is at the top of the screen.
1332 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001333 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001334 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001335 // Events for all additional wake locks aquired/release within a wake block.
1336 // These are not generated by default.
1337 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001338 // Event is about an application executing a scheduled job.
1339 public static final int EVENT_JOB = 0x0006;
1340 // Events for users running.
1341 public static final int EVENT_USER_RUNNING = 0x0007;
1342 // Events for foreground user.
1343 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001344 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001345 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001346 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001347 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001348 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001349 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001350 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001351 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001352 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001353 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001354 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001355 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001356 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001357 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001358 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001359 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001360 // Event for a package being on the temporary whitelist.
1361 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001362 // Event for the screen waking up.
1363 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001364 // Event for the UID that woke up the application processor.
1365 // Used for wakeups coming from WiFi, modem, etc.
1366 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001367 // Event for reporting that a specific partial wake lock has been held for a long duration.
1368 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001369
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001370 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001371 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001372 // Mask to extract out only the type part of the event.
1373 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001374
1375 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1376 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1377 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1378 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1379 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1380 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001381 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1382 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001383 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1384 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001385 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1386 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1387 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1388 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1389 public static final int EVENT_USER_FOREGROUND_START =
1390 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1391 public static final int EVENT_USER_FOREGROUND_FINISH =
1392 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001393 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1394 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001395 public static final int EVENT_TEMP_WHITELIST_START =
1396 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1397 public static final int EVENT_TEMP_WHITELIST_FINISH =
1398 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001399 public static final int EVENT_LONG_WAKE_LOCK_START =
1400 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1401 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1402 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001403
1404 // For CMD_EVENT.
1405 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001406 public HistoryTag eventTag;
1407
Dianne Hackborn9a755432014-05-15 17:05:22 -07001408 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001409 public long currentTime;
1410
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001411 // Meta-data when reading.
1412 public int numReadInts;
1413
1414 // Pre-allocated objects.
1415 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001416 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001417 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001418
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001419 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001420 }
1421
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001422 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001423 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001424 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001425 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001426 }
1427
1428 public int describeContents() {
1429 return 0;
1430 }
1431
1432 public void writeToParcel(Parcel dest, int flags) {
1433 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001434 int bat = (((int)cmd)&0xff)
1435 | ((((int)batteryLevel)<<8)&0xff00)
1436 | ((((int)batteryStatus)<<16)&0xf0000)
1437 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001438 | ((((int)batteryPlugType)<<24)&0xf000000)
1439 | (wakelockTag != null ? 0x10000000 : 0)
1440 | (wakeReasonTag != null ? 0x20000000 : 0)
1441 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001442 dest.writeInt(bat);
1443 bat = (((int)batteryTemperature)&0xffff)
1444 | ((((int)batteryVoltage)<<16)&0xffff0000);
1445 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001446 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001447 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001448 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001449 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001450 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001451 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001452 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001453 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001454 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001455 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001456 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001457 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001458 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001459 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001460 dest.writeLong(currentTime);
1461 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001462 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001463
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001464 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001465 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001466 int bat = src.readInt();
1467 cmd = (byte)(bat&0xff);
1468 batteryLevel = (byte)((bat>>8)&0xff);
1469 batteryStatus = (byte)((bat>>16)&0xf);
1470 batteryHealth = (byte)((bat>>20)&0xf);
1471 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001472 int bat2 = src.readInt();
1473 batteryTemperature = (short)(bat2&0xffff);
1474 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001475 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001476 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001477 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001478 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001479 wakelockTag = localWakelockTag;
1480 wakelockTag.readFromParcel(src);
1481 } else {
1482 wakelockTag = null;
1483 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001484 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001485 wakeReasonTag = localWakeReasonTag;
1486 wakeReasonTag.readFromParcel(src);
1487 } else {
1488 wakeReasonTag = null;
1489 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001490 if ((bat&0x40000000) != 0) {
1491 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001492 eventTag = localEventTag;
1493 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001494 } else {
1495 eventCode = EVENT_NONE;
1496 eventTag = null;
1497 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001498 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001499 currentTime = src.readLong();
1500 } else {
1501 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001502 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001503 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001504 }
1505
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001506 public void clear() {
1507 time = 0;
1508 cmd = CMD_NULL;
1509 batteryLevel = 0;
1510 batteryStatus = 0;
1511 batteryHealth = 0;
1512 batteryPlugType = 0;
1513 batteryTemperature = 0;
1514 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001515 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001516 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001517 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001518 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001519 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001520 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001521 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001522 }
1523
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001524 public void setTo(HistoryItem o) {
1525 time = o.time;
1526 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001527 setToCommon(o);
1528 }
1529
1530 public void setTo(long time, byte cmd, HistoryItem o) {
1531 this.time = time;
1532 this.cmd = cmd;
1533 setToCommon(o);
1534 }
1535
1536 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001537 batteryLevel = o.batteryLevel;
1538 batteryStatus = o.batteryStatus;
1539 batteryHealth = o.batteryHealth;
1540 batteryPlugType = o.batteryPlugType;
1541 batteryTemperature = o.batteryTemperature;
1542 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001543 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001544 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001545 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001546 if (o.wakelockTag != null) {
1547 wakelockTag = localWakelockTag;
1548 wakelockTag.setTo(o.wakelockTag);
1549 } else {
1550 wakelockTag = null;
1551 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001552 if (o.wakeReasonTag != null) {
1553 wakeReasonTag = localWakeReasonTag;
1554 wakeReasonTag.setTo(o.wakeReasonTag);
1555 } else {
1556 wakeReasonTag = null;
1557 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001558 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001559 if (o.eventTag != null) {
1560 eventTag = localEventTag;
1561 eventTag.setTo(o.eventTag);
1562 } else {
1563 eventTag = null;
1564 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001565 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001566 }
1567
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001568 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001569 return batteryLevel == o.batteryLevel
1570 && batteryStatus == o.batteryStatus
1571 && batteryHealth == o.batteryHealth
1572 && batteryPlugType == o.batteryPlugType
1573 && batteryTemperature == o.batteryTemperature
1574 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001575 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001576 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001577 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001578 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001579 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001580
1581 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001582 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001583 return false;
1584 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001585 if (wakelockTag != o.wakelockTag) {
1586 if (wakelockTag == null || o.wakelockTag == null) {
1587 return false;
1588 }
1589 if (!wakelockTag.equals(o.wakelockTag)) {
1590 return false;
1591 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001592 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001593 if (wakeReasonTag != o.wakeReasonTag) {
1594 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1595 return false;
1596 }
1597 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1598 return false;
1599 }
1600 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001601 if (eventTag != o.eventTag) {
1602 if (eventTag == null || o.eventTag == null) {
1603 return false;
1604 }
1605 if (!eventTag.equals(o.eventTag)) {
1606 return false;
1607 }
1608 }
1609 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001610 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001611 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001612
1613 public final static class HistoryEventTracker {
1614 private final HashMap<String, SparseIntArray>[] mActiveEvents
1615 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1616
1617 public boolean updateState(int code, String name, int uid, int poolIdx) {
1618 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1619 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1620 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1621 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001622 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001623 mActiveEvents[idx] = active;
1624 }
1625 SparseIntArray uids = active.get(name);
1626 if (uids == null) {
1627 uids = new SparseIntArray();
1628 active.put(name, uids);
1629 }
1630 if (uids.indexOfKey(uid) >= 0) {
1631 // Already set, nothing to do!
1632 return false;
1633 }
1634 uids.put(uid, poolIdx);
1635 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1636 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1637 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1638 if (active == null) {
1639 // not currently active, nothing to do.
1640 return false;
1641 }
1642 SparseIntArray uids = active.get(name);
1643 if (uids == null) {
1644 // not currently active, nothing to do.
1645 return false;
1646 }
1647 idx = uids.indexOfKey(uid);
1648 if (idx < 0) {
1649 // not currently active, nothing to do.
1650 return false;
1651 }
1652 uids.removeAt(idx);
1653 if (uids.size() <= 0) {
1654 active.remove(name);
1655 }
1656 }
1657 return true;
1658 }
1659
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001660 public void removeEvents(int code) {
1661 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1662 mActiveEvents[idx] = null;
1663 }
1664
Dianne Hackborn37de0982014-05-09 09:32:18 -07001665 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1666 return mActiveEvents[code];
1667 }
1668 }
1669
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001670 public static final class BitDescription {
1671 public final int mask;
1672 public final int shift;
1673 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001674 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001675 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001676 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001677
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001678 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001679 this.mask = mask;
1680 this.shift = -1;
1681 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001682 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001683 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001684 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001685 }
1686
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001687 public BitDescription(int mask, int shift, String name, String shortName,
1688 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001689 this.mask = mask;
1690 this.shift = shift;
1691 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001692 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001693 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001694 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001695 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001696 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001697
Dianne Hackbornfc064132014-06-02 12:42:12 -07001698 /**
1699 * Don't allow any more batching in to the current history event. This
1700 * is called when printing partial histories, so to ensure that the next
1701 * history event will go in to a new batch after what was printed in the
1702 * last partial history.
1703 */
1704 public abstract void commitCurrentHistoryBatchLocked();
1705
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001706 public abstract int getHistoryTotalSize();
1707
1708 public abstract int getHistoryUsedSize();
1709
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001710 public abstract boolean startIteratingHistoryLocked();
1711
Dianne Hackborn099bc622014-01-22 13:39:16 -08001712 public abstract int getHistoryStringPoolSize();
1713
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001714 public abstract int getHistoryStringPoolBytes();
1715
1716 public abstract String getHistoryTagPoolString(int index);
1717
1718 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001719
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001720 public abstract boolean getNextHistoryLocked(HistoryItem out);
1721
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001722 public abstract void finishIteratingHistoryLocked();
1723
1724 public abstract boolean startIteratingOldHistoryLocked();
1725
1726 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1727
1728 public abstract void finishIteratingOldHistoryLocked();
1729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001731 * Return the base time offset for the battery history.
1732 */
1733 public abstract long getHistoryBaseTime();
1734
1735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 * Returns the number of times the device has been started.
1737 */
1738 public abstract int getStartCount();
1739
1740 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001741 * 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 -08001742 * running on battery.
1743 *
1744 * {@hide}
1745 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001746 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001748 /**
1749 * Returns the number of times the screen was turned on.
1750 *
1751 * {@hide}
1752 */
1753 public abstract int getScreenOnCount(int which);
1754
Jeff Browne95c3cd2014-05-02 16:59:26 -07001755 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1756
Dianne Hackborn617f8772009-03-31 15:04:46 -07001757 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1758 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1759 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1760 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1761 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1762
1763 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1764 "dark", "dim", "medium", "light", "bright"
1765 };
1766
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001767 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1768 "0", "1", "2", "3", "4"
1769 };
1770
Dianne Hackborn617f8772009-03-31 15:04:46 -07001771 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001772
Dianne Hackborn617f8772009-03-31 15:04:46 -07001773 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001774 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001775 * the given brightness
1776 *
1777 * {@hide}
1778 */
1779 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001780 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001783 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001784 * running on battery.
1785 *
1786 * {@hide}
1787 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001788 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001789
1790 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001791 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001792 *
1793 * {@hide}
1794 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001795 public abstract int getPowerSaveModeEnabledCount(int which);
1796
1797 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001798 * Constant for device idle mode: not active.
1799 */
1800 public static final int DEVICE_IDLE_MODE_OFF = 0;
1801
1802 /**
1803 * Constant for device idle mode: active in lightweight mode.
1804 */
1805 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1806
1807 /**
1808 * Constant for device idle mode: active in full mode.
1809 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001810 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001811
1812 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001813 * Returns the time in microseconds that device has been in idle mode while
1814 * running on battery.
1815 *
1816 * {@hide}
1817 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001818 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001819
1820 /**
1821 * Returns the number of times that the devie has gone in to idle mode.
1822 *
1823 * {@hide}
1824 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001825 public abstract int getDeviceIdleModeCount(int mode, int which);
1826
1827 /**
1828 * Return the longest duration we spent in a particular device idle mode (fully in the
1829 * mode, not in idle maintenance etc).
1830 */
1831 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001832
1833 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001834 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001835 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001836 * counts all of the time that we consider the device to be idle, whether or not
1837 * it is currently in the actual device idle mode.
1838 *
1839 * {@hide}
1840 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001841 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001842
1843 /**
1844 * Returns the number of times that the devie has started idling.
1845 *
1846 * {@hide}
1847 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001848 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001849
1850 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001851 * Returns the number of times that connectivity state changed.
1852 *
1853 * {@hide}
1854 */
1855 public abstract int getNumConnectivityChange(int which);
1856
1857 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001858 * 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 -08001859 * running on battery.
1860 *
1861 * {@hide}
1862 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001863 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001864
1865 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001866 * Returns the number of times a phone call was activated.
1867 *
1868 * {@hide}
1869 */
1870 public abstract int getPhoneOnCount(int which);
1871
1872 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001873 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001874 * the given signal strength.
1875 *
1876 * {@hide}
1877 */
1878 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001879 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001880
Dianne Hackborn617f8772009-03-31 15:04:46 -07001881 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001882 * Returns the time in microseconds that the phone has been trying to
1883 * acquire a signal.
1884 *
1885 * {@hide}
1886 */
1887 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001888 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001889
1890 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001891 * Returns the number of times the phone has entered the given signal strength.
1892 *
1893 * {@hide}
1894 */
1895 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1896
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001897 /**
1898 * Returns the time in microseconds that the mobile network has been active
1899 * (in a high power state).
1900 *
1901 * {@hide}
1902 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001903 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001904
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001905 /**
1906 * Returns the number of times that the mobile network has transitioned to the
1907 * active state.
1908 *
1909 * {@hide}
1910 */
1911 public abstract int getMobileRadioActiveCount(int which);
1912
1913 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001914 * Returns the time in microseconds that is the difference between the mobile radio
1915 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1916 * from the radio.
1917 *
1918 * {@hide}
1919 */
1920 public abstract long getMobileRadioActiveAdjustedTime(int which);
1921
1922 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001923 * Returns the time in microseconds that the mobile network has been active
1924 * (in a high power state) but not being able to blame on an app.
1925 *
1926 * {@hide}
1927 */
1928 public abstract long getMobileRadioActiveUnknownTime(int which);
1929
1930 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001931 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001932 *
1933 * {@hide}
1934 */
1935 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001936
Dianne Hackborn627bba72009-03-24 22:32:56 -07001937 public static final int DATA_CONNECTION_NONE = 0;
1938 public static final int DATA_CONNECTION_GPRS = 1;
1939 public static final int DATA_CONNECTION_EDGE = 2;
1940 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001941 public static final int DATA_CONNECTION_CDMA = 4;
1942 public static final int DATA_CONNECTION_EVDO_0 = 5;
1943 public static final int DATA_CONNECTION_EVDO_A = 6;
1944 public static final int DATA_CONNECTION_1xRTT = 7;
1945 public static final int DATA_CONNECTION_HSDPA = 8;
1946 public static final int DATA_CONNECTION_HSUPA = 9;
1947 public static final int DATA_CONNECTION_HSPA = 10;
1948 public static final int DATA_CONNECTION_IDEN = 11;
1949 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001950 public static final int DATA_CONNECTION_LTE = 13;
1951 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001952 public static final int DATA_CONNECTION_HSPAP = 15;
1953 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001954
Dianne Hackborn627bba72009-03-24 22:32:56 -07001955 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001956 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001957 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001958 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001959 };
1960
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001961 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001962
1963 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001964 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001965 * the given data connection.
1966 *
1967 * {@hide}
1968 */
1969 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001970 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001973 * Returns the number of times the phone has entered the given data
1974 * connection type.
1975 *
1976 * {@hide}
1977 */
1978 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001979
Dianne Hackborn3251b902014-06-20 14:40:53 -07001980 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1981 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1982 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1983 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1984 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1985 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1986 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1987 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1988 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1989 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1990 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1991 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1992 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1993
1994 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1995
1996 static final String[] WIFI_SUPPL_STATE_NAMES = {
1997 "invalid", "disconn", "disabled", "inactive", "scanning",
1998 "authenticating", "associating", "associated", "4-way-handshake",
1999 "group-handshake", "completed", "dormant", "uninit"
2000 };
2001
2002 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2003 "inv", "dsc", "dis", "inact", "scan",
2004 "auth", "ascing", "asced", "4-way",
2005 "group", "compl", "dorm", "uninit"
2006 };
2007
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002008 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2009 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002010 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002011 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2012 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002013 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002014 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2015 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2016 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002017 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002018 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002019 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002020 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002021 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2022 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002023 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2024 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2025 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2026 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2027 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2028 new String[] {"in", "out", "emergency", "off"},
2029 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002030 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2031 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2032 SignalStrength.SIGNAL_STRENGTH_NAMES,
2033 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002034 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2035 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2036 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002037 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002038
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002039 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2040 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002041 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002042 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002043 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002044 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002045 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002046 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2047 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2048 new String[] { "off", "light", "full", "???" },
2049 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002050 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2051 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2052 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002053 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2054 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2055 new String[] { "0", "1", "2", "3", "4" },
2056 new String[] { "0", "1", "2", "3", "4" }),
2057 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2058 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2059 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002060 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002061 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002062 };
2063
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002064 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002065 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002066 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2067 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002068 };
2069
2070 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002071 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002072 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002073 "Esw", "Ewa", "Elw", "Eec"
2074 };
2075
2076 @FunctionalInterface
2077 public interface IntToString {
2078 String applyAsString(int val);
2079 }
2080
2081 private static final IntToString sUidToString = UserHandle::formatUid;
2082 private static final IntToString sIntToString = Integer::toString;
2083
2084 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2085 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2086 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2087 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2088 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002089 };
2090
Dianne Hackborn617f8772009-03-31 15:04:46 -07002091 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002092 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002093 * running on battery.
2094 *
2095 * {@hide}
2096 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002097 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002098
2099 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002100 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002101 * been in the running state while the device was running on battery.
2102 *
2103 * {@hide}
2104 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002105 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002106
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002107 public static final int WIFI_STATE_OFF = 0;
2108 public static final int WIFI_STATE_OFF_SCANNING = 1;
2109 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2110 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2111 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2112 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2113 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2114 public static final int WIFI_STATE_SOFT_AP = 7;
2115
2116 static final String[] WIFI_STATE_NAMES = {
2117 "off", "scanning", "no_net", "disconn",
2118 "sta", "p2p", "sta_p2p", "soft_ap"
2119 };
2120
2121 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2122
2123 /**
2124 * Returns the time in microseconds that WiFi has been running in the given state.
2125 *
2126 * {@hide}
2127 */
2128 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002129 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002130
2131 /**
2132 * Returns the number of times that WiFi has entered the given state.
2133 *
2134 * {@hide}
2135 */
2136 public abstract int getWifiStateCount(int wifiState, int which);
2137
The Android Open Source Project10592532009-03-18 17:39:46 -07002138 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002139 * Returns the time in microseconds that the wifi supplicant has been
2140 * in a given state.
2141 *
2142 * {@hide}
2143 */
2144 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2145
2146 /**
2147 * Returns the number of times that the wifi supplicant has transitioned
2148 * to a given state.
2149 *
2150 * {@hide}
2151 */
2152 public abstract int getWifiSupplStateCount(int state, int which);
2153
2154 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2155
2156 /**
2157 * Returns the time in microseconds that WIFI has been running with
2158 * the given signal strength.
2159 *
2160 * {@hide}
2161 */
2162 public abstract long getWifiSignalStrengthTime(int strengthBin,
2163 long elapsedRealtimeUs, int which);
2164
2165 /**
2166 * Returns the number of times WIFI has entered the given signal strength.
2167 *
2168 * {@hide}
2169 */
2170 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2171
2172 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002173 * Returns the time in microseconds that the flashlight has been on while the device was
2174 * running on battery.
2175 *
2176 * {@hide}
2177 */
2178 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2179
2180 /**
2181 * Returns the number of times that the flashlight has been turned on while the device was
2182 * running on battery.
2183 *
2184 * {@hide}
2185 */
2186 public abstract long getFlashlightOnCount(int which);
2187
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002188 /**
2189 * Returns the time in microseconds that the camera has been on while the device was
2190 * running on battery.
2191 *
2192 * {@hide}
2193 */
2194 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2195
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002196 /**
2197 * Returns the time in microseconds that bluetooth scans were running while the device was
2198 * on battery.
2199 *
2200 * {@hide}
2201 */
2202 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002203
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002204 public static final int NETWORK_MOBILE_RX_DATA = 0;
2205 public static final int NETWORK_MOBILE_TX_DATA = 1;
2206 public static final int NETWORK_WIFI_RX_DATA = 2;
2207 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002208 public static final int NETWORK_BT_RX_DATA = 4;
2209 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002210 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2211 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2212 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2213 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2214 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002215
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002216 public abstract long getNetworkActivityBytes(int type, int which);
2217 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002218
Adam Lesinskie08af192015-03-25 16:42:59 -07002219 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002220 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002221 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002222 * actual power data.
2223 */
2224 public abstract boolean hasWifiActivityReporting();
2225
2226 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002227 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2228 * in various radio controller states, such as transmit, receive, and idle.
2229 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002230 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002231 public abstract ControllerActivityCounter getWifiControllerActivity();
2232
2233 /**
2234 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2235 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2236 * actual power data.
2237 */
2238 public abstract boolean hasBluetoothActivityReporting();
2239
2240 /**
2241 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2242 * in various radio controller states, such as transmit, receive, and idle.
2243 * @return non-null {@link ControllerActivityCounter}
2244 */
2245 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2246
2247 /**
2248 * Returns true if the BatteryStats object has detailed modem power reports.
2249 * When true, calling {@link #getModemControllerActivity()} will yield the
2250 * actual power data.
2251 */
2252 public abstract boolean hasModemActivityReporting();
2253
2254 /**
2255 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2256 * in various radio controller states, such as transmit, receive, and idle.
2257 * @return non-null {@link ControllerActivityCounter}
2258 */
2259 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002260
The Android Open Source Project10592532009-03-18 17:39:46 -07002261 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002262 * Return the wall clock time when battery stats data collection started.
2263 */
2264 public abstract long getStartClockTime();
2265
2266 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002267 * Return platform version tag that we were running in when the battery stats started.
2268 */
2269 public abstract String getStartPlatformVersion();
2270
2271 /**
2272 * Return platform version tag that we were running in when the battery stats ended.
2273 */
2274 public abstract String getEndPlatformVersion();
2275
2276 /**
2277 * Return the internal version code of the parcelled format.
2278 */
2279 public abstract int getParcelVersion();
2280
2281 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 * Return whether we are currently running on battery.
2283 */
2284 public abstract boolean getIsOnBattery();
2285
2286 /**
2287 * Returns a SparseArray containing the statistics for each uid.
2288 */
2289 public abstract SparseArray<? extends Uid> getUidStats();
2290
2291 /**
2292 * Returns the current battery uptime in microseconds.
2293 *
2294 * @param curTime the amount of elapsed realtime in microseconds.
2295 */
2296 public abstract long getBatteryUptime(long curTime);
2297
2298 /**
2299 * Returns the current battery realtime in microseconds.
2300 *
2301 * @param curTime the amount of elapsed realtime in microseconds.
2302 */
2303 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002304
2305 /**
Evan Millar633a1742009-04-02 16:36:33 -07002306 * Returns the battery percentage level at the last time the device was unplugged from power, or
2307 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002308 */
Evan Millar633a1742009-04-02 16:36:33 -07002309 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002310
2311 /**
Evan Millar633a1742009-04-02 16:36:33 -07002312 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2313 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002314 */
Evan Millar633a1742009-04-02 16:36:33 -07002315 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316
2317 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002318 * Get the amount the battery has discharged since the stats were
2319 * last reset after charging, as a lower-end approximation.
2320 */
2321 public abstract int getLowDischargeAmountSinceCharge();
2322
2323 /**
2324 * Get the amount the battery has discharged since the stats were
2325 * last reset after charging, as an upper-end approximation.
2326 */
2327 public abstract int getHighDischargeAmountSinceCharge();
2328
2329 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002330 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2331 */
2332 public abstract int getDischargeAmount(int which);
2333
2334 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002335 * Get the amount the battery has discharged while the screen was on,
2336 * since the last time power was unplugged.
2337 */
2338 public abstract int getDischargeAmountScreenOn();
2339
2340 /**
2341 * Get the amount the battery has discharged while the screen was on,
2342 * since the last time the device was charged.
2343 */
2344 public abstract int getDischargeAmountScreenOnSinceCharge();
2345
2346 /**
2347 * Get the amount the battery has discharged while the screen was off,
2348 * since the last time power was unplugged.
2349 */
2350 public abstract int getDischargeAmountScreenOff();
2351
2352 /**
2353 * Get the amount the battery has discharged while the screen was off,
2354 * since the last time the device was charged.
2355 */
2356 public abstract int getDischargeAmountScreenOffSinceCharge();
2357
2358 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 * Returns the total, last, or current battery uptime in microseconds.
2360 *
2361 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002362 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 */
2364 public abstract long computeBatteryUptime(long curTime, int which);
2365
2366 /**
2367 * Returns the total, last, or current battery realtime in microseconds.
2368 *
2369 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002370 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 */
2372 public abstract long computeBatteryRealtime(long curTime, int which);
2373
2374 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002375 * Returns the total, last, or current battery screen off uptime in microseconds.
2376 *
2377 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002378 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002379 */
2380 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2381
2382 /**
2383 * Returns the total, last, or current battery screen off realtime in microseconds.
2384 *
2385 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002386 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002387 */
2388 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2389
2390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 * Returns the total, last, or current uptime in microseconds.
2392 *
2393 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002394 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 */
2396 public abstract long computeUptime(long curTime, int which);
2397
2398 /**
2399 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002402 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 */
2404 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002405
2406 /**
2407 * Compute an approximation for how much run time (in microseconds) is remaining on
2408 * the battery. Returns -1 if no time can be computed: either there is not
2409 * enough current data to make a decision, or the battery is currently
2410 * charging.
2411 *
2412 * @param curTime The current elepsed realtime in microseconds.
2413 */
2414 public abstract long computeBatteryTimeRemaining(long curTime);
2415
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002416 // The part of a step duration that is the actual time.
2417 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2418
2419 // Bits in a step duration that are the new battery level we are at.
2420 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002421 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002422
2423 // Bits in a step duration that are the initial mode we were in at that step.
2424 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002425 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002426
2427 // Bits in a step duration that indicate which modes changed during that step.
2428 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002429 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002430
2431 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2432 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2433
Santos Cordone94f0502017-02-24 12:31:20 -08002434 // The largest value for screen state that is tracked in battery states. Any values above
2435 // this should be mapped back to one of the tracked values before being tracked here.
2436 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2437
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002438 // Step duration mode: power save is on.
2439 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2440
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002441 // Step duration mode: device is currently in idle mode.
2442 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2443
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002444 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2445 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002446 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2447 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002448 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,
2452 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002453 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2454 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002455 };
2456 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2457 (Display.STATE_OFF-1),
2458 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002459 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002460 (Display.STATE_ON-1),
2461 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2462 (Display.STATE_DOZE-1),
2463 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2464 (Display.STATE_DOZE_SUSPEND-1),
2465 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002466 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002467 };
2468 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2469 "screen off",
2470 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002471 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002472 "screen on",
2473 "screen on power save",
2474 "screen doze",
2475 "screen doze power save",
2476 "screen doze-suspend",
2477 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002478 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002479 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002480
2481 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002482 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2483 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2484 * a coulomb counter.
2485 */
2486 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2487
2488 /**
2489 * Return the counter keeping track of the amount of battery discharge measured in
2490 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2491 * a coulomb counter.
2492 */
2493 public abstract LongCounter getDischargeCoulombCounter();
2494
2495 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002496 * Returns the estimated real battery capacity, which may be less than the capacity
2497 * declared by the PowerProfile.
2498 * @return The estimated battery capacity in mAh.
2499 */
2500 public abstract int getEstimatedBatteryCapacity();
2501
2502 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002503 * @return The minimum learned battery capacity in uAh.
2504 */
2505 public abstract int getMinLearnedBatteryCapacity();
2506
2507 /**
2508 * @return The maximum learned battery capacity in uAh.
2509 */
2510 public abstract int getMaxLearnedBatteryCapacity() ;
2511
2512 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002513 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002514 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002515 public abstract LevelStepTracker getDischargeLevelStepTracker();
2516
2517 /**
2518 * Return the array of daily discharge step durations.
2519 */
2520 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002521
2522 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002523 * Compute an approximation for how much time (in microseconds) remains until the battery
2524 * is fully charged. Returns -1 if no time can be computed: either there is not
2525 * enough current data to make a decision, or the battery is currently
2526 * discharging.
2527 *
2528 * @param curTime The current elepsed realtime in microseconds.
2529 */
2530 public abstract long computeChargeTimeRemaining(long curTime);
2531
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002532 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002533 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002534 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002535 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002536
2537 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002538 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002539 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002540 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002541
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002542 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2543
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002544 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002545
Evan Millarc64edde2009-04-18 12:26:32 -07002546 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547
James Carr2dd7e5e2016-07-20 18:48:39 -07002548 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2549
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002550 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2551
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002552 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 long days = seconds / (60 * 60 * 24);
2554 if (days != 0) {
2555 out.append(days);
2556 out.append("d ");
2557 }
2558 long used = days * 60 * 60 * 24;
2559
2560 long hours = (seconds - used) / (60 * 60);
2561 if (hours != 0 || used != 0) {
2562 out.append(hours);
2563 out.append("h ");
2564 }
2565 used += hours * 60 * 60;
2566
2567 long mins = (seconds-used) / 60;
2568 if (mins != 0 || used != 0) {
2569 out.append(mins);
2570 out.append("m ");
2571 }
2572 used += mins * 60;
2573
2574 if (seconds != 0 || used != 0) {
2575 out.append(seconds-used);
2576 out.append("s ");
2577 }
2578 }
2579
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002580 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002582 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 sb.append(time - (sec * 1000));
2584 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 }
2586
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002587 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002588 long sec = time / 1000;
2589 formatTimeRaw(sb, sec);
2590 sb.append(time - (sec * 1000));
2591 sb.append("ms");
2592 }
2593
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002594 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002596 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 }
2598 float perc = ((float)num) / ((float)den) * 100;
2599 mFormatBuilder.setLength(0);
2600 mFormatter.format("%.1f%%", perc);
2601 return mFormatBuilder.toString();
2602 }
2603
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002604 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002605 mFormatBuilder.setLength(0);
2606
2607 if (bytes < BYTES_PER_KB) {
2608 return bytes + "B";
2609 } else if (bytes < BYTES_PER_MB) {
2610 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2611 return mFormatBuilder.toString();
2612 } else if (bytes < BYTES_PER_GB){
2613 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2614 return mFormatBuilder.toString();
2615 } else {
2616 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2617 return mFormatBuilder.toString();
2618 }
2619 }
2620
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002621 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002622 if (timer != null) {
2623 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002624 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002625 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2626 return totalTimeMillis;
2627 }
2628 return 0;
2629 }
2630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 /**
2632 *
2633 * @param sb a StringBuilder object.
2634 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002635 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002637 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 * @param linePrefix a String to be prepended to each line of output.
2639 * @return the line prefix
2640 */
2641 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002642 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002645 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002646
Evan Millarc64edde2009-04-18 12:26:32 -07002647 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 if (totalTimeMillis != 0) {
2649 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002650 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002651 if (name != null) {
2652 sb.append(name);
2653 sb.append(' ');
2654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 sb.append('(');
2656 sb.append(count);
2657 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002658 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2659 if (maxDurationMs >= 0) {
2660 sb.append(" max=");
2661 sb.append(maxDurationMs);
2662 }
2663 if (timer.isRunningLocked()) {
2664 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2665 if (currentMs >= 0) {
2666 sb.append(" (running for ");
2667 sb.append(currentMs);
2668 sb.append("ms)");
2669 } else {
2670 sb.append(" (running)");
2671 }
2672 }
2673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 return ", ";
2675 }
2676 }
2677 return linePrefix;
2678 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002679
2680 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002681 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002682 *
2683 * @param pw a PrintWriter object to print to.
2684 * @param sb a StringBuilder object.
2685 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002686 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002687 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2688 * @param prefix a String to be prepended to each line of output.
2689 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002690 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002691 */
2692 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002693 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002694 if (timer != null) {
2695 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002696 final long totalTimeMs = (timer.getTotalTimeLocked(
2697 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002698 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002699 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002700 sb.setLength(0);
2701 sb.append(prefix);
2702 sb.append(" ");
2703 sb.append(type);
2704 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002705 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002706 sb.append("realtime (");
2707 sb.append(count);
2708 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002709 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2710 if (maxDurationMs >= 0) {
2711 sb.append(" max=");
2712 sb.append(maxDurationMs);
2713 }
2714 if (timer.isRunningLocked()) {
2715 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2716 if (currentMs >= 0) {
2717 sb.append(" (running for ");
2718 sb.append(currentMs);
2719 sb.append("ms)");
2720 } else {
2721 sb.append(" (running)");
2722 }
2723 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002724 pw.println(sb.toString());
2725 return true;
2726 }
2727 }
2728 return false;
2729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730
2731 /**
2732 * Checkin version of wakelock printer. Prints simple comma-separated list.
2733 *
2734 * @param sb a StringBuilder object.
2735 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002736 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002738 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 * @param linePrefix a String to be prepended to each line of output.
2740 * @return the line prefix
2741 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002742 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2743 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 long totalTimeMicros = 0;
2745 int count = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002746 long max = -1;
2747 long current = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002749 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002750 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002751 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2752 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 }
2754 sb.append(linePrefix);
2755 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2756 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002757 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002759 sb.append(',');
2760 sb.append(current);
2761 sb.append(',');
2762 sb.append(max);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 return ",";
2764 }
2765
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002766 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2767 String type) {
2768 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2769 pw.print(',');
2770 pw.print(uid);
2771 pw.print(',');
2772 pw.print(category);
2773 pw.print(',');
2774 pw.print(type);
2775 }
2776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 /**
2778 * Dump a comma-separated line of values for terse checkin mode.
2779 *
2780 * @param pw the PageWriter to dump log to
2781 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2782 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2783 * @param args type-dependent data arguments
2784 */
2785 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2786 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002787 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002788 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002789 pw.print(',');
2790 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002792 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002794
2795 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002796 * Dump a given timer stat for terse checkin mode.
2797 *
2798 * @param pw the PageWriter to dump log to
2799 * @param uid the UID to log
2800 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2801 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2802 * @param timer a {@link Timer} to dump stats for
2803 * @param rawRealtime the current elapsed realtime of the system in microseconds
2804 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2805 */
2806 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2807 Timer timer, long rawRealtime, int which) {
2808 if (timer != null) {
2809 // Convert from microseconds to milliseconds with rounding
2810 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2811 / 1000;
2812 final int count = timer.getCountLocked(which);
2813 if (totalTime != 0) {
2814 dumpLine(pw, uid, category, type, totalTime, count);
2815 }
2816 }
2817 }
2818
2819 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002820 * Checks if the ControllerActivityCounter has any data worth dumping.
2821 */
2822 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2823 if (counter == null) {
2824 return false;
2825 }
2826
2827 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2828 || counter.getRxTimeCounter().getCountLocked(which) != 0
2829 || counter.getPowerCounter().getCountLocked(which) != 0) {
2830 return true;
2831 }
2832
2833 for (LongCounter c : counter.getTxTimeCounters()) {
2834 if (c.getCountLocked(which) != 0) {
2835 return true;
2836 }
2837 }
2838 return false;
2839 }
2840
2841 /**
2842 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2843 * The order of the arguments in the final check in line is:
2844 *
2845 * idle, rx, power, tx...
2846 *
2847 * where tx... is one or more transmit level times.
2848 */
2849 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2850 String type,
2851 ControllerActivityCounter counter,
2852 int which) {
2853 if (!controllerActivityHasData(counter, which)) {
2854 return;
2855 }
2856
2857 dumpLineHeader(pw, uid, category, type);
2858 pw.print(",");
2859 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2860 pw.print(",");
2861 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2862 pw.print(",");
2863 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2864 for (LongCounter c : counter.getTxTimeCounters()) {
2865 pw.print(",");
2866 pw.print(c.getCountLocked(which));
2867 }
2868 pw.println();
2869 }
2870
2871 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2872 String prefix, String controllerName,
2873 ControllerActivityCounter counter,
2874 int which) {
2875 if (controllerActivityHasData(counter, which)) {
2876 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2877 }
2878 }
2879
2880 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2881 String controllerName,
2882 ControllerActivityCounter counter, int which) {
2883 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2884 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2885 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2886 long totalTxTimeMs = 0;
2887 for (LongCounter txState : counter.getTxTimeCounters()) {
2888 totalTxTimeMs += txState.getCountLocked(which);
2889 }
2890
2891 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2892
2893 sb.setLength(0);
2894 sb.append(prefix);
2895 sb.append(" ");
2896 sb.append(controllerName);
2897 sb.append(" Idle time: ");
2898 formatTimeMs(sb, idleTimeMs);
2899 sb.append("(");
2900 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2901 sb.append(")");
2902 pw.println(sb.toString());
2903
2904 sb.setLength(0);
2905 sb.append(prefix);
2906 sb.append(" ");
2907 sb.append(controllerName);
2908 sb.append(" Rx time: ");
2909 formatTimeMs(sb, rxTimeMs);
2910 sb.append("(");
2911 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2912 sb.append(")");
2913 pw.println(sb.toString());
2914
2915 sb.setLength(0);
2916 sb.append(prefix);
2917 sb.append(" ");
2918 sb.append(controllerName);
2919 sb.append(" Tx time: ");
2920 formatTimeMs(sb, totalTxTimeMs);
2921 sb.append("(");
2922 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2923 sb.append(")");
2924 pw.println(sb.toString());
2925
2926 final int numTxLvls = counter.getTxTimeCounters().length;
2927 if (numTxLvls > 1) {
2928 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2929 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2930 sb.setLength(0);
2931 sb.append(prefix);
2932 sb.append(" [");
2933 sb.append(lvl);
2934 sb.append("] ");
2935 formatTimeMs(sb, txLvlTimeMs);
2936 sb.append("(");
2937 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2938 sb.append(")");
2939 pw.println(sb.toString());
2940 }
2941 }
2942
2943 sb.setLength(0);
2944 sb.append(prefix);
2945 sb.append(" ");
2946 sb.append(controllerName);
2947 sb.append(" Power drain: ").append(
2948 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2949 sb.append("mAh");
2950 pw.println(sb.toString());
2951 }
2952
2953 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002954 * Temporary for settings.
2955 */
2956 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2957 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2958 }
2959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 /**
2961 * Checkin server version of dump to produce more compact, computer-readable log.
2962 *
2963 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002965 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2966 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2968 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2969 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2971 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002972 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2973 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2974 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 final long totalRealtime = computeRealtime(rawRealtime, which);
2976 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002977 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002978 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002979 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002980 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2981 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002982 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002983 rawRealtime, which);
2984 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2985 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002986 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002987 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002988 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002989 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002990 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
2991 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
2992 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002993
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002994 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002996 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002997 final int NU = uidStats.size();
2998
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002999 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003002 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003003 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003004 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003005 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003006 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003007 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003008 getEstimatedBatteryCapacity(),
3009 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07003010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003012 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003013 long fullWakeLockTimeTotal = 0;
3014 long partialWakeLockTimeTotal = 0;
3015
3016 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003017 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003018
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003019 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3020 = u.getWakelockStats();
3021 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3022 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003023
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003024 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3025 if (fullWakeTimer != null) {
3026 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3027 which);
3028 }
3029
3030 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3031 if (partialWakeTimer != null) {
3032 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3033 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003034 }
3035 }
3036 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003037
3038 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003039 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3040 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3041 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3042 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3043 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3044 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3045 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3046 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003047 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3048 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003049 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3050 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003051 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3052 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003053
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003054 // Dump Modem controller stats
3055 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3056 getModemControllerActivity(), which);
3057
Adam Lesinskie283d332015-04-16 12:29:25 -07003058 // Dump Wifi controller stats
3059 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3060 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003061 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003062 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003063
3064 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3065 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003066
3067 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003068 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3069 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 // Dump misc stats
3072 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003073 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003074 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003075 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003076 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003077 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003078 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3079 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003080 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003081 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3082 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3083 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3084 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003085 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003086
3087 // Dump screen brightness stats
3088 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3089 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003090 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003091 }
3092 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07003093
Dianne Hackborn627bba72009-03-24 22:32:56 -07003094 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003095 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3096 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003097 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003098 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003099 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003100 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003101 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003102 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003103 args[i] = getPhoneSignalStrengthCount(i, which);
3104 }
3105 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003106
Dianne Hackborn627bba72009-03-24 22:32:56 -07003107 // Dump network type stats
3108 args = new Object[NUM_DATA_CONNECTION_TYPES];
3109 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003110 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003111 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003112 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3113 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3114 args[i] = getPhoneDataConnectionCount(i, which);
3115 }
3116 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003117
3118 // Dump wifi state stats
3119 args = new Object[NUM_WIFI_STATES];
3120 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003121 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003122 }
3123 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3124 for (int i=0; i<NUM_WIFI_STATES; i++) {
3125 args[i] = getWifiStateCount(i, which);
3126 }
3127 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3128
Dianne Hackborn3251b902014-06-20 14:40:53 -07003129 // Dump wifi suppl state stats
3130 args = new Object[NUM_WIFI_SUPPL_STATES];
3131 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3132 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3133 }
3134 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3135 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3136 args[i] = getWifiSupplStateCount(i, which);
3137 }
3138 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3139
3140 // Dump wifi signal strength stats
3141 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3142 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3143 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3144 }
3145 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3146 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3147 args[i] = getWifiSignalStrengthCount(i, which);
3148 }
3149 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3150
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003151 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003152 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003153 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003156 if (which == STATS_SINCE_UNPLUGGED) {
3157 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3158 getDischargeStartLevel()-getDischargeCurrentLevel(),
3159 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003160 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3161 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003162 } else {
3163 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3164 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003165 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003166 getDischargeAmountScreenOffSinceCharge(),
3167 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003168 }
3169
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003170 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003171 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003172 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003173 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003174 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003175 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003176 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3177 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003178 }
Evan Millarc64edde2009-04-18 12:26:32 -07003179 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003180 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003181 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003182 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3183 // Not doing the regular wake lock formatting to remain compatible
3184 // with the old checkin format.
3185 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3186 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003187 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003188 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003189 }
3190 }
Evan Millarc64edde2009-04-18 12:26:32 -07003191 }
3192
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003193 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003194 helper.create(this);
3195 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003196 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003197 if (sippers != null && sippers.size() > 0) {
3198 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3199 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003200 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003201 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3202 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3203 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003204 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003205 int uid = 0;
3206 String label;
3207 switch (bs.drainType) {
3208 case IDLE:
3209 label="idle";
3210 break;
3211 case CELL:
3212 label="cell";
3213 break;
3214 case PHONE:
3215 label="phone";
3216 break;
3217 case WIFI:
3218 label="wifi";
3219 break;
3220 case BLUETOOTH:
3221 label="blue";
3222 break;
3223 case SCREEN:
3224 label="scrn";
3225 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003226 case FLASHLIGHT:
3227 label="flashlight";
3228 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003229 case APP:
3230 uid = bs.uidObj.getUid();
3231 label = "uid";
3232 break;
3233 case USER:
3234 uid = UserHandle.getUid(bs.userId, 0);
3235 label = "user";
3236 break;
3237 case UNACCOUNTED:
3238 label = "unacc";
3239 break;
3240 case OVERCOUNTED:
3241 label = "over";
3242 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003243 case CAMERA:
3244 label = "camera";
3245 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003246 default:
3247 label = "???";
3248 }
3249 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003250 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003251 }
3252 }
3253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 for (int iu = 0; iu < NU; iu++) {
3255 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003256 if (reqUid >= 0 && uid != reqUid) {
3257 continue;
3258 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003259 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003262 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3263 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3264 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3265 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3266 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3267 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3268 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3269 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003270 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003271 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3272 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003273 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003274 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3275 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003276 // Background data transfers
3277 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3278 which);
3279 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3280 which);
3281 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3282 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3283 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3284 which);
3285 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3286 which);
3287 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3288 which);
3289 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3290 which);
3291
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003292 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3293 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003294 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003295 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3296 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3297 || wifiBytesBgTx > 0
3298 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3299 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003300 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3301 wifiBytesRx, wifiBytesTx,
3302 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003303 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003304 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003305 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3306 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3307 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3308 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003309 }
3310
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003311 // Dump modem controller data, per UID.
3312 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3313 u.getModemControllerActivity(), which);
3314
3315 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003316 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3317 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3318 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003319 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3320 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003321 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3322 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3323 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003324 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003325 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003326 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3327 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003328 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3329 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003330 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003331 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003334 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3335 u.getWifiControllerActivity(), which);
3336
Bookatz867c0d72017-03-07 18:23:42 -08003337 final Timer bleTimer = u.getBluetoothScanTimer();
3338 if (bleTimer != null) {
3339 // Convert from microseconds to milliseconds with rounding
3340 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3341 / 1000;
3342 if (totalTime != 0) {
3343 final int count = bleTimer.getCountLocked(which);
3344 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3345 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
3346 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3347 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3348 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3349 final long actualTimeBg = bleTimerBg != null ?
3350 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatz956f36bf2017-04-28 09:48:17 -07003351 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3352 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003353 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatz956f36bf2017-04-28 09:48:17 -07003354 countBg, actualTime, actualTimeBg, resultCount);
Bookatz867c0d72017-03-07 18:23:42 -08003355 }
3356 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003357
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003358 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3359 u.getBluetoothControllerActivity(), which);
3360
Dianne Hackborn617f8772009-03-31 15:04:46 -07003361 if (u.hasUserActivity()) {
3362 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3363 boolean hasData = false;
3364 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3365 int val = u.getUserActivityCount(i, which);
3366 args[i] = val;
3367 if (val != 0) hasData = true;
3368 }
3369 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003370 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003371 }
3372 }
3373
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003374 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3375 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3376 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3377 String linePrefix = "";
3378 sb.setLength(0);
3379 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3380 rawRealtime, "f", which, linePrefix);
3381 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3382 rawRealtime, "p", which, linePrefix);
3383 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3384 rawRealtime, "w", which, linePrefix);
3385
3386 // Only log if we had at lease one wakelock...
3387 if (sb.length() > 0) {
3388 String name = wakelocks.keyAt(iw);
3389 if (name.indexOf(',') >= 0) {
3390 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003392 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 }
3394 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003395
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003396 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3397 for (int isy=syncs.size()-1; isy>=0; isy--) {
3398 final Timer timer = syncs.valueAt(isy);
3399 // Convert from microseconds to milliseconds with rounding
3400 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3401 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003402 final Timer bgTimer = timer.getSubTimer();
3403 final long bgTime = bgTimer != null ?
3404 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
3405 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003406 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003407 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003408 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003409 }
3410 }
3411
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003412 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3413 for (int ij=jobs.size()-1; ij>=0; ij--) {
3414 final Timer timer = jobs.valueAt(ij);
3415 // Convert from microseconds to milliseconds with rounding
3416 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3417 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003418 final Timer bgTimer = timer.getSubTimer();
3419 final long bgTime = bgTimer != null ?
3420 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
3421 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003422 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003423 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003424 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003425 }
3426 }
3427
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003428 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3429 rawRealtime, which);
3430 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3431 rawRealtime, which);
3432 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3433 rawRealtime, which);
3434 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3435 rawRealtime, which);
3436
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003437 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3438 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003439 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003440 final Uid.Sensor se = sensors.valueAt(ise);
3441 final int sensorNumber = sensors.keyAt(ise);
3442 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003443 if (timer != null) {
3444 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003445 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3446 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003447 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003448 final int count = timer.getCountLocked(which);
3449 final Timer bgTimer = se.getSensorBackgroundTime();
3450 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
3451 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3452 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3453 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3454 final long bgActualTime = bgTimer != null ?
3455 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3456 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3457 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 }
3460 }
3461
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003462 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3463 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003464
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003465 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3466 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003467
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003468 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003469 long totalStateTime = 0;
3470 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003471 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3472 totalStateTime += time;
3473 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003474 }
3475 if (totalStateTime > 0) {
3476 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3477 }
3478
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003479 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3480 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003481 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003482 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003483 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003484 }
3485
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003486 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3487 = u.getProcessStats();
3488 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3489 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003490
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003491 final long userMillis = ps.getUserTime(which);
3492 final long systemMillis = ps.getSystemTime(which);
3493 final long foregroundMillis = ps.getForegroundTime(which);
3494 final int starts = ps.getStarts(which);
3495 final int numCrashes = ps.getNumCrashes(which);
3496 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003497
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003498 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3499 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003500 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3501 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 }
3503 }
3504
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003505 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3506 = u.getPackageStats();
3507 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3508 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3509 int wakeups = 0;
3510 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3511 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003512 int count = alarms.valueAt(iwa).getCountLocked(which);
3513 wakeups += count;
3514 String name = alarms.keyAt(iwa).replace(',', '_');
3515 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003516 }
3517 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3518 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3519 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3520 final long startTime = ss.getStartTime(batteryUptime, which);
3521 final int starts = ss.getStarts(which);
3522 final int launches = ss.getLaunches(which);
3523 if (startTime != 0 || starts != 0 || launches != 0) {
3524 dumpLine(pw, uid, category, APK_DATA,
3525 wakeups, // wakeup alarms
3526 packageStats.keyAt(ipkg), // Apk
3527 serviceStats.keyAt(isvc), // service
3528 startTime / 1000, // time spent started, in ms
3529 starts,
3530 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 }
3532 }
3533 }
3534 }
3535 }
3536
Dianne Hackborn81038902012-11-26 17:04:09 -08003537 static final class TimerEntry {
3538 final String mName;
3539 final int mId;
3540 final BatteryStats.Timer mTimer;
3541 final long mTime;
3542 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3543 mName = name;
3544 mId = id;
3545 mTimer = timer;
3546 mTime = time;
3547 }
3548 }
3549
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003550 private void printmAh(PrintWriter printer, double power) {
3551 printer.print(BatteryStatsHelper.makemAh(power));
3552 }
3553
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003554 private void printmAh(StringBuilder sb, double power) {
3555 sb.append(BatteryStatsHelper.makemAh(power));
3556 }
3557
Dianne Hackbornd953c532014-08-16 18:17:38 -07003558 /**
3559 * Temporary for settings.
3560 */
3561 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3562 int reqUid) {
3563 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3564 }
3565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003567 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003568 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3570 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3571 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572
3573 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3574 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3575 final long totalRealtime = computeRealtime(rawRealtime, which);
3576 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003577 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3578 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3579 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003580 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3581 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003582
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003583 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003584
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003585 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003586 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003588 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3589 if (estimatedBatteryCapacity > 0) {
3590 sb.setLength(0);
3591 sb.append(prefix);
3592 sb.append(" Estimated battery capacity: ");
3593 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3594 sb.append(" mAh");
3595 pw.println(sb.toString());
3596 }
3597
Jocelyn Dangc627d102017-04-14 13:15:14 -07003598 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
3599 if (minLearnedBatteryCapacity > 0) {
3600 sb.setLength(0);
3601 sb.append(prefix);
3602 sb.append(" Min learned battery capacity: ");
3603 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
3604 sb.append(" mAh");
3605 pw.println(sb.toString());
3606 }
3607 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
3608 if (maxLearnedBatteryCapacity > 0) {
3609 sb.setLength(0);
3610 sb.append(prefix);
3611 sb.append(" Max learned battery capacity: ");
3612 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
3613 sb.append(" mAh");
3614 pw.println(sb.toString());
3615 }
3616
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003617 sb.setLength(0);
3618 sb.append(prefix);
3619 sb.append(" Time on battery: ");
3620 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3621 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3622 sb.append(") realtime, ");
3623 formatTimeMs(sb, whichBatteryUptime / 1000);
3624 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3625 sb.append(") uptime");
3626 pw.println(sb.toString());
3627 sb.setLength(0);
3628 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003629 sb.append(" Time on battery screen off: ");
3630 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3631 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3632 sb.append(") realtime, ");
3633 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3634 sb.append("(");
3635 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3636 sb.append(") uptime");
3637 pw.println(sb.toString());
3638 sb.setLength(0);
3639 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003640 sb.append(" Total run time: ");
3641 formatTimeMs(sb, totalRealtime / 1000);
3642 sb.append("realtime, ");
3643 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003644 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003645 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003646 if (batteryTimeRemaining >= 0) {
3647 sb.setLength(0);
3648 sb.append(prefix);
3649 sb.append(" Battery time remaining: ");
3650 formatTimeMs(sb, batteryTimeRemaining / 1000);
3651 pw.println(sb.toString());
3652 }
3653 if (chargeTimeRemaining >= 0) {
3654 sb.setLength(0);
3655 sb.append(prefix);
3656 sb.append(" Charge time remaining: ");
3657 formatTimeMs(sb, chargeTimeRemaining / 1000);
3658 pw.println(sb.toString());
3659 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003660
3661 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3662 final long dischargeCount = dischargeCounter.getCountLocked(which);
3663 if (dischargeCount >= 0) {
3664 sb.setLength(0);
3665 sb.append(prefix);
3666 sb.append(" Discharge: ");
3667 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3668 sb.append(" mAh");
3669 pw.println(sb.toString());
3670 }
3671
3672 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3673 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3674 if (dischargeScreenOffCount >= 0) {
3675 sb.setLength(0);
3676 sb.append(prefix);
3677 sb.append(" Screen off discharge: ");
3678 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3679 sb.append(" mAh");
3680 pw.println(sb.toString());
3681 }
3682
3683 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3684 if (dischargeScreenOnCount >= 0) {
3685 sb.setLength(0);
3686 sb.append(prefix);
3687 sb.append(" Screen on discharge: ");
3688 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3689 sb.append(" mAh");
3690 pw.println(sb.toString());
3691 }
3692
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003693 pw.print(" Start clock time: ");
3694 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3695
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003696 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003697 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003698 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003699 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3700 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003701 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003702 rawRealtime, which);
3703 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3704 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003705 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003706 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003707 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3708 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3709 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003710 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003711 sb.append(prefix);
3712 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3713 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003714 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003715 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3716 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003717 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003718 pw.println(sb.toString());
3719 sb.setLength(0);
3720 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003721 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003722 boolean didOne = false;
3723 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003724 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003725 if (time == 0) {
3726 continue;
3727 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003728 sb.append("\n ");
3729 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003730 didOne = true;
3731 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3732 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003733 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003734 sb.append("(");
3735 sb.append(formatRatioLocked(time, screenOnTime));
3736 sb.append(")");
3737 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003738 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003739 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003740 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003741 sb.setLength(0);
3742 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003743 sb.append(" Power save mode enabled: ");
3744 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003745 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003746 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003747 sb.append(")");
3748 pw.println(sb.toString());
3749 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003750 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003751 sb.setLength(0);
3752 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003753 sb.append(" Device light idling: ");
3754 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003755 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003756 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3757 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003758 sb.append("x");
3759 pw.println(sb.toString());
3760 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003761 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003762 sb.setLength(0);
3763 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003764 sb.append(" Idle mode light time: ");
3765 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003766 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003767 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3768 sb.append(") ");
3769 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003770 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003771 sb.append(" -- longest ");
3772 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3773 pw.println(sb.toString());
3774 }
3775 if (deviceIdlingTime != 0) {
3776 sb.setLength(0);
3777 sb.append(prefix);
3778 sb.append(" Device full idling: ");
3779 formatTimeMs(sb, deviceIdlingTime / 1000);
3780 sb.append("(");
3781 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003782 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003783 sb.append("x");
3784 pw.println(sb.toString());
3785 }
3786 if (deviceIdleModeFullTime != 0) {
3787 sb.setLength(0);
3788 sb.append(prefix);
3789 sb.append(" Idle mode full time: ");
3790 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3791 sb.append("(");
3792 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3793 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003794 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003795 sb.append("x");
3796 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003797 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003798 pw.println(sb.toString());
3799 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003800 if (phoneOnTime != 0) {
3801 sb.setLength(0);
3802 sb.append(prefix);
3803 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3804 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003805 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003806 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003807 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003808 if (connChanges != 0) {
3809 pw.print(prefix);
3810 pw.print(" Connectivity changes: "); pw.println(connChanges);
3811 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003812
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003813 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003814 long fullWakeLockTimeTotalMicros = 0;
3815 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003816
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003817 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003818
Evan Millar22ac0432009-03-31 11:33:18 -07003819 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003820 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003821
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003822 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3823 = u.getWakelockStats();
3824 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3825 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003826
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003827 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3828 if (fullWakeTimer != null) {
3829 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3830 rawRealtime, which);
3831 }
3832
3833 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3834 if (partialWakeTimer != null) {
3835 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3836 rawRealtime, which);
3837 if (totalTimeMicros > 0) {
3838 if (reqUid < 0) {
3839 // Only show the ordered list of all wake
3840 // locks if the caller is not asking for data
3841 // about a specific uid.
3842 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3843 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003844 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003845 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003846 }
3847 }
3848 }
3849 }
3850
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003851 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3852 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3853 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3854 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3855 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3856 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3857 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3858 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003859 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3860 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003861
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003862 if (fullWakeLockTimeTotalMicros != 0) {
3863 sb.setLength(0);
3864 sb.append(prefix);
3865 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3866 (fullWakeLockTimeTotalMicros + 500) / 1000);
3867 pw.println(sb.toString());
3868 }
3869
3870 if (partialWakeLockTimeTotalMicros != 0) {
3871 sb.setLength(0);
3872 sb.append(prefix);
3873 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3874 (partialWakeLockTimeTotalMicros + 500) / 1000);
3875 pw.println(sb.toString());
3876 }
3877
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003878 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003879 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3880 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3881 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3882 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003883 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003884 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003885 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003886 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003887 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003888 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003889 if (time == 0) {
3890 continue;
3891 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003892 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003893 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003894 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003895 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003896 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003897 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003898 sb.append("(");
3899 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003900 sb.append(") ");
3901 sb.append(getPhoneSignalStrengthCount(i, which));
3902 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003903 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003904 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003905 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003906
3907 sb.setLength(0);
3908 sb.append(prefix);
3909 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003910 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003911 pw.println(sb.toString());
3912
Dianne Hackborn627bba72009-03-24 22:32:56 -07003913 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003914 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003915 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003916 didOne = false;
3917 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003918 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003919 if (time == 0) {
3920 continue;
3921 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003922 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003923 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003924 didOne = true;
3925 sb.append(DATA_CONNECTION_NAMES[i]);
3926 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003927 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003928 sb.append("(");
3929 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003930 sb.append(") ");
3931 sb.append(getPhoneDataConnectionCount(i, which));
3932 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003933 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003934 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003935 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003936
3937 sb.setLength(0);
3938 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003939 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003940 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003941 formatTimeMs(sb, mobileActiveTime / 1000);
3942 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3943 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3944 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003945 pw.println(sb.toString());
3946
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003947 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3948 if (mobileActiveUnknownTime != 0) {
3949 sb.setLength(0);
3950 sb.append(prefix);
3951 sb.append(" Mobile radio active unknown time: ");
3952 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3953 sb.append("(");
3954 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3955 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3956 sb.append("x");
3957 pw.println(sb.toString());
3958 }
3959
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003960 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3961 if (mobileActiveAdjustedTime != 0) {
3962 sb.setLength(0);
3963 sb.append(prefix);
3964 sb.append(" Mobile radio active adjusted time: ");
3965 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3966 sb.append("(");
3967 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3968 sb.append(")");
3969 pw.println(sb.toString());
3970 }
3971
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003972 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3973
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003974 pw.print(prefix);
3975 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3976 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3977 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3978 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003979 sb.setLength(0);
3980 sb.append(prefix);
3981 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3982 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3983 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3984 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003985 sb.append(")");
3986 pw.println(sb.toString());
3987
3988 sb.setLength(0);
3989 sb.append(prefix);
3990 sb.append(" Wifi states:");
3991 didOne = false;
3992 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003993 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003994 if (time == 0) {
3995 continue;
3996 }
3997 sb.append("\n ");
3998 didOne = true;
3999 sb.append(WIFI_STATE_NAMES[i]);
4000 sb.append(" ");
4001 formatTimeMs(sb, time/1000);
4002 sb.append("(");
4003 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4004 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004005 sb.append(getWifiStateCount(i, which));
4006 sb.append("x");
4007 }
4008 if (!didOne) sb.append(" (no activity)");
4009 pw.println(sb.toString());
4010
4011 sb.setLength(0);
4012 sb.append(prefix);
4013 sb.append(" Wifi supplicant states:");
4014 didOne = false;
4015 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4016 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4017 if (time == 0) {
4018 continue;
4019 }
4020 sb.append("\n ");
4021 didOne = true;
4022 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4023 sb.append(" ");
4024 formatTimeMs(sb, time/1000);
4025 sb.append("(");
4026 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4027 sb.append(") ");
4028 sb.append(getWifiSupplStateCount(i, which));
4029 sb.append("x");
4030 }
4031 if (!didOne) sb.append(" (no activity)");
4032 pw.println(sb.toString());
4033
4034 sb.setLength(0);
4035 sb.append(prefix);
4036 sb.append(" Wifi signal levels:");
4037 didOne = false;
4038 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4039 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4040 if (time == 0) {
4041 continue;
4042 }
4043 sb.append("\n ");
4044 sb.append(prefix);
4045 didOne = true;
4046 sb.append("level(");
4047 sb.append(i);
4048 sb.append(") ");
4049 formatTimeMs(sb, time/1000);
4050 sb.append("(");
4051 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4052 sb.append(") ");
4053 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004054 sb.append("x");
4055 }
4056 if (!didOne) sb.append(" (no activity)");
4057 pw.println(sb.toString());
4058
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004059 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004060
Adam Lesinski50e47602015-12-04 17:04:54 -08004061 pw.print(prefix);
4062 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4063 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4064
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004065 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4066 sb.setLength(0);
4067 sb.append(prefix);
4068 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4069 pw.println(sb.toString());
4070
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004071 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4072 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004073
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004074 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004075
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004076 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004077 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004078 pw.print(prefix); pw.println(" Device is currently unplugged");
4079 pw.print(prefix); pw.print(" Discharge cycle start level: ");
4080 pw.println(getDischargeStartLevel());
4081 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4082 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004083 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004084 pw.print(prefix); pw.println(" Device is currently plugged into power");
4085 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
4086 pw.println(getDischargeStartLevel());
4087 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
4088 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004089 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004090 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4091 pw.println(getDischargeAmountScreenOn());
4092 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4093 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004094 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004095 } else {
4096 pw.print(prefix); pw.println(" Device battery use since last full charge");
4097 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4098 pw.println(getLowDischargeAmountSinceCharge());
4099 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4100 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004101 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4102 pw.println(getDischargeAmountScreenOnSinceCharge());
4103 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4104 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004105 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004106 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004107
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004108 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004109 helper.create(this);
4110 helper.refreshStats(which, UserHandle.USER_ALL);
4111 List<BatterySipper> sippers = helper.getUsageList();
4112 if (sippers != null && sippers.size() > 0) {
4113 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4114 pw.print(prefix); pw.print(" Capacity: ");
4115 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004116 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004117 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4118 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4119 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4120 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004121 pw.println();
4122 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004123 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004124 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004125 switch (bs.drainType) {
4126 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004127 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004128 break;
4129 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004130 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004131 break;
4132 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004133 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004134 break;
4135 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004136 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004137 break;
4138 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004139 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004140 break;
4141 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004142 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004143 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004144 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004145 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004146 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004147 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004148 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004149 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004150 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004151 break;
4152 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004153 pw.print(" User "); pw.print(bs.userId);
4154 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004155 break;
4156 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004157 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004158 break;
4159 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004160 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004161 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004162 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004163 pw.print(" Camera: ");
4164 break;
4165 default:
4166 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004167 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004168 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004169 printmAh(pw, bs.totalPowerMah);
4170
Adam Lesinski57123002015-06-12 16:12:07 -07004171 if (bs.usagePowerMah != bs.totalPowerMah) {
4172 // If the usage (generic power) isn't the whole amount, we list out
4173 // what components are involved in the calculation.
4174
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004175 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004176 if (bs.usagePowerMah != 0) {
4177 pw.print(" usage=");
4178 printmAh(pw, bs.usagePowerMah);
4179 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004180 if (bs.cpuPowerMah != 0) {
4181 pw.print(" cpu=");
4182 printmAh(pw, bs.cpuPowerMah);
4183 }
4184 if (bs.wakeLockPowerMah != 0) {
4185 pw.print(" wake=");
4186 printmAh(pw, bs.wakeLockPowerMah);
4187 }
4188 if (bs.mobileRadioPowerMah != 0) {
4189 pw.print(" radio=");
4190 printmAh(pw, bs.mobileRadioPowerMah);
4191 }
4192 if (bs.wifiPowerMah != 0) {
4193 pw.print(" wifi=");
4194 printmAh(pw, bs.wifiPowerMah);
4195 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004196 if (bs.bluetoothPowerMah != 0) {
4197 pw.print(" bt=");
4198 printmAh(pw, bs.bluetoothPowerMah);
4199 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004200 if (bs.gpsPowerMah != 0) {
4201 pw.print(" gps=");
4202 printmAh(pw, bs.gpsPowerMah);
4203 }
4204 if (bs.sensorPowerMah != 0) {
4205 pw.print(" sensor=");
4206 printmAh(pw, bs.sensorPowerMah);
4207 }
4208 if (bs.cameraPowerMah != 0) {
4209 pw.print(" camera=");
4210 printmAh(pw, bs.cameraPowerMah);
4211 }
4212 if (bs.flashlightPowerMah != 0) {
4213 pw.print(" flash=");
4214 printmAh(pw, bs.flashlightPowerMah);
4215 }
4216 pw.print(" )");
4217 }
4218 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004219 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004220 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004221 }
4222
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004223 sippers = helper.getMobilemsppList();
4224 if (sippers != null && sippers.size() > 0) {
4225 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004226 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004227 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004228 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004229 sb.setLength(0);
4230 sb.append(prefix); sb.append(" Uid ");
4231 UserHandle.formatUid(sb, bs.uidObj.getUid());
4232 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4233 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4234 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004235 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004236 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004237 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004238 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004239 sb.setLength(0);
4240 sb.append(prefix);
4241 sb.append(" TOTAL TIME: ");
4242 formatTimeMs(sb, totalTime);
4243 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4244 sb.append(")");
4245 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004246 pw.println();
4247 }
4248
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004249 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4250 @Override
4251 public int compare(TimerEntry lhs, TimerEntry rhs) {
4252 long lhsTime = lhs.mTime;
4253 long rhsTime = rhs.mTime;
4254 if (lhsTime < rhsTime) {
4255 return 1;
4256 }
4257 if (lhsTime > rhsTime) {
4258 return -1;
4259 }
4260 return 0;
4261 }
4262 };
4263
4264 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004265 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4266 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004267 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004268 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4269 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4270 : kernelWakelocks.entrySet()) {
4271 final BatteryStats.Timer timer = ent.getValue();
4272 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004273 if (totalTimeMillis > 0) {
4274 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4275 }
4276 }
4277 if (ktimers.size() > 0) {
4278 Collections.sort(ktimers, timerComparator);
4279 pw.print(prefix); pw.println(" All kernel wake locks:");
4280 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004281 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004282 String linePrefix = ": ";
4283 sb.setLength(0);
4284 sb.append(prefix);
4285 sb.append(" Kernel Wake lock ");
4286 sb.append(timer.mName);
4287 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4288 which, linePrefix);
4289 if (!linePrefix.equals(": ")) {
4290 sb.append(" realtime");
4291 // Only print out wake locks that were held
4292 pw.println(sb.toString());
4293 }
4294 }
4295 pw.println();
4296 }
4297 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004298
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004299 if (timers.size() > 0) {
4300 Collections.sort(timers, timerComparator);
4301 pw.print(prefix); pw.println(" All partial wake locks:");
4302 for (int i=0; i<timers.size(); i++) {
4303 TimerEntry timer = timers.get(i);
4304 sb.setLength(0);
4305 sb.append(" Wake lock ");
4306 UserHandle.formatUid(sb, timer.mId);
4307 sb.append(" ");
4308 sb.append(timer.mName);
4309 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4310 sb.append(" realtime");
4311 pw.println(sb.toString());
4312 }
4313 timers.clear();
4314 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004315 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004316
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004317 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004318 if (wakeupReasons.size() > 0) {
4319 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004320 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004321 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004322 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004323 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4324 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004325 }
4326 Collections.sort(reasons, timerComparator);
4327 for (int i=0; i<reasons.size(); i++) {
4328 TimerEntry timer = reasons.get(i);
4329 String linePrefix = ": ";
4330 sb.setLength(0);
4331 sb.append(prefix);
4332 sb.append(" Wakeup reason ");
4333 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004334 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4335 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004336 pw.println(sb.toString());
4337 }
4338 pw.println();
4339 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004340 }
Evan Millar22ac0432009-03-31 11:33:18 -07004341
James Carr2dd7e5e2016-07-20 18:48:39 -07004342 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4343 pw.println("Memory Stats");
4344 for (int i = 0; i < mMemoryStats.size(); i++) {
4345 sb.setLength(0);
4346 sb.append("Bandwidth ");
4347 sb.append(mMemoryStats.keyAt(i));
4348 sb.append(" Time ");
4349 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4350 pw.println(sb.toString());
4351 }
4352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004353 for (int iu=0; iu<NU; iu++) {
4354 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004355 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004356 continue;
4357 }
4358
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004359 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004360
4361 pw.print(prefix);
4362 pw.print(" ");
4363 UserHandle.formatUid(pw, uid);
4364 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004366
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004367 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4368 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4369 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4370 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004371 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4372 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4373
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004374 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4375 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004376 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4377 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004378
4379 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4380 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4381
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004382 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4383 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4384 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004385 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4386 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4387 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4388 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004389 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004390
Adam Lesinski5f056f62016-07-14 16:56:08 -07004391 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4392 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4393
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004394 if (mobileRxBytes > 0 || mobileTxBytes > 0
4395 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004396 pw.print(prefix); pw.print(" Mobile network: ");
4397 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004398 pw.print(formatBytesLocked(mobileTxBytes));
4399 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4400 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004402 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4403 sb.setLength(0);
4404 sb.append(prefix); sb.append(" Mobile radio active: ");
4405 formatTimeMs(sb, uidMobileActiveTime / 1000);
4406 sb.append("(");
4407 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4408 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4409 long packets = mobileRxPackets + mobileTxPackets;
4410 if (packets == 0) {
4411 packets = 1;
4412 }
4413 sb.append(" @ ");
4414 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4415 sb.append(" mspp");
4416 pw.println(sb.toString());
4417 }
4418
Adam Lesinski5f056f62016-07-14 16:56:08 -07004419 if (mobileWakeup > 0) {
4420 sb.setLength(0);
4421 sb.append(prefix);
4422 sb.append(" Mobile radio AP wakeups: ");
4423 sb.append(mobileWakeup);
4424 pw.println(sb.toString());
4425 }
4426
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004427 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4428 u.getModemControllerActivity(), which);
4429
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004430 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004431 pw.print(prefix); pw.print(" Wi-Fi network: ");
4432 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004433 pw.print(formatBytesLocked(wifiTxBytes));
4434 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4435 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004436 }
4437
Dianne Hackborn62793e42015-03-09 11:15:41 -07004438 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004439 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004440 || uidWifiRunningTime != 0) {
4441 sb.setLength(0);
4442 sb.append(prefix); sb.append(" Wifi Running: ");
4443 formatTimeMs(sb, uidWifiRunningTime / 1000);
4444 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4445 whichBatteryRealtime)); sb.append(")\n");
4446 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4447 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4448 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4449 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004450 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004451 formatTimeMs(sb, wifiScanTime / 1000);
4452 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004453 whichBatteryRealtime)); sb.append(") ");
4454 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004455 sb.append("x\n");
4456 // actual and background times are unpooled and since reset (regardless of 'which')
4457 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4458 formatTimeMs(sb, wifiScanActualTime / 1000);
4459 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4460 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4461 sb.append(") ");
4462 sb.append(wifiScanCount);
4463 sb.append("x\n");
4464 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4465 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4466 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4467 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4468 sb.append(") ");
4469 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004470 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004471 pw.println(sb.toString());
4472 }
4473
Adam Lesinski5f056f62016-07-14 16:56:08 -07004474 if (wifiWakeup > 0) {
4475 sb.setLength(0);
4476 sb.append(prefix);
4477 sb.append(" WiFi AP wakeups: ");
4478 sb.append(wifiWakeup);
4479 pw.println(sb.toString());
4480 }
4481
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004482 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4483 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004484
Adam Lesinski50e47602015-12-04 17:04:54 -08004485 if (btRxBytes > 0 || btTxBytes > 0) {
4486 pw.print(prefix); pw.print(" Bluetooth network: ");
4487 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4488 pw.print(formatBytesLocked(btTxBytes));
4489 pw.println(" sent");
4490 }
4491
Bookatz867c0d72017-03-07 18:23:42 -08004492 final Timer bleTimer = u.getBluetoothScanTimer();
4493 if (bleTimer != null) {
4494 // Convert from microseconds to milliseconds with rounding
4495 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4496 / 1000;
4497 if (totalTimeMs != 0) {
4498 final int count = bleTimer.getCountLocked(which);
4499 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4500 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
4501 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4502 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4503 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4504 final long actualTimeMsBg = bleTimerBg != null ?
4505 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatz956f36bf2017-04-28 09:48:17 -07004506 final int resultCount = u.getBluetoothScanResultCounter() != null ?
4507 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004508
4509 sb.setLength(0);
4510 sb.append(prefix);
4511 sb.append(" ");
4512 sb.append("Bluetooth Scan");
4513 sb.append(": ");
4514 if (actualTimeMs != totalTimeMs) {
4515 formatTimeMs(sb, totalTimeMs);
4516 sb.append("blamed realtime, ");
4517 }
4518 formatTimeMs(sb, actualTimeMs); // since reset, regardless of 'which'
4519 sb.append("realtime (");
4520 sb.append(count);
4521 sb.append(" times)");
4522 if (bleTimer.isRunningLocked()) {
4523 sb.append(" (running)");
4524 }
4525 if (actualTimeMsBg != 0 || countBg > 0) {
4526 sb.append(", ");
4527 formatTimeMs(sb, actualTimeMsBg); // since reset, regardless of 'which'
4528 sb.append("background (");
4529 sb.append(countBg);
4530 sb.append(" times)");
4531 }
Bookatz956f36bf2017-04-28 09:48:17 -07004532 sb.append("; Results count ");
4533 sb.append(resultCount);
Bookatz867c0d72017-03-07 18:23:42 -08004534 pw.println(sb.toString());
4535 uidActivity = true;
4536 }
4537 }
4538
4539
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004540
Dianne Hackborn617f8772009-03-31 15:04:46 -07004541 if (u.hasUserActivity()) {
4542 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004543 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004544 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004545 if (val != 0) {
4546 if (!hasData) {
4547 sb.setLength(0);
4548 sb.append(" User activity: ");
4549 hasData = true;
4550 } else {
4551 sb.append(", ");
4552 }
4553 sb.append(val);
4554 sb.append(" ");
4555 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4556 }
4557 }
4558 if (hasData) {
4559 pw.println(sb.toString());
4560 }
4561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004563 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4564 = u.getWakelockStats();
4565 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004566 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004567 int countWakelock = 0;
4568 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4569 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4570 String linePrefix = ": ";
4571 sb.setLength(0);
4572 sb.append(prefix);
4573 sb.append(" Wake lock ");
4574 sb.append(wakelocks.keyAt(iw));
4575 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4576 "full", which, linePrefix);
4577 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4578 "partial", which, linePrefix);
4579 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4580 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004581 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4582 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004583 sb.append(" realtime");
4584 pw.println(sb.toString());
4585 uidActivity = true;
4586 countWakelock++;
4587
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004588 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4589 rawRealtime, which);
4590 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4591 rawRealtime, which);
4592 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4593 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004594 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004595 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004596 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004597 if (countWakelock > 1) {
4598 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4599 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004600 sb.setLength(0);
4601 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004602 sb.append(" TOTAL wake: ");
4603 boolean needComma = false;
4604 if (totalFullWakelock != 0) {
4605 needComma = true;
4606 formatTimeMs(sb, totalFullWakelock);
4607 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004608 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004609 if (totalPartialWakelock != 0) {
4610 if (needComma) {
4611 sb.append(", ");
4612 }
4613 needComma = true;
4614 formatTimeMs(sb, totalPartialWakelock);
4615 sb.append("partial");
4616 }
4617 if (totalWindowWakelock != 0) {
4618 if (needComma) {
4619 sb.append(", ");
4620 }
4621 needComma = true;
4622 formatTimeMs(sb, totalWindowWakelock);
4623 sb.append("window");
4624 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004625 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004626 if (needComma) {
4627 sb.append(",");
4628 }
4629 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004630 formatTimeMs(sb, totalDrawWakelock);
4631 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004632 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004633 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004634 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004635 }
4636 }
4637
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004638 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4639 for (int isy=syncs.size()-1; isy>=0; isy--) {
4640 final Timer timer = syncs.valueAt(isy);
4641 // Convert from microseconds to milliseconds with rounding
4642 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4643 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004644 final Timer bgTimer = timer.getSubTimer();
4645 final long bgTime = bgTimer != null ?
4646 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4647 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004648 sb.setLength(0);
4649 sb.append(prefix);
4650 sb.append(" Sync ");
4651 sb.append(syncs.keyAt(isy));
4652 sb.append(": ");
4653 if (totalTime != 0) {
4654 formatTimeMs(sb, totalTime);
4655 sb.append("realtime (");
4656 sb.append(count);
4657 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07004658 if (bgTime > 0) {
4659 sb.append(", ");
4660 formatTimeMs(sb, bgTime);
4661 sb.append("background (");
4662 sb.append(bgCount);
4663 sb.append(" times)");
4664 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004665 } else {
4666 sb.append("(not used)");
4667 }
4668 pw.println(sb.toString());
4669 uidActivity = true;
4670 }
4671
4672 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4673 for (int ij=jobs.size()-1; ij>=0; ij--) {
4674 final Timer timer = jobs.valueAt(ij);
4675 // Convert from microseconds to milliseconds with rounding
4676 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4677 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004678 final Timer bgTimer = timer.getSubTimer();
4679 final long bgTime = bgTimer != null ?
4680 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4681 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004682 sb.setLength(0);
4683 sb.append(prefix);
4684 sb.append(" Job ");
4685 sb.append(jobs.keyAt(ij));
4686 sb.append(": ");
4687 if (totalTime != 0) {
4688 formatTimeMs(sb, totalTime);
4689 sb.append("realtime (");
4690 sb.append(count);
4691 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07004692 if (bgTime > 0) {
4693 sb.append(", ");
4694 formatTimeMs(sb, bgTime);
4695 sb.append("background (");
4696 sb.append(bgCount);
4697 sb.append(" times)");
4698 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004699 } else {
4700 sb.append("(not used)");
4701 }
4702 pw.println(sb.toString());
4703 uidActivity = true;
4704 }
4705
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004706 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4707 prefix, "Flashlight");
4708 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4709 prefix, "Camera");
4710 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4711 prefix, "Video");
4712 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4713 prefix, "Audio");
4714
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004715 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4716 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004717 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004718 final Uid.Sensor se = sensors.valueAt(ise);
4719 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004720 sb.setLength(0);
4721 sb.append(prefix);
4722 sb.append(" Sensor ");
4723 int handle = se.getHandle();
4724 if (handle == Uid.Sensor.GPS) {
4725 sb.append("GPS");
4726 } else {
4727 sb.append(handle);
4728 }
4729 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004730
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004731 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004732 if (timer != null) {
4733 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08004734 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4735 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004736 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08004737 final Timer bgTimer = se.getSensorBackgroundTime();
4738 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
4739 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4740 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4741 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4742 final long bgActualTime = bgTimer != null ?
4743 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4744
Dianne Hackborn61659e52014-07-09 16:13:01 -07004745 //timer.logState();
4746 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004747 if (actualTime != totalTime) {
4748 formatTimeMs(sb, totalTime);
4749 sb.append("blamed realtime, ");
4750 }
4751
4752 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07004753 sb.append("realtime (");
4754 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08004755 sb.append(" times)");
4756
4757 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004758 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08004759 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
4760 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004761 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08004762 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004764 } else {
4765 sb.append("(not used)");
4766 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004767 } else {
4768 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004769 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004770
4771 pw.println(sb.toString());
4772 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004773 }
4774
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004775 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4776 "Vibrator");
4777 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4778 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004779
Dianne Hackborn61659e52014-07-09 16:13:01 -07004780 long totalStateTime = 0;
4781 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4782 long time = u.getProcessStateTime(ips, rawRealtime, which);
4783 if (time > 0) {
4784 totalStateTime += time;
4785 sb.setLength(0);
4786 sb.append(prefix);
4787 sb.append(" ");
4788 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4789 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004790 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004791 pw.println(sb.toString());
4792 uidActivity = true;
4793 }
4794 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004795 if (totalStateTime > 0) {
4796 sb.setLength(0);
4797 sb.append(prefix);
4798 sb.append(" Total running: ");
4799 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4800 pw.println(sb.toString());
4801 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004802
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004803 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4804 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004805 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004806 sb.setLength(0);
4807 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004808 sb.append(" Total cpu time: u=");
4809 formatTimeMs(sb, userCpuTimeUs / 1000);
4810 sb.append("s=");
4811 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004812 pw.println(sb.toString());
4813 }
4814
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004815 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4816 = u.getProcessStats();
4817 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4818 final Uid.Proc ps = processStats.valueAt(ipr);
4819 long userTime;
4820 long systemTime;
4821 long foregroundTime;
4822 int starts;
4823 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004825 userTime = ps.getUserTime(which);
4826 systemTime = ps.getSystemTime(which);
4827 foregroundTime = ps.getForegroundTime(which);
4828 starts = ps.getStarts(which);
4829 final int numCrashes = ps.getNumCrashes(which);
4830 final int numAnrs = ps.getNumAnrs(which);
4831 numExcessive = which == STATS_SINCE_CHARGED
4832 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004834 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4835 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4836 sb.setLength(0);
4837 sb.append(prefix); sb.append(" Proc ");
4838 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4839 sb.append(prefix); sb.append(" CPU: ");
4840 formatTimeMs(sb, userTime); sb.append("usr + ");
4841 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4842 formatTimeMs(sb, foregroundTime); sb.append("fg");
4843 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4844 sb.append("\n"); sb.append(prefix); sb.append(" ");
4845 boolean hasOne = false;
4846 if (starts != 0) {
4847 hasOne = true;
4848 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004849 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004850 if (numCrashes != 0) {
4851 if (hasOne) {
4852 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004853 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004854 hasOne = true;
4855 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004856 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004857 if (numAnrs != 0) {
4858 if (hasOne) {
4859 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004861 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004862 }
4863 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004864 pw.println(sb.toString());
4865 for (int e=0; e<numExcessive; e++) {
4866 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4867 if (ew != null) {
4868 pw.print(prefix); pw.print(" * Killed for ");
4869 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4870 pw.print("wake lock");
4871 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4872 pw.print("cpu");
4873 } else {
4874 pw.print("unknown");
4875 }
4876 pw.print(" use: ");
4877 TimeUtils.formatDuration(ew.usedTime, pw);
4878 pw.print(" over ");
4879 TimeUtils.formatDuration(ew.overTime, pw);
4880 if (ew.overTime != 0) {
4881 pw.print(" (");
4882 pw.print((ew.usedTime*100)/ew.overTime);
4883 pw.println("%)");
4884 }
4885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004886 }
4887 uidActivity = true;
4888 }
4889 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004890
4891 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4892 = u.getPackageStats();
4893 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4894 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4895 pw.println(":");
4896 boolean apkActivity = false;
4897 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4898 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4899 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4900 pw.print(prefix); pw.print(" Wakeup alarm ");
4901 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4902 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4903 pw.println(" times");
4904 apkActivity = true;
4905 }
4906 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4907 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4908 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4909 final long startTime = ss.getStartTime(batteryUptime, which);
4910 final int starts = ss.getStarts(which);
4911 final int launches = ss.getLaunches(which);
4912 if (startTime != 0 || starts != 0 || launches != 0) {
4913 sb.setLength(0);
4914 sb.append(prefix); sb.append(" Service ");
4915 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4916 sb.append(prefix); sb.append(" Created for: ");
4917 formatTimeMs(sb, startTime / 1000);
4918 sb.append("uptime\n");
4919 sb.append(prefix); sb.append(" Starts: ");
4920 sb.append(starts);
4921 sb.append(", launches: "); sb.append(launches);
4922 pw.println(sb.toString());
4923 apkActivity = true;
4924 }
4925 }
4926 if (!apkActivity) {
4927 pw.print(prefix); pw.println(" (nothing executed)");
4928 }
4929 uidActivity = true;
4930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004931 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004932 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933 }
4934 }
4935 }
4936
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004937 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004938 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004939 int diff = oldval ^ newval;
4940 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004941 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004942 for (int i=0; i<descriptions.length; i++) {
4943 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004944 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004945 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004946 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004947 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004948 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004949 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4950 didWake = true;
4951 pw.print("=");
4952 if (longNames) {
4953 UserHandle.formatUid(pw, wakelockTag.uid);
4954 pw.print(":\"");
4955 pw.print(wakelockTag.string);
4956 pw.print("\"");
4957 } else {
4958 pw.print(wakelockTag.poolIdx);
4959 }
4960 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004961 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004962 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004963 pw.print("=");
4964 int val = (newval&bd.mask)>>bd.shift;
4965 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004966 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004967 } else {
4968 pw.print(val);
4969 }
4970 }
4971 }
4972 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004973 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004974 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004975 if (longNames) {
4976 UserHandle.formatUid(pw, wakelockTag.uid);
4977 pw.print(":\"");
4978 pw.print(wakelockTag.string);
4979 pw.print("\"");
4980 } else {
4981 pw.print(wakelockTag.poolIdx);
4982 }
4983 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004984 }
4985
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004986 public void prepareForDumpLocked() {
4987 }
4988
4989 public static class HistoryPrinter {
4990 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004991 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004992 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004993 int oldStatus = -1;
4994 int oldHealth = -1;
4995 int oldPlug = -1;
4996 int oldTemp = -1;
4997 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004998 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004999 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005000
Dianne Hackborn3251b902014-06-20 14:40:53 -07005001 void reset() {
5002 oldState = oldState2 = 0;
5003 oldLevel = -1;
5004 oldStatus = -1;
5005 oldHealth = -1;
5006 oldPlug = -1;
5007 oldTemp = -1;
5008 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005009 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005010 }
5011
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005012 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005013 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005014 if (!checkin) {
5015 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005016 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005017 pw.print(" (");
5018 pw.print(rec.numReadInts);
5019 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005020 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005021 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5022 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005023 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005024 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005025 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005026 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005027 }
5028 lastTime = rec.time;
5029 }
5030 if (rec.cmd == HistoryItem.CMD_START) {
5031 if (checkin) {
5032 pw.print(":");
5033 }
5034 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005035 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005036 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5037 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005038 if (checkin) {
5039 pw.print(":");
5040 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005041 if (rec.cmd == HistoryItem.CMD_RESET) {
5042 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005043 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005044 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005045 pw.print("TIME:");
5046 if (checkin) {
5047 pw.println(rec.currentTime);
5048 } else {
5049 pw.print(" ");
5050 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5051 rec.currentTime).toString());
5052 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005053 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5054 if (checkin) {
5055 pw.print(":");
5056 }
5057 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005058 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5059 if (checkin) {
5060 pw.print(":");
5061 }
5062 pw.println("*OVERFLOW*");
5063 } else {
5064 if (!checkin) {
5065 if (rec.batteryLevel < 10) pw.print("00");
5066 else if (rec.batteryLevel < 100) pw.print("0");
5067 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005068 if (verbose) {
5069 pw.print(" ");
5070 if (rec.states < 0) ;
5071 else if (rec.states < 0x10) pw.print("0000000");
5072 else if (rec.states < 0x100) pw.print("000000");
5073 else if (rec.states < 0x1000) pw.print("00000");
5074 else if (rec.states < 0x10000) pw.print("0000");
5075 else if (rec.states < 0x100000) pw.print("000");
5076 else if (rec.states < 0x1000000) pw.print("00");
5077 else if (rec.states < 0x10000000) pw.print("0");
5078 pw.print(Integer.toHexString(rec.states));
5079 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005080 } else {
5081 if (oldLevel != rec.batteryLevel) {
5082 oldLevel = rec.batteryLevel;
5083 pw.print(",Bl="); pw.print(rec.batteryLevel);
5084 }
5085 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005086 if (oldStatus != rec.batteryStatus) {
5087 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005088 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005089 switch (oldStatus) {
5090 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005091 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005092 break;
5093 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005094 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005095 break;
5096 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005097 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005098 break;
5099 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005100 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005101 break;
5102 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005103 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005104 break;
5105 default:
5106 pw.print(oldStatus);
5107 break;
5108 }
5109 }
5110 if (oldHealth != rec.batteryHealth) {
5111 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005112 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005113 switch (oldHealth) {
5114 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005115 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005116 break;
5117 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005118 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005119 break;
5120 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005121 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005122 break;
5123 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005124 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005125 break;
5126 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005127 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005128 break;
5129 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005130 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005131 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005132 case BatteryManager.BATTERY_HEALTH_COLD:
5133 pw.print(checkin ? "c" : "cold");
5134 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005135 default:
5136 pw.print(oldHealth);
5137 break;
5138 }
5139 }
5140 if (oldPlug != rec.batteryPlugType) {
5141 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005142 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005143 switch (oldPlug) {
5144 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005145 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005146 break;
5147 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005148 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005149 break;
5150 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005151 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005152 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005153 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005154 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005155 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005156 default:
5157 pw.print(oldPlug);
5158 break;
5159 }
5160 }
5161 if (oldTemp != rec.batteryTemperature) {
5162 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005163 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005164 pw.print(oldTemp);
5165 }
5166 if (oldVolt != rec.batteryVoltage) {
5167 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005168 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005169 pw.print(oldVolt);
5170 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005171 final int chargeMAh = rec.batteryChargeUAh / 1000;
5172 if (oldChargeMAh != chargeMAh) {
5173 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005174 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005175 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005176 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005177 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005178 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005179 printBitDescriptions(pw, oldState2, rec.states2, null,
5180 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005181 if (rec.wakeReasonTag != null) {
5182 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005183 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005184 pw.print(rec.wakeReasonTag.poolIdx);
5185 } else {
5186 pw.print(" wake_reason=");
5187 pw.print(rec.wakeReasonTag.uid);
5188 pw.print(":\"");
5189 pw.print(rec.wakeReasonTag.string);
5190 pw.print("\"");
5191 }
5192 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005193 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005194 pw.print(checkin ? "," : " ");
5195 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5196 pw.print("+");
5197 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5198 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005199 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005200 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5201 : HISTORY_EVENT_NAMES;
5202 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5203 | HistoryItem.EVENT_FLAG_FINISH);
5204 if (idx >= 0 && idx < eventNames.length) {
5205 pw.print(eventNames[idx]);
5206 } else {
5207 pw.print(checkin ? "Ev" : "event");
5208 pw.print(idx);
5209 }
5210 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005211 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005212 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005213 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005214 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5215 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005216 pw.print(":\"");
5217 pw.print(rec.eventTag.string);
5218 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005219 }
5220 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005221 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005222 if (rec.stepDetails != null) {
5223 if (!checkin) {
5224 pw.print(" Details: cpu=");
5225 pw.print(rec.stepDetails.userTime);
5226 pw.print("u+");
5227 pw.print(rec.stepDetails.systemTime);
5228 pw.print("s");
5229 if (rec.stepDetails.appCpuUid1 >= 0) {
5230 pw.print(" (");
5231 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5232 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5233 if (rec.stepDetails.appCpuUid2 >= 0) {
5234 pw.print(", ");
5235 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5236 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5237 }
5238 if (rec.stepDetails.appCpuUid3 >= 0) {
5239 pw.print(", ");
5240 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5241 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5242 }
5243 pw.print(')');
5244 }
5245 pw.println();
5246 pw.print(" /proc/stat=");
5247 pw.print(rec.stepDetails.statUserTime);
5248 pw.print(" usr, ");
5249 pw.print(rec.stepDetails.statSystemTime);
5250 pw.print(" sys, ");
5251 pw.print(rec.stepDetails.statIOWaitTime);
5252 pw.print(" io, ");
5253 pw.print(rec.stepDetails.statIrqTime);
5254 pw.print(" irq, ");
5255 pw.print(rec.stepDetails.statSoftIrqTime);
5256 pw.print(" sirq, ");
5257 pw.print(rec.stepDetails.statIdlTime);
5258 pw.print(" idle");
5259 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5260 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5261 + rec.stepDetails.statSoftIrqTime;
5262 int total = totalRun + rec.stepDetails.statIdlTime;
5263 if (total > 0) {
5264 pw.print(" (");
5265 float perc = ((float)totalRun) / ((float)total) * 100;
5266 pw.print(String.format("%.1f%%", perc));
5267 pw.print(" of ");
5268 StringBuilder sb = new StringBuilder(64);
5269 formatTimeMsNoSpace(sb, total*10);
5270 pw.print(sb);
5271 pw.print(")");
5272 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005273 pw.print(", PlatformIdleStat ");
5274 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005275 pw.println();
5276 } else {
5277 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5278 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5279 pw.print(rec.stepDetails.userTime);
5280 pw.print(":");
5281 pw.print(rec.stepDetails.systemTime);
5282 if (rec.stepDetails.appCpuUid1 >= 0) {
5283 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5284 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5285 if (rec.stepDetails.appCpuUid2 >= 0) {
5286 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5287 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5288 }
5289 if (rec.stepDetails.appCpuUid3 >= 0) {
5290 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5291 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5292 }
5293 }
5294 pw.println();
5295 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5296 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5297 pw.print(rec.stepDetails.statUserTime);
5298 pw.print(',');
5299 pw.print(rec.stepDetails.statSystemTime);
5300 pw.print(',');
5301 pw.print(rec.stepDetails.statIOWaitTime);
5302 pw.print(',');
5303 pw.print(rec.stepDetails.statIrqTime);
5304 pw.print(',');
5305 pw.print(rec.stepDetails.statSoftIrqTime);
5306 pw.print(',');
5307 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005308 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005309 if (rec.stepDetails.statPlatformIdleState != null) {
5310 pw.print(rec.stepDetails.statPlatformIdleState);
5311 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005312 pw.println();
5313 }
5314 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005315 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005316 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005317 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005318 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005319
5320 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5321 UserHandle.formatUid(pw, uid);
5322 pw.print("=");
5323 pw.print(utime);
5324 pw.print("u+");
5325 pw.print(stime);
5326 pw.print("s");
5327 }
5328
5329 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5330 pw.print('/');
5331 pw.print(uid);
5332 pw.print(":");
5333 pw.print(utime);
5334 pw.print(":");
5335 pw.print(stime);
5336 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005337 }
5338
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005339 private void printSizeValue(PrintWriter pw, long size) {
5340 float result = size;
5341 String suffix = "";
5342 if (result >= 10*1024) {
5343 suffix = "KB";
5344 result = result / 1024;
5345 }
5346 if (result >= 10*1024) {
5347 suffix = "MB";
5348 result = result / 1024;
5349 }
5350 if (result >= 10*1024) {
5351 suffix = "GB";
5352 result = result / 1024;
5353 }
5354 if (result >= 10*1024) {
5355 suffix = "TB";
5356 result = result / 1024;
5357 }
5358 if (result >= 10*1024) {
5359 suffix = "PB";
5360 result = result / 1024;
5361 }
5362 pw.print((int)result);
5363 pw.print(suffix);
5364 }
5365
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005366 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5367 String label3, long estimatedTime) {
5368 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005369 return false;
5370 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005371 pw.print(label1);
5372 pw.print(label2);
5373 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005374 StringBuilder sb = new StringBuilder(64);
5375 formatTimeMs(sb, estimatedTime);
5376 pw.print(sb);
5377 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005378 return true;
5379 }
5380
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005381 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5382 LevelStepTracker steps, boolean checkin) {
5383 if (steps == null) {
5384 return false;
5385 }
5386 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005387 if (count <= 0) {
5388 return false;
5389 }
5390 if (!checkin) {
5391 pw.println(header);
5392 }
Kweku Adams030980a2015-04-01 16:07:48 -07005393 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005394 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005395 long duration = steps.getDurationAt(i);
5396 int level = steps.getLevelAt(i);
5397 long initMode = steps.getInitModeAt(i);
5398 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005399 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005400 lineArgs[0] = Long.toString(duration);
5401 lineArgs[1] = Integer.toString(level);
5402 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5403 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5404 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5405 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5406 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5407 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005408 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005409 }
5410 } else {
5411 lineArgs[2] = "";
5412 }
5413 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5414 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5415 } else {
5416 lineArgs[3] = "";
5417 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005418 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005419 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005420 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005421 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005422 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005423 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5424 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005425 pw.print(prefix);
5426 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005427 TimeUtils.formatDuration(duration, pw);
5428 pw.print(" to "); pw.print(level);
5429 boolean haveModes = false;
5430 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5431 pw.print(" (");
5432 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5433 case Display.STATE_OFF: pw.print("screen-off"); break;
5434 case Display.STATE_ON: pw.print("screen-on"); break;
5435 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5436 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005437 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005438 }
5439 haveModes = true;
5440 }
5441 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5442 pw.print(haveModes ? ", " : " (");
5443 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5444 ? "power-save-on" : "power-save-off");
5445 haveModes = true;
5446 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005447 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5448 pw.print(haveModes ? ", " : " (");
5449 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5450 ? "device-idle-on" : "device-idle-off");
5451 haveModes = true;
5452 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005453 if (haveModes) {
5454 pw.print(")");
5455 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005456 pw.println();
5457 }
5458 }
5459 return true;
5460 }
5461
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005462 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005463 public static final int DUMP_DAILY_ONLY = 1<<2;
5464 public static final int DUMP_HISTORY_ONLY = 1<<3;
5465 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5466 public static final int DUMP_VERBOSE = 1<<5;
5467 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005468
Dianne Hackborn37de0982014-05-09 09:32:18 -07005469 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5470 final HistoryPrinter hprinter = new HistoryPrinter();
5471 final HistoryItem rec = new HistoryItem();
5472 long lastTime = -1;
5473 long baseTime = -1;
5474 boolean printed = false;
5475 HistoryEventTracker tracker = null;
5476 while (getNextHistoryLocked(rec)) {
5477 lastTime = rec.time;
5478 if (baseTime < 0) {
5479 baseTime = lastTime;
5480 }
5481 if (rec.time >= histStart) {
5482 if (histStart >= 0 && !printed) {
5483 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005484 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005485 || rec.cmd == HistoryItem.CMD_START
5486 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005487 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005488 hprinter.printNextItem(pw, rec, baseTime, checkin,
5489 (flags&DUMP_VERBOSE) != 0);
5490 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005491 } else if (rec.currentTime != 0) {
5492 printed = true;
5493 byte cmd = rec.cmd;
5494 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005495 hprinter.printNextItem(pw, rec, baseTime, checkin,
5496 (flags&DUMP_VERBOSE) != 0);
5497 rec.cmd = cmd;
5498 }
5499 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005500 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5501 hprinter.printNextItem(pw, rec, baseTime, checkin,
5502 (flags&DUMP_VERBOSE) != 0);
5503 rec.cmd = HistoryItem.CMD_UPDATE;
5504 }
5505 int oldEventCode = rec.eventCode;
5506 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005507 rec.eventTag = new HistoryTag();
5508 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5509 HashMap<String, SparseIntArray> active
5510 = tracker.getStateForEvent(i);
5511 if (active == null) {
5512 continue;
5513 }
5514 for (HashMap.Entry<String, SparseIntArray> ent
5515 : active.entrySet()) {
5516 SparseIntArray uids = ent.getValue();
5517 for (int j=0; j<uids.size(); j++) {
5518 rec.eventCode = i;
5519 rec.eventTag.string = ent.getKey();
5520 rec.eventTag.uid = uids.keyAt(j);
5521 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005522 hprinter.printNextItem(pw, rec, baseTime, checkin,
5523 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005524 rec.wakeReasonTag = null;
5525 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005526 }
5527 }
5528 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005529 rec.eventCode = oldEventCode;
5530 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005531 tracker = null;
5532 }
5533 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005534 hprinter.printNextItem(pw, rec, baseTime, checkin,
5535 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005536 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5537 // This is an attempt to aggregate the previous state and generate
5538 // fake events to reflect that state at the point where we start
5539 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005540 if (tracker == null) {
5541 tracker = new HistoryEventTracker();
5542 }
5543 tracker.updateState(rec.eventCode, rec.eventTag.string,
5544 rec.eventTag.uid, rec.eventTag.poolIdx);
5545 }
5546 }
5547 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005548 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005549 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5550 }
5551 }
5552
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005553 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5554 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5555 if (steps == null) {
5556 return;
5557 }
5558 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5559 if (timeRemaining >= 0) {
5560 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5561 tmpSb.setLength(0);
5562 formatTimeMs(tmpSb, timeRemaining);
5563 pw.print(tmpSb);
5564 pw.print(" (from "); pw.print(tmpOutInt[0]);
5565 pw.println(" steps)");
5566 }
5567 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5568 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5569 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5570 if (estimatedTime > 0) {
5571 pw.print(prefix); pw.print(label); pw.print(" ");
5572 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5573 pw.print(" time: ");
5574 tmpSb.setLength(0);
5575 formatTimeMs(tmpSb, estimatedTime);
5576 pw.print(tmpSb);
5577 pw.print(" (from "); pw.print(tmpOutInt[0]);
5578 pw.println(" steps)");
5579 }
5580 }
5581 }
5582
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005583 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5584 ArrayList<PackageChange> changes) {
5585 if (changes == null) {
5586 return;
5587 }
5588 pw.print(prefix); pw.println("Package changes:");
5589 for (int i=0; i<changes.size(); i++) {
5590 PackageChange pc = changes.get(i);
5591 if (pc.mUpdate) {
5592 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5593 pw.print(" vers="); pw.println(pc.mVersionCode);
5594 } else {
5595 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5596 }
5597 }
5598 }
5599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 /**
5601 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5602 *
5603 * @param pw a Printer to receive the dump output.
5604 */
5605 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005606 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005607 prepareForDumpLocked();
5608
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005609 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005610 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005611
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005612 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005613 final long historyTotalSize = getHistoryTotalSize();
5614 final long historyUsedSize = getHistoryUsedSize();
5615 if (startIteratingHistoryLocked()) {
5616 try {
5617 pw.print("Battery History (");
5618 pw.print((100*historyUsedSize)/historyTotalSize);
5619 pw.print("% used, ");
5620 printSizeValue(pw, historyUsedSize);
5621 pw.print(" used of ");
5622 printSizeValue(pw, historyTotalSize);
5623 pw.print(", ");
5624 pw.print(getHistoryStringPoolSize());
5625 pw.print(" strings using ");
5626 printSizeValue(pw, getHistoryStringPoolBytes());
5627 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005628 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005629 pw.println();
5630 } finally {
5631 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005632 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005633 }
5634
5635 if (startIteratingOldHistoryLocked()) {
5636 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005637 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005638 pw.println("Old battery History:");
5639 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005640 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005641 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005642 if (baseTime < 0) {
5643 baseTime = rec.time;
5644 }
5645 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005646 }
5647 pw.println();
5648 } finally {
5649 finishIteratingOldHistoryLocked();
5650 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005651 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005652 }
5653
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005654 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005655 return;
5656 }
5657
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005658 if (!filtering) {
5659 SparseArray<? extends Uid> uidStats = getUidStats();
5660 final int NU = uidStats.size();
5661 boolean didPid = false;
5662 long nowRealtime = SystemClock.elapsedRealtime();
5663 for (int i=0; i<NU; i++) {
5664 Uid uid = uidStats.valueAt(i);
5665 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5666 if (pids != null) {
5667 for (int j=0; j<pids.size(); j++) {
5668 Uid.Pid pid = pids.valueAt(j);
5669 if (!didPid) {
5670 pw.println("Per-PID Stats:");
5671 didPid = true;
5672 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005673 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5674 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005675 pw.print(" PID "); pw.print(pids.keyAt(j));
5676 pw.print(" wake time: ");
5677 TimeUtils.formatDuration(time, pw);
5678 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005679 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005680 }
5681 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005682 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005683 pw.println();
5684 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005685 }
5686
5687 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005688 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5689 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005690 long timeRemaining = computeBatteryTimeRemaining(
5691 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005692 if (timeRemaining >= 0) {
5693 pw.print(" Estimated discharge time remaining: ");
5694 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5695 pw.println();
5696 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005697 final LevelStepTracker steps = getDischargeLevelStepTracker();
5698 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5699 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5700 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5701 STEP_LEVEL_MODE_VALUES[i], null));
5702 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005703 pw.println();
5704 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005705 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5706 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005707 long timeRemaining = computeChargeTimeRemaining(
5708 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005709 if (timeRemaining >= 0) {
5710 pw.print(" Estimated charge time remaining: ");
5711 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5712 pw.println();
5713 }
5714 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005715 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005716 }
5717 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5718 pw.println("Daily stats:");
5719 pw.print(" Current start time: ");
5720 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5721 getCurrentDailyStartTime()).toString());
5722 pw.print(" Next min deadline: ");
5723 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5724 getNextMinDailyDeadline()).toString());
5725 pw.print(" Next max deadline: ");
5726 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5727 getNextMaxDailyDeadline()).toString());
5728 StringBuilder sb = new StringBuilder(64);
5729 int[] outInt = new int[1];
5730 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5731 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005732 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5733 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005734 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005735 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5736 dsteps, false)) {
5737 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5738 sb, outInt);
5739 }
5740 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5741 csteps, false)) {
5742 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5743 sb, outInt);
5744 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005745 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005746 } else {
5747 pw.println(" Current daily steps:");
5748 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5749 sb, outInt);
5750 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5751 sb, outInt);
5752 }
5753 }
5754 DailyItem dit;
5755 int curIndex = 0;
5756 while ((dit=getDailyItemLocked(curIndex)) != null) {
5757 curIndex++;
5758 if ((flags&DUMP_DAILY_ONLY) != 0) {
5759 pw.println();
5760 }
5761 pw.print(" Daily from ");
5762 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5763 pw.print(" to ");
5764 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5765 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005766 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005767 if (dumpDurationSteps(pw, " ",
5768 " Discharge step durations:", dit.mDischargeSteps, false)) {
5769 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5770 sb, outInt);
5771 }
5772 if (dumpDurationSteps(pw, " ",
5773 " Charge step durations:", dit.mChargeSteps, false)) {
5774 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5775 sb, outInt);
5776 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005777 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005778 } else {
5779 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5780 sb, outInt);
5781 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5782 sb, outInt);
5783 }
5784 }
5785 pw.println();
5786 }
5787 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005788 pw.println("Statistics since last charge:");
5789 pw.println(" System starts: " + getStartCount()
5790 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005791 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5792 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005793 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 }
5796
5797 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005798 public void dumpCheckinLocked(Context context, PrintWriter pw,
5799 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005800 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005801
5802 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005803 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5804 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005805
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005806 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5807
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005808 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005809 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005810
5811 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005812 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005813 try {
5814 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5815 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5816 pw.print(HISTORY_STRING_POOL); pw.print(',');
5817 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005818 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005819 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005820 pw.print(",\"");
5821 String str = getHistoryTagPoolString(i);
5822 str = str.replace("\\", "\\\\");
5823 str = str.replace("\"", "\\\"");
5824 pw.print(str);
5825 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005826 pw.println();
5827 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005828 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005829 } finally {
5830 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005831 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005832 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005833 }
5834
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005835 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005836 return;
5837 }
5838
Dianne Hackborne4a59512010-12-07 11:08:07 -08005839 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005840 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005841 for (int i=0; i<apps.size(); i++) {
5842 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005843 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
5844 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005845 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005846 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
5847 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005848 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005849 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005850 }
5851 SparseArray<? extends Uid> uidStats = getUidStats();
5852 final int NU = uidStats.size();
5853 String[] lineArgs = new String[2];
5854 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005855 int uid = UserHandle.getAppId(uidStats.keyAt(i));
5856 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
5857 if (pkgs != null && !pkgs.second.value) {
5858 pkgs.second.value = true;
5859 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005860 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005861 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005862 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5863 (Object[])lineArgs);
5864 }
5865 }
5866 }
5867 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005868 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005869 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005870 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07005871 long timeRemaining = computeBatteryTimeRemaining(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 */, DISCHARGE_TIME_REMAIN_DATA,
5875 (Object[])lineArgs);
5876 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005877 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07005878 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005879 if (timeRemaining >= 0) {
5880 lineArgs[0] = Long.toString(timeRemaining);
5881 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5882 (Object[])lineArgs);
5883 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005884 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5885 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888}