blob: 51df9c14c1027932ac6ad37bbbc565775df79bba [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
19import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080020import java.util.ArrayList;
Dianne Hackborn81038902012-11-26 17:04:09 -080021import java.util.Collections;
22import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.Formatter;
Dianne Hackborn37de0982014-05-09 09:32:18 -070024import java.util.HashMap;
Dianne Hackborne4a59512010-12-07 11:08:07 -080025import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import java.util.Map;
27
Dianne Hackborna7c837f2014-01-15 16:20:44 -080028import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080029import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080030import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080031import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070032import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070033import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070034import android.util.MutableBoolean;
35import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.util.Printer;
37import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070038import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070039import android.util.TimeUtils;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070040import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080041
Dianne Hackborna7c837f2014-01-15 16:20:44 -080042import com.android.internal.os.BatterySipper;
43import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45/**
46 * A class providing access to battery usage statistics, including information on
47 * wakelocks, processes, packages, and services. All times are represented in microseconds
48 * except where indicated otherwise.
49 * @hide
50 */
51public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070052 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070055
56 /** @hide */
57 public static final String SERVICE_NAME = "batterystats";
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 /**
60 * A constant indicating a partial wake lock timer.
61 */
62 public static final int WAKE_TYPE_PARTIAL = 0;
63
64 /**
65 * A constant indicating a full wake lock timer.
66 */
67 public static final int WAKE_TYPE_FULL = 1;
68
69 /**
70 * A constant indicating a window wake lock timer.
71 */
72 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 /**
75 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 */
77 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070078
79 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070080 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070081 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070082 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070083
84 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070085 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070086 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070087 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070088
89 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070090 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070091 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070092 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
Dianne Hackborn62793e42015-03-09 11:15:41 -070094 /**
95 * A constant indicating a wifi multicast timer
96 */
97 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -070098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700100 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700101 */
102 public static final int VIDEO_TURNED_ON = 8;
103
104 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800105 * A constant indicating a vibrator on timer
106 */
107 public static final int VIBRATOR_ON = 9;
108
109 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700110 * A constant indicating a foreground activity timer
111 */
112 public static final int FOREGROUND_ACTIVITY = 10;
113
114 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700115 * A constant indicating a wifi batched scan is active
116 */
117 public static final int WIFI_BATCHED_SCAN = 11;
118
119 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700120 * A constant indicating a process state timer
121 */
122 public static final int PROCESS_STATE = 12;
123
124 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700125 * A constant indicating a sync timer
126 */
127 public static final int SYNC = 13;
128
129 /**
130 * A constant indicating a job timer
131 */
132 public static final int JOB = 14;
133
134 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800135 * A constant indicating an audio turn on timer
136 */
137 public static final int AUDIO_TURNED_ON = 15;
138
139 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700140 * A constant indicating a flashlight turn on timer
141 */
142 public static final int FLASHLIGHT_TURNED_ON = 16;
143
144 /**
145 * A constant indicating a camera turn on timer
146 */
147 public static final int CAMERA_TURNED_ON = 17;
148
149 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700150 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700151 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700152 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700153
154 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800155 * A constant indicating a bluetooth scan timer.
156 */
157 public static final int BLUETOOTH_SCAN_ON = 19;
158
159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 * Include all of the data in the stats, including previously saved data.
161 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700162 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 * Include only the current run in the stats.
166 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700167 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
169 /**
170 * Include only the run since the last time the device was unplugged in the stats.
171 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700172 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700173
174 // NOTE: Update this list if you add/change any stats above.
175 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700176 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700177 private static final String[] STAT_NAMES = { "l", "c", "u" };
178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700180 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700181 *
182 * New in version 19:
183 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800184 * New in version 20:
Bookatz867c0d72017-03-07 18:23:42 -0800185 * - Sensor, BluetoothScan, WifiScan get background timers and counter.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700186 */
Kweku Adams47db5a82016-12-09 19:04:50 -0800187 static final String CHECKIN_VERSION = "20";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700188
189 /**
190 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700192 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700193
Evan Millar22ac0432009-03-31 11:33:18 -0700194 private static final long BYTES_PER_KB = 1024;
195 private static final long BYTES_PER_MB = 1048576; // 1024^2
196 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700198 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800199 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700200 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700202 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700203 private static final String CPU_DATA = "cpu";
Evan Millare84de8d2009-04-02 22:16:12 -0700204 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800205 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700206 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700207 private static final String STATE_TIME_DATA = "st";
Evan Millare84de8d2009-04-02 22:16:12 -0700208 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700209 private static final String SYNC_DATA = "sy";
210 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700211 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700212 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700213 private static final String NETWORK_DATA = "nt";
214 private static final String USER_ACTIVITY_DATA = "ua";
215 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800216 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700217 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700218 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700219 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800220 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
221 private static final String WIFI_CONTROLLER_DATA = "wfcd";
222 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
223 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700224 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700225 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800226 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800227 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
228 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800229 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700230 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700231 private static final String SCREEN_BRIGHTNESS_DATA = "br";
232 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700233 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700234 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
235 private static final String DATA_CONNECTION_TIME_DATA = "dct";
236 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800237 private static final String WIFI_STATE_TIME_DATA = "wst";
238 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700239 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
240 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
241 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
242 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800243 private static final String POWER_USE_SUMMARY_DATA = "pws";
244 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700245 private static final String DISCHARGE_STEP_DATA = "dsd";
246 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700247 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
248 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700249 private static final String FLASHLIGHT_DATA = "fla";
250 private static final String CAMERA_DATA = "cam";
251 private static final String VIDEO_DATA = "vid";
252 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Adam Lesinski010bf372016-04-11 12:18:18 -0700254 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
255
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700256 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 private final Formatter mFormatter = new Formatter(mFormatBuilder);
258
259 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700260 * State for keeping track of counting information.
261 */
262 public static abstract class Counter {
263
264 /**
265 * Returns the count associated with this Counter for the
266 * selected type of statistics.
267 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700268 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700269 */
Evan Millarc64edde2009-04-18 12:26:32 -0700270 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700271
272 /**
273 * Temporary for debugging.
274 */
275 public abstract void logState(Printer pw, String prefix);
276 }
277
278 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700279 * State for keeping track of long counting information.
280 */
281 public static abstract class LongCounter {
282
283 /**
284 * Returns the count associated with this Counter for the
285 * selected type of statistics.
286 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700287 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700288 */
289 public abstract long getCountLocked(int which);
290
291 /**
292 * Temporary for debugging.
293 */
294 public abstract void logState(Printer pw, String prefix);
295 }
296
297 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800298 * Container class that aggregates counters for transmit, receive, and idle state of a
299 * radio controller.
300 */
301 public static abstract class ControllerActivityCounter {
302 /**
303 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
304 * idle state.
305 */
306 public abstract LongCounter getIdleTimeCounter();
307
308 /**
309 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
310 * receive state.
311 */
312 public abstract LongCounter getRxTimeCounter();
313
314 /**
315 * An array of {@link LongCounter}, representing various transmit levels, where each level
316 * may draw a different amount of power. The levels themselves are controller-specific.
317 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
318 * various transmit level states.
319 */
320 public abstract LongCounter[] getTxTimeCounters();
321
322 /**
323 * @return a non-null {@link LongCounter} representing the power consumed by the controller
324 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
325 * yield a value of 0 if the device doesn't support power calculations.
326 */
327 public abstract LongCounter getPowerCounter();
328 }
329
330 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 * State for keeping track of timing information.
332 */
333 public static abstract class Timer {
334
335 /**
336 * Returns the count associated with this Timer for the
337 * selected type of statistics.
338 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700339 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 */
Evan Millarc64edde2009-04-18 12:26:32 -0700341 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342
343 /**
344 * Returns the total time in microseconds associated with this Timer for the
345 * selected type of statistics.
346 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800347 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700348 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * @return a time in microseconds
350 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800351 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700354 * Returns the total time in microseconds associated with this Timer since the
355 * 'mark' was last set.
356 *
357 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
358 * @return a time in microseconds
359 */
360 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
361
362 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700363 * Returns the max duration if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800364 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700365
366 */
367 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
368 return -1;
369 }
370
371 /**
372 * Returns the current time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800373 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700374 */
375 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
376 return -1;
377 }
378
379 /**
Bookatz867c0d72017-03-07 18:23:42 -0800380 * Returns the current time the timer has been active, if it is being tracked.
381 *
382 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
383 * been on since reset.
384 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
385 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
386 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
387 * the actual total time.
388 * Not all Timer subclasses track the max, total, current durations.
389 */
390 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
391 return -1;
392 }
393
394 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700395 * Returns whether the timer is currently running. Some types of timers
396 * (e.g. BatchTimers) don't know whether the event is currently active,
397 * and report false.
398 */
399 public boolean isRunningLocked() {
400 return false;
401 }
402
403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * Temporary for debugging.
405 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700406 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 }
408
409 /**
410 * The statistics associated with a particular uid.
411 */
412 public static abstract class Uid {
413
414 /**
415 * Returns a mapping containing wakelock statistics.
416 *
417 * @return a Map from Strings to Uid.Wakelock objects.
418 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700419 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420
421 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700422 * Returns a mapping containing sync statistics.
423 *
424 * @return a Map from Strings to Timer objects.
425 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700426 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700427
428 /**
429 * Returns a mapping containing scheduled job statistics.
430 *
431 * @return a Map from Strings to Timer objects.
432 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700433 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700434
435 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 * The statistics associated with a particular wake lock.
437 */
438 public static abstract class Wakelock {
439 public abstract Timer getWakeTime(int type);
440 }
441
442 /**
443 * Returns a mapping containing sensor statistics.
444 *
445 * @return a Map from Integer sensor ids to Uid.Sensor objects.
446 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700447 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448
449 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700450 * Returns a mapping containing active process data.
451 */
452 public abstract SparseArray<? extends Pid> getPidStats();
453
454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 * Returns a mapping containing process statistics.
456 *
457 * @return a Map from Strings to Uid.Proc objects.
458 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700459 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460
461 /**
462 * Returns a mapping containing package statistics.
463 *
464 * @return a Map from Strings to Uid.Pkg objects.
465 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700466 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700467
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800468 public abstract ControllerActivityCounter getWifiControllerActivity();
469 public abstract ControllerActivityCounter getBluetoothControllerActivity();
470 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800471
472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 * {@hide}
474 */
475 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700476
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800477 public abstract void noteWifiRunningLocked(long elapsedRealtime);
478 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
479 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
480 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
481 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
482 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
483 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
484 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
485 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
486 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800487 public abstract void noteActivityResumedLocked(long elapsedRealtime);
488 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800489 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
490 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
491 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700492 public abstract int getWifiScanCount(int which);
Bookatz867c0d72017-03-07 18:23:42 -0800493 public abstract int getWifiScanBackgroundCount(int which);
494 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
495 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800496 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700497 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800498 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700499 public abstract Timer getAudioTurnedOnTimer();
500 public abstract Timer getVideoTurnedOnTimer();
501 public abstract Timer getFlashlightTurnedOnTimer();
502 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700503 public abstract Timer getForegroundActivityTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800504 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800505 public abstract Timer getBluetoothScanBackgroundTimer();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700506
Dianne Hackborna0200e32016-03-30 18:01:41 -0700507 // Note: the following times are disjoint. They can be added together to find the
508 // total time a uid has had any processes running at all.
509
510 /**
511 * Time this uid has any processes in the top state (or above such as persistent).
512 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800513 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700514 /**
515 * Time this uid has any process with a started out bound foreground service, but
516 * none in the "top" state.
517 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800518 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700519 /**
520 * Time this uid has any process that is top while the device is sleeping, but none
521 * in the "foreground service" or better state.
522 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800523 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700524 /**
525 * Time this uid has any process in an active foreground state, but none in the
526 * "top sleeping" or better state.
527 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800528 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700529 /**
530 * Time this uid has any process in an active background state, but none in the
531 * "foreground" or better state.
532 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800533 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700534 /**
535 * Time this uid has any processes that are sitting around cached, not in one of the
536 * other active states.
537 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800538 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700539 /**
540 * Total number of process states we track.
541 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800542 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700543
544 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800545 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700546 };
547
548 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800549 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700550
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800551 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552
Robert Greenwalta029ea12013-09-25 16:38:12 -0700553 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
554
Dianne Hackborn617f8772009-03-31 15:04:46 -0700555 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700556 * Note that these must match the constants in android.os.PowerManager.
557 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
558 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700559 */
560 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700561 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700562 };
563
Phil Weaverda80d672016-03-15 16:25:46 -0700564 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700565
Dianne Hackborn617f8772009-03-31 15:04:46 -0700566 public abstract void noteUserActivityLocked(int type);
567 public abstract boolean hasUserActivity();
568 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700569
570 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800571 public abstract long getNetworkActivityBytes(int type, int which);
572 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800573 public abstract long getMobileRadioActiveTime(int which);
574 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700575
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700576 /**
577 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
578 */
579 public abstract long getUserCpuTimeUs(int which);
580
581 /**
582 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
583 */
584 public abstract long getSystemCpuTimeUs(int which);
585
586 /**
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700587 * Get the total cpu power consumed (in milli-ampere-microseconds).
588 */
589 public abstract long getCpuPowerMaUs(int which);
590
591 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700592 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
593 * given CPU cluster.
594 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700595 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700596 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700597 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
598 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700599 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700600 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700601
Adam Lesinski5f056f62016-07-14 16:56:08 -0700602 /**
603 * Returns the number of times this UID woke up the Application Processor to
604 * process a mobile radio packet.
605 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
606 */
607 public abstract long getMobileRadioApWakeupCount(int which);
608
609 /**
610 * Returns the number of times this UID woke up the Application Processor to
611 * process a WiFi packet.
612 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
613 */
614 public abstract long getWifiRadioApWakeupCount(int which);
615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800617 /*
618 * FIXME: it's not correct to use this magic value because it
619 * could clash with a sensor handle (which are defined by
620 * the sensor HAL, and therefore out of our control
621 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 // Magic sensor number for the GPS.
623 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800628
Bookatz867c0d72017-03-07 18:23:42 -0800629 /** Returns a Timer for sensor usage when app is in the background. */
630 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700633 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800634 public int mWakeNesting;
635 public long mWakeSumMs;
636 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700637 }
638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 /**
640 * The statistics associated with a particular process.
641 */
642 public static abstract class Proc {
643
Dianne Hackborn287952c2010-09-22 22:34:31 -0700644 public static class ExcessivePower {
645 public static final int TYPE_WAKE = 1;
646 public static final int TYPE_CPU = 2;
647
648 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700649 public long overTime;
650 public long usedTime;
651 }
652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800654 * Returns true if this process is still active in the battery stats.
655 */
656 public abstract boolean isActive();
657
658 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700659 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700661 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 */
663 public abstract long getUserTime(int which);
664
665 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700666 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700668 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 */
670 public abstract long getSystemTime(int which);
671
672 /**
673 * Returns the number of times the process has been started.
674 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700675 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 */
677 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700678
679 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800680 * Returns the number of times the process has crashed.
681 *
682 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
683 */
684 public abstract int getNumCrashes(int which);
685
686 /**
687 * Returns the number of times the process has ANRed.
688 *
689 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
690 */
691 public abstract int getNumAnrs(int which);
692
693 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700694 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700695 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700696 * @return foreground cpu time in microseconds
697 */
698 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700699
Dianne Hackborn287952c2010-09-22 22:34:31 -0700700 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700701
Dianne Hackborn287952c2010-09-22 22:34:31 -0700702 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 }
704
705 /**
706 * The statistics associated with a particular package.
707 */
708 public static abstract class Pkg {
709
710 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700711 * Returns information about all wakeup alarms that have been triggered for this
712 * package. The mapping keys are tag names for the alarms, the counter contains
713 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700715 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716
717 /**
718 * Returns a mapping containing service statistics.
719 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700720 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721
722 /**
723 * The statistics associated with a particular service.
724 */
Joe Onoratoabded112016-02-08 16:49:39 -0800725 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
727 /**
728 * Returns the amount of time spent started.
729 *
730 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700731 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 * @return
733 */
734 public abstract long getStartTime(long batteryUptime, int which);
735
736 /**
737 * Returns the total number of times startService() has been called.
738 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700739 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 */
741 public abstract int getStarts(int which);
742
743 /**
744 * Returns the total number times the service has been launched.
745 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700746 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 */
748 public abstract int getLaunches(int which);
749 }
750 }
751 }
752
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800753 public static final class LevelStepTracker {
754 public long mLastStepTime = -1;
755 public int mNumStepDurations;
756 public final long[] mStepDurations;
757
758 public LevelStepTracker(int maxLevelSteps) {
759 mStepDurations = new long[maxLevelSteps];
760 }
761
762 public LevelStepTracker(int numSteps, long[] steps) {
763 mNumStepDurations = numSteps;
764 mStepDurations = new long[numSteps];
765 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
766 }
767
768 public long getDurationAt(int index) {
769 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
770 }
771
772 public int getLevelAt(int index) {
773 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
774 >> STEP_LEVEL_LEVEL_SHIFT);
775 }
776
777 public int getInitModeAt(int index) {
778 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
779 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
780 }
781
782 public int getModModeAt(int index) {
783 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
784 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
785 }
786
787 private void appendHex(long val, int topOffset, StringBuilder out) {
788 boolean hasData = false;
789 while (topOffset >= 0) {
790 int digit = (int)( (val>>topOffset) & 0xf );
791 topOffset -= 4;
792 if (!hasData && digit == 0) {
793 continue;
794 }
795 hasData = true;
796 if (digit >= 0 && digit <= 9) {
797 out.append((char)('0' + digit));
798 } else {
799 out.append((char)('a' + digit - 10));
800 }
801 }
802 }
803
804 public void encodeEntryAt(int index, StringBuilder out) {
805 long item = mStepDurations[index];
806 long duration = item & STEP_LEVEL_TIME_MASK;
807 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
808 >> STEP_LEVEL_LEVEL_SHIFT);
809 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
810 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
811 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
812 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
813 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
814 case Display.STATE_OFF: out.append('f'); break;
815 case Display.STATE_ON: out.append('o'); break;
816 case Display.STATE_DOZE: out.append('d'); break;
817 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
818 }
819 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
820 out.append('p');
821 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700822 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
823 out.append('i');
824 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800825 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
826 case Display.STATE_OFF: out.append('F'); break;
827 case Display.STATE_ON: out.append('O'); break;
828 case Display.STATE_DOZE: out.append('D'); break;
829 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
830 }
831 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
832 out.append('P');
833 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700834 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
835 out.append('I');
836 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800837 out.append('-');
838 appendHex(level, 4, out);
839 out.append('-');
840 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
841 }
842
843 public void decodeEntryAt(int index, String value) {
844 final int N = value.length();
845 int i = 0;
846 char c;
847 long out = 0;
848 while (i < N && (c=value.charAt(i)) != '-') {
849 i++;
850 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800851 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800852 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800853 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800854 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800855 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800856 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800857 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
858 << STEP_LEVEL_INITIAL_MODE_SHIFT);
859 break;
860 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
861 << STEP_LEVEL_INITIAL_MODE_SHIFT);
862 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700863 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
864 << STEP_LEVEL_INITIAL_MODE_SHIFT);
865 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800866 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
867 break;
868 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
869 break;
870 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
871 break;
872 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
873 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
874 break;
875 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
876 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800877 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700878 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
879 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
880 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800881 }
882 }
883 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800884 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800885 while (i < N && (c=value.charAt(i)) != '-') {
886 i++;
887 level <<= 4;
888 if (c >= '0' && c <= '9') {
889 level += c - '0';
890 } else if (c >= 'a' && c <= 'f') {
891 level += c - 'a' + 10;
892 } else if (c >= 'A' && c <= 'F') {
893 level += c - 'A' + 10;
894 }
895 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800896 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800897 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
898 long duration = 0;
899 while (i < N && (c=value.charAt(i)) != '-') {
900 i++;
901 duration <<= 4;
902 if (c >= '0' && c <= '9') {
903 duration += c - '0';
904 } else if (c >= 'a' && c <= 'f') {
905 duration += c - 'a' + 10;
906 } else if (c >= 'A' && c <= 'F') {
907 duration += c - 'A' + 10;
908 }
909 }
910 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
911 }
912
913 public void init() {
914 mLastStepTime = -1;
915 mNumStepDurations = 0;
916 }
917
918 public void clearTime() {
919 mLastStepTime = -1;
920 }
921
922 public long computeTimePerLevel() {
923 final long[] steps = mStepDurations;
924 final int numSteps = mNumStepDurations;
925
926 // For now we'll do a simple average across all steps.
927 if (numSteps <= 0) {
928 return -1;
929 }
930 long total = 0;
931 for (int i=0; i<numSteps; i++) {
932 total += steps[i] & STEP_LEVEL_TIME_MASK;
933 }
934 return total / numSteps;
935 /*
936 long[] buckets = new long[numSteps];
937 int numBuckets = 0;
938 int numToAverage = 4;
939 int i = 0;
940 while (i < numSteps) {
941 long totalTime = 0;
942 int num = 0;
943 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
944 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
945 num++;
946 }
947 buckets[numBuckets] = totalTime / num;
948 numBuckets++;
949 numToAverage *= 2;
950 i += num;
951 }
952 if (numBuckets < 1) {
953 return -1;
954 }
955 long averageTime = buckets[numBuckets-1];
956 for (i=numBuckets-2; i>=0; i--) {
957 averageTime = (averageTime + buckets[i]) / 2;
958 }
959 return averageTime;
960 */
961 }
962
963 public long computeTimeEstimate(long modesOfInterest, long modeValues,
964 int[] outNumOfInterest) {
965 final long[] steps = mStepDurations;
966 final int count = mNumStepDurations;
967 if (count <= 0) {
968 return -1;
969 }
970 long total = 0;
971 int numOfInterest = 0;
972 for (int i=0; i<count; i++) {
973 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
974 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
975 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
976 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
977 // If the modes of interest didn't change during this step period...
978 if ((modMode&modesOfInterest) == 0) {
979 // And the mode values during this period match those we are measuring...
980 if ((initMode&modesOfInterest) == modeValues) {
981 // Then this can be used to estimate the total time!
982 numOfInterest++;
983 total += steps[i] & STEP_LEVEL_TIME_MASK;
984 }
985 }
986 }
987 if (numOfInterest <= 0) {
988 return -1;
989 }
990
991 if (outNumOfInterest != null) {
992 outNumOfInterest[0] = numOfInterest;
993 }
994
995 // The estimated time is the average time we spend in each level, multipled
996 // by 100 -- the total number of battery levels
997 return (total / numOfInterest) * 100;
998 }
999
1000 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1001 int stepCount = mNumStepDurations;
1002 final long lastStepTime = mLastStepTime;
1003 if (lastStepTime >= 0 && numStepLevels > 0) {
1004 final long[] steps = mStepDurations;
1005 long duration = elapsedRealtime - lastStepTime;
1006 for (int i=0; i<numStepLevels; i++) {
1007 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1008 long thisDuration = duration / (numStepLevels-i);
1009 duration -= thisDuration;
1010 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1011 thisDuration = STEP_LEVEL_TIME_MASK;
1012 }
1013 steps[0] = thisDuration | modeBits;
1014 }
1015 stepCount += numStepLevels;
1016 if (stepCount > steps.length) {
1017 stepCount = steps.length;
1018 }
1019 }
1020 mNumStepDurations = stepCount;
1021 mLastStepTime = elapsedRealtime;
1022 }
1023
1024 public void readFromParcel(Parcel in) {
1025 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001026 if (N > mStepDurations.length) {
1027 throw new ParcelFormatException("more step durations than available: " + N);
1028 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001029 mNumStepDurations = N;
1030 for (int i=0; i<N; i++) {
1031 mStepDurations[i] = in.readLong();
1032 }
1033 }
1034
1035 public void writeToParcel(Parcel out) {
1036 final int N = mNumStepDurations;
1037 out.writeInt(N);
1038 for (int i=0; i<N; i++) {
1039 out.writeLong(mStepDurations[i]);
1040 }
1041 }
1042 }
1043
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001044 public static final class PackageChange {
1045 public String mPackageName;
1046 public boolean mUpdate;
1047 public int mVersionCode;
1048 }
1049
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001050 public static final class DailyItem {
1051 public long mStartTime;
1052 public long mEndTime;
1053 public LevelStepTracker mDischargeSteps;
1054 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001055 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001056 }
1057
1058 public abstract DailyItem getDailyItemLocked(int daysAgo);
1059
1060 public abstract long getCurrentDailyStartTime();
1061
1062 public abstract long getNextMinDailyDeadline();
1063
1064 public abstract long getNextMaxDailyDeadline();
1065
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001066 public final static class HistoryTag {
1067 public String string;
1068 public int uid;
1069
1070 public int poolIdx;
1071
1072 public void setTo(HistoryTag o) {
1073 string = o.string;
1074 uid = o.uid;
1075 poolIdx = o.poolIdx;
1076 }
1077
1078 public void setTo(String _string, int _uid) {
1079 string = _string;
1080 uid = _uid;
1081 poolIdx = -1;
1082 }
1083
1084 public void writeToParcel(Parcel dest, int flags) {
1085 dest.writeString(string);
1086 dest.writeInt(uid);
1087 }
1088
1089 public void readFromParcel(Parcel src) {
1090 string = src.readString();
1091 uid = src.readInt();
1092 poolIdx = -1;
1093 }
1094
1095 @Override
1096 public boolean equals(Object o) {
1097 if (this == o) return true;
1098 if (o == null || getClass() != o.getClass()) return false;
1099
1100 HistoryTag that = (HistoryTag) o;
1101
1102 if (uid != that.uid) return false;
1103 if (!string.equals(that.string)) return false;
1104
1105 return true;
1106 }
1107
1108 @Override
1109 public int hashCode() {
1110 int result = string.hashCode();
1111 result = 31 * result + uid;
1112 return result;
1113 }
1114 }
1115
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001116 /**
1117 * Optional detailed information that can go into a history step. This is typically
1118 * generated each time the battery level changes.
1119 */
1120 public final static class HistoryStepDetails {
1121 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1122 public int userTime;
1123 public int systemTime;
1124
1125 // Top three apps using CPU in the last step, with times in 1/100 second.
1126 public int appCpuUid1;
1127 public int appCpuUTime1;
1128 public int appCpuSTime1;
1129 public int appCpuUid2;
1130 public int appCpuUTime2;
1131 public int appCpuSTime2;
1132 public int appCpuUid3;
1133 public int appCpuUTime3;
1134 public int appCpuSTime3;
1135
1136 // Information from /proc/stat
1137 public int statUserTime;
1138 public int statSystemTime;
1139 public int statIOWaitTime;
1140 public int statIrqTime;
1141 public int statSoftIrqTime;
1142 public int statIdlTime;
1143
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001144 // Platform-level low power state stats
1145 public String statPlatformIdleState;
1146
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001147 public HistoryStepDetails() {
1148 clear();
1149 }
1150
1151 public void clear() {
1152 userTime = systemTime = 0;
1153 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1154 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1155 = appCpuUTime3 = appCpuSTime3 = 0;
1156 }
1157
1158 public void writeToParcel(Parcel out) {
1159 out.writeInt(userTime);
1160 out.writeInt(systemTime);
1161 out.writeInt(appCpuUid1);
1162 out.writeInt(appCpuUTime1);
1163 out.writeInt(appCpuSTime1);
1164 out.writeInt(appCpuUid2);
1165 out.writeInt(appCpuUTime2);
1166 out.writeInt(appCpuSTime2);
1167 out.writeInt(appCpuUid3);
1168 out.writeInt(appCpuUTime3);
1169 out.writeInt(appCpuSTime3);
1170 out.writeInt(statUserTime);
1171 out.writeInt(statSystemTime);
1172 out.writeInt(statIOWaitTime);
1173 out.writeInt(statIrqTime);
1174 out.writeInt(statSoftIrqTime);
1175 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001176 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001177 }
1178
1179 public void readFromParcel(Parcel in) {
1180 userTime = in.readInt();
1181 systemTime = in.readInt();
1182 appCpuUid1 = in.readInt();
1183 appCpuUTime1 = in.readInt();
1184 appCpuSTime1 = in.readInt();
1185 appCpuUid2 = in.readInt();
1186 appCpuUTime2 = in.readInt();
1187 appCpuSTime2 = in.readInt();
1188 appCpuUid3 = in.readInt();
1189 appCpuUTime3 = in.readInt();
1190 appCpuSTime3 = in.readInt();
1191 statUserTime = in.readInt();
1192 statSystemTime = in.readInt();
1193 statIOWaitTime = in.readInt();
1194 statIrqTime = in.readInt();
1195 statSoftIrqTime = in.readInt();
1196 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001197 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001198 }
1199 }
1200
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001201 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001202 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001203
1204 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001205 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001206
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001207 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001208 public static final byte CMD_NULL = -1;
1209 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001210 public static final byte CMD_CURRENT_TIME = 5;
1211 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001212 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001213 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001214
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001215 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001216
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001217 /**
1218 * Return whether the command code is a delta data update.
1219 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001220 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001221 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001222 }
1223
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001224 public byte batteryLevel;
1225 public byte batteryStatus;
1226 public byte batteryHealth;
1227 public byte batteryPlugType;
1228
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001229 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001230 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001231
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001232 // The charge of the battery in micro-Ampere-hours.
1233 public int batteryChargeUAh;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001234
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001235 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001236 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001237 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001238 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001239 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1240 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001241 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001242 public static final int STATE_PHONE_STATE_SHIFT = 6;
1243 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001244 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001245 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001246 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001247
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001248 // These states always appear directly in the first int token
1249 // of a delta change; they should be ones that change relatively
1250 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001251 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1252 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001253 public static final int STATE_GPS_ON_FLAG = 1<<29;
1254 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001255 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001256 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001257 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001258 // Do not use, this is used for coulomb delta count.
1259 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001260 // These are on the lower bits used for the command; if they change
1261 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001262 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001263 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001264 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001265 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1266 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1267 // empty slot
1268 // empty slot
1269 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001270
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001271 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001272 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1273
1274 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001275
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001276 public int states;
1277
Dianne Hackborn3251b902014-06-20 14:40:53 -07001278 // Constants from WIFI_SUPPL_STATE_*
1279 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1280 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1281 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1282 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1283 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1284 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1285
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001286 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001287 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1288 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1289 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001290 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001291 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1292 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1293 public static final int STATE2_CHARGING_FLAG = 1<<24;
1294 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1295 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1296 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001297 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001298
1299 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001300 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001301 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1302
1303 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001304
Dianne Hackborn40c87252014-03-19 16:55:40 -07001305 public int states2;
1306
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001307 // The wake lock that was acquired at this point.
1308 public HistoryTag wakelockTag;
1309
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001310 // Kernel wakeup reason at this point.
1311 public HistoryTag wakeReasonTag;
1312
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001313 // Non-null when there is more detailed information at this step.
1314 public HistoryStepDetails stepDetails;
1315
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001316 public static final int EVENT_FLAG_START = 0x8000;
1317 public static final int EVENT_FLAG_FINISH = 0x4000;
1318
1319 // No event in this item.
1320 public static final int EVENT_NONE = 0x0000;
1321 // Event is about a process that is running.
1322 public static final int EVENT_PROC = 0x0001;
1323 // Event is about an application package that is in the foreground.
1324 public static final int EVENT_FOREGROUND = 0x0002;
1325 // Event is about an application package that is at the top of the screen.
1326 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001327 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001328 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001329 // Events for all additional wake locks aquired/release within a wake block.
1330 // These are not generated by default.
1331 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001332 // Event is about an application executing a scheduled job.
1333 public static final int EVENT_JOB = 0x0006;
1334 // Events for users running.
1335 public static final int EVENT_USER_RUNNING = 0x0007;
1336 // Events for foreground user.
1337 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001338 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001339 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001340 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001341 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001342 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001343 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001344 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001345 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001346 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001347 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001348 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001349 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001350 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001351 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001352 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001353 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001354 // Event for a package being on the temporary whitelist.
1355 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001356 // Event for the screen waking up.
1357 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001358 // Event for the UID that woke up the application processor.
1359 // Used for wakeups coming from WiFi, modem, etc.
1360 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001361 // Event for reporting that a specific partial wake lock has been held for a long duration.
1362 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Adam Lesinski041d9172016-12-12 12:03:56 -08001363 // Event reporting the new estimated (learned) capacity of the battery in mAh.
1364 public static final int EVENT_ESTIMATED_BATTERY_CAP = 0x0015;
Amith Yamasani67768492015-06-09 12:23:58 -07001365
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001366 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001367 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001368 // Mask to extract out only the type part of the event.
1369 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001370
1371 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1372 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1373 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1374 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1375 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1376 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001377 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1378 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001379 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1380 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001381 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1382 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1383 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1384 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1385 public static final int EVENT_USER_FOREGROUND_START =
1386 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1387 public static final int EVENT_USER_FOREGROUND_FINISH =
1388 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001389 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1390 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001391 public static final int EVENT_TEMP_WHITELIST_START =
1392 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1393 public static final int EVENT_TEMP_WHITELIST_FINISH =
1394 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001395 public static final int EVENT_LONG_WAKE_LOCK_START =
1396 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1397 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1398 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001399
1400 // For CMD_EVENT.
1401 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001402 public HistoryTag eventTag;
1403
Dianne Hackborn9a755432014-05-15 17:05:22 -07001404 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001405 public long currentTime;
1406
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001407 // Meta-data when reading.
1408 public int numReadInts;
1409
1410 // Pre-allocated objects.
1411 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001412 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001413 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001414
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001415 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001416 }
1417
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001418 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001419 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001420 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001421 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001422 }
1423
1424 public int describeContents() {
1425 return 0;
1426 }
1427
1428 public void writeToParcel(Parcel dest, int flags) {
1429 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001430 int bat = (((int)cmd)&0xff)
1431 | ((((int)batteryLevel)<<8)&0xff00)
1432 | ((((int)batteryStatus)<<16)&0xf0000)
1433 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001434 | ((((int)batteryPlugType)<<24)&0xf000000)
1435 | (wakelockTag != null ? 0x10000000 : 0)
1436 | (wakeReasonTag != null ? 0x20000000 : 0)
1437 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001438 dest.writeInt(bat);
1439 bat = (((int)batteryTemperature)&0xffff)
1440 | ((((int)batteryVoltage)<<16)&0xffff0000);
1441 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001442 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001443 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001444 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001445 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001446 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001447 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001448 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001449 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001450 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001451 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001452 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001453 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001454 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001455 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001456 dest.writeLong(currentTime);
1457 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001458 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001459
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001460 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001461 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001462 int bat = src.readInt();
1463 cmd = (byte)(bat&0xff);
1464 batteryLevel = (byte)((bat>>8)&0xff);
1465 batteryStatus = (byte)((bat>>16)&0xf);
1466 batteryHealth = (byte)((bat>>20)&0xf);
1467 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001468 int bat2 = src.readInt();
1469 batteryTemperature = (short)(bat2&0xffff);
1470 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001471 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001472 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001473 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001474 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001475 wakelockTag = localWakelockTag;
1476 wakelockTag.readFromParcel(src);
1477 } else {
1478 wakelockTag = null;
1479 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001480 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001481 wakeReasonTag = localWakeReasonTag;
1482 wakeReasonTag.readFromParcel(src);
1483 } else {
1484 wakeReasonTag = null;
1485 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001486 if ((bat&0x40000000) != 0) {
1487 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001488 eventTag = localEventTag;
1489 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001490 } else {
1491 eventCode = EVENT_NONE;
1492 eventTag = null;
1493 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001494 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001495 currentTime = src.readLong();
1496 } else {
1497 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001498 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001499 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001500 }
1501
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001502 public void clear() {
1503 time = 0;
1504 cmd = CMD_NULL;
1505 batteryLevel = 0;
1506 batteryStatus = 0;
1507 batteryHealth = 0;
1508 batteryPlugType = 0;
1509 batteryTemperature = 0;
1510 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001511 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001512 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001513 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001514 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001515 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001516 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001517 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001518 }
1519
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001520 public void setTo(HistoryItem o) {
1521 time = o.time;
1522 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001523 setToCommon(o);
1524 }
1525
1526 public void setTo(long time, byte cmd, HistoryItem o) {
1527 this.time = time;
1528 this.cmd = cmd;
1529 setToCommon(o);
1530 }
1531
1532 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001533 batteryLevel = o.batteryLevel;
1534 batteryStatus = o.batteryStatus;
1535 batteryHealth = o.batteryHealth;
1536 batteryPlugType = o.batteryPlugType;
1537 batteryTemperature = o.batteryTemperature;
1538 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001539 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001540 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001541 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001542 if (o.wakelockTag != null) {
1543 wakelockTag = localWakelockTag;
1544 wakelockTag.setTo(o.wakelockTag);
1545 } else {
1546 wakelockTag = null;
1547 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001548 if (o.wakeReasonTag != null) {
1549 wakeReasonTag = localWakeReasonTag;
1550 wakeReasonTag.setTo(o.wakeReasonTag);
1551 } else {
1552 wakeReasonTag = null;
1553 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001554 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001555 if (o.eventTag != null) {
1556 eventTag = localEventTag;
1557 eventTag.setTo(o.eventTag);
1558 } else {
1559 eventTag = null;
1560 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001561 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001562 }
1563
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001564 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001565 return batteryLevel == o.batteryLevel
1566 && batteryStatus == o.batteryStatus
1567 && batteryHealth == o.batteryHealth
1568 && batteryPlugType == o.batteryPlugType
1569 && batteryTemperature == o.batteryTemperature
1570 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001571 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001572 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001573 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001574 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001575 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001576
1577 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001578 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001579 return false;
1580 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001581 if (wakelockTag != o.wakelockTag) {
1582 if (wakelockTag == null || o.wakelockTag == null) {
1583 return false;
1584 }
1585 if (!wakelockTag.equals(o.wakelockTag)) {
1586 return false;
1587 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001588 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001589 if (wakeReasonTag != o.wakeReasonTag) {
1590 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1591 return false;
1592 }
1593 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1594 return false;
1595 }
1596 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001597 if (eventTag != o.eventTag) {
1598 if (eventTag == null || o.eventTag == null) {
1599 return false;
1600 }
1601 if (!eventTag.equals(o.eventTag)) {
1602 return false;
1603 }
1604 }
1605 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001606 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001607 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001608
1609 public final static class HistoryEventTracker {
1610 private final HashMap<String, SparseIntArray>[] mActiveEvents
1611 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1612
1613 public boolean updateState(int code, String name, int uid, int poolIdx) {
1614 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1615 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1616 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1617 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001618 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001619 mActiveEvents[idx] = active;
1620 }
1621 SparseIntArray uids = active.get(name);
1622 if (uids == null) {
1623 uids = new SparseIntArray();
1624 active.put(name, uids);
1625 }
1626 if (uids.indexOfKey(uid) >= 0) {
1627 // Already set, nothing to do!
1628 return false;
1629 }
1630 uids.put(uid, poolIdx);
1631 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1632 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1633 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1634 if (active == null) {
1635 // not currently active, nothing to do.
1636 return false;
1637 }
1638 SparseIntArray uids = active.get(name);
1639 if (uids == null) {
1640 // not currently active, nothing to do.
1641 return false;
1642 }
1643 idx = uids.indexOfKey(uid);
1644 if (idx < 0) {
1645 // not currently active, nothing to do.
1646 return false;
1647 }
1648 uids.removeAt(idx);
1649 if (uids.size() <= 0) {
1650 active.remove(name);
1651 }
1652 }
1653 return true;
1654 }
1655
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001656 public void removeEvents(int code) {
1657 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1658 mActiveEvents[idx] = null;
1659 }
1660
Dianne Hackborn37de0982014-05-09 09:32:18 -07001661 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1662 return mActiveEvents[code];
1663 }
1664 }
1665
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001666 public static final class BitDescription {
1667 public final int mask;
1668 public final int shift;
1669 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001670 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001671 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001672 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001673
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001674 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001675 this.mask = mask;
1676 this.shift = -1;
1677 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001678 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001679 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001680 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001681 }
1682
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001683 public BitDescription(int mask, int shift, String name, String shortName,
1684 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001685 this.mask = mask;
1686 this.shift = shift;
1687 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001688 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001689 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001690 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001691 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001692 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001693
Dianne Hackbornfc064132014-06-02 12:42:12 -07001694 /**
1695 * Don't allow any more batching in to the current history event. This
1696 * is called when printing partial histories, so to ensure that the next
1697 * history event will go in to a new batch after what was printed in the
1698 * last partial history.
1699 */
1700 public abstract void commitCurrentHistoryBatchLocked();
1701
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001702 public abstract int getHistoryTotalSize();
1703
1704 public abstract int getHistoryUsedSize();
1705
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001706 public abstract boolean startIteratingHistoryLocked();
1707
Dianne Hackborn099bc622014-01-22 13:39:16 -08001708 public abstract int getHistoryStringPoolSize();
1709
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001710 public abstract int getHistoryStringPoolBytes();
1711
1712 public abstract String getHistoryTagPoolString(int index);
1713
1714 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001715
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001716 public abstract boolean getNextHistoryLocked(HistoryItem out);
1717
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001718 public abstract void finishIteratingHistoryLocked();
1719
1720 public abstract boolean startIteratingOldHistoryLocked();
1721
1722 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1723
1724 public abstract void finishIteratingOldHistoryLocked();
1725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001727 * Return the base time offset for the battery history.
1728 */
1729 public abstract long getHistoryBaseTime();
1730
1731 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 * Returns the number of times the device has been started.
1733 */
1734 public abstract int getStartCount();
1735
1736 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001737 * Returns the time in microseconds that the screen has been on while the device was
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 * running on battery.
1739 *
1740 * {@hide}
1741 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001742 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001744 /**
1745 * Returns the number of times the screen was turned on.
1746 *
1747 * {@hide}
1748 */
1749 public abstract int getScreenOnCount(int which);
1750
Jeff Browne95c3cd2014-05-02 16:59:26 -07001751 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1752
Dianne Hackborn617f8772009-03-31 15:04:46 -07001753 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1754 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1755 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1756 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1757 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1758
1759 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1760 "dark", "dim", "medium", "light", "bright"
1761 };
1762
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001763 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1764 "0", "1", "2", "3", "4"
1765 };
1766
Dianne Hackborn617f8772009-03-31 15:04:46 -07001767 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001768
Dianne Hackborn617f8772009-03-31 15:04:46 -07001769 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001770 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001771 * the given brightness
1772 *
1773 * {@hide}
1774 */
1775 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001776 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001779 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001780 * running on battery.
1781 *
1782 * {@hide}
1783 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001784 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001785
1786 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001787 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001788 *
1789 * {@hide}
1790 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001791 public abstract int getPowerSaveModeEnabledCount(int which);
1792
1793 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001794 * Constant for device idle mode: not active.
1795 */
1796 public static final int DEVICE_IDLE_MODE_OFF = 0;
1797
1798 /**
1799 * Constant for device idle mode: active in lightweight mode.
1800 */
1801 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1802
1803 /**
1804 * Constant for device idle mode: active in full mode.
1805 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001806 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001807
1808 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001809 * Returns the time in microseconds that device has been in idle mode while
1810 * running on battery.
1811 *
1812 * {@hide}
1813 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001814 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001815
1816 /**
1817 * Returns the number of times that the devie has gone in to idle mode.
1818 *
1819 * {@hide}
1820 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001821 public abstract int getDeviceIdleModeCount(int mode, int which);
1822
1823 /**
1824 * Return the longest duration we spent in a particular device idle mode (fully in the
1825 * mode, not in idle maintenance etc).
1826 */
1827 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001828
1829 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001830 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001831 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001832 * counts all of the time that we consider the device to be idle, whether or not
1833 * it is currently in the actual device idle mode.
1834 *
1835 * {@hide}
1836 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001837 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001838
1839 /**
1840 * Returns the number of times that the devie has started idling.
1841 *
1842 * {@hide}
1843 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001844 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001845
1846 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001847 * Returns the number of times that connectivity state changed.
1848 *
1849 * {@hide}
1850 */
1851 public abstract int getNumConnectivityChange(int which);
1852
1853 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001854 * Returns the time in microseconds that the phone has been on while the device was
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 * running on battery.
1856 *
1857 * {@hide}
1858 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001859 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001860
1861 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001862 * Returns the number of times a phone call was activated.
1863 *
1864 * {@hide}
1865 */
1866 public abstract int getPhoneOnCount(int which);
1867
1868 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001869 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001870 * the given signal strength.
1871 *
1872 * {@hide}
1873 */
1874 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001875 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001876
Dianne Hackborn617f8772009-03-31 15:04:46 -07001877 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001878 * Returns the time in microseconds that the phone has been trying to
1879 * acquire a signal.
1880 *
1881 * {@hide}
1882 */
1883 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001884 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001885
1886 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001887 * Returns the number of times the phone has entered the given signal strength.
1888 *
1889 * {@hide}
1890 */
1891 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1892
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001893 /**
1894 * Returns the time in microseconds that the mobile network has been active
1895 * (in a high power state).
1896 *
1897 * {@hide}
1898 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001899 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001900
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001901 /**
1902 * Returns the number of times that the mobile network has transitioned to the
1903 * active state.
1904 *
1905 * {@hide}
1906 */
1907 public abstract int getMobileRadioActiveCount(int which);
1908
1909 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001910 * Returns the time in microseconds that is the difference between the mobile radio
1911 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1912 * from the radio.
1913 *
1914 * {@hide}
1915 */
1916 public abstract long getMobileRadioActiveAdjustedTime(int which);
1917
1918 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001919 * Returns the time in microseconds that the mobile network has been active
1920 * (in a high power state) but not being able to blame on an app.
1921 *
1922 * {@hide}
1923 */
1924 public abstract long getMobileRadioActiveUnknownTime(int which);
1925
1926 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001927 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001928 *
1929 * {@hide}
1930 */
1931 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001932
Dianne Hackborn627bba72009-03-24 22:32:56 -07001933 public static final int DATA_CONNECTION_NONE = 0;
1934 public static final int DATA_CONNECTION_GPRS = 1;
1935 public static final int DATA_CONNECTION_EDGE = 2;
1936 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001937 public static final int DATA_CONNECTION_CDMA = 4;
1938 public static final int DATA_CONNECTION_EVDO_0 = 5;
1939 public static final int DATA_CONNECTION_EVDO_A = 6;
1940 public static final int DATA_CONNECTION_1xRTT = 7;
1941 public static final int DATA_CONNECTION_HSDPA = 8;
1942 public static final int DATA_CONNECTION_HSUPA = 9;
1943 public static final int DATA_CONNECTION_HSPA = 10;
1944 public static final int DATA_CONNECTION_IDEN = 11;
1945 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001946 public static final int DATA_CONNECTION_LTE = 13;
1947 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001948 public static final int DATA_CONNECTION_HSPAP = 15;
1949 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001950
Dianne Hackborn627bba72009-03-24 22:32:56 -07001951 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001952 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001953 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001954 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001955 };
1956
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001957 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07001958
1959 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001960 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001961 * the given data connection.
1962 *
1963 * {@hide}
1964 */
1965 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001966 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001969 * Returns the number of times the phone has entered the given data
1970 * connection type.
1971 *
1972 * {@hide}
1973 */
1974 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001975
Dianne Hackborn3251b902014-06-20 14:40:53 -07001976 public static final int WIFI_SUPPL_STATE_INVALID = 0;
1977 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
1978 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
1979 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
1980 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
1981 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
1982 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
1983 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
1984 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
1985 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
1986 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
1987 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
1988 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
1989
1990 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
1991
1992 static final String[] WIFI_SUPPL_STATE_NAMES = {
1993 "invalid", "disconn", "disabled", "inactive", "scanning",
1994 "authenticating", "associating", "associated", "4-way-handshake",
1995 "group-handshake", "completed", "dormant", "uninit"
1996 };
1997
1998 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
1999 "inv", "dsc", "dis", "inact", "scan",
2000 "auth", "ascing", "asced", "4-way",
2001 "group", "compl", "dorm", "uninit"
2002 };
2003
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002004 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2005 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002006 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002007 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2008 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002009 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002010 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2011 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2012 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002013 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002014 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002015 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002016 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002017 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2018 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002019 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2020 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2021 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2022 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2023 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2024 new String[] {"in", "out", "emergency", "off"},
2025 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002026 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2027 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2028 SignalStrength.SIGNAL_STRENGTH_NAMES,
2029 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002030 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2031 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2032 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002033 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002034
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002035 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2036 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002037 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002038 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002039 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002040 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002041 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002042 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2043 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2044 new String[] { "off", "light", "full", "???" },
2045 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002046 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2047 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2048 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002049 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2050 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2051 new String[] { "0", "1", "2", "3", "4" },
2052 new String[] { "0", "1", "2", "3", "4" }),
2053 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2054 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2055 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002056 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002057 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002058 };
2059
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002060 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002061 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002062 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2063 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002064 };
2065
2066 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002067 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002068 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002069 "Esw", "Ewa", "Elw", "Eec"
2070 };
2071
2072 @FunctionalInterface
2073 public interface IntToString {
2074 String applyAsString(int val);
2075 }
2076
2077 private static final IntToString sUidToString = UserHandle::formatUid;
2078 private static final IntToString sIntToString = Integer::toString;
2079
2080 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2081 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2082 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2083 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2084 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002085 };
2086
Dianne Hackborn617f8772009-03-31 15:04:46 -07002087 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002088 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002089 * running on battery.
2090 *
2091 * {@hide}
2092 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002093 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002094
2095 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002096 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002097 * been in the running state while the device was running on battery.
2098 *
2099 * {@hide}
2100 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002101 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002102
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002103 public static final int WIFI_STATE_OFF = 0;
2104 public static final int WIFI_STATE_OFF_SCANNING = 1;
2105 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2106 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2107 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2108 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2109 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2110 public static final int WIFI_STATE_SOFT_AP = 7;
2111
2112 static final String[] WIFI_STATE_NAMES = {
2113 "off", "scanning", "no_net", "disconn",
2114 "sta", "p2p", "sta_p2p", "soft_ap"
2115 };
2116
2117 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2118
2119 /**
2120 * Returns the time in microseconds that WiFi has been running in the given state.
2121 *
2122 * {@hide}
2123 */
2124 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002125 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002126
2127 /**
2128 * Returns the number of times that WiFi has entered the given state.
2129 *
2130 * {@hide}
2131 */
2132 public abstract int getWifiStateCount(int wifiState, int which);
2133
The Android Open Source Project10592532009-03-18 17:39:46 -07002134 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002135 * Returns the time in microseconds that the wifi supplicant has been
2136 * in a given state.
2137 *
2138 * {@hide}
2139 */
2140 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2141
2142 /**
2143 * Returns the number of times that the wifi supplicant has transitioned
2144 * to a given state.
2145 *
2146 * {@hide}
2147 */
2148 public abstract int getWifiSupplStateCount(int state, int which);
2149
2150 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2151
2152 /**
2153 * Returns the time in microseconds that WIFI has been running with
2154 * the given signal strength.
2155 *
2156 * {@hide}
2157 */
2158 public abstract long getWifiSignalStrengthTime(int strengthBin,
2159 long elapsedRealtimeUs, int which);
2160
2161 /**
2162 * Returns the number of times WIFI has entered the given signal strength.
2163 *
2164 * {@hide}
2165 */
2166 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2167
2168 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002169 * Returns the time in microseconds that the flashlight has been on while the device was
2170 * running on battery.
2171 *
2172 * {@hide}
2173 */
2174 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2175
2176 /**
2177 * Returns the number of times that the flashlight has been turned on while the device was
2178 * running on battery.
2179 *
2180 * {@hide}
2181 */
2182 public abstract long getFlashlightOnCount(int which);
2183
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002184 /**
2185 * Returns the time in microseconds that the camera has been on while the device was
2186 * running on battery.
2187 *
2188 * {@hide}
2189 */
2190 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2191
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002192 /**
2193 * Returns the time in microseconds that bluetooth scans were running while the device was
2194 * on battery.
2195 *
2196 * {@hide}
2197 */
2198 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002199
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002200 public static final int NETWORK_MOBILE_RX_DATA = 0;
2201 public static final int NETWORK_MOBILE_TX_DATA = 1;
2202 public static final int NETWORK_WIFI_RX_DATA = 2;
2203 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002204 public static final int NETWORK_BT_RX_DATA = 4;
2205 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002206 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2207 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2208 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2209 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2210 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002211
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002212 public abstract long getNetworkActivityBytes(int type, int which);
2213 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002214
Adam Lesinskie08af192015-03-25 16:42:59 -07002215 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002216 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002217 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002218 * actual power data.
2219 */
2220 public abstract boolean hasWifiActivityReporting();
2221
2222 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002223 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2224 * in various radio controller states, such as transmit, receive, and idle.
2225 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002226 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002227 public abstract ControllerActivityCounter getWifiControllerActivity();
2228
2229 /**
2230 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2231 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2232 * actual power data.
2233 */
2234 public abstract boolean hasBluetoothActivityReporting();
2235
2236 /**
2237 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2238 * in various radio controller states, such as transmit, receive, and idle.
2239 * @return non-null {@link ControllerActivityCounter}
2240 */
2241 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2242
2243 /**
2244 * Returns true if the BatteryStats object has detailed modem power reports.
2245 * When true, calling {@link #getModemControllerActivity()} will yield the
2246 * actual power data.
2247 */
2248 public abstract boolean hasModemActivityReporting();
2249
2250 /**
2251 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2252 * in various radio controller states, such as transmit, receive, and idle.
2253 * @return non-null {@link ControllerActivityCounter}
2254 */
2255 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002256
The Android Open Source Project10592532009-03-18 17:39:46 -07002257 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002258 * Return the wall clock time when battery stats data collection started.
2259 */
2260 public abstract long getStartClockTime();
2261
2262 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002263 * Return platform version tag that we were running in when the battery stats started.
2264 */
2265 public abstract String getStartPlatformVersion();
2266
2267 /**
2268 * Return platform version tag that we were running in when the battery stats ended.
2269 */
2270 public abstract String getEndPlatformVersion();
2271
2272 /**
2273 * Return the internal version code of the parcelled format.
2274 */
2275 public abstract int getParcelVersion();
2276
2277 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 * Return whether we are currently running on battery.
2279 */
2280 public abstract boolean getIsOnBattery();
2281
2282 /**
2283 * Returns a SparseArray containing the statistics for each uid.
2284 */
2285 public abstract SparseArray<? extends Uid> getUidStats();
2286
2287 /**
2288 * Returns the current battery uptime in microseconds.
2289 *
2290 * @param curTime the amount of elapsed realtime in microseconds.
2291 */
2292 public abstract long getBatteryUptime(long curTime);
2293
2294 /**
2295 * Returns the current battery realtime in microseconds.
2296 *
2297 * @param curTime the amount of elapsed realtime in microseconds.
2298 */
2299 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002300
2301 /**
Evan Millar633a1742009-04-02 16:36:33 -07002302 * Returns the battery percentage level at the last time the device was unplugged from power, or
2303 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002304 */
Evan Millar633a1742009-04-02 16:36:33 -07002305 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002306
2307 /**
Evan Millar633a1742009-04-02 16:36:33 -07002308 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2309 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002310 */
Evan Millar633a1742009-04-02 16:36:33 -07002311 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312
2313 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002314 * Get the amount the battery has discharged since the stats were
2315 * last reset after charging, as a lower-end approximation.
2316 */
2317 public abstract int getLowDischargeAmountSinceCharge();
2318
2319 /**
2320 * Get the amount the battery has discharged since the stats were
2321 * last reset after charging, as an upper-end approximation.
2322 */
2323 public abstract int getHighDischargeAmountSinceCharge();
2324
2325 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002326 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2327 */
2328 public abstract int getDischargeAmount(int which);
2329
2330 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002331 * Get the amount the battery has discharged while the screen was on,
2332 * since the last time power was unplugged.
2333 */
2334 public abstract int getDischargeAmountScreenOn();
2335
2336 /**
2337 * Get the amount the battery has discharged while the screen was on,
2338 * since the last time the device was charged.
2339 */
2340 public abstract int getDischargeAmountScreenOnSinceCharge();
2341
2342 /**
2343 * Get the amount the battery has discharged while the screen was off,
2344 * since the last time power was unplugged.
2345 */
2346 public abstract int getDischargeAmountScreenOff();
2347
2348 /**
2349 * Get the amount the battery has discharged while the screen was off,
2350 * since the last time the device was charged.
2351 */
2352 public abstract int getDischargeAmountScreenOffSinceCharge();
2353
2354 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 * Returns the total, last, or current battery uptime in microseconds.
2356 *
2357 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002358 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 */
2360 public abstract long computeBatteryUptime(long curTime, int which);
2361
2362 /**
2363 * Returns the total, last, or current battery realtime in microseconds.
2364 *
2365 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002366 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 */
2368 public abstract long computeBatteryRealtime(long curTime, int which);
2369
2370 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002371 * Returns the total, last, or current battery screen off uptime in microseconds.
2372 *
2373 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002374 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002375 */
2376 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2377
2378 /**
2379 * Returns the total, last, or current battery screen off realtime in microseconds.
2380 *
2381 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002382 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002383 */
2384 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2385
2386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 * Returns the total, last, or current uptime in microseconds.
2388 *
2389 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002390 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 */
2392 public abstract long computeUptime(long curTime, int which);
2393
2394 /**
2395 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002396 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002398 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 */
2400 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002401
2402 /**
2403 * Compute an approximation for how much run time (in microseconds) is remaining on
2404 * the battery. Returns -1 if no time can be computed: either there is not
2405 * enough current data to make a decision, or the battery is currently
2406 * charging.
2407 *
2408 * @param curTime The current elepsed realtime in microseconds.
2409 */
2410 public abstract long computeBatteryTimeRemaining(long curTime);
2411
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002412 // The part of a step duration that is the actual time.
2413 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2414
2415 // Bits in a step duration that are the new battery level we are at.
2416 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002417 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002418
2419 // Bits in a step duration that are the initial mode we were in at that step.
2420 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002421 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002422
2423 // Bits in a step duration that indicate which modes changed during that step.
2424 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002425 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002426
2427 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2428 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2429
Santos Cordone94f0502017-02-24 12:31:20 -08002430 // The largest value for screen state that is tracked in battery states. Any values above
2431 // this should be mapped back to one of the tracked values before being tracked here.
2432 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2433
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002434 // Step duration mode: power save is on.
2435 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2436
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002437 // Step duration mode: device is currently in idle mode.
2438 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2439
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002440 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2441 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002442 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2443 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002444 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2445 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2446 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2447 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2448 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002449 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2450 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002451 };
2452 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2453 (Display.STATE_OFF-1),
2454 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002455 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002456 (Display.STATE_ON-1),
2457 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2458 (Display.STATE_DOZE-1),
2459 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2460 (Display.STATE_DOZE_SUSPEND-1),
2461 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002462 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002463 };
2464 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2465 "screen off",
2466 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002467 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002468 "screen on",
2469 "screen on power save",
2470 "screen doze",
2471 "screen doze power save",
2472 "screen doze-suspend",
2473 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002474 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002475 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002476
2477 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002478 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2479 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2480 * a coulomb counter.
2481 */
2482 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2483
2484 /**
2485 * Return the counter keeping track of the amount of battery discharge measured in
2486 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2487 * a coulomb counter.
2488 */
2489 public abstract LongCounter getDischargeCoulombCounter();
2490
2491 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002492 * Returns the estimated real battery capacity, which may be less than the capacity
2493 * declared by the PowerProfile.
2494 * @return The estimated battery capacity in mAh.
2495 */
2496 public abstract int getEstimatedBatteryCapacity();
2497
2498 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002499 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002500 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002501 public abstract LevelStepTracker getDischargeLevelStepTracker();
2502
2503 /**
2504 * Return the array of daily discharge step durations.
2505 */
2506 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002507
2508 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002509 * Compute an approximation for how much time (in microseconds) remains until the battery
2510 * is fully charged. Returns -1 if no time can be computed: either there is not
2511 * enough current data to make a decision, or the battery is currently
2512 * discharging.
2513 *
2514 * @param curTime The current elepsed realtime in microseconds.
2515 */
2516 public abstract long computeChargeTimeRemaining(long curTime);
2517
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002518 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002519 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002520 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002521 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002522
2523 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002524 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002525 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002526 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002527
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002528 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2529
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002530 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002531
Evan Millarc64edde2009-04-18 12:26:32 -07002532 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533
James Carr2dd7e5e2016-07-20 18:48:39 -07002534 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2535
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002536 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2537
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002538 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 long days = seconds / (60 * 60 * 24);
2540 if (days != 0) {
2541 out.append(days);
2542 out.append("d ");
2543 }
2544 long used = days * 60 * 60 * 24;
2545
2546 long hours = (seconds - used) / (60 * 60);
2547 if (hours != 0 || used != 0) {
2548 out.append(hours);
2549 out.append("h ");
2550 }
2551 used += hours * 60 * 60;
2552
2553 long mins = (seconds-used) / 60;
2554 if (mins != 0 || used != 0) {
2555 out.append(mins);
2556 out.append("m ");
2557 }
2558 used += mins * 60;
2559
2560 if (seconds != 0 || used != 0) {
2561 out.append(seconds-used);
2562 out.append("s ");
2563 }
2564 }
2565
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002566 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002568 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 sb.append(time - (sec * 1000));
2570 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 }
2572
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002573 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002574 long sec = time / 1000;
2575 formatTimeRaw(sb, sec);
2576 sb.append(time - (sec * 1000));
2577 sb.append("ms");
2578 }
2579
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002580 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002582 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 }
2584 float perc = ((float)num) / ((float)den) * 100;
2585 mFormatBuilder.setLength(0);
2586 mFormatter.format("%.1f%%", perc);
2587 return mFormatBuilder.toString();
2588 }
2589
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002590 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002591 mFormatBuilder.setLength(0);
2592
2593 if (bytes < BYTES_PER_KB) {
2594 return bytes + "B";
2595 } else if (bytes < BYTES_PER_MB) {
2596 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2597 return mFormatBuilder.toString();
2598 } else if (bytes < BYTES_PER_GB){
2599 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2600 return mFormatBuilder.toString();
2601 } else {
2602 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2603 return mFormatBuilder.toString();
2604 }
2605 }
2606
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002607 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002608 if (timer != null) {
2609 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002610 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002611 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2612 return totalTimeMillis;
2613 }
2614 return 0;
2615 }
2616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 /**
2618 *
2619 * @param sb a StringBuilder object.
2620 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002621 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002623 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 * @param linePrefix a String to be prepended to each line of output.
2625 * @return the line prefix
2626 */
2627 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002628 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002631 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002632
Evan Millarc64edde2009-04-18 12:26:32 -07002633 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 if (totalTimeMillis != 0) {
2635 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002636 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002637 if (name != null) {
2638 sb.append(name);
2639 sb.append(' ');
2640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 sb.append('(');
2642 sb.append(count);
2643 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002644 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2645 if (maxDurationMs >= 0) {
2646 sb.append(" max=");
2647 sb.append(maxDurationMs);
2648 }
2649 if (timer.isRunningLocked()) {
2650 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2651 if (currentMs >= 0) {
2652 sb.append(" (running for ");
2653 sb.append(currentMs);
2654 sb.append("ms)");
2655 } else {
2656 sb.append(" (running)");
2657 }
2658 }
2659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 return ", ";
2661 }
2662 }
2663 return linePrefix;
2664 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002665
2666 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002667 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002668 *
2669 * @param pw a PrintWriter object to print to.
2670 * @param sb a StringBuilder object.
2671 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002672 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002673 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2674 * @param prefix a String to be prepended to each line of output.
2675 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002676 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002677 */
2678 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002679 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002680 if (timer != null) {
2681 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002682 final long totalTimeMs = (timer.getTotalTimeLocked(
2683 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002684 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002685 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002686 sb.setLength(0);
2687 sb.append(prefix);
2688 sb.append(" ");
2689 sb.append(type);
2690 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002691 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002692 sb.append("realtime (");
2693 sb.append(count);
2694 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002695 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2696 if (maxDurationMs >= 0) {
2697 sb.append(" max=");
2698 sb.append(maxDurationMs);
2699 }
2700 if (timer.isRunningLocked()) {
2701 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2702 if (currentMs >= 0) {
2703 sb.append(" (running for ");
2704 sb.append(currentMs);
2705 sb.append("ms)");
2706 } else {
2707 sb.append(" (running)");
2708 }
2709 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002710 pw.println(sb.toString());
2711 return true;
2712 }
2713 }
2714 return false;
2715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716
2717 /**
2718 * Checkin version of wakelock printer. Prints simple comma-separated list.
2719 *
2720 * @param sb a StringBuilder object.
2721 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002722 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002724 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 * @param linePrefix a String to be prepended to each line of output.
2726 * @return the line prefix
2727 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002728 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2729 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 long totalTimeMicros = 0;
2731 int count = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002732 long max = -1;
2733 long current = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002735 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Evan Millarc64edde2009-04-18 12:26:32 -07002736 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002737 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2738 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 }
2740 sb.append(linePrefix);
2741 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2742 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002743 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002745 sb.append(',');
2746 sb.append(current);
2747 sb.append(',');
2748 sb.append(max);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 return ",";
2750 }
2751
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002752 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2753 String type) {
2754 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2755 pw.print(',');
2756 pw.print(uid);
2757 pw.print(',');
2758 pw.print(category);
2759 pw.print(',');
2760 pw.print(type);
2761 }
2762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 /**
2764 * Dump a comma-separated line of values for terse checkin mode.
2765 *
2766 * @param pw the PageWriter to dump log to
2767 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2768 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2769 * @param args type-dependent data arguments
2770 */
2771 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2772 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002773 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002774 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002775 pw.print(',');
2776 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002778 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002780
2781 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002782 * Dump a given timer stat for terse checkin mode.
2783 *
2784 * @param pw the PageWriter to dump log to
2785 * @param uid the UID to log
2786 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2787 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2788 * @param timer a {@link Timer} to dump stats for
2789 * @param rawRealtime the current elapsed realtime of the system in microseconds
2790 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2791 */
2792 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2793 Timer timer, long rawRealtime, int which) {
2794 if (timer != null) {
2795 // Convert from microseconds to milliseconds with rounding
2796 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2797 / 1000;
2798 final int count = timer.getCountLocked(which);
2799 if (totalTime != 0) {
2800 dumpLine(pw, uid, category, type, totalTime, count);
2801 }
2802 }
2803 }
2804
2805 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002806 * Checks if the ControllerActivityCounter has any data worth dumping.
2807 */
2808 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2809 if (counter == null) {
2810 return false;
2811 }
2812
2813 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2814 || counter.getRxTimeCounter().getCountLocked(which) != 0
2815 || counter.getPowerCounter().getCountLocked(which) != 0) {
2816 return true;
2817 }
2818
2819 for (LongCounter c : counter.getTxTimeCounters()) {
2820 if (c.getCountLocked(which) != 0) {
2821 return true;
2822 }
2823 }
2824 return false;
2825 }
2826
2827 /**
2828 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2829 * The order of the arguments in the final check in line is:
2830 *
2831 * idle, rx, power, tx...
2832 *
2833 * where tx... is one or more transmit level times.
2834 */
2835 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2836 String type,
2837 ControllerActivityCounter counter,
2838 int which) {
2839 if (!controllerActivityHasData(counter, which)) {
2840 return;
2841 }
2842
2843 dumpLineHeader(pw, uid, category, type);
2844 pw.print(",");
2845 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2846 pw.print(",");
2847 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2848 pw.print(",");
2849 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2850 for (LongCounter c : counter.getTxTimeCounters()) {
2851 pw.print(",");
2852 pw.print(c.getCountLocked(which));
2853 }
2854 pw.println();
2855 }
2856
2857 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2858 String prefix, String controllerName,
2859 ControllerActivityCounter counter,
2860 int which) {
2861 if (controllerActivityHasData(counter, which)) {
2862 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2863 }
2864 }
2865
2866 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2867 String controllerName,
2868 ControllerActivityCounter counter, int which) {
2869 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2870 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2871 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2872 long totalTxTimeMs = 0;
2873 for (LongCounter txState : counter.getTxTimeCounters()) {
2874 totalTxTimeMs += txState.getCountLocked(which);
2875 }
2876
2877 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2878
2879 sb.setLength(0);
2880 sb.append(prefix);
2881 sb.append(" ");
2882 sb.append(controllerName);
2883 sb.append(" Idle time: ");
2884 formatTimeMs(sb, idleTimeMs);
2885 sb.append("(");
2886 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2887 sb.append(")");
2888 pw.println(sb.toString());
2889
2890 sb.setLength(0);
2891 sb.append(prefix);
2892 sb.append(" ");
2893 sb.append(controllerName);
2894 sb.append(" Rx time: ");
2895 formatTimeMs(sb, rxTimeMs);
2896 sb.append("(");
2897 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2898 sb.append(")");
2899 pw.println(sb.toString());
2900
2901 sb.setLength(0);
2902 sb.append(prefix);
2903 sb.append(" ");
2904 sb.append(controllerName);
2905 sb.append(" Tx time: ");
2906 formatTimeMs(sb, totalTxTimeMs);
2907 sb.append("(");
2908 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2909 sb.append(")");
2910 pw.println(sb.toString());
2911
2912 final int numTxLvls = counter.getTxTimeCounters().length;
2913 if (numTxLvls > 1) {
2914 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2915 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2916 sb.setLength(0);
2917 sb.append(prefix);
2918 sb.append(" [");
2919 sb.append(lvl);
2920 sb.append("] ");
2921 formatTimeMs(sb, txLvlTimeMs);
2922 sb.append("(");
2923 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2924 sb.append(")");
2925 pw.println(sb.toString());
2926 }
2927 }
2928
2929 sb.setLength(0);
2930 sb.append(prefix);
2931 sb.append(" ");
2932 sb.append(controllerName);
2933 sb.append(" Power drain: ").append(
2934 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
2935 sb.append("mAh");
2936 pw.println(sb.toString());
2937 }
2938
2939 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07002940 * Temporary for settings.
2941 */
2942 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
2943 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
2944 }
2945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 /**
2947 * Checkin server version of dump to produce more compact, computer-readable log.
2948 *
2949 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07002951 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
2952 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 final long rawUptime = SystemClock.uptimeMillis() * 1000;
2954 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
2955 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
2957 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002958 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
2959 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
2960 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 final long totalRealtime = computeRealtime(rawRealtime, which);
2962 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002963 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002964 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002965 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002966 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
2967 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002968 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002969 rawRealtime, which);
2970 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
2971 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002972 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002973 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002974 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002975 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002976 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
2977 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
2978 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002979
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002980 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002982 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07002983 final int NU = uidStats.size();
2984
Dianne Hackborn1e725a72015-03-24 18:23:19 -07002985 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 // Dump "battery" stat
2988 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002989 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07002990 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002991 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002992 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002993 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
2994 getEstimatedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07002995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002997 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07002998 long fullWakeLockTimeTotal = 0;
2999 long partialWakeLockTimeTotal = 0;
3000
3001 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003002 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003003
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003004 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3005 = u.getWakelockStats();
3006 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3007 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003008
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003009 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3010 if (fullWakeTimer != null) {
3011 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3012 which);
3013 }
3014
3015 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3016 if (partialWakeTimer != null) {
3017 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3018 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003019 }
3020 }
3021 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003022
3023 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003024 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3025 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3026 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3027 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3028 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3029 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3030 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3031 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003032 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3033 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003034 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3035 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003036 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3037 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003038
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003039 // Dump Modem controller stats
3040 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3041 getModemControllerActivity(), which);
3042
Adam Lesinskie283d332015-04-16 12:29:25 -07003043 // Dump Wifi controller stats
3044 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3045 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003046 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003047 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003048
3049 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3050 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003051
3052 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003053 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3054 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 // Dump misc stats
3057 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003058 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003059 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003060 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003061 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003062 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003063 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3064 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003065 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003066 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3067 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3068 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3069 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003070 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003071
3072 // Dump screen brightness stats
3073 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3074 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003075 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003076 }
3077 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07003078
Dianne Hackborn627bba72009-03-24 22:32:56 -07003079 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003080 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3081 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003082 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003083 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003084 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003085 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003086 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003087 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003088 args[i] = getPhoneSignalStrengthCount(i, which);
3089 }
3090 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003091
Dianne Hackborn627bba72009-03-24 22:32:56 -07003092 // Dump network type stats
3093 args = new Object[NUM_DATA_CONNECTION_TYPES];
3094 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003095 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003096 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003097 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3098 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3099 args[i] = getPhoneDataConnectionCount(i, which);
3100 }
3101 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003102
3103 // Dump wifi state stats
3104 args = new Object[NUM_WIFI_STATES];
3105 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003106 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003107 }
3108 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3109 for (int i=0; i<NUM_WIFI_STATES; i++) {
3110 args[i] = getWifiStateCount(i, which);
3111 }
3112 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3113
Dianne Hackborn3251b902014-06-20 14:40:53 -07003114 // Dump wifi suppl state stats
3115 args = new Object[NUM_WIFI_SUPPL_STATES];
3116 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3117 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3118 }
3119 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3120 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3121 args[i] = getWifiSupplStateCount(i, which);
3122 }
3123 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3124
3125 // Dump wifi signal strength stats
3126 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3127 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3128 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3129 }
3130 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3131 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3132 args[i] = getWifiSignalStrengthCount(i, which);
3133 }
3134 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3135
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003136 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003137 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003138 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003141 if (which == STATS_SINCE_UNPLUGGED) {
3142 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3143 getDischargeStartLevel()-getDischargeCurrentLevel(),
3144 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003145 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3146 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003147 } else {
3148 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3149 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003150 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003151 getDischargeAmountScreenOffSinceCharge(),
3152 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003153 }
3154
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003155 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003156 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003157 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003158 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003159 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003160 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003161 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3162 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003163 }
Evan Millarc64edde2009-04-18 12:26:32 -07003164 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003165 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003166 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003167 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3168 // Not doing the regular wake lock formatting to remain compatible
3169 // with the old checkin format.
3170 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3171 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003172 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003173 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003174 }
3175 }
Evan Millarc64edde2009-04-18 12:26:32 -07003176 }
3177
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003178 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003179 helper.create(this);
3180 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003181 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003182 if (sippers != null && sippers.size() > 0) {
3183 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3184 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003185 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003186 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3187 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3188 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003189 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003190 int uid = 0;
3191 String label;
3192 switch (bs.drainType) {
3193 case IDLE:
3194 label="idle";
3195 break;
3196 case CELL:
3197 label="cell";
3198 break;
3199 case PHONE:
3200 label="phone";
3201 break;
3202 case WIFI:
3203 label="wifi";
3204 break;
3205 case BLUETOOTH:
3206 label="blue";
3207 break;
3208 case SCREEN:
3209 label="scrn";
3210 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003211 case FLASHLIGHT:
3212 label="flashlight";
3213 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003214 case APP:
3215 uid = bs.uidObj.getUid();
3216 label = "uid";
3217 break;
3218 case USER:
3219 uid = UserHandle.getUid(bs.userId, 0);
3220 label = "user";
3221 break;
3222 case UNACCOUNTED:
3223 label = "unacc";
3224 break;
3225 case OVERCOUNTED:
3226 label = "over";
3227 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003228 case CAMERA:
3229 label = "camera";
3230 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003231 default:
3232 label = "???";
3233 }
3234 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003235 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003236 }
3237 }
3238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 for (int iu = 0; iu < NU; iu++) {
3240 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003241 if (reqUid >= 0 && uid != reqUid) {
3242 continue;
3243 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003244 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003247 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3248 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3249 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3250 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3251 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3252 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3253 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3254 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003255 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003256 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3257 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003258 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003259 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3260 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003261 // Background data transfers
3262 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3263 which);
3264 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3265 which);
3266 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3267 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3268 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3269 which);
3270 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3271 which);
3272 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3273 which);
3274 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3275 which);
3276
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003277 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3278 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003279 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003280 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3281 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3282 || wifiBytesBgTx > 0
3283 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3284 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003285 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3286 wifiBytesRx, wifiBytesTx,
3287 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003288 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003289 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003290 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3291 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3292 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3293 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003294 }
3295
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003296 // Dump modem controller data, per UID.
3297 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3298 u.getModemControllerActivity(), which);
3299
3300 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003301 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3302 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3303 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003304 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3305 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003306 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3307 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3308 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003309 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003310 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003311 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3312 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003313 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3314 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003315 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003316 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003319 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3320 u.getWifiControllerActivity(), which);
3321
Bookatz867c0d72017-03-07 18:23:42 -08003322 final Timer bleTimer = u.getBluetoothScanTimer();
3323 if (bleTimer != null) {
3324 // Convert from microseconds to milliseconds with rounding
3325 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3326 / 1000;
3327 if (totalTime != 0) {
3328 final int count = bleTimer.getCountLocked(which);
3329 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3330 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
3331 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3332 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3333 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3334 final long actualTimeBg = bleTimerBg != null ?
3335 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3336 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
3337 countBg, actualTime, actualTimeBg);
3338 }
3339 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003340
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003341 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3342 u.getBluetoothControllerActivity(), which);
3343
Dianne Hackborn617f8772009-03-31 15:04:46 -07003344 if (u.hasUserActivity()) {
3345 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3346 boolean hasData = false;
3347 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3348 int val = u.getUserActivityCount(i, which);
3349 args[i] = val;
3350 if (val != 0) hasData = true;
3351 }
3352 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003353 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003354 }
3355 }
3356
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003357 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3358 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3359 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3360 String linePrefix = "";
3361 sb.setLength(0);
3362 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3363 rawRealtime, "f", which, linePrefix);
3364 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3365 rawRealtime, "p", which, linePrefix);
3366 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3367 rawRealtime, "w", which, linePrefix);
3368
3369 // Only log if we had at lease one wakelock...
3370 if (sb.length() > 0) {
3371 String name = wakelocks.keyAt(iw);
3372 if (name.indexOf(',') >= 0) {
3373 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003375 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 }
3377 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003378
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003379 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3380 for (int isy=syncs.size()-1; isy>=0; isy--) {
3381 final Timer timer = syncs.valueAt(isy);
3382 // Convert from microseconds to milliseconds with rounding
3383 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3384 final int count = timer.getCountLocked(which);
3385 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003386 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
3387 totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003388 }
3389 }
3390
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003391 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3392 for (int ij=jobs.size()-1; ij>=0; ij--) {
3393 final Timer timer = jobs.valueAt(ij);
3394 // Convert from microseconds to milliseconds with rounding
3395 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3396 final int count = timer.getCountLocked(which);
3397 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003398 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
3399 totalTime, count);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003400 }
3401 }
3402
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003403 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3404 rawRealtime, which);
3405 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3406 rawRealtime, which);
3407 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3408 rawRealtime, which);
3409 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3410 rawRealtime, which);
3411
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003412 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3413 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003414 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003415 final Uid.Sensor se = sensors.valueAt(ise);
3416 final int sensorNumber = sensors.keyAt(ise);
3417 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003418 if (timer != null) {
3419 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003420 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3421 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003422 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003423 final int count = timer.getCountLocked(which);
3424 final Timer bgTimer = se.getSensorBackgroundTime();
3425 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
3426 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3427 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3428 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3429 final long bgActualTime = bgTimer != null ?
3430 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3431 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3432 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 }
3435 }
3436
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003437 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3438 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003439
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003440 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3441 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003442
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003443 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003444 long totalStateTime = 0;
3445 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003446 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3447 totalStateTime += time;
3448 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003449 }
3450 if (totalStateTime > 0) {
3451 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3452 }
3453
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003454 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3455 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
3456 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
3457 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
3458 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
3459 powerCpuMaUs / 1000);
3460 }
3461
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003462 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3463 = u.getProcessStats();
3464 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3465 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003466
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003467 final long userMillis = ps.getUserTime(which);
3468 final long systemMillis = ps.getSystemTime(which);
3469 final long foregroundMillis = ps.getForegroundTime(which);
3470 final int starts = ps.getStarts(which);
3471 final int numCrashes = ps.getNumCrashes(which);
3472 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003473
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003474 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3475 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003476 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3477 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 }
3479 }
3480
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003481 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3482 = u.getPackageStats();
3483 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3484 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3485 int wakeups = 0;
3486 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3487 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003488 int count = alarms.valueAt(iwa).getCountLocked(which);
3489 wakeups += count;
3490 String name = alarms.keyAt(iwa).replace(',', '_');
3491 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003492 }
3493 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3494 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3495 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3496 final long startTime = ss.getStartTime(batteryUptime, which);
3497 final int starts = ss.getStarts(which);
3498 final int launches = ss.getLaunches(which);
3499 if (startTime != 0 || starts != 0 || launches != 0) {
3500 dumpLine(pw, uid, category, APK_DATA,
3501 wakeups, // wakeup alarms
3502 packageStats.keyAt(ipkg), // Apk
3503 serviceStats.keyAt(isvc), // service
3504 startTime / 1000, // time spent started, in ms
3505 starts,
3506 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 }
3508 }
3509 }
3510 }
3511 }
3512
Dianne Hackborn81038902012-11-26 17:04:09 -08003513 static final class TimerEntry {
3514 final String mName;
3515 final int mId;
3516 final BatteryStats.Timer mTimer;
3517 final long mTime;
3518 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3519 mName = name;
3520 mId = id;
3521 mTimer = timer;
3522 mTime = time;
3523 }
3524 }
3525
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003526 private void printmAh(PrintWriter printer, double power) {
3527 printer.print(BatteryStatsHelper.makemAh(power));
3528 }
3529
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003530 private void printmAh(StringBuilder sb, double power) {
3531 sb.append(BatteryStatsHelper.makemAh(power));
3532 }
3533
Dianne Hackbornd953c532014-08-16 18:17:38 -07003534 /**
3535 * Temporary for settings.
3536 */
3537 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3538 int reqUid) {
3539 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3540 }
3541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003543 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003544 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3546 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3547 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548
3549 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3550 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3551 final long totalRealtime = computeRealtime(rawRealtime, which);
3552 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003553 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3554 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3555 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003556 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3557 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003558
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003559 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003560
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003561 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003562 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003564 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3565 if (estimatedBatteryCapacity > 0) {
3566 sb.setLength(0);
3567 sb.append(prefix);
3568 sb.append(" Estimated battery capacity: ");
3569 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3570 sb.append(" mAh");
3571 pw.println(sb.toString());
3572 }
3573
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003574 sb.setLength(0);
3575 sb.append(prefix);
3576 sb.append(" Time on battery: ");
3577 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3578 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3579 sb.append(") realtime, ");
3580 formatTimeMs(sb, whichBatteryUptime / 1000);
3581 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3582 sb.append(") uptime");
3583 pw.println(sb.toString());
3584 sb.setLength(0);
3585 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003586 sb.append(" Time on battery screen off: ");
3587 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3588 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3589 sb.append(") realtime, ");
3590 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3591 sb.append("(");
3592 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3593 sb.append(") uptime");
3594 pw.println(sb.toString());
3595 sb.setLength(0);
3596 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003597 sb.append(" Total run time: ");
3598 formatTimeMs(sb, totalRealtime / 1000);
3599 sb.append("realtime, ");
3600 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003601 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003602 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003603 if (batteryTimeRemaining >= 0) {
3604 sb.setLength(0);
3605 sb.append(prefix);
3606 sb.append(" Battery time remaining: ");
3607 formatTimeMs(sb, batteryTimeRemaining / 1000);
3608 pw.println(sb.toString());
3609 }
3610 if (chargeTimeRemaining >= 0) {
3611 sb.setLength(0);
3612 sb.append(prefix);
3613 sb.append(" Charge time remaining: ");
3614 formatTimeMs(sb, chargeTimeRemaining / 1000);
3615 pw.println(sb.toString());
3616 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003617
3618 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3619 final long dischargeCount = dischargeCounter.getCountLocked(which);
3620 if (dischargeCount >= 0) {
3621 sb.setLength(0);
3622 sb.append(prefix);
3623 sb.append(" Discharge: ");
3624 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3625 sb.append(" mAh");
3626 pw.println(sb.toString());
3627 }
3628
3629 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3630 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3631 if (dischargeScreenOffCount >= 0) {
3632 sb.setLength(0);
3633 sb.append(prefix);
3634 sb.append(" Screen off discharge: ");
3635 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3636 sb.append(" mAh");
3637 pw.println(sb.toString());
3638 }
3639
3640 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3641 if (dischargeScreenOnCount >= 0) {
3642 sb.setLength(0);
3643 sb.append(prefix);
3644 sb.append(" Screen on discharge: ");
3645 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3646 sb.append(" mAh");
3647 pw.println(sb.toString());
3648 }
3649
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003650 pw.print(" Start clock time: ");
3651 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3652
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003653 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003654 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003655 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003656 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3657 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003658 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003659 rawRealtime, which);
3660 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3661 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003662 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003663 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003664 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3665 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3666 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003667 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003668 sb.append(prefix);
3669 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3670 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003671 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003672 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3673 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003674 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003675 pw.println(sb.toString());
3676 sb.setLength(0);
3677 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003678 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003679 boolean didOne = false;
3680 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003681 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003682 if (time == 0) {
3683 continue;
3684 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003685 sb.append("\n ");
3686 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003687 didOne = true;
3688 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3689 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003690 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003691 sb.append("(");
3692 sb.append(formatRatioLocked(time, screenOnTime));
3693 sb.append(")");
3694 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003695 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003696 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003697 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003698 sb.setLength(0);
3699 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003700 sb.append(" Power save mode enabled: ");
3701 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003702 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003703 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003704 sb.append(")");
3705 pw.println(sb.toString());
3706 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003707 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003708 sb.setLength(0);
3709 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003710 sb.append(" Device light idling: ");
3711 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003712 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003713 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3714 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003715 sb.append("x");
3716 pw.println(sb.toString());
3717 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003718 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003719 sb.setLength(0);
3720 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003721 sb.append(" Idle mode light time: ");
3722 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003723 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003724 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3725 sb.append(") ");
3726 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003727 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003728 sb.append(" -- longest ");
3729 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3730 pw.println(sb.toString());
3731 }
3732 if (deviceIdlingTime != 0) {
3733 sb.setLength(0);
3734 sb.append(prefix);
3735 sb.append(" Device full idling: ");
3736 formatTimeMs(sb, deviceIdlingTime / 1000);
3737 sb.append("(");
3738 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003739 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003740 sb.append("x");
3741 pw.println(sb.toString());
3742 }
3743 if (deviceIdleModeFullTime != 0) {
3744 sb.setLength(0);
3745 sb.append(prefix);
3746 sb.append(" Idle mode full time: ");
3747 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3748 sb.append("(");
3749 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3750 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003751 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003752 sb.append("x");
3753 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003754 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003755 pw.println(sb.toString());
3756 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003757 if (phoneOnTime != 0) {
3758 sb.setLength(0);
3759 sb.append(prefix);
3760 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3761 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003762 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003763 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003764 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003765 if (connChanges != 0) {
3766 pw.print(prefix);
3767 pw.print(" Connectivity changes: "); pw.println(connChanges);
3768 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003769
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003770 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003771 long fullWakeLockTimeTotalMicros = 0;
3772 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003773
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003774 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003775
Evan Millar22ac0432009-03-31 11:33:18 -07003776 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003777 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003778
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003779 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3780 = u.getWakelockStats();
3781 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3782 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003783
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003784 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3785 if (fullWakeTimer != null) {
3786 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3787 rawRealtime, which);
3788 }
3789
3790 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3791 if (partialWakeTimer != null) {
3792 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3793 rawRealtime, which);
3794 if (totalTimeMicros > 0) {
3795 if (reqUid < 0) {
3796 // Only show the ordered list of all wake
3797 // locks if the caller is not asking for data
3798 // about a specific uid.
3799 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3800 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003801 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003802 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003803 }
3804 }
3805 }
3806 }
3807
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003808 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3809 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3810 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3811 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3812 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3813 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3814 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3815 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003816 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3817 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003818
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003819 if (fullWakeLockTimeTotalMicros != 0) {
3820 sb.setLength(0);
3821 sb.append(prefix);
3822 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3823 (fullWakeLockTimeTotalMicros + 500) / 1000);
3824 pw.println(sb.toString());
3825 }
3826
3827 if (partialWakeLockTimeTotalMicros != 0) {
3828 sb.setLength(0);
3829 sb.append(prefix);
3830 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3831 (partialWakeLockTimeTotalMicros + 500) / 1000);
3832 pw.println(sb.toString());
3833 }
3834
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003835 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003836 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3837 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3838 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3839 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003840 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003841 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003842 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003843 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003844 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003845 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003846 if (time == 0) {
3847 continue;
3848 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003849 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003850 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003851 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003852 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003853 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003854 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003855 sb.append("(");
3856 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003857 sb.append(") ");
3858 sb.append(getPhoneSignalStrengthCount(i, which));
3859 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003860 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003861 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003862 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003863
3864 sb.setLength(0);
3865 sb.append(prefix);
3866 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003867 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003868 pw.println(sb.toString());
3869
Dianne Hackborn627bba72009-03-24 22:32:56 -07003870 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003871 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003872 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003873 didOne = false;
3874 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003875 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003876 if (time == 0) {
3877 continue;
3878 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003879 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003880 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003881 didOne = true;
3882 sb.append(DATA_CONNECTION_NAMES[i]);
3883 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003884 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003885 sb.append("(");
3886 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003887 sb.append(") ");
3888 sb.append(getPhoneDataConnectionCount(i, which));
3889 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003890 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003891 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003892 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003893
3894 sb.setLength(0);
3895 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003896 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003897 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003898 formatTimeMs(sb, mobileActiveTime / 1000);
3899 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
3900 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
3901 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07003902 pw.println(sb.toString());
3903
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003904 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
3905 if (mobileActiveUnknownTime != 0) {
3906 sb.setLength(0);
3907 sb.append(prefix);
3908 sb.append(" Mobile radio active unknown time: ");
3909 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
3910 sb.append("(");
3911 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
3912 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
3913 sb.append("x");
3914 pw.println(sb.toString());
3915 }
3916
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003917 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
3918 if (mobileActiveAdjustedTime != 0) {
3919 sb.setLength(0);
3920 sb.append(prefix);
3921 sb.append(" Mobile radio active adjusted time: ");
3922 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
3923 sb.append("(");
3924 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
3925 sb.append(")");
3926 pw.println(sb.toString());
3927 }
3928
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003929 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
3930
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003931 pw.print(prefix);
3932 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
3933 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
3934 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
3935 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003936 sb.setLength(0);
3937 sb.append(prefix);
3938 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
3939 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
3940 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
3941 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003942 sb.append(")");
3943 pw.println(sb.toString());
3944
3945 sb.setLength(0);
3946 sb.append(prefix);
3947 sb.append(" Wifi states:");
3948 didOne = false;
3949 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003950 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003951 if (time == 0) {
3952 continue;
3953 }
3954 sb.append("\n ");
3955 didOne = true;
3956 sb.append(WIFI_STATE_NAMES[i]);
3957 sb.append(" ");
3958 formatTimeMs(sb, time/1000);
3959 sb.append("(");
3960 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3961 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07003962 sb.append(getWifiStateCount(i, which));
3963 sb.append("x");
3964 }
3965 if (!didOne) sb.append(" (no activity)");
3966 pw.println(sb.toString());
3967
3968 sb.setLength(0);
3969 sb.append(prefix);
3970 sb.append(" Wifi supplicant states:");
3971 didOne = false;
3972 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3973 final long time = getWifiSupplStateTime(i, rawRealtime, which);
3974 if (time == 0) {
3975 continue;
3976 }
3977 sb.append("\n ");
3978 didOne = true;
3979 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
3980 sb.append(" ");
3981 formatTimeMs(sb, time/1000);
3982 sb.append("(");
3983 sb.append(formatRatioLocked(time, whichBatteryRealtime));
3984 sb.append(") ");
3985 sb.append(getWifiSupplStateCount(i, which));
3986 sb.append("x");
3987 }
3988 if (!didOne) sb.append(" (no activity)");
3989 pw.println(sb.toString());
3990
3991 sb.setLength(0);
3992 sb.append(prefix);
3993 sb.append(" Wifi signal levels:");
3994 didOne = false;
3995 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3996 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
3997 if (time == 0) {
3998 continue;
3999 }
4000 sb.append("\n ");
4001 sb.append(prefix);
4002 didOne = true;
4003 sb.append("level(");
4004 sb.append(i);
4005 sb.append(") ");
4006 formatTimeMs(sb, time/1000);
4007 sb.append("(");
4008 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4009 sb.append(") ");
4010 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004011 sb.append("x");
4012 }
4013 if (!didOne) sb.append(" (no activity)");
4014 pw.println(sb.toString());
4015
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004016 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004017
Adam Lesinski50e47602015-12-04 17:04:54 -08004018 pw.print(prefix);
4019 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4020 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4021
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004022 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4023 sb.setLength(0);
4024 sb.append(prefix);
4025 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4026 pw.println(sb.toString());
4027
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004028 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4029 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004030
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004031 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004032
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004033 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004034 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004035 pw.print(prefix); pw.println(" Device is currently unplugged");
4036 pw.print(prefix); pw.print(" Discharge cycle start level: ");
4037 pw.println(getDischargeStartLevel());
4038 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4039 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004040 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004041 pw.print(prefix); pw.println(" Device is currently plugged into power");
4042 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
4043 pw.println(getDischargeStartLevel());
4044 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
4045 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004046 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004047 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4048 pw.println(getDischargeAmountScreenOn());
4049 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4050 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004051 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004052 } else {
4053 pw.print(prefix); pw.println(" Device battery use since last full charge");
4054 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4055 pw.println(getLowDischargeAmountSinceCharge());
4056 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4057 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004058 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4059 pw.println(getDischargeAmountScreenOnSinceCharge());
4060 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4061 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004062 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004063 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004064
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004065 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004066 helper.create(this);
4067 helper.refreshStats(which, UserHandle.USER_ALL);
4068 List<BatterySipper> sippers = helper.getUsageList();
4069 if (sippers != null && sippers.size() > 0) {
4070 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4071 pw.print(prefix); pw.print(" Capacity: ");
4072 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004073 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004074 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4075 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4076 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4077 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004078 pw.println();
4079 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004080 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004081 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004082 switch (bs.drainType) {
4083 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004084 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004085 break;
4086 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004087 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004088 break;
4089 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004090 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004091 break;
4092 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004093 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004094 break;
4095 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004096 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004097 break;
4098 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004099 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004100 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004101 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004102 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004103 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004104 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004105 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004106 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004107 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004108 break;
4109 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004110 pw.print(" User "); pw.print(bs.userId);
4111 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004112 break;
4113 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004114 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004115 break;
4116 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004117 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004118 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004119 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004120 pw.print(" Camera: ");
4121 break;
4122 default:
4123 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004124 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004125 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004126 printmAh(pw, bs.totalPowerMah);
4127
Adam Lesinski57123002015-06-12 16:12:07 -07004128 if (bs.usagePowerMah != bs.totalPowerMah) {
4129 // If the usage (generic power) isn't the whole amount, we list out
4130 // what components are involved in the calculation.
4131
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004132 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004133 if (bs.usagePowerMah != 0) {
4134 pw.print(" usage=");
4135 printmAh(pw, bs.usagePowerMah);
4136 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004137 if (bs.cpuPowerMah != 0) {
4138 pw.print(" cpu=");
4139 printmAh(pw, bs.cpuPowerMah);
4140 }
4141 if (bs.wakeLockPowerMah != 0) {
4142 pw.print(" wake=");
4143 printmAh(pw, bs.wakeLockPowerMah);
4144 }
4145 if (bs.mobileRadioPowerMah != 0) {
4146 pw.print(" radio=");
4147 printmAh(pw, bs.mobileRadioPowerMah);
4148 }
4149 if (bs.wifiPowerMah != 0) {
4150 pw.print(" wifi=");
4151 printmAh(pw, bs.wifiPowerMah);
4152 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004153 if (bs.bluetoothPowerMah != 0) {
4154 pw.print(" bt=");
4155 printmAh(pw, bs.bluetoothPowerMah);
4156 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004157 if (bs.gpsPowerMah != 0) {
4158 pw.print(" gps=");
4159 printmAh(pw, bs.gpsPowerMah);
4160 }
4161 if (bs.sensorPowerMah != 0) {
4162 pw.print(" sensor=");
4163 printmAh(pw, bs.sensorPowerMah);
4164 }
4165 if (bs.cameraPowerMah != 0) {
4166 pw.print(" camera=");
4167 printmAh(pw, bs.cameraPowerMah);
4168 }
4169 if (bs.flashlightPowerMah != 0) {
4170 pw.print(" flash=");
4171 printmAh(pw, bs.flashlightPowerMah);
4172 }
4173 pw.print(" )");
4174 }
4175 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004176 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004177 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004178 }
4179
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004180 sippers = helper.getMobilemsppList();
4181 if (sippers != null && sippers.size() > 0) {
4182 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004183 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004184 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004185 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004186 sb.setLength(0);
4187 sb.append(prefix); sb.append(" Uid ");
4188 UserHandle.formatUid(sb, bs.uidObj.getUid());
4189 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4190 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4191 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004192 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004193 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004194 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004195 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004196 sb.setLength(0);
4197 sb.append(prefix);
4198 sb.append(" TOTAL TIME: ");
4199 formatTimeMs(sb, totalTime);
4200 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4201 sb.append(")");
4202 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004203 pw.println();
4204 }
4205
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004206 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4207 @Override
4208 public int compare(TimerEntry lhs, TimerEntry rhs) {
4209 long lhsTime = lhs.mTime;
4210 long rhsTime = rhs.mTime;
4211 if (lhsTime < rhsTime) {
4212 return 1;
4213 }
4214 if (lhsTime > rhsTime) {
4215 return -1;
4216 }
4217 return 0;
4218 }
4219 };
4220
4221 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004222 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4223 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004224 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004225 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4226 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4227 : kernelWakelocks.entrySet()) {
4228 final BatteryStats.Timer timer = ent.getValue();
4229 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004230 if (totalTimeMillis > 0) {
4231 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4232 }
4233 }
4234 if (ktimers.size() > 0) {
4235 Collections.sort(ktimers, timerComparator);
4236 pw.print(prefix); pw.println(" All kernel wake locks:");
4237 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004238 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004239 String linePrefix = ": ";
4240 sb.setLength(0);
4241 sb.append(prefix);
4242 sb.append(" Kernel Wake lock ");
4243 sb.append(timer.mName);
4244 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4245 which, linePrefix);
4246 if (!linePrefix.equals(": ")) {
4247 sb.append(" realtime");
4248 // Only print out wake locks that were held
4249 pw.println(sb.toString());
4250 }
4251 }
4252 pw.println();
4253 }
4254 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004255
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004256 if (timers.size() > 0) {
4257 Collections.sort(timers, timerComparator);
4258 pw.print(prefix); pw.println(" All partial wake locks:");
4259 for (int i=0; i<timers.size(); i++) {
4260 TimerEntry timer = timers.get(i);
4261 sb.setLength(0);
4262 sb.append(" Wake lock ");
4263 UserHandle.formatUid(sb, timer.mId);
4264 sb.append(" ");
4265 sb.append(timer.mName);
4266 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4267 sb.append(" realtime");
4268 pw.println(sb.toString());
4269 }
4270 timers.clear();
4271 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004272 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004273
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004274 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004275 if (wakeupReasons.size() > 0) {
4276 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004277 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004278 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004279 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004280 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4281 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004282 }
4283 Collections.sort(reasons, timerComparator);
4284 for (int i=0; i<reasons.size(); i++) {
4285 TimerEntry timer = reasons.get(i);
4286 String linePrefix = ": ";
4287 sb.setLength(0);
4288 sb.append(prefix);
4289 sb.append(" Wakeup reason ");
4290 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004291 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4292 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004293 pw.println(sb.toString());
4294 }
4295 pw.println();
4296 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004297 }
Evan Millar22ac0432009-03-31 11:33:18 -07004298
James Carr2dd7e5e2016-07-20 18:48:39 -07004299 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4300 pw.println("Memory Stats");
4301 for (int i = 0; i < mMemoryStats.size(); i++) {
4302 sb.setLength(0);
4303 sb.append("Bandwidth ");
4304 sb.append(mMemoryStats.keyAt(i));
4305 sb.append(" Time ");
4306 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4307 pw.println(sb.toString());
4308 }
4309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 for (int iu=0; iu<NU; iu++) {
4311 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004312 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004313 continue;
4314 }
4315
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004316 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004317
4318 pw.print(prefix);
4319 pw.print(" ");
4320 UserHandle.formatUid(pw, uid);
4321 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004323
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004324 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4325 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4326 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4327 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004328 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4329 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4330
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004331 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4332 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004333 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4334 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004335
4336 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4337 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4338
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004339 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4340 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4341 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004342 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4343 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4344 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4345 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004346 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004347
Adam Lesinski5f056f62016-07-14 16:56:08 -07004348 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4349 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4350
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004351 if (mobileRxBytes > 0 || mobileTxBytes > 0
4352 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004353 pw.print(prefix); pw.print(" Mobile network: ");
4354 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004355 pw.print(formatBytesLocked(mobileTxBytes));
4356 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4357 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004359 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4360 sb.setLength(0);
4361 sb.append(prefix); sb.append(" Mobile radio active: ");
4362 formatTimeMs(sb, uidMobileActiveTime / 1000);
4363 sb.append("(");
4364 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4365 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4366 long packets = mobileRxPackets + mobileTxPackets;
4367 if (packets == 0) {
4368 packets = 1;
4369 }
4370 sb.append(" @ ");
4371 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4372 sb.append(" mspp");
4373 pw.println(sb.toString());
4374 }
4375
Adam Lesinski5f056f62016-07-14 16:56:08 -07004376 if (mobileWakeup > 0) {
4377 sb.setLength(0);
4378 sb.append(prefix);
4379 sb.append(" Mobile radio AP wakeups: ");
4380 sb.append(mobileWakeup);
4381 pw.println(sb.toString());
4382 }
4383
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004384 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4385 u.getModemControllerActivity(), which);
4386
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004387 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004388 pw.print(prefix); pw.print(" Wi-Fi network: ");
4389 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004390 pw.print(formatBytesLocked(wifiTxBytes));
4391 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4392 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004393 }
4394
Dianne Hackborn62793e42015-03-09 11:15:41 -07004395 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004396 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004397 || uidWifiRunningTime != 0) {
4398 sb.setLength(0);
4399 sb.append(prefix); sb.append(" Wifi Running: ");
4400 formatTimeMs(sb, uidWifiRunningTime / 1000);
4401 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4402 whichBatteryRealtime)); sb.append(")\n");
4403 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4404 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4405 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4406 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004407 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004408 formatTimeMs(sb, wifiScanTime / 1000);
4409 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004410 whichBatteryRealtime)); sb.append(") ");
4411 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004412 sb.append("x\n");
4413 // actual and background times are unpooled and since reset (regardless of 'which')
4414 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4415 formatTimeMs(sb, wifiScanActualTime / 1000);
4416 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4417 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4418 sb.append(") ");
4419 sb.append(wifiScanCount);
4420 sb.append("x\n");
4421 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4422 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4423 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4424 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4425 sb.append(") ");
4426 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004427 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004428 pw.println(sb.toString());
4429 }
4430
Adam Lesinski5f056f62016-07-14 16:56:08 -07004431 if (wifiWakeup > 0) {
4432 sb.setLength(0);
4433 sb.append(prefix);
4434 sb.append(" WiFi AP wakeups: ");
4435 sb.append(wifiWakeup);
4436 pw.println(sb.toString());
4437 }
4438
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004439 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4440 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004441
Adam Lesinski50e47602015-12-04 17:04:54 -08004442 if (btRxBytes > 0 || btTxBytes > 0) {
4443 pw.print(prefix); pw.print(" Bluetooth network: ");
4444 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4445 pw.print(formatBytesLocked(btTxBytes));
4446 pw.println(" sent");
4447 }
4448
Bookatz867c0d72017-03-07 18:23:42 -08004449 final Timer bleTimer = u.getBluetoothScanTimer();
4450 if (bleTimer != null) {
4451 // Convert from microseconds to milliseconds with rounding
4452 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4453 / 1000;
4454 if (totalTimeMs != 0) {
4455 final int count = bleTimer.getCountLocked(which);
4456 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4457 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
4458 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4459 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4460 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4461 final long actualTimeMsBg = bleTimerBg != null ?
4462 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4463
4464 sb.setLength(0);
4465 sb.append(prefix);
4466 sb.append(" ");
4467 sb.append("Bluetooth Scan");
4468 sb.append(": ");
4469 if (actualTimeMs != totalTimeMs) {
4470 formatTimeMs(sb, totalTimeMs);
4471 sb.append("blamed realtime, ");
4472 }
4473 formatTimeMs(sb, actualTimeMs); // since reset, regardless of 'which'
4474 sb.append("realtime (");
4475 sb.append(count);
4476 sb.append(" times)");
4477 if (bleTimer.isRunningLocked()) {
4478 sb.append(" (running)");
4479 }
4480 if (actualTimeMsBg != 0 || countBg > 0) {
4481 sb.append(", ");
4482 formatTimeMs(sb, actualTimeMsBg); // since reset, regardless of 'which'
4483 sb.append("background (");
4484 sb.append(countBg);
4485 sb.append(" times)");
4486 }
4487 pw.println(sb.toString());
4488 uidActivity = true;
4489 }
4490 }
4491
4492
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004493
Dianne Hackborn617f8772009-03-31 15:04:46 -07004494 if (u.hasUserActivity()) {
4495 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004496 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004497 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004498 if (val != 0) {
4499 if (!hasData) {
4500 sb.setLength(0);
4501 sb.append(" User activity: ");
4502 hasData = true;
4503 } else {
4504 sb.append(", ");
4505 }
4506 sb.append(val);
4507 sb.append(" ");
4508 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4509 }
4510 }
4511 if (hasData) {
4512 pw.println(sb.toString());
4513 }
4514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004516 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4517 = u.getWakelockStats();
4518 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004519 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004520 int countWakelock = 0;
4521 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4522 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4523 String linePrefix = ": ";
4524 sb.setLength(0);
4525 sb.append(prefix);
4526 sb.append(" Wake lock ");
4527 sb.append(wakelocks.keyAt(iw));
4528 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4529 "full", which, linePrefix);
4530 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4531 "partial", which, linePrefix);
4532 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4533 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004534 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4535 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004536 sb.append(" realtime");
4537 pw.println(sb.toString());
4538 uidActivity = true;
4539 countWakelock++;
4540
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004541 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4542 rawRealtime, which);
4543 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4544 rawRealtime, which);
4545 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4546 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004547 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004548 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004549 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004550 if (countWakelock > 1) {
4551 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4552 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004553 sb.setLength(0);
4554 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004555 sb.append(" TOTAL wake: ");
4556 boolean needComma = false;
4557 if (totalFullWakelock != 0) {
4558 needComma = true;
4559 formatTimeMs(sb, totalFullWakelock);
4560 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004561 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004562 if (totalPartialWakelock != 0) {
4563 if (needComma) {
4564 sb.append(", ");
4565 }
4566 needComma = true;
4567 formatTimeMs(sb, totalPartialWakelock);
4568 sb.append("partial");
4569 }
4570 if (totalWindowWakelock != 0) {
4571 if (needComma) {
4572 sb.append(", ");
4573 }
4574 needComma = true;
4575 formatTimeMs(sb, totalWindowWakelock);
4576 sb.append("window");
4577 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004578 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004579 if (needComma) {
4580 sb.append(",");
4581 }
4582 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004583 formatTimeMs(sb, totalDrawWakelock);
4584 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004585 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004586 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004587 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004588 }
4589 }
4590
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004591 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4592 for (int isy=syncs.size()-1; isy>=0; isy--) {
4593 final Timer timer = syncs.valueAt(isy);
4594 // Convert from microseconds to milliseconds with rounding
4595 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4596 final int count = timer.getCountLocked(which);
4597 sb.setLength(0);
4598 sb.append(prefix);
4599 sb.append(" Sync ");
4600 sb.append(syncs.keyAt(isy));
4601 sb.append(": ");
4602 if (totalTime != 0) {
4603 formatTimeMs(sb, totalTime);
4604 sb.append("realtime (");
4605 sb.append(count);
4606 sb.append(" times)");
4607 } else {
4608 sb.append("(not used)");
4609 }
4610 pw.println(sb.toString());
4611 uidActivity = true;
4612 }
4613
4614 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4615 for (int ij=jobs.size()-1; ij>=0; ij--) {
4616 final Timer timer = jobs.valueAt(ij);
4617 // Convert from microseconds to milliseconds with rounding
4618 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4619 final int count = timer.getCountLocked(which);
4620 sb.setLength(0);
4621 sb.append(prefix);
4622 sb.append(" Job ");
4623 sb.append(jobs.keyAt(ij));
4624 sb.append(": ");
4625 if (totalTime != 0) {
4626 formatTimeMs(sb, totalTime);
4627 sb.append("realtime (");
4628 sb.append(count);
4629 sb.append(" times)");
4630 } else {
4631 sb.append("(not used)");
4632 }
4633 pw.println(sb.toString());
4634 uidActivity = true;
4635 }
4636
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004637 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4638 prefix, "Flashlight");
4639 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4640 prefix, "Camera");
4641 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4642 prefix, "Video");
4643 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4644 prefix, "Audio");
4645
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004646 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4647 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004648 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004649 final Uid.Sensor se = sensors.valueAt(ise);
4650 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004651 sb.setLength(0);
4652 sb.append(prefix);
4653 sb.append(" Sensor ");
4654 int handle = se.getHandle();
4655 if (handle == Uid.Sensor.GPS) {
4656 sb.append("GPS");
4657 } else {
4658 sb.append(handle);
4659 }
4660 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004661
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004662 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004663 if (timer != null) {
4664 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08004665 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4666 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004667 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08004668 final Timer bgTimer = se.getSensorBackgroundTime();
4669 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
4670 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4671 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4672 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4673 final long bgActualTime = bgTimer != null ?
4674 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4675
Dianne Hackborn61659e52014-07-09 16:13:01 -07004676 //timer.logState();
4677 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004678 if (actualTime != totalTime) {
4679 formatTimeMs(sb, totalTime);
4680 sb.append("blamed realtime, ");
4681 }
4682
4683 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07004684 sb.append("realtime (");
4685 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08004686 sb.append(" times)");
4687
4688 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004689 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08004690 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
4691 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004692 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08004693 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 } else {
4696 sb.append("(not used)");
4697 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004698 } else {
4699 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004700 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004701
4702 pw.println(sb.toString());
4703 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004704 }
4705
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004706 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4707 "Vibrator");
4708 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4709 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004710
Dianne Hackborn61659e52014-07-09 16:13:01 -07004711 long totalStateTime = 0;
4712 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4713 long time = u.getProcessStateTime(ips, rawRealtime, which);
4714 if (time > 0) {
4715 totalStateTime += time;
4716 sb.setLength(0);
4717 sb.append(prefix);
4718 sb.append(" ");
4719 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4720 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004721 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004722 pw.println(sb.toString());
4723 uidActivity = true;
4724 }
4725 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004726 if (totalStateTime > 0) {
4727 sb.setLength(0);
4728 sb.append(prefix);
4729 sb.append(" Total running: ");
4730 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4731 pw.println(sb.toString());
4732 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004733
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004734 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4735 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004736 final long powerCpuMaUs = u.getCpuPowerMaUs(which);
4737 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0 || powerCpuMaUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004738 sb.setLength(0);
4739 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004740 sb.append(" Total cpu time: u=");
4741 formatTimeMs(sb, userCpuTimeUs / 1000);
4742 sb.append("s=");
4743 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07004744 sb.append("p=");
4745 printmAh(sb, powerCpuMaUs / (1000.0 * 1000.0 * 60.0 * 60.0));
4746 sb.append("mAh");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004747 pw.println(sb.toString());
4748 }
4749
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004750 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4751 = u.getProcessStats();
4752 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4753 final Uid.Proc ps = processStats.valueAt(ipr);
4754 long userTime;
4755 long systemTime;
4756 long foregroundTime;
4757 int starts;
4758 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004759
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004760 userTime = ps.getUserTime(which);
4761 systemTime = ps.getSystemTime(which);
4762 foregroundTime = ps.getForegroundTime(which);
4763 starts = ps.getStarts(which);
4764 final int numCrashes = ps.getNumCrashes(which);
4765 final int numAnrs = ps.getNumAnrs(which);
4766 numExcessive = which == STATS_SINCE_CHARGED
4767 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004768
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004769 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4770 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4771 sb.setLength(0);
4772 sb.append(prefix); sb.append(" Proc ");
4773 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4774 sb.append(prefix); sb.append(" CPU: ");
4775 formatTimeMs(sb, userTime); sb.append("usr + ");
4776 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4777 formatTimeMs(sb, foregroundTime); sb.append("fg");
4778 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4779 sb.append("\n"); sb.append(prefix); sb.append(" ");
4780 boolean hasOne = false;
4781 if (starts != 0) {
4782 hasOne = true;
4783 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004784 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004785 if (numCrashes != 0) {
4786 if (hasOne) {
4787 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004788 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004789 hasOne = true;
4790 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004791 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004792 if (numAnrs != 0) {
4793 if (hasOne) {
4794 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004795 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004796 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004797 }
4798 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004799 pw.println(sb.toString());
4800 for (int e=0; e<numExcessive; e++) {
4801 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4802 if (ew != null) {
4803 pw.print(prefix); pw.print(" * Killed for ");
4804 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4805 pw.print("wake lock");
4806 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4807 pw.print("cpu");
4808 } else {
4809 pw.print("unknown");
4810 }
4811 pw.print(" use: ");
4812 TimeUtils.formatDuration(ew.usedTime, pw);
4813 pw.print(" over ");
4814 TimeUtils.formatDuration(ew.overTime, pw);
4815 if (ew.overTime != 0) {
4816 pw.print(" (");
4817 pw.print((ew.usedTime*100)/ew.overTime);
4818 pw.println("%)");
4819 }
4820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 }
4822 uidActivity = true;
4823 }
4824 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004825
4826 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
4827 = u.getPackageStats();
4828 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
4829 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
4830 pw.println(":");
4831 boolean apkActivity = false;
4832 final Uid.Pkg ps = packageStats.valueAt(ipkg);
4833 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
4834 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
4835 pw.print(prefix); pw.print(" Wakeup alarm ");
4836 pw.print(alarms.keyAt(iwa)); pw.print(": ");
4837 pw.print(alarms.valueAt(iwa).getCountLocked(which));
4838 pw.println(" times");
4839 apkActivity = true;
4840 }
4841 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
4842 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
4843 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
4844 final long startTime = ss.getStartTime(batteryUptime, which);
4845 final int starts = ss.getStarts(which);
4846 final int launches = ss.getLaunches(which);
4847 if (startTime != 0 || starts != 0 || launches != 0) {
4848 sb.setLength(0);
4849 sb.append(prefix); sb.append(" Service ");
4850 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
4851 sb.append(prefix); sb.append(" Created for: ");
4852 formatTimeMs(sb, startTime / 1000);
4853 sb.append("uptime\n");
4854 sb.append(prefix); sb.append(" Starts: ");
4855 sb.append(starts);
4856 sb.append(", launches: "); sb.append(launches);
4857 pw.println(sb.toString());
4858 apkActivity = true;
4859 }
4860 }
4861 if (!apkActivity) {
4862 pw.print(prefix); pw.println(" (nothing executed)");
4863 }
4864 uidActivity = true;
4865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004867 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 }
4869 }
4870 }
4871
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004872 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004873 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004874 int diff = oldval ^ newval;
4875 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004876 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004877 for (int i=0; i<descriptions.length; i++) {
4878 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004879 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004880 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004881 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004882 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004883 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004884 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
4885 didWake = true;
4886 pw.print("=");
4887 if (longNames) {
4888 UserHandle.formatUid(pw, wakelockTag.uid);
4889 pw.print(":\"");
4890 pw.print(wakelockTag.string);
4891 pw.print("\"");
4892 } else {
4893 pw.print(wakelockTag.poolIdx);
4894 }
4895 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004896 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004897 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004898 pw.print("=");
4899 int val = (newval&bd.mask)>>bd.shift;
4900 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004901 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004902 } else {
4903 pw.print(val);
4904 }
4905 }
4906 }
4907 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004908 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07004909 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004910 if (longNames) {
4911 UserHandle.formatUid(pw, wakelockTag.uid);
4912 pw.print(":\"");
4913 pw.print(wakelockTag.string);
4914 pw.print("\"");
4915 } else {
4916 pw.print(wakelockTag.poolIdx);
4917 }
4918 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004919 }
4920
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004921 public void prepareForDumpLocked() {
4922 }
4923
4924 public static class HistoryPrinter {
4925 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004926 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004927 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004928 int oldStatus = -1;
4929 int oldHealth = -1;
4930 int oldPlug = -1;
4931 int oldTemp = -1;
4932 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004933 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004934 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07004935
Dianne Hackborn3251b902014-06-20 14:40:53 -07004936 void reset() {
4937 oldState = oldState2 = 0;
4938 oldLevel = -1;
4939 oldStatus = -1;
4940 oldHealth = -1;
4941 oldPlug = -1;
4942 oldTemp = -1;
4943 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07004944 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07004945 }
4946
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004947 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004948 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004949 if (!checkin) {
4950 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004951 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004952 pw.print(" (");
4953 pw.print(rec.numReadInts);
4954 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004955 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004956 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
4957 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004958 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004959 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004960 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07004961 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004962 }
4963 lastTime = rec.time;
4964 }
4965 if (rec.cmd == HistoryItem.CMD_START) {
4966 if (checkin) {
4967 pw.print(":");
4968 }
4969 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004970 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004971 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
4972 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004973 if (checkin) {
4974 pw.print(":");
4975 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07004976 if (rec.cmd == HistoryItem.CMD_RESET) {
4977 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004978 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07004979 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004980 pw.print("TIME:");
4981 if (checkin) {
4982 pw.println(rec.currentTime);
4983 } else {
4984 pw.print(" ");
4985 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
4986 rec.currentTime).toString());
4987 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08004988 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
4989 if (checkin) {
4990 pw.print(":");
4991 }
4992 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004993 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
4994 if (checkin) {
4995 pw.print(":");
4996 }
4997 pw.println("*OVERFLOW*");
4998 } else {
4999 if (!checkin) {
5000 if (rec.batteryLevel < 10) pw.print("00");
5001 else if (rec.batteryLevel < 100) pw.print("0");
5002 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005003 if (verbose) {
5004 pw.print(" ");
5005 if (rec.states < 0) ;
5006 else if (rec.states < 0x10) pw.print("0000000");
5007 else if (rec.states < 0x100) pw.print("000000");
5008 else if (rec.states < 0x1000) pw.print("00000");
5009 else if (rec.states < 0x10000) pw.print("0000");
5010 else if (rec.states < 0x100000) pw.print("000");
5011 else if (rec.states < 0x1000000) pw.print("00");
5012 else if (rec.states < 0x10000000) pw.print("0");
5013 pw.print(Integer.toHexString(rec.states));
5014 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005015 } else {
5016 if (oldLevel != rec.batteryLevel) {
5017 oldLevel = rec.batteryLevel;
5018 pw.print(",Bl="); pw.print(rec.batteryLevel);
5019 }
5020 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005021 if (oldStatus != rec.batteryStatus) {
5022 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005023 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005024 switch (oldStatus) {
5025 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005026 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005027 break;
5028 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005029 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005030 break;
5031 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005032 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005033 break;
5034 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005035 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005036 break;
5037 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005038 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005039 break;
5040 default:
5041 pw.print(oldStatus);
5042 break;
5043 }
5044 }
5045 if (oldHealth != rec.batteryHealth) {
5046 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005047 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005048 switch (oldHealth) {
5049 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005050 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005051 break;
5052 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005053 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005054 break;
5055 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005056 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005057 break;
5058 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005059 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005060 break;
5061 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005062 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005063 break;
5064 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005065 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005066 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005067 case BatteryManager.BATTERY_HEALTH_COLD:
5068 pw.print(checkin ? "c" : "cold");
5069 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005070 default:
5071 pw.print(oldHealth);
5072 break;
5073 }
5074 }
5075 if (oldPlug != rec.batteryPlugType) {
5076 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005077 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005078 switch (oldPlug) {
5079 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005080 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005081 break;
5082 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005083 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005084 break;
5085 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005086 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005087 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005088 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005089 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005090 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005091 default:
5092 pw.print(oldPlug);
5093 break;
5094 }
5095 }
5096 if (oldTemp != rec.batteryTemperature) {
5097 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005098 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005099 pw.print(oldTemp);
5100 }
5101 if (oldVolt != rec.batteryVoltage) {
5102 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005103 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005104 pw.print(oldVolt);
5105 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005106 final int chargeMAh = rec.batteryChargeUAh / 1000;
5107 if (oldChargeMAh != chargeMAh) {
5108 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005109 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005110 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005111 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005112 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005113 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005114 printBitDescriptions(pw, oldState2, rec.states2, null,
5115 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005116 if (rec.wakeReasonTag != null) {
5117 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005118 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005119 pw.print(rec.wakeReasonTag.poolIdx);
5120 } else {
5121 pw.print(" wake_reason=");
5122 pw.print(rec.wakeReasonTag.uid);
5123 pw.print(":\"");
5124 pw.print(rec.wakeReasonTag.string);
5125 pw.print("\"");
5126 }
5127 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005128 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005129 pw.print(checkin ? "," : " ");
5130 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5131 pw.print("+");
5132 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5133 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005134 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005135 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5136 : HISTORY_EVENT_NAMES;
5137 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5138 | HistoryItem.EVENT_FLAG_FINISH);
5139 if (idx >= 0 && idx < eventNames.length) {
5140 pw.print(eventNames[idx]);
5141 } else {
5142 pw.print(checkin ? "Ev" : "event");
5143 pw.print(idx);
5144 }
5145 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005146 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005147 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005148 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005149 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5150 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005151 pw.print(":\"");
5152 pw.print(rec.eventTag.string);
5153 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005154 }
5155 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005156 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005157 if (rec.stepDetails != null) {
5158 if (!checkin) {
5159 pw.print(" Details: cpu=");
5160 pw.print(rec.stepDetails.userTime);
5161 pw.print("u+");
5162 pw.print(rec.stepDetails.systemTime);
5163 pw.print("s");
5164 if (rec.stepDetails.appCpuUid1 >= 0) {
5165 pw.print(" (");
5166 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5167 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5168 if (rec.stepDetails.appCpuUid2 >= 0) {
5169 pw.print(", ");
5170 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5171 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5172 }
5173 if (rec.stepDetails.appCpuUid3 >= 0) {
5174 pw.print(", ");
5175 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5176 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5177 }
5178 pw.print(')');
5179 }
5180 pw.println();
5181 pw.print(" /proc/stat=");
5182 pw.print(rec.stepDetails.statUserTime);
5183 pw.print(" usr, ");
5184 pw.print(rec.stepDetails.statSystemTime);
5185 pw.print(" sys, ");
5186 pw.print(rec.stepDetails.statIOWaitTime);
5187 pw.print(" io, ");
5188 pw.print(rec.stepDetails.statIrqTime);
5189 pw.print(" irq, ");
5190 pw.print(rec.stepDetails.statSoftIrqTime);
5191 pw.print(" sirq, ");
5192 pw.print(rec.stepDetails.statIdlTime);
5193 pw.print(" idle");
5194 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5195 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5196 + rec.stepDetails.statSoftIrqTime;
5197 int total = totalRun + rec.stepDetails.statIdlTime;
5198 if (total > 0) {
5199 pw.print(" (");
5200 float perc = ((float)totalRun) / ((float)total) * 100;
5201 pw.print(String.format("%.1f%%", perc));
5202 pw.print(" of ");
5203 StringBuilder sb = new StringBuilder(64);
5204 formatTimeMsNoSpace(sb, total*10);
5205 pw.print(sb);
5206 pw.print(")");
5207 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005208 pw.print(", PlatformIdleStat ");
5209 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005210 pw.println();
5211 } else {
5212 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5213 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5214 pw.print(rec.stepDetails.userTime);
5215 pw.print(":");
5216 pw.print(rec.stepDetails.systemTime);
5217 if (rec.stepDetails.appCpuUid1 >= 0) {
5218 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5219 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5220 if (rec.stepDetails.appCpuUid2 >= 0) {
5221 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5222 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5223 }
5224 if (rec.stepDetails.appCpuUid3 >= 0) {
5225 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5226 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5227 }
5228 }
5229 pw.println();
5230 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5231 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5232 pw.print(rec.stepDetails.statUserTime);
5233 pw.print(',');
5234 pw.print(rec.stepDetails.statSystemTime);
5235 pw.print(',');
5236 pw.print(rec.stepDetails.statIOWaitTime);
5237 pw.print(',');
5238 pw.print(rec.stepDetails.statIrqTime);
5239 pw.print(',');
5240 pw.print(rec.stepDetails.statSoftIrqTime);
5241 pw.print(',');
5242 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005243 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005244 if (rec.stepDetails.statPlatformIdleState != null) {
5245 pw.print(rec.stepDetails.statPlatformIdleState);
5246 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005247 pw.println();
5248 }
5249 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005250 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005251 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005252 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005253 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005254
5255 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5256 UserHandle.formatUid(pw, uid);
5257 pw.print("=");
5258 pw.print(utime);
5259 pw.print("u+");
5260 pw.print(stime);
5261 pw.print("s");
5262 }
5263
5264 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5265 pw.print('/');
5266 pw.print(uid);
5267 pw.print(":");
5268 pw.print(utime);
5269 pw.print(":");
5270 pw.print(stime);
5271 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005272 }
5273
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005274 private void printSizeValue(PrintWriter pw, long size) {
5275 float result = size;
5276 String suffix = "";
5277 if (result >= 10*1024) {
5278 suffix = "KB";
5279 result = result / 1024;
5280 }
5281 if (result >= 10*1024) {
5282 suffix = "MB";
5283 result = result / 1024;
5284 }
5285 if (result >= 10*1024) {
5286 suffix = "GB";
5287 result = result / 1024;
5288 }
5289 if (result >= 10*1024) {
5290 suffix = "TB";
5291 result = result / 1024;
5292 }
5293 if (result >= 10*1024) {
5294 suffix = "PB";
5295 result = result / 1024;
5296 }
5297 pw.print((int)result);
5298 pw.print(suffix);
5299 }
5300
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005301 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5302 String label3, long estimatedTime) {
5303 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005304 return false;
5305 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005306 pw.print(label1);
5307 pw.print(label2);
5308 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005309 StringBuilder sb = new StringBuilder(64);
5310 formatTimeMs(sb, estimatedTime);
5311 pw.print(sb);
5312 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005313 return true;
5314 }
5315
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005316 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5317 LevelStepTracker steps, boolean checkin) {
5318 if (steps == null) {
5319 return false;
5320 }
5321 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005322 if (count <= 0) {
5323 return false;
5324 }
5325 if (!checkin) {
5326 pw.println(header);
5327 }
Kweku Adams030980a2015-04-01 16:07:48 -07005328 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005329 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005330 long duration = steps.getDurationAt(i);
5331 int level = steps.getLevelAt(i);
5332 long initMode = steps.getInitModeAt(i);
5333 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005334 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005335 lineArgs[0] = Long.toString(duration);
5336 lineArgs[1] = Integer.toString(level);
5337 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5338 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5339 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5340 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5341 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5342 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005343 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005344 }
5345 } else {
5346 lineArgs[2] = "";
5347 }
5348 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5349 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5350 } else {
5351 lineArgs[3] = "";
5352 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005353 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005354 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005355 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005356 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005357 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005358 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5359 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005360 pw.print(prefix);
5361 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005362 TimeUtils.formatDuration(duration, pw);
5363 pw.print(" to "); pw.print(level);
5364 boolean haveModes = false;
5365 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5366 pw.print(" (");
5367 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5368 case Display.STATE_OFF: pw.print("screen-off"); break;
5369 case Display.STATE_ON: pw.print("screen-on"); break;
5370 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5371 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005372 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005373 }
5374 haveModes = true;
5375 }
5376 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5377 pw.print(haveModes ? ", " : " (");
5378 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5379 ? "power-save-on" : "power-save-off");
5380 haveModes = true;
5381 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005382 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5383 pw.print(haveModes ? ", " : " (");
5384 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5385 ? "device-idle-on" : "device-idle-off");
5386 haveModes = true;
5387 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005388 if (haveModes) {
5389 pw.print(")");
5390 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005391 pw.println();
5392 }
5393 }
5394 return true;
5395 }
5396
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005397 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005398 public static final int DUMP_DAILY_ONLY = 1<<2;
5399 public static final int DUMP_HISTORY_ONLY = 1<<3;
5400 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5401 public static final int DUMP_VERBOSE = 1<<5;
5402 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005403
Dianne Hackborn37de0982014-05-09 09:32:18 -07005404 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5405 final HistoryPrinter hprinter = new HistoryPrinter();
5406 final HistoryItem rec = new HistoryItem();
5407 long lastTime = -1;
5408 long baseTime = -1;
5409 boolean printed = false;
5410 HistoryEventTracker tracker = null;
5411 while (getNextHistoryLocked(rec)) {
5412 lastTime = rec.time;
5413 if (baseTime < 0) {
5414 baseTime = lastTime;
5415 }
5416 if (rec.time >= histStart) {
5417 if (histStart >= 0 && !printed) {
5418 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005419 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005420 || rec.cmd == HistoryItem.CMD_START
5421 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005422 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005423 hprinter.printNextItem(pw, rec, baseTime, checkin,
5424 (flags&DUMP_VERBOSE) != 0);
5425 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005426 } else if (rec.currentTime != 0) {
5427 printed = true;
5428 byte cmd = rec.cmd;
5429 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005430 hprinter.printNextItem(pw, rec, baseTime, checkin,
5431 (flags&DUMP_VERBOSE) != 0);
5432 rec.cmd = cmd;
5433 }
5434 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005435 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5436 hprinter.printNextItem(pw, rec, baseTime, checkin,
5437 (flags&DUMP_VERBOSE) != 0);
5438 rec.cmd = HistoryItem.CMD_UPDATE;
5439 }
5440 int oldEventCode = rec.eventCode;
5441 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005442 rec.eventTag = new HistoryTag();
5443 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5444 HashMap<String, SparseIntArray> active
5445 = tracker.getStateForEvent(i);
5446 if (active == null) {
5447 continue;
5448 }
5449 for (HashMap.Entry<String, SparseIntArray> ent
5450 : active.entrySet()) {
5451 SparseIntArray uids = ent.getValue();
5452 for (int j=0; j<uids.size(); j++) {
5453 rec.eventCode = i;
5454 rec.eventTag.string = ent.getKey();
5455 rec.eventTag.uid = uids.keyAt(j);
5456 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005457 hprinter.printNextItem(pw, rec, baseTime, checkin,
5458 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005459 rec.wakeReasonTag = null;
5460 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005461 }
5462 }
5463 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005464 rec.eventCode = oldEventCode;
5465 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005466 tracker = null;
5467 }
5468 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005469 hprinter.printNextItem(pw, rec, baseTime, checkin,
5470 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005471 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5472 // This is an attempt to aggregate the previous state and generate
5473 // fake events to reflect that state at the point where we start
5474 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005475 if (tracker == null) {
5476 tracker = new HistoryEventTracker();
5477 }
5478 tracker.updateState(rec.eventCode, rec.eventTag.string,
5479 rec.eventTag.uid, rec.eventTag.poolIdx);
5480 }
5481 }
5482 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005483 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005484 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5485 }
5486 }
5487
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005488 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5489 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5490 if (steps == null) {
5491 return;
5492 }
5493 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5494 if (timeRemaining >= 0) {
5495 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5496 tmpSb.setLength(0);
5497 formatTimeMs(tmpSb, timeRemaining);
5498 pw.print(tmpSb);
5499 pw.print(" (from "); pw.print(tmpOutInt[0]);
5500 pw.println(" steps)");
5501 }
5502 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5503 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5504 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5505 if (estimatedTime > 0) {
5506 pw.print(prefix); pw.print(label); pw.print(" ");
5507 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5508 pw.print(" time: ");
5509 tmpSb.setLength(0);
5510 formatTimeMs(tmpSb, estimatedTime);
5511 pw.print(tmpSb);
5512 pw.print(" (from "); pw.print(tmpOutInt[0]);
5513 pw.println(" steps)");
5514 }
5515 }
5516 }
5517
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005518 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5519 ArrayList<PackageChange> changes) {
5520 if (changes == null) {
5521 return;
5522 }
5523 pw.print(prefix); pw.println("Package changes:");
5524 for (int i=0; i<changes.size(); i++) {
5525 PackageChange pc = changes.get(i);
5526 if (pc.mUpdate) {
5527 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5528 pw.print(" vers="); pw.println(pc.mVersionCode);
5529 } else {
5530 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5531 }
5532 }
5533 }
5534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 /**
5536 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5537 *
5538 * @param pw a Printer to receive the dump output.
5539 */
5540 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005541 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005542 prepareForDumpLocked();
5543
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005544 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005545 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005546
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005547 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005548 final long historyTotalSize = getHistoryTotalSize();
5549 final long historyUsedSize = getHistoryUsedSize();
5550 if (startIteratingHistoryLocked()) {
5551 try {
5552 pw.print("Battery History (");
5553 pw.print((100*historyUsedSize)/historyTotalSize);
5554 pw.print("% used, ");
5555 printSizeValue(pw, historyUsedSize);
5556 pw.print(" used of ");
5557 printSizeValue(pw, historyTotalSize);
5558 pw.print(", ");
5559 pw.print(getHistoryStringPoolSize());
5560 pw.print(" strings using ");
5561 printSizeValue(pw, getHistoryStringPoolBytes());
5562 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005563 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005564 pw.println();
5565 } finally {
5566 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005567 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005568 }
5569
5570 if (startIteratingOldHistoryLocked()) {
5571 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005572 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005573 pw.println("Old battery History:");
5574 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005575 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005576 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005577 if (baseTime < 0) {
5578 baseTime = rec.time;
5579 }
5580 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005581 }
5582 pw.println();
5583 } finally {
5584 finishIteratingOldHistoryLocked();
5585 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005586 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005587 }
5588
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005589 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005590 return;
5591 }
5592
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005593 if (!filtering) {
5594 SparseArray<? extends Uid> uidStats = getUidStats();
5595 final int NU = uidStats.size();
5596 boolean didPid = false;
5597 long nowRealtime = SystemClock.elapsedRealtime();
5598 for (int i=0; i<NU; i++) {
5599 Uid uid = uidStats.valueAt(i);
5600 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5601 if (pids != null) {
5602 for (int j=0; j<pids.size(); j++) {
5603 Uid.Pid pid = pids.valueAt(j);
5604 if (!didPid) {
5605 pw.println("Per-PID Stats:");
5606 didPid = true;
5607 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005608 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5609 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005610 pw.print(" PID "); pw.print(pids.keyAt(j));
5611 pw.print(" wake time: ");
5612 TimeUtils.formatDuration(time, pw);
5613 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005614 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005615 }
5616 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005617 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005618 pw.println();
5619 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005620 }
5621
5622 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005623 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5624 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005625 long timeRemaining = computeBatteryTimeRemaining(
5626 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005627 if (timeRemaining >= 0) {
5628 pw.print(" Estimated discharge time remaining: ");
5629 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5630 pw.println();
5631 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005632 final LevelStepTracker steps = getDischargeLevelStepTracker();
5633 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5634 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5635 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5636 STEP_LEVEL_MODE_VALUES[i], null));
5637 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005638 pw.println();
5639 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005640 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5641 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005642 long timeRemaining = computeChargeTimeRemaining(
5643 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005644 if (timeRemaining >= 0) {
5645 pw.print(" Estimated charge time remaining: ");
5646 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5647 pw.println();
5648 }
5649 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005650 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005651 }
5652 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5653 pw.println("Daily stats:");
5654 pw.print(" Current start time: ");
5655 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5656 getCurrentDailyStartTime()).toString());
5657 pw.print(" Next min deadline: ");
5658 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5659 getNextMinDailyDeadline()).toString());
5660 pw.print(" Next max deadline: ");
5661 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5662 getNextMaxDailyDeadline()).toString());
5663 StringBuilder sb = new StringBuilder(64);
5664 int[] outInt = new int[1];
5665 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5666 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005667 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5668 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005669 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005670 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5671 dsteps, false)) {
5672 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5673 sb, outInt);
5674 }
5675 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5676 csteps, false)) {
5677 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5678 sb, outInt);
5679 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005680 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005681 } else {
5682 pw.println(" Current daily steps:");
5683 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5684 sb, outInt);
5685 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5686 sb, outInt);
5687 }
5688 }
5689 DailyItem dit;
5690 int curIndex = 0;
5691 while ((dit=getDailyItemLocked(curIndex)) != null) {
5692 curIndex++;
5693 if ((flags&DUMP_DAILY_ONLY) != 0) {
5694 pw.println();
5695 }
5696 pw.print(" Daily from ");
5697 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5698 pw.print(" to ");
5699 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5700 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005701 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005702 if (dumpDurationSteps(pw, " ",
5703 " Discharge step durations:", dit.mDischargeSteps, false)) {
5704 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5705 sb, outInt);
5706 }
5707 if (dumpDurationSteps(pw, " ",
5708 " Charge step durations:", dit.mChargeSteps, false)) {
5709 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5710 sb, outInt);
5711 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005712 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005713 } else {
5714 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5715 sb, outInt);
5716 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5717 sb, outInt);
5718 }
5719 }
5720 pw.println();
5721 }
5722 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005723 pw.println("Statistics since last charge:");
5724 pw.println(" System starts: " + getStartCount()
5725 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005726 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5727 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005728 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005730 }
5731
5732 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005733 public void dumpCheckinLocked(Context context, PrintWriter pw,
5734 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005735 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005736
5737 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005738 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5739 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005740
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005741 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5742
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005743 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005744 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005745
5746 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005747 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005748 try {
5749 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5750 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5751 pw.print(HISTORY_STRING_POOL); pw.print(',');
5752 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005753 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005754 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005755 pw.print(",\"");
5756 String str = getHistoryTagPoolString(i);
5757 str = str.replace("\\", "\\\\");
5758 str = str.replace("\"", "\\\"");
5759 pw.print(str);
5760 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005761 pw.println();
5762 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005763 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005764 } finally {
5765 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005766 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005767 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005768 }
5769
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005770 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005771 return;
5772 }
5773
Dianne Hackborne4a59512010-12-07 11:08:07 -08005774 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005775 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005776 for (int i=0; i<apps.size(); i++) {
5777 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005778 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
5779 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005780 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005781 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
5782 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005783 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005784 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005785 }
5786 SparseArray<? extends Uid> uidStats = getUidStats();
5787 final int NU = uidStats.size();
5788 String[] lineArgs = new String[2];
5789 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005790 int uid = UserHandle.getAppId(uidStats.keyAt(i));
5791 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
5792 if (pkgs != null && !pkgs.second.value) {
5793 pkgs.second.value = true;
5794 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005795 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005796 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005797 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5798 (Object[])lineArgs);
5799 }
5800 }
5801 }
5802 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005803 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005804 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005805 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07005806 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005807 if (timeRemaining >= 0) {
5808 lineArgs[0] = Long.toString(timeRemaining);
5809 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5810 (Object[])lineArgs);
5811 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005812 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07005813 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005814 if (timeRemaining >= 0) {
5815 lineArgs[0] = Long.toString(timeRemaining);
5816 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5817 (Object[])lineArgs);
5818 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005819 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5820 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005823}