blob: 1eb0a8c836cc198406a38e81163354525f019f37 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
19import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080020import java.util.ArrayList;
Dianne Hackborn81038902012-11-26 17:04:09 -080021import java.util.Collections;
22import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.Formatter;
Dianne Hackborn37de0982014-05-09 09:32:18 -070024import java.util.HashMap;
Dianne Hackborne4a59512010-12-07 11:08:07 -080025import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import java.util.Map;
27
Dianne Hackborna7c837f2014-01-15 16:20:44 -080028import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080029import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080030import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080031import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070032import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070033import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070034import android.util.MutableBoolean;
35import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.util.Printer;
37import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070038import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070039import android.util.TimeUtils;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070040import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080041
Dianne Hackborna7c837f2014-01-15 16:20:44 -080042import com.android.internal.os.BatterySipper;
43import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45/**
46 * A class providing access to battery usage statistics, including information on
47 * wakelocks, processes, packages, and services. All times are represented in microseconds
48 * except where indicated otherwise.
49 * @hide
50 */
51public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070052 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070055
56 /** @hide */
57 public static final String SERVICE_NAME = "batterystats";
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 /**
60 * A constant indicating a partial wake lock timer.
61 */
62 public static final int WAKE_TYPE_PARTIAL = 0;
63
64 /**
65 * A constant indicating a full wake lock timer.
66 */
67 public static final int WAKE_TYPE_FULL = 1;
68
69 /**
70 * A constant indicating a window wake lock timer.
71 */
72 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 /**
75 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 */
77 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070078
79 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070080 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070081 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070082 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070083
84 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070085 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070086 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070087 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070088
89 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070090 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070091 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070092 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
Dianne Hackborn62793e42015-03-09 11:15:41 -070094 /**
95 * A constant indicating a wifi multicast timer
96 */
97 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -070098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700100 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700101 */
102 public static final int VIDEO_TURNED_ON = 8;
103
104 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800105 * A constant indicating a vibrator on timer
106 */
107 public static final int VIBRATOR_ON = 9;
108
109 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700110 * A constant indicating a foreground activity timer
111 */
112 public static final int FOREGROUND_ACTIVITY = 10;
113
114 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700115 * A constant indicating a wifi batched scan is active
116 */
117 public static final int WIFI_BATCHED_SCAN = 11;
118
119 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700120 * A constant indicating a process state timer
121 */
122 public static final int PROCESS_STATE = 12;
123
124 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700125 * A constant indicating a sync timer
126 */
127 public static final int SYNC = 13;
128
129 /**
130 * A constant indicating a job timer
131 */
132 public static final int JOB = 14;
133
134 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800135 * A constant indicating an audio turn on timer
136 */
137 public static final int AUDIO_TURNED_ON = 15;
138
139 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700140 * A constant indicating a flashlight turn on timer
141 */
142 public static final int FLASHLIGHT_TURNED_ON = 16;
143
144 /**
145 * A constant indicating a camera turn on timer
146 */
147 public static final int CAMERA_TURNED_ON = 17;
148
149 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700150 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700151 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700152 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700153
154 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800155 * A constant indicating a bluetooth scan timer.
156 */
157 public static final int BLUETOOTH_SCAN_ON = 19;
158
159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 * Include all of the data in the stats, including previously saved data.
161 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700162 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 * Include only the current run in the stats.
166 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700167 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
169 /**
170 * Include only the run since the last time the device was unplugged in the stats.
171 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700172 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700173
174 // NOTE: Update this list if you add/change any stats above.
175 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700176 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700177 private static final String[] STAT_NAMES = { "l", "c", "u" };
178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700180 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700181 *
182 * New in version 19:
183 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800184 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700185 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700186 * New in version 21:
187 * - Actual (not just apportioned) Wakelock time is also recorded.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700188 */
Bookatz506a8182017-05-01 14:18:42 -0700189 static final String CHECKIN_VERSION = "21";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700190
191 /**
192 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700194 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700195
Evan Millar22ac0432009-03-31 11:33:18 -0700196 private static final long BYTES_PER_KB = 1024;
197 private static final long BYTES_PER_MB = 1048576; // 1024^2
198 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700199
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700200 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800201 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700202 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700204 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700205 private static final String CPU_DATA = "cpu";
Evan Millare84de8d2009-04-02 22:16:12 -0700206 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800207 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700208 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700209 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700210 // wl line is:
211 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
212 // full totalTime, 'f', count, current duration, max duration, total duration,
213 // partial totalTime, 'p', count, current duration, max duration, total duration,
214 // window totalTime, 'w', count, current duration, max duration, total duration
215 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700216 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700217 private static final String SYNC_DATA = "sy";
218 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700219 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700220 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700221 private static final String NETWORK_DATA = "nt";
222 private static final String USER_ACTIVITY_DATA = "ua";
223 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800224 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700225 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700226 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700227 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800228 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
229 private static final String WIFI_CONTROLLER_DATA = "wfcd";
230 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
231 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700232 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700233 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800234 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800235 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
236 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800237 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700238 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700239 private static final String SCREEN_BRIGHTNESS_DATA = "br";
240 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700241 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700242 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
243 private static final String DATA_CONNECTION_TIME_DATA = "dct";
244 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800245 private static final String WIFI_STATE_TIME_DATA = "wst";
246 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700247 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
248 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
249 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
250 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800251 private static final String POWER_USE_SUMMARY_DATA = "pws";
252 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700253 private static final String DISCHARGE_STEP_DATA = "dsd";
254 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700255 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
256 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700257 private static final String FLASHLIGHT_DATA = "fla";
258 private static final String CAMERA_DATA = "cam";
259 private static final String VIDEO_DATA = "vid";
260 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261
Adam Lesinski010bf372016-04-11 12:18:18 -0700262 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
263
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700264 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 private final Formatter mFormatter = new Formatter(mFormatBuilder);
266
267 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700268 * State for keeping track of counting information.
269 */
270 public static abstract class Counter {
271
272 /**
273 * Returns the count associated with this Counter for the
274 * selected type of statistics.
275 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700276 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700277 */
Evan Millarc64edde2009-04-18 12:26:32 -0700278 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700279
280 /**
281 * Temporary for debugging.
282 */
283 public abstract void logState(Printer pw, String prefix);
284 }
285
286 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700287 * State for keeping track of long counting information.
288 */
289 public static abstract class LongCounter {
290
291 /**
292 * Returns the count associated with this Counter for the
293 * selected type of statistics.
294 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700295 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700296 */
297 public abstract long getCountLocked(int which);
298
299 /**
300 * Temporary for debugging.
301 */
302 public abstract void logState(Printer pw, String prefix);
303 }
304
305 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800306 * Container class that aggregates counters for transmit, receive, and idle state of a
307 * radio controller.
308 */
309 public static abstract class ControllerActivityCounter {
310 /**
311 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
312 * idle state.
313 */
314 public abstract LongCounter getIdleTimeCounter();
315
316 /**
317 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
318 * receive state.
319 */
320 public abstract LongCounter getRxTimeCounter();
321
322 /**
323 * An array of {@link LongCounter}, representing various transmit levels, where each level
324 * may draw a different amount of power. The levels themselves are controller-specific.
325 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
326 * various transmit level states.
327 */
328 public abstract LongCounter[] getTxTimeCounters();
329
330 /**
331 * @return a non-null {@link LongCounter} representing the power consumed by the controller
332 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
333 * yield a value of 0 if the device doesn't support power calculations.
334 */
335 public abstract LongCounter getPowerCounter();
336 }
337
338 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 * State for keeping track of timing information.
340 */
341 public static abstract class Timer {
342
343 /**
344 * Returns the count associated with this Timer for the
345 * selected type of statistics.
346 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700347 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 */
Evan Millarc64edde2009-04-18 12:26:32 -0700349 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350
351 /**
352 * Returns the total time in microseconds associated with this Timer for the
353 * selected type of statistics.
354 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800355 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700356 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 * @return a time in microseconds
358 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800359 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700362 * Returns the total time in microseconds associated with this Timer since the
363 * 'mark' was last set.
364 *
365 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
366 * @return a time in microseconds
367 */
368 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
369
370 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700371 * Returns the max duration if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800372 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700373
374 */
375 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
376 return -1;
377 }
378
379 /**
380 * Returns the current time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800381 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700382 */
383 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
384 return -1;
385 }
386
387 /**
Bookatz867c0d72017-03-07 18:23:42 -0800388 * Returns the current time the timer has been active, if it is being tracked.
389 *
390 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
391 * been on since reset.
392 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
393 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
394 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
395 * the actual total time.
396 * Not all Timer subclasses track the max, total, current durations.
397 */
398 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
399 return -1;
400 }
401
402 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700403 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
404 * used, for example, for tracking background usage. Secondary timers are never pooled.
405 *
406 * Not all Timer subclasses have a secondary timer; those that don't return null.
407 */
408 public Timer getSubTimer() {
409 return null;
410 }
411
412 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700413 * Returns whether the timer is currently running. Some types of timers
414 * (e.g. BatchTimers) don't know whether the event is currently active,
415 * and report false.
416 */
417 public boolean isRunningLocked() {
418 return false;
419 }
420
421 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 * Temporary for debugging.
423 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700424 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 }
426
427 /**
428 * The statistics associated with a particular uid.
429 */
430 public static abstract class Uid {
431
432 /**
433 * Returns a mapping containing wakelock statistics.
434 *
435 * @return a Map from Strings to Uid.Wakelock objects.
436 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700437 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438
439 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700440 * Returns a mapping containing sync statistics.
441 *
442 * @return a Map from Strings to Timer objects.
443 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700444 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700445
446 /**
447 * Returns a mapping containing scheduled job statistics.
448 *
449 * @return a Map from Strings to Timer objects.
450 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700451 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700452
453 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 * The statistics associated with a particular wake lock.
455 */
456 public static abstract class Wakelock {
457 public abstract Timer getWakeTime(int type);
458 }
459
460 /**
461 * Returns a mapping containing sensor statistics.
462 *
463 * @return a Map from Integer sensor ids to Uid.Sensor objects.
464 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700465 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466
467 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700468 * Returns a mapping containing active process data.
469 */
470 public abstract SparseArray<? extends Pid> getPidStats();
471
472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 * Returns a mapping containing process statistics.
474 *
475 * @return a Map from Strings to Uid.Proc objects.
476 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700477 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478
479 /**
480 * Returns a mapping containing package statistics.
481 *
482 * @return a Map from Strings to Uid.Pkg objects.
483 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700484 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700485
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800486 public abstract ControllerActivityCounter getWifiControllerActivity();
487 public abstract ControllerActivityCounter getBluetoothControllerActivity();
488 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800489
490 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 * {@hide}
492 */
493 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700494
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800495 public abstract void noteWifiRunningLocked(long elapsedRealtime);
496 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
497 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
498 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
499 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
500 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
501 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
502 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
503 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
504 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800505 public abstract void noteActivityResumedLocked(long elapsedRealtime);
506 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800507 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
508 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
509 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700510 public abstract int getWifiScanCount(int which);
Bookatz867c0d72017-03-07 18:23:42 -0800511 public abstract int getWifiScanBackgroundCount(int which);
512 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
513 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800514 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700515 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800516 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700517 public abstract Timer getAudioTurnedOnTimer();
518 public abstract Timer getVideoTurnedOnTimer();
519 public abstract Timer getFlashlightTurnedOnTimer();
520 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700521 public abstract Timer getForegroundActivityTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800522 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800523 public abstract Timer getBluetoothScanBackgroundTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700524
Dianne Hackborna0200e32016-03-30 18:01:41 -0700525 // Note: the following times are disjoint. They can be added together to find the
526 // total time a uid has had any processes running at all.
527
528 /**
529 * Time this uid has any processes in the top state (or above such as persistent).
530 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800531 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700532 /**
533 * Time this uid has any process with a started out bound foreground service, but
534 * none in the "top" state.
535 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800536 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700537 /**
538 * Time this uid has any process that is top while the device is sleeping, but none
539 * in the "foreground service" or better state.
540 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800541 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700542 /**
543 * Time this uid has any process in an active foreground state, but none in the
544 * "top sleeping" or better state.
545 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800546 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700547 /**
548 * Time this uid has any process in an active background state, but none in the
549 * "foreground" or better state.
550 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800551 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700552 /**
553 * Time this uid has any processes that are sitting around cached, not in one of the
554 * other active states.
555 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800556 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700557 /**
558 * Total number of process states we track.
559 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800560 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700561
562 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800563 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700564 };
565
566 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800567 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700568
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800569 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
Robert Greenwalta029ea12013-09-25 16:38:12 -0700571 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
572
Dianne Hackborn617f8772009-03-31 15:04:46 -0700573 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700574 * Note that these must match the constants in android.os.PowerManager.
575 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
576 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700577 */
578 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700579 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700580 };
581
Phil Weaverda80d672016-03-15 16:25:46 -0700582 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700583
Dianne Hackborn617f8772009-03-31 15:04:46 -0700584 public abstract void noteUserActivityLocked(int type);
585 public abstract boolean hasUserActivity();
586 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700587
588 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800589 public abstract long getNetworkActivityBytes(int type, int which);
590 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800591 public abstract long getMobileRadioActiveTime(int which);
592 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700593
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700594 /**
595 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
596 */
597 public abstract long getUserCpuTimeUs(int which);
598
599 /**
600 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
601 */
602 public abstract long getSystemCpuTimeUs(int which);
603
604 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700605 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
606 * given CPU cluster.
607 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700608 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700609 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700610 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
611 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700612 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700613 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700614
Adam Lesinski5f056f62016-07-14 16:56:08 -0700615 /**
616 * Returns the number of times this UID woke up the Application Processor to
617 * process a mobile radio packet.
618 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
619 */
620 public abstract long getMobileRadioApWakeupCount(int which);
621
622 /**
623 * Returns the number of times this UID woke up the Application Processor to
624 * process a WiFi packet.
625 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
626 */
627 public abstract long getWifiRadioApWakeupCount(int which);
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800630 /*
631 * FIXME: it's not correct to use this magic value because it
632 * could clash with a sensor handle (which are defined by
633 * the sensor HAL, and therefore out of our control
634 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 // Magic sensor number for the GPS.
636 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800641
Bookatz867c0d72017-03-07 18:23:42 -0800642 /** Returns a Timer for sensor usage when app is in the background. */
643 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 }
645
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700646 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800647 public int mWakeNesting;
648 public long mWakeSumMs;
649 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700650 }
651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 /**
653 * The statistics associated with a particular process.
654 */
655 public static abstract class Proc {
656
Dianne Hackborn287952c2010-09-22 22:34:31 -0700657 public static class ExcessivePower {
658 public static final int TYPE_WAKE = 1;
659 public static final int TYPE_CPU = 2;
660
661 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700662 public long overTime;
663 public long usedTime;
664 }
665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800667 * Returns true if this process is still active in the battery stats.
668 */
669 public abstract boolean isActive();
670
671 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700672 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700674 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 */
676 public abstract long getUserTime(int which);
677
678 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700679 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700681 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 */
683 public abstract long getSystemTime(int which);
684
685 /**
686 * Returns the number of times the process has been started.
687 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700688 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 */
690 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700691
692 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800693 * Returns the number of times the process has crashed.
694 *
695 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
696 */
697 public abstract int getNumCrashes(int which);
698
699 /**
700 * Returns the number of times the process has ANRed.
701 *
702 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
703 */
704 public abstract int getNumAnrs(int which);
705
706 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700707 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700708 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700709 * @return foreground cpu time in microseconds
710 */
711 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700712
Dianne Hackborn287952c2010-09-22 22:34:31 -0700713 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700714
Dianne Hackborn287952c2010-09-22 22:34:31 -0700715 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717
718 /**
719 * The statistics associated with a particular package.
720 */
721 public static abstract class Pkg {
722
723 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700724 * Returns information about all wakeup alarms that have been triggered for this
725 * package. The mapping keys are tag names for the alarms, the counter contains
726 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700728 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729
730 /**
731 * Returns a mapping containing service statistics.
732 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700733 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734
735 /**
736 * The statistics associated with a particular service.
737 */
Joe Onoratoabded112016-02-08 16:49:39 -0800738 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739
740 /**
741 * Returns the amount of time spent started.
742 *
743 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700744 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 * @return
746 */
747 public abstract long getStartTime(long batteryUptime, int which);
748
749 /**
750 * Returns the total number of times startService() has been called.
751 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700752 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 */
754 public abstract int getStarts(int which);
755
756 /**
757 * Returns the total number times the service has been launched.
758 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700759 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 */
761 public abstract int getLaunches(int which);
762 }
763 }
764 }
765
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800766 public static final class LevelStepTracker {
767 public long mLastStepTime = -1;
768 public int mNumStepDurations;
769 public final long[] mStepDurations;
770
771 public LevelStepTracker(int maxLevelSteps) {
772 mStepDurations = new long[maxLevelSteps];
773 }
774
775 public LevelStepTracker(int numSteps, long[] steps) {
776 mNumStepDurations = numSteps;
777 mStepDurations = new long[numSteps];
778 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
779 }
780
781 public long getDurationAt(int index) {
782 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
783 }
784
785 public int getLevelAt(int index) {
786 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
787 >> STEP_LEVEL_LEVEL_SHIFT);
788 }
789
790 public int getInitModeAt(int index) {
791 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
792 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
793 }
794
795 public int getModModeAt(int index) {
796 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
797 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
798 }
799
800 private void appendHex(long val, int topOffset, StringBuilder out) {
801 boolean hasData = false;
802 while (topOffset >= 0) {
803 int digit = (int)( (val>>topOffset) & 0xf );
804 topOffset -= 4;
805 if (!hasData && digit == 0) {
806 continue;
807 }
808 hasData = true;
809 if (digit >= 0 && digit <= 9) {
810 out.append((char)('0' + digit));
811 } else {
812 out.append((char)('a' + digit - 10));
813 }
814 }
815 }
816
817 public void encodeEntryAt(int index, StringBuilder out) {
818 long item = mStepDurations[index];
819 long duration = item & STEP_LEVEL_TIME_MASK;
820 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
821 >> STEP_LEVEL_LEVEL_SHIFT);
822 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
823 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
824 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
825 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
826 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
827 case Display.STATE_OFF: out.append('f'); break;
828 case Display.STATE_ON: out.append('o'); break;
829 case Display.STATE_DOZE: out.append('d'); break;
830 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
831 }
832 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
833 out.append('p');
834 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700835 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
836 out.append('i');
837 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800838 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
839 case Display.STATE_OFF: out.append('F'); break;
840 case Display.STATE_ON: out.append('O'); break;
841 case Display.STATE_DOZE: out.append('D'); break;
842 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
843 }
844 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
845 out.append('P');
846 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700847 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
848 out.append('I');
849 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800850 out.append('-');
851 appendHex(level, 4, out);
852 out.append('-');
853 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
854 }
855
856 public void decodeEntryAt(int index, String value) {
857 final int N = value.length();
858 int i = 0;
859 char c;
860 long out = 0;
861 while (i < N && (c=value.charAt(i)) != '-') {
862 i++;
863 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800864 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800865 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800866 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800867 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800868 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800869 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800870 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
871 << STEP_LEVEL_INITIAL_MODE_SHIFT);
872 break;
873 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
874 << STEP_LEVEL_INITIAL_MODE_SHIFT);
875 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700876 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
877 << STEP_LEVEL_INITIAL_MODE_SHIFT);
878 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800879 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
880 break;
881 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
882 break;
883 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
884 break;
885 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
886 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
887 break;
888 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
889 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800890 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700891 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
892 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
893 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800894 }
895 }
896 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800897 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800898 while (i < N && (c=value.charAt(i)) != '-') {
899 i++;
900 level <<= 4;
901 if (c >= '0' && c <= '9') {
902 level += c - '0';
903 } else if (c >= 'a' && c <= 'f') {
904 level += c - 'a' + 10;
905 } else if (c >= 'A' && c <= 'F') {
906 level += c - 'A' + 10;
907 }
908 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800909 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800910 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
911 long duration = 0;
912 while (i < N && (c=value.charAt(i)) != '-') {
913 i++;
914 duration <<= 4;
915 if (c >= '0' && c <= '9') {
916 duration += c - '0';
917 } else if (c >= 'a' && c <= 'f') {
918 duration += c - 'a' + 10;
919 } else if (c >= 'A' && c <= 'F') {
920 duration += c - 'A' + 10;
921 }
922 }
923 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
924 }
925
926 public void init() {
927 mLastStepTime = -1;
928 mNumStepDurations = 0;
929 }
930
931 public void clearTime() {
932 mLastStepTime = -1;
933 }
934
935 public long computeTimePerLevel() {
936 final long[] steps = mStepDurations;
937 final int numSteps = mNumStepDurations;
938
939 // For now we'll do a simple average across all steps.
940 if (numSteps <= 0) {
941 return -1;
942 }
943 long total = 0;
944 for (int i=0; i<numSteps; i++) {
945 total += steps[i] & STEP_LEVEL_TIME_MASK;
946 }
947 return total / numSteps;
948 /*
949 long[] buckets = new long[numSteps];
950 int numBuckets = 0;
951 int numToAverage = 4;
952 int i = 0;
953 while (i < numSteps) {
954 long totalTime = 0;
955 int num = 0;
956 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
957 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
958 num++;
959 }
960 buckets[numBuckets] = totalTime / num;
961 numBuckets++;
962 numToAverage *= 2;
963 i += num;
964 }
965 if (numBuckets < 1) {
966 return -1;
967 }
968 long averageTime = buckets[numBuckets-1];
969 for (i=numBuckets-2; i>=0; i--) {
970 averageTime = (averageTime + buckets[i]) / 2;
971 }
972 return averageTime;
973 */
974 }
975
976 public long computeTimeEstimate(long modesOfInterest, long modeValues,
977 int[] outNumOfInterest) {
978 final long[] steps = mStepDurations;
979 final int count = mNumStepDurations;
980 if (count <= 0) {
981 return -1;
982 }
983 long total = 0;
984 int numOfInterest = 0;
985 for (int i=0; i<count; i++) {
986 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
987 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
988 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
989 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
990 // If the modes of interest didn't change during this step period...
991 if ((modMode&modesOfInterest) == 0) {
992 // And the mode values during this period match those we are measuring...
993 if ((initMode&modesOfInterest) == modeValues) {
994 // Then this can be used to estimate the total time!
995 numOfInterest++;
996 total += steps[i] & STEP_LEVEL_TIME_MASK;
997 }
998 }
999 }
1000 if (numOfInterest <= 0) {
1001 return -1;
1002 }
1003
1004 if (outNumOfInterest != null) {
1005 outNumOfInterest[0] = numOfInterest;
1006 }
1007
1008 // The estimated time is the average time we spend in each level, multipled
1009 // by 100 -- the total number of battery levels
1010 return (total / numOfInterest) * 100;
1011 }
1012
1013 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1014 int stepCount = mNumStepDurations;
1015 final long lastStepTime = mLastStepTime;
1016 if (lastStepTime >= 0 && numStepLevels > 0) {
1017 final long[] steps = mStepDurations;
1018 long duration = elapsedRealtime - lastStepTime;
1019 for (int i=0; i<numStepLevels; i++) {
1020 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1021 long thisDuration = duration / (numStepLevels-i);
1022 duration -= thisDuration;
1023 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1024 thisDuration = STEP_LEVEL_TIME_MASK;
1025 }
1026 steps[0] = thisDuration | modeBits;
1027 }
1028 stepCount += numStepLevels;
1029 if (stepCount > steps.length) {
1030 stepCount = steps.length;
1031 }
1032 }
1033 mNumStepDurations = stepCount;
1034 mLastStepTime = elapsedRealtime;
1035 }
1036
1037 public void readFromParcel(Parcel in) {
1038 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001039 if (N > mStepDurations.length) {
1040 throw new ParcelFormatException("more step durations than available: " + N);
1041 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001042 mNumStepDurations = N;
1043 for (int i=0; i<N; i++) {
1044 mStepDurations[i] = in.readLong();
1045 }
1046 }
1047
1048 public void writeToParcel(Parcel out) {
1049 final int N = mNumStepDurations;
1050 out.writeInt(N);
1051 for (int i=0; i<N; i++) {
1052 out.writeLong(mStepDurations[i]);
1053 }
1054 }
1055 }
1056
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001057 public static final class PackageChange {
1058 public String mPackageName;
1059 public boolean mUpdate;
1060 public int mVersionCode;
1061 }
1062
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001063 public static final class DailyItem {
1064 public long mStartTime;
1065 public long mEndTime;
1066 public LevelStepTracker mDischargeSteps;
1067 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001068 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001069 }
1070
1071 public abstract DailyItem getDailyItemLocked(int daysAgo);
1072
1073 public abstract long getCurrentDailyStartTime();
1074
1075 public abstract long getNextMinDailyDeadline();
1076
1077 public abstract long getNextMaxDailyDeadline();
1078
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001079 public final static class HistoryTag {
1080 public String string;
1081 public int uid;
1082
1083 public int poolIdx;
1084
1085 public void setTo(HistoryTag o) {
1086 string = o.string;
1087 uid = o.uid;
1088 poolIdx = o.poolIdx;
1089 }
1090
1091 public void setTo(String _string, int _uid) {
1092 string = _string;
1093 uid = _uid;
1094 poolIdx = -1;
1095 }
1096
1097 public void writeToParcel(Parcel dest, int flags) {
1098 dest.writeString(string);
1099 dest.writeInt(uid);
1100 }
1101
1102 public void readFromParcel(Parcel src) {
1103 string = src.readString();
1104 uid = src.readInt();
1105 poolIdx = -1;
1106 }
1107
1108 @Override
1109 public boolean equals(Object o) {
1110 if (this == o) return true;
1111 if (o == null || getClass() != o.getClass()) return false;
1112
1113 HistoryTag that = (HistoryTag) o;
1114
1115 if (uid != that.uid) return false;
1116 if (!string.equals(that.string)) return false;
1117
1118 return true;
1119 }
1120
1121 @Override
1122 public int hashCode() {
1123 int result = string.hashCode();
1124 result = 31 * result + uid;
1125 return result;
1126 }
1127 }
1128
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001129 /**
1130 * Optional detailed information that can go into a history step. This is typically
1131 * generated each time the battery level changes.
1132 */
1133 public final static class HistoryStepDetails {
1134 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1135 public int userTime;
1136 public int systemTime;
1137
1138 // Top three apps using CPU in the last step, with times in 1/100 second.
1139 public int appCpuUid1;
1140 public int appCpuUTime1;
1141 public int appCpuSTime1;
1142 public int appCpuUid2;
1143 public int appCpuUTime2;
1144 public int appCpuSTime2;
1145 public int appCpuUid3;
1146 public int appCpuUTime3;
1147 public int appCpuSTime3;
1148
1149 // Information from /proc/stat
1150 public int statUserTime;
1151 public int statSystemTime;
1152 public int statIOWaitTime;
1153 public int statIrqTime;
1154 public int statSoftIrqTime;
1155 public int statIdlTime;
1156
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001157 // Platform-level low power state stats
1158 public String statPlatformIdleState;
1159
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001160 public HistoryStepDetails() {
1161 clear();
1162 }
1163
1164 public void clear() {
1165 userTime = systemTime = 0;
1166 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1167 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1168 = appCpuUTime3 = appCpuSTime3 = 0;
1169 }
1170
1171 public void writeToParcel(Parcel out) {
1172 out.writeInt(userTime);
1173 out.writeInt(systemTime);
1174 out.writeInt(appCpuUid1);
1175 out.writeInt(appCpuUTime1);
1176 out.writeInt(appCpuSTime1);
1177 out.writeInt(appCpuUid2);
1178 out.writeInt(appCpuUTime2);
1179 out.writeInt(appCpuSTime2);
1180 out.writeInt(appCpuUid3);
1181 out.writeInt(appCpuUTime3);
1182 out.writeInt(appCpuSTime3);
1183 out.writeInt(statUserTime);
1184 out.writeInt(statSystemTime);
1185 out.writeInt(statIOWaitTime);
1186 out.writeInt(statIrqTime);
1187 out.writeInt(statSoftIrqTime);
1188 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001189 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001190 }
1191
1192 public void readFromParcel(Parcel in) {
1193 userTime = in.readInt();
1194 systemTime = in.readInt();
1195 appCpuUid1 = in.readInt();
1196 appCpuUTime1 = in.readInt();
1197 appCpuSTime1 = in.readInt();
1198 appCpuUid2 = in.readInt();
1199 appCpuUTime2 = in.readInt();
1200 appCpuSTime2 = in.readInt();
1201 appCpuUid3 = in.readInt();
1202 appCpuUTime3 = in.readInt();
1203 appCpuSTime3 = in.readInt();
1204 statUserTime = in.readInt();
1205 statSystemTime = in.readInt();
1206 statIOWaitTime = in.readInt();
1207 statIrqTime = in.readInt();
1208 statSoftIrqTime = in.readInt();
1209 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001210 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001211 }
1212 }
1213
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001214 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001215 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001216
1217 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001218 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001219
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001220 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001221 public static final byte CMD_NULL = -1;
1222 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001223 public static final byte CMD_CURRENT_TIME = 5;
1224 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001225 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001226 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001227
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001228 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001229
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001230 /**
1231 * Return whether the command code is a delta data update.
1232 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001233 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001234 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001235 }
1236
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001237 public byte batteryLevel;
1238 public byte batteryStatus;
1239 public byte batteryHealth;
1240 public byte batteryPlugType;
1241
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001242 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001243 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001244
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001245 // The charge of the battery in micro-Ampere-hours.
1246 public int batteryChargeUAh;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001247
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001248 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001249 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001250 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001251 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001252 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1253 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001254 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001255 public static final int STATE_PHONE_STATE_SHIFT = 6;
1256 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001257 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001258 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001259 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001260
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001261 // These states always appear directly in the first int token
1262 // of a delta change; they should be ones that change relatively
1263 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001264 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1265 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001266 public static final int STATE_GPS_ON_FLAG = 1<<29;
1267 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001268 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001269 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001270 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001271 // Do not use, this is used for coulomb delta count.
1272 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001273 // These are on the lower bits used for the command; if they change
1274 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001275 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001276 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001277 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001278 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1279 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1280 // empty slot
1281 // empty slot
1282 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001283
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001284 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001285 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1286
1287 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001288
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001289 public int states;
1290
Dianne Hackborn3251b902014-06-20 14:40:53 -07001291 // Constants from WIFI_SUPPL_STATE_*
1292 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1293 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1294 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1295 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1296 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1297 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1298
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001299 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001300 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1301 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1302 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001303 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001304 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1305 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1306 public static final int STATE2_CHARGING_FLAG = 1<<24;
1307 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1308 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1309 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001310 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001311
1312 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001313 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001314 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1315
1316 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001317
Dianne Hackborn40c87252014-03-19 16:55:40 -07001318 public int states2;
1319
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001320 // The wake lock that was acquired at this point.
1321 public HistoryTag wakelockTag;
1322
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001323 // Kernel wakeup reason at this point.
1324 public HistoryTag wakeReasonTag;
1325
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001326 // Non-null when there is more detailed information at this step.
1327 public HistoryStepDetails stepDetails;
1328
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001329 public static final int EVENT_FLAG_START = 0x8000;
1330 public static final int EVENT_FLAG_FINISH = 0x4000;
1331
1332 // No event in this item.
1333 public static final int EVENT_NONE = 0x0000;
1334 // Event is about a process that is running.
1335 public static final int EVENT_PROC = 0x0001;
1336 // Event is about an application package that is in the foreground.
1337 public static final int EVENT_FOREGROUND = 0x0002;
1338 // Event is about an application package that is at the top of the screen.
1339 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001340 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001341 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001342 // Events for all additional wake locks aquired/release within a wake block.
1343 // These are not generated by default.
1344 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001345 // Event is about an application executing a scheduled job.
1346 public static final int EVENT_JOB = 0x0006;
1347 // Events for users running.
1348 public static final int EVENT_USER_RUNNING = 0x0007;
1349 // Events for foreground user.
1350 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001351 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001352 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001353 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001354 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001355 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001356 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001357 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001358 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001359 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001360 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001361 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001362 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001363 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001364 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001365 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001366 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001367 // Event for a package being on the temporary whitelist.
1368 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001369 // Event for the screen waking up.
1370 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001371 // Event for the UID that woke up the application processor.
1372 // Used for wakeups coming from WiFi, modem, etc.
1373 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001374 // Event for reporting that a specific partial wake lock has been held for a long duration.
1375 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001376
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001377 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001378 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001379 // Mask to extract out only the type part of the event.
1380 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001381
1382 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1383 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1384 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1385 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1386 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1387 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001388 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1389 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001390 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1391 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001392 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1393 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1394 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1395 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1396 public static final int EVENT_USER_FOREGROUND_START =
1397 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1398 public static final int EVENT_USER_FOREGROUND_FINISH =
1399 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001400 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1401 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001402 public static final int EVENT_TEMP_WHITELIST_START =
1403 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1404 public static final int EVENT_TEMP_WHITELIST_FINISH =
1405 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001406 public static final int EVENT_LONG_WAKE_LOCK_START =
1407 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1408 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1409 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001410
1411 // For CMD_EVENT.
1412 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001413 public HistoryTag eventTag;
1414
Dianne Hackborn9a755432014-05-15 17:05:22 -07001415 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001416 public long currentTime;
1417
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001418 // Meta-data when reading.
1419 public int numReadInts;
1420
1421 // Pre-allocated objects.
1422 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001423 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001424 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001425
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001426 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001427 }
1428
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001429 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001430 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001431 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001432 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001433 }
1434
1435 public int describeContents() {
1436 return 0;
1437 }
1438
1439 public void writeToParcel(Parcel dest, int flags) {
1440 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001441 int bat = (((int)cmd)&0xff)
1442 | ((((int)batteryLevel)<<8)&0xff00)
1443 | ((((int)batteryStatus)<<16)&0xf0000)
1444 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001445 | ((((int)batteryPlugType)<<24)&0xf000000)
1446 | (wakelockTag != null ? 0x10000000 : 0)
1447 | (wakeReasonTag != null ? 0x20000000 : 0)
1448 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001449 dest.writeInt(bat);
1450 bat = (((int)batteryTemperature)&0xffff)
1451 | ((((int)batteryVoltage)<<16)&0xffff0000);
1452 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001453 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001454 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001455 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001456 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001457 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001458 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001459 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001460 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001461 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001462 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001463 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001464 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001465 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001466 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001467 dest.writeLong(currentTime);
1468 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001469 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001470
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001471 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001472 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001473 int bat = src.readInt();
1474 cmd = (byte)(bat&0xff);
1475 batteryLevel = (byte)((bat>>8)&0xff);
1476 batteryStatus = (byte)((bat>>16)&0xf);
1477 batteryHealth = (byte)((bat>>20)&0xf);
1478 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001479 int bat2 = src.readInt();
1480 batteryTemperature = (short)(bat2&0xffff);
1481 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001482 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001483 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001484 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001485 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001486 wakelockTag = localWakelockTag;
1487 wakelockTag.readFromParcel(src);
1488 } else {
1489 wakelockTag = null;
1490 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001491 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001492 wakeReasonTag = localWakeReasonTag;
1493 wakeReasonTag.readFromParcel(src);
1494 } else {
1495 wakeReasonTag = null;
1496 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001497 if ((bat&0x40000000) != 0) {
1498 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001499 eventTag = localEventTag;
1500 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001501 } else {
1502 eventCode = EVENT_NONE;
1503 eventTag = null;
1504 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001505 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001506 currentTime = src.readLong();
1507 } else {
1508 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001509 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001510 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001511 }
1512
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001513 public void clear() {
1514 time = 0;
1515 cmd = CMD_NULL;
1516 batteryLevel = 0;
1517 batteryStatus = 0;
1518 batteryHealth = 0;
1519 batteryPlugType = 0;
1520 batteryTemperature = 0;
1521 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001522 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001523 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001524 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001525 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001526 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001527 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001528 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001529 }
1530
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001531 public void setTo(HistoryItem o) {
1532 time = o.time;
1533 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001534 setToCommon(o);
1535 }
1536
1537 public void setTo(long time, byte cmd, HistoryItem o) {
1538 this.time = time;
1539 this.cmd = cmd;
1540 setToCommon(o);
1541 }
1542
1543 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001544 batteryLevel = o.batteryLevel;
1545 batteryStatus = o.batteryStatus;
1546 batteryHealth = o.batteryHealth;
1547 batteryPlugType = o.batteryPlugType;
1548 batteryTemperature = o.batteryTemperature;
1549 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001550 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001551 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001552 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001553 if (o.wakelockTag != null) {
1554 wakelockTag = localWakelockTag;
1555 wakelockTag.setTo(o.wakelockTag);
1556 } else {
1557 wakelockTag = null;
1558 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001559 if (o.wakeReasonTag != null) {
1560 wakeReasonTag = localWakeReasonTag;
1561 wakeReasonTag.setTo(o.wakeReasonTag);
1562 } else {
1563 wakeReasonTag = null;
1564 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001565 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001566 if (o.eventTag != null) {
1567 eventTag = localEventTag;
1568 eventTag.setTo(o.eventTag);
1569 } else {
1570 eventTag = null;
1571 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001572 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001573 }
1574
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001575 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001576 return batteryLevel == o.batteryLevel
1577 && batteryStatus == o.batteryStatus
1578 && batteryHealth == o.batteryHealth
1579 && batteryPlugType == o.batteryPlugType
1580 && batteryTemperature == o.batteryTemperature
1581 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001582 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001583 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001584 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001585 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001586 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001587
1588 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001589 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001590 return false;
1591 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001592 if (wakelockTag != o.wakelockTag) {
1593 if (wakelockTag == null || o.wakelockTag == null) {
1594 return false;
1595 }
1596 if (!wakelockTag.equals(o.wakelockTag)) {
1597 return false;
1598 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001599 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001600 if (wakeReasonTag != o.wakeReasonTag) {
1601 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1602 return false;
1603 }
1604 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1605 return false;
1606 }
1607 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001608 if (eventTag != o.eventTag) {
1609 if (eventTag == null || o.eventTag == null) {
1610 return false;
1611 }
1612 if (!eventTag.equals(o.eventTag)) {
1613 return false;
1614 }
1615 }
1616 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001617 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001618 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001619
1620 public final static class HistoryEventTracker {
1621 private final HashMap<String, SparseIntArray>[] mActiveEvents
1622 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1623
1624 public boolean updateState(int code, String name, int uid, int poolIdx) {
1625 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1626 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1627 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1628 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001629 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001630 mActiveEvents[idx] = active;
1631 }
1632 SparseIntArray uids = active.get(name);
1633 if (uids == null) {
1634 uids = new SparseIntArray();
1635 active.put(name, uids);
1636 }
1637 if (uids.indexOfKey(uid) >= 0) {
1638 // Already set, nothing to do!
1639 return false;
1640 }
1641 uids.put(uid, poolIdx);
1642 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1643 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1644 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1645 if (active == null) {
1646 // not currently active, nothing to do.
1647 return false;
1648 }
1649 SparseIntArray uids = active.get(name);
1650 if (uids == null) {
1651 // not currently active, nothing to do.
1652 return false;
1653 }
1654 idx = uids.indexOfKey(uid);
1655 if (idx < 0) {
1656 // not currently active, nothing to do.
1657 return false;
1658 }
1659 uids.removeAt(idx);
1660 if (uids.size() <= 0) {
1661 active.remove(name);
1662 }
1663 }
1664 return true;
1665 }
1666
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001667 public void removeEvents(int code) {
1668 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1669 mActiveEvents[idx] = null;
1670 }
1671
Dianne Hackborn37de0982014-05-09 09:32:18 -07001672 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1673 return mActiveEvents[code];
1674 }
1675 }
1676
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001677 public static final class BitDescription {
1678 public final int mask;
1679 public final int shift;
1680 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001681 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001682 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001683 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001684
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001685 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001686 this.mask = mask;
1687 this.shift = -1;
1688 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001689 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001690 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001691 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001692 }
1693
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001694 public BitDescription(int mask, int shift, String name, String shortName,
1695 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001696 this.mask = mask;
1697 this.shift = shift;
1698 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001699 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001700 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001701 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001702 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001703 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001704
Dianne Hackbornfc064132014-06-02 12:42:12 -07001705 /**
1706 * Don't allow any more batching in to the current history event. This
1707 * is called when printing partial histories, so to ensure that the next
1708 * history event will go in to a new batch after what was printed in the
1709 * last partial history.
1710 */
1711 public abstract void commitCurrentHistoryBatchLocked();
1712
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001713 public abstract int getHistoryTotalSize();
1714
1715 public abstract int getHistoryUsedSize();
1716
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001717 public abstract boolean startIteratingHistoryLocked();
1718
Dianne Hackborn099bc622014-01-22 13:39:16 -08001719 public abstract int getHistoryStringPoolSize();
1720
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001721 public abstract int getHistoryStringPoolBytes();
1722
1723 public abstract String getHistoryTagPoolString(int index);
1724
1725 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001726
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001727 public abstract boolean getNextHistoryLocked(HistoryItem out);
1728
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001729 public abstract void finishIteratingHistoryLocked();
1730
1731 public abstract boolean startIteratingOldHistoryLocked();
1732
1733 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1734
1735 public abstract void finishIteratingOldHistoryLocked();
1736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001738 * Return the base time offset for the battery history.
1739 */
1740 public abstract long getHistoryBaseTime();
1741
1742 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 * Returns the number of times the device has been started.
1744 */
1745 public abstract int getStartCount();
1746
1747 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001748 * 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 -08001749 * running on battery.
1750 *
1751 * {@hide}
1752 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001753 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001755 /**
1756 * Returns the number of times the screen was turned on.
1757 *
1758 * {@hide}
1759 */
1760 public abstract int getScreenOnCount(int which);
1761
Jeff Browne95c3cd2014-05-02 16:59:26 -07001762 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1763
Dianne Hackborn617f8772009-03-31 15:04:46 -07001764 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1765 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1766 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1767 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1768 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1769
1770 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1771 "dark", "dim", "medium", "light", "bright"
1772 };
1773
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001774 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1775 "0", "1", "2", "3", "4"
1776 };
1777
Dianne Hackborn617f8772009-03-31 15:04:46 -07001778 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001779
Dianne Hackborn617f8772009-03-31 15:04:46 -07001780 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001781 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001782 * the given brightness
1783 *
1784 * {@hide}
1785 */
1786 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001787 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001790 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001791 * running on battery.
1792 *
1793 * {@hide}
1794 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001795 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001796
1797 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001798 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001799 *
1800 * {@hide}
1801 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001802 public abstract int getPowerSaveModeEnabledCount(int which);
1803
1804 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001805 * Constant for device idle mode: not active.
1806 */
1807 public static final int DEVICE_IDLE_MODE_OFF = 0;
1808
1809 /**
1810 * Constant for device idle mode: active in lightweight mode.
1811 */
1812 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1813
1814 /**
1815 * Constant for device idle mode: active in full mode.
1816 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001817 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001818
1819 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001820 * Returns the time in microseconds that device has been in idle mode while
1821 * running on battery.
1822 *
1823 * {@hide}
1824 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001825 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001826
1827 /**
1828 * Returns the number of times that the devie has gone in to idle mode.
1829 *
1830 * {@hide}
1831 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001832 public abstract int getDeviceIdleModeCount(int mode, int which);
1833
1834 /**
1835 * Return the longest duration we spent in a particular device idle mode (fully in the
1836 * mode, not in idle maintenance etc).
1837 */
1838 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001839
1840 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001841 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001842 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001843 * counts all of the time that we consider the device to be idle, whether or not
1844 * it is currently in the actual device idle mode.
1845 *
1846 * {@hide}
1847 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001848 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001849
1850 /**
1851 * Returns the number of times that the devie has started idling.
1852 *
1853 * {@hide}
1854 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001855 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001856
1857 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001858 * Returns the number of times that connectivity state changed.
1859 *
1860 * {@hide}
1861 */
1862 public abstract int getNumConnectivityChange(int which);
1863
1864 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001865 * 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 -08001866 * running on battery.
1867 *
1868 * {@hide}
1869 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001870 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001871
1872 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001873 * Returns the number of times a phone call was activated.
1874 *
1875 * {@hide}
1876 */
1877 public abstract int getPhoneOnCount(int which);
1878
1879 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001880 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001881 * the given signal strength.
1882 *
1883 * {@hide}
1884 */
1885 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001886 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001887
Dianne Hackborn617f8772009-03-31 15:04:46 -07001888 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001889 * Returns the time in microseconds that the phone has been trying to
1890 * acquire a signal.
1891 *
1892 * {@hide}
1893 */
1894 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001895 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001896
1897 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001898 * Returns the number of times the phone has entered the given signal strength.
1899 *
1900 * {@hide}
1901 */
1902 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1903
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001904 /**
1905 * Returns the time in microseconds that the mobile network has been active
1906 * (in a high power state).
1907 *
1908 * {@hide}
1909 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001910 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001911
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001912 /**
1913 * Returns the number of times that the mobile network has transitioned to the
1914 * active state.
1915 *
1916 * {@hide}
1917 */
1918 public abstract int getMobileRadioActiveCount(int which);
1919
1920 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001921 * Returns the time in microseconds that is the difference between the mobile radio
1922 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1923 * from the radio.
1924 *
1925 * {@hide}
1926 */
1927 public abstract long getMobileRadioActiveAdjustedTime(int which);
1928
1929 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001930 * Returns the time in microseconds that the mobile network has been active
1931 * (in a high power state) but not being able to blame on an app.
1932 *
1933 * {@hide}
1934 */
1935 public abstract long getMobileRadioActiveUnknownTime(int which);
1936
1937 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001938 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001939 *
1940 * {@hide}
1941 */
1942 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001943
Dianne Hackborn627bba72009-03-24 22:32:56 -07001944 public static final int DATA_CONNECTION_NONE = 0;
1945 public static final int DATA_CONNECTION_GPRS = 1;
1946 public static final int DATA_CONNECTION_EDGE = 2;
1947 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001948 public static final int DATA_CONNECTION_CDMA = 4;
1949 public static final int DATA_CONNECTION_EVDO_0 = 5;
1950 public static final int DATA_CONNECTION_EVDO_A = 6;
1951 public static final int DATA_CONNECTION_1xRTT = 7;
1952 public static final int DATA_CONNECTION_HSDPA = 8;
1953 public static final int DATA_CONNECTION_HSUPA = 9;
1954 public static final int DATA_CONNECTION_HSPA = 10;
1955 public static final int DATA_CONNECTION_IDEN = 11;
1956 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001957 public static final int DATA_CONNECTION_LTE = 13;
1958 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001959 public static final int DATA_CONNECTION_HSPAP = 15;
1960 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001961
Dianne Hackborn627bba72009-03-24 22:32:56 -07001962 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001963 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001964 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001965 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001966 };
1967
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001968 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001969
1970 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001971 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001972 * the given data connection.
1973 *
1974 * {@hide}
1975 */
1976 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001977 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001980 * Returns the number of times the phone has entered the given data
1981 * connection type.
1982 *
1983 * {@hide}
1984 */
1985 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001986
Dianne Hackborn3251b902014-06-20 14:40:53 -07001987 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1988 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1989 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1990 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1991 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1992 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1993 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1994 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1995 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1996 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1997 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1998 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1999 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2000
2001 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2002
2003 static final String[] WIFI_SUPPL_STATE_NAMES = {
2004 "invalid", "disconn", "disabled", "inactive", "scanning",
2005 "authenticating", "associating", "associated", "4-way-handshake",
2006 "group-handshake", "completed", "dormant", "uninit"
2007 };
2008
2009 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2010 "inv", "dsc", "dis", "inact", "scan",
2011 "auth", "ascing", "asced", "4-way",
2012 "group", "compl", "dorm", "uninit"
2013 };
2014
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002015 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2016 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002017 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002018 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2019 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002020 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002021 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2022 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2023 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002024 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002025 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002026 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002027 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002028 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2029 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002030 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2031 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2032 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2033 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2034 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2035 new String[] {"in", "out", "emergency", "off"},
2036 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002037 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2038 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2039 SignalStrength.SIGNAL_STRENGTH_NAMES,
2040 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002041 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2042 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2043 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002044 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002045
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002046 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2047 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002048 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002049 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002050 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002051 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002052 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002053 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2054 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2055 new String[] { "off", "light", "full", "???" },
2056 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002057 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2058 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2059 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002060 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2061 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2062 new String[] { "0", "1", "2", "3", "4" },
2063 new String[] { "0", "1", "2", "3", "4" }),
2064 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2065 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2066 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002067 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002068 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002069 };
2070
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002071 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002072 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002073 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2074 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002075 };
2076
2077 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002078 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002079 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002080 "Esw", "Ewa", "Elw", "Eec"
2081 };
2082
2083 @FunctionalInterface
2084 public interface IntToString {
2085 String applyAsString(int val);
2086 }
2087
2088 private static final IntToString sUidToString = UserHandle::formatUid;
2089 private static final IntToString sIntToString = Integer::toString;
2090
2091 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2092 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2093 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2094 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2095 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002096 };
2097
Dianne Hackborn617f8772009-03-31 15:04:46 -07002098 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002099 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002100 * running on battery.
2101 *
2102 * {@hide}
2103 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002104 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002105
2106 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002107 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002108 * been in the running state while the device was running on battery.
2109 *
2110 * {@hide}
2111 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002112 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002113
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002114 public static final int WIFI_STATE_OFF = 0;
2115 public static final int WIFI_STATE_OFF_SCANNING = 1;
2116 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2117 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2118 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2119 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2120 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2121 public static final int WIFI_STATE_SOFT_AP = 7;
2122
2123 static final String[] WIFI_STATE_NAMES = {
2124 "off", "scanning", "no_net", "disconn",
2125 "sta", "p2p", "sta_p2p", "soft_ap"
2126 };
2127
2128 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2129
2130 /**
2131 * Returns the time in microseconds that WiFi has been running in the given state.
2132 *
2133 * {@hide}
2134 */
2135 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002136 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002137
2138 /**
2139 * Returns the number of times that WiFi has entered the given state.
2140 *
2141 * {@hide}
2142 */
2143 public abstract int getWifiStateCount(int wifiState, int which);
2144
The Android Open Source Project10592532009-03-18 17:39:46 -07002145 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002146 * Returns the time in microseconds that the wifi supplicant has been
2147 * in a given state.
2148 *
2149 * {@hide}
2150 */
2151 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2152
2153 /**
2154 * Returns the number of times that the wifi supplicant has transitioned
2155 * to a given state.
2156 *
2157 * {@hide}
2158 */
2159 public abstract int getWifiSupplStateCount(int state, int which);
2160
2161 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2162
2163 /**
2164 * Returns the time in microseconds that WIFI has been running with
2165 * the given signal strength.
2166 *
2167 * {@hide}
2168 */
2169 public abstract long getWifiSignalStrengthTime(int strengthBin,
2170 long elapsedRealtimeUs, int which);
2171
2172 /**
2173 * Returns the number of times WIFI has entered the given signal strength.
2174 *
2175 * {@hide}
2176 */
2177 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2178
2179 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002180 * Returns the time in microseconds that the flashlight has been on while the device was
2181 * running on battery.
2182 *
2183 * {@hide}
2184 */
2185 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2186
2187 /**
2188 * Returns the number of times that the flashlight has been turned on while the device was
2189 * running on battery.
2190 *
2191 * {@hide}
2192 */
2193 public abstract long getFlashlightOnCount(int which);
2194
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002195 /**
2196 * Returns the time in microseconds that the camera has been on while the device was
2197 * running on battery.
2198 *
2199 * {@hide}
2200 */
2201 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2202
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002203 /**
2204 * Returns the time in microseconds that bluetooth scans were running while the device was
2205 * on battery.
2206 *
2207 * {@hide}
2208 */
2209 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002210
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002211 public static final int NETWORK_MOBILE_RX_DATA = 0;
2212 public static final int NETWORK_MOBILE_TX_DATA = 1;
2213 public static final int NETWORK_WIFI_RX_DATA = 2;
2214 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002215 public static final int NETWORK_BT_RX_DATA = 4;
2216 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002217 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2218 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2219 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2220 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2221 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002222
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002223 public abstract long getNetworkActivityBytes(int type, int which);
2224 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002225
Adam Lesinskie08af192015-03-25 16:42:59 -07002226 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002227 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002228 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002229 * actual power data.
2230 */
2231 public abstract boolean hasWifiActivityReporting();
2232
2233 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002234 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2235 * in various radio controller states, such as transmit, receive, and idle.
2236 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002237 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002238 public abstract ControllerActivityCounter getWifiControllerActivity();
2239
2240 /**
2241 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2242 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2243 * actual power data.
2244 */
2245 public abstract boolean hasBluetoothActivityReporting();
2246
2247 /**
2248 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2249 * in various radio controller states, such as transmit, receive, and idle.
2250 * @return non-null {@link ControllerActivityCounter}
2251 */
2252 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2253
2254 /**
2255 * Returns true if the BatteryStats object has detailed modem power reports.
2256 * When true, calling {@link #getModemControllerActivity()} will yield the
2257 * actual power data.
2258 */
2259 public abstract boolean hasModemActivityReporting();
2260
2261 /**
2262 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2263 * in various radio controller states, such as transmit, receive, and idle.
2264 * @return non-null {@link ControllerActivityCounter}
2265 */
2266 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002267
The Android Open Source Project10592532009-03-18 17:39:46 -07002268 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002269 * Return the wall clock time when battery stats data collection started.
2270 */
2271 public abstract long getStartClockTime();
2272
2273 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002274 * Return platform version tag that we were running in when the battery stats started.
2275 */
2276 public abstract String getStartPlatformVersion();
2277
2278 /**
2279 * Return platform version tag that we were running in when the battery stats ended.
2280 */
2281 public abstract String getEndPlatformVersion();
2282
2283 /**
2284 * Return the internal version code of the parcelled format.
2285 */
2286 public abstract int getParcelVersion();
2287
2288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 * Return whether we are currently running on battery.
2290 */
2291 public abstract boolean getIsOnBattery();
2292
2293 /**
2294 * Returns a SparseArray containing the statistics for each uid.
2295 */
2296 public abstract SparseArray<? extends Uid> getUidStats();
2297
2298 /**
2299 * Returns the current battery uptime in microseconds.
2300 *
2301 * @param curTime the amount of elapsed realtime in microseconds.
2302 */
2303 public abstract long getBatteryUptime(long curTime);
2304
2305 /**
2306 * Returns the current battery realtime in microseconds.
2307 *
2308 * @param curTime the amount of elapsed realtime in microseconds.
2309 */
2310 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002311
2312 /**
Evan Millar633a1742009-04-02 16:36:33 -07002313 * Returns the battery percentage level at the last time the device was unplugged from power, or
2314 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002315 */
Evan Millar633a1742009-04-02 16:36:33 -07002316 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002317
2318 /**
Evan Millar633a1742009-04-02 16:36:33 -07002319 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2320 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002321 */
Evan Millar633a1742009-04-02 16:36:33 -07002322 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323
2324 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002325 * Get the amount the battery has discharged since the stats were
2326 * last reset after charging, as a lower-end approximation.
2327 */
2328 public abstract int getLowDischargeAmountSinceCharge();
2329
2330 /**
2331 * Get the amount the battery has discharged since the stats were
2332 * last reset after charging, as an upper-end approximation.
2333 */
2334 public abstract int getHighDischargeAmountSinceCharge();
2335
2336 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002337 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2338 */
2339 public abstract int getDischargeAmount(int which);
2340
2341 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002342 * Get the amount the battery has discharged while the screen was on,
2343 * since the last time power was unplugged.
2344 */
2345 public abstract int getDischargeAmountScreenOn();
2346
2347 /**
2348 * Get the amount the battery has discharged while the screen was on,
2349 * since the last time the device was charged.
2350 */
2351 public abstract int getDischargeAmountScreenOnSinceCharge();
2352
2353 /**
2354 * Get the amount the battery has discharged while the screen was off,
2355 * since the last time power was unplugged.
2356 */
2357 public abstract int getDischargeAmountScreenOff();
2358
2359 /**
2360 * Get the amount the battery has discharged while the screen was off,
2361 * since the last time the device was charged.
2362 */
2363 public abstract int getDischargeAmountScreenOffSinceCharge();
2364
2365 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 * Returns the total, last, or current battery uptime in microseconds.
2367 *
2368 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002369 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 */
2371 public abstract long computeBatteryUptime(long curTime, int which);
2372
2373 /**
2374 * Returns the total, last, or current battery realtime in microseconds.
2375 *
2376 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002377 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 */
2379 public abstract long computeBatteryRealtime(long curTime, int which);
2380
2381 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002382 * Returns the total, last, or current battery screen off uptime in microseconds.
2383 *
2384 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002385 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002386 */
2387 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2388
2389 /**
2390 * Returns the total, last, or current battery screen off realtime in microseconds.
2391 *
2392 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002393 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002394 */
2395 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2396
2397 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 * Returns the total, last, or current uptime in microseconds.
2399 *
2400 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002401 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 */
2403 public abstract long computeUptime(long curTime, int which);
2404
2405 /**
2406 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002407 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002409 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 */
2411 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002412
2413 /**
2414 * Compute an approximation for how much run time (in microseconds) is remaining on
2415 * the battery. Returns -1 if no time can be computed: either there is not
2416 * enough current data to make a decision, or the battery is currently
2417 * charging.
2418 *
2419 * @param curTime The current elepsed realtime in microseconds.
2420 */
2421 public abstract long computeBatteryTimeRemaining(long curTime);
2422
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002423 // The part of a step duration that is the actual time.
2424 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2425
2426 // Bits in a step duration that are the new battery level we are at.
2427 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002428 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002429
2430 // Bits in a step duration that are the initial mode we were in at that step.
2431 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002432 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002433
2434 // Bits in a step duration that indicate which modes changed during that step.
2435 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002436 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002437
2438 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2439 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2440
Santos Cordone94f0502017-02-24 12:31:20 -08002441 // The largest value for screen state that is tracked in battery states. Any values above
2442 // this should be mapped back to one of the tracked values before being tracked here.
2443 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2444
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002445 // Step duration mode: power save is on.
2446 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2447
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002448 // Step duration mode: device is currently in idle mode.
2449 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2450
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002451 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2452 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002453 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2454 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002455 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2456 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2457 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2458 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2459 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002460 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2461 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002462 };
2463 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2464 (Display.STATE_OFF-1),
2465 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002466 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002467 (Display.STATE_ON-1),
2468 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2469 (Display.STATE_DOZE-1),
2470 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2471 (Display.STATE_DOZE_SUSPEND-1),
2472 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002473 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002474 };
2475 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2476 "screen off",
2477 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002478 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002479 "screen on",
2480 "screen on power save",
2481 "screen doze",
2482 "screen doze power save",
2483 "screen doze-suspend",
2484 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002485 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002486 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002487
2488 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002489 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2490 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2491 * a coulomb counter.
2492 */
2493 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2494
2495 /**
2496 * Return the counter keeping track of the amount of battery discharge measured in
2497 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2498 * a coulomb counter.
2499 */
2500 public abstract LongCounter getDischargeCoulombCounter();
2501
2502 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002503 * Returns the estimated real battery capacity, which may be less than the capacity
2504 * declared by the PowerProfile.
2505 * @return The estimated battery capacity in mAh.
2506 */
2507 public abstract int getEstimatedBatteryCapacity();
2508
2509 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002510 * @return The minimum learned battery capacity in uAh.
2511 */
2512 public abstract int getMinLearnedBatteryCapacity();
2513
2514 /**
2515 * @return The maximum learned battery capacity in uAh.
2516 */
2517 public abstract int getMaxLearnedBatteryCapacity() ;
2518
2519 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002520 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002521 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002522 public abstract LevelStepTracker getDischargeLevelStepTracker();
2523
2524 /**
2525 * Return the array of daily discharge step durations.
2526 */
2527 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002528
2529 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002530 * Compute an approximation for how much time (in microseconds) remains until the battery
2531 * is fully charged. Returns -1 if no time can be computed: either there is not
2532 * enough current data to make a decision, or the battery is currently
2533 * discharging.
2534 *
2535 * @param curTime The current elepsed realtime in microseconds.
2536 */
2537 public abstract long computeChargeTimeRemaining(long curTime);
2538
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002539 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002540 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002541 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002542 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002543
2544 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002545 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002546 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002547 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002548
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002549 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2550
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002551 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002552
Evan Millarc64edde2009-04-18 12:26:32 -07002553 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554
James Carr2dd7e5e2016-07-20 18:48:39 -07002555 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2556
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002557 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2558
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002559 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 long days = seconds / (60 * 60 * 24);
2561 if (days != 0) {
2562 out.append(days);
2563 out.append("d ");
2564 }
2565 long used = days * 60 * 60 * 24;
2566
2567 long hours = (seconds - used) / (60 * 60);
2568 if (hours != 0 || used != 0) {
2569 out.append(hours);
2570 out.append("h ");
2571 }
2572 used += hours * 60 * 60;
2573
2574 long mins = (seconds-used) / 60;
2575 if (mins != 0 || used != 0) {
2576 out.append(mins);
2577 out.append("m ");
2578 }
2579 used += mins * 60;
2580
2581 if (seconds != 0 || used != 0) {
2582 out.append(seconds-used);
2583 out.append("s ");
2584 }
2585 }
2586
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002587 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002589 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 sb.append(time - (sec * 1000));
2591 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 }
2593
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002594 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002595 long sec = time / 1000;
2596 formatTimeRaw(sb, sec);
2597 sb.append(time - (sec * 1000));
2598 sb.append("ms");
2599 }
2600
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002601 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002603 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 }
2605 float perc = ((float)num) / ((float)den) * 100;
2606 mFormatBuilder.setLength(0);
2607 mFormatter.format("%.1f%%", perc);
2608 return mFormatBuilder.toString();
2609 }
2610
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002611 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002612 mFormatBuilder.setLength(0);
2613
2614 if (bytes < BYTES_PER_KB) {
2615 return bytes + "B";
2616 } else if (bytes < BYTES_PER_MB) {
2617 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2618 return mFormatBuilder.toString();
2619 } else if (bytes < BYTES_PER_GB){
2620 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2621 return mFormatBuilder.toString();
2622 } else {
2623 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2624 return mFormatBuilder.toString();
2625 }
2626 }
2627
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002628 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002629 if (timer != null) {
2630 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002631 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002632 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2633 return totalTimeMillis;
2634 }
2635 return 0;
2636 }
2637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 /**
2639 *
2640 * @param sb a StringBuilder object.
2641 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002642 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002644 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 * @param linePrefix a String to be prepended to each line of output.
2646 * @return the line prefix
2647 */
2648 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002649 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002652 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002653
Evan Millarc64edde2009-04-18 12:26:32 -07002654 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 if (totalTimeMillis != 0) {
2656 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002657 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002658 if (name != null) {
2659 sb.append(name);
2660 sb.append(' ');
2661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 sb.append('(');
2663 sb.append(count);
2664 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002665 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2666 if (maxDurationMs >= 0) {
2667 sb.append(" max=");
2668 sb.append(maxDurationMs);
2669 }
Bookatz506a8182017-05-01 14:18:42 -07002670 // Put actual time if it is available and different from totalTimeMillis.
2671 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2672 if (totalDurMs > totalTimeMillis) {
2673 sb.append(" actual=");
2674 sb.append(totalDurMs);
2675 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002676 if (timer.isRunningLocked()) {
2677 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2678 if (currentMs >= 0) {
2679 sb.append(" (running for ");
2680 sb.append(currentMs);
2681 sb.append("ms)");
2682 } else {
2683 sb.append(" (running)");
2684 }
2685 }
2686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 return ", ";
2688 }
2689 }
2690 return linePrefix;
2691 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002692
2693 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002694 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002695 *
2696 * @param pw a PrintWriter object to print to.
2697 * @param sb a StringBuilder object.
2698 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002699 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002700 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2701 * @param prefix a String to be prepended to each line of output.
2702 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002703 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002704 */
2705 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002706 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002707 if (timer != null) {
2708 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002709 final long totalTimeMs = (timer.getTotalTimeLocked(
2710 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002711 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002712 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002713 sb.setLength(0);
2714 sb.append(prefix);
2715 sb.append(" ");
2716 sb.append(type);
2717 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002718 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002719 sb.append("realtime (");
2720 sb.append(count);
2721 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002722 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2723 if (maxDurationMs >= 0) {
2724 sb.append(" max=");
2725 sb.append(maxDurationMs);
2726 }
2727 if (timer.isRunningLocked()) {
2728 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2729 if (currentMs >= 0) {
2730 sb.append(" (running for ");
2731 sb.append(currentMs);
2732 sb.append("ms)");
2733 } else {
2734 sb.append(" (running)");
2735 }
2736 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002737 pw.println(sb.toString());
2738 return true;
2739 }
2740 }
2741 return false;
2742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743
2744 /**
2745 * Checkin version of wakelock printer. Prints simple comma-separated list.
2746 *
2747 * @param sb a StringBuilder object.
2748 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002749 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002751 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 * @param linePrefix a String to be prepended to each line of output.
2753 * @return the line prefix
2754 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002755 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2756 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 long totalTimeMicros = 0;
2758 int count = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002759 long max = -1;
2760 long current = -1;
Bookatz506a8182017-05-01 14:18:42 -07002761 long totalDuration = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002763 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07002764 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002765 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2766 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07002767 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 }
2769 sb.append(linePrefix);
2770 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2771 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002772 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002774 sb.append(',');
2775 sb.append(current);
2776 sb.append(',');
2777 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07002778 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
2779 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
2780 // totalDuration independent of totalTimeMicros (since they are not pooled).
2781 if (name != null) {
2782 sb.append(',');
2783 sb.append(totalDuration);
2784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 return ",";
2786 }
Bookatz506a8182017-05-01 14:18:42 -07002787
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002788 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2789 String type) {
2790 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2791 pw.print(',');
2792 pw.print(uid);
2793 pw.print(',');
2794 pw.print(category);
2795 pw.print(',');
2796 pw.print(type);
2797 }
2798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 /**
2800 * Dump a comma-separated line of values for terse checkin mode.
2801 *
2802 * @param pw the PageWriter to dump log to
2803 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2804 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2805 * @param args type-dependent data arguments
2806 */
2807 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2808 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002809 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002810 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002811 pw.print(',');
2812 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002814 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002816
2817 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002818 * Dump a given timer stat for terse checkin mode.
2819 *
2820 * @param pw the PageWriter to dump log to
2821 * @param uid the UID to log
2822 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2823 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2824 * @param timer a {@link Timer} to dump stats for
2825 * @param rawRealtime the current elapsed realtime of the system in microseconds
2826 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2827 */
2828 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2829 Timer timer, long rawRealtime, int which) {
2830 if (timer != null) {
2831 // Convert from microseconds to milliseconds with rounding
2832 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2833 / 1000;
2834 final int count = timer.getCountLocked(which);
2835 if (totalTime != 0) {
2836 dumpLine(pw, uid, category, type, totalTime, count);
2837 }
2838 }
2839 }
2840
2841 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002842 * Checks if the ControllerActivityCounter has any data worth dumping.
2843 */
2844 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2845 if (counter == null) {
2846 return false;
2847 }
2848
2849 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2850 || counter.getRxTimeCounter().getCountLocked(which) != 0
2851 || counter.getPowerCounter().getCountLocked(which) != 0) {
2852 return true;
2853 }
2854
2855 for (LongCounter c : counter.getTxTimeCounters()) {
2856 if (c.getCountLocked(which) != 0) {
2857 return true;
2858 }
2859 }
2860 return false;
2861 }
2862
2863 /**
2864 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2865 * The order of the arguments in the final check in line is:
2866 *
2867 * idle, rx, power, tx...
2868 *
2869 * where tx... is one or more transmit level times.
2870 */
2871 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2872 String type,
2873 ControllerActivityCounter counter,
2874 int which) {
2875 if (!controllerActivityHasData(counter, which)) {
2876 return;
2877 }
2878
2879 dumpLineHeader(pw, uid, category, type);
2880 pw.print(",");
2881 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2882 pw.print(",");
2883 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2884 pw.print(",");
2885 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2886 for (LongCounter c : counter.getTxTimeCounters()) {
2887 pw.print(",");
2888 pw.print(c.getCountLocked(which));
2889 }
2890 pw.println();
2891 }
2892
2893 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2894 String prefix, String controllerName,
2895 ControllerActivityCounter counter,
2896 int which) {
2897 if (controllerActivityHasData(counter, which)) {
2898 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2899 }
2900 }
2901
2902 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2903 String controllerName,
2904 ControllerActivityCounter counter, int which) {
2905 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2906 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2907 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2908 long totalTxTimeMs = 0;
2909 for (LongCounter txState : counter.getTxTimeCounters()) {
2910 totalTxTimeMs += txState.getCountLocked(which);
2911 }
2912
2913 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2914
2915 sb.setLength(0);
2916 sb.append(prefix);
2917 sb.append(" ");
2918 sb.append(controllerName);
2919 sb.append(" Idle time: ");
2920 formatTimeMs(sb, idleTimeMs);
2921 sb.append("(");
2922 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2923 sb.append(")");
2924 pw.println(sb.toString());
2925
2926 sb.setLength(0);
2927 sb.append(prefix);
2928 sb.append(" ");
2929 sb.append(controllerName);
2930 sb.append(" Rx time: ");
2931 formatTimeMs(sb, rxTimeMs);
2932 sb.append("(");
2933 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2934 sb.append(")");
2935 pw.println(sb.toString());
2936
2937 sb.setLength(0);
2938 sb.append(prefix);
2939 sb.append(" ");
2940 sb.append(controllerName);
2941 sb.append(" Tx time: ");
2942 formatTimeMs(sb, totalTxTimeMs);
2943 sb.append("(");
2944 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2945 sb.append(")");
2946 pw.println(sb.toString());
2947
2948 final int numTxLvls = counter.getTxTimeCounters().length;
2949 if (numTxLvls > 1) {
2950 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2951 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2952 sb.setLength(0);
2953 sb.append(prefix);
2954 sb.append(" [");
2955 sb.append(lvl);
2956 sb.append("] ");
2957 formatTimeMs(sb, txLvlTimeMs);
2958 sb.append("(");
2959 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2960 sb.append(")");
2961 pw.println(sb.toString());
2962 }
2963 }
2964
2965 sb.setLength(0);
2966 sb.append(prefix);
2967 sb.append(" ");
2968 sb.append(controllerName);
2969 sb.append(" Power drain: ").append(
2970 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2971 sb.append("mAh");
2972 pw.println(sb.toString());
2973 }
2974
2975 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002976 * Temporary for settings.
2977 */
2978 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2979 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2980 }
2981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 /**
2983 * Checkin server version of dump to produce more compact, computer-readable log.
2984 *
2985 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002987 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2988 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2990 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2991 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2993 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002994 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2995 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2996 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 final long totalRealtime = computeRealtime(rawRealtime, which);
2998 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002999 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003000 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003001 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003002 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3003 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003004 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003005 rawRealtime, which);
3006 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3007 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003008 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003009 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003010 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003011 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003012 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
3013 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
3014 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003015
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003016 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003018 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003019 final int NU = uidStats.size();
3020
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003021 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003024 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003025 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003026 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003027 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003028 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003029 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003030 getEstimatedBatteryCapacity(),
3031 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07003032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003034 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003035 long fullWakeLockTimeTotal = 0;
3036 long partialWakeLockTimeTotal = 0;
3037
3038 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003039 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003040
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003041 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3042 = u.getWakelockStats();
3043 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3044 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003045
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003046 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3047 if (fullWakeTimer != null) {
3048 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3049 which);
3050 }
3051
3052 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3053 if (partialWakeTimer != null) {
3054 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3055 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003056 }
3057 }
3058 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003059
3060 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003061 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3062 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3063 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3064 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3065 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3066 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3067 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3068 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003069 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3070 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003071 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3072 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003073 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3074 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003075
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003076 // Dump Modem controller stats
3077 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3078 getModemControllerActivity(), which);
3079
Adam Lesinskie283d332015-04-16 12:29:25 -07003080 // Dump Wifi controller stats
3081 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3082 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003083 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003084 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003085
3086 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3087 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003088
3089 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003090 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3091 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 // Dump misc stats
3094 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003095 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003096 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003097 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003098 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003099 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003100 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3101 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003102 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003103 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3104 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3105 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3106 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003107 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003108
3109 // Dump screen brightness stats
3110 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3111 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003112 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003113 }
3114 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07003115
Dianne Hackborn627bba72009-03-24 22:32:56 -07003116 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003117 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3118 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003119 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003120 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003121 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003122 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003123 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003124 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003125 args[i] = getPhoneSignalStrengthCount(i, which);
3126 }
3127 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003128
Dianne Hackborn627bba72009-03-24 22:32:56 -07003129 // Dump network type stats
3130 args = new Object[NUM_DATA_CONNECTION_TYPES];
3131 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003132 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003133 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003134 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3135 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3136 args[i] = getPhoneDataConnectionCount(i, which);
3137 }
3138 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003139
3140 // Dump wifi state stats
3141 args = new Object[NUM_WIFI_STATES];
3142 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003143 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003144 }
3145 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3146 for (int i=0; i<NUM_WIFI_STATES; i++) {
3147 args[i] = getWifiStateCount(i, which);
3148 }
3149 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3150
Dianne Hackborn3251b902014-06-20 14:40:53 -07003151 // Dump wifi suppl state stats
3152 args = new Object[NUM_WIFI_SUPPL_STATES];
3153 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3154 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3155 }
3156 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3157 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3158 args[i] = getWifiSupplStateCount(i, which);
3159 }
3160 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3161
3162 // Dump wifi signal strength stats
3163 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3164 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3165 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3166 }
3167 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3168 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3169 args[i] = getWifiSignalStrengthCount(i, which);
3170 }
3171 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3172
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003173 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003174 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003175 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003178 if (which == STATS_SINCE_UNPLUGGED) {
3179 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3180 getDischargeStartLevel()-getDischargeCurrentLevel(),
3181 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003182 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3183 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003184 } else {
3185 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3186 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003187 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003188 getDischargeAmountScreenOffSinceCharge(),
3189 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003190 }
3191
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003192 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003193 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003194 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003195 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003196 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003197 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003198 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3199 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003200 }
Evan Millarc64edde2009-04-18 12:26:32 -07003201 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003202 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003203 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003204 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3205 // Not doing the regular wake lock formatting to remain compatible
3206 // with the old checkin format.
3207 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3208 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003209 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003210 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003211 }
3212 }
Evan Millarc64edde2009-04-18 12:26:32 -07003213 }
3214
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003215 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003216 helper.create(this);
3217 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003218 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003219 if (sippers != null && sippers.size() > 0) {
3220 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3221 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003222 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003223 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3224 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3225 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003226 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003227 int uid = 0;
3228 String label;
3229 switch (bs.drainType) {
3230 case IDLE:
3231 label="idle";
3232 break;
3233 case CELL:
3234 label="cell";
3235 break;
3236 case PHONE:
3237 label="phone";
3238 break;
3239 case WIFI:
3240 label="wifi";
3241 break;
3242 case BLUETOOTH:
3243 label="blue";
3244 break;
3245 case SCREEN:
3246 label="scrn";
3247 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003248 case FLASHLIGHT:
3249 label="flashlight";
3250 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003251 case APP:
3252 uid = bs.uidObj.getUid();
3253 label = "uid";
3254 break;
3255 case USER:
3256 uid = UserHandle.getUid(bs.userId, 0);
3257 label = "user";
3258 break;
3259 case UNACCOUNTED:
3260 label = "unacc";
3261 break;
3262 case OVERCOUNTED:
3263 label = "over";
3264 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003265 case CAMERA:
3266 label = "camera";
3267 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003268 default:
3269 label = "???";
3270 }
3271 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003272 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003273 }
3274 }
3275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 for (int iu = 0; iu < NU; iu++) {
3277 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003278 if (reqUid >= 0 && uid != reqUid) {
3279 continue;
3280 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003281 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003284 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3285 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3286 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3287 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3288 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3289 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3290 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3291 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003292 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003293 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3294 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003295 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003296 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3297 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003298 // Background data transfers
3299 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3300 which);
3301 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3302 which);
3303 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3304 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3305 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3306 which);
3307 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3308 which);
3309 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3310 which);
3311 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3312 which);
3313
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003314 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3315 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003316 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003317 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3318 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3319 || wifiBytesBgTx > 0
3320 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3321 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003322 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3323 wifiBytesRx, wifiBytesTx,
3324 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003325 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003326 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003327 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3328 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3329 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3330 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003331 }
3332
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003333 // Dump modem controller data, per UID.
3334 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3335 u.getModemControllerActivity(), which);
3336
3337 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003338 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3339 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3340 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003341 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3342 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003343 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3344 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3345 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003346 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003347 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003348 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3349 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003350 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3351 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003352 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003353 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003356 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3357 u.getWifiControllerActivity(), which);
3358
Bookatz867c0d72017-03-07 18:23:42 -08003359 final Timer bleTimer = u.getBluetoothScanTimer();
3360 if (bleTimer != null) {
3361 // Convert from microseconds to milliseconds with rounding
3362 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3363 / 1000;
3364 if (totalTime != 0) {
3365 final int count = bleTimer.getCountLocked(which);
3366 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3367 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
3368 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3369 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3370 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3371 final long actualTimeBg = bleTimerBg != null ?
3372 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3373 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
3374 countBg, actualTime, actualTimeBg);
3375 }
3376 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003377
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003378 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3379 u.getBluetoothControllerActivity(), which);
3380
Dianne Hackborn617f8772009-03-31 15:04:46 -07003381 if (u.hasUserActivity()) {
3382 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3383 boolean hasData = false;
3384 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3385 int val = u.getUserActivityCount(i, which);
3386 args[i] = val;
3387 if (val != 0) hasData = true;
3388 }
3389 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003390 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003391 }
3392 }
3393
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003394 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3395 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3396 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3397 String linePrefix = "";
3398 sb.setLength(0);
3399 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3400 rawRealtime, "f", which, linePrefix);
3401 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3402 rawRealtime, "p", which, linePrefix);
3403 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3404 rawRealtime, "w", which, linePrefix);
3405
3406 // Only log if we had at lease one wakelock...
3407 if (sb.length() > 0) {
3408 String name = wakelocks.keyAt(iw);
3409 if (name.indexOf(',') >= 0) {
3410 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003412 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 }
3414 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003415
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003416 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3417 for (int isy=syncs.size()-1; isy>=0; isy--) {
3418 final Timer timer = syncs.valueAt(isy);
3419 // Convert from microseconds to milliseconds with rounding
3420 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3421 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003422 final Timer bgTimer = timer.getSubTimer();
3423 final long bgTime = bgTimer != null ?
3424 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
3425 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003426 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003427 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003428 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003429 }
3430 }
3431
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003432 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3433 for (int ij=jobs.size()-1; ij>=0; ij--) {
3434 final Timer timer = jobs.valueAt(ij);
3435 // Convert from microseconds to milliseconds with rounding
3436 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3437 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003438 final Timer bgTimer = timer.getSubTimer();
3439 final long bgTime = bgTimer != null ?
3440 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
3441 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003442 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003443 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003444 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003445 }
3446 }
3447
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003448 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3449 rawRealtime, which);
3450 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3451 rawRealtime, which);
3452 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3453 rawRealtime, which);
3454 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3455 rawRealtime, which);
3456
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003457 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3458 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003459 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003460 final Uid.Sensor se = sensors.valueAt(ise);
3461 final int sensorNumber = sensors.keyAt(ise);
3462 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003463 if (timer != null) {
3464 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003465 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3466 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003467 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003468 final int count = timer.getCountLocked(which);
3469 final Timer bgTimer = se.getSensorBackgroundTime();
3470 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
3471 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3472 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3473 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3474 final long bgActualTime = bgTimer != null ?
3475 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3476 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3477 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 }
3480 }
3481
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003482 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3483 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003484
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003485 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3486 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003487
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003488 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003489 long totalStateTime = 0;
3490 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003491 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3492 totalStateTime += time;
3493 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003494 }
3495 if (totalStateTime > 0) {
3496 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3497 }
3498
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003499 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3500 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003501 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003502 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003503 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003504 }
3505
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003506 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3507 = u.getProcessStats();
3508 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3509 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003510
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003511 final long userMillis = ps.getUserTime(which);
3512 final long systemMillis = ps.getSystemTime(which);
3513 final long foregroundMillis = ps.getForegroundTime(which);
3514 final int starts = ps.getStarts(which);
3515 final int numCrashes = ps.getNumCrashes(which);
3516 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003517
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003518 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3519 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003520 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3521 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 }
3523 }
3524
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003525 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3526 = u.getPackageStats();
3527 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3528 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3529 int wakeups = 0;
3530 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3531 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003532 int count = alarms.valueAt(iwa).getCountLocked(which);
3533 wakeups += count;
3534 String name = alarms.keyAt(iwa).replace(',', '_');
3535 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003536 }
3537 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3538 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3539 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3540 final long startTime = ss.getStartTime(batteryUptime, which);
3541 final int starts = ss.getStarts(which);
3542 final int launches = ss.getLaunches(which);
3543 if (startTime != 0 || starts != 0 || launches != 0) {
3544 dumpLine(pw, uid, category, APK_DATA,
3545 wakeups, // wakeup alarms
3546 packageStats.keyAt(ipkg), // Apk
3547 serviceStats.keyAt(isvc), // service
3548 startTime / 1000, // time spent started, in ms
3549 starts,
3550 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 }
3552 }
3553 }
3554 }
3555 }
3556
Dianne Hackborn81038902012-11-26 17:04:09 -08003557 static final class TimerEntry {
3558 final String mName;
3559 final int mId;
3560 final BatteryStats.Timer mTimer;
3561 final long mTime;
3562 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3563 mName = name;
3564 mId = id;
3565 mTimer = timer;
3566 mTime = time;
3567 }
3568 }
3569
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003570 private void printmAh(PrintWriter printer, double power) {
3571 printer.print(BatteryStatsHelper.makemAh(power));
3572 }
3573
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003574 private void printmAh(StringBuilder sb, double power) {
3575 sb.append(BatteryStatsHelper.makemAh(power));
3576 }
3577
Dianne Hackbornd953c532014-08-16 18:17:38 -07003578 /**
3579 * Temporary for settings.
3580 */
3581 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3582 int reqUid) {
3583 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3584 }
3585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003587 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003588 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3590 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3591 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592
3593 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3594 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3595 final long totalRealtime = computeRealtime(rawRealtime, which);
3596 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003597 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3598 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3599 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003600 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3601 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003602
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003603 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003604
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003605 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003606 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003608 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3609 if (estimatedBatteryCapacity > 0) {
3610 sb.setLength(0);
3611 sb.append(prefix);
3612 sb.append(" Estimated battery capacity: ");
3613 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3614 sb.append(" mAh");
3615 pw.println(sb.toString());
3616 }
3617
Jocelyn Dangc627d102017-04-14 13:15:14 -07003618 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
3619 if (minLearnedBatteryCapacity > 0) {
3620 sb.setLength(0);
3621 sb.append(prefix);
3622 sb.append(" Min learned battery capacity: ");
3623 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
3624 sb.append(" mAh");
3625 pw.println(sb.toString());
3626 }
3627 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
3628 if (maxLearnedBatteryCapacity > 0) {
3629 sb.setLength(0);
3630 sb.append(prefix);
3631 sb.append(" Max learned battery capacity: ");
3632 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
3633 sb.append(" mAh");
3634 pw.println(sb.toString());
3635 }
3636
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003637 sb.setLength(0);
3638 sb.append(prefix);
3639 sb.append(" Time on battery: ");
3640 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3641 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3642 sb.append(") realtime, ");
3643 formatTimeMs(sb, whichBatteryUptime / 1000);
3644 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3645 sb.append(") uptime");
3646 pw.println(sb.toString());
3647 sb.setLength(0);
3648 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003649 sb.append(" Time on battery screen off: ");
3650 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3651 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3652 sb.append(") realtime, ");
3653 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3654 sb.append("(");
3655 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3656 sb.append(") uptime");
3657 pw.println(sb.toString());
3658 sb.setLength(0);
3659 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003660 sb.append(" Total run time: ");
3661 formatTimeMs(sb, totalRealtime / 1000);
3662 sb.append("realtime, ");
3663 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003664 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003665 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003666 if (batteryTimeRemaining >= 0) {
3667 sb.setLength(0);
3668 sb.append(prefix);
3669 sb.append(" Battery time remaining: ");
3670 formatTimeMs(sb, batteryTimeRemaining / 1000);
3671 pw.println(sb.toString());
3672 }
3673 if (chargeTimeRemaining >= 0) {
3674 sb.setLength(0);
3675 sb.append(prefix);
3676 sb.append(" Charge time remaining: ");
3677 formatTimeMs(sb, chargeTimeRemaining / 1000);
3678 pw.println(sb.toString());
3679 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003680
3681 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3682 final long dischargeCount = dischargeCounter.getCountLocked(which);
3683 if (dischargeCount >= 0) {
3684 sb.setLength(0);
3685 sb.append(prefix);
3686 sb.append(" Discharge: ");
3687 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3688 sb.append(" mAh");
3689 pw.println(sb.toString());
3690 }
3691
3692 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3693 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3694 if (dischargeScreenOffCount >= 0) {
3695 sb.setLength(0);
3696 sb.append(prefix);
3697 sb.append(" Screen off discharge: ");
3698 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3699 sb.append(" mAh");
3700 pw.println(sb.toString());
3701 }
3702
3703 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3704 if (dischargeScreenOnCount >= 0) {
3705 sb.setLength(0);
3706 sb.append(prefix);
3707 sb.append(" Screen on discharge: ");
3708 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3709 sb.append(" mAh");
3710 pw.println(sb.toString());
3711 }
3712
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003713 pw.print(" Start clock time: ");
3714 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3715
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003716 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003717 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003718 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003719 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3720 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003721 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003722 rawRealtime, which);
3723 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3724 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003725 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003726 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003727 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3728 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3729 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003730 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003731 sb.append(prefix);
3732 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3733 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003734 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003735 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3736 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003737 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003738 pw.println(sb.toString());
3739 sb.setLength(0);
3740 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003741 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003742 boolean didOne = false;
3743 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003744 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003745 if (time == 0) {
3746 continue;
3747 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003748 sb.append("\n ");
3749 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003750 didOne = true;
3751 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3752 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003753 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003754 sb.append("(");
3755 sb.append(formatRatioLocked(time, screenOnTime));
3756 sb.append(")");
3757 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003758 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003759 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003760 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003761 sb.setLength(0);
3762 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003763 sb.append(" Power save mode enabled: ");
3764 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003765 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003766 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003767 sb.append(")");
3768 pw.println(sb.toString());
3769 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003770 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003771 sb.setLength(0);
3772 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003773 sb.append(" Device light idling: ");
3774 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003775 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003776 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3777 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003778 sb.append("x");
3779 pw.println(sb.toString());
3780 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003781 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003782 sb.setLength(0);
3783 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003784 sb.append(" Idle mode light time: ");
3785 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003786 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003787 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3788 sb.append(") ");
3789 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003790 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003791 sb.append(" -- longest ");
3792 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3793 pw.println(sb.toString());
3794 }
3795 if (deviceIdlingTime != 0) {
3796 sb.setLength(0);
3797 sb.append(prefix);
3798 sb.append(" Device full idling: ");
3799 formatTimeMs(sb, deviceIdlingTime / 1000);
3800 sb.append("(");
3801 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003802 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003803 sb.append("x");
3804 pw.println(sb.toString());
3805 }
3806 if (deviceIdleModeFullTime != 0) {
3807 sb.setLength(0);
3808 sb.append(prefix);
3809 sb.append(" Idle mode full time: ");
3810 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3811 sb.append("(");
3812 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3813 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003814 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003815 sb.append("x");
3816 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003817 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003818 pw.println(sb.toString());
3819 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003820 if (phoneOnTime != 0) {
3821 sb.setLength(0);
3822 sb.append(prefix);
3823 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3824 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003825 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003826 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003827 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003828 if (connChanges != 0) {
3829 pw.print(prefix);
3830 pw.print(" Connectivity changes: "); pw.println(connChanges);
3831 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003832
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003833 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003834 long fullWakeLockTimeTotalMicros = 0;
3835 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003836
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003837 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003838
Evan Millar22ac0432009-03-31 11:33:18 -07003839 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003840 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003841
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003842 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3843 = u.getWakelockStats();
3844 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3845 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003846
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003847 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3848 if (fullWakeTimer != null) {
3849 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3850 rawRealtime, which);
3851 }
3852
3853 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3854 if (partialWakeTimer != null) {
3855 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3856 rawRealtime, which);
3857 if (totalTimeMicros > 0) {
3858 if (reqUid < 0) {
3859 // Only show the ordered list of all wake
3860 // locks if the caller is not asking for data
3861 // about a specific uid.
3862 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3863 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003864 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003865 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003866 }
3867 }
3868 }
3869 }
3870
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003871 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3872 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3873 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3874 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3875 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3876 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3877 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3878 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003879 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3880 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003881
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003882 if (fullWakeLockTimeTotalMicros != 0) {
3883 sb.setLength(0);
3884 sb.append(prefix);
3885 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3886 (fullWakeLockTimeTotalMicros + 500) / 1000);
3887 pw.println(sb.toString());
3888 }
3889
3890 if (partialWakeLockTimeTotalMicros != 0) {
3891 sb.setLength(0);
3892 sb.append(prefix);
3893 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3894 (partialWakeLockTimeTotalMicros + 500) / 1000);
3895 pw.println(sb.toString());
3896 }
3897
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003898 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003899 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3900 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3901 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3902 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003903 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003904 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003905 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003906 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003907 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003908 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003909 if (time == 0) {
3910 continue;
3911 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003912 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003913 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003914 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003915 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003916 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003917 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003918 sb.append("(");
3919 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003920 sb.append(") ");
3921 sb.append(getPhoneSignalStrengthCount(i, which));
3922 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003923 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003924 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003925 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003926
3927 sb.setLength(0);
3928 sb.append(prefix);
3929 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003930 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003931 pw.println(sb.toString());
3932
Dianne Hackborn627bba72009-03-24 22:32:56 -07003933 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003934 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003935 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003936 didOne = false;
3937 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003938 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003939 if (time == 0) {
3940 continue;
3941 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003942 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003943 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003944 didOne = true;
3945 sb.append(DATA_CONNECTION_NAMES[i]);
3946 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003947 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003948 sb.append("(");
3949 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003950 sb.append(") ");
3951 sb.append(getPhoneDataConnectionCount(i, which));
3952 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003953 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003954 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003955 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003956
3957 sb.setLength(0);
3958 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003959 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003960 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003961 formatTimeMs(sb, mobileActiveTime / 1000);
3962 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3963 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3964 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003965 pw.println(sb.toString());
3966
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003967 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3968 if (mobileActiveUnknownTime != 0) {
3969 sb.setLength(0);
3970 sb.append(prefix);
3971 sb.append(" Mobile radio active unknown time: ");
3972 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3973 sb.append("(");
3974 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3975 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3976 sb.append("x");
3977 pw.println(sb.toString());
3978 }
3979
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003980 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3981 if (mobileActiveAdjustedTime != 0) {
3982 sb.setLength(0);
3983 sb.append(prefix);
3984 sb.append(" Mobile radio active adjusted time: ");
3985 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3986 sb.append("(");
3987 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3988 sb.append(")");
3989 pw.println(sb.toString());
3990 }
3991
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003992 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3993
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003994 pw.print(prefix);
3995 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3996 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3997 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3998 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003999 sb.setLength(0);
4000 sb.append(prefix);
4001 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
4002 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
4003 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
4004 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004005 sb.append(")");
4006 pw.println(sb.toString());
4007
4008 sb.setLength(0);
4009 sb.append(prefix);
4010 sb.append(" Wifi states:");
4011 didOne = false;
4012 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004013 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004014 if (time == 0) {
4015 continue;
4016 }
4017 sb.append("\n ");
4018 didOne = true;
4019 sb.append(WIFI_STATE_NAMES[i]);
4020 sb.append(" ");
4021 formatTimeMs(sb, time/1000);
4022 sb.append("(");
4023 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4024 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004025 sb.append(getWifiStateCount(i, which));
4026 sb.append("x");
4027 }
4028 if (!didOne) sb.append(" (no activity)");
4029 pw.println(sb.toString());
4030
4031 sb.setLength(0);
4032 sb.append(prefix);
4033 sb.append(" Wifi supplicant states:");
4034 didOne = false;
4035 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4036 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4037 if (time == 0) {
4038 continue;
4039 }
4040 sb.append("\n ");
4041 didOne = true;
4042 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4043 sb.append(" ");
4044 formatTimeMs(sb, time/1000);
4045 sb.append("(");
4046 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4047 sb.append(") ");
4048 sb.append(getWifiSupplStateCount(i, which));
4049 sb.append("x");
4050 }
4051 if (!didOne) sb.append(" (no activity)");
4052 pw.println(sb.toString());
4053
4054 sb.setLength(0);
4055 sb.append(prefix);
4056 sb.append(" Wifi signal levels:");
4057 didOne = false;
4058 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4059 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4060 if (time == 0) {
4061 continue;
4062 }
4063 sb.append("\n ");
4064 sb.append(prefix);
4065 didOne = true;
4066 sb.append("level(");
4067 sb.append(i);
4068 sb.append(") ");
4069 formatTimeMs(sb, time/1000);
4070 sb.append("(");
4071 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4072 sb.append(") ");
4073 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004074 sb.append("x");
4075 }
4076 if (!didOne) sb.append(" (no activity)");
4077 pw.println(sb.toString());
4078
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004079 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004080
Adam Lesinski50e47602015-12-04 17:04:54 -08004081 pw.print(prefix);
4082 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4083 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4084
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004085 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4086 sb.setLength(0);
4087 sb.append(prefix);
4088 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4089 pw.println(sb.toString());
4090
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004091 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4092 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004093
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004094 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004095
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004096 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004097 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004098 pw.print(prefix); pw.println(" Device is currently unplugged");
4099 pw.print(prefix); pw.print(" Discharge cycle start level: ");
4100 pw.println(getDischargeStartLevel());
4101 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4102 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004103 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004104 pw.print(prefix); pw.println(" Device is currently plugged into power");
4105 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
4106 pw.println(getDischargeStartLevel());
4107 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
4108 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004109 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004110 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4111 pw.println(getDischargeAmountScreenOn());
4112 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4113 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004114 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004115 } else {
4116 pw.print(prefix); pw.println(" Device battery use since last full charge");
4117 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4118 pw.println(getLowDischargeAmountSinceCharge());
4119 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4120 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004121 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4122 pw.println(getDischargeAmountScreenOnSinceCharge());
4123 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4124 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004125 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004126 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004127
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004128 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004129 helper.create(this);
4130 helper.refreshStats(which, UserHandle.USER_ALL);
4131 List<BatterySipper> sippers = helper.getUsageList();
4132 if (sippers != null && sippers.size() > 0) {
4133 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4134 pw.print(prefix); pw.print(" Capacity: ");
4135 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004136 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004137 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4138 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4139 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4140 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004141 pw.println();
4142 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004143 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004144 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004145 switch (bs.drainType) {
4146 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004147 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004148 break;
4149 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004150 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004151 break;
4152 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004153 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004154 break;
4155 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004156 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004157 break;
4158 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004159 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004160 break;
4161 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004162 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004163 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004164 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004165 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004166 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004167 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004168 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004169 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004170 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004171 break;
4172 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004173 pw.print(" User "); pw.print(bs.userId);
4174 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004175 break;
4176 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004177 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004178 break;
4179 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004180 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004181 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004182 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004183 pw.print(" Camera: ");
4184 break;
4185 default:
4186 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004187 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004188 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004189 printmAh(pw, bs.totalPowerMah);
4190
Adam Lesinski57123002015-06-12 16:12:07 -07004191 if (bs.usagePowerMah != bs.totalPowerMah) {
4192 // If the usage (generic power) isn't the whole amount, we list out
4193 // what components are involved in the calculation.
4194
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004195 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004196 if (bs.usagePowerMah != 0) {
4197 pw.print(" usage=");
4198 printmAh(pw, bs.usagePowerMah);
4199 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004200 if (bs.cpuPowerMah != 0) {
4201 pw.print(" cpu=");
4202 printmAh(pw, bs.cpuPowerMah);
4203 }
4204 if (bs.wakeLockPowerMah != 0) {
4205 pw.print(" wake=");
4206 printmAh(pw, bs.wakeLockPowerMah);
4207 }
4208 if (bs.mobileRadioPowerMah != 0) {
4209 pw.print(" radio=");
4210 printmAh(pw, bs.mobileRadioPowerMah);
4211 }
4212 if (bs.wifiPowerMah != 0) {
4213 pw.print(" wifi=");
4214 printmAh(pw, bs.wifiPowerMah);
4215 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004216 if (bs.bluetoothPowerMah != 0) {
4217 pw.print(" bt=");
4218 printmAh(pw, bs.bluetoothPowerMah);
4219 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004220 if (bs.gpsPowerMah != 0) {
4221 pw.print(" gps=");
4222 printmAh(pw, bs.gpsPowerMah);
4223 }
4224 if (bs.sensorPowerMah != 0) {
4225 pw.print(" sensor=");
4226 printmAh(pw, bs.sensorPowerMah);
4227 }
4228 if (bs.cameraPowerMah != 0) {
4229 pw.print(" camera=");
4230 printmAh(pw, bs.cameraPowerMah);
4231 }
4232 if (bs.flashlightPowerMah != 0) {
4233 pw.print(" flash=");
4234 printmAh(pw, bs.flashlightPowerMah);
4235 }
4236 pw.print(" )");
4237 }
4238 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004239 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004240 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004241 }
4242
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004243 sippers = helper.getMobilemsppList();
4244 if (sippers != null && sippers.size() > 0) {
4245 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004246 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004247 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004248 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004249 sb.setLength(0);
4250 sb.append(prefix); sb.append(" Uid ");
4251 UserHandle.formatUid(sb, bs.uidObj.getUid());
4252 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4253 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4254 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004255 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004256 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004257 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004258 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004259 sb.setLength(0);
4260 sb.append(prefix);
4261 sb.append(" TOTAL TIME: ");
4262 formatTimeMs(sb, totalTime);
4263 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4264 sb.append(")");
4265 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004266 pw.println();
4267 }
4268
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004269 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4270 @Override
4271 public int compare(TimerEntry lhs, TimerEntry rhs) {
4272 long lhsTime = lhs.mTime;
4273 long rhsTime = rhs.mTime;
4274 if (lhsTime < rhsTime) {
4275 return 1;
4276 }
4277 if (lhsTime > rhsTime) {
4278 return -1;
4279 }
4280 return 0;
4281 }
4282 };
4283
4284 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004285 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4286 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004287 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004288 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4289 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4290 : kernelWakelocks.entrySet()) {
4291 final BatteryStats.Timer timer = ent.getValue();
4292 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004293 if (totalTimeMillis > 0) {
4294 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4295 }
4296 }
4297 if (ktimers.size() > 0) {
4298 Collections.sort(ktimers, timerComparator);
4299 pw.print(prefix); pw.println(" All kernel wake locks:");
4300 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004301 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004302 String linePrefix = ": ";
4303 sb.setLength(0);
4304 sb.append(prefix);
4305 sb.append(" Kernel Wake lock ");
4306 sb.append(timer.mName);
4307 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4308 which, linePrefix);
4309 if (!linePrefix.equals(": ")) {
4310 sb.append(" realtime");
4311 // Only print out wake locks that were held
4312 pw.println(sb.toString());
4313 }
4314 }
4315 pw.println();
4316 }
4317 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004318
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004319 if (timers.size() > 0) {
4320 Collections.sort(timers, timerComparator);
4321 pw.print(prefix); pw.println(" All partial wake locks:");
4322 for (int i=0; i<timers.size(); i++) {
4323 TimerEntry timer = timers.get(i);
4324 sb.setLength(0);
4325 sb.append(" Wake lock ");
4326 UserHandle.formatUid(sb, timer.mId);
4327 sb.append(" ");
4328 sb.append(timer.mName);
4329 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4330 sb.append(" realtime");
4331 pw.println(sb.toString());
4332 }
4333 timers.clear();
4334 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004335 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004336
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004337 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004338 if (wakeupReasons.size() > 0) {
4339 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004340 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004341 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004342 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004343 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4344 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004345 }
4346 Collections.sort(reasons, timerComparator);
4347 for (int i=0; i<reasons.size(); i++) {
4348 TimerEntry timer = reasons.get(i);
4349 String linePrefix = ": ";
4350 sb.setLength(0);
4351 sb.append(prefix);
4352 sb.append(" Wakeup reason ");
4353 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004354 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4355 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004356 pw.println(sb.toString());
4357 }
4358 pw.println();
4359 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004360 }
Evan Millar22ac0432009-03-31 11:33:18 -07004361
James Carr2dd7e5e2016-07-20 18:48:39 -07004362 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4363 pw.println("Memory Stats");
4364 for (int i = 0; i < mMemoryStats.size(); i++) {
4365 sb.setLength(0);
4366 sb.append("Bandwidth ");
4367 sb.append(mMemoryStats.keyAt(i));
4368 sb.append(" Time ");
4369 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4370 pw.println(sb.toString());
4371 }
4372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 for (int iu=0; iu<NU; iu++) {
4374 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004375 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004376 continue;
4377 }
4378
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004379 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004380
4381 pw.print(prefix);
4382 pw.print(" ");
4383 UserHandle.formatUid(pw, uid);
4384 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004385 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004386
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004387 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4388 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4389 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4390 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004391 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4392 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4393
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004394 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4395 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004396 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4397 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004398
4399 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4400 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4401
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004402 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4403 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4404 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004405 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4406 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4407 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4408 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004409 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004410
Adam Lesinski5f056f62016-07-14 16:56:08 -07004411 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4412 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4413
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004414 if (mobileRxBytes > 0 || mobileTxBytes > 0
4415 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004416 pw.print(prefix); pw.print(" Mobile network: ");
4417 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004418 pw.print(formatBytesLocked(mobileTxBytes));
4419 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4420 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004422 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4423 sb.setLength(0);
4424 sb.append(prefix); sb.append(" Mobile radio active: ");
4425 formatTimeMs(sb, uidMobileActiveTime / 1000);
4426 sb.append("(");
4427 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4428 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4429 long packets = mobileRxPackets + mobileTxPackets;
4430 if (packets == 0) {
4431 packets = 1;
4432 }
4433 sb.append(" @ ");
4434 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4435 sb.append(" mspp");
4436 pw.println(sb.toString());
4437 }
4438
Adam Lesinski5f056f62016-07-14 16:56:08 -07004439 if (mobileWakeup > 0) {
4440 sb.setLength(0);
4441 sb.append(prefix);
4442 sb.append(" Mobile radio AP wakeups: ");
4443 sb.append(mobileWakeup);
4444 pw.println(sb.toString());
4445 }
4446
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004447 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4448 u.getModemControllerActivity(), which);
4449
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004450 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004451 pw.print(prefix); pw.print(" Wi-Fi network: ");
4452 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004453 pw.print(formatBytesLocked(wifiTxBytes));
4454 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4455 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004456 }
4457
Dianne Hackborn62793e42015-03-09 11:15:41 -07004458 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004459 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004460 || uidWifiRunningTime != 0) {
4461 sb.setLength(0);
4462 sb.append(prefix); sb.append(" Wifi Running: ");
4463 formatTimeMs(sb, uidWifiRunningTime / 1000);
4464 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4465 whichBatteryRealtime)); sb.append(")\n");
4466 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4467 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4468 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4469 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004470 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004471 formatTimeMs(sb, wifiScanTime / 1000);
4472 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004473 whichBatteryRealtime)); sb.append(") ");
4474 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004475 sb.append("x\n");
4476 // actual and background times are unpooled and since reset (regardless of 'which')
4477 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4478 formatTimeMs(sb, wifiScanActualTime / 1000);
4479 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4480 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4481 sb.append(") ");
4482 sb.append(wifiScanCount);
4483 sb.append("x\n");
4484 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4485 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4486 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4487 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4488 sb.append(") ");
4489 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004490 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004491 pw.println(sb.toString());
4492 }
4493
Adam Lesinski5f056f62016-07-14 16:56:08 -07004494 if (wifiWakeup > 0) {
4495 sb.setLength(0);
4496 sb.append(prefix);
4497 sb.append(" WiFi AP wakeups: ");
4498 sb.append(wifiWakeup);
4499 pw.println(sb.toString());
4500 }
4501
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004502 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4503 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004504
Adam Lesinski50e47602015-12-04 17:04:54 -08004505 if (btRxBytes > 0 || btTxBytes > 0) {
4506 pw.print(prefix); pw.print(" Bluetooth network: ");
4507 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4508 pw.print(formatBytesLocked(btTxBytes));
4509 pw.println(" sent");
4510 }
4511
Bookatz867c0d72017-03-07 18:23:42 -08004512 final Timer bleTimer = u.getBluetoothScanTimer();
4513 if (bleTimer != null) {
4514 // Convert from microseconds to milliseconds with rounding
4515 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4516 / 1000;
4517 if (totalTimeMs != 0) {
4518 final int count = bleTimer.getCountLocked(which);
4519 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4520 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
4521 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4522 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4523 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4524 final long actualTimeMsBg = bleTimerBg != null ?
4525 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4526
4527 sb.setLength(0);
4528 sb.append(prefix);
4529 sb.append(" ");
4530 sb.append("Bluetooth Scan");
4531 sb.append(": ");
4532 if (actualTimeMs != totalTimeMs) {
4533 formatTimeMs(sb, totalTimeMs);
4534 sb.append("blamed realtime, ");
4535 }
4536 formatTimeMs(sb, actualTimeMs); // since reset, regardless of 'which'
4537 sb.append("realtime (");
4538 sb.append(count);
4539 sb.append(" times)");
4540 if (bleTimer.isRunningLocked()) {
4541 sb.append(" (running)");
4542 }
4543 if (actualTimeMsBg != 0 || countBg > 0) {
4544 sb.append(", ");
4545 formatTimeMs(sb, actualTimeMsBg); // since reset, regardless of 'which'
4546 sb.append("background (");
4547 sb.append(countBg);
4548 sb.append(" times)");
4549 }
4550 pw.println(sb.toString());
4551 uidActivity = true;
4552 }
4553 }
4554
4555
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004556
Dianne Hackborn617f8772009-03-31 15:04:46 -07004557 if (u.hasUserActivity()) {
4558 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004559 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004560 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004561 if (val != 0) {
4562 if (!hasData) {
4563 sb.setLength(0);
4564 sb.append(" User activity: ");
4565 hasData = true;
4566 } else {
4567 sb.append(", ");
4568 }
4569 sb.append(val);
4570 sb.append(" ");
4571 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4572 }
4573 }
4574 if (hasData) {
4575 pw.println(sb.toString());
4576 }
4577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004579 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4580 = u.getWakelockStats();
4581 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004582 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004583 int countWakelock = 0;
4584 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4585 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4586 String linePrefix = ": ";
4587 sb.setLength(0);
4588 sb.append(prefix);
4589 sb.append(" Wake lock ");
4590 sb.append(wakelocks.keyAt(iw));
4591 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4592 "full", which, linePrefix);
4593 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4594 "partial", which, linePrefix);
4595 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4596 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004597 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4598 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004599 sb.append(" realtime");
4600 pw.println(sb.toString());
4601 uidActivity = true;
4602 countWakelock++;
4603
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004604 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4605 rawRealtime, which);
4606 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4607 rawRealtime, which);
4608 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4609 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004610 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004611 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004612 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004613 if (countWakelock > 1) {
4614 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4615 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004616 sb.setLength(0);
4617 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004618 sb.append(" TOTAL wake: ");
4619 boolean needComma = false;
4620 if (totalFullWakelock != 0) {
4621 needComma = true;
4622 formatTimeMs(sb, totalFullWakelock);
4623 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004624 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004625 if (totalPartialWakelock != 0) {
4626 if (needComma) {
4627 sb.append(", ");
4628 }
4629 needComma = true;
4630 formatTimeMs(sb, totalPartialWakelock);
4631 sb.append("partial");
4632 }
4633 if (totalWindowWakelock != 0) {
4634 if (needComma) {
4635 sb.append(", ");
4636 }
4637 needComma = true;
4638 formatTimeMs(sb, totalWindowWakelock);
4639 sb.append("window");
4640 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004641 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004642 if (needComma) {
4643 sb.append(",");
4644 }
4645 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004646 formatTimeMs(sb, totalDrawWakelock);
4647 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004648 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004649 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004650 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004651 }
4652 }
4653
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004654 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4655 for (int isy=syncs.size()-1; isy>=0; isy--) {
4656 final Timer timer = syncs.valueAt(isy);
4657 // Convert from microseconds to milliseconds with rounding
4658 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4659 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004660 final Timer bgTimer = timer.getSubTimer();
4661 final long bgTime = bgTimer != null ?
4662 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4663 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004664 sb.setLength(0);
4665 sb.append(prefix);
4666 sb.append(" Sync ");
4667 sb.append(syncs.keyAt(isy));
4668 sb.append(": ");
4669 if (totalTime != 0) {
4670 formatTimeMs(sb, totalTime);
4671 sb.append("realtime (");
4672 sb.append(count);
4673 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07004674 if (bgTime > 0) {
4675 sb.append(", ");
4676 formatTimeMs(sb, bgTime);
4677 sb.append("background (");
4678 sb.append(bgCount);
4679 sb.append(" times)");
4680 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004681 } else {
4682 sb.append("(not used)");
4683 }
4684 pw.println(sb.toString());
4685 uidActivity = true;
4686 }
4687
4688 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4689 for (int ij=jobs.size()-1; ij>=0; ij--) {
4690 final Timer timer = jobs.valueAt(ij);
4691 // Convert from microseconds to milliseconds with rounding
4692 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4693 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004694 final Timer bgTimer = timer.getSubTimer();
4695 final long bgTime = bgTimer != null ?
4696 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4697 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004698 sb.setLength(0);
4699 sb.append(prefix);
4700 sb.append(" Job ");
4701 sb.append(jobs.keyAt(ij));
4702 sb.append(": ");
4703 if (totalTime != 0) {
4704 formatTimeMs(sb, totalTime);
4705 sb.append("realtime (");
4706 sb.append(count);
4707 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07004708 if (bgTime > 0) {
4709 sb.append(", ");
4710 formatTimeMs(sb, bgTime);
4711 sb.append("background (");
4712 sb.append(bgCount);
4713 sb.append(" times)");
4714 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004715 } else {
4716 sb.append("(not used)");
4717 }
4718 pw.println(sb.toString());
4719 uidActivity = true;
4720 }
4721
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004722 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4723 prefix, "Flashlight");
4724 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4725 prefix, "Camera");
4726 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4727 prefix, "Video");
4728 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4729 prefix, "Audio");
4730
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004731 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4732 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004733 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004734 final Uid.Sensor se = sensors.valueAt(ise);
4735 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004736 sb.setLength(0);
4737 sb.append(prefix);
4738 sb.append(" Sensor ");
4739 int handle = se.getHandle();
4740 if (handle == Uid.Sensor.GPS) {
4741 sb.append("GPS");
4742 } else {
4743 sb.append(handle);
4744 }
4745 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004747 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004748 if (timer != null) {
4749 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08004750 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4751 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004752 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08004753 final Timer bgTimer = se.getSensorBackgroundTime();
4754 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
4755 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4756 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4757 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4758 final long bgActualTime = bgTimer != null ?
4759 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4760
Dianne Hackborn61659e52014-07-09 16:13:01 -07004761 //timer.logState();
4762 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004763 if (actualTime != totalTime) {
4764 formatTimeMs(sb, totalTime);
4765 sb.append("blamed realtime, ");
4766 }
4767
4768 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07004769 sb.append("realtime (");
4770 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08004771 sb.append(" times)");
4772
4773 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004774 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08004775 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
4776 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004777 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08004778 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 } else {
4781 sb.append("(not used)");
4782 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004783 } else {
4784 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004785 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004786
4787 pw.println(sb.toString());
4788 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004789 }
4790
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004791 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4792 "Vibrator");
4793 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4794 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004795
Dianne Hackborn61659e52014-07-09 16:13:01 -07004796 long totalStateTime = 0;
4797 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4798 long time = u.getProcessStateTime(ips, rawRealtime, which);
4799 if (time > 0) {
4800 totalStateTime += time;
4801 sb.setLength(0);
4802 sb.append(prefix);
4803 sb.append(" ");
4804 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4805 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004806 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004807 pw.println(sb.toString());
4808 uidActivity = true;
4809 }
4810 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004811 if (totalStateTime > 0) {
4812 sb.setLength(0);
4813 sb.append(prefix);
4814 sb.append(" Total running: ");
4815 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4816 pw.println(sb.toString());
4817 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004818
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004819 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4820 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004821 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004822 sb.setLength(0);
4823 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004824 sb.append(" Total cpu time: u=");
4825 formatTimeMs(sb, userCpuTimeUs / 1000);
4826 sb.append("s=");
4827 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004828 pw.println(sb.toString());
4829 }
4830
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004831 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4832 = u.getProcessStats();
4833 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4834 final Uid.Proc ps = processStats.valueAt(ipr);
4835 long userTime;
4836 long systemTime;
4837 long foregroundTime;
4838 int starts;
4839 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004841 userTime = ps.getUserTime(which);
4842 systemTime = ps.getSystemTime(which);
4843 foregroundTime = ps.getForegroundTime(which);
4844 starts = ps.getStarts(which);
4845 final int numCrashes = ps.getNumCrashes(which);
4846 final int numAnrs = ps.getNumAnrs(which);
4847 numExcessive = which == STATS_SINCE_CHARGED
4848 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004849
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004850 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4851 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4852 sb.setLength(0);
4853 sb.append(prefix); sb.append(" Proc ");
4854 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4855 sb.append(prefix); sb.append(" CPU: ");
4856 formatTimeMs(sb, userTime); sb.append("usr + ");
4857 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4858 formatTimeMs(sb, foregroundTime); sb.append("fg");
4859 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4860 sb.append("\n"); sb.append(prefix); sb.append(" ");
4861 boolean hasOne = false;
4862 if (starts != 0) {
4863 hasOne = true;
4864 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004865 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004866 if (numCrashes != 0) {
4867 if (hasOne) {
4868 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004869 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004870 hasOne = true;
4871 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004872 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004873 if (numAnrs != 0) {
4874 if (hasOne) {
4875 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004876 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004877 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 }
4879 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004880 pw.println(sb.toString());
4881 for (int e=0; e<numExcessive; e++) {
4882 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4883 if (ew != null) {
4884 pw.print(prefix); pw.print(" * Killed for ");
4885 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4886 pw.print("wake lock");
4887 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4888 pw.print("cpu");
4889 } else {
4890 pw.print("unknown");
4891 }
4892 pw.print(" use: ");
4893 TimeUtils.formatDuration(ew.usedTime, pw);
4894 pw.print(" over ");
4895 TimeUtils.formatDuration(ew.overTime, pw);
4896 if (ew.overTime != 0) {
4897 pw.print(" (");
4898 pw.print((ew.usedTime*100)/ew.overTime);
4899 pw.println("%)");
4900 }
4901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 }
4903 uidActivity = true;
4904 }
4905 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004906
4907 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4908 = u.getPackageStats();
4909 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4910 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4911 pw.println(":");
4912 boolean apkActivity = false;
4913 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4914 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4915 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4916 pw.print(prefix); pw.print(" Wakeup alarm ");
4917 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4918 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4919 pw.println(" times");
4920 apkActivity = true;
4921 }
4922 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4923 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4924 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4925 final long startTime = ss.getStartTime(batteryUptime, which);
4926 final int starts = ss.getStarts(which);
4927 final int launches = ss.getLaunches(which);
4928 if (startTime != 0 || starts != 0 || launches != 0) {
4929 sb.setLength(0);
4930 sb.append(prefix); sb.append(" Service ");
4931 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4932 sb.append(prefix); sb.append(" Created for: ");
4933 formatTimeMs(sb, startTime / 1000);
4934 sb.append("uptime\n");
4935 sb.append(prefix); sb.append(" Starts: ");
4936 sb.append(starts);
4937 sb.append(", launches: "); sb.append(launches);
4938 pw.println(sb.toString());
4939 apkActivity = true;
4940 }
4941 }
4942 if (!apkActivity) {
4943 pw.print(prefix); pw.println(" (nothing executed)");
4944 }
4945 uidActivity = true;
4946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004948 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004949 }
4950 }
4951 }
4952
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004953 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004954 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004955 int diff = oldval ^ newval;
4956 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004957 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004958 for (int i=0; i<descriptions.length; i++) {
4959 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004960 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004961 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004962 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004963 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004964 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004965 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4966 didWake = true;
4967 pw.print("=");
4968 if (longNames) {
4969 UserHandle.formatUid(pw, wakelockTag.uid);
4970 pw.print(":\"");
4971 pw.print(wakelockTag.string);
4972 pw.print("\"");
4973 } else {
4974 pw.print(wakelockTag.poolIdx);
4975 }
4976 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004977 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004978 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004979 pw.print("=");
4980 int val = (newval&bd.mask)>>bd.shift;
4981 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004982 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004983 } else {
4984 pw.print(val);
4985 }
4986 }
4987 }
4988 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004989 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004990 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004991 if (longNames) {
4992 UserHandle.formatUid(pw, wakelockTag.uid);
4993 pw.print(":\"");
4994 pw.print(wakelockTag.string);
4995 pw.print("\"");
4996 } else {
4997 pw.print(wakelockTag.poolIdx);
4998 }
4999 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005000 }
5001
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005002 public void prepareForDumpLocked() {
5003 }
5004
5005 public static class HistoryPrinter {
5006 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005007 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005008 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005009 int oldStatus = -1;
5010 int oldHealth = -1;
5011 int oldPlug = -1;
5012 int oldTemp = -1;
5013 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005014 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005015 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005016
Dianne Hackborn3251b902014-06-20 14:40:53 -07005017 void reset() {
5018 oldState = oldState2 = 0;
5019 oldLevel = -1;
5020 oldStatus = -1;
5021 oldHealth = -1;
5022 oldPlug = -1;
5023 oldTemp = -1;
5024 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005025 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005026 }
5027
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005028 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005029 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005030 if (!checkin) {
5031 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005032 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005033 pw.print(" (");
5034 pw.print(rec.numReadInts);
5035 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005036 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005037 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5038 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005039 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005040 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005041 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005042 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005043 }
5044 lastTime = rec.time;
5045 }
5046 if (rec.cmd == HistoryItem.CMD_START) {
5047 if (checkin) {
5048 pw.print(":");
5049 }
5050 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005051 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005052 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5053 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005054 if (checkin) {
5055 pw.print(":");
5056 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005057 if (rec.cmd == HistoryItem.CMD_RESET) {
5058 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005059 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005060 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005061 pw.print("TIME:");
5062 if (checkin) {
5063 pw.println(rec.currentTime);
5064 } else {
5065 pw.print(" ");
5066 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5067 rec.currentTime).toString());
5068 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005069 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5070 if (checkin) {
5071 pw.print(":");
5072 }
5073 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005074 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5075 if (checkin) {
5076 pw.print(":");
5077 }
5078 pw.println("*OVERFLOW*");
5079 } else {
5080 if (!checkin) {
5081 if (rec.batteryLevel < 10) pw.print("00");
5082 else if (rec.batteryLevel < 100) pw.print("0");
5083 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005084 if (verbose) {
5085 pw.print(" ");
5086 if (rec.states < 0) ;
5087 else if (rec.states < 0x10) pw.print("0000000");
5088 else if (rec.states < 0x100) pw.print("000000");
5089 else if (rec.states < 0x1000) pw.print("00000");
5090 else if (rec.states < 0x10000) pw.print("0000");
5091 else if (rec.states < 0x100000) pw.print("000");
5092 else if (rec.states < 0x1000000) pw.print("00");
5093 else if (rec.states < 0x10000000) pw.print("0");
5094 pw.print(Integer.toHexString(rec.states));
5095 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005096 } else {
5097 if (oldLevel != rec.batteryLevel) {
5098 oldLevel = rec.batteryLevel;
5099 pw.print(",Bl="); pw.print(rec.batteryLevel);
5100 }
5101 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005102 if (oldStatus != rec.batteryStatus) {
5103 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005104 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005105 switch (oldStatus) {
5106 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005107 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005108 break;
5109 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005110 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005111 break;
5112 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005113 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005114 break;
5115 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005116 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005117 break;
5118 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005119 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005120 break;
5121 default:
5122 pw.print(oldStatus);
5123 break;
5124 }
5125 }
5126 if (oldHealth != rec.batteryHealth) {
5127 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005128 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005129 switch (oldHealth) {
5130 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005131 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005132 break;
5133 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005134 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005135 break;
5136 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005137 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005138 break;
5139 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005140 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005141 break;
5142 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005143 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005144 break;
5145 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005146 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005147 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005148 case BatteryManager.BATTERY_HEALTH_COLD:
5149 pw.print(checkin ? "c" : "cold");
5150 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005151 default:
5152 pw.print(oldHealth);
5153 break;
5154 }
5155 }
5156 if (oldPlug != rec.batteryPlugType) {
5157 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005158 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005159 switch (oldPlug) {
5160 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005161 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005162 break;
5163 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005164 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005165 break;
5166 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005167 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005168 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005169 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005170 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005171 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005172 default:
5173 pw.print(oldPlug);
5174 break;
5175 }
5176 }
5177 if (oldTemp != rec.batteryTemperature) {
5178 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005179 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005180 pw.print(oldTemp);
5181 }
5182 if (oldVolt != rec.batteryVoltage) {
5183 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005184 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005185 pw.print(oldVolt);
5186 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005187 final int chargeMAh = rec.batteryChargeUAh / 1000;
5188 if (oldChargeMAh != chargeMAh) {
5189 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005190 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005191 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005192 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005193 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005194 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005195 printBitDescriptions(pw, oldState2, rec.states2, null,
5196 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005197 if (rec.wakeReasonTag != null) {
5198 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005199 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005200 pw.print(rec.wakeReasonTag.poolIdx);
5201 } else {
5202 pw.print(" wake_reason=");
5203 pw.print(rec.wakeReasonTag.uid);
5204 pw.print(":\"");
5205 pw.print(rec.wakeReasonTag.string);
5206 pw.print("\"");
5207 }
5208 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005209 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005210 pw.print(checkin ? "," : " ");
5211 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5212 pw.print("+");
5213 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5214 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005215 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005216 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5217 : HISTORY_EVENT_NAMES;
5218 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5219 | HistoryItem.EVENT_FLAG_FINISH);
5220 if (idx >= 0 && idx < eventNames.length) {
5221 pw.print(eventNames[idx]);
5222 } else {
5223 pw.print(checkin ? "Ev" : "event");
5224 pw.print(idx);
5225 }
5226 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005227 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005228 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005229 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005230 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5231 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005232 pw.print(":\"");
5233 pw.print(rec.eventTag.string);
5234 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005235 }
5236 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005237 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005238 if (rec.stepDetails != null) {
5239 if (!checkin) {
5240 pw.print(" Details: cpu=");
5241 pw.print(rec.stepDetails.userTime);
5242 pw.print("u+");
5243 pw.print(rec.stepDetails.systemTime);
5244 pw.print("s");
5245 if (rec.stepDetails.appCpuUid1 >= 0) {
5246 pw.print(" (");
5247 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5248 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5249 if (rec.stepDetails.appCpuUid2 >= 0) {
5250 pw.print(", ");
5251 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5252 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5253 }
5254 if (rec.stepDetails.appCpuUid3 >= 0) {
5255 pw.print(", ");
5256 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5257 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5258 }
5259 pw.print(')');
5260 }
5261 pw.println();
5262 pw.print(" /proc/stat=");
5263 pw.print(rec.stepDetails.statUserTime);
5264 pw.print(" usr, ");
5265 pw.print(rec.stepDetails.statSystemTime);
5266 pw.print(" sys, ");
5267 pw.print(rec.stepDetails.statIOWaitTime);
5268 pw.print(" io, ");
5269 pw.print(rec.stepDetails.statIrqTime);
5270 pw.print(" irq, ");
5271 pw.print(rec.stepDetails.statSoftIrqTime);
5272 pw.print(" sirq, ");
5273 pw.print(rec.stepDetails.statIdlTime);
5274 pw.print(" idle");
5275 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5276 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5277 + rec.stepDetails.statSoftIrqTime;
5278 int total = totalRun + rec.stepDetails.statIdlTime;
5279 if (total > 0) {
5280 pw.print(" (");
5281 float perc = ((float)totalRun) / ((float)total) * 100;
5282 pw.print(String.format("%.1f%%", perc));
5283 pw.print(" of ");
5284 StringBuilder sb = new StringBuilder(64);
5285 formatTimeMsNoSpace(sb, total*10);
5286 pw.print(sb);
5287 pw.print(")");
5288 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005289 pw.print(", PlatformIdleStat ");
5290 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005291 pw.println();
5292 } else {
5293 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5294 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5295 pw.print(rec.stepDetails.userTime);
5296 pw.print(":");
5297 pw.print(rec.stepDetails.systemTime);
5298 if (rec.stepDetails.appCpuUid1 >= 0) {
5299 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5300 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5301 if (rec.stepDetails.appCpuUid2 >= 0) {
5302 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5303 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5304 }
5305 if (rec.stepDetails.appCpuUid3 >= 0) {
5306 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5307 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5308 }
5309 }
5310 pw.println();
5311 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5312 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5313 pw.print(rec.stepDetails.statUserTime);
5314 pw.print(',');
5315 pw.print(rec.stepDetails.statSystemTime);
5316 pw.print(',');
5317 pw.print(rec.stepDetails.statIOWaitTime);
5318 pw.print(',');
5319 pw.print(rec.stepDetails.statIrqTime);
5320 pw.print(',');
5321 pw.print(rec.stepDetails.statSoftIrqTime);
5322 pw.print(',');
5323 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005324 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005325 if (rec.stepDetails.statPlatformIdleState != null) {
5326 pw.print(rec.stepDetails.statPlatformIdleState);
5327 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005328 pw.println();
5329 }
5330 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005331 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005332 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005333 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005334 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005335
5336 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5337 UserHandle.formatUid(pw, uid);
5338 pw.print("=");
5339 pw.print(utime);
5340 pw.print("u+");
5341 pw.print(stime);
5342 pw.print("s");
5343 }
5344
5345 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5346 pw.print('/');
5347 pw.print(uid);
5348 pw.print(":");
5349 pw.print(utime);
5350 pw.print(":");
5351 pw.print(stime);
5352 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005353 }
5354
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005355 private void printSizeValue(PrintWriter pw, long size) {
5356 float result = size;
5357 String suffix = "";
5358 if (result >= 10*1024) {
5359 suffix = "KB";
5360 result = result / 1024;
5361 }
5362 if (result >= 10*1024) {
5363 suffix = "MB";
5364 result = result / 1024;
5365 }
5366 if (result >= 10*1024) {
5367 suffix = "GB";
5368 result = result / 1024;
5369 }
5370 if (result >= 10*1024) {
5371 suffix = "TB";
5372 result = result / 1024;
5373 }
5374 if (result >= 10*1024) {
5375 suffix = "PB";
5376 result = result / 1024;
5377 }
5378 pw.print((int)result);
5379 pw.print(suffix);
5380 }
5381
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005382 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5383 String label3, long estimatedTime) {
5384 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005385 return false;
5386 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005387 pw.print(label1);
5388 pw.print(label2);
5389 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005390 StringBuilder sb = new StringBuilder(64);
5391 formatTimeMs(sb, estimatedTime);
5392 pw.print(sb);
5393 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005394 return true;
5395 }
5396
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005397 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5398 LevelStepTracker steps, boolean checkin) {
5399 if (steps == null) {
5400 return false;
5401 }
5402 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005403 if (count <= 0) {
5404 return false;
5405 }
5406 if (!checkin) {
5407 pw.println(header);
5408 }
Kweku Adams030980a2015-04-01 16:07:48 -07005409 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005410 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005411 long duration = steps.getDurationAt(i);
5412 int level = steps.getLevelAt(i);
5413 long initMode = steps.getInitModeAt(i);
5414 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005415 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005416 lineArgs[0] = Long.toString(duration);
5417 lineArgs[1] = Integer.toString(level);
5418 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5419 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5420 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5421 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5422 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5423 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005424 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005425 }
5426 } else {
5427 lineArgs[2] = "";
5428 }
5429 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5430 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5431 } else {
5432 lineArgs[3] = "";
5433 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005434 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005435 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005436 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005437 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005438 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005439 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5440 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005441 pw.print(prefix);
5442 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005443 TimeUtils.formatDuration(duration, pw);
5444 pw.print(" to "); pw.print(level);
5445 boolean haveModes = false;
5446 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5447 pw.print(" (");
5448 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5449 case Display.STATE_OFF: pw.print("screen-off"); break;
5450 case Display.STATE_ON: pw.print("screen-on"); break;
5451 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5452 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005453 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005454 }
5455 haveModes = true;
5456 }
5457 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5458 pw.print(haveModes ? ", " : " (");
5459 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5460 ? "power-save-on" : "power-save-off");
5461 haveModes = true;
5462 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005463 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5464 pw.print(haveModes ? ", " : " (");
5465 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5466 ? "device-idle-on" : "device-idle-off");
5467 haveModes = true;
5468 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005469 if (haveModes) {
5470 pw.print(")");
5471 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005472 pw.println();
5473 }
5474 }
5475 return true;
5476 }
5477
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005478 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005479 public static final int DUMP_DAILY_ONLY = 1<<2;
5480 public static final int DUMP_HISTORY_ONLY = 1<<3;
5481 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5482 public static final int DUMP_VERBOSE = 1<<5;
5483 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005484
Dianne Hackborn37de0982014-05-09 09:32:18 -07005485 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5486 final HistoryPrinter hprinter = new HistoryPrinter();
5487 final HistoryItem rec = new HistoryItem();
5488 long lastTime = -1;
5489 long baseTime = -1;
5490 boolean printed = false;
5491 HistoryEventTracker tracker = null;
5492 while (getNextHistoryLocked(rec)) {
5493 lastTime = rec.time;
5494 if (baseTime < 0) {
5495 baseTime = lastTime;
5496 }
5497 if (rec.time >= histStart) {
5498 if (histStart >= 0 && !printed) {
5499 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005500 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005501 || rec.cmd == HistoryItem.CMD_START
5502 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005503 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005504 hprinter.printNextItem(pw, rec, baseTime, checkin,
5505 (flags&DUMP_VERBOSE) != 0);
5506 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005507 } else if (rec.currentTime != 0) {
5508 printed = true;
5509 byte cmd = rec.cmd;
5510 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005511 hprinter.printNextItem(pw, rec, baseTime, checkin,
5512 (flags&DUMP_VERBOSE) != 0);
5513 rec.cmd = cmd;
5514 }
5515 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005516 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5517 hprinter.printNextItem(pw, rec, baseTime, checkin,
5518 (flags&DUMP_VERBOSE) != 0);
5519 rec.cmd = HistoryItem.CMD_UPDATE;
5520 }
5521 int oldEventCode = rec.eventCode;
5522 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005523 rec.eventTag = new HistoryTag();
5524 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5525 HashMap<String, SparseIntArray> active
5526 = tracker.getStateForEvent(i);
5527 if (active == null) {
5528 continue;
5529 }
5530 for (HashMap.Entry<String, SparseIntArray> ent
5531 : active.entrySet()) {
5532 SparseIntArray uids = ent.getValue();
5533 for (int j=0; j<uids.size(); j++) {
5534 rec.eventCode = i;
5535 rec.eventTag.string = ent.getKey();
5536 rec.eventTag.uid = uids.keyAt(j);
5537 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005538 hprinter.printNextItem(pw, rec, baseTime, checkin,
5539 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005540 rec.wakeReasonTag = null;
5541 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005542 }
5543 }
5544 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005545 rec.eventCode = oldEventCode;
5546 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005547 tracker = null;
5548 }
5549 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005550 hprinter.printNextItem(pw, rec, baseTime, checkin,
5551 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005552 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5553 // This is an attempt to aggregate the previous state and generate
5554 // fake events to reflect that state at the point where we start
5555 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005556 if (tracker == null) {
5557 tracker = new HistoryEventTracker();
5558 }
5559 tracker.updateState(rec.eventCode, rec.eventTag.string,
5560 rec.eventTag.uid, rec.eventTag.poolIdx);
5561 }
5562 }
5563 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005564 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005565 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5566 }
5567 }
5568
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005569 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5570 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5571 if (steps == null) {
5572 return;
5573 }
5574 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5575 if (timeRemaining >= 0) {
5576 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5577 tmpSb.setLength(0);
5578 formatTimeMs(tmpSb, timeRemaining);
5579 pw.print(tmpSb);
5580 pw.print(" (from "); pw.print(tmpOutInt[0]);
5581 pw.println(" steps)");
5582 }
5583 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5584 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5585 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5586 if (estimatedTime > 0) {
5587 pw.print(prefix); pw.print(label); pw.print(" ");
5588 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5589 pw.print(" time: ");
5590 tmpSb.setLength(0);
5591 formatTimeMs(tmpSb, estimatedTime);
5592 pw.print(tmpSb);
5593 pw.print(" (from "); pw.print(tmpOutInt[0]);
5594 pw.println(" steps)");
5595 }
5596 }
5597 }
5598
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005599 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5600 ArrayList<PackageChange> changes) {
5601 if (changes == null) {
5602 return;
5603 }
5604 pw.print(prefix); pw.println("Package changes:");
5605 for (int i=0; i<changes.size(); i++) {
5606 PackageChange pc = changes.get(i);
5607 if (pc.mUpdate) {
5608 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5609 pw.print(" vers="); pw.println(pc.mVersionCode);
5610 } else {
5611 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5612 }
5613 }
5614 }
5615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616 /**
5617 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5618 *
5619 * @param pw a Printer to receive the dump output.
5620 */
5621 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005622 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005623 prepareForDumpLocked();
5624
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005625 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005626 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005627
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005628 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005629 final long historyTotalSize = getHistoryTotalSize();
5630 final long historyUsedSize = getHistoryUsedSize();
5631 if (startIteratingHistoryLocked()) {
5632 try {
5633 pw.print("Battery History (");
5634 pw.print((100*historyUsedSize)/historyTotalSize);
5635 pw.print("% used, ");
5636 printSizeValue(pw, historyUsedSize);
5637 pw.print(" used of ");
5638 printSizeValue(pw, historyTotalSize);
5639 pw.print(", ");
5640 pw.print(getHistoryStringPoolSize());
5641 pw.print(" strings using ");
5642 printSizeValue(pw, getHistoryStringPoolBytes());
5643 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005644 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005645 pw.println();
5646 } finally {
5647 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005648 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005649 }
5650
5651 if (startIteratingOldHistoryLocked()) {
5652 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005653 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005654 pw.println("Old battery History:");
5655 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005656 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005657 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005658 if (baseTime < 0) {
5659 baseTime = rec.time;
5660 }
5661 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005662 }
5663 pw.println();
5664 } finally {
5665 finishIteratingOldHistoryLocked();
5666 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005667 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005668 }
5669
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005670 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005671 return;
5672 }
5673
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005674 if (!filtering) {
5675 SparseArray<? extends Uid> uidStats = getUidStats();
5676 final int NU = uidStats.size();
5677 boolean didPid = false;
5678 long nowRealtime = SystemClock.elapsedRealtime();
5679 for (int i=0; i<NU; i++) {
5680 Uid uid = uidStats.valueAt(i);
5681 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5682 if (pids != null) {
5683 for (int j=0; j<pids.size(); j++) {
5684 Uid.Pid pid = pids.valueAt(j);
5685 if (!didPid) {
5686 pw.println("Per-PID Stats:");
5687 didPid = true;
5688 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005689 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5690 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005691 pw.print(" PID "); pw.print(pids.keyAt(j));
5692 pw.print(" wake time: ");
5693 TimeUtils.formatDuration(time, pw);
5694 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005695 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005696 }
5697 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005698 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005699 pw.println();
5700 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005701 }
5702
5703 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005704 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5705 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005706 long timeRemaining = computeBatteryTimeRemaining(
5707 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005708 if (timeRemaining >= 0) {
5709 pw.print(" Estimated discharge time remaining: ");
5710 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5711 pw.println();
5712 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005713 final LevelStepTracker steps = getDischargeLevelStepTracker();
5714 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5715 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5716 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5717 STEP_LEVEL_MODE_VALUES[i], null));
5718 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005719 pw.println();
5720 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005721 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5722 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005723 long timeRemaining = computeChargeTimeRemaining(
5724 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005725 if (timeRemaining >= 0) {
5726 pw.print(" Estimated charge time remaining: ");
5727 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5728 pw.println();
5729 }
5730 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005731 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005732 }
5733 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5734 pw.println("Daily stats:");
5735 pw.print(" Current start time: ");
5736 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5737 getCurrentDailyStartTime()).toString());
5738 pw.print(" Next min deadline: ");
5739 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5740 getNextMinDailyDeadline()).toString());
5741 pw.print(" Next max deadline: ");
5742 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5743 getNextMaxDailyDeadline()).toString());
5744 StringBuilder sb = new StringBuilder(64);
5745 int[] outInt = new int[1];
5746 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5747 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005748 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5749 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005750 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005751 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5752 dsteps, false)) {
5753 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5754 sb, outInt);
5755 }
5756 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5757 csteps, false)) {
5758 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5759 sb, outInt);
5760 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005761 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005762 } else {
5763 pw.println(" Current daily steps:");
5764 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5765 sb, outInt);
5766 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5767 sb, outInt);
5768 }
5769 }
5770 DailyItem dit;
5771 int curIndex = 0;
5772 while ((dit=getDailyItemLocked(curIndex)) != null) {
5773 curIndex++;
5774 if ((flags&DUMP_DAILY_ONLY) != 0) {
5775 pw.println();
5776 }
5777 pw.print(" Daily from ");
5778 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5779 pw.print(" to ");
5780 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5781 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005782 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005783 if (dumpDurationSteps(pw, " ",
5784 " Discharge step durations:", dit.mDischargeSteps, false)) {
5785 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5786 sb, outInt);
5787 }
5788 if (dumpDurationSteps(pw, " ",
5789 " Charge step durations:", dit.mChargeSteps, false)) {
5790 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5791 sb, outInt);
5792 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005793 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005794 } else {
5795 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5796 sb, outInt);
5797 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5798 sb, outInt);
5799 }
5800 }
5801 pw.println();
5802 }
5803 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005804 pw.println("Statistics since last charge:");
5805 pw.println(" System starts: " + getStartCount()
5806 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005807 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5808 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005809 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 }
5812
5813 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005814 public void dumpCheckinLocked(Context context, PrintWriter pw,
5815 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005816 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005817
5818 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005819 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5820 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005821
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005822 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5823
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005824 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005825 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005826
5827 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005828 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005829 try {
5830 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5831 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5832 pw.print(HISTORY_STRING_POOL); pw.print(',');
5833 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005834 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005835 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005836 pw.print(",\"");
5837 String str = getHistoryTagPoolString(i);
5838 str = str.replace("\\", "\\\\");
5839 str = str.replace("\"", "\\\"");
5840 pw.print(str);
5841 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005842 pw.println();
5843 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005844 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005845 } finally {
5846 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005847 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005848 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005849 }
5850
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005851 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005852 return;
5853 }
5854
Dianne Hackborne4a59512010-12-07 11:08:07 -08005855 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005856 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005857 for (int i=0; i<apps.size(); i++) {
5858 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005859 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
5860 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005861 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005862 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
5863 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005864 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005865 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005866 }
5867 SparseArray<? extends Uid> uidStats = getUidStats();
5868 final int NU = uidStats.size();
5869 String[] lineArgs = new String[2];
5870 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005871 int uid = UserHandle.getAppId(uidStats.keyAt(i));
5872 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
5873 if (pkgs != null && !pkgs.second.value) {
5874 pkgs.second.value = true;
5875 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005876 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005877 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005878 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5879 (Object[])lineArgs);
5880 }
5881 }
5882 }
5883 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005884 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005885 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005886 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07005887 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005888 if (timeRemaining >= 0) {
5889 lineArgs[0] = Long.toString(timeRemaining);
5890 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5891 (Object[])lineArgs);
5892 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005893 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07005894 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005895 if (timeRemaining >= 0) {
5896 lineArgs[0] = Long.toString(timeRemaining);
5897 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5898 (Object[])lineArgs);
5899 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005900 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5901 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904}