blob: dd11f68ffe509700c45d43914d5f98b506367b3e [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:
Bookatz867c0d72017-03-07 18:23:42 -0800185 * - Sensor, BluetoothScan, WifiScan get background timers and counter.
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 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700395 * Returns whether the timer is currently running. Some types of timers
396 * (e.g. BatchTimers) don't know whether the event is currently active,
397 * and report false.
398 */
399 public boolean isRunningLocked() {
400 return false;
401 }
402
403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * Temporary for debugging.
405 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700406 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 }
408
409 /**
410 * The statistics associated with a particular uid.
411 */
412 public static abstract class Uid {
413
414 /**
415 * Returns a mapping containing wakelock statistics.
416 *
417 * @return a Map from Strings to Uid.Wakelock objects.
418 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700419 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420
421 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700422 * Returns a mapping containing sync statistics.
423 *
424 * @return a Map from Strings to Timer objects.
425 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700426 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700427
428 /**
429 * Returns a mapping containing scheduled job statistics.
430 *
431 * @return a Map from Strings to Timer objects.
432 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700433 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700434
435 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 * The statistics associated with a particular wake lock.
437 */
438 public static abstract class Wakelock {
439 public abstract Timer getWakeTime(int type);
440 }
441
442 /**
443 * Returns a mapping containing sensor statistics.
444 *
445 * @return a Map from Integer sensor ids to Uid.Sensor objects.
446 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700447 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448
449 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700450 * Returns a mapping containing active process data.
451 */
452 public abstract SparseArray<? extends Pid> getPidStats();
453
454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 * Returns a mapping containing process statistics.
456 *
457 * @return a Map from Strings to Uid.Proc objects.
458 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700459 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460
461 /**
462 * Returns a mapping containing package statistics.
463 *
464 * @return a Map from Strings to Uid.Pkg objects.
465 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700466 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700467
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800468 public abstract ControllerActivityCounter getWifiControllerActivity();
469 public abstract ControllerActivityCounter getBluetoothControllerActivity();
470 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800471
472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 * {@hide}
474 */
475 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700476
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800477 public abstract void noteWifiRunningLocked(long elapsedRealtime);
478 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
479 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
480 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
481 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
482 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
483 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
484 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
485 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
486 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800487 public abstract void noteActivityResumedLocked(long elapsedRealtime);
488 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800489 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
490 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
491 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700492 public abstract int getWifiScanCount(int which);
Bookatz867c0d72017-03-07 18:23:42 -0800493 public abstract int getWifiScanBackgroundCount(int which);
494 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
495 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800496 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700497 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800498 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700499 public abstract Timer getAudioTurnedOnTimer();
500 public abstract Timer getVideoTurnedOnTimer();
501 public abstract Timer getFlashlightTurnedOnTimer();
502 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700503 public abstract Timer getForegroundActivityTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800504 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800505 public abstract Timer getBluetoothScanBackgroundTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700506
Dianne Hackborna0200e32016-03-30 18:01:41 -0700507 // Note: the following times are disjoint. They can be added together to find the
508 // total time a uid has had any processes running at all.
509
510 /**
511 * Time this uid has any processes in the top state (or above such as persistent).
512 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800513 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700514 /**
515 * Time this uid has any process with a started out bound foreground service, but
516 * none in the "top" state.
517 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800518 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700519 /**
520 * Time this uid has any process that is top while the device is sleeping, but none
521 * in the "foreground service" or better state.
522 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800523 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700524 /**
525 * Time this uid has any process in an active foreground state, but none in the
526 * "top sleeping" or better state.
527 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800528 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700529 /**
530 * Time this uid has any process in an active background state, but none in the
531 * "foreground" or better state.
532 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800533 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700534 /**
535 * Time this uid has any processes that are sitting around cached, not in one of the
536 * other active states.
537 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800538 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700539 /**
540 * Total number of process states we track.
541 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800542 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700543
544 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800545 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700546 };
547
548 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800549 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700550
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800551 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552
Robert Greenwalta029ea12013-09-25 16:38:12 -0700553 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
554
Dianne Hackborn617f8772009-03-31 15:04:46 -0700555 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700556 * Note that these must match the constants in android.os.PowerManager.
557 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
558 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700559 */
560 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700561 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700562 };
563
Phil Weaverda80d672016-03-15 16:25:46 -0700564 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700565
Dianne Hackborn617f8772009-03-31 15:04:46 -0700566 public abstract void noteUserActivityLocked(int type);
567 public abstract boolean hasUserActivity();
568 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700569
570 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800571 public abstract long getNetworkActivityBytes(int type, int which);
572 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800573 public abstract long getMobileRadioActiveTime(int which);
574 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700575
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700576 /**
577 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
578 */
579 public abstract long getUserCpuTimeUs(int which);
580
581 /**
582 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
583 */
584 public abstract long getSystemCpuTimeUs(int which);
585
586 /**
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700587 * Get the total cpu power consumed (in milli-ampere-microseconds).
588 */
589 public abstract long getCpuPowerMaUs(int which);
590
591 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700592 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
593 * given CPU cluster.
594 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700595 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700596 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700597 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
598 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700599 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700600 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700601
Adam Lesinski5f056f62016-07-14 16:56:08 -0700602 /**
603 * Returns the number of times this UID woke up the Application Processor to
604 * process a mobile radio packet.
605 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
606 */
607 public abstract long getMobileRadioApWakeupCount(int which);
608
609 /**
610 * Returns the number of times this UID woke up the Application Processor to
611 * process a WiFi packet.
612 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
613 */
614 public abstract long getWifiRadioApWakeupCount(int which);
615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800617 /*
618 * FIXME: it's not correct to use this magic value because it
619 * could clash with a sensor handle (which are defined by
620 * the sensor HAL, and therefore out of our control
621 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 // Magic sensor number for the GPS.
623 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800628
Bookatz867c0d72017-03-07 18:23:42 -0800629 /** Returns a Timer for sensor usage when app is in the background. */
630 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700633 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800634 public int mWakeNesting;
635 public long mWakeSumMs;
636 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700637 }
638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 /**
640 * The statistics associated with a particular process.
641 */
642 public static abstract class Proc {
643
Dianne Hackborn287952c2010-09-22 22:34:31 -0700644 public static class ExcessivePower {
645 public static final int TYPE_WAKE = 1;
646 public static final int TYPE_CPU = 2;
647
648 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700649 public long overTime;
650 public long usedTime;
651 }
652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800654 * Returns true if this process is still active in the battery stats.
655 */
656 public abstract boolean isActive();
657
658 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700659 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700661 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 */
663 public abstract long getUserTime(int which);
664
665 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700666 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700668 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 */
670 public abstract long getSystemTime(int which);
671
672 /**
673 * Returns the number of times the process has been started.
674 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700675 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 */
677 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700678
679 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800680 * Returns the number of times the process has crashed.
681 *
682 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
683 */
684 public abstract int getNumCrashes(int which);
685
686 /**
687 * Returns the number of times the process has ANRed.
688 *
689 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
690 */
691 public abstract int getNumAnrs(int which);
692
693 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700694 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700695 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700696 * @return foreground cpu time in microseconds
697 */
698 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700699
Dianne Hackborn287952c2010-09-22 22:34:31 -0700700 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700701
Dianne Hackborn287952c2010-09-22 22:34:31 -0700702 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 }
704
705 /**
706 * The statistics associated with a particular package.
707 */
708 public static abstract class Pkg {
709
710 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700711 * Returns information about all wakeup alarms that have been triggered for this
712 * package. The mapping keys are tag names for the alarms, the counter contains
713 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700715 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716
717 /**
718 * Returns a mapping containing service statistics.
719 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700720 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721
722 /**
723 * The statistics associated with a particular service.
724 */
Joe Onoratoabded112016-02-08 16:49:39 -0800725 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
727 /**
728 * Returns the amount of time spent started.
729 *
730 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700731 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 * @return
733 */
734 public abstract long getStartTime(long batteryUptime, int which);
735
736 /**
737 * Returns the total number of times startService() has been called.
738 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700739 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 */
741 public abstract int getStarts(int which);
742
743 /**
744 * Returns the total number times the service has been launched.
745 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700746 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 */
748 public abstract int getLaunches(int which);
749 }
750 }
751 }
752
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800753 public static final class LevelStepTracker {
754 public long mLastStepTime = -1;
755 public int mNumStepDurations;
756 public final long[] mStepDurations;
757
758 public LevelStepTracker(int maxLevelSteps) {
759 mStepDurations = new long[maxLevelSteps];
760 }
761
762 public LevelStepTracker(int numSteps, long[] steps) {
763 mNumStepDurations = numSteps;
764 mStepDurations = new long[numSteps];
765 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
766 }
767
768 public long getDurationAt(int index) {
769 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
770 }
771
772 public int getLevelAt(int index) {
773 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
774 >> STEP_LEVEL_LEVEL_SHIFT);
775 }
776
777 public int getInitModeAt(int index) {
778 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
779 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
780 }
781
782 public int getModModeAt(int index) {
783 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
784 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
785 }
786
787 private void appendHex(long val, int topOffset, StringBuilder out) {
788 boolean hasData = false;
789 while (topOffset >= 0) {
790 int digit = (int)( (val>>topOffset) & 0xf );
791 topOffset -= 4;
792 if (!hasData && digit == 0) {
793 continue;
794 }
795 hasData = true;
796 if (digit >= 0 && digit <= 9) {
797 out.append((char)('0' + digit));
798 } else {
799 out.append((char)('a' + digit - 10));
800 }
801 }
802 }
803
804 public void encodeEntryAt(int index, StringBuilder out) {
805 long item = mStepDurations[index];
806 long duration = item & STEP_LEVEL_TIME_MASK;
807 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
808 >> STEP_LEVEL_LEVEL_SHIFT);
809 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
810 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
811 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
812 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
813 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
814 case Display.STATE_OFF: out.append('f'); break;
815 case Display.STATE_ON: out.append('o'); break;
816 case Display.STATE_DOZE: out.append('d'); break;
817 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
818 }
819 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
820 out.append('p');
821 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700822 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
823 out.append('i');
824 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800825 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
826 case Display.STATE_OFF: out.append('F'); break;
827 case Display.STATE_ON: out.append('O'); break;
828 case Display.STATE_DOZE: out.append('D'); break;
829 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
830 }
831 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
832 out.append('P');
833 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700834 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
835 out.append('I');
836 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800837 out.append('-');
838 appendHex(level, 4, out);
839 out.append('-');
840 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
841 }
842
843 public void decodeEntryAt(int index, String value) {
844 final int N = value.length();
845 int i = 0;
846 char c;
847 long out = 0;
848 while (i < N && (c=value.charAt(i)) != '-') {
849 i++;
850 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800851 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800852 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800853 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800854 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800855 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800856 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800857 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
858 << STEP_LEVEL_INITIAL_MODE_SHIFT);
859 break;
860 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
861 << STEP_LEVEL_INITIAL_MODE_SHIFT);
862 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700863 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
864 << STEP_LEVEL_INITIAL_MODE_SHIFT);
865 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800866 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
867 break;
868 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
869 break;
870 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
871 break;
872 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
873 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
874 break;
875 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
876 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800877 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700878 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
879 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
880 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800881 }
882 }
883 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800884 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800885 while (i < N && (c=value.charAt(i)) != '-') {
886 i++;
887 level <<= 4;
888 if (c >= '0' && c <= '9') {
889 level += c - '0';
890 } else if (c >= 'a' && c <= 'f') {
891 level += c - 'a' + 10;
892 } else if (c >= 'A' && c <= 'F') {
893 level += c - 'A' + 10;
894 }
895 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800896 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800897 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
898 long duration = 0;
899 while (i < N && (c=value.charAt(i)) != '-') {
900 i++;
901 duration <<= 4;
902 if (c >= '0' && c <= '9') {
903 duration += c - '0';
904 } else if (c >= 'a' && c <= 'f') {
905 duration += c - 'a' + 10;
906 } else if (c >= 'A' && c <= 'F') {
907 duration += c - 'A' + 10;
908 }
909 }
910 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
911 }
912
913 public void init() {
914 mLastStepTime = -1;
915 mNumStepDurations = 0;
916 }
917
918 public void clearTime() {
919 mLastStepTime = -1;
920 }
921
922 public long computeTimePerLevel() {
923 final long[] steps = mStepDurations;
924 final int numSteps = mNumStepDurations;
925
926 // For now we'll do a simple average across all steps.
927 if (numSteps <= 0) {
928 return -1;
929 }
930 long total = 0;
931 for (int i=0; i<numSteps; i++) {
932 total += steps[i] & STEP_LEVEL_TIME_MASK;
933 }
934 return total / numSteps;
935 /*
936 long[] buckets = new long[numSteps];
937 int numBuckets = 0;
938 int numToAverage = 4;
939 int i = 0;
940 while (i < numSteps) {
941 long totalTime = 0;
942 int num = 0;
943 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
944 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
945 num++;
946 }
947 buckets[numBuckets] = totalTime / num;
948 numBuckets++;
949 numToAverage *= 2;
950 i += num;
951 }
952 if (numBuckets < 1) {
953 return -1;
954 }
955 long averageTime = buckets[numBuckets-1];
956 for (i=numBuckets-2; i>=0; i--) {
957 averageTime = (averageTime + buckets[i]) / 2;
958 }
959 return averageTime;
960 */
961 }
962
963 public long computeTimeEstimate(long modesOfInterest, long modeValues,
964 int[] outNumOfInterest) {
965 final long[] steps = mStepDurations;
966 final int count = mNumStepDurations;
967 if (count <= 0) {
968 return -1;
969 }
970 long total = 0;
971 int numOfInterest = 0;
972 for (int i=0; i<count; i++) {
973 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
974 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
975 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
976 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
977 // If the modes of interest didn't change during this step period...
978 if ((modMode&modesOfInterest) == 0) {
979 // And the mode values during this period match those we are measuring...
980 if ((initMode&modesOfInterest) == modeValues) {
981 // Then this can be used to estimate the total time!
982 numOfInterest++;
983 total += steps[i] & STEP_LEVEL_TIME_MASK;
984 }
985 }
986 }
987 if (numOfInterest <= 0) {
988 return -1;
989 }
990
991 if (outNumOfInterest != null) {
992 outNumOfInterest[0] = numOfInterest;
993 }
994
995 // The estimated time is the average time we spend in each level, multipled
996 // by 100 -- the total number of battery levels
997 return (total / numOfInterest) * 100;
998 }
999
1000 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1001 int stepCount = mNumStepDurations;
1002 final long lastStepTime = mLastStepTime;
1003 if (lastStepTime >= 0 && numStepLevels > 0) {
1004 final long[] steps = mStepDurations;
1005 long duration = elapsedRealtime - lastStepTime;
1006 for (int i=0; i<numStepLevels; i++) {
1007 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1008 long thisDuration = duration / (numStepLevels-i);
1009 duration -= thisDuration;
1010 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1011 thisDuration = STEP_LEVEL_TIME_MASK;
1012 }
1013 steps[0] = thisDuration | modeBits;
1014 }
1015 stepCount += numStepLevels;
1016 if (stepCount > steps.length) {
1017 stepCount = steps.length;
1018 }
1019 }
1020 mNumStepDurations = stepCount;
1021 mLastStepTime = elapsedRealtime;
1022 }
1023
1024 public void readFromParcel(Parcel in) {
1025 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001026 if (N > mStepDurations.length) {
1027 throw new ParcelFormatException("more step durations than available: " + N);
1028 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001029 mNumStepDurations = N;
1030 for (int i=0; i<N; i++) {
1031 mStepDurations[i] = in.readLong();
1032 }
1033 }
1034
1035 public void writeToParcel(Parcel out) {
1036 final int N = mNumStepDurations;
1037 out.writeInt(N);
1038 for (int i=0; i<N; i++) {
1039 out.writeLong(mStepDurations[i]);
1040 }
1041 }
1042 }
1043
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001044 public static final class PackageChange {
1045 public String mPackageName;
1046 public boolean mUpdate;
1047 public int mVersionCode;
1048 }
1049
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001050 public static final class DailyItem {
1051 public long mStartTime;
1052 public long mEndTime;
1053 public LevelStepTracker mDischargeSteps;
1054 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001055 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001056 }
1057
1058 public abstract DailyItem getDailyItemLocked(int daysAgo);
1059
1060 public abstract long getCurrentDailyStartTime();
1061
1062 public abstract long getNextMinDailyDeadline();
1063
1064 public abstract long getNextMaxDailyDeadline();
1065
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001066 public final static class HistoryTag {
1067 public String string;
1068 public int uid;
1069
1070 public int poolIdx;
1071
1072 public void setTo(HistoryTag o) {
1073 string = o.string;
1074 uid = o.uid;
1075 poolIdx = o.poolIdx;
1076 }
1077
1078 public void setTo(String _string, int _uid) {
1079 string = _string;
1080 uid = _uid;
1081 poolIdx = -1;
1082 }
1083
1084 public void writeToParcel(Parcel dest, int flags) {
1085 dest.writeString(string);
1086 dest.writeInt(uid);
1087 }
1088
1089 public void readFromParcel(Parcel src) {
1090 string = src.readString();
1091 uid = src.readInt();
1092 poolIdx = -1;
1093 }
1094
1095 @Override
1096 public boolean equals(Object o) {
1097 if (this == o) return true;
1098 if (o == null || getClass() != o.getClass()) return false;
1099
1100 HistoryTag that = (HistoryTag) o;
1101
1102 if (uid != that.uid) return false;
1103 if (!string.equals(that.string)) return false;
1104
1105 return true;
1106 }
1107
1108 @Override
1109 public int hashCode() {
1110 int result = string.hashCode();
1111 result = 31 * result + uid;
1112 return result;
1113 }
1114 }
1115
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001116 /**
1117 * Optional detailed information that can go into a history step. This is typically
1118 * generated each time the battery level changes.
1119 */
1120 public final static class HistoryStepDetails {
1121 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1122 public int userTime;
1123 public int systemTime;
1124
1125 // Top three apps using CPU in the last step, with times in 1/100 second.
1126 public int appCpuUid1;
1127 public int appCpuUTime1;
1128 public int appCpuSTime1;
1129 public int appCpuUid2;
1130 public int appCpuUTime2;
1131 public int appCpuSTime2;
1132 public int appCpuUid3;
1133 public int appCpuUTime3;
1134 public int appCpuSTime3;
1135
1136 // Information from /proc/stat
1137 public int statUserTime;
1138 public int statSystemTime;
1139 public int statIOWaitTime;
1140 public int statIrqTime;
1141 public int statSoftIrqTime;
1142 public int statIdlTime;
1143
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001144 // Platform-level low power state stats
1145 public String statPlatformIdleState;
1146
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001147 public HistoryStepDetails() {
1148 clear();
1149 }
1150
1151 public void clear() {
1152 userTime = systemTime = 0;
1153 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1154 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1155 = appCpuUTime3 = appCpuSTime3 = 0;
1156 }
1157
1158 public void writeToParcel(Parcel out) {
1159 out.writeInt(userTime);
1160 out.writeInt(systemTime);
1161 out.writeInt(appCpuUid1);
1162 out.writeInt(appCpuUTime1);
1163 out.writeInt(appCpuSTime1);
1164 out.writeInt(appCpuUid2);
1165 out.writeInt(appCpuUTime2);
1166 out.writeInt(appCpuSTime2);
1167 out.writeInt(appCpuUid3);
1168 out.writeInt(appCpuUTime3);
1169 out.writeInt(appCpuSTime3);
1170 out.writeInt(statUserTime);
1171 out.writeInt(statSystemTime);
1172 out.writeInt(statIOWaitTime);
1173 out.writeInt(statIrqTime);
1174 out.writeInt(statSoftIrqTime);
1175 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001176 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001177 }
1178
1179 public void readFromParcel(Parcel in) {
1180 userTime = in.readInt();
1181 systemTime = in.readInt();
1182 appCpuUid1 = in.readInt();
1183 appCpuUTime1 = in.readInt();
1184 appCpuSTime1 = in.readInt();
1185 appCpuUid2 = in.readInt();
1186 appCpuUTime2 = in.readInt();
1187 appCpuSTime2 = in.readInt();
1188 appCpuUid3 = in.readInt();
1189 appCpuUTime3 = in.readInt();
1190 appCpuSTime3 = in.readInt();
1191 statUserTime = in.readInt();
1192 statSystemTime = in.readInt();
1193 statIOWaitTime = in.readInt();
1194 statIrqTime = in.readInt();
1195 statSoftIrqTime = in.readInt();
1196 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001197 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001198 }
1199 }
1200
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001201 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001202 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001203
1204 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001205 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001206
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001207 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001208 public static final byte CMD_NULL = -1;
1209 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001210 public static final byte CMD_CURRENT_TIME = 5;
1211 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001212 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001213 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001214
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001215 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001216
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001217 /**
1218 * Return whether the command code is a delta data update.
1219 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001220 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001221 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001222 }
1223
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001224 public byte batteryLevel;
1225 public byte batteryStatus;
1226 public byte batteryHealth;
1227 public byte batteryPlugType;
1228
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001229 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001230 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001231
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001232 // The charge of the battery in micro-Ampere-hours.
1233 public int batteryChargeUAh;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001234
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001235 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001236 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001237 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001238 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001239 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1240 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001241 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001242 public static final int STATE_PHONE_STATE_SHIFT = 6;
1243 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001244 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001245 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001246 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001247
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001248 // These states always appear directly in the first int token
1249 // of a delta change; they should be ones that change relatively
1250 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001251 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1252 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001253 public static final int STATE_GPS_ON_FLAG = 1<<29;
1254 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001255 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001256 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001257 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001258 // Do not use, this is used for coulomb delta count.
1259 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001260 // These are on the lower bits used for the command; if they change
1261 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001262 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001263 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001264 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001265 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1266 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1267 // empty slot
1268 // empty slot
1269 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001270
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001271 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001272 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1273
1274 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001275
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001276 public int states;
1277
Dianne Hackborn3251b902014-06-20 14:40:53 -07001278 // Constants from WIFI_SUPPL_STATE_*
1279 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1280 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1281 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1282 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1283 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1284 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1285
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001286 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001287 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1288 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1289 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001290 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001291 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1292 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1293 public static final int STATE2_CHARGING_FLAG = 1<<24;
1294 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1295 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1296 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001297 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001298
1299 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001300 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001301 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1302
1303 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001304
Dianne Hackborn40c87252014-03-19 16:55:40 -07001305 public int states2;
1306
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001307 // The wake lock that was acquired at this point.
1308 public HistoryTag wakelockTag;
1309
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001310 // Kernel wakeup reason at this point.
1311 public HistoryTag wakeReasonTag;
1312
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001313 // Non-null when there is more detailed information at this step.
1314 public HistoryStepDetails stepDetails;
1315
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001316 public static final int EVENT_FLAG_START = 0x8000;
1317 public static final int EVENT_FLAG_FINISH = 0x4000;
1318
1319 // No event in this item.
1320 public static final int EVENT_NONE = 0x0000;
1321 // Event is about a process that is running.
1322 public static final int EVENT_PROC = 0x0001;
1323 // Event is about an application package that is in the foreground.
1324 public static final int EVENT_FOREGROUND = 0x0002;
1325 // Event is about an application package that is at the top of the screen.
1326 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001327 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001328 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001329 // Events for all additional wake locks aquired/release within a wake block.
1330 // These are not generated by default.
1331 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001332 // Event is about an application executing a scheduled job.
1333 public static final int EVENT_JOB = 0x0006;
1334 // Events for users running.
1335 public static final int EVENT_USER_RUNNING = 0x0007;
1336 // Events for foreground user.
1337 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001338 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001339 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001340 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001341 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001342 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001343 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001344 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001345 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001346 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001347 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001348 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001349 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001350 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001351 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001352 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001353 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001354 // Event for a package being on the temporary whitelist.
1355 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001356 // Event for the screen waking up.
1357 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001358 // Event for the UID that woke up the application processor.
1359 // Used for wakeups coming from WiFi, modem, etc.
1360 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001361 // Event for reporting that a specific partial wake lock has been held for a long duration.
1362 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Adam Lesinski041d9172016-12-12 12:03:56 -08001363 // Event reporting the new estimated (learned) capacity of the battery in mAh.
1364 public static final int EVENT_ESTIMATED_BATTERY_CAP = 0x0015;
Amith Yamasani67768492015-06-09 12:23:58 -07001365
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001366 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001367 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001368 // Mask to extract out only the type part of the event.
1369 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001370
1371 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1372 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1373 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1374 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1375 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1376 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001377 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1378 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001379 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1380 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001381 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1382 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1383 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1384 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1385 public static final int EVENT_USER_FOREGROUND_START =
1386 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1387 public static final int EVENT_USER_FOREGROUND_FINISH =
1388 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001389 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1390 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001391 public static final int EVENT_TEMP_WHITELIST_START =
1392 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1393 public static final int EVENT_TEMP_WHITELIST_FINISH =
1394 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001395 public static final int EVENT_LONG_WAKE_LOCK_START =
1396 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1397 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1398 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001399
1400 // For CMD_EVENT.
1401 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001402 public HistoryTag eventTag;
1403
Dianne Hackborn9a755432014-05-15 17:05:22 -07001404 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001405 public long currentTime;
1406
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001407 // Meta-data when reading.
1408 public int numReadInts;
1409
1410 // Pre-allocated objects.
1411 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001412 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001413 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001414
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001415 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001416 }
1417
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001418 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001419 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001420 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001421 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001422 }
1423
1424 public int describeContents() {
1425 return 0;
1426 }
1427
1428 public void writeToParcel(Parcel dest, int flags) {
1429 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001430 int bat = (((int)cmd)&0xff)
1431 | ((((int)batteryLevel)<<8)&0xff00)
1432 | ((((int)batteryStatus)<<16)&0xf0000)
1433 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001434 | ((((int)batteryPlugType)<<24)&0xf000000)
1435 | (wakelockTag != null ? 0x10000000 : 0)
1436 | (wakeReasonTag != null ? 0x20000000 : 0)
1437 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001438 dest.writeInt(bat);
1439 bat = (((int)batteryTemperature)&0xffff)
1440 | ((((int)batteryVoltage)<<16)&0xffff0000);
1441 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001442 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001443 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001444 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001445 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001446 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001447 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001448 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001449 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001450 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001451 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001452 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001453 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001454 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001455 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001456 dest.writeLong(currentTime);
1457 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001458 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001459
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001460 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001461 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001462 int bat = src.readInt();
1463 cmd = (byte)(bat&0xff);
1464 batteryLevel = (byte)((bat>>8)&0xff);
1465 batteryStatus = (byte)((bat>>16)&0xf);
1466 batteryHealth = (byte)((bat>>20)&0xf);
1467 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001468 int bat2 = src.readInt();
1469 batteryTemperature = (short)(bat2&0xffff);
1470 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001471 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001472 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001473 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001474 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001475 wakelockTag = localWakelockTag;
1476 wakelockTag.readFromParcel(src);
1477 } else {
1478 wakelockTag = null;
1479 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001480 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001481 wakeReasonTag = localWakeReasonTag;
1482 wakeReasonTag.readFromParcel(src);
1483 } else {
1484 wakeReasonTag = null;
1485 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001486 if ((bat&0x40000000) != 0) {
1487 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001488 eventTag = localEventTag;
1489 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001490 } else {
1491 eventCode = EVENT_NONE;
1492 eventTag = null;
1493 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001494 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001495 currentTime = src.readLong();
1496 } else {
1497 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001498 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001499 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001500 }
1501
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001502 public void clear() {
1503 time = 0;
1504 cmd = CMD_NULL;
1505 batteryLevel = 0;
1506 batteryStatus = 0;
1507 batteryHealth = 0;
1508 batteryPlugType = 0;
1509 batteryTemperature = 0;
1510 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001511 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001512 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001513 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001514 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001515 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001516 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001517 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001518 }
1519
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001520 public void setTo(HistoryItem o) {
1521 time = o.time;
1522 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001523 setToCommon(o);
1524 }
1525
1526 public void setTo(long time, byte cmd, HistoryItem o) {
1527 this.time = time;
1528 this.cmd = cmd;
1529 setToCommon(o);
1530 }
1531
1532 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001533 batteryLevel = o.batteryLevel;
1534 batteryStatus = o.batteryStatus;
1535 batteryHealth = o.batteryHealth;
1536 batteryPlugType = o.batteryPlugType;
1537 batteryTemperature = o.batteryTemperature;
1538 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001539 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001540 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001541 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001542 if (o.wakelockTag != null) {
1543 wakelockTag = localWakelockTag;
1544 wakelockTag.setTo(o.wakelockTag);
1545 } else {
1546 wakelockTag = null;
1547 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001548 if (o.wakeReasonTag != null) {
1549 wakeReasonTag = localWakeReasonTag;
1550 wakeReasonTag.setTo(o.wakeReasonTag);
1551 } else {
1552 wakeReasonTag = null;
1553 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001554 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001555 if (o.eventTag != null) {
1556 eventTag = localEventTag;
1557 eventTag.setTo(o.eventTag);
1558 } else {
1559 eventTag = null;
1560 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001561 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001562 }
1563
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001564 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001565 return batteryLevel == o.batteryLevel
1566 && batteryStatus == o.batteryStatus
1567 && batteryHealth == o.batteryHealth
1568 && batteryPlugType == o.batteryPlugType
1569 && batteryTemperature == o.batteryTemperature
1570 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001571 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001572 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001573 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001574 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001575 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001576
1577 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001578 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001579 return false;
1580 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001581 if (wakelockTag != o.wakelockTag) {
1582 if (wakelockTag == null || o.wakelockTag == null) {
1583 return false;
1584 }
1585 if (!wakelockTag.equals(o.wakelockTag)) {
1586 return false;
1587 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001588 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001589 if (wakeReasonTag != o.wakeReasonTag) {
1590 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1591 return false;
1592 }
1593 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1594 return false;
1595 }
1596 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001597 if (eventTag != o.eventTag) {
1598 if (eventTag == null || o.eventTag == null) {
1599 return false;
1600 }
1601 if (!eventTag.equals(o.eventTag)) {
1602 return false;
1603 }
1604 }
1605 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001606 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001607 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001608
1609 public final static class HistoryEventTracker {
1610 private final HashMap<String, SparseIntArray>[] mActiveEvents
1611 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1612
1613 public boolean updateState(int code, String name, int uid, int poolIdx) {
1614 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1615 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1616 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1617 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001618 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001619 mActiveEvents[idx] = active;
1620 }
1621 SparseIntArray uids = active.get(name);
1622 if (uids == null) {
1623 uids = new SparseIntArray();
1624 active.put(name, uids);
1625 }
1626 if (uids.indexOfKey(uid) >= 0) {
1627 // Already set, nothing to do!
1628 return false;
1629 }
1630 uids.put(uid, poolIdx);
1631 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1632 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1633 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1634 if (active == null) {
1635 // not currently active, nothing to do.
1636 return false;
1637 }
1638 SparseIntArray uids = active.get(name);
1639 if (uids == null) {
1640 // not currently active, nothing to do.
1641 return false;
1642 }
1643 idx = uids.indexOfKey(uid);
1644 if (idx < 0) {
1645 // not currently active, nothing to do.
1646 return false;
1647 }
1648 uids.removeAt(idx);
1649 if (uids.size() <= 0) {
1650 active.remove(name);
1651 }
1652 }
1653 return true;
1654 }
1655
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001656 public void removeEvents(int code) {
1657 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1658 mActiveEvents[idx] = null;
1659 }
1660
Dianne Hackborn37de0982014-05-09 09:32:18 -07001661 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1662 return mActiveEvents[code];
1663 }
1664 }
1665
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001666 public static final class BitDescription {
1667 public final int mask;
1668 public final int shift;
1669 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001670 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001671 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001672 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001673
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001674 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001675 this.mask = mask;
1676 this.shift = -1;
1677 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001678 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001679 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001680 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001681 }
1682
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001683 public BitDescription(int mask, int shift, String name, String shortName,
1684 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001685 this.mask = mask;
1686 this.shift = shift;
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 = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001690 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001691 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001692 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001693
Dianne Hackbornfc064132014-06-02 12:42:12 -07001694 /**
1695 * Don't allow any more batching in to the current history event. This
1696 * is called when printing partial histories, so to ensure that the next
1697 * history event will go in to a new batch after what was printed in the
1698 * last partial history.
1699 */
1700 public abstract void commitCurrentHistoryBatchLocked();
1701
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001702 public abstract int getHistoryTotalSize();
1703
1704 public abstract int getHistoryUsedSize();
1705
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001706 public abstract boolean startIteratingHistoryLocked();
1707
Dianne Hackborn099bc622014-01-22 13:39:16 -08001708 public abstract int getHistoryStringPoolSize();
1709
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001710 public abstract int getHistoryStringPoolBytes();
1711
1712 public abstract String getHistoryTagPoolString(int index);
1713
1714 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001715
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001716 public abstract boolean getNextHistoryLocked(HistoryItem out);
1717
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001718 public abstract void finishIteratingHistoryLocked();
1719
1720 public abstract boolean startIteratingOldHistoryLocked();
1721
1722 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1723
1724 public abstract void finishIteratingOldHistoryLocked();
1725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001727 * Return the base time offset for the battery history.
1728 */
1729 public abstract long getHistoryBaseTime();
1730
1731 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 * Returns the number of times the device has been started.
1733 */
1734 public abstract int getStartCount();
1735
1736 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001737 * 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 -08001738 * running on battery.
1739 *
1740 * {@hide}
1741 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001742 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001744 /**
1745 * Returns the number of times the screen was turned on.
1746 *
1747 * {@hide}
1748 */
1749 public abstract int getScreenOnCount(int which);
1750
Jeff Browne95c3cd2014-05-02 16:59:26 -07001751 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1752
Dianne Hackborn617f8772009-03-31 15:04:46 -07001753 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1754 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1755 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1756 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1757 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1758
1759 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1760 "dark", "dim", "medium", "light", "bright"
1761 };
1762
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001763 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1764 "0", "1", "2", "3", "4"
1765 };
1766
Dianne Hackborn617f8772009-03-31 15:04:46 -07001767 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001768
Dianne Hackborn617f8772009-03-31 15:04:46 -07001769 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001770 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001771 * the given brightness
1772 *
1773 * {@hide}
1774 */
1775 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001776 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001779 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001780 * running on battery.
1781 *
1782 * {@hide}
1783 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001784 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001785
1786 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001787 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001788 *
1789 * {@hide}
1790 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001791 public abstract int getPowerSaveModeEnabledCount(int which);
1792
1793 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001794 * Constant for device idle mode: not active.
1795 */
1796 public static final int DEVICE_IDLE_MODE_OFF = 0;
1797
1798 /**
1799 * Constant for device idle mode: active in lightweight mode.
1800 */
1801 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1802
1803 /**
1804 * Constant for device idle mode: active in full mode.
1805 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001806 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001807
1808 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001809 * Returns the time in microseconds that device has been in idle mode while
1810 * running on battery.
1811 *
1812 * {@hide}
1813 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001814 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001815
1816 /**
1817 * Returns the number of times that the devie has gone in to idle mode.
1818 *
1819 * {@hide}
1820 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001821 public abstract int getDeviceIdleModeCount(int mode, int which);
1822
1823 /**
1824 * Return the longest duration we spent in a particular device idle mode (fully in the
1825 * mode, not in idle maintenance etc).
1826 */
1827 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001828
1829 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001830 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001831 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001832 * counts all of the time that we consider the device to be idle, whether or not
1833 * it is currently in the actual device idle mode.
1834 *
1835 * {@hide}
1836 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001837 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001838
1839 /**
1840 * Returns the number of times that the devie has started idling.
1841 *
1842 * {@hide}
1843 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001844 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001845
1846 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001847 * Returns the number of times that connectivity state changed.
1848 *
1849 * {@hide}
1850 */
1851 public abstract int getNumConnectivityChange(int which);
1852
1853 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001854 * 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 -08001855 * running on battery.
1856 *
1857 * {@hide}
1858 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001859 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001860
1861 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001862 * Returns the number of times a phone call was activated.
1863 *
1864 * {@hide}
1865 */
1866 public abstract int getPhoneOnCount(int which);
1867
1868 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001869 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001870 * the given signal strength.
1871 *
1872 * {@hide}
1873 */
1874 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001875 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001876
Dianne Hackborn617f8772009-03-31 15:04:46 -07001877 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001878 * Returns the time in microseconds that the phone has been trying to
1879 * acquire a signal.
1880 *
1881 * {@hide}
1882 */
1883 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001884 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001885
1886 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001887 * Returns the number of times the phone has entered the given signal strength.
1888 *
1889 * {@hide}
1890 */
1891 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1892
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001893 /**
1894 * Returns the time in microseconds that the mobile network has been active
1895 * (in a high power state).
1896 *
1897 * {@hide}
1898 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001899 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001900
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001901 /**
1902 * Returns the number of times that the mobile network has transitioned to the
1903 * active state.
1904 *
1905 * {@hide}
1906 */
1907 public abstract int getMobileRadioActiveCount(int which);
1908
1909 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001910 * Returns the time in microseconds that is the difference between the mobile radio
1911 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1912 * from the radio.
1913 *
1914 * {@hide}
1915 */
1916 public abstract long getMobileRadioActiveAdjustedTime(int which);
1917
1918 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001919 * Returns the time in microseconds that the mobile network has been active
1920 * (in a high power state) but not being able to blame on an app.
1921 *
1922 * {@hide}
1923 */
1924 public abstract long getMobileRadioActiveUnknownTime(int which);
1925
1926 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001927 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001928 *
1929 * {@hide}
1930 */
1931 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001932
Dianne Hackborn627bba72009-03-24 22:32:56 -07001933 public static final int DATA_CONNECTION_NONE = 0;
1934 public static final int DATA_CONNECTION_GPRS = 1;
1935 public static final int DATA_CONNECTION_EDGE = 2;
1936 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001937 public static final int DATA_CONNECTION_CDMA = 4;
1938 public static final int DATA_CONNECTION_EVDO_0 = 5;
1939 public static final int DATA_CONNECTION_EVDO_A = 6;
1940 public static final int DATA_CONNECTION_1xRTT = 7;
1941 public static final int DATA_CONNECTION_HSDPA = 8;
1942 public static final int DATA_CONNECTION_HSUPA = 9;
1943 public static final int DATA_CONNECTION_HSPA = 10;
1944 public static final int DATA_CONNECTION_IDEN = 11;
1945 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001946 public static final int DATA_CONNECTION_LTE = 13;
1947 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001948 public static final int DATA_CONNECTION_HSPAP = 15;
1949 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001950
Dianne Hackborn627bba72009-03-24 22:32:56 -07001951 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001952 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001953 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001954 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001955 };
1956
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001957 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001958
1959 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001960 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001961 * the given data connection.
1962 *
1963 * {@hide}
1964 */
1965 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001966 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001969 * Returns the number of times the phone has entered the given data
1970 * connection type.
1971 *
1972 * {@hide}
1973 */
1974 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001975
Dianne Hackborn3251b902014-06-20 14:40:53 -07001976 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1977 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1978 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1979 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1980 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1981 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1982 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1983 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1984 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1985 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1986 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1987 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1988 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1989
1990 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1991
1992 static final String[] WIFI_SUPPL_STATE_NAMES = {
1993 "invalid", "disconn", "disabled", "inactive", "scanning",
1994 "authenticating", "associating", "associated", "4-way-handshake",
1995 "group-handshake", "completed", "dormant", "uninit"
1996 };
1997
1998 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1999 "inv", "dsc", "dis", "inact", "scan",
2000 "auth", "ascing", "asced", "4-way",
2001 "group", "compl", "dorm", "uninit"
2002 };
2003
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002004 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2005 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002006 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002007 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2008 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002009 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002010 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2011 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2012 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002013 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002014 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002015 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002016 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002017 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2018 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002019 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2020 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2021 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2022 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2023 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2024 new String[] {"in", "out", "emergency", "off"},
2025 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002026 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2027 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2028 SignalStrength.SIGNAL_STRENGTH_NAMES,
2029 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002030 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2031 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2032 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002033 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002034
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002035 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2036 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002037 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002038 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002039 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002040 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002041 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002042 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2043 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2044 new String[] { "off", "light", "full", "???" },
2045 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002046 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2047 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2048 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002049 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2050 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2051 new String[] { "0", "1", "2", "3", "4" },
2052 new String[] { "0", "1", "2", "3", "4" }),
2053 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2054 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2055 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002056 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002057 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002058 };
2059
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002060 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002061 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002062 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2063 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002064 };
2065
2066 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002067 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002068 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002069 "Esw", "Ewa", "Elw", "Eec"
2070 };
2071
2072 @FunctionalInterface
2073 public interface IntToString {
2074 String applyAsString(int val);
2075 }
2076
2077 private static final IntToString sUidToString = UserHandle::formatUid;
2078 private static final IntToString sIntToString = Integer::toString;
2079
2080 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2081 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2082 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2083 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2084 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002085 };
2086
Dianne Hackborn617f8772009-03-31 15:04:46 -07002087 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002088 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002089 * running on battery.
2090 *
2091 * {@hide}
2092 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002093 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002094
2095 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002096 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002097 * been in the running state while the device was running on battery.
2098 *
2099 * {@hide}
2100 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002101 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002102
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002103 public static final int WIFI_STATE_OFF = 0;
2104 public static final int WIFI_STATE_OFF_SCANNING = 1;
2105 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2106 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2107 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2108 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2109 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2110 public static final int WIFI_STATE_SOFT_AP = 7;
2111
2112 static final String[] WIFI_STATE_NAMES = {
2113 "off", "scanning", "no_net", "disconn",
2114 "sta", "p2p", "sta_p2p", "soft_ap"
2115 };
2116
2117 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2118
2119 /**
2120 * Returns the time in microseconds that WiFi has been running in the given state.
2121 *
2122 * {@hide}
2123 */
2124 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002125 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002126
2127 /**
2128 * Returns the number of times that WiFi has entered the given state.
2129 *
2130 * {@hide}
2131 */
2132 public abstract int getWifiStateCount(int wifiState, int which);
2133
The Android Open Source Project10592532009-03-18 17:39:46 -07002134 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002135 * Returns the time in microseconds that the wifi supplicant has been
2136 * in a given state.
2137 *
2138 * {@hide}
2139 */
2140 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2141
2142 /**
2143 * Returns the number of times that the wifi supplicant has transitioned
2144 * to a given state.
2145 *
2146 * {@hide}
2147 */
2148 public abstract int getWifiSupplStateCount(int state, int which);
2149
2150 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2151
2152 /**
2153 * Returns the time in microseconds that WIFI has been running with
2154 * the given signal strength.
2155 *
2156 * {@hide}
2157 */
2158 public abstract long getWifiSignalStrengthTime(int strengthBin,
2159 long elapsedRealtimeUs, int which);
2160
2161 /**
2162 * Returns the number of times WIFI has entered the given signal strength.
2163 *
2164 * {@hide}
2165 */
2166 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2167
2168 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002169 * Returns the time in microseconds that the flashlight has been on while the device was
2170 * running on battery.
2171 *
2172 * {@hide}
2173 */
2174 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2175
2176 /**
2177 * Returns the number of times that the flashlight has been turned on while the device was
2178 * running on battery.
2179 *
2180 * {@hide}
2181 */
2182 public abstract long getFlashlightOnCount(int which);
2183
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002184 /**
2185 * Returns the time in microseconds that the camera has been on while the device was
2186 * running on battery.
2187 *
2188 * {@hide}
2189 */
2190 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2191
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002192 /**
2193 * Returns the time in microseconds that bluetooth scans were running while the device was
2194 * on battery.
2195 *
2196 * {@hide}
2197 */
2198 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002199
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002200 public static final int NETWORK_MOBILE_RX_DATA = 0;
2201 public static final int NETWORK_MOBILE_TX_DATA = 1;
2202 public static final int NETWORK_WIFI_RX_DATA = 2;
2203 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002204 public static final int NETWORK_BT_RX_DATA = 4;
2205 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002206 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2207 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2208 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2209 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2210 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002211
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002212 public abstract long getNetworkActivityBytes(int type, int which);
2213 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002214
Adam Lesinskie08af192015-03-25 16:42:59 -07002215 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002216 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002217 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002218 * actual power data.
2219 */
2220 public abstract boolean hasWifiActivityReporting();
2221
2222 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002223 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2224 * in various radio controller states, such as transmit, receive, and idle.
2225 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002226 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002227 public abstract ControllerActivityCounter getWifiControllerActivity();
2228
2229 /**
2230 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2231 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2232 * actual power data.
2233 */
2234 public abstract boolean hasBluetoothActivityReporting();
2235
2236 /**
2237 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2238 * in various radio controller states, such as transmit, receive, and idle.
2239 * @return non-null {@link ControllerActivityCounter}
2240 */
2241 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2242
2243 /**
2244 * Returns true if the BatteryStats object has detailed modem power reports.
2245 * When true, calling {@link #getModemControllerActivity()} will yield the
2246 * actual power data.
2247 */
2248 public abstract boolean hasModemActivityReporting();
2249
2250 /**
2251 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2252 * in various radio controller states, such as transmit, receive, and idle.
2253 * @return non-null {@link ControllerActivityCounter}
2254 */
2255 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002256
The Android Open Source Project10592532009-03-18 17:39:46 -07002257 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002258 * Return the wall clock time when battery stats data collection started.
2259 */
2260 public abstract long getStartClockTime();
2261
2262 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002263 * Return platform version tag that we were running in when the battery stats started.
2264 */
2265 public abstract String getStartPlatformVersion();
2266
2267 /**
2268 * Return platform version tag that we were running in when the battery stats ended.
2269 */
2270 public abstract String getEndPlatformVersion();
2271
2272 /**
2273 * Return the internal version code of the parcelled format.
2274 */
2275 public abstract int getParcelVersion();
2276
2277 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 * Return whether we are currently running on battery.
2279 */
2280 public abstract boolean getIsOnBattery();
2281
2282 /**
2283 * Returns a SparseArray containing the statistics for each uid.
2284 */
2285 public abstract SparseArray<? extends Uid> getUidStats();
2286
2287 /**
2288 * Returns the current battery uptime in microseconds.
2289 *
2290 * @param curTime the amount of elapsed realtime in microseconds.
2291 */
2292 public abstract long getBatteryUptime(long curTime);
2293
2294 /**
2295 * Returns the current battery realtime in microseconds.
2296 *
2297 * @param curTime the amount of elapsed realtime in microseconds.
2298 */
2299 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002300
2301 /**
Evan Millar633a1742009-04-02 16:36:33 -07002302 * Returns the battery percentage level at the last time the device was unplugged from power, or
2303 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002304 */
Evan Millar633a1742009-04-02 16:36:33 -07002305 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002306
2307 /**
Evan Millar633a1742009-04-02 16:36:33 -07002308 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2309 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002310 */
Evan Millar633a1742009-04-02 16:36:33 -07002311 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312
2313 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002314 * Get the amount the battery has discharged since the stats were
2315 * last reset after charging, as a lower-end approximation.
2316 */
2317 public abstract int getLowDischargeAmountSinceCharge();
2318
2319 /**
2320 * Get the amount the battery has discharged since the stats were
2321 * last reset after charging, as an upper-end approximation.
2322 */
2323 public abstract int getHighDischargeAmountSinceCharge();
2324
2325 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002326 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2327 */
2328 public abstract int getDischargeAmount(int which);
2329
2330 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002331 * Get the amount the battery has discharged while the screen was on,
2332 * since the last time power was unplugged.
2333 */
2334 public abstract int getDischargeAmountScreenOn();
2335
2336 /**
2337 * Get the amount the battery has discharged while the screen was on,
2338 * since the last time the device was charged.
2339 */
2340 public abstract int getDischargeAmountScreenOnSinceCharge();
2341
2342 /**
2343 * Get the amount the battery has discharged while the screen was off,
2344 * since the last time power was unplugged.
2345 */
2346 public abstract int getDischargeAmountScreenOff();
2347
2348 /**
2349 * Get the amount the battery has discharged while the screen was off,
2350 * since the last time the device was charged.
2351 */
2352 public abstract int getDischargeAmountScreenOffSinceCharge();
2353
2354 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 * Returns the total, last, or current battery uptime in microseconds.
2356 *
2357 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002358 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 */
2360 public abstract long computeBatteryUptime(long curTime, int which);
2361
2362 /**
2363 * Returns the total, last, or current battery realtime in microseconds.
2364 *
2365 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002366 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 */
2368 public abstract long computeBatteryRealtime(long curTime, int which);
2369
2370 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002371 * Returns the total, last, or current battery screen off uptime in microseconds.
2372 *
2373 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002374 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002375 */
2376 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2377
2378 /**
2379 * Returns the total, last, or current battery screen off realtime in microseconds.
2380 *
2381 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002382 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002383 */
2384 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2385
2386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 * Returns the total, last, or current uptime in microseconds.
2388 *
2389 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002390 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 */
2392 public abstract long computeUptime(long curTime, int which);
2393
2394 /**
2395 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002396 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002398 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 */
2400 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002401
2402 /**
2403 * Compute an approximation for how much run time (in microseconds) is remaining on
2404 * the battery. Returns -1 if no time can be computed: either there is not
2405 * enough current data to make a decision, or the battery is currently
2406 * charging.
2407 *
2408 * @param curTime The current elepsed realtime in microseconds.
2409 */
2410 public abstract long computeBatteryTimeRemaining(long curTime);
2411
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002412 // The part of a step duration that is the actual time.
2413 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2414
2415 // Bits in a step duration that are the new battery level we are at.
2416 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002417 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002418
2419 // Bits in a step duration that are the initial mode we were in at that step.
2420 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002421 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002422
2423 // Bits in a step duration that indicate which modes changed during that step.
2424 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002425 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002426
2427 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2428 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2429
Santos Cordone94f0502017-02-24 12:31:20 -08002430 // The largest value for screen state that is tracked in battery states. Any values above
2431 // this should be mapped back to one of the tracked values before being tracked here.
2432 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2433
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002434 // Step duration mode: power save is on.
2435 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2436
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002437 // Step duration mode: device is currently in idle mode.
2438 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2439
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002440 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2441 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002442 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2443 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002444 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2445 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2446 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2447 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2448 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002449 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2450 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002451 };
2452 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2453 (Display.STATE_OFF-1),
2454 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002455 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002456 (Display.STATE_ON-1),
2457 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2458 (Display.STATE_DOZE-1),
2459 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2460 (Display.STATE_DOZE_SUSPEND-1),
2461 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002462 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002463 };
2464 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2465 "screen off",
2466 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002467 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002468 "screen on",
2469 "screen on power save",
2470 "screen doze",
2471 "screen doze power save",
2472 "screen doze-suspend",
2473 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002474 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002475 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002476
2477 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002478 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2479 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2480 * a coulomb counter.
2481 */
2482 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2483
2484 /**
2485 * Return the counter keeping track of the amount of battery discharge measured in
2486 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2487 * a coulomb counter.
2488 */
2489 public abstract LongCounter getDischargeCoulombCounter();
2490
2491 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002492 * Returns the estimated real battery capacity, which may be less than the capacity
2493 * declared by the PowerProfile.
2494 * @return The estimated battery capacity in mAh.
2495 */
2496 public abstract int getEstimatedBatteryCapacity();
2497
2498 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002499 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002500 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002501 public abstract LevelStepTracker getDischargeLevelStepTracker();
2502
2503 /**
2504 * Return the array of daily discharge step durations.
2505 */
2506 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002507
2508 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002509 * Compute an approximation for how much time (in microseconds) remains until the battery
2510 * is fully charged. Returns -1 if no time can be computed: either there is not
2511 * enough current data to make a decision, or the battery is currently
2512 * discharging.
2513 *
2514 * @param curTime The current elepsed realtime in microseconds.
2515 */
2516 public abstract long computeChargeTimeRemaining(long curTime);
2517
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002518 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002519 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002520 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002521 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002522
2523 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002524 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002525 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002526 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002527
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002528 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2529
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002530 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002531
Evan Millarc64edde2009-04-18 12:26:32 -07002532 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533
James Carr2dd7e5e2016-07-20 18:48:39 -07002534 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2535
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002536 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2537
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002538 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 long days = seconds / (60 * 60 * 24);
2540 if (days != 0) {
2541 out.append(days);
2542 out.append("d ");
2543 }
2544 long used = days * 60 * 60 * 24;
2545
2546 long hours = (seconds - used) / (60 * 60);
2547 if (hours != 0 || used != 0) {
2548 out.append(hours);
2549 out.append("h ");
2550 }
2551 used += hours * 60 * 60;
2552
2553 long mins = (seconds-used) / 60;
2554 if (mins != 0 || used != 0) {
2555 out.append(mins);
2556 out.append("m ");
2557 }
2558 used += mins * 60;
2559
2560 if (seconds != 0 || used != 0) {
2561 out.append(seconds-used);
2562 out.append("s ");
2563 }
2564 }
2565
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002566 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002568 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 sb.append(time - (sec * 1000));
2570 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 }
2572
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002573 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002574 long sec = time / 1000;
2575 formatTimeRaw(sb, sec);
2576 sb.append(time - (sec * 1000));
2577 sb.append("ms");
2578 }
2579
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002580 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002582 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 }
2584 float perc = ((float)num) / ((float)den) * 100;
2585 mFormatBuilder.setLength(0);
2586 mFormatter.format("%.1f%%", perc);
2587 return mFormatBuilder.toString();
2588 }
2589
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002590 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002591 mFormatBuilder.setLength(0);
2592
2593 if (bytes < BYTES_PER_KB) {
2594 return bytes + "B";
2595 } else if (bytes < BYTES_PER_MB) {
2596 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2597 return mFormatBuilder.toString();
2598 } else if (bytes < BYTES_PER_GB){
2599 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2600 return mFormatBuilder.toString();
2601 } else {
2602 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2603 return mFormatBuilder.toString();
2604 }
2605 }
2606
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002607 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002608 if (timer != null) {
2609 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002610 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002611 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2612 return totalTimeMillis;
2613 }
2614 return 0;
2615 }
2616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 /**
2618 *
2619 * @param sb a StringBuilder object.
2620 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002621 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002623 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 * @param linePrefix a String to be prepended to each line of output.
2625 * @return the line prefix
2626 */
2627 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002628 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002631 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002632
Evan Millarc64edde2009-04-18 12:26:32 -07002633 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 if (totalTimeMillis != 0) {
2635 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002636 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002637 if (name != null) {
2638 sb.append(name);
2639 sb.append(' ');
2640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 sb.append('(');
2642 sb.append(count);
2643 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002644 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2645 if (maxDurationMs >= 0) {
2646 sb.append(" max=");
2647 sb.append(maxDurationMs);
2648 }
2649 if (timer.isRunningLocked()) {
2650 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2651 if (currentMs >= 0) {
2652 sb.append(" (running for ");
2653 sb.append(currentMs);
2654 sb.append("ms)");
2655 } else {
2656 sb.append(" (running)");
2657 }
2658 }
2659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 return ", ";
2661 }
2662 }
2663 return linePrefix;
2664 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002665
2666 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002667 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002668 *
2669 * @param pw a PrintWriter object to print to.
2670 * @param sb a StringBuilder object.
2671 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002672 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002673 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2674 * @param prefix a String to be prepended to each line of output.
2675 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002676 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002677 */
2678 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002679 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002680 if (timer != null) {
2681 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002682 final long totalTimeMs = (timer.getTotalTimeLocked(
2683 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002684 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002685 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002686 sb.setLength(0);
2687 sb.append(prefix);
2688 sb.append(" ");
2689 sb.append(type);
2690 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002691 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002692 sb.append("realtime (");
2693 sb.append(count);
2694 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002695 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2696 if (maxDurationMs >= 0) {
2697 sb.append(" max=");
2698 sb.append(maxDurationMs);
2699 }
2700 if (timer.isRunningLocked()) {
2701 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2702 if (currentMs >= 0) {
2703 sb.append(" (running for ");
2704 sb.append(currentMs);
2705 sb.append("ms)");
2706 } else {
2707 sb.append(" (running)");
2708 }
2709 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002710 pw.println(sb.toString());
2711 return true;
2712 }
2713 }
2714 return false;
2715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716
2717 /**
2718 * Checkin version of wakelock printer. Prints simple comma-separated list.
2719 *
2720 * @param sb a StringBuilder object.
2721 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002722 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002724 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 * @param linePrefix a String to be prepended to each line of output.
2726 * @return the line prefix
2727 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002728 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2729 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 long totalTimeMicros = 0;
2731 int count = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002732 long max = -1;
2733 long current = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002735 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002736 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002737 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2738 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 }
2740 sb.append(linePrefix);
2741 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2742 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002743 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002745 sb.append(',');
2746 sb.append(current);
2747 sb.append(',');
2748 sb.append(max);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 return ",";
2750 }
2751
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002752 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2753 String type) {
2754 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2755 pw.print(',');
2756 pw.print(uid);
2757 pw.print(',');
2758 pw.print(category);
2759 pw.print(',');
2760 pw.print(type);
2761 }
2762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 /**
2764 * Dump a comma-separated line of values for terse checkin mode.
2765 *
2766 * @param pw the PageWriter to dump log to
2767 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2768 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2769 * @param args type-dependent data arguments
2770 */
2771 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2772 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002773 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002774 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002775 pw.print(',');
2776 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002778 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002780
2781 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002782 * Dump a given timer stat for terse checkin mode.
2783 *
2784 * @param pw the PageWriter to dump log to
2785 * @param uid the UID to log
2786 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2787 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2788 * @param timer a {@link Timer} to dump stats for
2789 * @param rawRealtime the current elapsed realtime of the system in microseconds
2790 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2791 */
2792 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2793 Timer timer, long rawRealtime, int which) {
2794 if (timer != null) {
2795 // Convert from microseconds to milliseconds with rounding
2796 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2797 / 1000;
2798 final int count = timer.getCountLocked(which);
2799 if (totalTime != 0) {
2800 dumpLine(pw, uid, category, type, totalTime, count);
2801 }
2802 }
2803 }
2804
2805 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002806 * Checks if the ControllerActivityCounter has any data worth dumping.
2807 */
2808 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2809 if (counter == null) {
2810 return false;
2811 }
2812
2813 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2814 || counter.getRxTimeCounter().getCountLocked(which) != 0
2815 || counter.getPowerCounter().getCountLocked(which) != 0) {
2816 return true;
2817 }
2818
2819 for (LongCounter c : counter.getTxTimeCounters()) {
2820 if (c.getCountLocked(which) != 0) {
2821 return true;
2822 }
2823 }
2824 return false;
2825 }
2826
2827 /**
2828 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2829 * The order of the arguments in the final check in line is:
2830 *
2831 * idle, rx, power, tx...
2832 *
2833 * where tx... is one or more transmit level times.
2834 */
2835 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2836 String type,
2837 ControllerActivityCounter counter,
2838 int which) {
2839 if (!controllerActivityHasData(counter, which)) {
2840 return;
2841 }
2842
2843 dumpLineHeader(pw, uid, category, type);
2844 pw.print(",");
2845 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2846 pw.print(",");
2847 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2848 pw.print(",");
2849 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2850 for (LongCounter c : counter.getTxTimeCounters()) {
2851 pw.print(",");
2852 pw.print(c.getCountLocked(which));
2853 }
2854 pw.println();
2855 }
2856
2857 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2858 String prefix, String controllerName,
2859 ControllerActivityCounter counter,
2860 int which) {
2861 if (controllerActivityHasData(counter, which)) {
2862 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2863 }
2864 }
2865
2866 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2867 String controllerName,
2868 ControllerActivityCounter counter, int which) {
2869 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2870 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2871 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2872 long totalTxTimeMs = 0;
2873 for (LongCounter txState : counter.getTxTimeCounters()) {
2874 totalTxTimeMs += txState.getCountLocked(which);
2875 }
2876
2877 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2878
2879 sb.setLength(0);
2880 sb.append(prefix);
2881 sb.append(" ");
2882 sb.append(controllerName);
2883 sb.append(" Idle time: ");
2884 formatTimeMs(sb, idleTimeMs);
2885 sb.append("(");
2886 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2887 sb.append(")");
2888 pw.println(sb.toString());
2889
2890 sb.setLength(0);
2891 sb.append(prefix);
2892 sb.append(" ");
2893 sb.append(controllerName);
2894 sb.append(" Rx time: ");
2895 formatTimeMs(sb, rxTimeMs);
2896 sb.append("(");
2897 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2898 sb.append(")");
2899 pw.println(sb.toString());
2900
2901 sb.setLength(0);
2902 sb.append(prefix);
2903 sb.append(" ");
2904 sb.append(controllerName);
2905 sb.append(" Tx time: ");
2906 formatTimeMs(sb, totalTxTimeMs);
2907 sb.append("(");
2908 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2909 sb.append(")");
2910 pw.println(sb.toString());
2911
2912 final int numTxLvls = counter.getTxTimeCounters().length;
2913 if (numTxLvls > 1) {
2914 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2915 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2916 sb.setLength(0);
2917 sb.append(prefix);
2918 sb.append(" [");
2919 sb.append(lvl);
2920 sb.append("] ");
2921 formatTimeMs(sb, txLvlTimeMs);
2922 sb.append("(");
2923 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2924 sb.append(")");
2925 pw.println(sb.toString());
2926 }
2927 }
2928
2929 sb.setLength(0);
2930 sb.append(prefix);
2931 sb.append(" ");
2932 sb.append(controllerName);
2933 sb.append(" Power drain: ").append(
2934 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2935 sb.append("mAh");
2936 pw.println(sb.toString());
2937 }
2938
2939 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002940 * Temporary for settings.
2941 */
2942 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2943 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2944 }
2945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 /**
2947 * Checkin server version of dump to produce more compact, computer-readable log.
2948 *
2949 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002951 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2952 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2954 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2955 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2957 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002958 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2959 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2960 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 final long totalRealtime = computeRealtime(rawRealtime, which);
2962 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002963 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002964 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002965 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002966 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2967 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002968 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002969 rawRealtime, which);
2970 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2971 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002972 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002973 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002974 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002975 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002976 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
2977 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
2978 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002979
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002980 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002982 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002983 final int NU = uidStats.size();
2984
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002985 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 // Dump "battery" stat
2988 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002989 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002990 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002991 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002992 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002993 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
2994 getEstimatedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07002995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002997 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002998 long fullWakeLockTimeTotal = 0;
2999 long partialWakeLockTimeTotal = 0;
3000
3001 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003002 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003003
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003004 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3005 = u.getWakelockStats();
3006 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3007 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003008
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003009 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3010 if (fullWakeTimer != null) {
3011 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3012 which);
3013 }
3014
3015 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3016 if (partialWakeTimer != null) {
3017 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3018 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003019 }
3020 }
3021 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003022
3023 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003024 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3025 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3026 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3027 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3028 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3029 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3030 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3031 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003032 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3033 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003034 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3035 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003036 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3037 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003038
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003039 // Dump Modem controller stats
3040 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3041 getModemControllerActivity(), which);
3042
Adam Lesinskie283d332015-04-16 12:29:25 -07003043 // Dump Wifi controller stats
3044 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3045 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003046 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003047 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003048
3049 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3050 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003051
3052 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003053 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3054 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 // Dump misc stats
3057 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003058 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003059 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003060 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003061 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003062 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003063 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3064 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003065 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003066 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3067 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3068 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3069 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003070 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003071
3072 // Dump screen brightness stats
3073 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3074 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003075 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003076 }
3077 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07003078
Dianne Hackborn627bba72009-03-24 22:32:56 -07003079 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003080 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3081 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003082 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003083 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003084 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003085 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003086 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003087 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003088 args[i] = getPhoneSignalStrengthCount(i, which);
3089 }
3090 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003091
Dianne Hackborn627bba72009-03-24 22:32:56 -07003092 // Dump network type stats
3093 args = new Object[NUM_DATA_CONNECTION_TYPES];
3094 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003095 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003096 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003097 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3098 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3099 args[i] = getPhoneDataConnectionCount(i, which);
3100 }
3101 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003102
3103 // Dump wifi state stats
3104 args = new Object[NUM_WIFI_STATES];
3105 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003106 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003107 }
3108 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3109 for (int i=0; i<NUM_WIFI_STATES; i++) {
3110 args[i] = getWifiStateCount(i, which);
3111 }
3112 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3113
Dianne Hackborn3251b902014-06-20 14:40:53 -07003114 // Dump wifi suppl state stats
3115 args = new Object[NUM_WIFI_SUPPL_STATES];
3116 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3117 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3118 }
3119 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3120 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3121 args[i] = getWifiSupplStateCount(i, which);
3122 }
3123 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3124
3125 // Dump wifi signal strength stats
3126 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3127 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3128 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3129 }
3130 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3131 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3132 args[i] = getWifiSignalStrengthCount(i, which);
3133 }
3134 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3135
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003136 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003137 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003138 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003141 if (which == STATS_SINCE_UNPLUGGED) {
3142 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3143 getDischargeStartLevel()-getDischargeCurrentLevel(),
3144 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003145 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3146 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003147 } else {
3148 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3149 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003150 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003151 getDischargeAmountScreenOffSinceCharge(),
3152 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003153 }
3154
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003155 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003156 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003157 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003158 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003159 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003160 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003161 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3162 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003163 }
Evan Millarc64edde2009-04-18 12:26:32 -07003164 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003165 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003166 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003167 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3168 // Not doing the regular wake lock formatting to remain compatible
3169 // with the old checkin format.
3170 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3171 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003172 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003173 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003174 }
3175 }
Evan Millarc64edde2009-04-18 12:26:32 -07003176 }
3177
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003178 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003179 helper.create(this);
3180 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003181 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003182 if (sippers != null && sippers.size() > 0) {
3183 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3184 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003185 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003186 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3187 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3188 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003189 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003190 int uid = 0;
3191 String label;
3192 switch (bs.drainType) {
3193 case IDLE:
3194 label="idle";
3195 break;
3196 case CELL:
3197 label="cell";
3198 break;
3199 case PHONE:
3200 label="phone";
3201 break;
3202 case WIFI:
3203 label="wifi";
3204 break;
3205 case BLUETOOTH:
3206 label="blue";
3207 break;
3208 case SCREEN:
3209 label="scrn";
3210 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003211 case FLASHLIGHT:
3212 label="flashlight";
3213 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003214 case APP:
3215 uid = bs.uidObj.getUid();
3216 label = "uid";
3217 break;
3218 case USER:
3219 uid = UserHandle.getUid(bs.userId, 0);
3220 label = "user";
3221 break;
3222 case UNACCOUNTED:
3223 label = "unacc";
3224 break;
3225 case OVERCOUNTED:
3226 label = "over";
3227 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003228 case CAMERA:
3229 label = "camera";
3230 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003231 default:
3232 label = "???";
3233 }
3234 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003235 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003236 }
3237 }
3238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 for (int iu = 0; iu < NU; iu++) {
3240 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003241 if (reqUid >= 0 && uid != reqUid) {
3242 continue;
3243 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003244 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003247 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3248 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3249 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3250 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3251 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3252 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3253 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3254 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003255 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003256 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3257 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003258 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003259 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3260 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003261 // Background data transfers
3262 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3263 which);
3264 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3265 which);
3266 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3267 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3268 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3269 which);
3270 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3271 which);
3272 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3273 which);
3274 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3275 which);
3276
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003277 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3278 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003279 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003280 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3281 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3282 || wifiBytesBgTx > 0
3283 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3284 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003285 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3286 wifiBytesRx, wifiBytesTx,
3287 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003288 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003289 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003290 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3291 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3292 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3293 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003294 }
3295
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003296 // Dump modem controller data, per UID.
3297 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3298 u.getModemControllerActivity(), which);
3299
3300 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003301 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3302 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3303 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003304 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3305 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
3306 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
3307 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Adam Lesinskie283d332015-04-16 12:29:25 -07003308 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003309 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08003310 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003311 || uidWifiRunningTime != 0) {
3312 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3313 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003314 /* legacy fields follow, keep at 0 */ 0, 0, 0,
3315 wifiScanCountBg, wifiScanActualTime, wifiScanActualTimeBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003318 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3319 u.getWifiControllerActivity(), which);
3320
Bookatz867c0d72017-03-07 18:23:42 -08003321 final Timer bleTimer = u.getBluetoothScanTimer();
3322 if (bleTimer != null) {
3323 // Convert from microseconds to milliseconds with rounding
3324 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3325 / 1000;
3326 if (totalTime != 0) {
3327 final int count = bleTimer.getCountLocked(which);
3328 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3329 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
3330 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3331 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3332 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3333 final long actualTimeBg = bleTimerBg != null ?
3334 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3335 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
3336 countBg, actualTime, actualTimeBg);
3337 }
3338 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003339
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003340 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3341 u.getBluetoothControllerActivity(), which);
3342
Dianne Hackborn617f8772009-03-31 15:04:46 -07003343 if (u.hasUserActivity()) {
3344 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3345 boolean hasData = false;
3346 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3347 int val = u.getUserActivityCount(i, which);
3348 args[i] = val;
3349 if (val != 0) hasData = true;
3350 }
3351 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003352 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003353 }
3354 }
3355
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003356 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3357 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3358 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3359 String linePrefix = "";
3360 sb.setLength(0);
3361 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3362 rawRealtime, "f", which, linePrefix);
3363 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3364 rawRealtime, "p", which, linePrefix);
3365 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3366 rawRealtime, "w", which, linePrefix);
3367
3368 // Only log if we had at lease one wakelock...
3369 if (sb.length() > 0) {
3370 String name = wakelocks.keyAt(iw);
3371 if (name.indexOf(',') >= 0) {
3372 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003374 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 }
3376 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003377
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003378 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3379 for (int isy=syncs.size()-1; isy>=0; isy--) {
3380 final Timer timer = syncs.valueAt(isy);
3381 // Convert from microseconds to milliseconds with rounding
3382 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3383 final int count = timer.getCountLocked(which);
3384 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003385 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
3386 totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003387 }
3388 }
3389
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003390 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3391 for (int ij=jobs.size()-1; ij>=0; ij--) {
3392 final Timer timer = jobs.valueAt(ij);
3393 // Convert from microseconds to milliseconds with rounding
3394 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3395 final int count = timer.getCountLocked(which);
3396 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003397 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
3398 totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003399 }
3400 }
3401
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003402 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3403 rawRealtime, which);
3404 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3405 rawRealtime, which);
3406 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3407 rawRealtime, which);
3408 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3409 rawRealtime, which);
3410
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003411 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3412 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003413 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003414 final Uid.Sensor se = sensors.valueAt(ise);
3415 final int sensorNumber = sensors.keyAt(ise);
3416 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003417 if (timer != null) {
3418 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003419 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3420 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003421 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003422 final int count = timer.getCountLocked(which);
3423 final Timer bgTimer = se.getSensorBackgroundTime();
3424 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
3425 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3426 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3427 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3428 final long bgActualTime = bgTimer != null ?
3429 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3430 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3431 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 }
3434 }
3435
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003436 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3437 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003438
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003439 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3440 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003441
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003442 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003443 long totalStateTime = 0;
3444 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003445 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3446 totalStateTime += time;
3447 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003448 }
3449 if (totalStateTime > 0) {
3450 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3451 }
3452
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003453 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3454 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
3455 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
3456 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
3457 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
3458 powerCpuMaUs / 1000);
3459 }
3460
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003461 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3462 = u.getProcessStats();
3463 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3464 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003465
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003466 final long userMillis = ps.getUserTime(which);
3467 final long systemMillis = ps.getSystemTime(which);
3468 final long foregroundMillis = ps.getForegroundTime(which);
3469 final int starts = ps.getStarts(which);
3470 final int numCrashes = ps.getNumCrashes(which);
3471 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003472
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003473 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3474 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003475 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3476 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 }
3478 }
3479
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003480 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3481 = u.getPackageStats();
3482 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3483 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3484 int wakeups = 0;
3485 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3486 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003487 int count = alarms.valueAt(iwa).getCountLocked(which);
3488 wakeups += count;
3489 String name = alarms.keyAt(iwa).replace(',', '_');
3490 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003491 }
3492 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3493 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3494 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3495 final long startTime = ss.getStartTime(batteryUptime, which);
3496 final int starts = ss.getStarts(which);
3497 final int launches = ss.getLaunches(which);
3498 if (startTime != 0 || starts != 0 || launches != 0) {
3499 dumpLine(pw, uid, category, APK_DATA,
3500 wakeups, // wakeup alarms
3501 packageStats.keyAt(ipkg), // Apk
3502 serviceStats.keyAt(isvc), // service
3503 startTime / 1000, // time spent started, in ms
3504 starts,
3505 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 }
3507 }
3508 }
3509 }
3510 }
3511
Dianne Hackborn81038902012-11-26 17:04:09 -08003512 static final class TimerEntry {
3513 final String mName;
3514 final int mId;
3515 final BatteryStats.Timer mTimer;
3516 final long mTime;
3517 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3518 mName = name;
3519 mId = id;
3520 mTimer = timer;
3521 mTime = time;
3522 }
3523 }
3524
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003525 private void printmAh(PrintWriter printer, double power) {
3526 printer.print(BatteryStatsHelper.makemAh(power));
3527 }
3528
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003529 private void printmAh(StringBuilder sb, double power) {
3530 sb.append(BatteryStatsHelper.makemAh(power));
3531 }
3532
Dianne Hackbornd953c532014-08-16 18:17:38 -07003533 /**
3534 * Temporary for settings.
3535 */
3536 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3537 int reqUid) {
3538 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3539 }
3540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003542 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003543 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3545 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3546 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547
3548 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3549 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3550 final long totalRealtime = computeRealtime(rawRealtime, which);
3551 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003552 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3553 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3554 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003555 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3556 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003557
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003558 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003559
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003560 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003561 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003563 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3564 if (estimatedBatteryCapacity > 0) {
3565 sb.setLength(0);
3566 sb.append(prefix);
3567 sb.append(" Estimated battery capacity: ");
3568 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3569 sb.append(" mAh");
3570 pw.println(sb.toString());
3571 }
3572
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003573 sb.setLength(0);
3574 sb.append(prefix);
3575 sb.append(" Time on battery: ");
3576 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3577 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3578 sb.append(") realtime, ");
3579 formatTimeMs(sb, whichBatteryUptime / 1000);
3580 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3581 sb.append(") uptime");
3582 pw.println(sb.toString());
3583 sb.setLength(0);
3584 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003585 sb.append(" Time on battery screen off: ");
3586 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3587 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3588 sb.append(") realtime, ");
3589 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3590 sb.append("(");
3591 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3592 sb.append(") uptime");
3593 pw.println(sb.toString());
3594 sb.setLength(0);
3595 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003596 sb.append(" Total run time: ");
3597 formatTimeMs(sb, totalRealtime / 1000);
3598 sb.append("realtime, ");
3599 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003600 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003601 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003602 if (batteryTimeRemaining >= 0) {
3603 sb.setLength(0);
3604 sb.append(prefix);
3605 sb.append(" Battery time remaining: ");
3606 formatTimeMs(sb, batteryTimeRemaining / 1000);
3607 pw.println(sb.toString());
3608 }
3609 if (chargeTimeRemaining >= 0) {
3610 sb.setLength(0);
3611 sb.append(prefix);
3612 sb.append(" Charge time remaining: ");
3613 formatTimeMs(sb, chargeTimeRemaining / 1000);
3614 pw.println(sb.toString());
3615 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003616
3617 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3618 final long dischargeCount = dischargeCounter.getCountLocked(which);
3619 if (dischargeCount >= 0) {
3620 sb.setLength(0);
3621 sb.append(prefix);
3622 sb.append(" Discharge: ");
3623 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3624 sb.append(" mAh");
3625 pw.println(sb.toString());
3626 }
3627
3628 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3629 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3630 if (dischargeScreenOffCount >= 0) {
3631 sb.setLength(0);
3632 sb.append(prefix);
3633 sb.append(" Screen off discharge: ");
3634 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3635 sb.append(" mAh");
3636 pw.println(sb.toString());
3637 }
3638
3639 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3640 if (dischargeScreenOnCount >= 0) {
3641 sb.setLength(0);
3642 sb.append(prefix);
3643 sb.append(" Screen on discharge: ");
3644 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3645 sb.append(" mAh");
3646 pw.println(sb.toString());
3647 }
3648
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003649 pw.print(" Start clock time: ");
3650 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3651
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003652 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003653 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003654 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003655 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3656 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003657 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003658 rawRealtime, which);
3659 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3660 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003661 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003662 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003663 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3664 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3665 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003666 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003667 sb.append(prefix);
3668 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3669 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003670 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003671 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3672 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003673 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003674 pw.println(sb.toString());
3675 sb.setLength(0);
3676 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003677 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003678 boolean didOne = false;
3679 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003680 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003681 if (time == 0) {
3682 continue;
3683 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003684 sb.append("\n ");
3685 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003686 didOne = true;
3687 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3688 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003689 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003690 sb.append("(");
3691 sb.append(formatRatioLocked(time, screenOnTime));
3692 sb.append(")");
3693 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003694 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003695 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003696 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003697 sb.setLength(0);
3698 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003699 sb.append(" Power save mode enabled: ");
3700 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003701 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003702 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003703 sb.append(")");
3704 pw.println(sb.toString());
3705 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003706 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003707 sb.setLength(0);
3708 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003709 sb.append(" Device light idling: ");
3710 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003711 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003712 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3713 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003714 sb.append("x");
3715 pw.println(sb.toString());
3716 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003717 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003718 sb.setLength(0);
3719 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003720 sb.append(" Idle mode light time: ");
3721 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003722 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003723 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3724 sb.append(") ");
3725 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003726 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003727 sb.append(" -- longest ");
3728 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3729 pw.println(sb.toString());
3730 }
3731 if (deviceIdlingTime != 0) {
3732 sb.setLength(0);
3733 sb.append(prefix);
3734 sb.append(" Device full idling: ");
3735 formatTimeMs(sb, deviceIdlingTime / 1000);
3736 sb.append("(");
3737 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003738 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003739 sb.append("x");
3740 pw.println(sb.toString());
3741 }
3742 if (deviceIdleModeFullTime != 0) {
3743 sb.setLength(0);
3744 sb.append(prefix);
3745 sb.append(" Idle mode full time: ");
3746 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3747 sb.append("(");
3748 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3749 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003750 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003751 sb.append("x");
3752 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003753 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003754 pw.println(sb.toString());
3755 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003756 if (phoneOnTime != 0) {
3757 sb.setLength(0);
3758 sb.append(prefix);
3759 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3760 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003761 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003762 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003763 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003764 if (connChanges != 0) {
3765 pw.print(prefix);
3766 pw.print(" Connectivity changes: "); pw.println(connChanges);
3767 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003768
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003769 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003770 long fullWakeLockTimeTotalMicros = 0;
3771 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003772
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003773 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003774
Evan Millar22ac0432009-03-31 11:33:18 -07003775 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003776 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003777
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003778 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3779 = u.getWakelockStats();
3780 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3781 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003782
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003783 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3784 if (fullWakeTimer != null) {
3785 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3786 rawRealtime, which);
3787 }
3788
3789 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3790 if (partialWakeTimer != null) {
3791 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3792 rawRealtime, which);
3793 if (totalTimeMicros > 0) {
3794 if (reqUid < 0) {
3795 // Only show the ordered list of all wake
3796 // locks if the caller is not asking for data
3797 // about a specific uid.
3798 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3799 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003800 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003801 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003802 }
3803 }
3804 }
3805 }
3806
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003807 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3808 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3809 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3810 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3811 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3812 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3813 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3814 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003815 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3816 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003817
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003818 if (fullWakeLockTimeTotalMicros != 0) {
3819 sb.setLength(0);
3820 sb.append(prefix);
3821 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3822 (fullWakeLockTimeTotalMicros + 500) / 1000);
3823 pw.println(sb.toString());
3824 }
3825
3826 if (partialWakeLockTimeTotalMicros != 0) {
3827 sb.setLength(0);
3828 sb.append(prefix);
3829 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3830 (partialWakeLockTimeTotalMicros + 500) / 1000);
3831 pw.println(sb.toString());
3832 }
3833
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003834 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003835 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3836 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3837 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3838 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003839 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003840 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003841 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003842 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003843 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003844 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003845 if (time == 0) {
3846 continue;
3847 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003848 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003849 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003850 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003851 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003852 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003853 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003854 sb.append("(");
3855 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003856 sb.append(") ");
3857 sb.append(getPhoneSignalStrengthCount(i, which));
3858 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003859 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003860 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003861 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003862
3863 sb.setLength(0);
3864 sb.append(prefix);
3865 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003866 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003867 pw.println(sb.toString());
3868
Dianne Hackborn627bba72009-03-24 22:32:56 -07003869 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003870 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003871 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003872 didOne = false;
3873 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003874 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003875 if (time == 0) {
3876 continue;
3877 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003878 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003879 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003880 didOne = true;
3881 sb.append(DATA_CONNECTION_NAMES[i]);
3882 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003883 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003884 sb.append("(");
3885 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003886 sb.append(") ");
3887 sb.append(getPhoneDataConnectionCount(i, which));
3888 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003889 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003890 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003891 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003892
3893 sb.setLength(0);
3894 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003895 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003896 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003897 formatTimeMs(sb, mobileActiveTime / 1000);
3898 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3899 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3900 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003901 pw.println(sb.toString());
3902
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003903 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3904 if (mobileActiveUnknownTime != 0) {
3905 sb.setLength(0);
3906 sb.append(prefix);
3907 sb.append(" Mobile radio active unknown time: ");
3908 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3909 sb.append("(");
3910 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3911 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3912 sb.append("x");
3913 pw.println(sb.toString());
3914 }
3915
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003916 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3917 if (mobileActiveAdjustedTime != 0) {
3918 sb.setLength(0);
3919 sb.append(prefix);
3920 sb.append(" Mobile radio active adjusted time: ");
3921 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3922 sb.append("(");
3923 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3924 sb.append(")");
3925 pw.println(sb.toString());
3926 }
3927
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003928 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3929
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003930 pw.print(prefix);
3931 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3932 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3933 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3934 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003935 sb.setLength(0);
3936 sb.append(prefix);
3937 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3938 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3939 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3940 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003941 sb.append(")");
3942 pw.println(sb.toString());
3943
3944 sb.setLength(0);
3945 sb.append(prefix);
3946 sb.append(" Wifi states:");
3947 didOne = false;
3948 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003949 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003950 if (time == 0) {
3951 continue;
3952 }
3953 sb.append("\n ");
3954 didOne = true;
3955 sb.append(WIFI_STATE_NAMES[i]);
3956 sb.append(" ");
3957 formatTimeMs(sb, time/1000);
3958 sb.append("(");
3959 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3960 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003961 sb.append(getWifiStateCount(i, which));
3962 sb.append("x");
3963 }
3964 if (!didOne) sb.append(" (no activity)");
3965 pw.println(sb.toString());
3966
3967 sb.setLength(0);
3968 sb.append(prefix);
3969 sb.append(" Wifi supplicant states:");
3970 didOne = false;
3971 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3972 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3973 if (time == 0) {
3974 continue;
3975 }
3976 sb.append("\n ");
3977 didOne = true;
3978 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3979 sb.append(" ");
3980 formatTimeMs(sb, time/1000);
3981 sb.append("(");
3982 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3983 sb.append(") ");
3984 sb.append(getWifiSupplStateCount(i, which));
3985 sb.append("x");
3986 }
3987 if (!didOne) sb.append(" (no activity)");
3988 pw.println(sb.toString());
3989
3990 sb.setLength(0);
3991 sb.append(prefix);
3992 sb.append(" Wifi signal levels:");
3993 didOne = false;
3994 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3995 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3996 if (time == 0) {
3997 continue;
3998 }
3999 sb.append("\n ");
4000 sb.append(prefix);
4001 didOne = true;
4002 sb.append("level(");
4003 sb.append(i);
4004 sb.append(") ");
4005 formatTimeMs(sb, time/1000);
4006 sb.append("(");
4007 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4008 sb.append(") ");
4009 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004010 sb.append("x");
4011 }
4012 if (!didOne) sb.append(" (no activity)");
4013 pw.println(sb.toString());
4014
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004015 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004016
Adam Lesinski50e47602015-12-04 17:04:54 -08004017 pw.print(prefix);
4018 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4019 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4020
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004021 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4022 sb.setLength(0);
4023 sb.append(prefix);
4024 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4025 pw.println(sb.toString());
4026
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004027 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4028 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004029
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004030 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004031
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004032 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004033 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004034 pw.print(prefix); pw.println(" Device is currently unplugged");
4035 pw.print(prefix); pw.print(" Discharge cycle start level: ");
4036 pw.println(getDischargeStartLevel());
4037 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4038 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004039 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004040 pw.print(prefix); pw.println(" Device is currently plugged into power");
4041 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
4042 pw.println(getDischargeStartLevel());
4043 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
4044 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004045 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004046 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4047 pw.println(getDischargeAmountScreenOn());
4048 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4049 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004050 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004051 } else {
4052 pw.print(prefix); pw.println(" Device battery use since last full charge");
4053 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4054 pw.println(getLowDischargeAmountSinceCharge());
4055 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4056 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004057 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4058 pw.println(getDischargeAmountScreenOnSinceCharge());
4059 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4060 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004061 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004062 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004063
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004064 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004065 helper.create(this);
4066 helper.refreshStats(which, UserHandle.USER_ALL);
4067 List<BatterySipper> sippers = helper.getUsageList();
4068 if (sippers != null && sippers.size() > 0) {
4069 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4070 pw.print(prefix); pw.print(" Capacity: ");
4071 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004072 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004073 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4074 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4075 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4076 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004077 pw.println();
4078 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004079 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004080 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004081 switch (bs.drainType) {
4082 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004083 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004084 break;
4085 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004086 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004087 break;
4088 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004089 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004090 break;
4091 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004092 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004093 break;
4094 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004095 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004096 break;
4097 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004098 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004099 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004100 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004101 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004102 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004103 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004104 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004105 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004106 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004107 break;
4108 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004109 pw.print(" User "); pw.print(bs.userId);
4110 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004111 break;
4112 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004113 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004114 break;
4115 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004116 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004117 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004118 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004119 pw.print(" Camera: ");
4120 break;
4121 default:
4122 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004123 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004124 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004125 printmAh(pw, bs.totalPowerMah);
4126
Adam Lesinski57123002015-06-12 16:12:07 -07004127 if (bs.usagePowerMah != bs.totalPowerMah) {
4128 // If the usage (generic power) isn't the whole amount, we list out
4129 // what components are involved in the calculation.
4130
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004131 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004132 if (bs.usagePowerMah != 0) {
4133 pw.print(" usage=");
4134 printmAh(pw, bs.usagePowerMah);
4135 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004136 if (bs.cpuPowerMah != 0) {
4137 pw.print(" cpu=");
4138 printmAh(pw, bs.cpuPowerMah);
4139 }
4140 if (bs.wakeLockPowerMah != 0) {
4141 pw.print(" wake=");
4142 printmAh(pw, bs.wakeLockPowerMah);
4143 }
4144 if (bs.mobileRadioPowerMah != 0) {
4145 pw.print(" radio=");
4146 printmAh(pw, bs.mobileRadioPowerMah);
4147 }
4148 if (bs.wifiPowerMah != 0) {
4149 pw.print(" wifi=");
4150 printmAh(pw, bs.wifiPowerMah);
4151 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004152 if (bs.bluetoothPowerMah != 0) {
4153 pw.print(" bt=");
4154 printmAh(pw, bs.bluetoothPowerMah);
4155 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004156 if (bs.gpsPowerMah != 0) {
4157 pw.print(" gps=");
4158 printmAh(pw, bs.gpsPowerMah);
4159 }
4160 if (bs.sensorPowerMah != 0) {
4161 pw.print(" sensor=");
4162 printmAh(pw, bs.sensorPowerMah);
4163 }
4164 if (bs.cameraPowerMah != 0) {
4165 pw.print(" camera=");
4166 printmAh(pw, bs.cameraPowerMah);
4167 }
4168 if (bs.flashlightPowerMah != 0) {
4169 pw.print(" flash=");
4170 printmAh(pw, bs.flashlightPowerMah);
4171 }
4172 pw.print(" )");
4173 }
4174 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004175 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004176 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004177 }
4178
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004179 sippers = helper.getMobilemsppList();
4180 if (sippers != null && sippers.size() > 0) {
4181 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004182 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004183 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004184 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004185 sb.setLength(0);
4186 sb.append(prefix); sb.append(" Uid ");
4187 UserHandle.formatUid(sb, bs.uidObj.getUid());
4188 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4189 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4190 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004191 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004192 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004193 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004194 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004195 sb.setLength(0);
4196 sb.append(prefix);
4197 sb.append(" TOTAL TIME: ");
4198 formatTimeMs(sb, totalTime);
4199 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4200 sb.append(")");
4201 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004202 pw.println();
4203 }
4204
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004205 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4206 @Override
4207 public int compare(TimerEntry lhs, TimerEntry rhs) {
4208 long lhsTime = lhs.mTime;
4209 long rhsTime = rhs.mTime;
4210 if (lhsTime < rhsTime) {
4211 return 1;
4212 }
4213 if (lhsTime > rhsTime) {
4214 return -1;
4215 }
4216 return 0;
4217 }
4218 };
4219
4220 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004221 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4222 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004223 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004224 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4225 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4226 : kernelWakelocks.entrySet()) {
4227 final BatteryStats.Timer timer = ent.getValue();
4228 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004229 if (totalTimeMillis > 0) {
4230 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4231 }
4232 }
4233 if (ktimers.size() > 0) {
4234 Collections.sort(ktimers, timerComparator);
4235 pw.print(prefix); pw.println(" All kernel wake locks:");
4236 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004237 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004238 String linePrefix = ": ";
4239 sb.setLength(0);
4240 sb.append(prefix);
4241 sb.append(" Kernel Wake lock ");
4242 sb.append(timer.mName);
4243 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4244 which, linePrefix);
4245 if (!linePrefix.equals(": ")) {
4246 sb.append(" realtime");
4247 // Only print out wake locks that were held
4248 pw.println(sb.toString());
4249 }
4250 }
4251 pw.println();
4252 }
4253 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004254
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004255 if (timers.size() > 0) {
4256 Collections.sort(timers, timerComparator);
4257 pw.print(prefix); pw.println(" All partial wake locks:");
4258 for (int i=0; i<timers.size(); i++) {
4259 TimerEntry timer = timers.get(i);
4260 sb.setLength(0);
4261 sb.append(" Wake lock ");
4262 UserHandle.formatUid(sb, timer.mId);
4263 sb.append(" ");
4264 sb.append(timer.mName);
4265 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4266 sb.append(" realtime");
4267 pw.println(sb.toString());
4268 }
4269 timers.clear();
4270 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004271 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004272
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004273 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004274 if (wakeupReasons.size() > 0) {
4275 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004276 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004277 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004278 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004279 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4280 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004281 }
4282 Collections.sort(reasons, timerComparator);
4283 for (int i=0; i<reasons.size(); i++) {
4284 TimerEntry timer = reasons.get(i);
4285 String linePrefix = ": ";
4286 sb.setLength(0);
4287 sb.append(prefix);
4288 sb.append(" Wakeup reason ");
4289 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004290 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4291 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004292 pw.println(sb.toString());
4293 }
4294 pw.println();
4295 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004296 }
Evan Millar22ac0432009-03-31 11:33:18 -07004297
James Carr2dd7e5e2016-07-20 18:48:39 -07004298 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4299 pw.println("Memory Stats");
4300 for (int i = 0; i < mMemoryStats.size(); i++) {
4301 sb.setLength(0);
4302 sb.append("Bandwidth ");
4303 sb.append(mMemoryStats.keyAt(i));
4304 sb.append(" Time ");
4305 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4306 pw.println(sb.toString());
4307 }
4308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 for (int iu=0; iu<NU; iu++) {
4310 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004311 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004312 continue;
4313 }
4314
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004315 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004316
4317 pw.print(prefix);
4318 pw.print(" ");
4319 UserHandle.formatUid(pw, uid);
4320 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004322
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004323 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4324 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4325 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4326 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004327 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4328 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4329
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004330 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4331 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004332 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4333 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004334
4335 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4336 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4337
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004338 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4339 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4340 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004341 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4342 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4343 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4344 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004345 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004346
Adam Lesinski5f056f62016-07-14 16:56:08 -07004347 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4348 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4349
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004350 if (mobileRxBytes > 0 || mobileTxBytes > 0
4351 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004352 pw.print(prefix); pw.print(" Mobile network: ");
4353 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004354 pw.print(formatBytesLocked(mobileTxBytes));
4355 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4356 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004358 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4359 sb.setLength(0);
4360 sb.append(prefix); sb.append(" Mobile radio active: ");
4361 formatTimeMs(sb, uidMobileActiveTime / 1000);
4362 sb.append("(");
4363 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4364 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4365 long packets = mobileRxPackets + mobileTxPackets;
4366 if (packets == 0) {
4367 packets = 1;
4368 }
4369 sb.append(" @ ");
4370 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4371 sb.append(" mspp");
4372 pw.println(sb.toString());
4373 }
4374
Adam Lesinski5f056f62016-07-14 16:56:08 -07004375 if (mobileWakeup > 0) {
4376 sb.setLength(0);
4377 sb.append(prefix);
4378 sb.append(" Mobile radio AP wakeups: ");
4379 sb.append(mobileWakeup);
4380 pw.println(sb.toString());
4381 }
4382
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004383 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4384 u.getModemControllerActivity(), which);
4385
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004386 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004387 pw.print(prefix); pw.print(" Wi-Fi network: ");
4388 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004389 pw.print(formatBytesLocked(wifiTxBytes));
4390 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4391 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004392 }
4393
Dianne Hackborn62793e42015-03-09 11:15:41 -07004394 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004395 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004396 || uidWifiRunningTime != 0) {
4397 sb.setLength(0);
4398 sb.append(prefix); sb.append(" Wifi Running: ");
4399 formatTimeMs(sb, uidWifiRunningTime / 1000);
4400 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4401 whichBatteryRealtime)); sb.append(")\n");
4402 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4403 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4404 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4405 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004406 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004407 formatTimeMs(sb, wifiScanTime / 1000);
4408 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004409 whichBatteryRealtime)); sb.append(") ");
4410 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004411 sb.append("x\n");
4412 // actual and background times are unpooled and since reset (regardless of 'which')
4413 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4414 formatTimeMs(sb, wifiScanActualTime / 1000);
4415 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4416 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4417 sb.append(") ");
4418 sb.append(wifiScanCount);
4419 sb.append("x\n");
4420 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4421 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4422 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4423 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4424 sb.append(") ");
4425 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004426 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004427 pw.println(sb.toString());
4428 }
4429
Adam Lesinski5f056f62016-07-14 16:56:08 -07004430 if (wifiWakeup > 0) {
4431 sb.setLength(0);
4432 sb.append(prefix);
4433 sb.append(" WiFi AP wakeups: ");
4434 sb.append(wifiWakeup);
4435 pw.println(sb.toString());
4436 }
4437
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004438 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4439 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004440
Adam Lesinski50e47602015-12-04 17:04:54 -08004441 if (btRxBytes > 0 || btTxBytes > 0) {
4442 pw.print(prefix); pw.print(" Bluetooth network: ");
4443 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4444 pw.print(formatBytesLocked(btTxBytes));
4445 pw.println(" sent");
4446 }
4447
Bookatz867c0d72017-03-07 18:23:42 -08004448 final Timer bleTimer = u.getBluetoothScanTimer();
4449 if (bleTimer != null) {
4450 // Convert from microseconds to milliseconds with rounding
4451 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4452 / 1000;
4453 if (totalTimeMs != 0) {
4454 final int count = bleTimer.getCountLocked(which);
4455 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4456 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
4457 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4458 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4459 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4460 final long actualTimeMsBg = bleTimerBg != null ?
4461 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4462
4463 sb.setLength(0);
4464 sb.append(prefix);
4465 sb.append(" ");
4466 sb.append("Bluetooth Scan");
4467 sb.append(": ");
4468 if (actualTimeMs != totalTimeMs) {
4469 formatTimeMs(sb, totalTimeMs);
4470 sb.append("blamed realtime, ");
4471 }
4472 formatTimeMs(sb, actualTimeMs); // since reset, regardless of 'which'
4473 sb.append("realtime (");
4474 sb.append(count);
4475 sb.append(" times)");
4476 if (bleTimer.isRunningLocked()) {
4477 sb.append(" (running)");
4478 }
4479 if (actualTimeMsBg != 0 || countBg > 0) {
4480 sb.append(", ");
4481 formatTimeMs(sb, actualTimeMsBg); // since reset, regardless of 'which'
4482 sb.append("background (");
4483 sb.append(countBg);
4484 sb.append(" times)");
4485 }
4486 pw.println(sb.toString());
4487 uidActivity = true;
4488 }
4489 }
4490
4491
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004492
Dianne Hackborn617f8772009-03-31 15:04:46 -07004493 if (u.hasUserActivity()) {
4494 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004495 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004496 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004497 if (val != 0) {
4498 if (!hasData) {
4499 sb.setLength(0);
4500 sb.append(" User activity: ");
4501 hasData = true;
4502 } else {
4503 sb.append(", ");
4504 }
4505 sb.append(val);
4506 sb.append(" ");
4507 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4508 }
4509 }
4510 if (hasData) {
4511 pw.println(sb.toString());
4512 }
4513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004515 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4516 = u.getWakelockStats();
4517 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004518 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004519 int countWakelock = 0;
4520 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4521 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4522 String linePrefix = ": ";
4523 sb.setLength(0);
4524 sb.append(prefix);
4525 sb.append(" Wake lock ");
4526 sb.append(wakelocks.keyAt(iw));
4527 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4528 "full", which, linePrefix);
4529 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4530 "partial", which, linePrefix);
4531 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4532 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004533 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4534 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004535 sb.append(" realtime");
4536 pw.println(sb.toString());
4537 uidActivity = true;
4538 countWakelock++;
4539
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004540 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4541 rawRealtime, which);
4542 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4543 rawRealtime, which);
4544 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4545 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004546 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004547 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004548 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004549 if (countWakelock > 1) {
4550 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4551 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004552 sb.setLength(0);
4553 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004554 sb.append(" TOTAL wake: ");
4555 boolean needComma = false;
4556 if (totalFullWakelock != 0) {
4557 needComma = true;
4558 formatTimeMs(sb, totalFullWakelock);
4559 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004560 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004561 if (totalPartialWakelock != 0) {
4562 if (needComma) {
4563 sb.append(", ");
4564 }
4565 needComma = true;
4566 formatTimeMs(sb, totalPartialWakelock);
4567 sb.append("partial");
4568 }
4569 if (totalWindowWakelock != 0) {
4570 if (needComma) {
4571 sb.append(", ");
4572 }
4573 needComma = true;
4574 formatTimeMs(sb, totalWindowWakelock);
4575 sb.append("window");
4576 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004577 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004578 if (needComma) {
4579 sb.append(",");
4580 }
4581 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004582 formatTimeMs(sb, totalDrawWakelock);
4583 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004584 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004585 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004586 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004587 }
4588 }
4589
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004590 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4591 for (int isy=syncs.size()-1; isy>=0; isy--) {
4592 final Timer timer = syncs.valueAt(isy);
4593 // Convert from microseconds to milliseconds with rounding
4594 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4595 final int count = timer.getCountLocked(which);
4596 sb.setLength(0);
4597 sb.append(prefix);
4598 sb.append(" Sync ");
4599 sb.append(syncs.keyAt(isy));
4600 sb.append(": ");
4601 if (totalTime != 0) {
4602 formatTimeMs(sb, totalTime);
4603 sb.append("realtime (");
4604 sb.append(count);
4605 sb.append(" times)");
4606 } else {
4607 sb.append("(not used)");
4608 }
4609 pw.println(sb.toString());
4610 uidActivity = true;
4611 }
4612
4613 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4614 for (int ij=jobs.size()-1; ij>=0; ij--) {
4615 final Timer timer = jobs.valueAt(ij);
4616 // Convert from microseconds to milliseconds with rounding
4617 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4618 final int count = timer.getCountLocked(which);
4619 sb.setLength(0);
4620 sb.append(prefix);
4621 sb.append(" Job ");
4622 sb.append(jobs.keyAt(ij));
4623 sb.append(": ");
4624 if (totalTime != 0) {
4625 formatTimeMs(sb, totalTime);
4626 sb.append("realtime (");
4627 sb.append(count);
4628 sb.append(" times)");
4629 } else {
4630 sb.append("(not used)");
4631 }
4632 pw.println(sb.toString());
4633 uidActivity = true;
4634 }
4635
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004636 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4637 prefix, "Flashlight");
4638 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4639 prefix, "Camera");
4640 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4641 prefix, "Video");
4642 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4643 prefix, "Audio");
4644
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004645 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4646 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004647 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004648 final Uid.Sensor se = sensors.valueAt(ise);
4649 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004650 sb.setLength(0);
4651 sb.append(prefix);
4652 sb.append(" Sensor ");
4653 int handle = se.getHandle();
4654 if (handle == Uid.Sensor.GPS) {
4655 sb.append("GPS");
4656 } else {
4657 sb.append(handle);
4658 }
4659 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004661 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004662 if (timer != null) {
4663 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08004664 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4665 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004666 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08004667 final Timer bgTimer = se.getSensorBackgroundTime();
4668 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
4669 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4670 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4671 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4672 final long bgActualTime = bgTimer != null ?
4673 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4674
Dianne Hackborn61659e52014-07-09 16:13:01 -07004675 //timer.logState();
4676 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004677 if (actualTime != totalTime) {
4678 formatTimeMs(sb, totalTime);
4679 sb.append("blamed realtime, ");
4680 }
4681
4682 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07004683 sb.append("realtime (");
4684 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08004685 sb.append(" times)");
4686
4687 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004688 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08004689 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
4690 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004691 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08004692 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 } else {
4695 sb.append("(not used)");
4696 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004697 } else {
4698 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004700
4701 pw.println(sb.toString());
4702 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 }
4704
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004705 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4706 "Vibrator");
4707 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4708 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004709
Dianne Hackborn61659e52014-07-09 16:13:01 -07004710 long totalStateTime = 0;
4711 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4712 long time = u.getProcessStateTime(ips, rawRealtime, which);
4713 if (time > 0) {
4714 totalStateTime += time;
4715 sb.setLength(0);
4716 sb.append(prefix);
4717 sb.append(" ");
4718 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4719 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004720 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004721 pw.println(sb.toString());
4722 uidActivity = true;
4723 }
4724 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004725 if (totalStateTime > 0) {
4726 sb.setLength(0);
4727 sb.append(prefix);
4728 sb.append(" Total running: ");
4729 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4730 pw.println(sb.toString());
4731 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004732
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004733 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4734 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004735 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4736 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004737 sb.setLength(0);
4738 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004739 sb.append(" Total cpu time: u=");
4740 formatTimeMs(sb, userCpuTimeUs / 1000);
4741 sb.append("s=");
4742 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004743 sb.append("p=");
4744 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4745 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004746 pw.println(sb.toString());
4747 }
4748
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004749 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4750 = u.getProcessStats();
4751 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4752 final Uid.Proc ps = processStats.valueAt(ipr);
4753 long userTime;
4754 long systemTime;
4755 long foregroundTime;
4756 int starts;
4757 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004759 userTime = ps.getUserTime(which);
4760 systemTime = ps.getSystemTime(which);
4761 foregroundTime = ps.getForegroundTime(which);
4762 starts = ps.getStarts(which);
4763 final int numCrashes = ps.getNumCrashes(which);
4764 final int numAnrs = ps.getNumAnrs(which);
4765 numExcessive = which == STATS_SINCE_CHARGED
4766 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004767
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004768 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4769 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4770 sb.setLength(0);
4771 sb.append(prefix); sb.append(" Proc ");
4772 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4773 sb.append(prefix); sb.append(" CPU: ");
4774 formatTimeMs(sb, userTime); sb.append("usr + ");
4775 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4776 formatTimeMs(sb, foregroundTime); sb.append("fg");
4777 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4778 sb.append("\n"); sb.append(prefix); sb.append(" ");
4779 boolean hasOne = false;
4780 if (starts != 0) {
4781 hasOne = true;
4782 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004783 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004784 if (numCrashes != 0) {
4785 if (hasOne) {
4786 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004787 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004788 hasOne = true;
4789 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004790 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004791 if (numAnrs != 0) {
4792 if (hasOne) {
4793 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004794 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004795 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 }
4797 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004798 pw.println(sb.toString());
4799 for (int e=0; e<numExcessive; e++) {
4800 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4801 if (ew != null) {
4802 pw.print(prefix); pw.print(" * Killed for ");
4803 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4804 pw.print("wake lock");
4805 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4806 pw.print("cpu");
4807 } else {
4808 pw.print("unknown");
4809 }
4810 pw.print(" use: ");
4811 TimeUtils.formatDuration(ew.usedTime, pw);
4812 pw.print(" over ");
4813 TimeUtils.formatDuration(ew.overTime, pw);
4814 if (ew.overTime != 0) {
4815 pw.print(" (");
4816 pw.print((ew.usedTime*100)/ew.overTime);
4817 pw.println("%)");
4818 }
4819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 }
4821 uidActivity = true;
4822 }
4823 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004824
4825 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4826 = u.getPackageStats();
4827 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4828 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4829 pw.println(":");
4830 boolean apkActivity = false;
4831 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4832 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4833 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4834 pw.print(prefix); pw.print(" Wakeup alarm ");
4835 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4836 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4837 pw.println(" times");
4838 apkActivity = true;
4839 }
4840 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4841 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4842 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4843 final long startTime = ss.getStartTime(batteryUptime, which);
4844 final int starts = ss.getStarts(which);
4845 final int launches = ss.getLaunches(which);
4846 if (startTime != 0 || starts != 0 || launches != 0) {
4847 sb.setLength(0);
4848 sb.append(prefix); sb.append(" Service ");
4849 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4850 sb.append(prefix); sb.append(" Created for: ");
4851 formatTimeMs(sb, startTime / 1000);
4852 sb.append("uptime\n");
4853 sb.append(prefix); sb.append(" Starts: ");
4854 sb.append(starts);
4855 sb.append(", launches: "); sb.append(launches);
4856 pw.println(sb.toString());
4857 apkActivity = true;
4858 }
4859 }
4860 if (!apkActivity) {
4861 pw.print(prefix); pw.println(" (nothing executed)");
4862 }
4863 uidActivity = true;
4864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004865 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004866 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867 }
4868 }
4869 }
4870
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004871 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004872 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004873 int diff = oldval ^ newval;
4874 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004875 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004876 for (int i=0; i<descriptions.length; i++) {
4877 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004878 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004879 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004880 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004881 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004882 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004883 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4884 didWake = true;
4885 pw.print("=");
4886 if (longNames) {
4887 UserHandle.formatUid(pw, wakelockTag.uid);
4888 pw.print(":\"");
4889 pw.print(wakelockTag.string);
4890 pw.print("\"");
4891 } else {
4892 pw.print(wakelockTag.poolIdx);
4893 }
4894 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004895 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004896 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004897 pw.print("=");
4898 int val = (newval&bd.mask)>>bd.shift;
4899 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004900 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004901 } else {
4902 pw.print(val);
4903 }
4904 }
4905 }
4906 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004907 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004908 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004909 if (longNames) {
4910 UserHandle.formatUid(pw, wakelockTag.uid);
4911 pw.print(":\"");
4912 pw.print(wakelockTag.string);
4913 pw.print("\"");
4914 } else {
4915 pw.print(wakelockTag.poolIdx);
4916 }
4917 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004918 }
4919
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004920 public void prepareForDumpLocked() {
4921 }
4922
4923 public static class HistoryPrinter {
4924 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004925 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004926 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004927 int oldStatus = -1;
4928 int oldHealth = -1;
4929 int oldPlug = -1;
4930 int oldTemp = -1;
4931 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004932 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004933 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004934
Dianne Hackborn3251b902014-06-20 14:40:53 -07004935 void reset() {
4936 oldState = oldState2 = 0;
4937 oldLevel = -1;
4938 oldStatus = -1;
4939 oldHealth = -1;
4940 oldPlug = -1;
4941 oldTemp = -1;
4942 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004943 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07004944 }
4945
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004946 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004947 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004948 if (!checkin) {
4949 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004950 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004951 pw.print(" (");
4952 pw.print(rec.numReadInts);
4953 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004954 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004955 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4956 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004957 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004958 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004959 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004960 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004961 }
4962 lastTime = rec.time;
4963 }
4964 if (rec.cmd == HistoryItem.CMD_START) {
4965 if (checkin) {
4966 pw.print(":");
4967 }
4968 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004969 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004970 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4971 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004972 if (checkin) {
4973 pw.print(":");
4974 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004975 if (rec.cmd == HistoryItem.CMD_RESET) {
4976 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004977 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004978 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004979 pw.print("TIME:");
4980 if (checkin) {
4981 pw.println(rec.currentTime);
4982 } else {
4983 pw.print(" ");
4984 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4985 rec.currentTime).toString());
4986 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004987 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4988 if (checkin) {
4989 pw.print(":");
4990 }
4991 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004992 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4993 if (checkin) {
4994 pw.print(":");
4995 }
4996 pw.println("*OVERFLOW*");
4997 } else {
4998 if (!checkin) {
4999 if (rec.batteryLevel < 10) pw.print("00");
5000 else if (rec.batteryLevel < 100) pw.print("0");
5001 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005002 if (verbose) {
5003 pw.print(" ");
5004 if (rec.states < 0) ;
5005 else if (rec.states < 0x10) pw.print("0000000");
5006 else if (rec.states < 0x100) pw.print("000000");
5007 else if (rec.states < 0x1000) pw.print("00000");
5008 else if (rec.states < 0x10000) pw.print("0000");
5009 else if (rec.states < 0x100000) pw.print("000");
5010 else if (rec.states < 0x1000000) pw.print("00");
5011 else if (rec.states < 0x10000000) pw.print("0");
5012 pw.print(Integer.toHexString(rec.states));
5013 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005014 } else {
5015 if (oldLevel != rec.batteryLevel) {
5016 oldLevel = rec.batteryLevel;
5017 pw.print(",Bl="); pw.print(rec.batteryLevel);
5018 }
5019 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005020 if (oldStatus != rec.batteryStatus) {
5021 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005022 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005023 switch (oldStatus) {
5024 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005025 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005026 break;
5027 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005028 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005029 break;
5030 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005031 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005032 break;
5033 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005034 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005035 break;
5036 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005037 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005038 break;
5039 default:
5040 pw.print(oldStatus);
5041 break;
5042 }
5043 }
5044 if (oldHealth != rec.batteryHealth) {
5045 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005046 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005047 switch (oldHealth) {
5048 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005049 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005050 break;
5051 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005052 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005053 break;
5054 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005055 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005056 break;
5057 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005058 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005059 break;
5060 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005061 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005062 break;
5063 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005064 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005065 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005066 case BatteryManager.BATTERY_HEALTH_COLD:
5067 pw.print(checkin ? "c" : "cold");
5068 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005069 default:
5070 pw.print(oldHealth);
5071 break;
5072 }
5073 }
5074 if (oldPlug != rec.batteryPlugType) {
5075 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005076 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005077 switch (oldPlug) {
5078 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005079 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005080 break;
5081 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005082 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005083 break;
5084 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005085 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005086 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005087 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005088 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005089 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005090 default:
5091 pw.print(oldPlug);
5092 break;
5093 }
5094 }
5095 if (oldTemp != rec.batteryTemperature) {
5096 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005097 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005098 pw.print(oldTemp);
5099 }
5100 if (oldVolt != rec.batteryVoltage) {
5101 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005102 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005103 pw.print(oldVolt);
5104 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005105 final int chargeMAh = rec.batteryChargeUAh / 1000;
5106 if (oldChargeMAh != chargeMAh) {
5107 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005108 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005109 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005110 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005111 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005112 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005113 printBitDescriptions(pw, oldState2, rec.states2, null,
5114 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005115 if (rec.wakeReasonTag != null) {
5116 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005117 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005118 pw.print(rec.wakeReasonTag.poolIdx);
5119 } else {
5120 pw.print(" wake_reason=");
5121 pw.print(rec.wakeReasonTag.uid);
5122 pw.print(":\"");
5123 pw.print(rec.wakeReasonTag.string);
5124 pw.print("\"");
5125 }
5126 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005127 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005128 pw.print(checkin ? "," : " ");
5129 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5130 pw.print("+");
5131 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5132 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005133 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005134 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5135 : HISTORY_EVENT_NAMES;
5136 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5137 | HistoryItem.EVENT_FLAG_FINISH);
5138 if (idx >= 0 && idx < eventNames.length) {
5139 pw.print(eventNames[idx]);
5140 } else {
5141 pw.print(checkin ? "Ev" : "event");
5142 pw.print(idx);
5143 }
5144 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005145 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005146 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005147 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005148 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5149 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005150 pw.print(":\"");
5151 pw.print(rec.eventTag.string);
5152 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005153 }
5154 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005155 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005156 if (rec.stepDetails != null) {
5157 if (!checkin) {
5158 pw.print(" Details: cpu=");
5159 pw.print(rec.stepDetails.userTime);
5160 pw.print("u+");
5161 pw.print(rec.stepDetails.systemTime);
5162 pw.print("s");
5163 if (rec.stepDetails.appCpuUid1 >= 0) {
5164 pw.print(" (");
5165 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5166 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5167 if (rec.stepDetails.appCpuUid2 >= 0) {
5168 pw.print(", ");
5169 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5170 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5171 }
5172 if (rec.stepDetails.appCpuUid3 >= 0) {
5173 pw.print(", ");
5174 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5175 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5176 }
5177 pw.print(')');
5178 }
5179 pw.println();
5180 pw.print(" /proc/stat=");
5181 pw.print(rec.stepDetails.statUserTime);
5182 pw.print(" usr, ");
5183 pw.print(rec.stepDetails.statSystemTime);
5184 pw.print(" sys, ");
5185 pw.print(rec.stepDetails.statIOWaitTime);
5186 pw.print(" io, ");
5187 pw.print(rec.stepDetails.statIrqTime);
5188 pw.print(" irq, ");
5189 pw.print(rec.stepDetails.statSoftIrqTime);
5190 pw.print(" sirq, ");
5191 pw.print(rec.stepDetails.statIdlTime);
5192 pw.print(" idle");
5193 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5194 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5195 + rec.stepDetails.statSoftIrqTime;
5196 int total = totalRun + rec.stepDetails.statIdlTime;
5197 if (total > 0) {
5198 pw.print(" (");
5199 float perc = ((float)totalRun) / ((float)total) * 100;
5200 pw.print(String.format("%.1f%%", perc));
5201 pw.print(" of ");
5202 StringBuilder sb = new StringBuilder(64);
5203 formatTimeMsNoSpace(sb, total*10);
5204 pw.print(sb);
5205 pw.print(")");
5206 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005207 pw.print(", PlatformIdleStat ");
5208 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005209 pw.println();
5210 } else {
5211 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5212 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5213 pw.print(rec.stepDetails.userTime);
5214 pw.print(":");
5215 pw.print(rec.stepDetails.systemTime);
5216 if (rec.stepDetails.appCpuUid1 >= 0) {
5217 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5218 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5219 if (rec.stepDetails.appCpuUid2 >= 0) {
5220 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5221 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5222 }
5223 if (rec.stepDetails.appCpuUid3 >= 0) {
5224 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5225 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5226 }
5227 }
5228 pw.println();
5229 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5230 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5231 pw.print(rec.stepDetails.statUserTime);
5232 pw.print(',');
5233 pw.print(rec.stepDetails.statSystemTime);
5234 pw.print(',');
5235 pw.print(rec.stepDetails.statIOWaitTime);
5236 pw.print(',');
5237 pw.print(rec.stepDetails.statIrqTime);
5238 pw.print(',');
5239 pw.print(rec.stepDetails.statSoftIrqTime);
5240 pw.print(',');
5241 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005242 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005243 if (rec.stepDetails.statPlatformIdleState != null) {
5244 pw.print(rec.stepDetails.statPlatformIdleState);
5245 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005246 pw.println();
5247 }
5248 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005249 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005250 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005251 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005252 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005253
5254 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5255 UserHandle.formatUid(pw, uid);
5256 pw.print("=");
5257 pw.print(utime);
5258 pw.print("u+");
5259 pw.print(stime);
5260 pw.print("s");
5261 }
5262
5263 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5264 pw.print('/');
5265 pw.print(uid);
5266 pw.print(":");
5267 pw.print(utime);
5268 pw.print(":");
5269 pw.print(stime);
5270 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005271 }
5272
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005273 private void printSizeValue(PrintWriter pw, long size) {
5274 float result = size;
5275 String suffix = "";
5276 if (result >= 10*1024) {
5277 suffix = "KB";
5278 result = result / 1024;
5279 }
5280 if (result >= 10*1024) {
5281 suffix = "MB";
5282 result = result / 1024;
5283 }
5284 if (result >= 10*1024) {
5285 suffix = "GB";
5286 result = result / 1024;
5287 }
5288 if (result >= 10*1024) {
5289 suffix = "TB";
5290 result = result / 1024;
5291 }
5292 if (result >= 10*1024) {
5293 suffix = "PB";
5294 result = result / 1024;
5295 }
5296 pw.print((int)result);
5297 pw.print(suffix);
5298 }
5299
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005300 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5301 String label3, long estimatedTime) {
5302 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005303 return false;
5304 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005305 pw.print(label1);
5306 pw.print(label2);
5307 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005308 StringBuilder sb = new StringBuilder(64);
5309 formatTimeMs(sb, estimatedTime);
5310 pw.print(sb);
5311 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005312 return true;
5313 }
5314
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005315 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5316 LevelStepTracker steps, boolean checkin) {
5317 if (steps == null) {
5318 return false;
5319 }
5320 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005321 if (count <= 0) {
5322 return false;
5323 }
5324 if (!checkin) {
5325 pw.println(header);
5326 }
Kweku Adams030980a2015-04-01 16:07:48 -07005327 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005328 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005329 long duration = steps.getDurationAt(i);
5330 int level = steps.getLevelAt(i);
5331 long initMode = steps.getInitModeAt(i);
5332 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005333 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005334 lineArgs[0] = Long.toString(duration);
5335 lineArgs[1] = Integer.toString(level);
5336 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5337 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5338 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5339 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5340 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5341 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005342 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005343 }
5344 } else {
5345 lineArgs[2] = "";
5346 }
5347 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5348 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5349 } else {
5350 lineArgs[3] = "";
5351 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005352 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005353 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005354 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005355 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005356 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005357 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5358 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005359 pw.print(prefix);
5360 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005361 TimeUtils.formatDuration(duration, pw);
5362 pw.print(" to "); pw.print(level);
5363 boolean haveModes = false;
5364 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5365 pw.print(" (");
5366 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5367 case Display.STATE_OFF: pw.print("screen-off"); break;
5368 case Display.STATE_ON: pw.print("screen-on"); break;
5369 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5370 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005371 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005372 }
5373 haveModes = true;
5374 }
5375 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5376 pw.print(haveModes ? ", " : " (");
5377 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5378 ? "power-save-on" : "power-save-off");
5379 haveModes = true;
5380 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005381 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5382 pw.print(haveModes ? ", " : " (");
5383 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5384 ? "device-idle-on" : "device-idle-off");
5385 haveModes = true;
5386 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005387 if (haveModes) {
5388 pw.print(")");
5389 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005390 pw.println();
5391 }
5392 }
5393 return true;
5394 }
5395
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005396 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005397 public static final int DUMP_DAILY_ONLY = 1<<2;
5398 public static final int DUMP_HISTORY_ONLY = 1<<3;
5399 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5400 public static final int DUMP_VERBOSE = 1<<5;
5401 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005402
Dianne Hackborn37de0982014-05-09 09:32:18 -07005403 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5404 final HistoryPrinter hprinter = new HistoryPrinter();
5405 final HistoryItem rec = new HistoryItem();
5406 long lastTime = -1;
5407 long baseTime = -1;
5408 boolean printed = false;
5409 HistoryEventTracker tracker = null;
5410 while (getNextHistoryLocked(rec)) {
5411 lastTime = rec.time;
5412 if (baseTime < 0) {
5413 baseTime = lastTime;
5414 }
5415 if (rec.time >= histStart) {
5416 if (histStart >= 0 && !printed) {
5417 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005418 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005419 || rec.cmd == HistoryItem.CMD_START
5420 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005421 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005422 hprinter.printNextItem(pw, rec, baseTime, checkin,
5423 (flags&DUMP_VERBOSE) != 0);
5424 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005425 } else if (rec.currentTime != 0) {
5426 printed = true;
5427 byte cmd = rec.cmd;
5428 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005429 hprinter.printNextItem(pw, rec, baseTime, checkin,
5430 (flags&DUMP_VERBOSE) != 0);
5431 rec.cmd = cmd;
5432 }
5433 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005434 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5435 hprinter.printNextItem(pw, rec, baseTime, checkin,
5436 (flags&DUMP_VERBOSE) != 0);
5437 rec.cmd = HistoryItem.CMD_UPDATE;
5438 }
5439 int oldEventCode = rec.eventCode;
5440 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005441 rec.eventTag = new HistoryTag();
5442 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5443 HashMap<String, SparseIntArray> active
5444 = tracker.getStateForEvent(i);
5445 if (active == null) {
5446 continue;
5447 }
5448 for (HashMap.Entry<String, SparseIntArray> ent
5449 : active.entrySet()) {
5450 SparseIntArray uids = ent.getValue();
5451 for (int j=0; j<uids.size(); j++) {
5452 rec.eventCode = i;
5453 rec.eventTag.string = ent.getKey();
5454 rec.eventTag.uid = uids.keyAt(j);
5455 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005456 hprinter.printNextItem(pw, rec, baseTime, checkin,
5457 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005458 rec.wakeReasonTag = null;
5459 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005460 }
5461 }
5462 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005463 rec.eventCode = oldEventCode;
5464 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005465 tracker = null;
5466 }
5467 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005468 hprinter.printNextItem(pw, rec, baseTime, checkin,
5469 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005470 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5471 // This is an attempt to aggregate the previous state and generate
5472 // fake events to reflect that state at the point where we start
5473 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005474 if (tracker == null) {
5475 tracker = new HistoryEventTracker();
5476 }
5477 tracker.updateState(rec.eventCode, rec.eventTag.string,
5478 rec.eventTag.uid, rec.eventTag.poolIdx);
5479 }
5480 }
5481 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005482 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005483 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5484 }
5485 }
5486
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005487 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5488 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5489 if (steps == null) {
5490 return;
5491 }
5492 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5493 if (timeRemaining >= 0) {
5494 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5495 tmpSb.setLength(0);
5496 formatTimeMs(tmpSb, timeRemaining);
5497 pw.print(tmpSb);
5498 pw.print(" (from "); pw.print(tmpOutInt[0]);
5499 pw.println(" steps)");
5500 }
5501 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5502 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5503 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5504 if (estimatedTime > 0) {
5505 pw.print(prefix); pw.print(label); pw.print(" ");
5506 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5507 pw.print(" time: ");
5508 tmpSb.setLength(0);
5509 formatTimeMs(tmpSb, estimatedTime);
5510 pw.print(tmpSb);
5511 pw.print(" (from "); pw.print(tmpOutInt[0]);
5512 pw.println(" steps)");
5513 }
5514 }
5515 }
5516
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005517 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5518 ArrayList<PackageChange> changes) {
5519 if (changes == null) {
5520 return;
5521 }
5522 pw.print(prefix); pw.println("Package changes:");
5523 for (int i=0; i<changes.size(); i++) {
5524 PackageChange pc = changes.get(i);
5525 if (pc.mUpdate) {
5526 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5527 pw.print(" vers="); pw.println(pc.mVersionCode);
5528 } else {
5529 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5530 }
5531 }
5532 }
5533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 /**
5535 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5536 *
5537 * @param pw a Printer to receive the dump output.
5538 */
5539 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005540 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005541 prepareForDumpLocked();
5542
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005543 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005544 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005545
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005546 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005547 final long historyTotalSize = getHistoryTotalSize();
5548 final long historyUsedSize = getHistoryUsedSize();
5549 if (startIteratingHistoryLocked()) {
5550 try {
5551 pw.print("Battery History (");
5552 pw.print((100*historyUsedSize)/historyTotalSize);
5553 pw.print("% used, ");
5554 printSizeValue(pw, historyUsedSize);
5555 pw.print(" used of ");
5556 printSizeValue(pw, historyTotalSize);
5557 pw.print(", ");
5558 pw.print(getHistoryStringPoolSize());
5559 pw.print(" strings using ");
5560 printSizeValue(pw, getHistoryStringPoolBytes());
5561 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005562 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005563 pw.println();
5564 } finally {
5565 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005566 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005567 }
5568
5569 if (startIteratingOldHistoryLocked()) {
5570 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005571 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005572 pw.println("Old battery History:");
5573 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005574 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005575 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005576 if (baseTime < 0) {
5577 baseTime = rec.time;
5578 }
5579 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005580 }
5581 pw.println();
5582 } finally {
5583 finishIteratingOldHistoryLocked();
5584 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005585 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005586 }
5587
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005588 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005589 return;
5590 }
5591
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005592 if (!filtering) {
5593 SparseArray<? extends Uid> uidStats = getUidStats();
5594 final int NU = uidStats.size();
5595 boolean didPid = false;
5596 long nowRealtime = SystemClock.elapsedRealtime();
5597 for (int i=0; i<NU; i++) {
5598 Uid uid = uidStats.valueAt(i);
5599 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5600 if (pids != null) {
5601 for (int j=0; j<pids.size(); j++) {
5602 Uid.Pid pid = pids.valueAt(j);
5603 if (!didPid) {
5604 pw.println("Per-PID Stats:");
5605 didPid = true;
5606 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005607 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5608 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005609 pw.print(" PID "); pw.print(pids.keyAt(j));
5610 pw.print(" wake time: ");
5611 TimeUtils.formatDuration(time, pw);
5612 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005613 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005614 }
5615 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005616 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005617 pw.println();
5618 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005619 }
5620
5621 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005622 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5623 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005624 long timeRemaining = computeBatteryTimeRemaining(
5625 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005626 if (timeRemaining >= 0) {
5627 pw.print(" Estimated discharge time remaining: ");
5628 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5629 pw.println();
5630 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005631 final LevelStepTracker steps = getDischargeLevelStepTracker();
5632 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5633 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5634 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5635 STEP_LEVEL_MODE_VALUES[i], null));
5636 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005637 pw.println();
5638 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005639 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5640 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005641 long timeRemaining = computeChargeTimeRemaining(
5642 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005643 if (timeRemaining >= 0) {
5644 pw.print(" Estimated charge time remaining: ");
5645 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5646 pw.println();
5647 }
5648 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005649 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005650 }
5651 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5652 pw.println("Daily stats:");
5653 pw.print(" Current start time: ");
5654 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5655 getCurrentDailyStartTime()).toString());
5656 pw.print(" Next min deadline: ");
5657 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5658 getNextMinDailyDeadline()).toString());
5659 pw.print(" Next max deadline: ");
5660 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5661 getNextMaxDailyDeadline()).toString());
5662 StringBuilder sb = new StringBuilder(64);
5663 int[] outInt = new int[1];
5664 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5665 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005666 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5667 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005668 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005669 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5670 dsteps, false)) {
5671 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5672 sb, outInt);
5673 }
5674 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5675 csteps, false)) {
5676 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5677 sb, outInt);
5678 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005679 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005680 } else {
5681 pw.println(" Current daily steps:");
5682 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5683 sb, outInt);
5684 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5685 sb, outInt);
5686 }
5687 }
5688 DailyItem dit;
5689 int curIndex = 0;
5690 while ((dit=getDailyItemLocked(curIndex)) != null) {
5691 curIndex++;
5692 if ((flags&DUMP_DAILY_ONLY) != 0) {
5693 pw.println();
5694 }
5695 pw.print(" Daily from ");
5696 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5697 pw.print(" to ");
5698 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5699 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005700 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005701 if (dumpDurationSteps(pw, " ",
5702 " Discharge step durations:", dit.mDischargeSteps, false)) {
5703 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5704 sb, outInt);
5705 }
5706 if (dumpDurationSteps(pw, " ",
5707 " Charge step durations:", dit.mChargeSteps, false)) {
5708 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5709 sb, outInt);
5710 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005711 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005712 } else {
5713 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5714 sb, outInt);
5715 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5716 sb, outInt);
5717 }
5718 }
5719 pw.println();
5720 }
5721 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005722 pw.println("Statistics since last charge:");
5723 pw.println(" System starts: " + getStartCount()
5724 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005725 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5726 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005727 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 }
5730
5731 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005732 public void dumpCheckinLocked(Context context, PrintWriter pw,
5733 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005734 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005735
5736 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005737 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5738 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005739
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005740 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5741
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005742 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005743 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005744
5745 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005746 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005747 try {
5748 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5749 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5750 pw.print(HISTORY_STRING_POOL); pw.print(',');
5751 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005752 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005753 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005754 pw.print(",\"");
5755 String str = getHistoryTagPoolString(i);
5756 str = str.replace("\\", "\\\\");
5757 str = str.replace("\"", "\\\"");
5758 pw.print(str);
5759 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005760 pw.println();
5761 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005762 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005763 } finally {
5764 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005765 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005766 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005767 }
5768
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005769 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005770 return;
5771 }
5772
Dianne Hackborne4a59512010-12-07 11:08:07 -08005773 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005774 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005775 for (int i=0; i<apps.size(); i++) {
5776 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005777 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
5778 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005779 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005780 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
5781 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005782 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005783 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005784 }
5785 SparseArray<? extends Uid> uidStats = getUidStats();
5786 final int NU = uidStats.size();
5787 String[] lineArgs = new String[2];
5788 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005789 int uid = UserHandle.getAppId(uidStats.keyAt(i));
5790 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
5791 if (pkgs != null && !pkgs.second.value) {
5792 pkgs.second.value = true;
5793 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005794 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005795 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005796 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5797 (Object[])lineArgs);
5798 }
5799 }
5800 }
5801 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005802 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005803 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005804 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07005805 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005806 if (timeRemaining >= 0) {
5807 lineArgs[0] = Long.toString(timeRemaining);
5808 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5809 (Object[])lineArgs);
5810 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005811 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07005812 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005813 if (timeRemaining >= 0) {
5814 lineArgs[0] = Long.toString(timeRemaining);
5815 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5816 (Object[])lineArgs);
5817 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005818 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5819 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822}