blob: 8349510a14620091669af327af9172be2edfcf4d [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 Lesinskia7a4ccc2015-06-26 17:43:04 -0700597 * Get the total cpu power consumed (in milli-ampere-microseconds).
598 */
599 public abstract long getCpuPowerMaUs(int which);
600
601 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700602 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
603 * given CPU cluster.
604 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700605 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700606 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700607 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
608 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700609 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700610 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700611
Adam Lesinski5f056f62016-07-14 16:56:08 -0700612 /**
613 * Returns the number of times this UID woke up the Application Processor to
614 * process a mobile radio packet.
615 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
616 */
617 public abstract long getMobileRadioApWakeupCount(int which);
618
619 /**
620 * Returns the number of times this UID woke up the Application Processor to
621 * process a WiFi packet.
622 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
623 */
624 public abstract long getWifiRadioApWakeupCount(int which);
625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800627 /*
628 * FIXME: it's not correct to use this magic value because it
629 * could clash with a sensor handle (which are defined by
630 * the sensor HAL, and therefore out of our control
631 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 // Magic sensor number for the GPS.
633 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800638
Bookatz867c0d72017-03-07 18:23:42 -0800639 /** Returns a Timer for sensor usage when app is in the background. */
640 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
642
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700643 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800644 public int mWakeNesting;
645 public long mWakeSumMs;
646 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700647 }
648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 /**
650 * The statistics associated with a particular process.
651 */
652 public static abstract class Proc {
653
Dianne Hackborn287952c2010-09-22 22:34:31 -0700654 public static class ExcessivePower {
655 public static final int TYPE_WAKE = 1;
656 public static final int TYPE_CPU = 2;
657
658 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700659 public long overTime;
660 public long usedTime;
661 }
662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800664 * Returns true if this process is still active in the battery stats.
665 */
666 public abstract boolean isActive();
667
668 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700669 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700671 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 */
673 public abstract long getUserTime(int which);
674
675 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700676 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700678 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 */
680 public abstract long getSystemTime(int which);
681
682 /**
683 * Returns the number of times the process has been started.
684 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700685 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 */
687 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700688
689 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800690 * Returns the number of times the process has crashed.
691 *
692 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
693 */
694 public abstract int getNumCrashes(int which);
695
696 /**
697 * Returns the number of times the process has ANRed.
698 *
699 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
700 */
701 public abstract int getNumAnrs(int which);
702
703 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700704 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700705 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700706 * @return foreground cpu time in microseconds
707 */
708 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700709
Dianne Hackborn287952c2010-09-22 22:34:31 -0700710 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700711
Dianne Hackborn287952c2010-09-22 22:34:31 -0700712 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714
715 /**
716 * The statistics associated with a particular package.
717 */
718 public static abstract class Pkg {
719
720 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700721 * Returns information about all wakeup alarms that have been triggered for this
722 * package. The mapping keys are tag names for the alarms, the counter contains
723 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700725 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
727 /**
728 * Returns a mapping containing service statistics.
729 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700730 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731
732 /**
733 * The statistics associated with a particular service.
734 */
Joe Onoratoabded112016-02-08 16:49:39 -0800735 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736
737 /**
738 * Returns the amount of time spent started.
739 *
740 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700741 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 * @return
743 */
744 public abstract long getStartTime(long batteryUptime, int which);
745
746 /**
747 * Returns the total number of times startService() has been called.
748 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700749 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 */
751 public abstract int getStarts(int which);
752
753 /**
754 * Returns the total number times the service has been launched.
755 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700756 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 */
758 public abstract int getLaunches(int which);
759 }
760 }
761 }
762
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800763 public static final class LevelStepTracker {
764 public long mLastStepTime = -1;
765 public int mNumStepDurations;
766 public final long[] mStepDurations;
767
768 public LevelStepTracker(int maxLevelSteps) {
769 mStepDurations = new long[maxLevelSteps];
770 }
771
772 public LevelStepTracker(int numSteps, long[] steps) {
773 mNumStepDurations = numSteps;
774 mStepDurations = new long[numSteps];
775 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
776 }
777
778 public long getDurationAt(int index) {
779 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
780 }
781
782 public int getLevelAt(int index) {
783 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
784 >> STEP_LEVEL_LEVEL_SHIFT);
785 }
786
787 public int getInitModeAt(int index) {
788 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
789 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
790 }
791
792 public int getModModeAt(int index) {
793 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
794 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
795 }
796
797 private void appendHex(long val, int topOffset, StringBuilder out) {
798 boolean hasData = false;
799 while (topOffset >= 0) {
800 int digit = (int)( (val>>topOffset) & 0xf );
801 topOffset -= 4;
802 if (!hasData && digit == 0) {
803 continue;
804 }
805 hasData = true;
806 if (digit >= 0 && digit <= 9) {
807 out.append((char)('0' + digit));
808 } else {
809 out.append((char)('a' + digit - 10));
810 }
811 }
812 }
813
814 public void encodeEntryAt(int index, StringBuilder out) {
815 long item = mStepDurations[index];
816 long duration = item & STEP_LEVEL_TIME_MASK;
817 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
818 >> STEP_LEVEL_LEVEL_SHIFT);
819 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
820 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
821 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
822 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
823 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
824 case Display.STATE_OFF: out.append('f'); break;
825 case Display.STATE_ON: out.append('o'); break;
826 case Display.STATE_DOZE: out.append('d'); break;
827 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
828 }
829 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
830 out.append('p');
831 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700832 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
833 out.append('i');
834 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800835 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
836 case Display.STATE_OFF: out.append('F'); break;
837 case Display.STATE_ON: out.append('O'); break;
838 case Display.STATE_DOZE: out.append('D'); break;
839 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
840 }
841 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
842 out.append('P');
843 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700844 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
845 out.append('I');
846 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800847 out.append('-');
848 appendHex(level, 4, out);
849 out.append('-');
850 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
851 }
852
853 public void decodeEntryAt(int index, String value) {
854 final int N = value.length();
855 int i = 0;
856 char c;
857 long out = 0;
858 while (i < N && (c=value.charAt(i)) != '-') {
859 i++;
860 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800861 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800862 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800863 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800864 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800865 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800866 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800867 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
868 << STEP_LEVEL_INITIAL_MODE_SHIFT);
869 break;
870 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
871 << STEP_LEVEL_INITIAL_MODE_SHIFT);
872 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700873 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
874 << STEP_LEVEL_INITIAL_MODE_SHIFT);
875 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800876 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
877 break;
878 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
879 break;
880 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
881 break;
882 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
883 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
884 break;
885 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
886 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800887 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700888 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
889 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
890 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800891 }
892 }
893 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800894 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800895 while (i < N && (c=value.charAt(i)) != '-') {
896 i++;
897 level <<= 4;
898 if (c >= '0' && c <= '9') {
899 level += c - '0';
900 } else if (c >= 'a' && c <= 'f') {
901 level += c - 'a' + 10;
902 } else if (c >= 'A' && c <= 'F') {
903 level += c - 'A' + 10;
904 }
905 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800906 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800907 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
908 long duration = 0;
909 while (i < N && (c=value.charAt(i)) != '-') {
910 i++;
911 duration <<= 4;
912 if (c >= '0' && c <= '9') {
913 duration += c - '0';
914 } else if (c >= 'a' && c <= 'f') {
915 duration += c - 'a' + 10;
916 } else if (c >= 'A' && c <= 'F') {
917 duration += c - 'A' + 10;
918 }
919 }
920 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
921 }
922
923 public void init() {
924 mLastStepTime = -1;
925 mNumStepDurations = 0;
926 }
927
928 public void clearTime() {
929 mLastStepTime = -1;
930 }
931
932 public long computeTimePerLevel() {
933 final long[] steps = mStepDurations;
934 final int numSteps = mNumStepDurations;
935
936 // For now we'll do a simple average across all steps.
937 if (numSteps <= 0) {
938 return -1;
939 }
940 long total = 0;
941 for (int i=0; i<numSteps; i++) {
942 total += steps[i] & STEP_LEVEL_TIME_MASK;
943 }
944 return total / numSteps;
945 /*
946 long[] buckets = new long[numSteps];
947 int numBuckets = 0;
948 int numToAverage = 4;
949 int i = 0;
950 while (i < numSteps) {
951 long totalTime = 0;
952 int num = 0;
953 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
954 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
955 num++;
956 }
957 buckets[numBuckets] = totalTime / num;
958 numBuckets++;
959 numToAverage *= 2;
960 i += num;
961 }
962 if (numBuckets < 1) {
963 return -1;
964 }
965 long averageTime = buckets[numBuckets-1];
966 for (i=numBuckets-2; i>=0; i--) {
967 averageTime = (averageTime + buckets[i]) / 2;
968 }
969 return averageTime;
970 */
971 }
972
973 public long computeTimeEstimate(long modesOfInterest, long modeValues,
974 int[] outNumOfInterest) {
975 final long[] steps = mStepDurations;
976 final int count = mNumStepDurations;
977 if (count <= 0) {
978 return -1;
979 }
980 long total = 0;
981 int numOfInterest = 0;
982 for (int i=0; i<count; i++) {
983 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
984 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
985 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
986 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
987 // If the modes of interest didn't change during this step period...
988 if ((modMode&modesOfInterest) == 0) {
989 // And the mode values during this period match those we are measuring...
990 if ((initMode&modesOfInterest) == modeValues) {
991 // Then this can be used to estimate the total time!
992 numOfInterest++;
993 total += steps[i] & STEP_LEVEL_TIME_MASK;
994 }
995 }
996 }
997 if (numOfInterest <= 0) {
998 return -1;
999 }
1000
1001 if (outNumOfInterest != null) {
1002 outNumOfInterest[0] = numOfInterest;
1003 }
1004
1005 // The estimated time is the average time we spend in each level, multipled
1006 // by 100 -- the total number of battery levels
1007 return (total / numOfInterest) * 100;
1008 }
1009
1010 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1011 int stepCount = mNumStepDurations;
1012 final long lastStepTime = mLastStepTime;
1013 if (lastStepTime >= 0 && numStepLevels > 0) {
1014 final long[] steps = mStepDurations;
1015 long duration = elapsedRealtime - lastStepTime;
1016 for (int i=0; i<numStepLevels; i++) {
1017 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1018 long thisDuration = duration / (numStepLevels-i);
1019 duration -= thisDuration;
1020 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1021 thisDuration = STEP_LEVEL_TIME_MASK;
1022 }
1023 steps[0] = thisDuration | modeBits;
1024 }
1025 stepCount += numStepLevels;
1026 if (stepCount > steps.length) {
1027 stepCount = steps.length;
1028 }
1029 }
1030 mNumStepDurations = stepCount;
1031 mLastStepTime = elapsedRealtime;
1032 }
1033
1034 public void readFromParcel(Parcel in) {
1035 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001036 if (N > mStepDurations.length) {
1037 throw new ParcelFormatException("more step durations than available: " + N);
1038 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001039 mNumStepDurations = N;
1040 for (int i=0; i<N; i++) {
1041 mStepDurations[i] = in.readLong();
1042 }
1043 }
1044
1045 public void writeToParcel(Parcel out) {
1046 final int N = mNumStepDurations;
1047 out.writeInt(N);
1048 for (int i=0; i<N; i++) {
1049 out.writeLong(mStepDurations[i]);
1050 }
1051 }
1052 }
1053
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001054 public static final class PackageChange {
1055 public String mPackageName;
1056 public boolean mUpdate;
1057 public int mVersionCode;
1058 }
1059
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001060 public static final class DailyItem {
1061 public long mStartTime;
1062 public long mEndTime;
1063 public LevelStepTracker mDischargeSteps;
1064 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001065 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001066 }
1067
1068 public abstract DailyItem getDailyItemLocked(int daysAgo);
1069
1070 public abstract long getCurrentDailyStartTime();
1071
1072 public abstract long getNextMinDailyDeadline();
1073
1074 public abstract long getNextMaxDailyDeadline();
1075
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001076 public final static class HistoryTag {
1077 public String string;
1078 public int uid;
1079
1080 public int poolIdx;
1081
1082 public void setTo(HistoryTag o) {
1083 string = o.string;
1084 uid = o.uid;
1085 poolIdx = o.poolIdx;
1086 }
1087
1088 public void setTo(String _string, int _uid) {
1089 string = _string;
1090 uid = _uid;
1091 poolIdx = -1;
1092 }
1093
1094 public void writeToParcel(Parcel dest, int flags) {
1095 dest.writeString(string);
1096 dest.writeInt(uid);
1097 }
1098
1099 public void readFromParcel(Parcel src) {
1100 string = src.readString();
1101 uid = src.readInt();
1102 poolIdx = -1;
1103 }
1104
1105 @Override
1106 public boolean equals(Object o) {
1107 if (this == o) return true;
1108 if (o == null || getClass() != o.getClass()) return false;
1109
1110 HistoryTag that = (HistoryTag) o;
1111
1112 if (uid != that.uid) return false;
1113 if (!string.equals(that.string)) return false;
1114
1115 return true;
1116 }
1117
1118 @Override
1119 public int hashCode() {
1120 int result = string.hashCode();
1121 result = 31 * result + uid;
1122 return result;
1123 }
1124 }
1125
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001126 /**
1127 * Optional detailed information that can go into a history step. This is typically
1128 * generated each time the battery level changes.
1129 */
1130 public final static class HistoryStepDetails {
1131 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1132 public int userTime;
1133 public int systemTime;
1134
1135 // Top three apps using CPU in the last step, with times in 1/100 second.
1136 public int appCpuUid1;
1137 public int appCpuUTime1;
1138 public int appCpuSTime1;
1139 public int appCpuUid2;
1140 public int appCpuUTime2;
1141 public int appCpuSTime2;
1142 public int appCpuUid3;
1143 public int appCpuUTime3;
1144 public int appCpuSTime3;
1145
1146 // Information from /proc/stat
1147 public int statUserTime;
1148 public int statSystemTime;
1149 public int statIOWaitTime;
1150 public int statIrqTime;
1151 public int statSoftIrqTime;
1152 public int statIdlTime;
1153
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001154 // Platform-level low power state stats
1155 public String statPlatformIdleState;
1156
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001157 public HistoryStepDetails() {
1158 clear();
1159 }
1160
1161 public void clear() {
1162 userTime = systemTime = 0;
1163 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1164 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1165 = appCpuUTime3 = appCpuSTime3 = 0;
1166 }
1167
1168 public void writeToParcel(Parcel out) {
1169 out.writeInt(userTime);
1170 out.writeInt(systemTime);
1171 out.writeInt(appCpuUid1);
1172 out.writeInt(appCpuUTime1);
1173 out.writeInt(appCpuSTime1);
1174 out.writeInt(appCpuUid2);
1175 out.writeInt(appCpuUTime2);
1176 out.writeInt(appCpuSTime2);
1177 out.writeInt(appCpuUid3);
1178 out.writeInt(appCpuUTime3);
1179 out.writeInt(appCpuSTime3);
1180 out.writeInt(statUserTime);
1181 out.writeInt(statSystemTime);
1182 out.writeInt(statIOWaitTime);
1183 out.writeInt(statIrqTime);
1184 out.writeInt(statSoftIrqTime);
1185 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001186 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001187 }
1188
1189 public void readFromParcel(Parcel in) {
1190 userTime = in.readInt();
1191 systemTime = in.readInt();
1192 appCpuUid1 = in.readInt();
1193 appCpuUTime1 = in.readInt();
1194 appCpuSTime1 = in.readInt();
1195 appCpuUid2 = in.readInt();
1196 appCpuUTime2 = in.readInt();
1197 appCpuSTime2 = in.readInt();
1198 appCpuUid3 = in.readInt();
1199 appCpuUTime3 = in.readInt();
1200 appCpuSTime3 = in.readInt();
1201 statUserTime = in.readInt();
1202 statSystemTime = in.readInt();
1203 statIOWaitTime = in.readInt();
1204 statIrqTime = in.readInt();
1205 statSoftIrqTime = in.readInt();
1206 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001207 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001208 }
1209 }
1210
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001211 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001212 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001213
1214 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001215 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001216
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001217 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001218 public static final byte CMD_NULL = -1;
1219 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001220 public static final byte CMD_CURRENT_TIME = 5;
1221 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001222 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001223 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001224
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001225 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001226
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001227 /**
1228 * Return whether the command code is a delta data update.
1229 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001230 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001231 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001232 }
1233
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001234 public byte batteryLevel;
1235 public byte batteryStatus;
1236 public byte batteryHealth;
1237 public byte batteryPlugType;
1238
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001239 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001240 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001241
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001242 // The charge of the battery in micro-Ampere-hours.
1243 public int batteryChargeUAh;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001244
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001245 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001246 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001247 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001248 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001249 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1250 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001251 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001252 public static final int STATE_PHONE_STATE_SHIFT = 6;
1253 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001254 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001255 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001256 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001257
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001258 // These states always appear directly in the first int token
1259 // of a delta change; they should be ones that change relatively
1260 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001261 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1262 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001263 public static final int STATE_GPS_ON_FLAG = 1<<29;
1264 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001265 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001266 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001267 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001268 // Do not use, this is used for coulomb delta count.
1269 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001270 // These are on the lower bits used for the command; if they change
1271 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001272 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001273 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001274 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001275 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1276 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1277 // empty slot
1278 // empty slot
1279 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001280
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001281 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001282 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1283
1284 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001285
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001286 public int states;
1287
Dianne Hackborn3251b902014-06-20 14:40:53 -07001288 // Constants from WIFI_SUPPL_STATE_*
1289 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1290 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1291 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1292 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1293 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1294 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1295
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001296 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001297 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1298 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1299 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001300 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001301 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1302 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1303 public static final int STATE2_CHARGING_FLAG = 1<<24;
1304 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1305 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1306 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001307 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001308
1309 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001310 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001311 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1312
1313 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001314
Dianne Hackborn40c87252014-03-19 16:55:40 -07001315 public int states2;
1316
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001317 // The wake lock that was acquired at this point.
1318 public HistoryTag wakelockTag;
1319
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001320 // Kernel wakeup reason at this point.
1321 public HistoryTag wakeReasonTag;
1322
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001323 // Non-null when there is more detailed information at this step.
1324 public HistoryStepDetails stepDetails;
1325
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001326 public static final int EVENT_FLAG_START = 0x8000;
1327 public static final int EVENT_FLAG_FINISH = 0x4000;
1328
1329 // No event in this item.
1330 public static final int EVENT_NONE = 0x0000;
1331 // Event is about a process that is running.
1332 public static final int EVENT_PROC = 0x0001;
1333 // Event is about an application package that is in the foreground.
1334 public static final int EVENT_FOREGROUND = 0x0002;
1335 // Event is about an application package that is at the top of the screen.
1336 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001337 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001338 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001339 // Events for all additional wake locks aquired/release within a wake block.
1340 // These are not generated by default.
1341 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001342 // Event is about an application executing a scheduled job.
1343 public static final int EVENT_JOB = 0x0006;
1344 // Events for users running.
1345 public static final int EVENT_USER_RUNNING = 0x0007;
1346 // Events for foreground user.
1347 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001348 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001349 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001350 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001351 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001352 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001353 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001354 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001355 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001356 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001357 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001358 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001359 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001360 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001361 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001362 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001363 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001364 // Event for a package being on the temporary whitelist.
1365 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001366 // Event for the screen waking up.
1367 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001368 // Event for the UID that woke up the application processor.
1369 // Used for wakeups coming from WiFi, modem, etc.
1370 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001371 // Event for reporting that a specific partial wake lock has been held for a long duration.
1372 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Adam Lesinski041d9172016-12-12 12:03:56 -08001373 // Event reporting the new estimated (learned) capacity of the battery in mAh.
1374 public static final int EVENT_ESTIMATED_BATTERY_CAP = 0x0015;
Amith Yamasani67768492015-06-09 12:23:58 -07001375
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001376 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001377 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001378 // Mask to extract out only the type part of the event.
1379 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001380
1381 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1382 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1383 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1384 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1385 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1386 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001387 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1388 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001389 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1390 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001391 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1392 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1393 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1394 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1395 public static final int EVENT_USER_FOREGROUND_START =
1396 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1397 public static final int EVENT_USER_FOREGROUND_FINISH =
1398 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001399 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1400 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001401 public static final int EVENT_TEMP_WHITELIST_START =
1402 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1403 public static final int EVENT_TEMP_WHITELIST_FINISH =
1404 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001405 public static final int EVENT_LONG_WAKE_LOCK_START =
1406 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1407 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1408 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001409
1410 // For CMD_EVENT.
1411 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001412 public HistoryTag eventTag;
1413
Dianne Hackborn9a755432014-05-15 17:05:22 -07001414 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001415 public long currentTime;
1416
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001417 // Meta-data when reading.
1418 public int numReadInts;
1419
1420 // Pre-allocated objects.
1421 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001422 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001423 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001424
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001425 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001426 }
1427
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001428 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001429 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001430 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001431 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001432 }
1433
1434 public int describeContents() {
1435 return 0;
1436 }
1437
1438 public void writeToParcel(Parcel dest, int flags) {
1439 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001440 int bat = (((int)cmd)&0xff)
1441 | ((((int)batteryLevel)<<8)&0xff00)
1442 | ((((int)batteryStatus)<<16)&0xf0000)
1443 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001444 | ((((int)batteryPlugType)<<24)&0xf000000)
1445 | (wakelockTag != null ? 0x10000000 : 0)
1446 | (wakeReasonTag != null ? 0x20000000 : 0)
1447 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001448 dest.writeInt(bat);
1449 bat = (((int)batteryTemperature)&0xffff)
1450 | ((((int)batteryVoltage)<<16)&0xffff0000);
1451 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001452 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001453 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001454 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001455 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001456 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001457 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001458 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001459 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001460 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001461 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001462 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001463 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001464 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001465 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001466 dest.writeLong(currentTime);
1467 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001468 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001469
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001470 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001471 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001472 int bat = src.readInt();
1473 cmd = (byte)(bat&0xff);
1474 batteryLevel = (byte)((bat>>8)&0xff);
1475 batteryStatus = (byte)((bat>>16)&0xf);
1476 batteryHealth = (byte)((bat>>20)&0xf);
1477 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001478 int bat2 = src.readInt();
1479 batteryTemperature = (short)(bat2&0xffff);
1480 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001481 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001482 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001483 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001484 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001485 wakelockTag = localWakelockTag;
1486 wakelockTag.readFromParcel(src);
1487 } else {
1488 wakelockTag = null;
1489 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001490 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001491 wakeReasonTag = localWakeReasonTag;
1492 wakeReasonTag.readFromParcel(src);
1493 } else {
1494 wakeReasonTag = null;
1495 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001496 if ((bat&0x40000000) != 0) {
1497 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001498 eventTag = localEventTag;
1499 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001500 } else {
1501 eventCode = EVENT_NONE;
1502 eventTag = null;
1503 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001504 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001505 currentTime = src.readLong();
1506 } else {
1507 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001508 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001509 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001510 }
1511
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001512 public void clear() {
1513 time = 0;
1514 cmd = CMD_NULL;
1515 batteryLevel = 0;
1516 batteryStatus = 0;
1517 batteryHealth = 0;
1518 batteryPlugType = 0;
1519 batteryTemperature = 0;
1520 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001521 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001522 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001523 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001524 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001525 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001526 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001527 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001528 }
1529
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001530 public void setTo(HistoryItem o) {
1531 time = o.time;
1532 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001533 setToCommon(o);
1534 }
1535
1536 public void setTo(long time, byte cmd, HistoryItem o) {
1537 this.time = time;
1538 this.cmd = cmd;
1539 setToCommon(o);
1540 }
1541
1542 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001543 batteryLevel = o.batteryLevel;
1544 batteryStatus = o.batteryStatus;
1545 batteryHealth = o.batteryHealth;
1546 batteryPlugType = o.batteryPlugType;
1547 batteryTemperature = o.batteryTemperature;
1548 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001549 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001550 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001551 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001552 if (o.wakelockTag != null) {
1553 wakelockTag = localWakelockTag;
1554 wakelockTag.setTo(o.wakelockTag);
1555 } else {
1556 wakelockTag = null;
1557 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001558 if (o.wakeReasonTag != null) {
1559 wakeReasonTag = localWakeReasonTag;
1560 wakeReasonTag.setTo(o.wakeReasonTag);
1561 } else {
1562 wakeReasonTag = null;
1563 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001564 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001565 if (o.eventTag != null) {
1566 eventTag = localEventTag;
1567 eventTag.setTo(o.eventTag);
1568 } else {
1569 eventTag = null;
1570 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001571 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001572 }
1573
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001574 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001575 return batteryLevel == o.batteryLevel
1576 && batteryStatus == o.batteryStatus
1577 && batteryHealth == o.batteryHealth
1578 && batteryPlugType == o.batteryPlugType
1579 && batteryTemperature == o.batteryTemperature
1580 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001581 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001582 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001583 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001584 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001585 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001586
1587 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001588 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001589 return false;
1590 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001591 if (wakelockTag != o.wakelockTag) {
1592 if (wakelockTag == null || o.wakelockTag == null) {
1593 return false;
1594 }
1595 if (!wakelockTag.equals(o.wakelockTag)) {
1596 return false;
1597 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001598 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001599 if (wakeReasonTag != o.wakeReasonTag) {
1600 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1601 return false;
1602 }
1603 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1604 return false;
1605 }
1606 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001607 if (eventTag != o.eventTag) {
1608 if (eventTag == null || o.eventTag == null) {
1609 return false;
1610 }
1611 if (!eventTag.equals(o.eventTag)) {
1612 return false;
1613 }
1614 }
1615 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001616 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001617 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001618
1619 public final static class HistoryEventTracker {
1620 private final HashMap<String, SparseIntArray>[] mActiveEvents
1621 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1622
1623 public boolean updateState(int code, String name, int uid, int poolIdx) {
1624 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1625 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1626 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1627 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001628 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001629 mActiveEvents[idx] = active;
1630 }
1631 SparseIntArray uids = active.get(name);
1632 if (uids == null) {
1633 uids = new SparseIntArray();
1634 active.put(name, uids);
1635 }
1636 if (uids.indexOfKey(uid) >= 0) {
1637 // Already set, nothing to do!
1638 return false;
1639 }
1640 uids.put(uid, poolIdx);
1641 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1642 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1643 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1644 if (active == null) {
1645 // not currently active, nothing to do.
1646 return false;
1647 }
1648 SparseIntArray uids = active.get(name);
1649 if (uids == null) {
1650 // not currently active, nothing to do.
1651 return false;
1652 }
1653 idx = uids.indexOfKey(uid);
1654 if (idx < 0) {
1655 // not currently active, nothing to do.
1656 return false;
1657 }
1658 uids.removeAt(idx);
1659 if (uids.size() <= 0) {
1660 active.remove(name);
1661 }
1662 }
1663 return true;
1664 }
1665
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001666 public void removeEvents(int code) {
1667 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1668 mActiveEvents[idx] = null;
1669 }
1670
Dianne Hackborn37de0982014-05-09 09:32:18 -07001671 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1672 return mActiveEvents[code];
1673 }
1674 }
1675
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001676 public static final class BitDescription {
1677 public final int mask;
1678 public final int shift;
1679 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001680 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001681 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001682 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001683
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001684 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001685 this.mask = mask;
1686 this.shift = -1;
1687 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001688 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001689 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001690 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001691 }
1692
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001693 public BitDescription(int mask, int shift, String name, String shortName,
1694 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001695 this.mask = mask;
1696 this.shift = shift;
1697 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001698 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001699 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001700 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001701 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001702 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001703
Dianne Hackbornfc064132014-06-02 12:42:12 -07001704 /**
1705 * Don't allow any more batching in to the current history event. This
1706 * is called when printing partial histories, so to ensure that the next
1707 * history event will go in to a new batch after what was printed in the
1708 * last partial history.
1709 */
1710 public abstract void commitCurrentHistoryBatchLocked();
1711
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001712 public abstract int getHistoryTotalSize();
1713
1714 public abstract int getHistoryUsedSize();
1715
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001716 public abstract boolean startIteratingHistoryLocked();
1717
Dianne Hackborn099bc622014-01-22 13:39:16 -08001718 public abstract int getHistoryStringPoolSize();
1719
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001720 public abstract int getHistoryStringPoolBytes();
1721
1722 public abstract String getHistoryTagPoolString(int index);
1723
1724 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001725
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001726 public abstract boolean getNextHistoryLocked(HistoryItem out);
1727
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001728 public abstract void finishIteratingHistoryLocked();
1729
1730 public abstract boolean startIteratingOldHistoryLocked();
1731
1732 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1733
1734 public abstract void finishIteratingOldHistoryLocked();
1735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001737 * Return the base time offset for the battery history.
1738 */
1739 public abstract long getHistoryBaseTime();
1740
1741 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 * Returns the number of times the device has been started.
1743 */
1744 public abstract int getStartCount();
1745
1746 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001747 * 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 -08001748 * running on battery.
1749 *
1750 * {@hide}
1751 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001752 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001754 /**
1755 * Returns the number of times the screen was turned on.
1756 *
1757 * {@hide}
1758 */
1759 public abstract int getScreenOnCount(int which);
1760
Jeff Browne95c3cd2014-05-02 16:59:26 -07001761 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1762
Dianne Hackborn617f8772009-03-31 15:04:46 -07001763 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1764 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1765 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1766 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1767 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1768
1769 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1770 "dark", "dim", "medium", "light", "bright"
1771 };
1772
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001773 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1774 "0", "1", "2", "3", "4"
1775 };
1776
Dianne Hackborn617f8772009-03-31 15:04:46 -07001777 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001778
Dianne Hackborn617f8772009-03-31 15:04:46 -07001779 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001780 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001781 * the given brightness
1782 *
1783 * {@hide}
1784 */
1785 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001786 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001789 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001790 * running on battery.
1791 *
1792 * {@hide}
1793 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001794 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001795
1796 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001797 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001798 *
1799 * {@hide}
1800 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001801 public abstract int getPowerSaveModeEnabledCount(int which);
1802
1803 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001804 * Constant for device idle mode: not active.
1805 */
1806 public static final int DEVICE_IDLE_MODE_OFF = 0;
1807
1808 /**
1809 * Constant for device idle mode: active in lightweight mode.
1810 */
1811 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1812
1813 /**
1814 * Constant for device idle mode: active in full mode.
1815 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001816 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001817
1818 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001819 * Returns the time in microseconds that device has been in idle mode while
1820 * running on battery.
1821 *
1822 * {@hide}
1823 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001824 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001825
1826 /**
1827 * Returns the number of times that the devie has gone in to idle mode.
1828 *
1829 * {@hide}
1830 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001831 public abstract int getDeviceIdleModeCount(int mode, int which);
1832
1833 /**
1834 * Return the longest duration we spent in a particular device idle mode (fully in the
1835 * mode, not in idle maintenance etc).
1836 */
1837 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001838
1839 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001840 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001841 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001842 * counts all of the time that we consider the device to be idle, whether or not
1843 * it is currently in the actual device idle mode.
1844 *
1845 * {@hide}
1846 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001847 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001848
1849 /**
1850 * Returns the number of times that the devie has started idling.
1851 *
1852 * {@hide}
1853 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001854 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001855
1856 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001857 * Returns the number of times that connectivity state changed.
1858 *
1859 * {@hide}
1860 */
1861 public abstract int getNumConnectivityChange(int which);
1862
1863 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001864 * 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 -08001865 * running on battery.
1866 *
1867 * {@hide}
1868 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001869 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001870
1871 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001872 * Returns the number of times a phone call was activated.
1873 *
1874 * {@hide}
1875 */
1876 public abstract int getPhoneOnCount(int which);
1877
1878 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001879 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001880 * the given signal strength.
1881 *
1882 * {@hide}
1883 */
1884 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001885 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001886
Dianne Hackborn617f8772009-03-31 15:04:46 -07001887 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001888 * Returns the time in microseconds that the phone has been trying to
1889 * acquire a signal.
1890 *
1891 * {@hide}
1892 */
1893 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001894 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001895
1896 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001897 * Returns the number of times the phone has entered the given signal strength.
1898 *
1899 * {@hide}
1900 */
1901 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1902
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001903 /**
1904 * Returns the time in microseconds that the mobile network has been active
1905 * (in a high power state).
1906 *
1907 * {@hide}
1908 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001909 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001910
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001911 /**
1912 * Returns the number of times that the mobile network has transitioned to the
1913 * active state.
1914 *
1915 * {@hide}
1916 */
1917 public abstract int getMobileRadioActiveCount(int which);
1918
1919 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001920 * Returns the time in microseconds that is the difference between the mobile radio
1921 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1922 * from the radio.
1923 *
1924 * {@hide}
1925 */
1926 public abstract long getMobileRadioActiveAdjustedTime(int which);
1927
1928 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001929 * Returns the time in microseconds that the mobile network has been active
1930 * (in a high power state) but not being able to blame on an app.
1931 *
1932 * {@hide}
1933 */
1934 public abstract long getMobileRadioActiveUnknownTime(int which);
1935
1936 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001937 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001938 *
1939 * {@hide}
1940 */
1941 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001942
Dianne Hackborn627bba72009-03-24 22:32:56 -07001943 public static final int DATA_CONNECTION_NONE = 0;
1944 public static final int DATA_CONNECTION_GPRS = 1;
1945 public static final int DATA_CONNECTION_EDGE = 2;
1946 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001947 public static final int DATA_CONNECTION_CDMA = 4;
1948 public static final int DATA_CONNECTION_EVDO_0 = 5;
1949 public static final int DATA_CONNECTION_EVDO_A = 6;
1950 public static final int DATA_CONNECTION_1xRTT = 7;
1951 public static final int DATA_CONNECTION_HSDPA = 8;
1952 public static final int DATA_CONNECTION_HSUPA = 9;
1953 public static final int DATA_CONNECTION_HSPA = 10;
1954 public static final int DATA_CONNECTION_IDEN = 11;
1955 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001956 public static final int DATA_CONNECTION_LTE = 13;
1957 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001958 public static final int DATA_CONNECTION_HSPAP = 15;
1959 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001960
Dianne Hackborn627bba72009-03-24 22:32:56 -07001961 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001962 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001963 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001964 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001965 };
1966
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001967 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001968
1969 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001970 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001971 * the given data connection.
1972 *
1973 * {@hide}
1974 */
1975 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001976 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001979 * Returns the number of times the phone has entered the given data
1980 * connection type.
1981 *
1982 * {@hide}
1983 */
1984 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001985
Dianne Hackborn3251b902014-06-20 14:40:53 -07001986 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1987 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1988 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1989 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1990 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1991 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1992 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1993 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1994 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1995 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1996 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1997 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1998 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1999
2000 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2001
2002 static final String[] WIFI_SUPPL_STATE_NAMES = {
2003 "invalid", "disconn", "disabled", "inactive", "scanning",
2004 "authenticating", "associating", "associated", "4-way-handshake",
2005 "group-handshake", "completed", "dormant", "uninit"
2006 };
2007
2008 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2009 "inv", "dsc", "dis", "inact", "scan",
2010 "auth", "ascing", "asced", "4-way",
2011 "group", "compl", "dorm", "uninit"
2012 };
2013
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002014 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2015 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002016 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002017 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2018 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002019 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002020 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2021 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2022 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002023 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002024 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002025 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002026 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002027 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2028 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002029 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2030 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2031 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2032 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2033 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2034 new String[] {"in", "out", "emergency", "off"},
2035 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002036 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2037 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2038 SignalStrength.SIGNAL_STRENGTH_NAMES,
2039 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002040 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2041 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2042 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002043 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002044
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002045 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2046 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002047 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002048 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002049 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002050 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002051 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002052 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2053 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2054 new String[] { "off", "light", "full", "???" },
2055 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002056 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2057 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2058 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002059 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2060 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2061 new String[] { "0", "1", "2", "3", "4" },
2062 new String[] { "0", "1", "2", "3", "4" }),
2063 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2064 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2065 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002066 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002067 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002068 };
2069
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002070 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002071 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002072 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2073 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002074 };
2075
2076 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002077 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002078 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002079 "Esw", "Ewa", "Elw", "Eec"
2080 };
2081
2082 @FunctionalInterface
2083 public interface IntToString {
2084 String applyAsString(int val);
2085 }
2086
2087 private static final IntToString sUidToString = UserHandle::formatUid;
2088 private static final IntToString sIntToString = Integer::toString;
2089
2090 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2091 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2092 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2093 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2094 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002095 };
2096
Dianne Hackborn617f8772009-03-31 15:04:46 -07002097 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002098 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002099 * running on battery.
2100 *
2101 * {@hide}
2102 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002103 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002104
2105 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002106 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002107 * been in the running state while the device was running on battery.
2108 *
2109 * {@hide}
2110 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002111 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002112
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002113 public static final int WIFI_STATE_OFF = 0;
2114 public static final int WIFI_STATE_OFF_SCANNING = 1;
2115 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2116 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2117 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2118 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2119 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2120 public static final int WIFI_STATE_SOFT_AP = 7;
2121
2122 static final String[] WIFI_STATE_NAMES = {
2123 "off", "scanning", "no_net", "disconn",
2124 "sta", "p2p", "sta_p2p", "soft_ap"
2125 };
2126
2127 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2128
2129 /**
2130 * Returns the time in microseconds that WiFi has been running in the given state.
2131 *
2132 * {@hide}
2133 */
2134 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002135 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002136
2137 /**
2138 * Returns the number of times that WiFi has entered the given state.
2139 *
2140 * {@hide}
2141 */
2142 public abstract int getWifiStateCount(int wifiState, int which);
2143
The Android Open Source Project10592532009-03-18 17:39:46 -07002144 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002145 * Returns the time in microseconds that the wifi supplicant has been
2146 * in a given state.
2147 *
2148 * {@hide}
2149 */
2150 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2151
2152 /**
2153 * Returns the number of times that the wifi supplicant has transitioned
2154 * to a given state.
2155 *
2156 * {@hide}
2157 */
2158 public abstract int getWifiSupplStateCount(int state, int which);
2159
2160 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2161
2162 /**
2163 * Returns the time in microseconds that WIFI has been running with
2164 * the given signal strength.
2165 *
2166 * {@hide}
2167 */
2168 public abstract long getWifiSignalStrengthTime(int strengthBin,
2169 long elapsedRealtimeUs, int which);
2170
2171 /**
2172 * Returns the number of times WIFI has entered the given signal strength.
2173 *
2174 * {@hide}
2175 */
2176 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2177
2178 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002179 * Returns the time in microseconds that the flashlight has been on while the device was
2180 * running on battery.
2181 *
2182 * {@hide}
2183 */
2184 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2185
2186 /**
2187 * Returns the number of times that the flashlight has been turned on while the device was
2188 * running on battery.
2189 *
2190 * {@hide}
2191 */
2192 public abstract long getFlashlightOnCount(int which);
2193
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002194 /**
2195 * Returns the time in microseconds that the camera has been on while the device was
2196 * running on battery.
2197 *
2198 * {@hide}
2199 */
2200 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2201
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002202 /**
2203 * Returns the time in microseconds that bluetooth scans were running while the device was
2204 * on battery.
2205 *
2206 * {@hide}
2207 */
2208 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002209
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002210 public static final int NETWORK_MOBILE_RX_DATA = 0;
2211 public static final int NETWORK_MOBILE_TX_DATA = 1;
2212 public static final int NETWORK_WIFI_RX_DATA = 2;
2213 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002214 public static final int NETWORK_BT_RX_DATA = 4;
2215 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002216 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2217 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2218 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2219 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2220 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002221
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002222 public abstract long getNetworkActivityBytes(int type, int which);
2223 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002224
Adam Lesinskie08af192015-03-25 16:42:59 -07002225 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002226 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002227 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002228 * actual power data.
2229 */
2230 public abstract boolean hasWifiActivityReporting();
2231
2232 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002233 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2234 * in various radio controller states, such as transmit, receive, and idle.
2235 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002236 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002237 public abstract ControllerActivityCounter getWifiControllerActivity();
2238
2239 /**
2240 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2241 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2242 * actual power data.
2243 */
2244 public abstract boolean hasBluetoothActivityReporting();
2245
2246 /**
2247 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2248 * in various radio controller states, such as transmit, receive, and idle.
2249 * @return non-null {@link ControllerActivityCounter}
2250 */
2251 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2252
2253 /**
2254 * Returns true if the BatteryStats object has detailed modem power reports.
2255 * When true, calling {@link #getModemControllerActivity()} will yield the
2256 * actual power data.
2257 */
2258 public abstract boolean hasModemActivityReporting();
2259
2260 /**
2261 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2262 * in various radio controller states, such as transmit, receive, and idle.
2263 * @return non-null {@link ControllerActivityCounter}
2264 */
2265 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002266
The Android Open Source Project10592532009-03-18 17:39:46 -07002267 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002268 * Return the wall clock time when battery stats data collection started.
2269 */
2270 public abstract long getStartClockTime();
2271
2272 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002273 * Return platform version tag that we were running in when the battery stats started.
2274 */
2275 public abstract String getStartPlatformVersion();
2276
2277 /**
2278 * Return platform version tag that we were running in when the battery stats ended.
2279 */
2280 public abstract String getEndPlatformVersion();
2281
2282 /**
2283 * Return the internal version code of the parcelled format.
2284 */
2285 public abstract int getParcelVersion();
2286
2287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 * Return whether we are currently running on battery.
2289 */
2290 public abstract boolean getIsOnBattery();
2291
2292 /**
2293 * Returns a SparseArray containing the statistics for each uid.
2294 */
2295 public abstract SparseArray<? extends Uid> getUidStats();
2296
2297 /**
2298 * Returns the current battery uptime in microseconds.
2299 *
2300 * @param curTime the amount of elapsed realtime in microseconds.
2301 */
2302 public abstract long getBatteryUptime(long curTime);
2303
2304 /**
2305 * Returns the current battery realtime in microseconds.
2306 *
2307 * @param curTime the amount of elapsed realtime in microseconds.
2308 */
2309 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002310
2311 /**
Evan Millar633a1742009-04-02 16:36:33 -07002312 * Returns the battery percentage level at the last time the device was unplugged from power, or
2313 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002314 */
Evan Millar633a1742009-04-02 16:36:33 -07002315 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002316
2317 /**
Evan Millar633a1742009-04-02 16:36:33 -07002318 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2319 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002320 */
Evan Millar633a1742009-04-02 16:36:33 -07002321 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322
2323 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002324 * Get the amount the battery has discharged since the stats were
2325 * last reset after charging, as a lower-end approximation.
2326 */
2327 public abstract int getLowDischargeAmountSinceCharge();
2328
2329 /**
2330 * Get the amount the battery has discharged since the stats were
2331 * last reset after charging, as an upper-end approximation.
2332 */
2333 public abstract int getHighDischargeAmountSinceCharge();
2334
2335 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002336 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2337 */
2338 public abstract int getDischargeAmount(int which);
2339
2340 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002341 * Get the amount the battery has discharged while the screen was on,
2342 * since the last time power was unplugged.
2343 */
2344 public abstract int getDischargeAmountScreenOn();
2345
2346 /**
2347 * Get the amount the battery has discharged while the screen was on,
2348 * since the last time the device was charged.
2349 */
2350 public abstract int getDischargeAmountScreenOnSinceCharge();
2351
2352 /**
2353 * Get the amount the battery has discharged while the screen was off,
2354 * since the last time power was unplugged.
2355 */
2356 public abstract int getDischargeAmountScreenOff();
2357
2358 /**
2359 * Get the amount the battery has discharged while the screen was off,
2360 * since the last time the device was charged.
2361 */
2362 public abstract int getDischargeAmountScreenOffSinceCharge();
2363
2364 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 * Returns the total, last, or current battery uptime in microseconds.
2366 *
2367 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002368 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 */
2370 public abstract long computeBatteryUptime(long curTime, int which);
2371
2372 /**
2373 * Returns the total, last, or current battery realtime in microseconds.
2374 *
2375 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002376 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 */
2378 public abstract long computeBatteryRealtime(long curTime, int which);
2379
2380 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002381 * Returns the total, last, or current battery screen off uptime in microseconds.
2382 *
2383 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002384 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002385 */
2386 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2387
2388 /**
2389 * Returns the total, last, or current battery screen off realtime in microseconds.
2390 *
2391 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002392 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002393 */
2394 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2395
2396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 * Returns the total, last, or current uptime in microseconds.
2398 *
2399 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002400 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 */
2402 public abstract long computeUptime(long curTime, int which);
2403
2404 /**
2405 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002406 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002408 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 */
2410 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002411
2412 /**
2413 * Compute an approximation for how much run time (in microseconds) is remaining on
2414 * the battery. Returns -1 if no time can be computed: either there is not
2415 * enough current data to make a decision, or the battery is currently
2416 * charging.
2417 *
2418 * @param curTime The current elepsed realtime in microseconds.
2419 */
2420 public abstract long computeBatteryTimeRemaining(long curTime);
2421
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002422 // The part of a step duration that is the actual time.
2423 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2424
2425 // Bits in a step duration that are the new battery level we are at.
2426 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002427 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002428
2429 // Bits in a step duration that are the initial mode we were in at that step.
2430 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002431 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002432
2433 // Bits in a step duration that indicate which modes changed during that step.
2434 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002435 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002436
2437 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2438 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2439
Santos Cordone94f0502017-02-24 12:31:20 -08002440 // The largest value for screen state that is tracked in battery states. Any values above
2441 // this should be mapped back to one of the tracked values before being tracked here.
2442 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2443
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002444 // Step duration mode: power save is on.
2445 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2446
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002447 // Step duration mode: device is currently in idle mode.
2448 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2449
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002450 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2451 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002452 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2453 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002454 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2455 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2456 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2457 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2458 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002459 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2460 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002461 };
2462 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2463 (Display.STATE_OFF-1),
2464 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002465 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002466 (Display.STATE_ON-1),
2467 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2468 (Display.STATE_DOZE-1),
2469 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2470 (Display.STATE_DOZE_SUSPEND-1),
2471 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002472 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002473 };
2474 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2475 "screen off",
2476 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002477 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002478 "screen on",
2479 "screen on power save",
2480 "screen doze",
2481 "screen doze power save",
2482 "screen doze-suspend",
2483 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002484 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002485 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002486
2487 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002488 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2489 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2490 * a coulomb counter.
2491 */
2492 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2493
2494 /**
2495 * Return the counter keeping track of the amount of battery discharge measured in
2496 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2497 * a coulomb counter.
2498 */
2499 public abstract LongCounter getDischargeCoulombCounter();
2500
2501 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002502 * Returns the estimated real battery capacity, which may be less than the capacity
2503 * declared by the PowerProfile.
2504 * @return The estimated battery capacity in mAh.
2505 */
2506 public abstract int getEstimatedBatteryCapacity();
2507
2508 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002509 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002510 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002511 public abstract LevelStepTracker getDischargeLevelStepTracker();
2512
2513 /**
2514 * Return the array of daily discharge step durations.
2515 */
2516 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002517
2518 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002519 * Compute an approximation for how much time (in microseconds) remains until the battery
2520 * is fully charged. Returns -1 if no time can be computed: either there is not
2521 * enough current data to make a decision, or the battery is currently
2522 * discharging.
2523 *
2524 * @param curTime The current elepsed realtime in microseconds.
2525 */
2526 public abstract long computeChargeTimeRemaining(long curTime);
2527
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002528 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002529 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002530 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002531 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002532
2533 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002534 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002535 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002536 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002537
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002538 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2539
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002540 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002541
Evan Millarc64edde2009-04-18 12:26:32 -07002542 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543
James Carr2dd7e5e2016-07-20 18:48:39 -07002544 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2545
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002546 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2547
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002548 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 long days = seconds / (60 * 60 * 24);
2550 if (days != 0) {
2551 out.append(days);
2552 out.append("d ");
2553 }
2554 long used = days * 60 * 60 * 24;
2555
2556 long hours = (seconds - used) / (60 * 60);
2557 if (hours != 0 || used != 0) {
2558 out.append(hours);
2559 out.append("h ");
2560 }
2561 used += hours * 60 * 60;
2562
2563 long mins = (seconds-used) / 60;
2564 if (mins != 0 || used != 0) {
2565 out.append(mins);
2566 out.append("m ");
2567 }
2568 used += mins * 60;
2569
2570 if (seconds != 0 || used != 0) {
2571 out.append(seconds-used);
2572 out.append("s ");
2573 }
2574 }
2575
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002576 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002578 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 sb.append(time - (sec * 1000));
2580 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 }
2582
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002583 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002584 long sec = time / 1000;
2585 formatTimeRaw(sb, sec);
2586 sb.append(time - (sec * 1000));
2587 sb.append("ms");
2588 }
2589
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002590 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002592 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 }
2594 float perc = ((float)num) / ((float)den) * 100;
2595 mFormatBuilder.setLength(0);
2596 mFormatter.format("%.1f%%", perc);
2597 return mFormatBuilder.toString();
2598 }
2599
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002600 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002601 mFormatBuilder.setLength(0);
2602
2603 if (bytes < BYTES_PER_KB) {
2604 return bytes + "B";
2605 } else if (bytes < BYTES_PER_MB) {
2606 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2607 return mFormatBuilder.toString();
2608 } else if (bytes < BYTES_PER_GB){
2609 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2610 return mFormatBuilder.toString();
2611 } else {
2612 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2613 return mFormatBuilder.toString();
2614 }
2615 }
2616
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002617 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002618 if (timer != null) {
2619 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002620 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002621 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2622 return totalTimeMillis;
2623 }
2624 return 0;
2625 }
2626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 /**
2628 *
2629 * @param sb a StringBuilder object.
2630 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002631 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002633 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 * @param linePrefix a String to be prepended to each line of output.
2635 * @return the line prefix
2636 */
2637 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002638 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002641 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002642
Evan Millarc64edde2009-04-18 12:26:32 -07002643 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 if (totalTimeMillis != 0) {
2645 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002646 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002647 if (name != null) {
2648 sb.append(name);
2649 sb.append(' ');
2650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 sb.append('(');
2652 sb.append(count);
2653 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002654 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2655 if (maxDurationMs >= 0) {
2656 sb.append(" max=");
2657 sb.append(maxDurationMs);
2658 }
2659 if (timer.isRunningLocked()) {
2660 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2661 if (currentMs >= 0) {
2662 sb.append(" (running for ");
2663 sb.append(currentMs);
2664 sb.append("ms)");
2665 } else {
2666 sb.append(" (running)");
2667 }
2668 }
2669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 return ", ";
2671 }
2672 }
2673 return linePrefix;
2674 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002675
2676 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002677 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002678 *
2679 * @param pw a PrintWriter object to print to.
2680 * @param sb a StringBuilder object.
2681 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002682 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002683 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2684 * @param prefix a String to be prepended to each line of output.
2685 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002686 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002687 */
2688 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002689 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002690 if (timer != null) {
2691 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002692 final long totalTimeMs = (timer.getTotalTimeLocked(
2693 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002694 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002695 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002696 sb.setLength(0);
2697 sb.append(prefix);
2698 sb.append(" ");
2699 sb.append(type);
2700 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002701 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002702 sb.append("realtime (");
2703 sb.append(count);
2704 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002705 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2706 if (maxDurationMs >= 0) {
2707 sb.append(" max=");
2708 sb.append(maxDurationMs);
2709 }
2710 if (timer.isRunningLocked()) {
2711 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2712 if (currentMs >= 0) {
2713 sb.append(" (running for ");
2714 sb.append(currentMs);
2715 sb.append("ms)");
2716 } else {
2717 sb.append(" (running)");
2718 }
2719 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002720 pw.println(sb.toString());
2721 return true;
2722 }
2723 }
2724 return false;
2725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726
2727 /**
2728 * Checkin version of wakelock printer. Prints simple comma-separated list.
2729 *
2730 * @param sb a StringBuilder object.
2731 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002732 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002734 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 * @param linePrefix a String to be prepended to each line of output.
2736 * @return the line prefix
2737 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002738 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2739 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 long totalTimeMicros = 0;
2741 int count = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002742 long max = -1;
2743 long current = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002745 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002746 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002747 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2748 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 }
2750 sb.append(linePrefix);
2751 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2752 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002753 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002755 sb.append(',');
2756 sb.append(current);
2757 sb.append(',');
2758 sb.append(max);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 return ",";
2760 }
2761
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002762 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2763 String type) {
2764 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2765 pw.print(',');
2766 pw.print(uid);
2767 pw.print(',');
2768 pw.print(category);
2769 pw.print(',');
2770 pw.print(type);
2771 }
2772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 /**
2774 * Dump a comma-separated line of values for terse checkin mode.
2775 *
2776 * @param pw the PageWriter to dump log to
2777 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2778 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2779 * @param args type-dependent data arguments
2780 */
2781 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2782 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002783 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002784 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002785 pw.print(',');
2786 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002788 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002790
2791 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002792 * Dump a given timer stat for terse checkin mode.
2793 *
2794 * @param pw the PageWriter to dump log to
2795 * @param uid the UID to log
2796 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2797 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2798 * @param timer a {@link Timer} to dump stats for
2799 * @param rawRealtime the current elapsed realtime of the system in microseconds
2800 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2801 */
2802 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2803 Timer timer, long rawRealtime, int which) {
2804 if (timer != null) {
2805 // Convert from microseconds to milliseconds with rounding
2806 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2807 / 1000;
2808 final int count = timer.getCountLocked(which);
2809 if (totalTime != 0) {
2810 dumpLine(pw, uid, category, type, totalTime, count);
2811 }
2812 }
2813 }
2814
2815 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002816 * Checks if the ControllerActivityCounter has any data worth dumping.
2817 */
2818 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2819 if (counter == null) {
2820 return false;
2821 }
2822
2823 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2824 || counter.getRxTimeCounter().getCountLocked(which) != 0
2825 || counter.getPowerCounter().getCountLocked(which) != 0) {
2826 return true;
2827 }
2828
2829 for (LongCounter c : counter.getTxTimeCounters()) {
2830 if (c.getCountLocked(which) != 0) {
2831 return true;
2832 }
2833 }
2834 return false;
2835 }
2836
2837 /**
2838 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2839 * The order of the arguments in the final check in line is:
2840 *
2841 * idle, rx, power, tx...
2842 *
2843 * where tx... is one or more transmit level times.
2844 */
2845 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2846 String type,
2847 ControllerActivityCounter counter,
2848 int which) {
2849 if (!controllerActivityHasData(counter, which)) {
2850 return;
2851 }
2852
2853 dumpLineHeader(pw, uid, category, type);
2854 pw.print(",");
2855 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2856 pw.print(",");
2857 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2858 pw.print(",");
2859 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2860 for (LongCounter c : counter.getTxTimeCounters()) {
2861 pw.print(",");
2862 pw.print(c.getCountLocked(which));
2863 }
2864 pw.println();
2865 }
2866
2867 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2868 String prefix, String controllerName,
2869 ControllerActivityCounter counter,
2870 int which) {
2871 if (controllerActivityHasData(counter, which)) {
2872 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2873 }
2874 }
2875
2876 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2877 String controllerName,
2878 ControllerActivityCounter counter, int which) {
2879 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2880 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2881 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2882 long totalTxTimeMs = 0;
2883 for (LongCounter txState : counter.getTxTimeCounters()) {
2884 totalTxTimeMs += txState.getCountLocked(which);
2885 }
2886
2887 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2888
2889 sb.setLength(0);
2890 sb.append(prefix);
2891 sb.append(" ");
2892 sb.append(controllerName);
2893 sb.append(" Idle time: ");
2894 formatTimeMs(sb, idleTimeMs);
2895 sb.append("(");
2896 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2897 sb.append(")");
2898 pw.println(sb.toString());
2899
2900 sb.setLength(0);
2901 sb.append(prefix);
2902 sb.append(" ");
2903 sb.append(controllerName);
2904 sb.append(" Rx time: ");
2905 formatTimeMs(sb, rxTimeMs);
2906 sb.append("(");
2907 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2908 sb.append(")");
2909 pw.println(sb.toString());
2910
2911 sb.setLength(0);
2912 sb.append(prefix);
2913 sb.append(" ");
2914 sb.append(controllerName);
2915 sb.append(" Tx time: ");
2916 formatTimeMs(sb, totalTxTimeMs);
2917 sb.append("(");
2918 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2919 sb.append(")");
2920 pw.println(sb.toString());
2921
2922 final int numTxLvls = counter.getTxTimeCounters().length;
2923 if (numTxLvls > 1) {
2924 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2925 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2926 sb.setLength(0);
2927 sb.append(prefix);
2928 sb.append(" [");
2929 sb.append(lvl);
2930 sb.append("] ");
2931 formatTimeMs(sb, txLvlTimeMs);
2932 sb.append("(");
2933 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2934 sb.append(")");
2935 pw.println(sb.toString());
2936 }
2937 }
2938
2939 sb.setLength(0);
2940 sb.append(prefix);
2941 sb.append(" ");
2942 sb.append(controllerName);
2943 sb.append(" Power drain: ").append(
2944 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2945 sb.append("mAh");
2946 pw.println(sb.toString());
2947 }
2948
2949 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002950 * Temporary for settings.
2951 */
2952 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2953 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2954 }
2955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 /**
2957 * Checkin server version of dump to produce more compact, computer-readable log.
2958 *
2959 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002961 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2962 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2964 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2965 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2967 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002968 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2969 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2970 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 final long totalRealtime = computeRealtime(rawRealtime, which);
2972 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002973 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002974 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002975 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002976 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2977 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002978 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002979 rawRealtime, which);
2980 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2981 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002982 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002983 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002984 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002985 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002986 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
2987 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
2988 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002989
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002990 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002992 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002993 final int NU = uidStats.size();
2994
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002995 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 // Dump "battery" stat
2998 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002999 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003000 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003001 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003002 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003003 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
3004 getEstimatedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07003005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003007 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003008 long fullWakeLockTimeTotal = 0;
3009 long partialWakeLockTimeTotal = 0;
3010
3011 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003012 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003013
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003014 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3015 = u.getWakelockStats();
3016 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3017 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003018
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003019 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3020 if (fullWakeTimer != null) {
3021 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3022 which);
3023 }
3024
3025 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3026 if (partialWakeTimer != null) {
3027 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3028 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003029 }
3030 }
3031 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003032
3033 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003034 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3035 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3036 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3037 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3038 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3039 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3040 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3041 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003042 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3043 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003044 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3045 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003046 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3047 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003048
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003049 // Dump Modem controller stats
3050 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3051 getModemControllerActivity(), which);
3052
Adam Lesinskie283d332015-04-16 12:29:25 -07003053 // Dump Wifi controller stats
3054 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3055 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003056 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003057 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003058
3059 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3060 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003061
3062 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003063 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3064 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 // Dump misc stats
3067 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003068 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003069 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003070 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003071 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003072 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003073 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3074 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003075 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003076 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3077 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3078 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3079 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003080 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003081
3082 // Dump screen brightness stats
3083 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3084 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003085 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003086 }
3087 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07003088
Dianne Hackborn627bba72009-03-24 22:32:56 -07003089 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003090 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3091 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003092 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003093 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003094 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003095 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003096 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003097 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003098 args[i] = getPhoneSignalStrengthCount(i, which);
3099 }
3100 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003101
Dianne Hackborn627bba72009-03-24 22:32:56 -07003102 // Dump network type stats
3103 args = new Object[NUM_DATA_CONNECTION_TYPES];
3104 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003105 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003106 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003107 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3108 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3109 args[i] = getPhoneDataConnectionCount(i, which);
3110 }
3111 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003112
3113 // Dump wifi state stats
3114 args = new Object[NUM_WIFI_STATES];
3115 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003116 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003117 }
3118 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3119 for (int i=0; i<NUM_WIFI_STATES; i++) {
3120 args[i] = getWifiStateCount(i, which);
3121 }
3122 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3123
Dianne Hackborn3251b902014-06-20 14:40:53 -07003124 // Dump wifi suppl state stats
3125 args = new Object[NUM_WIFI_SUPPL_STATES];
3126 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3127 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3128 }
3129 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3130 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3131 args[i] = getWifiSupplStateCount(i, which);
3132 }
3133 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3134
3135 // Dump wifi signal strength stats
3136 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3137 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3138 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3139 }
3140 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3141 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3142 args[i] = getWifiSignalStrengthCount(i, which);
3143 }
3144 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3145
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003146 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003147 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003148 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003151 if (which == STATS_SINCE_UNPLUGGED) {
3152 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3153 getDischargeStartLevel()-getDischargeCurrentLevel(),
3154 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003155 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3156 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003157 } else {
3158 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3159 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003160 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003161 getDischargeAmountScreenOffSinceCharge(),
3162 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003163 }
3164
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003165 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003166 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003167 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003168 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003169 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003170 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003171 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3172 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003173 }
Evan Millarc64edde2009-04-18 12:26:32 -07003174 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003175 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003176 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003177 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3178 // Not doing the regular wake lock formatting to remain compatible
3179 // with the old checkin format.
3180 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3181 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003182 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003183 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003184 }
3185 }
Evan Millarc64edde2009-04-18 12:26:32 -07003186 }
3187
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003188 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003189 helper.create(this);
3190 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003191 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003192 if (sippers != null && sippers.size() > 0) {
3193 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3194 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003195 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003196 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3197 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3198 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003199 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003200 int uid = 0;
3201 String label;
3202 switch (bs.drainType) {
3203 case IDLE:
3204 label="idle";
3205 break;
3206 case CELL:
3207 label="cell";
3208 break;
3209 case PHONE:
3210 label="phone";
3211 break;
3212 case WIFI:
3213 label="wifi";
3214 break;
3215 case BLUETOOTH:
3216 label="blue";
3217 break;
3218 case SCREEN:
3219 label="scrn";
3220 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003221 case FLASHLIGHT:
3222 label="flashlight";
3223 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003224 case APP:
3225 uid = bs.uidObj.getUid();
3226 label = "uid";
3227 break;
3228 case USER:
3229 uid = UserHandle.getUid(bs.userId, 0);
3230 label = "user";
3231 break;
3232 case UNACCOUNTED:
3233 label = "unacc";
3234 break;
3235 case OVERCOUNTED:
3236 label = "over";
3237 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003238 case CAMERA:
3239 label = "camera";
3240 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003241 default:
3242 label = "???";
3243 }
3244 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003245 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003246 }
3247 }
3248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 for (int iu = 0; iu < NU; iu++) {
3250 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003251 if (reqUid >= 0 && uid != reqUid) {
3252 continue;
3253 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003254 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003257 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3258 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3259 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3260 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3261 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3262 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3263 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3264 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003265 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003266 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3267 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003268 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003269 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3270 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003271 // Background data transfers
3272 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3273 which);
3274 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3275 which);
3276 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3277 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3278 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3279 which);
3280 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3281 which);
3282 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3283 which);
3284 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3285 which);
3286
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003287 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3288 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003289 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003290 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3291 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3292 || wifiBytesBgTx > 0
3293 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3294 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003295 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3296 wifiBytesRx, wifiBytesTx,
3297 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003298 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003299 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003300 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3301 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3302 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3303 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003304 }
3305
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003306 // Dump modem controller data, per UID.
3307 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3308 u.getModemControllerActivity(), which);
3309
3310 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003311 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3312 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3313 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003314 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3315 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
3316 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
3317 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Adam Lesinskie283d332015-04-16 12:29:25 -07003318 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003319 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08003320 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003321 || uidWifiRunningTime != 0) {
3322 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3323 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003324 /* legacy fields follow, keep at 0 */ 0, 0, 0,
3325 wifiScanCountBg, wifiScanActualTime, wifiScanActualTimeBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003328 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3329 u.getWifiControllerActivity(), which);
3330
Bookatz867c0d72017-03-07 18:23:42 -08003331 final Timer bleTimer = u.getBluetoothScanTimer();
3332 if (bleTimer != null) {
3333 // Convert from microseconds to milliseconds with rounding
3334 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3335 / 1000;
3336 if (totalTime != 0) {
3337 final int count = bleTimer.getCountLocked(which);
3338 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3339 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
3340 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3341 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3342 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3343 final long actualTimeBg = bleTimerBg != null ?
3344 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3345 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
3346 countBg, actualTime, actualTimeBg);
3347 }
3348 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003349
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003350 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3351 u.getBluetoothControllerActivity(), which);
3352
Dianne Hackborn617f8772009-03-31 15:04:46 -07003353 if (u.hasUserActivity()) {
3354 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3355 boolean hasData = false;
3356 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3357 int val = u.getUserActivityCount(i, which);
3358 args[i] = val;
3359 if (val != 0) hasData = true;
3360 }
3361 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003362 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003363 }
3364 }
3365
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003366 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3367 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3368 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3369 String linePrefix = "";
3370 sb.setLength(0);
3371 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3372 rawRealtime, "f", which, linePrefix);
3373 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3374 rawRealtime, "p", which, linePrefix);
3375 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3376 rawRealtime, "w", which, linePrefix);
3377
3378 // Only log if we had at lease one wakelock...
3379 if (sb.length() > 0) {
3380 String name = wakelocks.keyAt(iw);
3381 if (name.indexOf(',') >= 0) {
3382 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003384 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 }
3386 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003387
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003388 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3389 for (int isy=syncs.size()-1; isy>=0; isy--) {
3390 final Timer timer = syncs.valueAt(isy);
3391 // Convert from microseconds to milliseconds with rounding
3392 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3393 final int count = timer.getCountLocked(which);
3394 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003395 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
3396 totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003397 }
3398 }
3399
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003400 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3401 for (int ij=jobs.size()-1; ij>=0; ij--) {
3402 final Timer timer = jobs.valueAt(ij);
3403 // Convert from microseconds to milliseconds with rounding
3404 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3405 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003406 final Timer bgTimer = timer.getSubTimer();
3407 final long bgTime = bgTimer != null ?
3408 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
3409 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003410 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003411 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003412 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003413 }
3414 }
3415
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003416 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3417 rawRealtime, which);
3418 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3419 rawRealtime, which);
3420 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3421 rawRealtime, which);
3422 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3423 rawRealtime, which);
3424
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003425 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3426 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003427 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003428 final Uid.Sensor se = sensors.valueAt(ise);
3429 final int sensorNumber = sensors.keyAt(ise);
3430 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003431 if (timer != null) {
3432 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003433 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3434 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003435 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003436 final int count = timer.getCountLocked(which);
3437 final Timer bgTimer = se.getSensorBackgroundTime();
3438 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
3439 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3440 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3441 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3442 final long bgActualTime = bgTimer != null ?
3443 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3444 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3445 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 }
3448 }
3449
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003450 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3451 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003452
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003453 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3454 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003455
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003456 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003457 long totalStateTime = 0;
3458 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003459 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3460 totalStateTime += time;
3461 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003462 }
3463 if (totalStateTime > 0) {
3464 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3465 }
3466
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003467 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3468 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
3469 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
3470 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
3471 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
3472 powerCpuMaUs / 1000);
3473 }
3474
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003475 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3476 = u.getProcessStats();
3477 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3478 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003479
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003480 final long userMillis = ps.getUserTime(which);
3481 final long systemMillis = ps.getSystemTime(which);
3482 final long foregroundMillis = ps.getForegroundTime(which);
3483 final int starts = ps.getStarts(which);
3484 final int numCrashes = ps.getNumCrashes(which);
3485 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003486
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003487 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3488 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003489 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3490 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 }
3492 }
3493
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003494 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3495 = u.getPackageStats();
3496 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3497 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3498 int wakeups = 0;
3499 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3500 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003501 int count = alarms.valueAt(iwa).getCountLocked(which);
3502 wakeups += count;
3503 String name = alarms.keyAt(iwa).replace(',', '_');
3504 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003505 }
3506 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3507 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3508 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3509 final long startTime = ss.getStartTime(batteryUptime, which);
3510 final int starts = ss.getStarts(which);
3511 final int launches = ss.getLaunches(which);
3512 if (startTime != 0 || starts != 0 || launches != 0) {
3513 dumpLine(pw, uid, category, APK_DATA,
3514 wakeups, // wakeup alarms
3515 packageStats.keyAt(ipkg), // Apk
3516 serviceStats.keyAt(isvc), // service
3517 startTime / 1000, // time spent started, in ms
3518 starts,
3519 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 }
3521 }
3522 }
3523 }
3524 }
3525
Dianne Hackborn81038902012-11-26 17:04:09 -08003526 static final class TimerEntry {
3527 final String mName;
3528 final int mId;
3529 final BatteryStats.Timer mTimer;
3530 final long mTime;
3531 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3532 mName = name;
3533 mId = id;
3534 mTimer = timer;
3535 mTime = time;
3536 }
3537 }
3538
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003539 private void printmAh(PrintWriter printer, double power) {
3540 printer.print(BatteryStatsHelper.makemAh(power));
3541 }
3542
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003543 private void printmAh(StringBuilder sb, double power) {
3544 sb.append(BatteryStatsHelper.makemAh(power));
3545 }
3546
Dianne Hackbornd953c532014-08-16 18:17:38 -07003547 /**
3548 * Temporary for settings.
3549 */
3550 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3551 int reqUid) {
3552 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3553 }
3554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003556 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003557 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3559 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3560 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561
3562 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3563 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3564 final long totalRealtime = computeRealtime(rawRealtime, which);
3565 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003566 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3567 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3568 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003569 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3570 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003571
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003572 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003573
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003574 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003575 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003577 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3578 if (estimatedBatteryCapacity > 0) {
3579 sb.setLength(0);
3580 sb.append(prefix);
3581 sb.append(" Estimated battery capacity: ");
3582 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3583 sb.append(" mAh");
3584 pw.println(sb.toString());
3585 }
3586
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003587 sb.setLength(0);
3588 sb.append(prefix);
3589 sb.append(" Time on battery: ");
3590 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3591 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3592 sb.append(") realtime, ");
3593 formatTimeMs(sb, whichBatteryUptime / 1000);
3594 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3595 sb.append(") uptime");
3596 pw.println(sb.toString());
3597 sb.setLength(0);
3598 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003599 sb.append(" Time on battery screen off: ");
3600 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3601 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3602 sb.append(") realtime, ");
3603 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3604 sb.append("(");
3605 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3606 sb.append(") uptime");
3607 pw.println(sb.toString());
3608 sb.setLength(0);
3609 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003610 sb.append(" Total run time: ");
3611 formatTimeMs(sb, totalRealtime / 1000);
3612 sb.append("realtime, ");
3613 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003614 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003615 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003616 if (batteryTimeRemaining >= 0) {
3617 sb.setLength(0);
3618 sb.append(prefix);
3619 sb.append(" Battery time remaining: ");
3620 formatTimeMs(sb, batteryTimeRemaining / 1000);
3621 pw.println(sb.toString());
3622 }
3623 if (chargeTimeRemaining >= 0) {
3624 sb.setLength(0);
3625 sb.append(prefix);
3626 sb.append(" Charge time remaining: ");
3627 formatTimeMs(sb, chargeTimeRemaining / 1000);
3628 pw.println(sb.toString());
3629 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003630
3631 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3632 final long dischargeCount = dischargeCounter.getCountLocked(which);
3633 if (dischargeCount >= 0) {
3634 sb.setLength(0);
3635 sb.append(prefix);
3636 sb.append(" Discharge: ");
3637 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3638 sb.append(" mAh");
3639 pw.println(sb.toString());
3640 }
3641
3642 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3643 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3644 if (dischargeScreenOffCount >= 0) {
3645 sb.setLength(0);
3646 sb.append(prefix);
3647 sb.append(" Screen off discharge: ");
3648 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3649 sb.append(" mAh");
3650 pw.println(sb.toString());
3651 }
3652
3653 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3654 if (dischargeScreenOnCount >= 0) {
3655 sb.setLength(0);
3656 sb.append(prefix);
3657 sb.append(" Screen on discharge: ");
3658 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3659 sb.append(" mAh");
3660 pw.println(sb.toString());
3661 }
3662
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003663 pw.print(" Start clock time: ");
3664 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3665
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003666 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003667 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003668 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003669 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3670 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003671 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003672 rawRealtime, which);
3673 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3674 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003675 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003676 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003677 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3678 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3679 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003680 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003681 sb.append(prefix);
3682 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3683 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003684 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003685 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3686 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003687 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003688 pw.println(sb.toString());
3689 sb.setLength(0);
3690 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003691 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003692 boolean didOne = false;
3693 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003694 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003695 if (time == 0) {
3696 continue;
3697 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003698 sb.append("\n ");
3699 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003700 didOne = true;
3701 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3702 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003703 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003704 sb.append("(");
3705 sb.append(formatRatioLocked(time, screenOnTime));
3706 sb.append(")");
3707 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003708 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003709 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003710 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003711 sb.setLength(0);
3712 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003713 sb.append(" Power save mode enabled: ");
3714 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003715 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003716 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003717 sb.append(")");
3718 pw.println(sb.toString());
3719 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003720 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003721 sb.setLength(0);
3722 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003723 sb.append(" Device light idling: ");
3724 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003725 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003726 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3727 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003728 sb.append("x");
3729 pw.println(sb.toString());
3730 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003731 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003732 sb.setLength(0);
3733 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003734 sb.append(" Idle mode light time: ");
3735 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003736 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003737 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3738 sb.append(") ");
3739 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003740 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003741 sb.append(" -- longest ");
3742 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3743 pw.println(sb.toString());
3744 }
3745 if (deviceIdlingTime != 0) {
3746 sb.setLength(0);
3747 sb.append(prefix);
3748 sb.append(" Device full idling: ");
3749 formatTimeMs(sb, deviceIdlingTime / 1000);
3750 sb.append("(");
3751 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003752 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003753 sb.append("x");
3754 pw.println(sb.toString());
3755 }
3756 if (deviceIdleModeFullTime != 0) {
3757 sb.setLength(0);
3758 sb.append(prefix);
3759 sb.append(" Idle mode full time: ");
3760 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3761 sb.append("(");
3762 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3763 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003764 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003765 sb.append("x");
3766 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003767 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003768 pw.println(sb.toString());
3769 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003770 if (phoneOnTime != 0) {
3771 sb.setLength(0);
3772 sb.append(prefix);
3773 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3774 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003775 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003776 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003777 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003778 if (connChanges != 0) {
3779 pw.print(prefix);
3780 pw.print(" Connectivity changes: "); pw.println(connChanges);
3781 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003782
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003783 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003784 long fullWakeLockTimeTotalMicros = 0;
3785 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003786
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003787 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003788
Evan Millar22ac0432009-03-31 11:33:18 -07003789 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003790 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003791
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003792 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3793 = u.getWakelockStats();
3794 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3795 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003796
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003797 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3798 if (fullWakeTimer != null) {
3799 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3800 rawRealtime, which);
3801 }
3802
3803 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3804 if (partialWakeTimer != null) {
3805 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3806 rawRealtime, which);
3807 if (totalTimeMicros > 0) {
3808 if (reqUid < 0) {
3809 // Only show the ordered list of all wake
3810 // locks if the caller is not asking for data
3811 // about a specific uid.
3812 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3813 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003814 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003815 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003816 }
3817 }
3818 }
3819 }
3820
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003821 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3822 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3823 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3824 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3825 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3826 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3827 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3828 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003829 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3830 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003831
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003832 if (fullWakeLockTimeTotalMicros != 0) {
3833 sb.setLength(0);
3834 sb.append(prefix);
3835 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3836 (fullWakeLockTimeTotalMicros + 500) / 1000);
3837 pw.println(sb.toString());
3838 }
3839
3840 if (partialWakeLockTimeTotalMicros != 0) {
3841 sb.setLength(0);
3842 sb.append(prefix);
3843 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3844 (partialWakeLockTimeTotalMicros + 500) / 1000);
3845 pw.println(sb.toString());
3846 }
3847
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003848 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003849 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3850 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3851 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3852 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003853 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003854 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003855 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003856 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003857 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003858 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003859 if (time == 0) {
3860 continue;
3861 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003862 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003863 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003864 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003865 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003866 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003867 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003868 sb.append("(");
3869 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003870 sb.append(") ");
3871 sb.append(getPhoneSignalStrengthCount(i, which));
3872 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003873 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003874 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003875 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003876
3877 sb.setLength(0);
3878 sb.append(prefix);
3879 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003880 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003881 pw.println(sb.toString());
3882
Dianne Hackborn627bba72009-03-24 22:32:56 -07003883 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003884 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003885 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003886 didOne = false;
3887 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003888 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003889 if (time == 0) {
3890 continue;
3891 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003892 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003893 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003894 didOne = true;
3895 sb.append(DATA_CONNECTION_NAMES[i]);
3896 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003897 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003898 sb.append("(");
3899 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003900 sb.append(") ");
3901 sb.append(getPhoneDataConnectionCount(i, which));
3902 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003903 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003904 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003905 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003906
3907 sb.setLength(0);
3908 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003909 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003910 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003911 formatTimeMs(sb, mobileActiveTime / 1000);
3912 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3913 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3914 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003915 pw.println(sb.toString());
3916
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003917 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3918 if (mobileActiveUnknownTime != 0) {
3919 sb.setLength(0);
3920 sb.append(prefix);
3921 sb.append(" Mobile radio active unknown time: ");
3922 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3923 sb.append("(");
3924 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3925 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3926 sb.append("x");
3927 pw.println(sb.toString());
3928 }
3929
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003930 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3931 if (mobileActiveAdjustedTime != 0) {
3932 sb.setLength(0);
3933 sb.append(prefix);
3934 sb.append(" Mobile radio active adjusted time: ");
3935 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3936 sb.append("(");
3937 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3938 sb.append(")");
3939 pw.println(sb.toString());
3940 }
3941
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003942 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3943
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003944 pw.print(prefix);
3945 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3946 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3947 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3948 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003949 sb.setLength(0);
3950 sb.append(prefix);
3951 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3952 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3953 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3954 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003955 sb.append(")");
3956 pw.println(sb.toString());
3957
3958 sb.setLength(0);
3959 sb.append(prefix);
3960 sb.append(" Wifi states:");
3961 didOne = false;
3962 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003963 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003964 if (time == 0) {
3965 continue;
3966 }
3967 sb.append("\n ");
3968 didOne = true;
3969 sb.append(WIFI_STATE_NAMES[i]);
3970 sb.append(" ");
3971 formatTimeMs(sb, time/1000);
3972 sb.append("(");
3973 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3974 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003975 sb.append(getWifiStateCount(i, which));
3976 sb.append("x");
3977 }
3978 if (!didOne) sb.append(" (no activity)");
3979 pw.println(sb.toString());
3980
3981 sb.setLength(0);
3982 sb.append(prefix);
3983 sb.append(" Wifi supplicant states:");
3984 didOne = false;
3985 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3986 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3987 if (time == 0) {
3988 continue;
3989 }
3990 sb.append("\n ");
3991 didOne = true;
3992 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3993 sb.append(" ");
3994 formatTimeMs(sb, time/1000);
3995 sb.append("(");
3996 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3997 sb.append(") ");
3998 sb.append(getWifiSupplStateCount(i, which));
3999 sb.append("x");
4000 }
4001 if (!didOne) sb.append(" (no activity)");
4002 pw.println(sb.toString());
4003
4004 sb.setLength(0);
4005 sb.append(prefix);
4006 sb.append(" Wifi signal levels:");
4007 didOne = false;
4008 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4009 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4010 if (time == 0) {
4011 continue;
4012 }
4013 sb.append("\n ");
4014 sb.append(prefix);
4015 didOne = true;
4016 sb.append("level(");
4017 sb.append(i);
4018 sb.append(") ");
4019 formatTimeMs(sb, time/1000);
4020 sb.append("(");
4021 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4022 sb.append(") ");
4023 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004024 sb.append("x");
4025 }
4026 if (!didOne) sb.append(" (no activity)");
4027 pw.println(sb.toString());
4028
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004029 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004030
Adam Lesinski50e47602015-12-04 17:04:54 -08004031 pw.print(prefix);
4032 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4033 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4034
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004035 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4036 sb.setLength(0);
4037 sb.append(prefix);
4038 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4039 pw.println(sb.toString());
4040
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004041 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4042 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004043
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004044 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004045
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004046 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004047 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004048 pw.print(prefix); pw.println(" Device is currently unplugged");
4049 pw.print(prefix); pw.print(" Discharge cycle start level: ");
4050 pw.println(getDischargeStartLevel());
4051 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4052 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004053 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004054 pw.print(prefix); pw.println(" Device is currently plugged into power");
4055 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
4056 pw.println(getDischargeStartLevel());
4057 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
4058 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004059 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004060 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4061 pw.println(getDischargeAmountScreenOn());
4062 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4063 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004064 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004065 } else {
4066 pw.print(prefix); pw.println(" Device battery use since last full charge");
4067 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4068 pw.println(getLowDischargeAmountSinceCharge());
4069 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4070 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004071 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4072 pw.println(getDischargeAmountScreenOnSinceCharge());
4073 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4074 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004075 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004076 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004077
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004078 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004079 helper.create(this);
4080 helper.refreshStats(which, UserHandle.USER_ALL);
4081 List<BatterySipper> sippers = helper.getUsageList();
4082 if (sippers != null && sippers.size() > 0) {
4083 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4084 pw.print(prefix); pw.print(" Capacity: ");
4085 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004086 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004087 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4088 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4089 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4090 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004091 pw.println();
4092 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004093 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004094 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004095 switch (bs.drainType) {
4096 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004097 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004098 break;
4099 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004100 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004101 break;
4102 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004103 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004104 break;
4105 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004106 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004107 break;
4108 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004109 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004110 break;
4111 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004112 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004113 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004114 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004115 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004116 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004117 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004118 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004119 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004120 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004121 break;
4122 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004123 pw.print(" User "); pw.print(bs.userId);
4124 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004125 break;
4126 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004127 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004128 break;
4129 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004130 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004131 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004132 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004133 pw.print(" Camera: ");
4134 break;
4135 default:
4136 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004137 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004138 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004139 printmAh(pw, bs.totalPowerMah);
4140
Adam Lesinski57123002015-06-12 16:12:07 -07004141 if (bs.usagePowerMah != bs.totalPowerMah) {
4142 // If the usage (generic power) isn't the whole amount, we list out
4143 // what components are involved in the calculation.
4144
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004145 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004146 if (bs.usagePowerMah != 0) {
4147 pw.print(" usage=");
4148 printmAh(pw, bs.usagePowerMah);
4149 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004150 if (bs.cpuPowerMah != 0) {
4151 pw.print(" cpu=");
4152 printmAh(pw, bs.cpuPowerMah);
4153 }
4154 if (bs.wakeLockPowerMah != 0) {
4155 pw.print(" wake=");
4156 printmAh(pw, bs.wakeLockPowerMah);
4157 }
4158 if (bs.mobileRadioPowerMah != 0) {
4159 pw.print(" radio=");
4160 printmAh(pw, bs.mobileRadioPowerMah);
4161 }
4162 if (bs.wifiPowerMah != 0) {
4163 pw.print(" wifi=");
4164 printmAh(pw, bs.wifiPowerMah);
4165 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004166 if (bs.bluetoothPowerMah != 0) {
4167 pw.print(" bt=");
4168 printmAh(pw, bs.bluetoothPowerMah);
4169 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004170 if (bs.gpsPowerMah != 0) {
4171 pw.print(" gps=");
4172 printmAh(pw, bs.gpsPowerMah);
4173 }
4174 if (bs.sensorPowerMah != 0) {
4175 pw.print(" sensor=");
4176 printmAh(pw, bs.sensorPowerMah);
4177 }
4178 if (bs.cameraPowerMah != 0) {
4179 pw.print(" camera=");
4180 printmAh(pw, bs.cameraPowerMah);
4181 }
4182 if (bs.flashlightPowerMah != 0) {
4183 pw.print(" flash=");
4184 printmAh(pw, bs.flashlightPowerMah);
4185 }
4186 pw.print(" )");
4187 }
4188 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004189 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004190 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004191 }
4192
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004193 sippers = helper.getMobilemsppList();
4194 if (sippers != null && sippers.size() > 0) {
4195 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004196 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004197 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004198 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004199 sb.setLength(0);
4200 sb.append(prefix); sb.append(" Uid ");
4201 UserHandle.formatUid(sb, bs.uidObj.getUid());
4202 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4203 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4204 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004205 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004206 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004207 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004208 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004209 sb.setLength(0);
4210 sb.append(prefix);
4211 sb.append(" TOTAL TIME: ");
4212 formatTimeMs(sb, totalTime);
4213 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4214 sb.append(")");
4215 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004216 pw.println();
4217 }
4218
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004219 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4220 @Override
4221 public int compare(TimerEntry lhs, TimerEntry rhs) {
4222 long lhsTime = lhs.mTime;
4223 long rhsTime = rhs.mTime;
4224 if (lhsTime < rhsTime) {
4225 return 1;
4226 }
4227 if (lhsTime > rhsTime) {
4228 return -1;
4229 }
4230 return 0;
4231 }
4232 };
4233
4234 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004235 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4236 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004237 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004238 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4239 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4240 : kernelWakelocks.entrySet()) {
4241 final BatteryStats.Timer timer = ent.getValue();
4242 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004243 if (totalTimeMillis > 0) {
4244 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4245 }
4246 }
4247 if (ktimers.size() > 0) {
4248 Collections.sort(ktimers, timerComparator);
4249 pw.print(prefix); pw.println(" All kernel wake locks:");
4250 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004251 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004252 String linePrefix = ": ";
4253 sb.setLength(0);
4254 sb.append(prefix);
4255 sb.append(" Kernel Wake lock ");
4256 sb.append(timer.mName);
4257 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4258 which, linePrefix);
4259 if (!linePrefix.equals(": ")) {
4260 sb.append(" realtime");
4261 // Only print out wake locks that were held
4262 pw.println(sb.toString());
4263 }
4264 }
4265 pw.println();
4266 }
4267 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004268
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004269 if (timers.size() > 0) {
4270 Collections.sort(timers, timerComparator);
4271 pw.print(prefix); pw.println(" All partial wake locks:");
4272 for (int i=0; i<timers.size(); i++) {
4273 TimerEntry timer = timers.get(i);
4274 sb.setLength(0);
4275 sb.append(" Wake lock ");
4276 UserHandle.formatUid(sb, timer.mId);
4277 sb.append(" ");
4278 sb.append(timer.mName);
4279 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4280 sb.append(" realtime");
4281 pw.println(sb.toString());
4282 }
4283 timers.clear();
4284 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004285 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004286
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004287 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004288 if (wakeupReasons.size() > 0) {
4289 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004290 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004291 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004292 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004293 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4294 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004295 }
4296 Collections.sort(reasons, timerComparator);
4297 for (int i=0; i<reasons.size(); i++) {
4298 TimerEntry timer = reasons.get(i);
4299 String linePrefix = ": ";
4300 sb.setLength(0);
4301 sb.append(prefix);
4302 sb.append(" Wakeup reason ");
4303 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004304 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4305 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004306 pw.println(sb.toString());
4307 }
4308 pw.println();
4309 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004310 }
Evan Millar22ac0432009-03-31 11:33:18 -07004311
James Carr2dd7e5e2016-07-20 18:48:39 -07004312 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4313 pw.println("Memory Stats");
4314 for (int i = 0; i < mMemoryStats.size(); i++) {
4315 sb.setLength(0);
4316 sb.append("Bandwidth ");
4317 sb.append(mMemoryStats.keyAt(i));
4318 sb.append(" Time ");
4319 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4320 pw.println(sb.toString());
4321 }
4322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 for (int iu=0; iu<NU; iu++) {
4324 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004325 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004326 continue;
4327 }
4328
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004329 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004330
4331 pw.print(prefix);
4332 pw.print(" ");
4333 UserHandle.formatUid(pw, uid);
4334 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004336
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004337 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4338 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4339 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4340 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004341 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4342 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4343
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004344 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4345 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004346 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4347 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004348
4349 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4350 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4351
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004352 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4353 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4354 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004355 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4356 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4357 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4358 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004359 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004360
Adam Lesinski5f056f62016-07-14 16:56:08 -07004361 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4362 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4363
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004364 if (mobileRxBytes > 0 || mobileTxBytes > 0
4365 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004366 pw.print(prefix); pw.print(" Mobile network: ");
4367 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004368 pw.print(formatBytesLocked(mobileTxBytes));
4369 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4370 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004372 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4373 sb.setLength(0);
4374 sb.append(prefix); sb.append(" Mobile radio active: ");
4375 formatTimeMs(sb, uidMobileActiveTime / 1000);
4376 sb.append("(");
4377 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4378 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4379 long packets = mobileRxPackets + mobileTxPackets;
4380 if (packets == 0) {
4381 packets = 1;
4382 }
4383 sb.append(" @ ");
4384 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4385 sb.append(" mspp");
4386 pw.println(sb.toString());
4387 }
4388
Adam Lesinski5f056f62016-07-14 16:56:08 -07004389 if (mobileWakeup > 0) {
4390 sb.setLength(0);
4391 sb.append(prefix);
4392 sb.append(" Mobile radio AP wakeups: ");
4393 sb.append(mobileWakeup);
4394 pw.println(sb.toString());
4395 }
4396
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004397 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4398 u.getModemControllerActivity(), which);
4399
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004400 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004401 pw.print(prefix); pw.print(" Wi-Fi network: ");
4402 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004403 pw.print(formatBytesLocked(wifiTxBytes));
4404 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4405 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004406 }
4407
Dianne Hackborn62793e42015-03-09 11:15:41 -07004408 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004409 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004410 || uidWifiRunningTime != 0) {
4411 sb.setLength(0);
4412 sb.append(prefix); sb.append(" Wifi Running: ");
4413 formatTimeMs(sb, uidWifiRunningTime / 1000);
4414 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4415 whichBatteryRealtime)); sb.append(")\n");
4416 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4417 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4418 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4419 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004420 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004421 formatTimeMs(sb, wifiScanTime / 1000);
4422 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004423 whichBatteryRealtime)); sb.append(") ");
4424 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004425 sb.append("x\n");
4426 // actual and background times are unpooled and since reset (regardless of 'which')
4427 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4428 formatTimeMs(sb, wifiScanActualTime / 1000);
4429 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4430 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4431 sb.append(") ");
4432 sb.append(wifiScanCount);
4433 sb.append("x\n");
4434 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4435 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4436 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4437 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4438 sb.append(") ");
4439 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004440 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004441 pw.println(sb.toString());
4442 }
4443
Adam Lesinski5f056f62016-07-14 16:56:08 -07004444 if (wifiWakeup > 0) {
4445 sb.setLength(0);
4446 sb.append(prefix);
4447 sb.append(" WiFi AP wakeups: ");
4448 sb.append(wifiWakeup);
4449 pw.println(sb.toString());
4450 }
4451
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004452 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4453 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004454
Adam Lesinski50e47602015-12-04 17:04:54 -08004455 if (btRxBytes > 0 || btTxBytes > 0) {
4456 pw.print(prefix); pw.print(" Bluetooth network: ");
4457 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4458 pw.print(formatBytesLocked(btTxBytes));
4459 pw.println(" sent");
4460 }
4461
Bookatz867c0d72017-03-07 18:23:42 -08004462 final Timer bleTimer = u.getBluetoothScanTimer();
4463 if (bleTimer != null) {
4464 // Convert from microseconds to milliseconds with rounding
4465 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4466 / 1000;
4467 if (totalTimeMs != 0) {
4468 final int count = bleTimer.getCountLocked(which);
4469 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4470 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
4471 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4472 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4473 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4474 final long actualTimeMsBg = bleTimerBg != null ?
4475 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4476
4477 sb.setLength(0);
4478 sb.append(prefix);
4479 sb.append(" ");
4480 sb.append("Bluetooth Scan");
4481 sb.append(": ");
4482 if (actualTimeMs != totalTimeMs) {
4483 formatTimeMs(sb, totalTimeMs);
4484 sb.append("blamed realtime, ");
4485 }
4486 formatTimeMs(sb, actualTimeMs); // since reset, regardless of 'which'
4487 sb.append("realtime (");
4488 sb.append(count);
4489 sb.append(" times)");
4490 if (bleTimer.isRunningLocked()) {
4491 sb.append(" (running)");
4492 }
4493 if (actualTimeMsBg != 0 || countBg > 0) {
4494 sb.append(", ");
4495 formatTimeMs(sb, actualTimeMsBg); // since reset, regardless of 'which'
4496 sb.append("background (");
4497 sb.append(countBg);
4498 sb.append(" times)");
4499 }
4500 pw.println(sb.toString());
4501 uidActivity = true;
4502 }
4503 }
4504
4505
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004506
Dianne Hackborn617f8772009-03-31 15:04:46 -07004507 if (u.hasUserActivity()) {
4508 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004509 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004510 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004511 if (val != 0) {
4512 if (!hasData) {
4513 sb.setLength(0);
4514 sb.append(" User activity: ");
4515 hasData = true;
4516 } else {
4517 sb.append(", ");
4518 }
4519 sb.append(val);
4520 sb.append(" ");
4521 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4522 }
4523 }
4524 if (hasData) {
4525 pw.println(sb.toString());
4526 }
4527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004529 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4530 = u.getWakelockStats();
4531 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004532 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004533 int countWakelock = 0;
4534 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4535 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4536 String linePrefix = ": ";
4537 sb.setLength(0);
4538 sb.append(prefix);
4539 sb.append(" Wake lock ");
4540 sb.append(wakelocks.keyAt(iw));
4541 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4542 "full", which, linePrefix);
4543 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4544 "partial", which, linePrefix);
4545 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4546 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004547 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4548 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004549 sb.append(" realtime");
4550 pw.println(sb.toString());
4551 uidActivity = true;
4552 countWakelock++;
4553
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004554 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4555 rawRealtime, which);
4556 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4557 rawRealtime, which);
4558 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4559 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004560 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004561 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004562 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004563 if (countWakelock > 1) {
4564 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4565 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004566 sb.setLength(0);
4567 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004568 sb.append(" TOTAL wake: ");
4569 boolean needComma = false;
4570 if (totalFullWakelock != 0) {
4571 needComma = true;
4572 formatTimeMs(sb, totalFullWakelock);
4573 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004574 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004575 if (totalPartialWakelock != 0) {
4576 if (needComma) {
4577 sb.append(", ");
4578 }
4579 needComma = true;
4580 formatTimeMs(sb, totalPartialWakelock);
4581 sb.append("partial");
4582 }
4583 if (totalWindowWakelock != 0) {
4584 if (needComma) {
4585 sb.append(", ");
4586 }
4587 needComma = true;
4588 formatTimeMs(sb, totalWindowWakelock);
4589 sb.append("window");
4590 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004591 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004592 if (needComma) {
4593 sb.append(",");
4594 }
4595 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004596 formatTimeMs(sb, totalDrawWakelock);
4597 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004598 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004599 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004600 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004601 }
4602 }
4603
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004604 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4605 for (int isy=syncs.size()-1; isy>=0; isy--) {
4606 final Timer timer = syncs.valueAt(isy);
4607 // Convert from microseconds to milliseconds with rounding
4608 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4609 final int count = timer.getCountLocked(which);
4610 sb.setLength(0);
4611 sb.append(prefix);
4612 sb.append(" Sync ");
4613 sb.append(syncs.keyAt(isy));
4614 sb.append(": ");
4615 if (totalTime != 0) {
4616 formatTimeMs(sb, totalTime);
4617 sb.append("realtime (");
4618 sb.append(count);
4619 sb.append(" times)");
4620 } else {
4621 sb.append("(not used)");
4622 }
4623 pw.println(sb.toString());
4624 uidActivity = true;
4625 }
4626
4627 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4628 for (int ij=jobs.size()-1; ij>=0; ij--) {
4629 final Timer timer = jobs.valueAt(ij);
4630 // Convert from microseconds to milliseconds with rounding
4631 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4632 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004633 final Timer bgTimer = timer.getSubTimer();
4634 final long bgTime = bgTimer != null ?
4635 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4636 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004637 sb.setLength(0);
4638 sb.append(prefix);
4639 sb.append(" Job ");
4640 sb.append(jobs.keyAt(ij));
4641 sb.append(": ");
4642 if (totalTime != 0) {
4643 formatTimeMs(sb, totalTime);
4644 sb.append("realtime (");
4645 sb.append(count);
4646 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07004647 if (bgTime > 0) {
4648 sb.append(", ");
4649 formatTimeMs(sb, bgTime);
4650 sb.append("background (");
4651 sb.append(bgCount);
4652 sb.append(" times)");
4653 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004654 } else {
4655 sb.append("(not used)");
4656 }
4657 pw.println(sb.toString());
4658 uidActivity = true;
4659 }
4660
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004661 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4662 prefix, "Flashlight");
4663 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4664 prefix, "Camera");
4665 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4666 prefix, "Video");
4667 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4668 prefix, "Audio");
4669
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004670 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4671 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004672 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004673 final Uid.Sensor se = sensors.valueAt(ise);
4674 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004675 sb.setLength(0);
4676 sb.append(prefix);
4677 sb.append(" Sensor ");
4678 int handle = se.getHandle();
4679 if (handle == Uid.Sensor.GPS) {
4680 sb.append("GPS");
4681 } else {
4682 sb.append(handle);
4683 }
4684 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004685
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004686 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004687 if (timer != null) {
4688 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08004689 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4690 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004691 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08004692 final Timer bgTimer = se.getSensorBackgroundTime();
4693 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
4694 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4695 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4696 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4697 final long bgActualTime = bgTimer != null ?
4698 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4699
Dianne Hackborn61659e52014-07-09 16:13:01 -07004700 //timer.logState();
4701 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004702 if (actualTime != totalTime) {
4703 formatTimeMs(sb, totalTime);
4704 sb.append("blamed realtime, ");
4705 }
4706
4707 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07004708 sb.append("realtime (");
4709 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08004710 sb.append(" times)");
4711
4712 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004713 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08004714 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
4715 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004716 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08004717 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004719 } else {
4720 sb.append("(not used)");
4721 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004722 } else {
4723 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004725
4726 pw.println(sb.toString());
4727 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004728 }
4729
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004730 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4731 "Vibrator");
4732 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4733 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004734
Dianne Hackborn61659e52014-07-09 16:13:01 -07004735 long totalStateTime = 0;
4736 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4737 long time = u.getProcessStateTime(ips, rawRealtime, which);
4738 if (time > 0) {
4739 totalStateTime += time;
4740 sb.setLength(0);
4741 sb.append(prefix);
4742 sb.append(" ");
4743 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4744 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004745 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004746 pw.println(sb.toString());
4747 uidActivity = true;
4748 }
4749 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004750 if (totalStateTime > 0) {
4751 sb.setLength(0);
4752 sb.append(prefix);
4753 sb.append(" Total running: ");
4754 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4755 pw.println(sb.toString());
4756 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004757
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004758 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4759 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004760 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4761 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004762 sb.setLength(0);
4763 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004764 sb.append(" Total cpu time: u=");
4765 formatTimeMs(sb, userCpuTimeUs / 1000);
4766 sb.append("s=");
4767 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004768 sb.append("p=");
4769 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4770 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004771 pw.println(sb.toString());
4772 }
4773
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004774 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4775 = u.getProcessStats();
4776 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4777 final Uid.Proc ps = processStats.valueAt(ipr);
4778 long userTime;
4779 long systemTime;
4780 long foregroundTime;
4781 int starts;
4782 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004784 userTime = ps.getUserTime(which);
4785 systemTime = ps.getSystemTime(which);
4786 foregroundTime = ps.getForegroundTime(which);
4787 starts = ps.getStarts(which);
4788 final int numCrashes = ps.getNumCrashes(which);
4789 final int numAnrs = ps.getNumAnrs(which);
4790 numExcessive = which == STATS_SINCE_CHARGED
4791 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004793 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4794 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4795 sb.setLength(0);
4796 sb.append(prefix); sb.append(" Proc ");
4797 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4798 sb.append(prefix); sb.append(" CPU: ");
4799 formatTimeMs(sb, userTime); sb.append("usr + ");
4800 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4801 formatTimeMs(sb, foregroundTime); sb.append("fg");
4802 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4803 sb.append("\n"); sb.append(prefix); sb.append(" ");
4804 boolean hasOne = false;
4805 if (starts != 0) {
4806 hasOne = true;
4807 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004808 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004809 if (numCrashes != 0) {
4810 if (hasOne) {
4811 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004812 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004813 hasOne = true;
4814 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004815 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004816 if (numAnrs != 0) {
4817 if (hasOne) {
4818 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004819 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004820 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 }
4822 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004823 pw.println(sb.toString());
4824 for (int e=0; e<numExcessive; e++) {
4825 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4826 if (ew != null) {
4827 pw.print(prefix); pw.print(" * Killed for ");
4828 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4829 pw.print("wake lock");
4830 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4831 pw.print("cpu");
4832 } else {
4833 pw.print("unknown");
4834 }
4835 pw.print(" use: ");
4836 TimeUtils.formatDuration(ew.usedTime, pw);
4837 pw.print(" over ");
4838 TimeUtils.formatDuration(ew.overTime, pw);
4839 if (ew.overTime != 0) {
4840 pw.print(" (");
4841 pw.print((ew.usedTime*100)/ew.overTime);
4842 pw.println("%)");
4843 }
4844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004845 }
4846 uidActivity = true;
4847 }
4848 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004849
4850 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4851 = u.getPackageStats();
4852 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4853 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4854 pw.println(":");
4855 boolean apkActivity = false;
4856 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4857 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4858 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4859 pw.print(prefix); pw.print(" Wakeup alarm ");
4860 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4861 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4862 pw.println(" times");
4863 apkActivity = true;
4864 }
4865 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4866 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4867 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4868 final long startTime = ss.getStartTime(batteryUptime, which);
4869 final int starts = ss.getStarts(which);
4870 final int launches = ss.getLaunches(which);
4871 if (startTime != 0 || starts != 0 || launches != 0) {
4872 sb.setLength(0);
4873 sb.append(prefix); sb.append(" Service ");
4874 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4875 sb.append(prefix); sb.append(" Created for: ");
4876 formatTimeMs(sb, startTime / 1000);
4877 sb.append("uptime\n");
4878 sb.append(prefix); sb.append(" Starts: ");
4879 sb.append(starts);
4880 sb.append(", launches: "); sb.append(launches);
4881 pw.println(sb.toString());
4882 apkActivity = true;
4883 }
4884 }
4885 if (!apkActivity) {
4886 pw.print(prefix); pw.println(" (nothing executed)");
4887 }
4888 uidActivity = true;
4889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004891 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 }
4893 }
4894 }
4895
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004896 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004897 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004898 int diff = oldval ^ newval;
4899 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004900 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004901 for (int i=0; i<descriptions.length; i++) {
4902 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004903 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004904 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004905 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004906 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004907 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004908 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4909 didWake = true;
4910 pw.print("=");
4911 if (longNames) {
4912 UserHandle.formatUid(pw, wakelockTag.uid);
4913 pw.print(":\"");
4914 pw.print(wakelockTag.string);
4915 pw.print("\"");
4916 } else {
4917 pw.print(wakelockTag.poolIdx);
4918 }
4919 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004920 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004921 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004922 pw.print("=");
4923 int val = (newval&bd.mask)>>bd.shift;
4924 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004925 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004926 } else {
4927 pw.print(val);
4928 }
4929 }
4930 }
4931 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004932 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004933 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004934 if (longNames) {
4935 UserHandle.formatUid(pw, wakelockTag.uid);
4936 pw.print(":\"");
4937 pw.print(wakelockTag.string);
4938 pw.print("\"");
4939 } else {
4940 pw.print(wakelockTag.poolIdx);
4941 }
4942 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004943 }
4944
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004945 public void prepareForDumpLocked() {
4946 }
4947
4948 public static class HistoryPrinter {
4949 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004950 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004951 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004952 int oldStatus = -1;
4953 int oldHealth = -1;
4954 int oldPlug = -1;
4955 int oldTemp = -1;
4956 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004957 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004958 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004959
Dianne Hackborn3251b902014-06-20 14:40:53 -07004960 void reset() {
4961 oldState = oldState2 = 0;
4962 oldLevel = -1;
4963 oldStatus = -1;
4964 oldHealth = -1;
4965 oldPlug = -1;
4966 oldTemp = -1;
4967 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004968 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07004969 }
4970
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004971 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004972 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004973 if (!checkin) {
4974 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004975 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004976 pw.print(" (");
4977 pw.print(rec.numReadInts);
4978 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004979 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004980 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4981 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004982 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004983 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004984 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004985 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004986 }
4987 lastTime = rec.time;
4988 }
4989 if (rec.cmd == HistoryItem.CMD_START) {
4990 if (checkin) {
4991 pw.print(":");
4992 }
4993 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004994 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004995 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4996 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004997 if (checkin) {
4998 pw.print(":");
4999 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005000 if (rec.cmd == HistoryItem.CMD_RESET) {
5001 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005002 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005003 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005004 pw.print("TIME:");
5005 if (checkin) {
5006 pw.println(rec.currentTime);
5007 } else {
5008 pw.print(" ");
5009 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5010 rec.currentTime).toString());
5011 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005012 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5013 if (checkin) {
5014 pw.print(":");
5015 }
5016 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005017 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5018 if (checkin) {
5019 pw.print(":");
5020 }
5021 pw.println("*OVERFLOW*");
5022 } else {
5023 if (!checkin) {
5024 if (rec.batteryLevel < 10) pw.print("00");
5025 else if (rec.batteryLevel < 100) pw.print("0");
5026 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005027 if (verbose) {
5028 pw.print(" ");
5029 if (rec.states < 0) ;
5030 else if (rec.states < 0x10) pw.print("0000000");
5031 else if (rec.states < 0x100) pw.print("000000");
5032 else if (rec.states < 0x1000) pw.print("00000");
5033 else if (rec.states < 0x10000) pw.print("0000");
5034 else if (rec.states < 0x100000) pw.print("000");
5035 else if (rec.states < 0x1000000) pw.print("00");
5036 else if (rec.states < 0x10000000) pw.print("0");
5037 pw.print(Integer.toHexString(rec.states));
5038 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005039 } else {
5040 if (oldLevel != rec.batteryLevel) {
5041 oldLevel = rec.batteryLevel;
5042 pw.print(",Bl="); pw.print(rec.batteryLevel);
5043 }
5044 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005045 if (oldStatus != rec.batteryStatus) {
5046 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005047 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005048 switch (oldStatus) {
5049 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005050 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005051 break;
5052 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005053 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005054 break;
5055 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005056 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005057 break;
5058 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005059 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005060 break;
5061 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005062 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005063 break;
5064 default:
5065 pw.print(oldStatus);
5066 break;
5067 }
5068 }
5069 if (oldHealth != rec.batteryHealth) {
5070 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005071 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005072 switch (oldHealth) {
5073 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005074 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005075 break;
5076 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005077 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005078 break;
5079 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005080 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005081 break;
5082 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005083 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005084 break;
5085 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005086 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005087 break;
5088 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005089 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005090 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005091 case BatteryManager.BATTERY_HEALTH_COLD:
5092 pw.print(checkin ? "c" : "cold");
5093 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005094 default:
5095 pw.print(oldHealth);
5096 break;
5097 }
5098 }
5099 if (oldPlug != rec.batteryPlugType) {
5100 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005101 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005102 switch (oldPlug) {
5103 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005104 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005105 break;
5106 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005107 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005108 break;
5109 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005110 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005111 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005112 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005113 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005114 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005115 default:
5116 pw.print(oldPlug);
5117 break;
5118 }
5119 }
5120 if (oldTemp != rec.batteryTemperature) {
5121 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005122 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005123 pw.print(oldTemp);
5124 }
5125 if (oldVolt != rec.batteryVoltage) {
5126 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005127 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005128 pw.print(oldVolt);
5129 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005130 final int chargeMAh = rec.batteryChargeUAh / 1000;
5131 if (oldChargeMAh != chargeMAh) {
5132 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005133 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005134 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005135 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005136 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005137 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005138 printBitDescriptions(pw, oldState2, rec.states2, null,
5139 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005140 if (rec.wakeReasonTag != null) {
5141 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005142 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005143 pw.print(rec.wakeReasonTag.poolIdx);
5144 } else {
5145 pw.print(" wake_reason=");
5146 pw.print(rec.wakeReasonTag.uid);
5147 pw.print(":\"");
5148 pw.print(rec.wakeReasonTag.string);
5149 pw.print("\"");
5150 }
5151 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005152 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005153 pw.print(checkin ? "," : " ");
5154 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5155 pw.print("+");
5156 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5157 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005158 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005159 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5160 : HISTORY_EVENT_NAMES;
5161 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5162 | HistoryItem.EVENT_FLAG_FINISH);
5163 if (idx >= 0 && idx < eventNames.length) {
5164 pw.print(eventNames[idx]);
5165 } else {
5166 pw.print(checkin ? "Ev" : "event");
5167 pw.print(idx);
5168 }
5169 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005170 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005171 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005172 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005173 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5174 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005175 pw.print(":\"");
5176 pw.print(rec.eventTag.string);
5177 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005178 }
5179 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005180 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005181 if (rec.stepDetails != null) {
5182 if (!checkin) {
5183 pw.print(" Details: cpu=");
5184 pw.print(rec.stepDetails.userTime);
5185 pw.print("u+");
5186 pw.print(rec.stepDetails.systemTime);
5187 pw.print("s");
5188 if (rec.stepDetails.appCpuUid1 >= 0) {
5189 pw.print(" (");
5190 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5191 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5192 if (rec.stepDetails.appCpuUid2 >= 0) {
5193 pw.print(", ");
5194 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5195 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5196 }
5197 if (rec.stepDetails.appCpuUid3 >= 0) {
5198 pw.print(", ");
5199 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5200 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5201 }
5202 pw.print(')');
5203 }
5204 pw.println();
5205 pw.print(" /proc/stat=");
5206 pw.print(rec.stepDetails.statUserTime);
5207 pw.print(" usr, ");
5208 pw.print(rec.stepDetails.statSystemTime);
5209 pw.print(" sys, ");
5210 pw.print(rec.stepDetails.statIOWaitTime);
5211 pw.print(" io, ");
5212 pw.print(rec.stepDetails.statIrqTime);
5213 pw.print(" irq, ");
5214 pw.print(rec.stepDetails.statSoftIrqTime);
5215 pw.print(" sirq, ");
5216 pw.print(rec.stepDetails.statIdlTime);
5217 pw.print(" idle");
5218 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5219 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5220 + rec.stepDetails.statSoftIrqTime;
5221 int total = totalRun + rec.stepDetails.statIdlTime;
5222 if (total > 0) {
5223 pw.print(" (");
5224 float perc = ((float)totalRun) / ((float)total) * 100;
5225 pw.print(String.format("%.1f%%", perc));
5226 pw.print(" of ");
5227 StringBuilder sb = new StringBuilder(64);
5228 formatTimeMsNoSpace(sb, total*10);
5229 pw.print(sb);
5230 pw.print(")");
5231 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005232 pw.print(", PlatformIdleStat ");
5233 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005234 pw.println();
5235 } else {
5236 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5237 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5238 pw.print(rec.stepDetails.userTime);
5239 pw.print(":");
5240 pw.print(rec.stepDetails.systemTime);
5241 if (rec.stepDetails.appCpuUid1 >= 0) {
5242 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5243 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5244 if (rec.stepDetails.appCpuUid2 >= 0) {
5245 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5246 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5247 }
5248 if (rec.stepDetails.appCpuUid3 >= 0) {
5249 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5250 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5251 }
5252 }
5253 pw.println();
5254 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5255 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5256 pw.print(rec.stepDetails.statUserTime);
5257 pw.print(',');
5258 pw.print(rec.stepDetails.statSystemTime);
5259 pw.print(',');
5260 pw.print(rec.stepDetails.statIOWaitTime);
5261 pw.print(',');
5262 pw.print(rec.stepDetails.statIrqTime);
5263 pw.print(',');
5264 pw.print(rec.stepDetails.statSoftIrqTime);
5265 pw.print(',');
5266 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005267 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005268 if (rec.stepDetails.statPlatformIdleState != null) {
5269 pw.print(rec.stepDetails.statPlatformIdleState);
5270 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005271 pw.println();
5272 }
5273 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005274 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005275 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005276 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005277 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005278
5279 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5280 UserHandle.formatUid(pw, uid);
5281 pw.print("=");
5282 pw.print(utime);
5283 pw.print("u+");
5284 pw.print(stime);
5285 pw.print("s");
5286 }
5287
5288 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5289 pw.print('/');
5290 pw.print(uid);
5291 pw.print(":");
5292 pw.print(utime);
5293 pw.print(":");
5294 pw.print(stime);
5295 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005296 }
5297
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005298 private void printSizeValue(PrintWriter pw, long size) {
5299 float result = size;
5300 String suffix = "";
5301 if (result >= 10*1024) {
5302 suffix = "KB";
5303 result = result / 1024;
5304 }
5305 if (result >= 10*1024) {
5306 suffix = "MB";
5307 result = result / 1024;
5308 }
5309 if (result >= 10*1024) {
5310 suffix = "GB";
5311 result = result / 1024;
5312 }
5313 if (result >= 10*1024) {
5314 suffix = "TB";
5315 result = result / 1024;
5316 }
5317 if (result >= 10*1024) {
5318 suffix = "PB";
5319 result = result / 1024;
5320 }
5321 pw.print((int)result);
5322 pw.print(suffix);
5323 }
5324
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005325 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5326 String label3, long estimatedTime) {
5327 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005328 return false;
5329 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005330 pw.print(label1);
5331 pw.print(label2);
5332 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005333 StringBuilder sb = new StringBuilder(64);
5334 formatTimeMs(sb, estimatedTime);
5335 pw.print(sb);
5336 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005337 return true;
5338 }
5339
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005340 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5341 LevelStepTracker steps, boolean checkin) {
5342 if (steps == null) {
5343 return false;
5344 }
5345 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005346 if (count <= 0) {
5347 return false;
5348 }
5349 if (!checkin) {
5350 pw.println(header);
5351 }
Kweku Adams030980a2015-04-01 16:07:48 -07005352 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005353 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005354 long duration = steps.getDurationAt(i);
5355 int level = steps.getLevelAt(i);
5356 long initMode = steps.getInitModeAt(i);
5357 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005358 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005359 lineArgs[0] = Long.toString(duration);
5360 lineArgs[1] = Integer.toString(level);
5361 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5362 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5363 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5364 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5365 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5366 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005367 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005368 }
5369 } else {
5370 lineArgs[2] = "";
5371 }
5372 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5373 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5374 } else {
5375 lineArgs[3] = "";
5376 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005377 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005378 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005379 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005380 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005381 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005382 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5383 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005384 pw.print(prefix);
5385 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005386 TimeUtils.formatDuration(duration, pw);
5387 pw.print(" to "); pw.print(level);
5388 boolean haveModes = false;
5389 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5390 pw.print(" (");
5391 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5392 case Display.STATE_OFF: pw.print("screen-off"); break;
5393 case Display.STATE_ON: pw.print("screen-on"); break;
5394 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5395 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005396 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005397 }
5398 haveModes = true;
5399 }
5400 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5401 pw.print(haveModes ? ", " : " (");
5402 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5403 ? "power-save-on" : "power-save-off");
5404 haveModes = true;
5405 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005406 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5407 pw.print(haveModes ? ", " : " (");
5408 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5409 ? "device-idle-on" : "device-idle-off");
5410 haveModes = true;
5411 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005412 if (haveModes) {
5413 pw.print(")");
5414 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005415 pw.println();
5416 }
5417 }
5418 return true;
5419 }
5420
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005421 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005422 public static final int DUMP_DAILY_ONLY = 1<<2;
5423 public static final int DUMP_HISTORY_ONLY = 1<<3;
5424 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5425 public static final int DUMP_VERBOSE = 1<<5;
5426 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005427
Dianne Hackborn37de0982014-05-09 09:32:18 -07005428 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5429 final HistoryPrinter hprinter = new HistoryPrinter();
5430 final HistoryItem rec = new HistoryItem();
5431 long lastTime = -1;
5432 long baseTime = -1;
5433 boolean printed = false;
5434 HistoryEventTracker tracker = null;
5435 while (getNextHistoryLocked(rec)) {
5436 lastTime = rec.time;
5437 if (baseTime < 0) {
5438 baseTime = lastTime;
5439 }
5440 if (rec.time >= histStart) {
5441 if (histStart >= 0 && !printed) {
5442 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005443 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005444 || rec.cmd == HistoryItem.CMD_START
5445 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005446 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005447 hprinter.printNextItem(pw, rec, baseTime, checkin,
5448 (flags&DUMP_VERBOSE) != 0);
5449 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005450 } else if (rec.currentTime != 0) {
5451 printed = true;
5452 byte cmd = rec.cmd;
5453 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005454 hprinter.printNextItem(pw, rec, baseTime, checkin,
5455 (flags&DUMP_VERBOSE) != 0);
5456 rec.cmd = cmd;
5457 }
5458 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005459 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5460 hprinter.printNextItem(pw, rec, baseTime, checkin,
5461 (flags&DUMP_VERBOSE) != 0);
5462 rec.cmd = HistoryItem.CMD_UPDATE;
5463 }
5464 int oldEventCode = rec.eventCode;
5465 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005466 rec.eventTag = new HistoryTag();
5467 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5468 HashMap<String, SparseIntArray> active
5469 = tracker.getStateForEvent(i);
5470 if (active == null) {
5471 continue;
5472 }
5473 for (HashMap.Entry<String, SparseIntArray> ent
5474 : active.entrySet()) {
5475 SparseIntArray uids = ent.getValue();
5476 for (int j=0; j<uids.size(); j++) {
5477 rec.eventCode = i;
5478 rec.eventTag.string = ent.getKey();
5479 rec.eventTag.uid = uids.keyAt(j);
5480 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005481 hprinter.printNextItem(pw, rec, baseTime, checkin,
5482 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005483 rec.wakeReasonTag = null;
5484 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005485 }
5486 }
5487 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005488 rec.eventCode = oldEventCode;
5489 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005490 tracker = null;
5491 }
5492 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005493 hprinter.printNextItem(pw, rec, baseTime, checkin,
5494 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005495 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5496 // This is an attempt to aggregate the previous state and generate
5497 // fake events to reflect that state at the point where we start
5498 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005499 if (tracker == null) {
5500 tracker = new HistoryEventTracker();
5501 }
5502 tracker.updateState(rec.eventCode, rec.eventTag.string,
5503 rec.eventTag.uid, rec.eventTag.poolIdx);
5504 }
5505 }
5506 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005507 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005508 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5509 }
5510 }
5511
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005512 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5513 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5514 if (steps == null) {
5515 return;
5516 }
5517 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5518 if (timeRemaining >= 0) {
5519 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5520 tmpSb.setLength(0);
5521 formatTimeMs(tmpSb, timeRemaining);
5522 pw.print(tmpSb);
5523 pw.print(" (from "); pw.print(tmpOutInt[0]);
5524 pw.println(" steps)");
5525 }
5526 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5527 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5528 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5529 if (estimatedTime > 0) {
5530 pw.print(prefix); pw.print(label); pw.print(" ");
5531 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5532 pw.print(" time: ");
5533 tmpSb.setLength(0);
5534 formatTimeMs(tmpSb, estimatedTime);
5535 pw.print(tmpSb);
5536 pw.print(" (from "); pw.print(tmpOutInt[0]);
5537 pw.println(" steps)");
5538 }
5539 }
5540 }
5541
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005542 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5543 ArrayList<PackageChange> changes) {
5544 if (changes == null) {
5545 return;
5546 }
5547 pw.print(prefix); pw.println("Package changes:");
5548 for (int i=0; i<changes.size(); i++) {
5549 PackageChange pc = changes.get(i);
5550 if (pc.mUpdate) {
5551 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5552 pw.print(" vers="); pw.println(pc.mVersionCode);
5553 } else {
5554 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5555 }
5556 }
5557 }
5558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 /**
5560 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5561 *
5562 * @param pw a Printer to receive the dump output.
5563 */
5564 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005565 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005566 prepareForDumpLocked();
5567
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005568 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005569 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005570
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005571 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005572 final long historyTotalSize = getHistoryTotalSize();
5573 final long historyUsedSize = getHistoryUsedSize();
5574 if (startIteratingHistoryLocked()) {
5575 try {
5576 pw.print("Battery History (");
5577 pw.print((100*historyUsedSize)/historyTotalSize);
5578 pw.print("% used, ");
5579 printSizeValue(pw, historyUsedSize);
5580 pw.print(" used of ");
5581 printSizeValue(pw, historyTotalSize);
5582 pw.print(", ");
5583 pw.print(getHistoryStringPoolSize());
5584 pw.print(" strings using ");
5585 printSizeValue(pw, getHistoryStringPoolBytes());
5586 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005587 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005588 pw.println();
5589 } finally {
5590 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005591 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005592 }
5593
5594 if (startIteratingOldHistoryLocked()) {
5595 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005596 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005597 pw.println("Old battery History:");
5598 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005599 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005600 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005601 if (baseTime < 0) {
5602 baseTime = rec.time;
5603 }
5604 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005605 }
5606 pw.println();
5607 } finally {
5608 finishIteratingOldHistoryLocked();
5609 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005610 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005611 }
5612
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005613 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005614 return;
5615 }
5616
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005617 if (!filtering) {
5618 SparseArray<? extends Uid> uidStats = getUidStats();
5619 final int NU = uidStats.size();
5620 boolean didPid = false;
5621 long nowRealtime = SystemClock.elapsedRealtime();
5622 for (int i=0; i<NU; i++) {
5623 Uid uid = uidStats.valueAt(i);
5624 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5625 if (pids != null) {
5626 for (int j=0; j<pids.size(); j++) {
5627 Uid.Pid pid = pids.valueAt(j);
5628 if (!didPid) {
5629 pw.println("Per-PID Stats:");
5630 didPid = true;
5631 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005632 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5633 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005634 pw.print(" PID "); pw.print(pids.keyAt(j));
5635 pw.print(" wake time: ");
5636 TimeUtils.formatDuration(time, pw);
5637 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005638 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005639 }
5640 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005641 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005642 pw.println();
5643 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005644 }
5645
5646 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005647 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5648 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005649 long timeRemaining = computeBatteryTimeRemaining(
5650 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005651 if (timeRemaining >= 0) {
5652 pw.print(" Estimated discharge time remaining: ");
5653 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5654 pw.println();
5655 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005656 final LevelStepTracker steps = getDischargeLevelStepTracker();
5657 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5658 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5659 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5660 STEP_LEVEL_MODE_VALUES[i], null));
5661 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005662 pw.println();
5663 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005664 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5665 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005666 long timeRemaining = computeChargeTimeRemaining(
5667 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005668 if (timeRemaining >= 0) {
5669 pw.print(" Estimated charge time remaining: ");
5670 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5671 pw.println();
5672 }
5673 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005674 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005675 }
5676 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5677 pw.println("Daily stats:");
5678 pw.print(" Current start time: ");
5679 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5680 getCurrentDailyStartTime()).toString());
5681 pw.print(" Next min deadline: ");
5682 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5683 getNextMinDailyDeadline()).toString());
5684 pw.print(" Next max deadline: ");
5685 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5686 getNextMaxDailyDeadline()).toString());
5687 StringBuilder sb = new StringBuilder(64);
5688 int[] outInt = new int[1];
5689 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5690 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005691 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5692 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005693 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005694 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5695 dsteps, false)) {
5696 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5697 sb, outInt);
5698 }
5699 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5700 csteps, false)) {
5701 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5702 sb, outInt);
5703 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005704 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005705 } else {
5706 pw.println(" Current daily steps:");
5707 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5708 sb, outInt);
5709 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5710 sb, outInt);
5711 }
5712 }
5713 DailyItem dit;
5714 int curIndex = 0;
5715 while ((dit=getDailyItemLocked(curIndex)) != null) {
5716 curIndex++;
5717 if ((flags&DUMP_DAILY_ONLY) != 0) {
5718 pw.println();
5719 }
5720 pw.print(" Daily from ");
5721 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5722 pw.print(" to ");
5723 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5724 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005725 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005726 if (dumpDurationSteps(pw, " ",
5727 " Discharge step durations:", dit.mDischargeSteps, false)) {
5728 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5729 sb, outInt);
5730 }
5731 if (dumpDurationSteps(pw, " ",
5732 " Charge step durations:", dit.mChargeSteps, false)) {
5733 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5734 sb, outInt);
5735 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005736 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005737 } else {
5738 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5739 sb, outInt);
5740 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5741 sb, outInt);
5742 }
5743 }
5744 pw.println();
5745 }
5746 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005747 pw.println("Statistics since last charge:");
5748 pw.println(" System starts: " + getStartCount()
5749 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005750 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5751 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005752 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 }
5755
5756 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005757 public void dumpCheckinLocked(Context context, PrintWriter pw,
5758 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005759 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005760
5761 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005762 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5763 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005764
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005765 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5766
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005767 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005768 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005769
5770 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005771 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005772 try {
5773 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5774 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5775 pw.print(HISTORY_STRING_POOL); pw.print(',');
5776 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005777 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005778 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005779 pw.print(",\"");
5780 String str = getHistoryTagPoolString(i);
5781 str = str.replace("\\", "\\\\");
5782 str = str.replace("\"", "\\\"");
5783 pw.print(str);
5784 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005785 pw.println();
5786 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005787 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005788 } finally {
5789 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005790 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005791 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005792 }
5793
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005794 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005795 return;
5796 }
5797
Dianne Hackborne4a59512010-12-07 11:08:07 -08005798 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005799 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005800 for (int i=0; i<apps.size(); i++) {
5801 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005802 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
5803 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005804 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005805 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
5806 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005807 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005808 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005809 }
5810 SparseArray<? extends Uid> uidStats = getUidStats();
5811 final int NU = uidStats.size();
5812 String[] lineArgs = new String[2];
5813 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005814 int uid = UserHandle.getAppId(uidStats.keyAt(i));
5815 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
5816 if (pkgs != null && !pkgs.second.value) {
5817 pkgs.second.value = true;
5818 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005819 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005820 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005821 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5822 (Object[])lineArgs);
5823 }
5824 }
5825 }
5826 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005827 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005828 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005829 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07005830 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005831 if (timeRemaining >= 0) {
5832 lineArgs[0] = Long.toString(timeRemaining);
5833 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5834 (Object[])lineArgs);
5835 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005836 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07005837 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005838 if (timeRemaining >= 0) {
5839 lineArgs[0] = Long.toString(timeRemaining);
5840 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5841 (Object[])lineArgs);
5842 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005843 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5844 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847}