blob: a87cb0929a84d060b94e6e2fc478d9367898723f [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:
Bookatzaa4594a2017-03-24 12:39:56 -0700185 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700186 */
Kweku Adams47db5a82016-12-09 19:04:50 -0800187 static final String CHECKIN_VERSION = "20";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700188
189 /**
190 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700192 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700193
Evan Millar22ac0432009-03-31 11:33:18 -0700194 private static final long BYTES_PER_KB = 1024;
195 private static final long BYTES_PER_MB = 1048576; // 1024^2
196 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700198 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800199 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700200 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700202 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700203 private static final String CPU_DATA = "cpu";
Evan Millare84de8d2009-04-02 22:16:12 -0700204 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800205 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700206 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700207 private static final String STATE_TIME_DATA = "st";
Evan Millare84de8d2009-04-02 22:16:12 -0700208 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700209 private static final String SYNC_DATA = "sy";
210 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700211 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700212 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700213 private static final String NETWORK_DATA = "nt";
214 private static final String USER_ACTIVITY_DATA = "ua";
215 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800216 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700217 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700218 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700219 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800220 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
221 private static final String WIFI_CONTROLLER_DATA = "wfcd";
222 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
223 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700224 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700225 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800226 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800227 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
228 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800229 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700230 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700231 private static final String SCREEN_BRIGHTNESS_DATA = "br";
232 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700233 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700234 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
235 private static final String DATA_CONNECTION_TIME_DATA = "dct";
236 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800237 private static final String WIFI_STATE_TIME_DATA = "wst";
238 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700239 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
240 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
241 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
242 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800243 private static final String POWER_USE_SUMMARY_DATA = "pws";
244 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700245 private static final String DISCHARGE_STEP_DATA = "dsd";
246 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700247 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
248 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700249 private static final String FLASHLIGHT_DATA = "fla";
250 private static final String CAMERA_DATA = "cam";
251 private static final String VIDEO_DATA = "vid";
252 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Adam Lesinski010bf372016-04-11 12:18:18 -0700254 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
255
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700256 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 private final Formatter mFormatter = new Formatter(mFormatBuilder);
258
259 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700260 * State for keeping track of counting information.
261 */
262 public static abstract class Counter {
263
264 /**
265 * Returns the count associated with this Counter for the
266 * selected type of statistics.
267 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700268 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700269 */
Evan Millarc64edde2009-04-18 12:26:32 -0700270 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700271
272 /**
273 * Temporary for debugging.
274 */
275 public abstract void logState(Printer pw, String prefix);
276 }
277
278 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700279 * State for keeping track of long counting information.
280 */
281 public static abstract class LongCounter {
282
283 /**
284 * Returns the count associated with this Counter for the
285 * selected type of statistics.
286 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700287 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700288 */
289 public abstract long getCountLocked(int which);
290
291 /**
292 * Temporary for debugging.
293 */
294 public abstract void logState(Printer pw, String prefix);
295 }
296
297 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800298 * Container class that aggregates counters for transmit, receive, and idle state of a
299 * radio controller.
300 */
301 public static abstract class ControllerActivityCounter {
302 /**
303 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
304 * idle state.
305 */
306 public abstract LongCounter getIdleTimeCounter();
307
308 /**
309 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
310 * receive state.
311 */
312 public abstract LongCounter getRxTimeCounter();
313
314 /**
315 * An array of {@link LongCounter}, representing various transmit levels, where each level
316 * may draw a different amount of power. The levels themselves are controller-specific.
317 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
318 * various transmit level states.
319 */
320 public abstract LongCounter[] getTxTimeCounters();
321
322 /**
323 * @return a non-null {@link LongCounter} representing the power consumed by the controller
324 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
325 * yield a value of 0 if the device doesn't support power calculations.
326 */
327 public abstract LongCounter getPowerCounter();
328 }
329
330 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 * State for keeping track of timing information.
332 */
333 public static abstract class Timer {
334
335 /**
336 * Returns the count associated with this Timer for the
337 * selected type of statistics.
338 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700339 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 */
Evan Millarc64edde2009-04-18 12:26:32 -0700341 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342
343 /**
344 * Returns the total time in microseconds associated with this Timer for the
345 * selected type of statistics.
346 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800347 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700348 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * @return a time in microseconds
350 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800351 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700354 * Returns the total time in microseconds associated with this Timer since the
355 * 'mark' was last set.
356 *
357 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
358 * @return a time in microseconds
359 */
360 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
361
362 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700363 * Returns the max duration if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800364 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700365
366 */
367 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
368 return -1;
369 }
370
371 /**
372 * Returns the current time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800373 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700374 */
375 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
376 return -1;
377 }
378
379 /**
Bookatz867c0d72017-03-07 18:23:42 -0800380 * Returns the current time the timer has been active, if it is being tracked.
381 *
382 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
383 * been on since reset.
384 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
385 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
386 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
387 * the actual total time.
388 * Not all Timer subclasses track the max, total, current durations.
389 */
390 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
391 return -1;
392 }
393
394 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700395 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
396 * used, for example, for tracking background usage. Secondary timers are never pooled.
397 *
398 * Not all Timer subclasses have a secondary timer; those that don't return null.
399 */
400 public Timer getSubTimer() {
401 return null;
402 }
403
404 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700405 * Returns whether the timer is currently running. Some types of timers
406 * (e.g. BatchTimers) don't know whether the event is currently active,
407 * and report false.
408 */
409 public boolean isRunningLocked() {
410 return false;
411 }
412
413 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 * Temporary for debugging.
415 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700416 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418
419 /**
420 * The statistics associated with a particular uid.
421 */
422 public static abstract class Uid {
423
424 /**
425 * Returns a mapping containing wakelock statistics.
426 *
427 * @return a Map from Strings to Uid.Wakelock objects.
428 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700429 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430
431 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700432 * Returns a mapping containing sync statistics.
433 *
434 * @return a Map from Strings to Timer objects.
435 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700436 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700437
438 /**
439 * Returns a mapping containing scheduled job statistics.
440 *
441 * @return a Map from Strings to Timer objects.
442 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700443 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700444
445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 * The statistics associated with a particular wake lock.
447 */
448 public static abstract class Wakelock {
449 public abstract Timer getWakeTime(int type);
450 }
451
452 /**
453 * Returns a mapping containing sensor statistics.
454 *
455 * @return a Map from Integer sensor ids to Uid.Sensor objects.
456 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700457 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458
459 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700460 * Returns a mapping containing active process data.
461 */
462 public abstract SparseArray<? extends Pid> getPidStats();
463
464 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 * Returns a mapping containing process statistics.
466 *
467 * @return a Map from Strings to Uid.Proc objects.
468 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700469 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470
471 /**
472 * Returns a mapping containing package statistics.
473 *
474 * @return a Map from Strings to Uid.Pkg objects.
475 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700476 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700477
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800478 public abstract ControllerActivityCounter getWifiControllerActivity();
479 public abstract ControllerActivityCounter getBluetoothControllerActivity();
480 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800481
482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 * {@hide}
484 */
485 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700486
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800487 public abstract void noteWifiRunningLocked(long elapsedRealtime);
488 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
489 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
490 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
491 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
492 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
493 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
494 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
495 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
496 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800497 public abstract void noteActivityResumedLocked(long elapsedRealtime);
498 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800499 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
500 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
501 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700502 public abstract int getWifiScanCount(int which);
Bookatz867c0d72017-03-07 18:23:42 -0800503 public abstract int getWifiScanBackgroundCount(int which);
504 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
505 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800506 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700507 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800508 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700509 public abstract Timer getAudioTurnedOnTimer();
510 public abstract Timer getVideoTurnedOnTimer();
511 public abstract Timer getFlashlightTurnedOnTimer();
512 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700513 public abstract Timer getForegroundActivityTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800514 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800515 public abstract Timer getBluetoothScanBackgroundTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700516
Dianne Hackborna0200e32016-03-30 18:01:41 -0700517 // Note: the following times are disjoint. They can be added together to find the
518 // total time a uid has had any processes running at all.
519
520 /**
521 * Time this uid has any processes in the top state (or above such as persistent).
522 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800523 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700524 /**
525 * Time this uid has any process with a started out bound foreground service, but
526 * none in the "top" state.
527 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800528 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700529 /**
530 * Time this uid has any process that is top while the device is sleeping, but none
531 * in the "foreground service" or better state.
532 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800533 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700534 /**
535 * Time this uid has any process in an active foreground state, but none in the
536 * "top sleeping" or better state.
537 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800538 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700539 /**
540 * Time this uid has any process in an active background state, but none in the
541 * "foreground" or better state.
542 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800543 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700544 /**
545 * Time this uid has any processes that are sitting around cached, not in one of the
546 * other active states.
547 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800548 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700549 /**
550 * Total number of process states we track.
551 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800552 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700553
554 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800555 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700556 };
557
558 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800559 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700560
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800561 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562
Robert Greenwalta029ea12013-09-25 16:38:12 -0700563 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
564
Dianne Hackborn617f8772009-03-31 15:04:46 -0700565 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700566 * Note that these must match the constants in android.os.PowerManager.
567 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
568 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700569 */
570 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700571 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700572 };
573
Phil Weaverda80d672016-03-15 16:25:46 -0700574 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700575
Dianne Hackborn617f8772009-03-31 15:04:46 -0700576 public abstract void noteUserActivityLocked(int type);
577 public abstract boolean hasUserActivity();
578 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700579
580 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800581 public abstract long getNetworkActivityBytes(int type, int which);
582 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800583 public abstract long getMobileRadioActiveTime(int which);
584 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700585
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700586 /**
587 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
588 */
589 public abstract long getUserCpuTimeUs(int which);
590
591 /**
592 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
593 */
594 public abstract long getSystemCpuTimeUs(int which);
595
596 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700597 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
598 * given CPU cluster.
599 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700600 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700601 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700602 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
603 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700604 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700605 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700606
Adam Lesinski5f056f62016-07-14 16:56:08 -0700607 /**
608 * Returns the number of times this UID woke up the Application Processor to
609 * process a mobile radio packet.
610 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
611 */
612 public abstract long getMobileRadioApWakeupCount(int which);
613
614 /**
615 * Returns the number of times this UID woke up the Application Processor to
616 * process a WiFi packet.
617 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
618 */
619 public abstract long getWifiRadioApWakeupCount(int which);
620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800622 /*
623 * FIXME: it's not correct to use this magic value because it
624 * could clash with a sensor handle (which are defined by
625 * the sensor HAL, and therefore out of our control
626 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 // Magic sensor number for the GPS.
628 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800633
Bookatz867c0d72017-03-07 18:23:42 -0800634 /** Returns a Timer for sensor usage when app is in the background. */
635 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
637
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700638 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800639 public int mWakeNesting;
640 public long mWakeSumMs;
641 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700642 }
643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 /**
645 * The statistics associated with a particular process.
646 */
647 public static abstract class Proc {
648
Dianne Hackborn287952c2010-09-22 22:34:31 -0700649 public static class ExcessivePower {
650 public static final int TYPE_WAKE = 1;
651 public static final int TYPE_CPU = 2;
652
653 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700654 public long overTime;
655 public long usedTime;
656 }
657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800659 * Returns true if this process is still active in the battery stats.
660 */
661 public abstract boolean isActive();
662
663 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700664 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700666 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 */
668 public abstract long getUserTime(int which);
669
670 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700671 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700673 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 */
675 public abstract long getSystemTime(int which);
676
677 /**
678 * Returns the number of times the process has been started.
679 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700680 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 */
682 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700683
684 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800685 * Returns the number of times the process has crashed.
686 *
687 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
688 */
689 public abstract int getNumCrashes(int which);
690
691 /**
692 * Returns the number of times the process has ANRed.
693 *
694 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
695 */
696 public abstract int getNumAnrs(int which);
697
698 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700699 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700700 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700701 * @return foreground cpu time in microseconds
702 */
703 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700704
Dianne Hackborn287952c2010-09-22 22:34:31 -0700705 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700706
Dianne Hackborn287952c2010-09-22 22:34:31 -0700707 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 }
709
710 /**
711 * The statistics associated with a particular package.
712 */
713 public static abstract class Pkg {
714
715 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700716 * Returns information about all wakeup alarms that have been triggered for this
717 * package. The mapping keys are tag names for the alarms, the counter contains
718 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700720 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721
722 /**
723 * Returns a mapping containing service statistics.
724 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700725 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
727 /**
728 * The statistics associated with a particular service.
729 */
Joe Onoratoabded112016-02-08 16:49:39 -0800730 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731
732 /**
733 * Returns the amount of time spent started.
734 *
735 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700736 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 * @return
738 */
739 public abstract long getStartTime(long batteryUptime, int which);
740
741 /**
742 * Returns the total number of times startService() has been called.
743 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700744 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 */
746 public abstract int getStarts(int which);
747
748 /**
749 * Returns the total number times the service has been launched.
750 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700751 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 */
753 public abstract int getLaunches(int which);
754 }
755 }
756 }
757
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800758 public static final class LevelStepTracker {
759 public long mLastStepTime = -1;
760 public int mNumStepDurations;
761 public final long[] mStepDurations;
762
763 public LevelStepTracker(int maxLevelSteps) {
764 mStepDurations = new long[maxLevelSteps];
765 }
766
767 public LevelStepTracker(int numSteps, long[] steps) {
768 mNumStepDurations = numSteps;
769 mStepDurations = new long[numSteps];
770 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
771 }
772
773 public long getDurationAt(int index) {
774 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
775 }
776
777 public int getLevelAt(int index) {
778 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
779 >> STEP_LEVEL_LEVEL_SHIFT);
780 }
781
782 public int getInitModeAt(int index) {
783 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
784 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
785 }
786
787 public int getModModeAt(int index) {
788 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
789 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
790 }
791
792 private void appendHex(long val, int topOffset, StringBuilder out) {
793 boolean hasData = false;
794 while (topOffset >= 0) {
795 int digit = (int)( (val>>topOffset) & 0xf );
796 topOffset -= 4;
797 if (!hasData && digit == 0) {
798 continue;
799 }
800 hasData = true;
801 if (digit >= 0 && digit <= 9) {
802 out.append((char)('0' + digit));
803 } else {
804 out.append((char)('a' + digit - 10));
805 }
806 }
807 }
808
809 public void encodeEntryAt(int index, StringBuilder out) {
810 long item = mStepDurations[index];
811 long duration = item & STEP_LEVEL_TIME_MASK;
812 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
813 >> STEP_LEVEL_LEVEL_SHIFT);
814 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
815 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
816 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
817 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
818 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
819 case Display.STATE_OFF: out.append('f'); break;
820 case Display.STATE_ON: out.append('o'); break;
821 case Display.STATE_DOZE: out.append('d'); break;
822 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
823 }
824 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
825 out.append('p');
826 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700827 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
828 out.append('i');
829 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800830 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
831 case Display.STATE_OFF: out.append('F'); break;
832 case Display.STATE_ON: out.append('O'); break;
833 case Display.STATE_DOZE: out.append('D'); break;
834 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
835 }
836 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
837 out.append('P');
838 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700839 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
840 out.append('I');
841 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800842 out.append('-');
843 appendHex(level, 4, out);
844 out.append('-');
845 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
846 }
847
848 public void decodeEntryAt(int index, String value) {
849 final int N = value.length();
850 int i = 0;
851 char c;
852 long out = 0;
853 while (i < N && (c=value.charAt(i)) != '-') {
854 i++;
855 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800856 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800857 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800858 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800859 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800860 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800861 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800862 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
863 << STEP_LEVEL_INITIAL_MODE_SHIFT);
864 break;
865 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
866 << STEP_LEVEL_INITIAL_MODE_SHIFT);
867 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700868 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
869 << STEP_LEVEL_INITIAL_MODE_SHIFT);
870 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800871 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
872 break;
873 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
874 break;
875 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
876 break;
877 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
878 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
879 break;
880 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
881 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800882 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700883 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
884 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
885 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800886 }
887 }
888 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800889 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800890 while (i < N && (c=value.charAt(i)) != '-') {
891 i++;
892 level <<= 4;
893 if (c >= '0' && c <= '9') {
894 level += c - '0';
895 } else if (c >= 'a' && c <= 'f') {
896 level += c - 'a' + 10;
897 } else if (c >= 'A' && c <= 'F') {
898 level += c - 'A' + 10;
899 }
900 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800901 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800902 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
903 long duration = 0;
904 while (i < N && (c=value.charAt(i)) != '-') {
905 i++;
906 duration <<= 4;
907 if (c >= '0' && c <= '9') {
908 duration += c - '0';
909 } else if (c >= 'a' && c <= 'f') {
910 duration += c - 'a' + 10;
911 } else if (c >= 'A' && c <= 'F') {
912 duration += c - 'A' + 10;
913 }
914 }
915 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
916 }
917
918 public void init() {
919 mLastStepTime = -1;
920 mNumStepDurations = 0;
921 }
922
923 public void clearTime() {
924 mLastStepTime = -1;
925 }
926
927 public long computeTimePerLevel() {
928 final long[] steps = mStepDurations;
929 final int numSteps = mNumStepDurations;
930
931 // For now we'll do a simple average across all steps.
932 if (numSteps <= 0) {
933 return -1;
934 }
935 long total = 0;
936 for (int i=0; i<numSteps; i++) {
937 total += steps[i] & STEP_LEVEL_TIME_MASK;
938 }
939 return total / numSteps;
940 /*
941 long[] buckets = new long[numSteps];
942 int numBuckets = 0;
943 int numToAverage = 4;
944 int i = 0;
945 while (i < numSteps) {
946 long totalTime = 0;
947 int num = 0;
948 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
949 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
950 num++;
951 }
952 buckets[numBuckets] = totalTime / num;
953 numBuckets++;
954 numToAverage *= 2;
955 i += num;
956 }
957 if (numBuckets < 1) {
958 return -1;
959 }
960 long averageTime = buckets[numBuckets-1];
961 for (i=numBuckets-2; i>=0; i--) {
962 averageTime = (averageTime + buckets[i]) / 2;
963 }
964 return averageTime;
965 */
966 }
967
968 public long computeTimeEstimate(long modesOfInterest, long modeValues,
969 int[] outNumOfInterest) {
970 final long[] steps = mStepDurations;
971 final int count = mNumStepDurations;
972 if (count <= 0) {
973 return -1;
974 }
975 long total = 0;
976 int numOfInterest = 0;
977 for (int i=0; i<count; i++) {
978 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
979 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
980 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
981 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
982 // If the modes of interest didn't change during this step period...
983 if ((modMode&modesOfInterest) == 0) {
984 // And the mode values during this period match those we are measuring...
985 if ((initMode&modesOfInterest) == modeValues) {
986 // Then this can be used to estimate the total time!
987 numOfInterest++;
988 total += steps[i] & STEP_LEVEL_TIME_MASK;
989 }
990 }
991 }
992 if (numOfInterest <= 0) {
993 return -1;
994 }
995
996 if (outNumOfInterest != null) {
997 outNumOfInterest[0] = numOfInterest;
998 }
999
1000 // The estimated time is the average time we spend in each level, multipled
1001 // by 100 -- the total number of battery levels
1002 return (total / numOfInterest) * 100;
1003 }
1004
1005 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1006 int stepCount = mNumStepDurations;
1007 final long lastStepTime = mLastStepTime;
1008 if (lastStepTime >= 0 && numStepLevels > 0) {
1009 final long[] steps = mStepDurations;
1010 long duration = elapsedRealtime - lastStepTime;
1011 for (int i=0; i<numStepLevels; i++) {
1012 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1013 long thisDuration = duration / (numStepLevels-i);
1014 duration -= thisDuration;
1015 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1016 thisDuration = STEP_LEVEL_TIME_MASK;
1017 }
1018 steps[0] = thisDuration | modeBits;
1019 }
1020 stepCount += numStepLevels;
1021 if (stepCount > steps.length) {
1022 stepCount = steps.length;
1023 }
1024 }
1025 mNumStepDurations = stepCount;
1026 mLastStepTime = elapsedRealtime;
1027 }
1028
1029 public void readFromParcel(Parcel in) {
1030 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001031 if (N > mStepDurations.length) {
1032 throw new ParcelFormatException("more step durations than available: " + N);
1033 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001034 mNumStepDurations = N;
1035 for (int i=0; i<N; i++) {
1036 mStepDurations[i] = in.readLong();
1037 }
1038 }
1039
1040 public void writeToParcel(Parcel out) {
1041 final int N = mNumStepDurations;
1042 out.writeInt(N);
1043 for (int i=0; i<N; i++) {
1044 out.writeLong(mStepDurations[i]);
1045 }
1046 }
1047 }
1048
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001049 public static final class PackageChange {
1050 public String mPackageName;
1051 public boolean mUpdate;
1052 public int mVersionCode;
1053 }
1054
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001055 public static final class DailyItem {
1056 public long mStartTime;
1057 public long mEndTime;
1058 public LevelStepTracker mDischargeSteps;
1059 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001060 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001061 }
1062
1063 public abstract DailyItem getDailyItemLocked(int daysAgo);
1064
1065 public abstract long getCurrentDailyStartTime();
1066
1067 public abstract long getNextMinDailyDeadline();
1068
1069 public abstract long getNextMaxDailyDeadline();
1070
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001071 public final static class HistoryTag {
1072 public String string;
1073 public int uid;
1074
1075 public int poolIdx;
1076
1077 public void setTo(HistoryTag o) {
1078 string = o.string;
1079 uid = o.uid;
1080 poolIdx = o.poolIdx;
1081 }
1082
1083 public void setTo(String _string, int _uid) {
1084 string = _string;
1085 uid = _uid;
1086 poolIdx = -1;
1087 }
1088
1089 public void writeToParcel(Parcel dest, int flags) {
1090 dest.writeString(string);
1091 dest.writeInt(uid);
1092 }
1093
1094 public void readFromParcel(Parcel src) {
1095 string = src.readString();
1096 uid = src.readInt();
1097 poolIdx = -1;
1098 }
1099
1100 @Override
1101 public boolean equals(Object o) {
1102 if (this == o) return true;
1103 if (o == null || getClass() != o.getClass()) return false;
1104
1105 HistoryTag that = (HistoryTag) o;
1106
1107 if (uid != that.uid) return false;
1108 if (!string.equals(that.string)) return false;
1109
1110 return true;
1111 }
1112
1113 @Override
1114 public int hashCode() {
1115 int result = string.hashCode();
1116 result = 31 * result + uid;
1117 return result;
1118 }
1119 }
1120
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001121 /**
1122 * Optional detailed information that can go into a history step. This is typically
1123 * generated each time the battery level changes.
1124 */
1125 public final static class HistoryStepDetails {
1126 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1127 public int userTime;
1128 public int systemTime;
1129
1130 // Top three apps using CPU in the last step, with times in 1/100 second.
1131 public int appCpuUid1;
1132 public int appCpuUTime1;
1133 public int appCpuSTime1;
1134 public int appCpuUid2;
1135 public int appCpuUTime2;
1136 public int appCpuSTime2;
1137 public int appCpuUid3;
1138 public int appCpuUTime3;
1139 public int appCpuSTime3;
1140
1141 // Information from /proc/stat
1142 public int statUserTime;
1143 public int statSystemTime;
1144 public int statIOWaitTime;
1145 public int statIrqTime;
1146 public int statSoftIrqTime;
1147 public int statIdlTime;
1148
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001149 // Platform-level low power state stats
1150 public String statPlatformIdleState;
1151
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001152 public HistoryStepDetails() {
1153 clear();
1154 }
1155
1156 public void clear() {
1157 userTime = systemTime = 0;
1158 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1159 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1160 = appCpuUTime3 = appCpuSTime3 = 0;
1161 }
1162
1163 public void writeToParcel(Parcel out) {
1164 out.writeInt(userTime);
1165 out.writeInt(systemTime);
1166 out.writeInt(appCpuUid1);
1167 out.writeInt(appCpuUTime1);
1168 out.writeInt(appCpuSTime1);
1169 out.writeInt(appCpuUid2);
1170 out.writeInt(appCpuUTime2);
1171 out.writeInt(appCpuSTime2);
1172 out.writeInt(appCpuUid3);
1173 out.writeInt(appCpuUTime3);
1174 out.writeInt(appCpuSTime3);
1175 out.writeInt(statUserTime);
1176 out.writeInt(statSystemTime);
1177 out.writeInt(statIOWaitTime);
1178 out.writeInt(statIrqTime);
1179 out.writeInt(statSoftIrqTime);
1180 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001181 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001182 }
1183
1184 public void readFromParcel(Parcel in) {
1185 userTime = in.readInt();
1186 systemTime = in.readInt();
1187 appCpuUid1 = in.readInt();
1188 appCpuUTime1 = in.readInt();
1189 appCpuSTime1 = in.readInt();
1190 appCpuUid2 = in.readInt();
1191 appCpuUTime2 = in.readInt();
1192 appCpuSTime2 = in.readInt();
1193 appCpuUid3 = in.readInt();
1194 appCpuUTime3 = in.readInt();
1195 appCpuSTime3 = in.readInt();
1196 statUserTime = in.readInt();
1197 statSystemTime = in.readInt();
1198 statIOWaitTime = in.readInt();
1199 statIrqTime = in.readInt();
1200 statSoftIrqTime = in.readInt();
1201 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001202 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001203 }
1204 }
1205
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001206 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001207 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001208
1209 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001210 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001211
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001212 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001213 public static final byte CMD_NULL = -1;
1214 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001215 public static final byte CMD_CURRENT_TIME = 5;
1216 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001217 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001218 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001219
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001220 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001221
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001222 /**
1223 * Return whether the command code is a delta data update.
1224 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001225 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001226 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001227 }
1228
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001229 public byte batteryLevel;
1230 public byte batteryStatus;
1231 public byte batteryHealth;
1232 public byte batteryPlugType;
1233
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001234 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001235 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001236
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001237 // The charge of the battery in micro-Ampere-hours.
1238 public int batteryChargeUAh;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001239
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001240 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001241 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001242 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001243 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001244 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1245 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001246 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001247 public static final int STATE_PHONE_STATE_SHIFT = 6;
1248 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001249 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001250 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001251 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001252
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001253 // These states always appear directly in the first int token
1254 // of a delta change; they should be ones that change relatively
1255 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001256 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1257 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001258 public static final int STATE_GPS_ON_FLAG = 1<<29;
1259 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001260 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001261 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001262 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001263 // Do not use, this is used for coulomb delta count.
1264 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001265 // These are on the lower bits used for the command; if they change
1266 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001267 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001268 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001269 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001270 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1271 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1272 // empty slot
1273 // empty slot
1274 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001275
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001276 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001277 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1278
1279 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001280
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001281 public int states;
1282
Dianne Hackborn3251b902014-06-20 14:40:53 -07001283 // Constants from WIFI_SUPPL_STATE_*
1284 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1285 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1286 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1287 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1288 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1289 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1290
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001291 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001292 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1293 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1294 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001295 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001296 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1297 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1298 public static final int STATE2_CHARGING_FLAG = 1<<24;
1299 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1300 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1301 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001302 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001303
1304 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001305 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001306 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1307
1308 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001309
Dianne Hackborn40c87252014-03-19 16:55:40 -07001310 public int states2;
1311
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001312 // The wake lock that was acquired at this point.
1313 public HistoryTag wakelockTag;
1314
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001315 // Kernel wakeup reason at this point.
1316 public HistoryTag wakeReasonTag;
1317
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001318 // Non-null when there is more detailed information at this step.
1319 public HistoryStepDetails stepDetails;
1320
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001321 public static final int EVENT_FLAG_START = 0x8000;
1322 public static final int EVENT_FLAG_FINISH = 0x4000;
1323
1324 // No event in this item.
1325 public static final int EVENT_NONE = 0x0000;
1326 // Event is about a process that is running.
1327 public static final int EVENT_PROC = 0x0001;
1328 // Event is about an application package that is in the foreground.
1329 public static final int EVENT_FOREGROUND = 0x0002;
1330 // Event is about an application package that is at the top of the screen.
1331 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001332 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001333 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001334 // Events for all additional wake locks aquired/release within a wake block.
1335 // These are not generated by default.
1336 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001337 // Event is about an application executing a scheduled job.
1338 public static final int EVENT_JOB = 0x0006;
1339 // Events for users running.
1340 public static final int EVENT_USER_RUNNING = 0x0007;
1341 // Events for foreground user.
1342 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001343 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001344 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001345 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001346 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001347 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001348 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001349 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001350 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001351 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001352 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001353 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001354 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001355 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001356 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001357 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001358 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001359 // Event for a package being on the temporary whitelist.
1360 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001361 // Event for the screen waking up.
1362 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001363 // Event for the UID that woke up the application processor.
1364 // Used for wakeups coming from WiFi, modem, etc.
1365 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001366 // Event for reporting that a specific partial wake lock has been held for a long duration.
1367 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Adam Lesinski041d9172016-12-12 12:03:56 -08001368 // Event reporting the new estimated (learned) capacity of the battery in mAh.
1369 public static final int EVENT_ESTIMATED_BATTERY_CAP = 0x0015;
Amith Yamasani67768492015-06-09 12:23:58 -07001370
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001371 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001372 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001373 // Mask to extract out only the type part of the event.
1374 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001375
1376 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1377 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1378 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1379 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1380 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1381 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001382 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1383 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001384 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1385 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001386 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1387 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1388 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1389 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1390 public static final int EVENT_USER_FOREGROUND_START =
1391 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1392 public static final int EVENT_USER_FOREGROUND_FINISH =
1393 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001394 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1395 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001396 public static final int EVENT_TEMP_WHITELIST_START =
1397 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1398 public static final int EVENT_TEMP_WHITELIST_FINISH =
1399 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001400 public static final int EVENT_LONG_WAKE_LOCK_START =
1401 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1402 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1403 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001404
1405 // For CMD_EVENT.
1406 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001407 public HistoryTag eventTag;
1408
Dianne Hackborn9a755432014-05-15 17:05:22 -07001409 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001410 public long currentTime;
1411
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001412 // Meta-data when reading.
1413 public int numReadInts;
1414
1415 // Pre-allocated objects.
1416 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001417 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001418 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001419
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001420 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001421 }
1422
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001423 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001424 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001425 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001426 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001427 }
1428
1429 public int describeContents() {
1430 return 0;
1431 }
1432
1433 public void writeToParcel(Parcel dest, int flags) {
1434 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001435 int bat = (((int)cmd)&0xff)
1436 | ((((int)batteryLevel)<<8)&0xff00)
1437 | ((((int)batteryStatus)<<16)&0xf0000)
1438 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001439 | ((((int)batteryPlugType)<<24)&0xf000000)
1440 | (wakelockTag != null ? 0x10000000 : 0)
1441 | (wakeReasonTag != null ? 0x20000000 : 0)
1442 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001443 dest.writeInt(bat);
1444 bat = (((int)batteryTemperature)&0xffff)
1445 | ((((int)batteryVoltage)<<16)&0xffff0000);
1446 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001447 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001448 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001449 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001450 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001451 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001452 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001453 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001454 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001455 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001456 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001457 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001458 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001459 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001460 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001461 dest.writeLong(currentTime);
1462 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001463 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001464
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001465 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001466 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001467 int bat = src.readInt();
1468 cmd = (byte)(bat&0xff);
1469 batteryLevel = (byte)((bat>>8)&0xff);
1470 batteryStatus = (byte)((bat>>16)&0xf);
1471 batteryHealth = (byte)((bat>>20)&0xf);
1472 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001473 int bat2 = src.readInt();
1474 batteryTemperature = (short)(bat2&0xffff);
1475 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001476 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001477 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001478 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001479 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001480 wakelockTag = localWakelockTag;
1481 wakelockTag.readFromParcel(src);
1482 } else {
1483 wakelockTag = null;
1484 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001485 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001486 wakeReasonTag = localWakeReasonTag;
1487 wakeReasonTag.readFromParcel(src);
1488 } else {
1489 wakeReasonTag = null;
1490 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001491 if ((bat&0x40000000) != 0) {
1492 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001493 eventTag = localEventTag;
1494 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001495 } else {
1496 eventCode = EVENT_NONE;
1497 eventTag = null;
1498 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001499 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001500 currentTime = src.readLong();
1501 } else {
1502 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001503 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001504 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001505 }
1506
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001507 public void clear() {
1508 time = 0;
1509 cmd = CMD_NULL;
1510 batteryLevel = 0;
1511 batteryStatus = 0;
1512 batteryHealth = 0;
1513 batteryPlugType = 0;
1514 batteryTemperature = 0;
1515 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001516 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001517 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001518 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001519 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001520 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001521 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001522 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001523 }
1524
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001525 public void setTo(HistoryItem o) {
1526 time = o.time;
1527 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001528 setToCommon(o);
1529 }
1530
1531 public void setTo(long time, byte cmd, HistoryItem o) {
1532 this.time = time;
1533 this.cmd = cmd;
1534 setToCommon(o);
1535 }
1536
1537 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001538 batteryLevel = o.batteryLevel;
1539 batteryStatus = o.batteryStatus;
1540 batteryHealth = o.batteryHealth;
1541 batteryPlugType = o.batteryPlugType;
1542 batteryTemperature = o.batteryTemperature;
1543 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001544 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001545 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001546 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001547 if (o.wakelockTag != null) {
1548 wakelockTag = localWakelockTag;
1549 wakelockTag.setTo(o.wakelockTag);
1550 } else {
1551 wakelockTag = null;
1552 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001553 if (o.wakeReasonTag != null) {
1554 wakeReasonTag = localWakeReasonTag;
1555 wakeReasonTag.setTo(o.wakeReasonTag);
1556 } else {
1557 wakeReasonTag = null;
1558 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001559 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001560 if (o.eventTag != null) {
1561 eventTag = localEventTag;
1562 eventTag.setTo(o.eventTag);
1563 } else {
1564 eventTag = null;
1565 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001566 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001567 }
1568
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001569 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001570 return batteryLevel == o.batteryLevel
1571 && batteryStatus == o.batteryStatus
1572 && batteryHealth == o.batteryHealth
1573 && batteryPlugType == o.batteryPlugType
1574 && batteryTemperature == o.batteryTemperature
1575 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001576 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001577 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001578 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001579 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001580 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001581
1582 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001583 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001584 return false;
1585 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001586 if (wakelockTag != o.wakelockTag) {
1587 if (wakelockTag == null || o.wakelockTag == null) {
1588 return false;
1589 }
1590 if (!wakelockTag.equals(o.wakelockTag)) {
1591 return false;
1592 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001593 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001594 if (wakeReasonTag != o.wakeReasonTag) {
1595 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1596 return false;
1597 }
1598 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1599 return false;
1600 }
1601 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001602 if (eventTag != o.eventTag) {
1603 if (eventTag == null || o.eventTag == null) {
1604 return false;
1605 }
1606 if (!eventTag.equals(o.eventTag)) {
1607 return false;
1608 }
1609 }
1610 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001611 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001612 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001613
1614 public final static class HistoryEventTracker {
1615 private final HashMap<String, SparseIntArray>[] mActiveEvents
1616 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1617
1618 public boolean updateState(int code, String name, int uid, int poolIdx) {
1619 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1620 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1621 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1622 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001623 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001624 mActiveEvents[idx] = active;
1625 }
1626 SparseIntArray uids = active.get(name);
1627 if (uids == null) {
1628 uids = new SparseIntArray();
1629 active.put(name, uids);
1630 }
1631 if (uids.indexOfKey(uid) >= 0) {
1632 // Already set, nothing to do!
1633 return false;
1634 }
1635 uids.put(uid, poolIdx);
1636 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1637 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1638 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1639 if (active == null) {
1640 // not currently active, nothing to do.
1641 return false;
1642 }
1643 SparseIntArray uids = active.get(name);
1644 if (uids == null) {
1645 // not currently active, nothing to do.
1646 return false;
1647 }
1648 idx = uids.indexOfKey(uid);
1649 if (idx < 0) {
1650 // not currently active, nothing to do.
1651 return false;
1652 }
1653 uids.removeAt(idx);
1654 if (uids.size() <= 0) {
1655 active.remove(name);
1656 }
1657 }
1658 return true;
1659 }
1660
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001661 public void removeEvents(int code) {
1662 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1663 mActiveEvents[idx] = null;
1664 }
1665
Dianne Hackborn37de0982014-05-09 09:32:18 -07001666 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1667 return mActiveEvents[code];
1668 }
1669 }
1670
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001671 public static final class BitDescription {
1672 public final int mask;
1673 public final int shift;
1674 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001675 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001676 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001677 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001678
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001679 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001680 this.mask = mask;
1681 this.shift = -1;
1682 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001683 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001684 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001685 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001686 }
1687
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001688 public BitDescription(int mask, int shift, String name, String shortName,
1689 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001690 this.mask = mask;
1691 this.shift = shift;
1692 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001693 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001694 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001695 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001696 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001697 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001698
Dianne Hackbornfc064132014-06-02 12:42:12 -07001699 /**
1700 * Don't allow any more batching in to the current history event. This
1701 * is called when printing partial histories, so to ensure that the next
1702 * history event will go in to a new batch after what was printed in the
1703 * last partial history.
1704 */
1705 public abstract void commitCurrentHistoryBatchLocked();
1706
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001707 public abstract int getHistoryTotalSize();
1708
1709 public abstract int getHistoryUsedSize();
1710
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001711 public abstract boolean startIteratingHistoryLocked();
1712
Dianne Hackborn099bc622014-01-22 13:39:16 -08001713 public abstract int getHistoryStringPoolSize();
1714
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001715 public abstract int getHistoryStringPoolBytes();
1716
1717 public abstract String getHistoryTagPoolString(int index);
1718
1719 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001720
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001721 public abstract boolean getNextHistoryLocked(HistoryItem out);
1722
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001723 public abstract void finishIteratingHistoryLocked();
1724
1725 public abstract boolean startIteratingOldHistoryLocked();
1726
1727 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1728
1729 public abstract void finishIteratingOldHistoryLocked();
1730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001732 * Return the base time offset for the battery history.
1733 */
1734 public abstract long getHistoryBaseTime();
1735
1736 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 * Returns the number of times the device has been started.
1738 */
1739 public abstract int getStartCount();
1740
1741 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001742 * 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 -08001743 * running on battery.
1744 *
1745 * {@hide}
1746 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001747 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001749 /**
1750 * Returns the number of times the screen was turned on.
1751 *
1752 * {@hide}
1753 */
1754 public abstract int getScreenOnCount(int which);
1755
Jeff Browne95c3cd2014-05-02 16:59:26 -07001756 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1757
Dianne Hackborn617f8772009-03-31 15:04:46 -07001758 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1759 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1760 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1761 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1762 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1763
1764 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1765 "dark", "dim", "medium", "light", "bright"
1766 };
1767
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001768 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1769 "0", "1", "2", "3", "4"
1770 };
1771
Dianne Hackborn617f8772009-03-31 15:04:46 -07001772 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001773
Dianne Hackborn617f8772009-03-31 15:04:46 -07001774 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001775 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001776 * the given brightness
1777 *
1778 * {@hide}
1779 */
1780 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001781 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001784 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001785 * running on battery.
1786 *
1787 * {@hide}
1788 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001789 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001790
1791 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001792 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001793 *
1794 * {@hide}
1795 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001796 public abstract int getPowerSaveModeEnabledCount(int which);
1797
1798 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001799 * Constant for device idle mode: not active.
1800 */
1801 public static final int DEVICE_IDLE_MODE_OFF = 0;
1802
1803 /**
1804 * Constant for device idle mode: active in lightweight mode.
1805 */
1806 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1807
1808 /**
1809 * Constant for device idle mode: active in full mode.
1810 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001811 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001812
1813 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001814 * Returns the time in microseconds that device has been in idle mode while
1815 * running on battery.
1816 *
1817 * {@hide}
1818 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001819 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001820
1821 /**
1822 * Returns the number of times that the devie has gone in to idle mode.
1823 *
1824 * {@hide}
1825 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001826 public abstract int getDeviceIdleModeCount(int mode, int which);
1827
1828 /**
1829 * Return the longest duration we spent in a particular device idle mode (fully in the
1830 * mode, not in idle maintenance etc).
1831 */
1832 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001833
1834 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001835 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001836 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001837 * counts all of the time that we consider the device to be idle, whether or not
1838 * it is currently in the actual device idle mode.
1839 *
1840 * {@hide}
1841 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001842 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001843
1844 /**
1845 * Returns the number of times that the devie has started idling.
1846 *
1847 * {@hide}
1848 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001849 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001850
1851 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001852 * Returns the number of times that connectivity state changed.
1853 *
1854 * {@hide}
1855 */
1856 public abstract int getNumConnectivityChange(int which);
1857
1858 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001859 * 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 -08001860 * running on battery.
1861 *
1862 * {@hide}
1863 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001864 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001865
1866 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001867 * Returns the number of times a phone call was activated.
1868 *
1869 * {@hide}
1870 */
1871 public abstract int getPhoneOnCount(int which);
1872
1873 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001874 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001875 * the given signal strength.
1876 *
1877 * {@hide}
1878 */
1879 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001880 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001881
Dianne Hackborn617f8772009-03-31 15:04:46 -07001882 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001883 * Returns the time in microseconds that the phone has been trying to
1884 * acquire a signal.
1885 *
1886 * {@hide}
1887 */
1888 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001889 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001890
1891 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001892 * Returns the number of times the phone has entered the given signal strength.
1893 *
1894 * {@hide}
1895 */
1896 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1897
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001898 /**
1899 * Returns the time in microseconds that the mobile network has been active
1900 * (in a high power state).
1901 *
1902 * {@hide}
1903 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001904 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001905
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001906 /**
1907 * Returns the number of times that the mobile network has transitioned to the
1908 * active state.
1909 *
1910 * {@hide}
1911 */
1912 public abstract int getMobileRadioActiveCount(int which);
1913
1914 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001915 * Returns the time in microseconds that is the difference between the mobile radio
1916 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1917 * from the radio.
1918 *
1919 * {@hide}
1920 */
1921 public abstract long getMobileRadioActiveAdjustedTime(int which);
1922
1923 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001924 * Returns the time in microseconds that the mobile network has been active
1925 * (in a high power state) but not being able to blame on an app.
1926 *
1927 * {@hide}
1928 */
1929 public abstract long getMobileRadioActiveUnknownTime(int which);
1930
1931 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001932 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001933 *
1934 * {@hide}
1935 */
1936 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001937
Dianne Hackborn627bba72009-03-24 22:32:56 -07001938 public static final int DATA_CONNECTION_NONE = 0;
1939 public static final int DATA_CONNECTION_GPRS = 1;
1940 public static final int DATA_CONNECTION_EDGE = 2;
1941 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001942 public static final int DATA_CONNECTION_CDMA = 4;
1943 public static final int DATA_CONNECTION_EVDO_0 = 5;
1944 public static final int DATA_CONNECTION_EVDO_A = 6;
1945 public static final int DATA_CONNECTION_1xRTT = 7;
1946 public static final int DATA_CONNECTION_HSDPA = 8;
1947 public static final int DATA_CONNECTION_HSUPA = 9;
1948 public static final int DATA_CONNECTION_HSPA = 10;
1949 public static final int DATA_CONNECTION_IDEN = 11;
1950 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001951 public static final int DATA_CONNECTION_LTE = 13;
1952 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001953 public static final int DATA_CONNECTION_HSPAP = 15;
1954 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001955
Dianne Hackborn627bba72009-03-24 22:32:56 -07001956 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001957 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001958 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001959 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001960 };
1961
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001962 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001963
1964 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001965 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001966 * the given data connection.
1967 *
1968 * {@hide}
1969 */
1970 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001971 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001974 * Returns the number of times the phone has entered the given data
1975 * connection type.
1976 *
1977 * {@hide}
1978 */
1979 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001980
Dianne Hackborn3251b902014-06-20 14:40:53 -07001981 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1982 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1983 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1984 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1985 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1986 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1987 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1988 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1989 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1990 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1991 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1992 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1993 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1994
1995 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1996
1997 static final String[] WIFI_SUPPL_STATE_NAMES = {
1998 "invalid", "disconn", "disabled", "inactive", "scanning",
1999 "authenticating", "associating", "associated", "4-way-handshake",
2000 "group-handshake", "completed", "dormant", "uninit"
2001 };
2002
2003 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2004 "inv", "dsc", "dis", "inact", "scan",
2005 "auth", "ascing", "asced", "4-way",
2006 "group", "compl", "dorm", "uninit"
2007 };
2008
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002009 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2010 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002011 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002012 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2013 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002014 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002015 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2016 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2017 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002018 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002019 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002020 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002021 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002022 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2023 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002024 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2025 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2026 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2027 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2028 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2029 new String[] {"in", "out", "emergency", "off"},
2030 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002031 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2032 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2033 SignalStrength.SIGNAL_STRENGTH_NAMES,
2034 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002035 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2036 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2037 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002038 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002039
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002040 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2041 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002042 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002043 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002044 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002045 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002046 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002047 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2048 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2049 new String[] { "off", "light", "full", "???" },
2050 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002051 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2052 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2053 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002054 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2055 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2056 new String[] { "0", "1", "2", "3", "4" },
2057 new String[] { "0", "1", "2", "3", "4" }),
2058 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2059 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2060 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002061 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002062 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002063 };
2064
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002065 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002066 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002067 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2068 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002069 };
2070
2071 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002072 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002073 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002074 "Esw", "Ewa", "Elw", "Eec"
2075 };
2076
2077 @FunctionalInterface
2078 public interface IntToString {
2079 String applyAsString(int val);
2080 }
2081
2082 private static final IntToString sUidToString = UserHandle::formatUid;
2083 private static final IntToString sIntToString = Integer::toString;
2084
2085 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2086 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2087 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2088 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2089 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002090 };
2091
Dianne Hackborn617f8772009-03-31 15:04:46 -07002092 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002093 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002094 * running on battery.
2095 *
2096 * {@hide}
2097 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002098 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002099
2100 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002101 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002102 * been in the running state while the device was running on battery.
2103 *
2104 * {@hide}
2105 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002106 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002107
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002108 public static final int WIFI_STATE_OFF = 0;
2109 public static final int WIFI_STATE_OFF_SCANNING = 1;
2110 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2111 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2112 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2113 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2114 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2115 public static final int WIFI_STATE_SOFT_AP = 7;
2116
2117 static final String[] WIFI_STATE_NAMES = {
2118 "off", "scanning", "no_net", "disconn",
2119 "sta", "p2p", "sta_p2p", "soft_ap"
2120 };
2121
2122 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2123
2124 /**
2125 * Returns the time in microseconds that WiFi has been running in the given state.
2126 *
2127 * {@hide}
2128 */
2129 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002130 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002131
2132 /**
2133 * Returns the number of times that WiFi has entered the given state.
2134 *
2135 * {@hide}
2136 */
2137 public abstract int getWifiStateCount(int wifiState, int which);
2138
The Android Open Source Project10592532009-03-18 17:39:46 -07002139 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002140 * Returns the time in microseconds that the wifi supplicant has been
2141 * in a given state.
2142 *
2143 * {@hide}
2144 */
2145 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2146
2147 /**
2148 * Returns the number of times that the wifi supplicant has transitioned
2149 * to a given state.
2150 *
2151 * {@hide}
2152 */
2153 public abstract int getWifiSupplStateCount(int state, int which);
2154
2155 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2156
2157 /**
2158 * Returns the time in microseconds that WIFI has been running with
2159 * the given signal strength.
2160 *
2161 * {@hide}
2162 */
2163 public abstract long getWifiSignalStrengthTime(int strengthBin,
2164 long elapsedRealtimeUs, int which);
2165
2166 /**
2167 * Returns the number of times WIFI has entered the given signal strength.
2168 *
2169 * {@hide}
2170 */
2171 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2172
2173 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002174 * Returns the time in microseconds that the flashlight has been on while the device was
2175 * running on battery.
2176 *
2177 * {@hide}
2178 */
2179 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2180
2181 /**
2182 * Returns the number of times that the flashlight has been turned on while the device was
2183 * running on battery.
2184 *
2185 * {@hide}
2186 */
2187 public abstract long getFlashlightOnCount(int which);
2188
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002189 /**
2190 * Returns the time in microseconds that the camera has been on while the device was
2191 * running on battery.
2192 *
2193 * {@hide}
2194 */
2195 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2196
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002197 /**
2198 * Returns the time in microseconds that bluetooth scans were running while the device was
2199 * on battery.
2200 *
2201 * {@hide}
2202 */
2203 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002204
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002205 public static final int NETWORK_MOBILE_RX_DATA = 0;
2206 public static final int NETWORK_MOBILE_TX_DATA = 1;
2207 public static final int NETWORK_WIFI_RX_DATA = 2;
2208 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002209 public static final int NETWORK_BT_RX_DATA = 4;
2210 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002211 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2212 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2213 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2214 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2215 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002216
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002217 public abstract long getNetworkActivityBytes(int type, int which);
2218 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002219
Adam Lesinskie08af192015-03-25 16:42:59 -07002220 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002221 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002222 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002223 * actual power data.
2224 */
2225 public abstract boolean hasWifiActivityReporting();
2226
2227 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002228 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2229 * in various radio controller states, such as transmit, receive, and idle.
2230 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002231 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002232 public abstract ControllerActivityCounter getWifiControllerActivity();
2233
2234 /**
2235 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2236 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2237 * actual power data.
2238 */
2239 public abstract boolean hasBluetoothActivityReporting();
2240
2241 /**
2242 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2243 * in various radio controller states, such as transmit, receive, and idle.
2244 * @return non-null {@link ControllerActivityCounter}
2245 */
2246 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2247
2248 /**
2249 * Returns true if the BatteryStats object has detailed modem power reports.
2250 * When true, calling {@link #getModemControllerActivity()} will yield the
2251 * actual power data.
2252 */
2253 public abstract boolean hasModemActivityReporting();
2254
2255 /**
2256 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2257 * in various radio controller states, such as transmit, receive, and idle.
2258 * @return non-null {@link ControllerActivityCounter}
2259 */
2260 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002261
The Android Open Source Project10592532009-03-18 17:39:46 -07002262 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002263 * Return the wall clock time when battery stats data collection started.
2264 */
2265 public abstract long getStartClockTime();
2266
2267 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002268 * Return platform version tag that we were running in when the battery stats started.
2269 */
2270 public abstract String getStartPlatformVersion();
2271
2272 /**
2273 * Return platform version tag that we were running in when the battery stats ended.
2274 */
2275 public abstract String getEndPlatformVersion();
2276
2277 /**
2278 * Return the internal version code of the parcelled format.
2279 */
2280 public abstract int getParcelVersion();
2281
2282 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 * Return whether we are currently running on battery.
2284 */
2285 public abstract boolean getIsOnBattery();
2286
2287 /**
2288 * Returns a SparseArray containing the statistics for each uid.
2289 */
2290 public abstract SparseArray<? extends Uid> getUidStats();
2291
2292 /**
2293 * Returns the current battery uptime in microseconds.
2294 *
2295 * @param curTime the amount of elapsed realtime in microseconds.
2296 */
2297 public abstract long getBatteryUptime(long curTime);
2298
2299 /**
2300 * Returns the current battery realtime in microseconds.
2301 *
2302 * @param curTime the amount of elapsed realtime in microseconds.
2303 */
2304 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002305
2306 /**
Evan Millar633a1742009-04-02 16:36:33 -07002307 * Returns the battery percentage level at the last time the device was unplugged from power, or
2308 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002309 */
Evan Millar633a1742009-04-02 16:36:33 -07002310 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002311
2312 /**
Evan Millar633a1742009-04-02 16:36:33 -07002313 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2314 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002315 */
Evan Millar633a1742009-04-02 16:36:33 -07002316 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317
2318 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002319 * Get the amount the battery has discharged since the stats were
2320 * last reset after charging, as a lower-end approximation.
2321 */
2322 public abstract int getLowDischargeAmountSinceCharge();
2323
2324 /**
2325 * Get the amount the battery has discharged since the stats were
2326 * last reset after charging, as an upper-end approximation.
2327 */
2328 public abstract int getHighDischargeAmountSinceCharge();
2329
2330 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002331 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2332 */
2333 public abstract int getDischargeAmount(int which);
2334
2335 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002336 * Get the amount the battery has discharged while the screen was on,
2337 * since the last time power was unplugged.
2338 */
2339 public abstract int getDischargeAmountScreenOn();
2340
2341 /**
2342 * Get the amount the battery has discharged while the screen was on,
2343 * since the last time the device was charged.
2344 */
2345 public abstract int getDischargeAmountScreenOnSinceCharge();
2346
2347 /**
2348 * Get the amount the battery has discharged while the screen was off,
2349 * since the last time power was unplugged.
2350 */
2351 public abstract int getDischargeAmountScreenOff();
2352
2353 /**
2354 * Get the amount the battery has discharged while the screen was off,
2355 * since the last time the device was charged.
2356 */
2357 public abstract int getDischargeAmountScreenOffSinceCharge();
2358
2359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 * Returns the total, last, or current battery uptime in microseconds.
2361 *
2362 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002363 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 */
2365 public abstract long computeBatteryUptime(long curTime, int which);
2366
2367 /**
2368 * Returns the total, last, or current battery realtime in microseconds.
2369 *
2370 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002371 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 */
2373 public abstract long computeBatteryRealtime(long curTime, int which);
2374
2375 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002376 * Returns the total, last, or current battery screen off uptime in microseconds.
2377 *
2378 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002379 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002380 */
2381 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2382
2383 /**
2384 * Returns the total, last, or current battery screen off realtime in microseconds.
2385 *
2386 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002387 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002388 */
2389 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2390
2391 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 * Returns the total, last, or current uptime in microseconds.
2393 *
2394 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002395 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 */
2397 public abstract long computeUptime(long curTime, int which);
2398
2399 /**
2400 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002401 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002403 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 */
2405 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002406
2407 /**
2408 * Compute an approximation for how much run time (in microseconds) is remaining on
2409 * the battery. Returns -1 if no time can be computed: either there is not
2410 * enough current data to make a decision, or the battery is currently
2411 * charging.
2412 *
2413 * @param curTime The current elepsed realtime in microseconds.
2414 */
2415 public abstract long computeBatteryTimeRemaining(long curTime);
2416
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002417 // The part of a step duration that is the actual time.
2418 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2419
2420 // Bits in a step duration that are the new battery level we are at.
2421 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002422 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002423
2424 // Bits in a step duration that are the initial mode we were in at that step.
2425 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002426 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002427
2428 // Bits in a step duration that indicate which modes changed during that step.
2429 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002430 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002431
2432 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2433 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2434
Santos Cordone94f0502017-02-24 12:31:20 -08002435 // The largest value for screen state that is tracked in battery states. Any values above
2436 // this should be mapped back to one of the tracked values before being tracked here.
2437 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2438
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002439 // Step duration mode: power save is on.
2440 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2441
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002442 // Step duration mode: device is currently in idle mode.
2443 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2444
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002445 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2446 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002447 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2448 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002449 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,
2453 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002454 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2455 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002456 };
2457 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2458 (Display.STATE_OFF-1),
2459 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002460 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002461 (Display.STATE_ON-1),
2462 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2463 (Display.STATE_DOZE-1),
2464 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2465 (Display.STATE_DOZE_SUSPEND-1),
2466 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002467 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002468 };
2469 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2470 "screen off",
2471 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002472 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002473 "screen on",
2474 "screen on power save",
2475 "screen doze",
2476 "screen doze power save",
2477 "screen doze-suspend",
2478 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002479 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002480 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002481
2482 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002483 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2484 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2485 * a coulomb counter.
2486 */
2487 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2488
2489 /**
2490 * Return the counter keeping track of the amount of battery discharge measured in
2491 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2492 * a coulomb counter.
2493 */
2494 public abstract LongCounter getDischargeCoulombCounter();
2495
2496 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002497 * Returns the estimated real battery capacity, which may be less than the capacity
2498 * declared by the PowerProfile.
2499 * @return The estimated battery capacity in mAh.
2500 */
2501 public abstract int getEstimatedBatteryCapacity();
2502
2503 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002504 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002505 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002506 public abstract LevelStepTracker getDischargeLevelStepTracker();
2507
2508 /**
2509 * Return the array of daily discharge step durations.
2510 */
2511 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002512
2513 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002514 * Compute an approximation for how much time (in microseconds) remains until the battery
2515 * is fully charged. Returns -1 if no time can be computed: either there is not
2516 * enough current data to make a decision, or the battery is currently
2517 * discharging.
2518 *
2519 * @param curTime The current elepsed realtime in microseconds.
2520 */
2521 public abstract long computeChargeTimeRemaining(long curTime);
2522
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002523 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002524 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002525 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002526 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002527
2528 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002529 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002530 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002531 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002532
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002533 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2534
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002535 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002536
Evan Millarc64edde2009-04-18 12:26:32 -07002537 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538
James Carr2dd7e5e2016-07-20 18:48:39 -07002539 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2540
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002541 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2542
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002543 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 long days = seconds / (60 * 60 * 24);
2545 if (days != 0) {
2546 out.append(days);
2547 out.append("d ");
2548 }
2549 long used = days * 60 * 60 * 24;
2550
2551 long hours = (seconds - used) / (60 * 60);
2552 if (hours != 0 || used != 0) {
2553 out.append(hours);
2554 out.append("h ");
2555 }
2556 used += hours * 60 * 60;
2557
2558 long mins = (seconds-used) / 60;
2559 if (mins != 0 || used != 0) {
2560 out.append(mins);
2561 out.append("m ");
2562 }
2563 used += mins * 60;
2564
2565 if (seconds != 0 || used != 0) {
2566 out.append(seconds-used);
2567 out.append("s ");
2568 }
2569 }
2570
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002571 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002573 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 sb.append(time - (sec * 1000));
2575 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 }
2577
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002578 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002579 long sec = time / 1000;
2580 formatTimeRaw(sb, sec);
2581 sb.append(time - (sec * 1000));
2582 sb.append("ms");
2583 }
2584
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002585 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002587 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 }
2589 float perc = ((float)num) / ((float)den) * 100;
2590 mFormatBuilder.setLength(0);
2591 mFormatter.format("%.1f%%", perc);
2592 return mFormatBuilder.toString();
2593 }
2594
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002595 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002596 mFormatBuilder.setLength(0);
2597
2598 if (bytes < BYTES_PER_KB) {
2599 return bytes + "B";
2600 } else if (bytes < BYTES_PER_MB) {
2601 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2602 return mFormatBuilder.toString();
2603 } else if (bytes < BYTES_PER_GB){
2604 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2605 return mFormatBuilder.toString();
2606 } else {
2607 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2608 return mFormatBuilder.toString();
2609 }
2610 }
2611
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002612 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002613 if (timer != null) {
2614 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002615 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002616 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2617 return totalTimeMillis;
2618 }
2619 return 0;
2620 }
2621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 /**
2623 *
2624 * @param sb a StringBuilder object.
2625 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002626 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002628 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 * @param linePrefix a String to be prepended to each line of output.
2630 * @return the line prefix
2631 */
2632 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002633 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002636 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002637
Evan Millarc64edde2009-04-18 12:26:32 -07002638 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 if (totalTimeMillis != 0) {
2640 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002641 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002642 if (name != null) {
2643 sb.append(name);
2644 sb.append(' ');
2645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 sb.append('(');
2647 sb.append(count);
2648 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002649 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2650 if (maxDurationMs >= 0) {
2651 sb.append(" max=");
2652 sb.append(maxDurationMs);
2653 }
2654 if (timer.isRunningLocked()) {
2655 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2656 if (currentMs >= 0) {
2657 sb.append(" (running for ");
2658 sb.append(currentMs);
2659 sb.append("ms)");
2660 } else {
2661 sb.append(" (running)");
2662 }
2663 }
2664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 return ", ";
2666 }
2667 }
2668 return linePrefix;
2669 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002670
2671 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002672 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002673 *
2674 * @param pw a PrintWriter object to print to.
2675 * @param sb a StringBuilder object.
2676 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002677 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002678 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2679 * @param prefix a String to be prepended to each line of output.
2680 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002681 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002682 */
2683 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002684 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002685 if (timer != null) {
2686 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002687 final long totalTimeMs = (timer.getTotalTimeLocked(
2688 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002689 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002690 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002691 sb.setLength(0);
2692 sb.append(prefix);
2693 sb.append(" ");
2694 sb.append(type);
2695 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002696 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002697 sb.append("realtime (");
2698 sb.append(count);
2699 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002700 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2701 if (maxDurationMs >= 0) {
2702 sb.append(" max=");
2703 sb.append(maxDurationMs);
2704 }
2705 if (timer.isRunningLocked()) {
2706 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2707 if (currentMs >= 0) {
2708 sb.append(" (running for ");
2709 sb.append(currentMs);
2710 sb.append("ms)");
2711 } else {
2712 sb.append(" (running)");
2713 }
2714 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002715 pw.println(sb.toString());
2716 return true;
2717 }
2718 }
2719 return false;
2720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721
2722 /**
2723 * Checkin version of wakelock printer. Prints simple comma-separated list.
2724 *
2725 * @param sb a StringBuilder object.
2726 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002727 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002729 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 * @param linePrefix a String to be prepended to each line of output.
2731 * @return the line prefix
2732 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002733 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2734 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 long totalTimeMicros = 0;
2736 int count = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002737 long max = -1;
2738 long current = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002740 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002741 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002742 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2743 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 }
2745 sb.append(linePrefix);
2746 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2747 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002748 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002750 sb.append(',');
2751 sb.append(current);
2752 sb.append(',');
2753 sb.append(max);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 return ",";
2755 }
2756
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002757 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2758 String type) {
2759 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2760 pw.print(',');
2761 pw.print(uid);
2762 pw.print(',');
2763 pw.print(category);
2764 pw.print(',');
2765 pw.print(type);
2766 }
2767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 /**
2769 * Dump a comma-separated line of values for terse checkin mode.
2770 *
2771 * @param pw the PageWriter to dump log to
2772 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2773 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2774 * @param args type-dependent data arguments
2775 */
2776 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2777 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002778 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002779 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002780 pw.print(',');
2781 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002783 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002785
2786 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002787 * Dump a given timer stat for terse checkin mode.
2788 *
2789 * @param pw the PageWriter to dump log to
2790 * @param uid the UID to log
2791 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2792 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2793 * @param timer a {@link Timer} to dump stats for
2794 * @param rawRealtime the current elapsed realtime of the system in microseconds
2795 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2796 */
2797 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2798 Timer timer, long rawRealtime, int which) {
2799 if (timer != null) {
2800 // Convert from microseconds to milliseconds with rounding
2801 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2802 / 1000;
2803 final int count = timer.getCountLocked(which);
2804 if (totalTime != 0) {
2805 dumpLine(pw, uid, category, type, totalTime, count);
2806 }
2807 }
2808 }
2809
2810 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002811 * Checks if the ControllerActivityCounter has any data worth dumping.
2812 */
2813 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2814 if (counter == null) {
2815 return false;
2816 }
2817
2818 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2819 || counter.getRxTimeCounter().getCountLocked(which) != 0
2820 || counter.getPowerCounter().getCountLocked(which) != 0) {
2821 return true;
2822 }
2823
2824 for (LongCounter c : counter.getTxTimeCounters()) {
2825 if (c.getCountLocked(which) != 0) {
2826 return true;
2827 }
2828 }
2829 return false;
2830 }
2831
2832 /**
2833 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2834 * The order of the arguments in the final check in line is:
2835 *
2836 * idle, rx, power, tx...
2837 *
2838 * where tx... is one or more transmit level times.
2839 */
2840 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2841 String type,
2842 ControllerActivityCounter counter,
2843 int which) {
2844 if (!controllerActivityHasData(counter, which)) {
2845 return;
2846 }
2847
2848 dumpLineHeader(pw, uid, category, type);
2849 pw.print(",");
2850 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2851 pw.print(",");
2852 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2853 pw.print(",");
2854 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2855 for (LongCounter c : counter.getTxTimeCounters()) {
2856 pw.print(",");
2857 pw.print(c.getCountLocked(which));
2858 }
2859 pw.println();
2860 }
2861
2862 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2863 String prefix, String controllerName,
2864 ControllerActivityCounter counter,
2865 int which) {
2866 if (controllerActivityHasData(counter, which)) {
2867 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2868 }
2869 }
2870
2871 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2872 String controllerName,
2873 ControllerActivityCounter counter, int which) {
2874 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2875 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2876 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2877 long totalTxTimeMs = 0;
2878 for (LongCounter txState : counter.getTxTimeCounters()) {
2879 totalTxTimeMs += txState.getCountLocked(which);
2880 }
2881
2882 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2883
2884 sb.setLength(0);
2885 sb.append(prefix);
2886 sb.append(" ");
2887 sb.append(controllerName);
2888 sb.append(" Idle time: ");
2889 formatTimeMs(sb, idleTimeMs);
2890 sb.append("(");
2891 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2892 sb.append(")");
2893 pw.println(sb.toString());
2894
2895 sb.setLength(0);
2896 sb.append(prefix);
2897 sb.append(" ");
2898 sb.append(controllerName);
2899 sb.append(" Rx time: ");
2900 formatTimeMs(sb, rxTimeMs);
2901 sb.append("(");
2902 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2903 sb.append(")");
2904 pw.println(sb.toString());
2905
2906 sb.setLength(0);
2907 sb.append(prefix);
2908 sb.append(" ");
2909 sb.append(controllerName);
2910 sb.append(" Tx time: ");
2911 formatTimeMs(sb, totalTxTimeMs);
2912 sb.append("(");
2913 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2914 sb.append(")");
2915 pw.println(sb.toString());
2916
2917 final int numTxLvls = counter.getTxTimeCounters().length;
2918 if (numTxLvls > 1) {
2919 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2920 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2921 sb.setLength(0);
2922 sb.append(prefix);
2923 sb.append(" [");
2924 sb.append(lvl);
2925 sb.append("] ");
2926 formatTimeMs(sb, txLvlTimeMs);
2927 sb.append("(");
2928 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2929 sb.append(")");
2930 pw.println(sb.toString());
2931 }
2932 }
2933
2934 sb.setLength(0);
2935 sb.append(prefix);
2936 sb.append(" ");
2937 sb.append(controllerName);
2938 sb.append(" Power drain: ").append(
2939 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2940 sb.append("mAh");
2941 pw.println(sb.toString());
2942 }
2943
2944 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002945 * Temporary for settings.
2946 */
2947 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2948 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2949 }
2950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 /**
2952 * Checkin server version of dump to produce more compact, computer-readable log.
2953 *
2954 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002956 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2957 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2959 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2960 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2962 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002963 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2964 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2965 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 final long totalRealtime = computeRealtime(rawRealtime, which);
2967 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002968 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002969 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002970 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002971 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2972 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002973 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002974 rawRealtime, which);
2975 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2976 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002977 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002978 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002979 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002980 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002981 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
2982 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
2983 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002984
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002985 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002987 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002988 final int NU = uidStats.size();
2989
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002990 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 // Dump "battery" stat
2993 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002994 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002995 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002996 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002997 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002998 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
2999 getEstimatedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07003000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003002 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003003 long fullWakeLockTimeTotal = 0;
3004 long partialWakeLockTimeTotal = 0;
3005
3006 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003007 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003008
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003009 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3010 = u.getWakelockStats();
3011 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3012 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003013
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003014 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3015 if (fullWakeTimer != null) {
3016 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3017 which);
3018 }
3019
3020 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3021 if (partialWakeTimer != null) {
3022 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3023 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003024 }
3025 }
3026 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003027
3028 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003029 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3030 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3031 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3032 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3033 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3034 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3035 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3036 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003037 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3038 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003039 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3040 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003041 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3042 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003043
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003044 // Dump Modem controller stats
3045 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3046 getModemControllerActivity(), which);
3047
Adam Lesinskie283d332015-04-16 12:29:25 -07003048 // Dump Wifi controller stats
3049 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3050 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003051 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003052 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003053
3054 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3055 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003056
3057 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003058 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3059 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 // Dump misc stats
3062 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003063 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003064 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003065 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003066 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003067 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003068 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3069 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003070 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003071 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3072 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3073 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3074 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003075 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003076
3077 // Dump screen brightness stats
3078 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3079 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003080 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003081 }
3082 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07003083
Dianne Hackborn627bba72009-03-24 22:32:56 -07003084 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003085 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3086 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003087 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003088 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003089 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003090 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003091 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003092 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003093 args[i] = getPhoneSignalStrengthCount(i, which);
3094 }
3095 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003096
Dianne Hackborn627bba72009-03-24 22:32:56 -07003097 // Dump network type stats
3098 args = new Object[NUM_DATA_CONNECTION_TYPES];
3099 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003100 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003101 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003102 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3103 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3104 args[i] = getPhoneDataConnectionCount(i, which);
3105 }
3106 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003107
3108 // Dump wifi state stats
3109 args = new Object[NUM_WIFI_STATES];
3110 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003111 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003112 }
3113 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3114 for (int i=0; i<NUM_WIFI_STATES; i++) {
3115 args[i] = getWifiStateCount(i, which);
3116 }
3117 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3118
Dianne Hackborn3251b902014-06-20 14:40:53 -07003119 // Dump wifi suppl state stats
3120 args = new Object[NUM_WIFI_SUPPL_STATES];
3121 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3122 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3123 }
3124 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3125 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3126 args[i] = getWifiSupplStateCount(i, which);
3127 }
3128 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3129
3130 // Dump wifi signal strength stats
3131 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3132 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3133 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3134 }
3135 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3136 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3137 args[i] = getWifiSignalStrengthCount(i, which);
3138 }
3139 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3140
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003141 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003142 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003143 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003146 if (which == STATS_SINCE_UNPLUGGED) {
3147 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3148 getDischargeStartLevel()-getDischargeCurrentLevel(),
3149 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003150 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3151 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003152 } else {
3153 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3154 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003155 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003156 getDischargeAmountScreenOffSinceCharge(),
3157 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003158 }
3159
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003160 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003161 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003162 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003163 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003164 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003165 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003166 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3167 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003168 }
Evan Millarc64edde2009-04-18 12:26:32 -07003169 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003170 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003171 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003172 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3173 // Not doing the regular wake lock formatting to remain compatible
3174 // with the old checkin format.
3175 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3176 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003177 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003178 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003179 }
3180 }
Evan Millarc64edde2009-04-18 12:26:32 -07003181 }
3182
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003183 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003184 helper.create(this);
3185 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003186 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003187 if (sippers != null && sippers.size() > 0) {
3188 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3189 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003190 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003191 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3192 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3193 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003194 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003195 int uid = 0;
3196 String label;
3197 switch (bs.drainType) {
3198 case IDLE:
3199 label="idle";
3200 break;
3201 case CELL:
3202 label="cell";
3203 break;
3204 case PHONE:
3205 label="phone";
3206 break;
3207 case WIFI:
3208 label="wifi";
3209 break;
3210 case BLUETOOTH:
3211 label="blue";
3212 break;
3213 case SCREEN:
3214 label="scrn";
3215 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003216 case FLASHLIGHT:
3217 label="flashlight";
3218 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003219 case APP:
3220 uid = bs.uidObj.getUid();
3221 label = "uid";
3222 break;
3223 case USER:
3224 uid = UserHandle.getUid(bs.userId, 0);
3225 label = "user";
3226 break;
3227 case UNACCOUNTED:
3228 label = "unacc";
3229 break;
3230 case OVERCOUNTED:
3231 label = "over";
3232 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003233 case CAMERA:
3234 label = "camera";
3235 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003236 default:
3237 label = "???";
3238 }
3239 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003240 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003241 }
3242 }
3243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 for (int iu = 0; iu < NU; iu++) {
3245 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003246 if (reqUid >= 0 && uid != reqUid) {
3247 continue;
3248 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003249 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003252 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3253 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3254 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3255 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3256 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3257 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3258 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3259 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003260 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003261 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3262 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003263 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003264 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3265 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003266 // Background data transfers
3267 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3268 which);
3269 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3270 which);
3271 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3272 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3273 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3274 which);
3275 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3276 which);
3277 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3278 which);
3279 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3280 which);
3281
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003282 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3283 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003284 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003285 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3286 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3287 || wifiBytesBgTx > 0
3288 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3289 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003290 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3291 wifiBytesRx, wifiBytesTx,
3292 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003293 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003294 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003295 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3296 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3297 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3298 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003299 }
3300
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003301 // Dump modem controller data, per UID.
3302 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3303 u.getModemControllerActivity(), which);
3304
3305 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003306 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3307 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3308 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003309 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3310 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003311 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3312 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3313 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003314 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003315 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003316 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3317 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003318 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3319 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003320 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003321 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003324 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3325 u.getWifiControllerActivity(), which);
3326
Bookatz867c0d72017-03-07 18:23:42 -08003327 final Timer bleTimer = u.getBluetoothScanTimer();
3328 if (bleTimer != null) {
3329 // Convert from microseconds to milliseconds with rounding
3330 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3331 / 1000;
3332 if (totalTime != 0) {
3333 final int count = bleTimer.getCountLocked(which);
3334 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3335 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
3336 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3337 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3338 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3339 final long actualTimeBg = bleTimerBg != null ?
3340 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3341 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
3342 countBg, actualTime, actualTimeBg);
3343 }
3344 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003345
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003346 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3347 u.getBluetoothControllerActivity(), which);
3348
Dianne Hackborn617f8772009-03-31 15:04:46 -07003349 if (u.hasUserActivity()) {
3350 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3351 boolean hasData = false;
3352 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3353 int val = u.getUserActivityCount(i, which);
3354 args[i] = val;
3355 if (val != 0) hasData = true;
3356 }
3357 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003358 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003359 }
3360 }
3361
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003362 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3363 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3364 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3365 String linePrefix = "";
3366 sb.setLength(0);
3367 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3368 rawRealtime, "f", which, linePrefix);
3369 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3370 rawRealtime, "p", which, linePrefix);
3371 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3372 rawRealtime, "w", which, linePrefix);
3373
3374 // Only log if we had at lease one wakelock...
3375 if (sb.length() > 0) {
3376 String name = wakelocks.keyAt(iw);
3377 if (name.indexOf(',') >= 0) {
3378 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003380 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 }
3382 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003383
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003384 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3385 for (int isy=syncs.size()-1; isy>=0; isy--) {
3386 final Timer timer = syncs.valueAt(isy);
3387 // Convert from microseconds to milliseconds with rounding
3388 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3389 final int count = timer.getCountLocked(which);
3390 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003391 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
3392 totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003393 }
3394 }
3395
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003396 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3397 for (int ij=jobs.size()-1; ij>=0; ij--) {
3398 final Timer timer = jobs.valueAt(ij);
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);
Bookatzaa4594a2017-03-24 12:39:56 -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, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003408 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003409 }
3410 }
3411
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003412 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3413 rawRealtime, which);
3414 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3415 rawRealtime, which);
3416 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3417 rawRealtime, which);
3418 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3419 rawRealtime, which);
3420
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003421 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3422 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003423 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003424 final Uid.Sensor se = sensors.valueAt(ise);
3425 final int sensorNumber = sensors.keyAt(ise);
3426 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003427 if (timer != null) {
3428 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003429 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3430 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003431 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003432 final int count = timer.getCountLocked(which);
3433 final Timer bgTimer = se.getSensorBackgroundTime();
3434 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
3435 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3436 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3437 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3438 final long bgActualTime = bgTimer != null ?
3439 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3440 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3441 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 }
3444 }
3445
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003446 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3447 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003448
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003449 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3450 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003451
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003452 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003453 long totalStateTime = 0;
3454 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003455 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3456 totalStateTime += time;
3457 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003458 }
3459 if (totalStateTime > 0) {
3460 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3461 }
3462
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003463 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3464 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003465 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003466 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003467 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003468 }
3469
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003470 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3471 = u.getProcessStats();
3472 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3473 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003474
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003475 final long userMillis = ps.getUserTime(which);
3476 final long systemMillis = ps.getSystemTime(which);
3477 final long foregroundMillis = ps.getForegroundTime(which);
3478 final int starts = ps.getStarts(which);
3479 final int numCrashes = ps.getNumCrashes(which);
3480 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003481
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003482 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3483 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003484 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3485 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 }
3487 }
3488
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003489 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3490 = u.getPackageStats();
3491 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3492 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3493 int wakeups = 0;
3494 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3495 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003496 int count = alarms.valueAt(iwa).getCountLocked(which);
3497 wakeups += count;
3498 String name = alarms.keyAt(iwa).replace(',', '_');
3499 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003500 }
3501 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3502 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3503 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3504 final long startTime = ss.getStartTime(batteryUptime, which);
3505 final int starts = ss.getStarts(which);
3506 final int launches = ss.getLaunches(which);
3507 if (startTime != 0 || starts != 0 || launches != 0) {
3508 dumpLine(pw, uid, category, APK_DATA,
3509 wakeups, // wakeup alarms
3510 packageStats.keyAt(ipkg), // Apk
3511 serviceStats.keyAt(isvc), // service
3512 startTime / 1000, // time spent started, in ms
3513 starts,
3514 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 }
3516 }
3517 }
3518 }
3519 }
3520
Dianne Hackborn81038902012-11-26 17:04:09 -08003521 static final class TimerEntry {
3522 final String mName;
3523 final int mId;
3524 final BatteryStats.Timer mTimer;
3525 final long mTime;
3526 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3527 mName = name;
3528 mId = id;
3529 mTimer = timer;
3530 mTime = time;
3531 }
3532 }
3533
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003534 private void printmAh(PrintWriter printer, double power) {
3535 printer.print(BatteryStatsHelper.makemAh(power));
3536 }
3537
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003538 private void printmAh(StringBuilder sb, double power) {
3539 sb.append(BatteryStatsHelper.makemAh(power));
3540 }
3541
Dianne Hackbornd953c532014-08-16 18:17:38 -07003542 /**
3543 * Temporary for settings.
3544 */
3545 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3546 int reqUid) {
3547 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3548 }
3549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003551 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003552 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3554 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3555 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556
3557 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3558 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3559 final long totalRealtime = computeRealtime(rawRealtime, which);
3560 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003561 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3562 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3563 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003564 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3565 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003566
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003567 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003568
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003569 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003570 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003572 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3573 if (estimatedBatteryCapacity > 0) {
3574 sb.setLength(0);
3575 sb.append(prefix);
3576 sb.append(" Estimated battery capacity: ");
3577 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3578 sb.append(" mAh");
3579 pw.println(sb.toString());
3580 }
3581
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003582 sb.setLength(0);
3583 sb.append(prefix);
3584 sb.append(" Time on battery: ");
3585 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3586 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3587 sb.append(") realtime, ");
3588 formatTimeMs(sb, whichBatteryUptime / 1000);
3589 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3590 sb.append(") uptime");
3591 pw.println(sb.toString());
3592 sb.setLength(0);
3593 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003594 sb.append(" Time on battery screen off: ");
3595 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3596 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3597 sb.append(") realtime, ");
3598 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3599 sb.append("(");
3600 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3601 sb.append(") uptime");
3602 pw.println(sb.toString());
3603 sb.setLength(0);
3604 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003605 sb.append(" Total run time: ");
3606 formatTimeMs(sb, totalRealtime / 1000);
3607 sb.append("realtime, ");
3608 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003609 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003610 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003611 if (batteryTimeRemaining >= 0) {
3612 sb.setLength(0);
3613 sb.append(prefix);
3614 sb.append(" Battery time remaining: ");
3615 formatTimeMs(sb, batteryTimeRemaining / 1000);
3616 pw.println(sb.toString());
3617 }
3618 if (chargeTimeRemaining >= 0) {
3619 sb.setLength(0);
3620 sb.append(prefix);
3621 sb.append(" Charge time remaining: ");
3622 formatTimeMs(sb, chargeTimeRemaining / 1000);
3623 pw.println(sb.toString());
3624 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003625
3626 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3627 final long dischargeCount = dischargeCounter.getCountLocked(which);
3628 if (dischargeCount >= 0) {
3629 sb.setLength(0);
3630 sb.append(prefix);
3631 sb.append(" Discharge: ");
3632 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3633 sb.append(" mAh");
3634 pw.println(sb.toString());
3635 }
3636
3637 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3638 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3639 if (dischargeScreenOffCount >= 0) {
3640 sb.setLength(0);
3641 sb.append(prefix);
3642 sb.append(" Screen off discharge: ");
3643 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3644 sb.append(" mAh");
3645 pw.println(sb.toString());
3646 }
3647
3648 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3649 if (dischargeScreenOnCount >= 0) {
3650 sb.setLength(0);
3651 sb.append(prefix);
3652 sb.append(" Screen on discharge: ");
3653 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3654 sb.append(" mAh");
3655 pw.println(sb.toString());
3656 }
3657
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003658 pw.print(" Start clock time: ");
3659 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3660
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003661 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003662 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003663 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003664 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3665 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003666 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003667 rawRealtime, which);
3668 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3669 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003670 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003671 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003672 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3673 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3674 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003675 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003676 sb.append(prefix);
3677 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3678 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003679 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003680 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3681 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003682 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003683 pw.println(sb.toString());
3684 sb.setLength(0);
3685 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003686 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003687 boolean didOne = false;
3688 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003689 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003690 if (time == 0) {
3691 continue;
3692 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003693 sb.append("\n ");
3694 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003695 didOne = true;
3696 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3697 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003698 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003699 sb.append("(");
3700 sb.append(formatRatioLocked(time, screenOnTime));
3701 sb.append(")");
3702 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003703 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003704 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003705 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003706 sb.setLength(0);
3707 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003708 sb.append(" Power save mode enabled: ");
3709 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003710 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003711 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003712 sb.append(")");
3713 pw.println(sb.toString());
3714 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003715 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003716 sb.setLength(0);
3717 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003718 sb.append(" Device light idling: ");
3719 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003720 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003721 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3722 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003723 sb.append("x");
3724 pw.println(sb.toString());
3725 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003726 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003727 sb.setLength(0);
3728 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003729 sb.append(" Idle mode light time: ");
3730 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003731 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003732 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3733 sb.append(") ");
3734 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003735 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003736 sb.append(" -- longest ");
3737 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3738 pw.println(sb.toString());
3739 }
3740 if (deviceIdlingTime != 0) {
3741 sb.setLength(0);
3742 sb.append(prefix);
3743 sb.append(" Device full idling: ");
3744 formatTimeMs(sb, deviceIdlingTime / 1000);
3745 sb.append("(");
3746 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003747 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003748 sb.append("x");
3749 pw.println(sb.toString());
3750 }
3751 if (deviceIdleModeFullTime != 0) {
3752 sb.setLength(0);
3753 sb.append(prefix);
3754 sb.append(" Idle mode full time: ");
3755 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3756 sb.append("(");
3757 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3758 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003759 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003760 sb.append("x");
3761 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003762 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003763 pw.println(sb.toString());
3764 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003765 if (phoneOnTime != 0) {
3766 sb.setLength(0);
3767 sb.append(prefix);
3768 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3769 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003770 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003771 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003772 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003773 if (connChanges != 0) {
3774 pw.print(prefix);
3775 pw.print(" Connectivity changes: "); pw.println(connChanges);
3776 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003777
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003778 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003779 long fullWakeLockTimeTotalMicros = 0;
3780 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003781
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003782 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003783
Evan Millar22ac0432009-03-31 11:33:18 -07003784 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003785 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003786
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003787 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3788 = u.getWakelockStats();
3789 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3790 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003791
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003792 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3793 if (fullWakeTimer != null) {
3794 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3795 rawRealtime, which);
3796 }
3797
3798 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3799 if (partialWakeTimer != null) {
3800 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3801 rawRealtime, which);
3802 if (totalTimeMicros > 0) {
3803 if (reqUid < 0) {
3804 // Only show the ordered list of all wake
3805 // locks if the caller is not asking for data
3806 // about a specific uid.
3807 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3808 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003809 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003810 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003811 }
3812 }
3813 }
3814 }
3815
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003816 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3817 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3818 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3819 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3820 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3821 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3822 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3823 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003824 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3825 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003826
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003827 if (fullWakeLockTimeTotalMicros != 0) {
3828 sb.setLength(0);
3829 sb.append(prefix);
3830 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3831 (fullWakeLockTimeTotalMicros + 500) / 1000);
3832 pw.println(sb.toString());
3833 }
3834
3835 if (partialWakeLockTimeTotalMicros != 0) {
3836 sb.setLength(0);
3837 sb.append(prefix);
3838 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3839 (partialWakeLockTimeTotalMicros + 500) / 1000);
3840 pw.println(sb.toString());
3841 }
3842
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003843 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003844 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3845 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3846 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3847 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003848 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003849 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003850 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003851 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003852 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003853 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003854 if (time == 0) {
3855 continue;
3856 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003857 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003858 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003859 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003860 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003861 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003862 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003863 sb.append("(");
3864 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003865 sb.append(") ");
3866 sb.append(getPhoneSignalStrengthCount(i, which));
3867 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003868 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003869 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003870 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003871
3872 sb.setLength(0);
3873 sb.append(prefix);
3874 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003875 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003876 pw.println(sb.toString());
3877
Dianne Hackborn627bba72009-03-24 22:32:56 -07003878 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003879 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003880 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003881 didOne = false;
3882 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003883 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003884 if (time == 0) {
3885 continue;
3886 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003887 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003888 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003889 didOne = true;
3890 sb.append(DATA_CONNECTION_NAMES[i]);
3891 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003892 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003893 sb.append("(");
3894 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003895 sb.append(") ");
3896 sb.append(getPhoneDataConnectionCount(i, which));
3897 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003898 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003899 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003900 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003901
3902 sb.setLength(0);
3903 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003904 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003905 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003906 formatTimeMs(sb, mobileActiveTime / 1000);
3907 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3908 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3909 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003910 pw.println(sb.toString());
3911
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003912 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3913 if (mobileActiveUnknownTime != 0) {
3914 sb.setLength(0);
3915 sb.append(prefix);
3916 sb.append(" Mobile radio active unknown time: ");
3917 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3918 sb.append("(");
3919 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3920 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3921 sb.append("x");
3922 pw.println(sb.toString());
3923 }
3924
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003925 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3926 if (mobileActiveAdjustedTime != 0) {
3927 sb.setLength(0);
3928 sb.append(prefix);
3929 sb.append(" Mobile radio active adjusted time: ");
3930 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3931 sb.append("(");
3932 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3933 sb.append(")");
3934 pw.println(sb.toString());
3935 }
3936
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003937 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3938
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003939 pw.print(prefix);
3940 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3941 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3942 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3943 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003944 sb.setLength(0);
3945 sb.append(prefix);
3946 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3947 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3948 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3949 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003950 sb.append(")");
3951 pw.println(sb.toString());
3952
3953 sb.setLength(0);
3954 sb.append(prefix);
3955 sb.append(" Wifi states:");
3956 didOne = false;
3957 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003958 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003959 if (time == 0) {
3960 continue;
3961 }
3962 sb.append("\n ");
3963 didOne = true;
3964 sb.append(WIFI_STATE_NAMES[i]);
3965 sb.append(" ");
3966 formatTimeMs(sb, time/1000);
3967 sb.append("(");
3968 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3969 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003970 sb.append(getWifiStateCount(i, which));
3971 sb.append("x");
3972 }
3973 if (!didOne) sb.append(" (no activity)");
3974 pw.println(sb.toString());
3975
3976 sb.setLength(0);
3977 sb.append(prefix);
3978 sb.append(" Wifi supplicant states:");
3979 didOne = false;
3980 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3981 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3982 if (time == 0) {
3983 continue;
3984 }
3985 sb.append("\n ");
3986 didOne = true;
3987 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3988 sb.append(" ");
3989 formatTimeMs(sb, time/1000);
3990 sb.append("(");
3991 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3992 sb.append(") ");
3993 sb.append(getWifiSupplStateCount(i, which));
3994 sb.append("x");
3995 }
3996 if (!didOne) sb.append(" (no activity)");
3997 pw.println(sb.toString());
3998
3999 sb.setLength(0);
4000 sb.append(prefix);
4001 sb.append(" Wifi signal levels:");
4002 didOne = false;
4003 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4004 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4005 if (time == 0) {
4006 continue;
4007 }
4008 sb.append("\n ");
4009 sb.append(prefix);
4010 didOne = true;
4011 sb.append("level(");
4012 sb.append(i);
4013 sb.append(") ");
4014 formatTimeMs(sb, time/1000);
4015 sb.append("(");
4016 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4017 sb.append(") ");
4018 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004019 sb.append("x");
4020 }
4021 if (!didOne) sb.append(" (no activity)");
4022 pw.println(sb.toString());
4023
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004024 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004025
Adam Lesinski50e47602015-12-04 17:04:54 -08004026 pw.print(prefix);
4027 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4028 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4029
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004030 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4031 sb.setLength(0);
4032 sb.append(prefix);
4033 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4034 pw.println(sb.toString());
4035
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004036 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4037 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004038
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004039 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004040
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004041 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004042 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004043 pw.print(prefix); pw.println(" Device is currently unplugged");
4044 pw.print(prefix); pw.print(" Discharge cycle start level: ");
4045 pw.println(getDischargeStartLevel());
4046 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4047 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004048 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004049 pw.print(prefix); pw.println(" Device is currently plugged into power");
4050 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
4051 pw.println(getDischargeStartLevel());
4052 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
4053 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004054 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004055 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4056 pw.println(getDischargeAmountScreenOn());
4057 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4058 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004059 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004060 } else {
4061 pw.print(prefix); pw.println(" Device battery use since last full charge");
4062 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4063 pw.println(getLowDischargeAmountSinceCharge());
4064 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4065 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004066 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4067 pw.println(getDischargeAmountScreenOnSinceCharge());
4068 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4069 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004070 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004071 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004072
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004073 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004074 helper.create(this);
4075 helper.refreshStats(which, UserHandle.USER_ALL);
4076 List<BatterySipper> sippers = helper.getUsageList();
4077 if (sippers != null && sippers.size() > 0) {
4078 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4079 pw.print(prefix); pw.print(" Capacity: ");
4080 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004081 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004082 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4083 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4084 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4085 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004086 pw.println();
4087 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004088 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004089 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004090 switch (bs.drainType) {
4091 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004092 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004093 break;
4094 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004095 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004096 break;
4097 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004098 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004099 break;
4100 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004101 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004102 break;
4103 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004104 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004105 break;
4106 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004107 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004108 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004109 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004110 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004111 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004112 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004113 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004114 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004115 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004116 break;
4117 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004118 pw.print(" User "); pw.print(bs.userId);
4119 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004120 break;
4121 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004122 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004123 break;
4124 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004125 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004126 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004127 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004128 pw.print(" Camera: ");
4129 break;
4130 default:
4131 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004132 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004133 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004134 printmAh(pw, bs.totalPowerMah);
4135
Adam Lesinski57123002015-06-12 16:12:07 -07004136 if (bs.usagePowerMah != bs.totalPowerMah) {
4137 // If the usage (generic power) isn't the whole amount, we list out
4138 // what components are involved in the calculation.
4139
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004140 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004141 if (bs.usagePowerMah != 0) {
4142 pw.print(" usage=");
4143 printmAh(pw, bs.usagePowerMah);
4144 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004145 if (bs.cpuPowerMah != 0) {
4146 pw.print(" cpu=");
4147 printmAh(pw, bs.cpuPowerMah);
4148 }
4149 if (bs.wakeLockPowerMah != 0) {
4150 pw.print(" wake=");
4151 printmAh(pw, bs.wakeLockPowerMah);
4152 }
4153 if (bs.mobileRadioPowerMah != 0) {
4154 pw.print(" radio=");
4155 printmAh(pw, bs.mobileRadioPowerMah);
4156 }
4157 if (bs.wifiPowerMah != 0) {
4158 pw.print(" wifi=");
4159 printmAh(pw, bs.wifiPowerMah);
4160 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004161 if (bs.bluetoothPowerMah != 0) {
4162 pw.print(" bt=");
4163 printmAh(pw, bs.bluetoothPowerMah);
4164 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004165 if (bs.gpsPowerMah != 0) {
4166 pw.print(" gps=");
4167 printmAh(pw, bs.gpsPowerMah);
4168 }
4169 if (bs.sensorPowerMah != 0) {
4170 pw.print(" sensor=");
4171 printmAh(pw, bs.sensorPowerMah);
4172 }
4173 if (bs.cameraPowerMah != 0) {
4174 pw.print(" camera=");
4175 printmAh(pw, bs.cameraPowerMah);
4176 }
4177 if (bs.flashlightPowerMah != 0) {
4178 pw.print(" flash=");
4179 printmAh(pw, bs.flashlightPowerMah);
4180 }
4181 pw.print(" )");
4182 }
4183 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004184 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004185 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004186 }
4187
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004188 sippers = helper.getMobilemsppList();
4189 if (sippers != null && sippers.size() > 0) {
4190 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004191 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004192 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004193 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004194 sb.setLength(0);
4195 sb.append(prefix); sb.append(" Uid ");
4196 UserHandle.formatUid(sb, bs.uidObj.getUid());
4197 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4198 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4199 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004200 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004201 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004202 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004203 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004204 sb.setLength(0);
4205 sb.append(prefix);
4206 sb.append(" TOTAL TIME: ");
4207 formatTimeMs(sb, totalTime);
4208 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4209 sb.append(")");
4210 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004211 pw.println();
4212 }
4213
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004214 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4215 @Override
4216 public int compare(TimerEntry lhs, TimerEntry rhs) {
4217 long lhsTime = lhs.mTime;
4218 long rhsTime = rhs.mTime;
4219 if (lhsTime < rhsTime) {
4220 return 1;
4221 }
4222 if (lhsTime > rhsTime) {
4223 return -1;
4224 }
4225 return 0;
4226 }
4227 };
4228
4229 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004230 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4231 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004232 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004233 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4234 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4235 : kernelWakelocks.entrySet()) {
4236 final BatteryStats.Timer timer = ent.getValue();
4237 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004238 if (totalTimeMillis > 0) {
4239 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4240 }
4241 }
4242 if (ktimers.size() > 0) {
4243 Collections.sort(ktimers, timerComparator);
4244 pw.print(prefix); pw.println(" All kernel wake locks:");
4245 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004246 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004247 String linePrefix = ": ";
4248 sb.setLength(0);
4249 sb.append(prefix);
4250 sb.append(" Kernel Wake lock ");
4251 sb.append(timer.mName);
4252 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4253 which, linePrefix);
4254 if (!linePrefix.equals(": ")) {
4255 sb.append(" realtime");
4256 // Only print out wake locks that were held
4257 pw.println(sb.toString());
4258 }
4259 }
4260 pw.println();
4261 }
4262 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004263
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004264 if (timers.size() > 0) {
4265 Collections.sort(timers, timerComparator);
4266 pw.print(prefix); pw.println(" All partial wake locks:");
4267 for (int i=0; i<timers.size(); i++) {
4268 TimerEntry timer = timers.get(i);
4269 sb.setLength(0);
4270 sb.append(" Wake lock ");
4271 UserHandle.formatUid(sb, timer.mId);
4272 sb.append(" ");
4273 sb.append(timer.mName);
4274 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4275 sb.append(" realtime");
4276 pw.println(sb.toString());
4277 }
4278 timers.clear();
4279 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004280 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004281
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004282 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004283 if (wakeupReasons.size() > 0) {
4284 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004285 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004286 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004287 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004288 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4289 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004290 }
4291 Collections.sort(reasons, timerComparator);
4292 for (int i=0; i<reasons.size(); i++) {
4293 TimerEntry timer = reasons.get(i);
4294 String linePrefix = ": ";
4295 sb.setLength(0);
4296 sb.append(prefix);
4297 sb.append(" Wakeup reason ");
4298 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004299 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4300 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004301 pw.println(sb.toString());
4302 }
4303 pw.println();
4304 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004305 }
Evan Millar22ac0432009-03-31 11:33:18 -07004306
James Carr2dd7e5e2016-07-20 18:48:39 -07004307 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4308 pw.println("Memory Stats");
4309 for (int i = 0; i < mMemoryStats.size(); i++) {
4310 sb.setLength(0);
4311 sb.append("Bandwidth ");
4312 sb.append(mMemoryStats.keyAt(i));
4313 sb.append(" Time ");
4314 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4315 pw.println(sb.toString());
4316 }
4317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 for (int iu=0; iu<NU; iu++) {
4319 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004320 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004321 continue;
4322 }
4323
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004324 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004325
4326 pw.print(prefix);
4327 pw.print(" ");
4328 UserHandle.formatUid(pw, uid);
4329 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004331
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004332 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4333 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4334 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4335 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004336 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4337 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4338
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004339 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4340 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004341 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4342 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004343
4344 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4345 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4346
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004347 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4348 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4349 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004350 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4351 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4352 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4353 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004354 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004355
Adam Lesinski5f056f62016-07-14 16:56:08 -07004356 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4357 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4358
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004359 if (mobileRxBytes > 0 || mobileTxBytes > 0
4360 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004361 pw.print(prefix); pw.print(" Mobile network: ");
4362 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004363 pw.print(formatBytesLocked(mobileTxBytes));
4364 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4365 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004367 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4368 sb.setLength(0);
4369 sb.append(prefix); sb.append(" Mobile radio active: ");
4370 formatTimeMs(sb, uidMobileActiveTime / 1000);
4371 sb.append("(");
4372 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4373 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4374 long packets = mobileRxPackets + mobileTxPackets;
4375 if (packets == 0) {
4376 packets = 1;
4377 }
4378 sb.append(" @ ");
4379 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4380 sb.append(" mspp");
4381 pw.println(sb.toString());
4382 }
4383
Adam Lesinski5f056f62016-07-14 16:56:08 -07004384 if (mobileWakeup > 0) {
4385 sb.setLength(0);
4386 sb.append(prefix);
4387 sb.append(" Mobile radio AP wakeups: ");
4388 sb.append(mobileWakeup);
4389 pw.println(sb.toString());
4390 }
4391
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004392 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4393 u.getModemControllerActivity(), which);
4394
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004395 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004396 pw.print(prefix); pw.print(" Wi-Fi network: ");
4397 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004398 pw.print(formatBytesLocked(wifiTxBytes));
4399 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4400 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004401 }
4402
Dianne Hackborn62793e42015-03-09 11:15:41 -07004403 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004404 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004405 || uidWifiRunningTime != 0) {
4406 sb.setLength(0);
4407 sb.append(prefix); sb.append(" Wifi Running: ");
4408 formatTimeMs(sb, uidWifiRunningTime / 1000);
4409 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4410 whichBatteryRealtime)); sb.append(")\n");
4411 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4412 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4413 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4414 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004415 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004416 formatTimeMs(sb, wifiScanTime / 1000);
4417 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004418 whichBatteryRealtime)); sb.append(") ");
4419 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004420 sb.append("x\n");
4421 // actual and background times are unpooled and since reset (regardless of 'which')
4422 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4423 formatTimeMs(sb, wifiScanActualTime / 1000);
4424 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4425 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4426 sb.append(") ");
4427 sb.append(wifiScanCount);
4428 sb.append("x\n");
4429 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4430 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4431 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4432 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4433 sb.append(") ");
4434 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004435 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004436 pw.println(sb.toString());
4437 }
4438
Adam Lesinski5f056f62016-07-14 16:56:08 -07004439 if (wifiWakeup > 0) {
4440 sb.setLength(0);
4441 sb.append(prefix);
4442 sb.append(" WiFi AP wakeups: ");
4443 sb.append(wifiWakeup);
4444 pw.println(sb.toString());
4445 }
4446
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004447 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4448 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004449
Adam Lesinski50e47602015-12-04 17:04:54 -08004450 if (btRxBytes > 0 || btTxBytes > 0) {
4451 pw.print(prefix); pw.print(" Bluetooth network: ");
4452 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4453 pw.print(formatBytesLocked(btTxBytes));
4454 pw.println(" sent");
4455 }
4456
Bookatz867c0d72017-03-07 18:23:42 -08004457 final Timer bleTimer = u.getBluetoothScanTimer();
4458 if (bleTimer != null) {
4459 // Convert from microseconds to milliseconds with rounding
4460 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4461 / 1000;
4462 if (totalTimeMs != 0) {
4463 final int count = bleTimer.getCountLocked(which);
4464 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4465 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
4466 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4467 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4468 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4469 final long actualTimeMsBg = bleTimerBg != null ?
4470 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4471
4472 sb.setLength(0);
4473 sb.append(prefix);
4474 sb.append(" ");
4475 sb.append("Bluetooth Scan");
4476 sb.append(": ");
4477 if (actualTimeMs != totalTimeMs) {
4478 formatTimeMs(sb, totalTimeMs);
4479 sb.append("blamed realtime, ");
4480 }
4481 formatTimeMs(sb, actualTimeMs); // since reset, regardless of 'which'
4482 sb.append("realtime (");
4483 sb.append(count);
4484 sb.append(" times)");
4485 if (bleTimer.isRunningLocked()) {
4486 sb.append(" (running)");
4487 }
4488 if (actualTimeMsBg != 0 || countBg > 0) {
4489 sb.append(", ");
4490 formatTimeMs(sb, actualTimeMsBg); // since reset, regardless of 'which'
4491 sb.append("background (");
4492 sb.append(countBg);
4493 sb.append(" times)");
4494 }
4495 pw.println(sb.toString());
4496 uidActivity = true;
4497 }
4498 }
4499
4500
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004501
Dianne Hackborn617f8772009-03-31 15:04:46 -07004502 if (u.hasUserActivity()) {
4503 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004504 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004505 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004506 if (val != 0) {
4507 if (!hasData) {
4508 sb.setLength(0);
4509 sb.append(" User activity: ");
4510 hasData = true;
4511 } else {
4512 sb.append(", ");
4513 }
4514 sb.append(val);
4515 sb.append(" ");
4516 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4517 }
4518 }
4519 if (hasData) {
4520 pw.println(sb.toString());
4521 }
4522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004524 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4525 = u.getWakelockStats();
4526 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004527 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004528 int countWakelock = 0;
4529 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4530 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4531 String linePrefix = ": ";
4532 sb.setLength(0);
4533 sb.append(prefix);
4534 sb.append(" Wake lock ");
4535 sb.append(wakelocks.keyAt(iw));
4536 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4537 "full", which, linePrefix);
4538 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4539 "partial", which, linePrefix);
4540 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4541 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004542 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4543 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004544 sb.append(" realtime");
4545 pw.println(sb.toString());
4546 uidActivity = true;
4547 countWakelock++;
4548
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004549 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4550 rawRealtime, which);
4551 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4552 rawRealtime, which);
4553 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4554 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004555 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004556 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004557 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004558 if (countWakelock > 1) {
4559 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4560 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004561 sb.setLength(0);
4562 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004563 sb.append(" TOTAL wake: ");
4564 boolean needComma = false;
4565 if (totalFullWakelock != 0) {
4566 needComma = true;
4567 formatTimeMs(sb, totalFullWakelock);
4568 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004569 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004570 if (totalPartialWakelock != 0) {
4571 if (needComma) {
4572 sb.append(", ");
4573 }
4574 needComma = true;
4575 formatTimeMs(sb, totalPartialWakelock);
4576 sb.append("partial");
4577 }
4578 if (totalWindowWakelock != 0) {
4579 if (needComma) {
4580 sb.append(", ");
4581 }
4582 needComma = true;
4583 formatTimeMs(sb, totalWindowWakelock);
4584 sb.append("window");
4585 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004586 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004587 if (needComma) {
4588 sb.append(",");
4589 }
4590 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004591 formatTimeMs(sb, totalDrawWakelock);
4592 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004593 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004594 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004595 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004596 }
4597 }
4598
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004599 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4600 for (int isy=syncs.size()-1; isy>=0; isy--) {
4601 final Timer timer = syncs.valueAt(isy);
4602 // Convert from microseconds to milliseconds with rounding
4603 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4604 final int count = timer.getCountLocked(which);
4605 sb.setLength(0);
4606 sb.append(prefix);
4607 sb.append(" Sync ");
4608 sb.append(syncs.keyAt(isy));
4609 sb.append(": ");
4610 if (totalTime != 0) {
4611 formatTimeMs(sb, totalTime);
4612 sb.append("realtime (");
4613 sb.append(count);
4614 sb.append(" times)");
4615 } else {
4616 sb.append("(not used)");
4617 }
4618 pw.println(sb.toString());
4619 uidActivity = true;
4620 }
4621
4622 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4623 for (int ij=jobs.size()-1; ij>=0; ij--) {
4624 final Timer timer = jobs.valueAt(ij);
4625 // Convert from microseconds to milliseconds with rounding
4626 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4627 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004628 final Timer bgTimer = timer.getSubTimer();
4629 final long bgTime = bgTimer != null ?
4630 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4631 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004632 sb.setLength(0);
4633 sb.append(prefix);
4634 sb.append(" Job ");
4635 sb.append(jobs.keyAt(ij));
4636 sb.append(": ");
4637 if (totalTime != 0) {
4638 formatTimeMs(sb, totalTime);
4639 sb.append("realtime (");
4640 sb.append(count);
4641 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07004642 if (bgTime > 0) {
4643 sb.append(", ");
4644 formatTimeMs(sb, bgTime);
4645 sb.append("background (");
4646 sb.append(bgCount);
4647 sb.append(" times)");
4648 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004649 } else {
4650 sb.append("(not used)");
4651 }
4652 pw.println(sb.toString());
4653 uidActivity = true;
4654 }
4655
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004656 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4657 prefix, "Flashlight");
4658 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4659 prefix, "Camera");
4660 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4661 prefix, "Video");
4662 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4663 prefix, "Audio");
4664
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004665 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4666 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004667 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004668 final Uid.Sensor se = sensors.valueAt(ise);
4669 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004670 sb.setLength(0);
4671 sb.append(prefix);
4672 sb.append(" Sensor ");
4673 int handle = se.getHandle();
4674 if (handle == Uid.Sensor.GPS) {
4675 sb.append("GPS");
4676 } else {
4677 sb.append(handle);
4678 }
4679 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004681 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004682 if (timer != null) {
4683 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08004684 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4685 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004686 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08004687 final Timer bgTimer = se.getSensorBackgroundTime();
4688 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
4689 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4690 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4691 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4692 final long bgActualTime = bgTimer != null ?
4693 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4694
Dianne Hackborn61659e52014-07-09 16:13:01 -07004695 //timer.logState();
4696 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004697 if (actualTime != totalTime) {
4698 formatTimeMs(sb, totalTime);
4699 sb.append("blamed realtime, ");
4700 }
4701
4702 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07004703 sb.append("realtime (");
4704 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08004705 sb.append(" times)");
4706
4707 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004708 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08004709 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
4710 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004711 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08004712 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004714 } else {
4715 sb.append("(not used)");
4716 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004717 } else {
4718 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004719 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004720
4721 pw.println(sb.toString());
4722 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723 }
4724
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004725 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4726 "Vibrator");
4727 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4728 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004729
Dianne Hackborn61659e52014-07-09 16:13:01 -07004730 long totalStateTime = 0;
4731 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4732 long time = u.getProcessStateTime(ips, rawRealtime, which);
4733 if (time > 0) {
4734 totalStateTime += time;
4735 sb.setLength(0);
4736 sb.append(prefix);
4737 sb.append(" ");
4738 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4739 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004740 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004741 pw.println(sb.toString());
4742 uidActivity = true;
4743 }
4744 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004745 if (totalStateTime > 0) {
4746 sb.setLength(0);
4747 sb.append(prefix);
4748 sb.append(" Total running: ");
4749 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4750 pw.println(sb.toString());
4751 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004752
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004753 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4754 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004755 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004756 sb.setLength(0);
4757 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004758 sb.append(" Total cpu time: u=");
4759 formatTimeMs(sb, userCpuTimeUs / 1000);
4760 sb.append("s=");
4761 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004762 pw.println(sb.toString());
4763 }
4764
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004765 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4766 = u.getProcessStats();
4767 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4768 final Uid.Proc ps = processStats.valueAt(ipr);
4769 long userTime;
4770 long systemTime;
4771 long foregroundTime;
4772 int starts;
4773 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004774
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004775 userTime = ps.getUserTime(which);
4776 systemTime = ps.getSystemTime(which);
4777 foregroundTime = ps.getForegroundTime(which);
4778 starts = ps.getStarts(which);
4779 final int numCrashes = ps.getNumCrashes(which);
4780 final int numAnrs = ps.getNumAnrs(which);
4781 numExcessive = which == STATS_SINCE_CHARGED
4782 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004784 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4785 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4786 sb.setLength(0);
4787 sb.append(prefix); sb.append(" Proc ");
4788 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4789 sb.append(prefix); sb.append(" CPU: ");
4790 formatTimeMs(sb, userTime); sb.append("usr + ");
4791 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4792 formatTimeMs(sb, foregroundTime); sb.append("fg");
4793 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4794 sb.append("\n"); sb.append(prefix); sb.append(" ");
4795 boolean hasOne = false;
4796 if (starts != 0) {
4797 hasOne = true;
4798 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004799 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004800 if (numCrashes != 0) {
4801 if (hasOne) {
4802 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004803 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004804 hasOne = true;
4805 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004806 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004807 if (numAnrs != 0) {
4808 if (hasOne) {
4809 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004810 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004811 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 }
4813 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004814 pw.println(sb.toString());
4815 for (int e=0; e<numExcessive; e++) {
4816 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4817 if (ew != null) {
4818 pw.print(prefix); pw.print(" * Killed for ");
4819 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4820 pw.print("wake lock");
4821 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4822 pw.print("cpu");
4823 } else {
4824 pw.print("unknown");
4825 }
4826 pw.print(" use: ");
4827 TimeUtils.formatDuration(ew.usedTime, pw);
4828 pw.print(" over ");
4829 TimeUtils.formatDuration(ew.overTime, pw);
4830 if (ew.overTime != 0) {
4831 pw.print(" (");
4832 pw.print((ew.usedTime*100)/ew.overTime);
4833 pw.println("%)");
4834 }
4835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004836 }
4837 uidActivity = true;
4838 }
4839 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004840
4841 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4842 = u.getPackageStats();
4843 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4844 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4845 pw.println(":");
4846 boolean apkActivity = false;
4847 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4848 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4849 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4850 pw.print(prefix); pw.print(" Wakeup alarm ");
4851 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4852 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4853 pw.println(" times");
4854 apkActivity = true;
4855 }
4856 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4857 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4858 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4859 final long startTime = ss.getStartTime(batteryUptime, which);
4860 final int starts = ss.getStarts(which);
4861 final int launches = ss.getLaunches(which);
4862 if (startTime != 0 || starts != 0 || launches != 0) {
4863 sb.setLength(0);
4864 sb.append(prefix); sb.append(" Service ");
4865 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4866 sb.append(prefix); sb.append(" Created for: ");
4867 formatTimeMs(sb, startTime / 1000);
4868 sb.append("uptime\n");
4869 sb.append(prefix); sb.append(" Starts: ");
4870 sb.append(starts);
4871 sb.append(", launches: "); sb.append(launches);
4872 pw.println(sb.toString());
4873 apkActivity = true;
4874 }
4875 }
4876 if (!apkActivity) {
4877 pw.print(prefix); pw.println(" (nothing executed)");
4878 }
4879 uidActivity = true;
4880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004881 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004882 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 }
4884 }
4885 }
4886
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004887 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004888 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004889 int diff = oldval ^ newval;
4890 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004891 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004892 for (int i=0; i<descriptions.length; i++) {
4893 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004894 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004895 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004896 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004897 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004898 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004899 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4900 didWake = true;
4901 pw.print("=");
4902 if (longNames) {
4903 UserHandle.formatUid(pw, wakelockTag.uid);
4904 pw.print(":\"");
4905 pw.print(wakelockTag.string);
4906 pw.print("\"");
4907 } else {
4908 pw.print(wakelockTag.poolIdx);
4909 }
4910 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004911 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004912 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004913 pw.print("=");
4914 int val = (newval&bd.mask)>>bd.shift;
4915 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004916 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004917 } else {
4918 pw.print(val);
4919 }
4920 }
4921 }
4922 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004923 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004924 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004925 if (longNames) {
4926 UserHandle.formatUid(pw, wakelockTag.uid);
4927 pw.print(":\"");
4928 pw.print(wakelockTag.string);
4929 pw.print("\"");
4930 } else {
4931 pw.print(wakelockTag.poolIdx);
4932 }
4933 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004934 }
4935
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004936 public void prepareForDumpLocked() {
4937 }
4938
4939 public static class HistoryPrinter {
4940 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004941 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004942 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004943 int oldStatus = -1;
4944 int oldHealth = -1;
4945 int oldPlug = -1;
4946 int oldTemp = -1;
4947 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004948 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004949 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004950
Dianne Hackborn3251b902014-06-20 14:40:53 -07004951 void reset() {
4952 oldState = oldState2 = 0;
4953 oldLevel = -1;
4954 oldStatus = -1;
4955 oldHealth = -1;
4956 oldPlug = -1;
4957 oldTemp = -1;
4958 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004959 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07004960 }
4961
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004962 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004963 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004964 if (!checkin) {
4965 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004966 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004967 pw.print(" (");
4968 pw.print(rec.numReadInts);
4969 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004970 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004971 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4972 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004973 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004974 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004975 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004976 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004977 }
4978 lastTime = rec.time;
4979 }
4980 if (rec.cmd == HistoryItem.CMD_START) {
4981 if (checkin) {
4982 pw.print(":");
4983 }
4984 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004985 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004986 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4987 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004988 if (checkin) {
4989 pw.print(":");
4990 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004991 if (rec.cmd == HistoryItem.CMD_RESET) {
4992 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004993 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004994 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004995 pw.print("TIME:");
4996 if (checkin) {
4997 pw.println(rec.currentTime);
4998 } else {
4999 pw.print(" ");
5000 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5001 rec.currentTime).toString());
5002 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005003 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5004 if (checkin) {
5005 pw.print(":");
5006 }
5007 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005008 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5009 if (checkin) {
5010 pw.print(":");
5011 }
5012 pw.println("*OVERFLOW*");
5013 } else {
5014 if (!checkin) {
5015 if (rec.batteryLevel < 10) pw.print("00");
5016 else if (rec.batteryLevel < 100) pw.print("0");
5017 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005018 if (verbose) {
5019 pw.print(" ");
5020 if (rec.states < 0) ;
5021 else if (rec.states < 0x10) pw.print("0000000");
5022 else if (rec.states < 0x100) pw.print("000000");
5023 else if (rec.states < 0x1000) pw.print("00000");
5024 else if (rec.states < 0x10000) pw.print("0000");
5025 else if (rec.states < 0x100000) pw.print("000");
5026 else if (rec.states < 0x1000000) pw.print("00");
5027 else if (rec.states < 0x10000000) pw.print("0");
5028 pw.print(Integer.toHexString(rec.states));
5029 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005030 } else {
5031 if (oldLevel != rec.batteryLevel) {
5032 oldLevel = rec.batteryLevel;
5033 pw.print(",Bl="); pw.print(rec.batteryLevel);
5034 }
5035 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005036 if (oldStatus != rec.batteryStatus) {
5037 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005038 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005039 switch (oldStatus) {
5040 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005041 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005042 break;
5043 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005044 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005045 break;
5046 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005047 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005048 break;
5049 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005050 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005051 break;
5052 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005053 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005054 break;
5055 default:
5056 pw.print(oldStatus);
5057 break;
5058 }
5059 }
5060 if (oldHealth != rec.batteryHealth) {
5061 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005062 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005063 switch (oldHealth) {
5064 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005065 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005066 break;
5067 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005068 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005069 break;
5070 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005071 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005072 break;
5073 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005074 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005075 break;
5076 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005077 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005078 break;
5079 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005080 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005081 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005082 case BatteryManager.BATTERY_HEALTH_COLD:
5083 pw.print(checkin ? "c" : "cold");
5084 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005085 default:
5086 pw.print(oldHealth);
5087 break;
5088 }
5089 }
5090 if (oldPlug != rec.batteryPlugType) {
5091 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005092 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005093 switch (oldPlug) {
5094 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005095 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005096 break;
5097 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005098 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005099 break;
5100 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005101 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005102 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005103 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005104 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005105 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005106 default:
5107 pw.print(oldPlug);
5108 break;
5109 }
5110 }
5111 if (oldTemp != rec.batteryTemperature) {
5112 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005113 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005114 pw.print(oldTemp);
5115 }
5116 if (oldVolt != rec.batteryVoltage) {
5117 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005118 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005119 pw.print(oldVolt);
5120 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005121 final int chargeMAh = rec.batteryChargeUAh / 1000;
5122 if (oldChargeMAh != chargeMAh) {
5123 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005124 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005125 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005126 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005127 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005128 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005129 printBitDescriptions(pw, oldState2, rec.states2, null,
5130 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005131 if (rec.wakeReasonTag != null) {
5132 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005133 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005134 pw.print(rec.wakeReasonTag.poolIdx);
5135 } else {
5136 pw.print(" wake_reason=");
5137 pw.print(rec.wakeReasonTag.uid);
5138 pw.print(":\"");
5139 pw.print(rec.wakeReasonTag.string);
5140 pw.print("\"");
5141 }
5142 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005143 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005144 pw.print(checkin ? "," : " ");
5145 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5146 pw.print("+");
5147 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5148 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005149 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005150 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5151 : HISTORY_EVENT_NAMES;
5152 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5153 | HistoryItem.EVENT_FLAG_FINISH);
5154 if (idx >= 0 && idx < eventNames.length) {
5155 pw.print(eventNames[idx]);
5156 } else {
5157 pw.print(checkin ? "Ev" : "event");
5158 pw.print(idx);
5159 }
5160 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005161 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005162 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005163 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005164 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5165 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005166 pw.print(":\"");
5167 pw.print(rec.eventTag.string);
5168 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005169 }
5170 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005171 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005172 if (rec.stepDetails != null) {
5173 if (!checkin) {
5174 pw.print(" Details: cpu=");
5175 pw.print(rec.stepDetails.userTime);
5176 pw.print("u+");
5177 pw.print(rec.stepDetails.systemTime);
5178 pw.print("s");
5179 if (rec.stepDetails.appCpuUid1 >= 0) {
5180 pw.print(" (");
5181 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5182 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5183 if (rec.stepDetails.appCpuUid2 >= 0) {
5184 pw.print(", ");
5185 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5186 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5187 }
5188 if (rec.stepDetails.appCpuUid3 >= 0) {
5189 pw.print(", ");
5190 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5191 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5192 }
5193 pw.print(')');
5194 }
5195 pw.println();
5196 pw.print(" /proc/stat=");
5197 pw.print(rec.stepDetails.statUserTime);
5198 pw.print(" usr, ");
5199 pw.print(rec.stepDetails.statSystemTime);
5200 pw.print(" sys, ");
5201 pw.print(rec.stepDetails.statIOWaitTime);
5202 pw.print(" io, ");
5203 pw.print(rec.stepDetails.statIrqTime);
5204 pw.print(" irq, ");
5205 pw.print(rec.stepDetails.statSoftIrqTime);
5206 pw.print(" sirq, ");
5207 pw.print(rec.stepDetails.statIdlTime);
5208 pw.print(" idle");
5209 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5210 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5211 + rec.stepDetails.statSoftIrqTime;
5212 int total = totalRun + rec.stepDetails.statIdlTime;
5213 if (total > 0) {
5214 pw.print(" (");
5215 float perc = ((float)totalRun) / ((float)total) * 100;
5216 pw.print(String.format("%.1f%%", perc));
5217 pw.print(" of ");
5218 StringBuilder sb = new StringBuilder(64);
5219 formatTimeMsNoSpace(sb, total*10);
5220 pw.print(sb);
5221 pw.print(")");
5222 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005223 pw.print(", PlatformIdleStat ");
5224 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005225 pw.println();
5226 } else {
5227 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5228 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5229 pw.print(rec.stepDetails.userTime);
5230 pw.print(":");
5231 pw.print(rec.stepDetails.systemTime);
5232 if (rec.stepDetails.appCpuUid1 >= 0) {
5233 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5234 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5235 if (rec.stepDetails.appCpuUid2 >= 0) {
5236 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5237 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5238 }
5239 if (rec.stepDetails.appCpuUid3 >= 0) {
5240 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5241 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5242 }
5243 }
5244 pw.println();
5245 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5246 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5247 pw.print(rec.stepDetails.statUserTime);
5248 pw.print(',');
5249 pw.print(rec.stepDetails.statSystemTime);
5250 pw.print(',');
5251 pw.print(rec.stepDetails.statIOWaitTime);
5252 pw.print(',');
5253 pw.print(rec.stepDetails.statIrqTime);
5254 pw.print(',');
5255 pw.print(rec.stepDetails.statSoftIrqTime);
5256 pw.print(',');
5257 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005258 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005259 if (rec.stepDetails.statPlatformIdleState != null) {
5260 pw.print(rec.stepDetails.statPlatformIdleState);
5261 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005262 pw.println();
5263 }
5264 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005265 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005266 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005267 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005268 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005269
5270 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5271 UserHandle.formatUid(pw, uid);
5272 pw.print("=");
5273 pw.print(utime);
5274 pw.print("u+");
5275 pw.print(stime);
5276 pw.print("s");
5277 }
5278
5279 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5280 pw.print('/');
5281 pw.print(uid);
5282 pw.print(":");
5283 pw.print(utime);
5284 pw.print(":");
5285 pw.print(stime);
5286 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005287 }
5288
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005289 private void printSizeValue(PrintWriter pw, long size) {
5290 float result = size;
5291 String suffix = "";
5292 if (result >= 10*1024) {
5293 suffix = "KB";
5294 result = result / 1024;
5295 }
5296 if (result >= 10*1024) {
5297 suffix = "MB";
5298 result = result / 1024;
5299 }
5300 if (result >= 10*1024) {
5301 suffix = "GB";
5302 result = result / 1024;
5303 }
5304 if (result >= 10*1024) {
5305 suffix = "TB";
5306 result = result / 1024;
5307 }
5308 if (result >= 10*1024) {
5309 suffix = "PB";
5310 result = result / 1024;
5311 }
5312 pw.print((int)result);
5313 pw.print(suffix);
5314 }
5315
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005316 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5317 String label3, long estimatedTime) {
5318 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005319 return false;
5320 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005321 pw.print(label1);
5322 pw.print(label2);
5323 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005324 StringBuilder sb = new StringBuilder(64);
5325 formatTimeMs(sb, estimatedTime);
5326 pw.print(sb);
5327 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005328 return true;
5329 }
5330
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005331 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5332 LevelStepTracker steps, boolean checkin) {
5333 if (steps == null) {
5334 return false;
5335 }
5336 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005337 if (count <= 0) {
5338 return false;
5339 }
5340 if (!checkin) {
5341 pw.println(header);
5342 }
Kweku Adams030980a2015-04-01 16:07:48 -07005343 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005344 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005345 long duration = steps.getDurationAt(i);
5346 int level = steps.getLevelAt(i);
5347 long initMode = steps.getInitModeAt(i);
5348 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005349 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005350 lineArgs[0] = Long.toString(duration);
5351 lineArgs[1] = Integer.toString(level);
5352 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5353 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5354 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5355 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5356 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5357 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005358 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005359 }
5360 } else {
5361 lineArgs[2] = "";
5362 }
5363 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5364 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5365 } else {
5366 lineArgs[3] = "";
5367 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005368 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005369 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005370 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005371 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005372 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005373 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5374 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005375 pw.print(prefix);
5376 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005377 TimeUtils.formatDuration(duration, pw);
5378 pw.print(" to "); pw.print(level);
5379 boolean haveModes = false;
5380 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5381 pw.print(" (");
5382 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5383 case Display.STATE_OFF: pw.print("screen-off"); break;
5384 case Display.STATE_ON: pw.print("screen-on"); break;
5385 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5386 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005387 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005388 }
5389 haveModes = true;
5390 }
5391 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5392 pw.print(haveModes ? ", " : " (");
5393 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5394 ? "power-save-on" : "power-save-off");
5395 haveModes = true;
5396 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005397 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5398 pw.print(haveModes ? ", " : " (");
5399 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5400 ? "device-idle-on" : "device-idle-off");
5401 haveModes = true;
5402 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005403 if (haveModes) {
5404 pw.print(")");
5405 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005406 pw.println();
5407 }
5408 }
5409 return true;
5410 }
5411
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005412 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005413 public static final int DUMP_DAILY_ONLY = 1<<2;
5414 public static final int DUMP_HISTORY_ONLY = 1<<3;
5415 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5416 public static final int DUMP_VERBOSE = 1<<5;
5417 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005418
Dianne Hackborn37de0982014-05-09 09:32:18 -07005419 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5420 final HistoryPrinter hprinter = new HistoryPrinter();
5421 final HistoryItem rec = new HistoryItem();
5422 long lastTime = -1;
5423 long baseTime = -1;
5424 boolean printed = false;
5425 HistoryEventTracker tracker = null;
5426 while (getNextHistoryLocked(rec)) {
5427 lastTime = rec.time;
5428 if (baseTime < 0) {
5429 baseTime = lastTime;
5430 }
5431 if (rec.time >= histStart) {
5432 if (histStart >= 0 && !printed) {
5433 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005434 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005435 || rec.cmd == HistoryItem.CMD_START
5436 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005437 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005438 hprinter.printNextItem(pw, rec, baseTime, checkin,
5439 (flags&DUMP_VERBOSE) != 0);
5440 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005441 } else if (rec.currentTime != 0) {
5442 printed = true;
5443 byte cmd = rec.cmd;
5444 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005445 hprinter.printNextItem(pw, rec, baseTime, checkin,
5446 (flags&DUMP_VERBOSE) != 0);
5447 rec.cmd = cmd;
5448 }
5449 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005450 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5451 hprinter.printNextItem(pw, rec, baseTime, checkin,
5452 (flags&DUMP_VERBOSE) != 0);
5453 rec.cmd = HistoryItem.CMD_UPDATE;
5454 }
5455 int oldEventCode = rec.eventCode;
5456 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005457 rec.eventTag = new HistoryTag();
5458 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5459 HashMap<String, SparseIntArray> active
5460 = tracker.getStateForEvent(i);
5461 if (active == null) {
5462 continue;
5463 }
5464 for (HashMap.Entry<String, SparseIntArray> ent
5465 : active.entrySet()) {
5466 SparseIntArray uids = ent.getValue();
5467 for (int j=0; j<uids.size(); j++) {
5468 rec.eventCode = i;
5469 rec.eventTag.string = ent.getKey();
5470 rec.eventTag.uid = uids.keyAt(j);
5471 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005472 hprinter.printNextItem(pw, rec, baseTime, checkin,
5473 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005474 rec.wakeReasonTag = null;
5475 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005476 }
5477 }
5478 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005479 rec.eventCode = oldEventCode;
5480 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005481 tracker = null;
5482 }
5483 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005484 hprinter.printNextItem(pw, rec, baseTime, checkin,
5485 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005486 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5487 // This is an attempt to aggregate the previous state and generate
5488 // fake events to reflect that state at the point where we start
5489 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005490 if (tracker == null) {
5491 tracker = new HistoryEventTracker();
5492 }
5493 tracker.updateState(rec.eventCode, rec.eventTag.string,
5494 rec.eventTag.uid, rec.eventTag.poolIdx);
5495 }
5496 }
5497 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005498 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005499 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5500 }
5501 }
5502
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005503 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5504 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5505 if (steps == null) {
5506 return;
5507 }
5508 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5509 if (timeRemaining >= 0) {
5510 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5511 tmpSb.setLength(0);
5512 formatTimeMs(tmpSb, timeRemaining);
5513 pw.print(tmpSb);
5514 pw.print(" (from "); pw.print(tmpOutInt[0]);
5515 pw.println(" steps)");
5516 }
5517 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5518 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5519 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5520 if (estimatedTime > 0) {
5521 pw.print(prefix); pw.print(label); pw.print(" ");
5522 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5523 pw.print(" time: ");
5524 tmpSb.setLength(0);
5525 formatTimeMs(tmpSb, estimatedTime);
5526 pw.print(tmpSb);
5527 pw.print(" (from "); pw.print(tmpOutInt[0]);
5528 pw.println(" steps)");
5529 }
5530 }
5531 }
5532
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005533 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5534 ArrayList<PackageChange> changes) {
5535 if (changes == null) {
5536 return;
5537 }
5538 pw.print(prefix); pw.println("Package changes:");
5539 for (int i=0; i<changes.size(); i++) {
5540 PackageChange pc = changes.get(i);
5541 if (pc.mUpdate) {
5542 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5543 pw.print(" vers="); pw.println(pc.mVersionCode);
5544 } else {
5545 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5546 }
5547 }
5548 }
5549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 /**
5551 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5552 *
5553 * @param pw a Printer to receive the dump output.
5554 */
5555 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005556 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005557 prepareForDumpLocked();
5558
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005559 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005560 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005561
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005562 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005563 final long historyTotalSize = getHistoryTotalSize();
5564 final long historyUsedSize = getHistoryUsedSize();
5565 if (startIteratingHistoryLocked()) {
5566 try {
5567 pw.print("Battery History (");
5568 pw.print((100*historyUsedSize)/historyTotalSize);
5569 pw.print("% used, ");
5570 printSizeValue(pw, historyUsedSize);
5571 pw.print(" used of ");
5572 printSizeValue(pw, historyTotalSize);
5573 pw.print(", ");
5574 pw.print(getHistoryStringPoolSize());
5575 pw.print(" strings using ");
5576 printSizeValue(pw, getHistoryStringPoolBytes());
5577 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005578 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005579 pw.println();
5580 } finally {
5581 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005582 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005583 }
5584
5585 if (startIteratingOldHistoryLocked()) {
5586 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005587 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005588 pw.println("Old battery History:");
5589 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005590 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005591 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005592 if (baseTime < 0) {
5593 baseTime = rec.time;
5594 }
5595 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005596 }
5597 pw.println();
5598 } finally {
5599 finishIteratingOldHistoryLocked();
5600 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005601 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005602 }
5603
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005604 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005605 return;
5606 }
5607
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005608 if (!filtering) {
5609 SparseArray<? extends Uid> uidStats = getUidStats();
5610 final int NU = uidStats.size();
5611 boolean didPid = false;
5612 long nowRealtime = SystemClock.elapsedRealtime();
5613 for (int i=0; i<NU; i++) {
5614 Uid uid = uidStats.valueAt(i);
5615 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5616 if (pids != null) {
5617 for (int j=0; j<pids.size(); j++) {
5618 Uid.Pid pid = pids.valueAt(j);
5619 if (!didPid) {
5620 pw.println("Per-PID Stats:");
5621 didPid = true;
5622 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005623 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5624 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005625 pw.print(" PID "); pw.print(pids.keyAt(j));
5626 pw.print(" wake time: ");
5627 TimeUtils.formatDuration(time, pw);
5628 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005629 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005630 }
5631 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005632 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005633 pw.println();
5634 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005635 }
5636
5637 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005638 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5639 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005640 long timeRemaining = computeBatteryTimeRemaining(
5641 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005642 if (timeRemaining >= 0) {
5643 pw.print(" Estimated discharge time remaining: ");
5644 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5645 pw.println();
5646 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005647 final LevelStepTracker steps = getDischargeLevelStepTracker();
5648 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5649 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5650 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5651 STEP_LEVEL_MODE_VALUES[i], null));
5652 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005653 pw.println();
5654 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005655 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5656 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005657 long timeRemaining = computeChargeTimeRemaining(
5658 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005659 if (timeRemaining >= 0) {
5660 pw.print(" Estimated charge time remaining: ");
5661 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5662 pw.println();
5663 }
5664 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005665 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005666 }
5667 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5668 pw.println("Daily stats:");
5669 pw.print(" Current start time: ");
5670 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5671 getCurrentDailyStartTime()).toString());
5672 pw.print(" Next min deadline: ");
5673 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5674 getNextMinDailyDeadline()).toString());
5675 pw.print(" Next max deadline: ");
5676 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5677 getNextMaxDailyDeadline()).toString());
5678 StringBuilder sb = new StringBuilder(64);
5679 int[] outInt = new int[1];
5680 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5681 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005682 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5683 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005684 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005685 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5686 dsteps, false)) {
5687 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5688 sb, outInt);
5689 }
5690 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5691 csteps, false)) {
5692 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5693 sb, outInt);
5694 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005695 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005696 } else {
5697 pw.println(" Current daily steps:");
5698 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5699 sb, outInt);
5700 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5701 sb, outInt);
5702 }
5703 }
5704 DailyItem dit;
5705 int curIndex = 0;
5706 while ((dit=getDailyItemLocked(curIndex)) != null) {
5707 curIndex++;
5708 if ((flags&DUMP_DAILY_ONLY) != 0) {
5709 pw.println();
5710 }
5711 pw.print(" Daily from ");
5712 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5713 pw.print(" to ");
5714 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5715 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005716 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005717 if (dumpDurationSteps(pw, " ",
5718 " Discharge step durations:", dit.mDischargeSteps, false)) {
5719 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5720 sb, outInt);
5721 }
5722 if (dumpDurationSteps(pw, " ",
5723 " Charge step durations:", dit.mChargeSteps, false)) {
5724 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5725 sb, outInt);
5726 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005727 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005728 } else {
5729 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5730 sb, outInt);
5731 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5732 sb, outInt);
5733 }
5734 }
5735 pw.println();
5736 }
5737 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005738 pw.println("Statistics since last charge:");
5739 pw.println(" System starts: " + getStartCount()
5740 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005741 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5742 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005743 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 }
5746
5747 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005748 public void dumpCheckinLocked(Context context, PrintWriter pw,
5749 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005750 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005751
5752 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005753 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5754 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005755
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005756 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5757
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005758 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005759 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005760
5761 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005762 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005763 try {
5764 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5765 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5766 pw.print(HISTORY_STRING_POOL); pw.print(',');
5767 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005768 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005769 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005770 pw.print(",\"");
5771 String str = getHistoryTagPoolString(i);
5772 str = str.replace("\\", "\\\\");
5773 str = str.replace("\"", "\\\"");
5774 pw.print(str);
5775 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005776 pw.println();
5777 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005778 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005779 } finally {
5780 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005781 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005782 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005783 }
5784
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005785 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005786 return;
5787 }
5788
Dianne Hackborne4a59512010-12-07 11:08:07 -08005789 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005790 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005791 for (int i=0; i<apps.size(); i++) {
5792 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005793 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
5794 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005795 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005796 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
5797 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005798 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005799 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005800 }
5801 SparseArray<? extends Uid> uidStats = getUidStats();
5802 final int NU = uidStats.size();
5803 String[] lineArgs = new String[2];
5804 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005805 int uid = UserHandle.getAppId(uidStats.keyAt(i));
5806 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
5807 if (pkgs != null && !pkgs.second.value) {
5808 pkgs.second.value = true;
5809 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005810 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005811 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005812 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5813 (Object[])lineArgs);
5814 }
5815 }
5816 }
5817 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005818 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005819 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005820 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07005821 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005822 if (timeRemaining >= 0) {
5823 lineArgs[0] = Long.toString(timeRemaining);
5824 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5825 (Object[])lineArgs);
5826 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005827 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07005828 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005829 if (timeRemaining >= 0) {
5830 lineArgs[0] = Long.toString(timeRemaining);
5831 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5832 (Object[])lineArgs);
5833 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005834 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5835 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838}