blob: 3eea72d5819bafbab407168fa767b94995e217cf [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
19import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080020import java.util.ArrayList;
Dianne Hackborn81038902012-11-26 17:04:09 -080021import java.util.Collections;
22import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.Formatter;
Dianne Hackborn37de0982014-05-09 09:32:18 -070024import java.util.HashMap;
Dianne Hackborne4a59512010-12-07 11:08:07 -080025import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import java.util.Map;
27
Dianne Hackborna7c837f2014-01-15 16:20:44 -080028import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080029import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080030import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080031import android.text.format.DateFormat;
Dianne Hackborn1e725a72015-03-24 18:23:19 -070032import android.util.ArrayMap;
James Carr2dd7e5e2016-07-20 18:48:39 -070033import android.util.LongSparseArray;
Dianne Hackborn9cfba352016-03-24 17:31:28 -070034import android.util.MutableBoolean;
35import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.util.Printer;
37import android.util.SparseArray;
Dianne Hackborn37de0982014-05-09 09:32:18 -070038import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070039import android.util.TimeUtils;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070040import android.view.Display;
Amith Yamasaniab9ad192016-12-06 12:46:59 -080041
Dianne Hackborna7c837f2014-01-15 16:20:44 -080042import com.android.internal.os.BatterySipper;
43import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45/**
46 * A class providing access to battery usage statistics, including information on
47 * wakelocks, processes, packages, and services. All times are represented in microseconds
48 * except where indicated otherwise.
49 * @hide
50 */
51public abstract class BatteryStats implements Parcelable {
Joe Onorato92fd23f2016-07-25 11:18:42 -070052 private static final String TAG = "BatteryStats";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070055
56 /** @hide */
57 public static final String SERVICE_NAME = "batterystats";
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 /**
60 * A constant indicating a partial wake lock timer.
61 */
62 public static final int WAKE_TYPE_PARTIAL = 0;
63
64 /**
65 * A constant indicating a full wake lock timer.
66 */
67 public static final int WAKE_TYPE_FULL = 1;
68
69 /**
70 * A constant indicating a window wake lock timer.
71 */
72 public static final int WAKE_TYPE_WINDOW = 2;
Adam Lesinski9425fe22015-06-19 12:02:13 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 /**
75 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 */
77 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070078
79 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070080 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070081 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070082 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070083
84 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070085 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070086 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070087 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070088
89 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070090 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070091 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070092 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
Dianne Hackborn62793e42015-03-09 11:15:41 -070094 /**
95 * A constant indicating a wifi multicast timer
96 */
97 public static final int WIFI_MULTICAST_ENABLED = 7;
Robert Greenwalt5347bd42009-05-13 15:10:16 -070098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700100 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700101 */
102 public static final int VIDEO_TURNED_ON = 8;
103
104 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800105 * A constant indicating a vibrator on timer
106 */
107 public static final int VIBRATOR_ON = 9;
108
109 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700110 * A constant indicating a foreground activity timer
111 */
112 public static final int FOREGROUND_ACTIVITY = 10;
113
114 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700115 * A constant indicating a wifi batched scan is active
116 */
117 public static final int WIFI_BATCHED_SCAN = 11;
118
119 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -0700120 * A constant indicating a process state timer
121 */
122 public static final int PROCESS_STATE = 12;
123
124 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700125 * A constant indicating a sync timer
126 */
127 public static final int SYNC = 13;
128
129 /**
130 * A constant indicating a job timer
131 */
132 public static final int JOB = 14;
133
134 /**
Kweku Adamsd5379872014-11-24 17:34:05 -0800135 * A constant indicating an audio turn on timer
136 */
137 public static final int AUDIO_TURNED_ON = 15;
138
139 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700140 * A constant indicating a flashlight turn on timer
141 */
142 public static final int FLASHLIGHT_TURNED_ON = 16;
143
144 /**
145 * A constant indicating a camera turn on timer
146 */
147 public static final int CAMERA_TURNED_ON = 17;
148
149 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700150 * A constant indicating a draw wake lock timer.
Adam Lesinski9425fe22015-06-19 12:02:13 -0700151 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700152 public static final int WAKE_TYPE_DRAW = 18;
Adam Lesinski9425fe22015-06-19 12:02:13 -0700153
154 /**
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800155 * A constant indicating a bluetooth scan timer.
156 */
157 public static final int BLUETOOTH_SCAN_ON = 19;
158
159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 * Include all of the data in the stats, including previously saved data.
161 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700162 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 * Include only the current run in the stats.
166 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700167 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
169 /**
170 * Include only the run since the last time the device was unplugged in the stats.
171 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700172 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700173
174 // NOTE: Update this list if you add/change any stats above.
175 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700176 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700177 private static final String[] STAT_NAMES = { "l", "c", "u" };
178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700180 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700181 *
182 * New in version 19:
183 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800184 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700185 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700186 * New in version 21:
187 * - Actual (not just apportioned) Wakelock time is also recorded.
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700188 */
Bookatz506a8182017-05-01 14:18:42 -0700189 static final String CHECKIN_VERSION = "21";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700190
191 /**
192 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700194 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700195
Evan Millar22ac0432009-03-31 11:33:18 -0700196 private static final long BYTES_PER_KB = 1024;
197 private static final long BYTES_PER_MB = 1048576; // 1024^2
198 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700199
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700200 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800201 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700202 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700204 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700205 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700206 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
207 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Evan Millare84de8d2009-04-02 22:16:12 -0700208 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800209 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700210 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700211 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700212 // wl line is:
213 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
214 // full totalTime, 'f', count, current duration, max duration, total duration,
215 // partial totalTime, 'p', count, current duration, max duration, total duration,
216 // window totalTime, 'w', count, current duration, max duration, total duration
217 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700218 private static final String WAKELOCK_DATA = "wl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700219 private static final String SYNC_DATA = "sy";
220 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700221 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700222 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700223 private static final String NETWORK_DATA = "nt";
224 private static final String USER_ACTIVITY_DATA = "ua";
225 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800226 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700227 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700228 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700229 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800230 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
231 private static final String WIFI_CONTROLLER_DATA = "wfcd";
232 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
233 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700234 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700235 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800236 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800237 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
238 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800239 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700240 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700241 private static final String SCREEN_BRIGHTNESS_DATA = "br";
242 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700243 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700244 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
245 private static final String DATA_CONNECTION_TIME_DATA = "dct";
246 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800247 private static final String WIFI_STATE_TIME_DATA = "wst";
248 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700249 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
250 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
251 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
252 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800253 private static final String POWER_USE_SUMMARY_DATA = "pws";
254 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700255 private static final String DISCHARGE_STEP_DATA = "dsd";
256 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700257 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
258 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700259 private static final String FLASHLIGHT_DATA = "fla";
260 private static final String CAMERA_DATA = "cam";
261 private static final String VIDEO_DATA = "vid";
262 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263
Adam Lesinski010bf372016-04-11 12:18:18 -0700264 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
265
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700266 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 private final Formatter mFormatter = new Formatter(mFormatBuilder);
268
269 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700270 * Indicates times spent by the uid at each cpu frequency in all process states.
271 *
272 * Other types might include times spent in foreground, background etc.
273 */
274 private final String UID_TIMES_TYPE_ALL = "A";
275
276 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700277 * State for keeping track of counting information.
278 */
279 public static abstract class Counter {
280
281 /**
282 * Returns the count associated with this Counter for the
283 * selected type of statistics.
284 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700285 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700286 */
Evan Millarc64edde2009-04-18 12:26:32 -0700287 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700288
289 /**
290 * Temporary for debugging.
291 */
292 public abstract void logState(Printer pw, String prefix);
293 }
294
295 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700296 * State for keeping track of long counting information.
297 */
298 public static abstract class LongCounter {
299
300 /**
301 * Returns the count associated with this Counter for the
302 * selected type of statistics.
303 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700304 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700305 */
306 public abstract long getCountLocked(int which);
307
308 /**
309 * Temporary for debugging.
310 */
311 public abstract void logState(Printer pw, String prefix);
312 }
313
314 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700315 * State for keeping track of array of long counting information.
316 */
317 public static abstract class LongCounterArray {
318 /**
319 * Returns the counts associated with this Counter for the
320 * selected type of statistics.
321 *
322 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
323 */
324 public abstract long[] getCountsLocked(int which);
325
326 /**
327 * Temporary for debugging.
328 */
329 public abstract void logState(Printer pw, String prefix);
330 }
331
332 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800333 * Container class that aggregates counters for transmit, receive, and idle state of a
334 * radio controller.
335 */
336 public static abstract class ControllerActivityCounter {
337 /**
338 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
339 * idle state.
340 */
341 public abstract LongCounter getIdleTimeCounter();
342
343 /**
344 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
345 * receive state.
346 */
347 public abstract LongCounter getRxTimeCounter();
348
349 /**
350 * An array of {@link LongCounter}, representing various transmit levels, where each level
351 * may draw a different amount of power. The levels themselves are controller-specific.
352 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
353 * various transmit level states.
354 */
355 public abstract LongCounter[] getTxTimeCounters();
356
357 /**
358 * @return a non-null {@link LongCounter} representing the power consumed by the controller
359 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
360 * yield a value of 0 if the device doesn't support power calculations.
361 */
362 public abstract LongCounter getPowerCounter();
363 }
364
365 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 * State for keeping track of timing information.
367 */
368 public static abstract class Timer {
369
370 /**
371 * Returns the count associated with this Timer for the
372 * selected type of statistics.
373 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700374 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 */
Evan Millarc64edde2009-04-18 12:26:32 -0700376 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377
378 /**
379 * Returns the total time in microseconds associated with this Timer for the
380 * selected type of statistics.
381 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800382 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700383 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 * @return a time in microseconds
385 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800386 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700389 * Returns the total time in microseconds associated with this Timer since the
390 * 'mark' was last set.
391 *
392 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
393 * @return a time in microseconds
394 */
395 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
396
397 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700398 * Returns the max duration if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800399 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700400
401 */
402 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
403 return -1;
404 }
405
406 /**
407 * Returns the current time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800408 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700409 */
410 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
411 return -1;
412 }
413
414 /**
Bookatz867c0d72017-03-07 18:23:42 -0800415 * Returns the current time the timer has been active, if it is being tracked.
416 *
417 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
418 * been on since reset.
419 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
420 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
421 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
422 * the actual total time.
423 * Not all Timer subclasses track the max, total, current durations.
424 */
425 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
426 return -1;
427 }
428
429 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700430 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
431 * used, for example, for tracking background usage. Secondary timers are never pooled.
432 *
433 * Not all Timer subclasses have a secondary timer; those that don't return null.
434 */
435 public Timer getSubTimer() {
436 return null;
437 }
438
439 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700440 * Returns whether the timer is currently running. Some types of timers
441 * (e.g. BatchTimers) don't know whether the event is currently active,
442 * and report false.
443 */
444 public boolean isRunningLocked() {
445 return false;
446 }
447
448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 * Temporary for debugging.
450 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700451 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 }
453
454 /**
455 * The statistics associated with a particular uid.
456 */
457 public static abstract class Uid {
458
459 /**
460 * Returns a mapping containing wakelock statistics.
461 *
462 * @return a Map from Strings to Uid.Wakelock objects.
463 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700464 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465
466 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700467 * Returns a mapping containing sync statistics.
468 *
469 * @return a Map from Strings to Timer objects.
470 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700471 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700472
473 /**
474 * Returns a mapping containing scheduled job statistics.
475 *
476 * @return a Map from Strings to Timer objects.
477 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700478 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700479
480 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 * The statistics associated with a particular wake lock.
482 */
483 public static abstract class Wakelock {
484 public abstract Timer getWakeTime(int type);
485 }
486
487 /**
488 * Returns a mapping containing sensor statistics.
489 *
490 * @return a Map from Integer sensor ids to Uid.Sensor objects.
491 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700492 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493
494 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700495 * Returns a mapping containing active process data.
496 */
497 public abstract SparseArray<? extends Pid> getPidStats();
498
499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 * Returns a mapping containing process statistics.
501 *
502 * @return a Map from Strings to Uid.Proc objects.
503 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700504 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505
506 /**
507 * Returns a mapping containing package statistics.
508 *
509 * @return a Map from Strings to Uid.Pkg objects.
510 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700511 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700512
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800513 public abstract ControllerActivityCounter getWifiControllerActivity();
514 public abstract ControllerActivityCounter getBluetoothControllerActivity();
515 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800516
517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 * {@hide}
519 */
520 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700521
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800522 public abstract void noteWifiRunningLocked(long elapsedRealtime);
523 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
524 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
525 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
526 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
527 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
528 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
529 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
530 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
531 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800532 public abstract void noteActivityResumedLocked(long elapsedRealtime);
533 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800534 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
535 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
536 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700537 public abstract int getWifiScanCount(int which);
Bookatz867c0d72017-03-07 18:23:42 -0800538 public abstract int getWifiScanBackgroundCount(int which);
539 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
540 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800541 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700542 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800543 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700544 public abstract Timer getAudioTurnedOnTimer();
545 public abstract Timer getVideoTurnedOnTimer();
546 public abstract Timer getFlashlightTurnedOnTimer();
547 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700548 public abstract Timer getForegroundActivityTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800549 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800550 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700551 public abstract Counter getBluetoothScanResultCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700552
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700553 public abstract long[] getCpuFreqTimes(int which);
554 public abstract long[] getScreenOffCpuFreqTimes(int which);
555
Dianne Hackborna0200e32016-03-30 18:01:41 -0700556 // Note: the following times are disjoint. They can be added together to find the
557 // total time a uid has had any processes running at all.
558
559 /**
560 * Time this uid has any processes in the top state (or above such as persistent).
561 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800562 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700563 /**
564 * Time this uid has any process with a started out bound foreground service, but
565 * none in the "top" state.
566 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800567 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700568 /**
569 * Time this uid has any process that is top while the device is sleeping, but none
570 * in the "foreground service" or better state.
571 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800572 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700573 /**
574 * Time this uid has any process in an active foreground state, but none in the
575 * "top sleeping" or better state.
576 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800577 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700578 /**
579 * Time this uid has any process in an active background state, but none in the
580 * "foreground" or better state.
581 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800582 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700583 /**
584 * Time this uid has any processes that are sitting around cached, not in one of the
585 * other active states.
586 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800587 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700588 /**
589 * Total number of process states we track.
590 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800591 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700592
593 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800594 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700595 };
596
597 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800598 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700599
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800600 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601
Robert Greenwalta029ea12013-09-25 16:38:12 -0700602 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
603
Dianne Hackborn617f8772009-03-31 15:04:46 -0700604 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700605 * Note that these must match the constants in android.os.PowerManager.
606 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
607 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700608 */
609 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700610 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700611 };
612
Phil Weaverda80d672016-03-15 16:25:46 -0700613 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700614
Dianne Hackborn617f8772009-03-31 15:04:46 -0700615 public abstract void noteUserActivityLocked(int type);
616 public abstract boolean hasUserActivity();
617 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700618
619 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800620 public abstract long getNetworkActivityBytes(int type, int which);
621 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800622 public abstract long getMobileRadioActiveTime(int which);
623 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700624
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700625 /**
626 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
627 */
628 public abstract long getUserCpuTimeUs(int which);
629
630 /**
631 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
632 */
633 public abstract long getSystemCpuTimeUs(int which);
634
635 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700636 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
637 * given CPU cluster.
638 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700639 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700640 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700641 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
642 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700643 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700644 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700645
Adam Lesinski5f056f62016-07-14 16:56:08 -0700646 /**
647 * Returns the number of times this UID woke up the Application Processor to
648 * process a mobile radio packet.
649 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
650 */
651 public abstract long getMobileRadioApWakeupCount(int which);
652
653 /**
654 * Returns the number of times this UID woke up the Application Processor to
655 * process a WiFi packet.
656 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
657 */
658 public abstract long getWifiRadioApWakeupCount(int which);
659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800661 /*
662 * FIXME: it's not correct to use this magic value because it
663 * could clash with a sensor handle (which are defined by
664 * the sensor HAL, and therefore out of our control
665 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 // Magic sensor number for the GPS.
667 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800672
Bookatz867c0d72017-03-07 18:23:42 -0800673 /** Returns a Timer for sensor usage when app is in the background. */
674 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 }
676
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700677 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800678 public int mWakeNesting;
679 public long mWakeSumMs;
680 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700681 }
682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 /**
684 * The statistics associated with a particular process.
685 */
686 public static abstract class Proc {
687
Dianne Hackborn287952c2010-09-22 22:34:31 -0700688 public static class ExcessivePower {
689 public static final int TYPE_WAKE = 1;
690 public static final int TYPE_CPU = 2;
691
692 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700693 public long overTime;
694 public long usedTime;
695 }
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800698 * Returns true if this process is still active in the battery stats.
699 */
700 public abstract boolean isActive();
701
702 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700703 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700705 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 */
707 public abstract long getUserTime(int which);
708
709 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700710 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700712 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public abstract long getSystemTime(int which);
715
716 /**
717 * Returns the number of times the process has been started.
718 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700719 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700722
723 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800724 * Returns the number of times the process has crashed.
725 *
726 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
727 */
728 public abstract int getNumCrashes(int which);
729
730 /**
731 * Returns the number of times the process has ANRed.
732 *
733 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
734 */
735 public abstract int getNumAnrs(int which);
736
737 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700738 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700739 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700740 * @return foreground cpu time in microseconds
741 */
742 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700743
Dianne Hackborn287952c2010-09-22 22:34:31 -0700744 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700745
Dianne Hackborn287952c2010-09-22 22:34:31 -0700746 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748
749 /**
750 * The statistics associated with a particular package.
751 */
752 public static abstract class Pkg {
753
754 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700755 * Returns information about all wakeup alarms that have been triggered for this
756 * package. The mapping keys are tag names for the alarms, the counter contains
757 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700759 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760
761 /**
762 * Returns a mapping containing service statistics.
763 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700764 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
766 /**
767 * The statistics associated with a particular service.
768 */
Joe Onoratoabded112016-02-08 16:49:39 -0800769 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770
771 /**
772 * Returns the amount of time spent started.
773 *
774 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700775 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 * @return
777 */
778 public abstract long getStartTime(long batteryUptime, int which);
779
780 /**
781 * Returns the total number of times startService() has been called.
782 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700783 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 */
785 public abstract int getStarts(int which);
786
787 /**
788 * Returns the total number times the service has been launched.
789 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700790 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 */
792 public abstract int getLaunches(int which);
793 }
794 }
795 }
796
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800797 public static final class LevelStepTracker {
798 public long mLastStepTime = -1;
799 public int mNumStepDurations;
800 public final long[] mStepDurations;
801
802 public LevelStepTracker(int maxLevelSteps) {
803 mStepDurations = new long[maxLevelSteps];
804 }
805
806 public LevelStepTracker(int numSteps, long[] steps) {
807 mNumStepDurations = numSteps;
808 mStepDurations = new long[numSteps];
809 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
810 }
811
812 public long getDurationAt(int index) {
813 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
814 }
815
816 public int getLevelAt(int index) {
817 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
818 >> STEP_LEVEL_LEVEL_SHIFT);
819 }
820
821 public int getInitModeAt(int index) {
822 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
823 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
824 }
825
826 public int getModModeAt(int index) {
827 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
828 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
829 }
830
831 private void appendHex(long val, int topOffset, StringBuilder out) {
832 boolean hasData = false;
833 while (topOffset >= 0) {
834 int digit = (int)( (val>>topOffset) & 0xf );
835 topOffset -= 4;
836 if (!hasData && digit == 0) {
837 continue;
838 }
839 hasData = true;
840 if (digit >= 0 && digit <= 9) {
841 out.append((char)('0' + digit));
842 } else {
843 out.append((char)('a' + digit - 10));
844 }
845 }
846 }
847
848 public void encodeEntryAt(int index, StringBuilder out) {
849 long item = mStepDurations[index];
850 long duration = item & STEP_LEVEL_TIME_MASK;
851 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
852 >> STEP_LEVEL_LEVEL_SHIFT);
853 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
854 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
855 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
856 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
857 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
858 case Display.STATE_OFF: out.append('f'); break;
859 case Display.STATE_ON: out.append('o'); break;
860 case Display.STATE_DOZE: out.append('d'); break;
861 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
862 }
863 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
864 out.append('p');
865 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700866 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
867 out.append('i');
868 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800869 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
870 case Display.STATE_OFF: out.append('F'); break;
871 case Display.STATE_ON: out.append('O'); break;
872 case Display.STATE_DOZE: out.append('D'); break;
873 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
874 }
875 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
876 out.append('P');
877 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700878 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
879 out.append('I');
880 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800881 out.append('-');
882 appendHex(level, 4, out);
883 out.append('-');
884 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
885 }
886
887 public void decodeEntryAt(int index, String value) {
888 final int N = value.length();
889 int i = 0;
890 char c;
891 long out = 0;
892 while (i < N && (c=value.charAt(i)) != '-') {
893 i++;
894 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800895 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800896 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800897 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800898 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800899 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800900 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800901 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
902 << STEP_LEVEL_INITIAL_MODE_SHIFT);
903 break;
904 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
905 << STEP_LEVEL_INITIAL_MODE_SHIFT);
906 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700907 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
908 << STEP_LEVEL_INITIAL_MODE_SHIFT);
909 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800910 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
911 break;
912 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
913 break;
914 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
915 break;
916 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
917 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
918 break;
919 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
920 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800921 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700922 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
923 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
924 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800925 }
926 }
927 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800928 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800929 while (i < N && (c=value.charAt(i)) != '-') {
930 i++;
931 level <<= 4;
932 if (c >= '0' && c <= '9') {
933 level += c - '0';
934 } else if (c >= 'a' && c <= 'f') {
935 level += c - 'a' + 10;
936 } else if (c >= 'A' && c <= 'F') {
937 level += c - 'A' + 10;
938 }
939 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800940 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800941 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
942 long duration = 0;
943 while (i < N && (c=value.charAt(i)) != '-') {
944 i++;
945 duration <<= 4;
946 if (c >= '0' && c <= '9') {
947 duration += c - '0';
948 } else if (c >= 'a' && c <= 'f') {
949 duration += c - 'a' + 10;
950 } else if (c >= 'A' && c <= 'F') {
951 duration += c - 'A' + 10;
952 }
953 }
954 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
955 }
956
957 public void init() {
958 mLastStepTime = -1;
959 mNumStepDurations = 0;
960 }
961
962 public void clearTime() {
963 mLastStepTime = -1;
964 }
965
966 public long computeTimePerLevel() {
967 final long[] steps = mStepDurations;
968 final int numSteps = mNumStepDurations;
969
970 // For now we'll do a simple average across all steps.
971 if (numSteps <= 0) {
972 return -1;
973 }
974 long total = 0;
975 for (int i=0; i<numSteps; i++) {
976 total += steps[i] & STEP_LEVEL_TIME_MASK;
977 }
978 return total / numSteps;
979 /*
980 long[] buckets = new long[numSteps];
981 int numBuckets = 0;
982 int numToAverage = 4;
983 int i = 0;
984 while (i < numSteps) {
985 long totalTime = 0;
986 int num = 0;
987 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
988 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
989 num++;
990 }
991 buckets[numBuckets] = totalTime / num;
992 numBuckets++;
993 numToAverage *= 2;
994 i += num;
995 }
996 if (numBuckets < 1) {
997 return -1;
998 }
999 long averageTime = buckets[numBuckets-1];
1000 for (i=numBuckets-2; i>=0; i--) {
1001 averageTime = (averageTime + buckets[i]) / 2;
1002 }
1003 return averageTime;
1004 */
1005 }
1006
1007 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1008 int[] outNumOfInterest) {
1009 final long[] steps = mStepDurations;
1010 final int count = mNumStepDurations;
1011 if (count <= 0) {
1012 return -1;
1013 }
1014 long total = 0;
1015 int numOfInterest = 0;
1016 for (int i=0; i<count; i++) {
1017 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1018 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1019 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1020 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1021 // If the modes of interest didn't change during this step period...
1022 if ((modMode&modesOfInterest) == 0) {
1023 // And the mode values during this period match those we are measuring...
1024 if ((initMode&modesOfInterest) == modeValues) {
1025 // Then this can be used to estimate the total time!
1026 numOfInterest++;
1027 total += steps[i] & STEP_LEVEL_TIME_MASK;
1028 }
1029 }
1030 }
1031 if (numOfInterest <= 0) {
1032 return -1;
1033 }
1034
1035 if (outNumOfInterest != null) {
1036 outNumOfInterest[0] = numOfInterest;
1037 }
1038
1039 // The estimated time is the average time we spend in each level, multipled
1040 // by 100 -- the total number of battery levels
1041 return (total / numOfInterest) * 100;
1042 }
1043
1044 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1045 int stepCount = mNumStepDurations;
1046 final long lastStepTime = mLastStepTime;
1047 if (lastStepTime >= 0 && numStepLevels > 0) {
1048 final long[] steps = mStepDurations;
1049 long duration = elapsedRealtime - lastStepTime;
1050 for (int i=0; i<numStepLevels; i++) {
1051 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1052 long thisDuration = duration / (numStepLevels-i);
1053 duration -= thisDuration;
1054 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1055 thisDuration = STEP_LEVEL_TIME_MASK;
1056 }
1057 steps[0] = thisDuration | modeBits;
1058 }
1059 stepCount += numStepLevels;
1060 if (stepCount > steps.length) {
1061 stepCount = steps.length;
1062 }
1063 }
1064 mNumStepDurations = stepCount;
1065 mLastStepTime = elapsedRealtime;
1066 }
1067
1068 public void readFromParcel(Parcel in) {
1069 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001070 if (N > mStepDurations.length) {
1071 throw new ParcelFormatException("more step durations than available: " + N);
1072 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001073 mNumStepDurations = N;
1074 for (int i=0; i<N; i++) {
1075 mStepDurations[i] = in.readLong();
1076 }
1077 }
1078
1079 public void writeToParcel(Parcel out) {
1080 final int N = mNumStepDurations;
1081 out.writeInt(N);
1082 for (int i=0; i<N; i++) {
1083 out.writeLong(mStepDurations[i]);
1084 }
1085 }
1086 }
1087
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001088 public static final class PackageChange {
1089 public String mPackageName;
1090 public boolean mUpdate;
1091 public int mVersionCode;
1092 }
1093
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001094 public static final class DailyItem {
1095 public long mStartTime;
1096 public long mEndTime;
1097 public LevelStepTracker mDischargeSteps;
1098 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001099 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001100 }
1101
1102 public abstract DailyItem getDailyItemLocked(int daysAgo);
1103
1104 public abstract long getCurrentDailyStartTime();
1105
1106 public abstract long getNextMinDailyDeadline();
1107
1108 public abstract long getNextMaxDailyDeadline();
1109
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001110 public abstract long[] getCpuFreqs();
1111
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001112 public final static class HistoryTag {
1113 public String string;
1114 public int uid;
1115
1116 public int poolIdx;
1117
1118 public void setTo(HistoryTag o) {
1119 string = o.string;
1120 uid = o.uid;
1121 poolIdx = o.poolIdx;
1122 }
1123
1124 public void setTo(String _string, int _uid) {
1125 string = _string;
1126 uid = _uid;
1127 poolIdx = -1;
1128 }
1129
1130 public void writeToParcel(Parcel dest, int flags) {
1131 dest.writeString(string);
1132 dest.writeInt(uid);
1133 }
1134
1135 public void readFromParcel(Parcel src) {
1136 string = src.readString();
1137 uid = src.readInt();
1138 poolIdx = -1;
1139 }
1140
1141 @Override
1142 public boolean equals(Object o) {
1143 if (this == o) return true;
1144 if (o == null || getClass() != o.getClass()) return false;
1145
1146 HistoryTag that = (HistoryTag) o;
1147
1148 if (uid != that.uid) return false;
1149 if (!string.equals(that.string)) return false;
1150
1151 return true;
1152 }
1153
1154 @Override
1155 public int hashCode() {
1156 int result = string.hashCode();
1157 result = 31 * result + uid;
1158 return result;
1159 }
1160 }
1161
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001162 /**
1163 * Optional detailed information that can go into a history step. This is typically
1164 * generated each time the battery level changes.
1165 */
1166 public final static class HistoryStepDetails {
1167 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1168 public int userTime;
1169 public int systemTime;
1170
1171 // Top three apps using CPU in the last step, with times in 1/100 second.
1172 public int appCpuUid1;
1173 public int appCpuUTime1;
1174 public int appCpuSTime1;
1175 public int appCpuUid2;
1176 public int appCpuUTime2;
1177 public int appCpuSTime2;
1178 public int appCpuUid3;
1179 public int appCpuUTime3;
1180 public int appCpuSTime3;
1181
1182 // Information from /proc/stat
1183 public int statUserTime;
1184 public int statSystemTime;
1185 public int statIOWaitTime;
1186 public int statIrqTime;
1187 public int statSoftIrqTime;
1188 public int statIdlTime;
1189
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001190 // Platform-level low power state stats
1191 public String statPlatformIdleState;
1192
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001193 public HistoryStepDetails() {
1194 clear();
1195 }
1196
1197 public void clear() {
1198 userTime = systemTime = 0;
1199 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1200 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1201 = appCpuUTime3 = appCpuSTime3 = 0;
1202 }
1203
1204 public void writeToParcel(Parcel out) {
1205 out.writeInt(userTime);
1206 out.writeInt(systemTime);
1207 out.writeInt(appCpuUid1);
1208 out.writeInt(appCpuUTime1);
1209 out.writeInt(appCpuSTime1);
1210 out.writeInt(appCpuUid2);
1211 out.writeInt(appCpuUTime2);
1212 out.writeInt(appCpuSTime2);
1213 out.writeInt(appCpuUid3);
1214 out.writeInt(appCpuUTime3);
1215 out.writeInt(appCpuSTime3);
1216 out.writeInt(statUserTime);
1217 out.writeInt(statSystemTime);
1218 out.writeInt(statIOWaitTime);
1219 out.writeInt(statIrqTime);
1220 out.writeInt(statSoftIrqTime);
1221 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001222 out.writeString(statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001223 }
1224
1225 public void readFromParcel(Parcel in) {
1226 userTime = in.readInt();
1227 systemTime = in.readInt();
1228 appCpuUid1 = in.readInt();
1229 appCpuUTime1 = in.readInt();
1230 appCpuSTime1 = in.readInt();
1231 appCpuUid2 = in.readInt();
1232 appCpuUTime2 = in.readInt();
1233 appCpuSTime2 = in.readInt();
1234 appCpuUid3 = in.readInt();
1235 appCpuUTime3 = in.readInt();
1236 appCpuSTime3 = in.readInt();
1237 statUserTime = in.readInt();
1238 statSystemTime = in.readInt();
1239 statIOWaitTime = in.readInt();
1240 statIrqTime = in.readInt();
1241 statSoftIrqTime = in.readInt();
1242 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001243 statPlatformIdleState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001244 }
1245 }
1246
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001247 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001248 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001249
1250 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001251 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001252
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001253 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001254 public static final byte CMD_NULL = -1;
1255 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001256 public static final byte CMD_CURRENT_TIME = 5;
1257 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001258 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001259 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001260
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001261 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001262
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001263 /**
1264 * Return whether the command code is a delta data update.
1265 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001266 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001267 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001268 }
1269
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001270 public byte batteryLevel;
1271 public byte batteryStatus;
1272 public byte batteryHealth;
1273 public byte batteryPlugType;
1274
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001275 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001276 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001277
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001278 // The charge of the battery in micro-Ampere-hours.
1279 public int batteryChargeUAh;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001280
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001281 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001282 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001283 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001284 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001285 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1286 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001287 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001288 public static final int STATE_PHONE_STATE_SHIFT = 6;
1289 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001290 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001291 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001292 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001293
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001294 // These states always appear directly in the first int token
1295 // of a delta change; they should be ones that change relatively
1296 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001297 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1298 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001299 public static final int STATE_GPS_ON_FLAG = 1<<29;
1300 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001301 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001302 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001303 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001304 // Do not use, this is used for coulomb delta count.
1305 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001306 // These are on the lower bits used for the command; if they change
1307 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001308 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001309 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001310 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001311 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1312 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1313 // empty slot
1314 // empty slot
1315 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001316
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001317 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001318 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1319
1320 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001321
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001322 public int states;
1323
Dianne Hackborn3251b902014-06-20 14:40:53 -07001324 // Constants from WIFI_SUPPL_STATE_*
1325 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1326 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1327 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1328 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1329 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1330 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1331
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001332 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001333 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1334 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1335 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001336 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001337 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1338 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1339 public static final int STATE2_CHARGING_FLAG = 1<<24;
1340 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1341 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1342 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001343 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001344
1345 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001346 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001347 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1348
1349 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001350
Dianne Hackborn40c87252014-03-19 16:55:40 -07001351 public int states2;
1352
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001353 // The wake lock that was acquired at this point.
1354 public HistoryTag wakelockTag;
1355
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001356 // Kernel wakeup reason at this point.
1357 public HistoryTag wakeReasonTag;
1358
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001359 // Non-null when there is more detailed information at this step.
1360 public HistoryStepDetails stepDetails;
1361
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001362 public static final int EVENT_FLAG_START = 0x8000;
1363 public static final int EVENT_FLAG_FINISH = 0x4000;
1364
1365 // No event in this item.
1366 public static final int EVENT_NONE = 0x0000;
1367 // Event is about a process that is running.
1368 public static final int EVENT_PROC = 0x0001;
1369 // Event is about an application package that is in the foreground.
1370 public static final int EVENT_FOREGROUND = 0x0002;
1371 // Event is about an application package that is at the top of the screen.
1372 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001373 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001374 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001375 // Events for all additional wake locks aquired/release within a wake block.
1376 // These are not generated by default.
1377 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001378 // Event is about an application executing a scheduled job.
1379 public static final int EVENT_JOB = 0x0006;
1380 // Events for users running.
1381 public static final int EVENT_USER_RUNNING = 0x0007;
1382 // Events for foreground user.
1383 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001384 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001385 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001386 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001387 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001388 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001389 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001390 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001391 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001392 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001393 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001394 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001395 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001396 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001397 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001398 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001399 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001400 // Event for a package being on the temporary whitelist.
1401 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001402 // Event for the screen waking up.
1403 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001404 // Event for the UID that woke up the application processor.
1405 // Used for wakeups coming from WiFi, modem, etc.
1406 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001407 // Event for reporting that a specific partial wake lock has been held for a long duration.
1408 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001409
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001410 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001411 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001412 // Mask to extract out only the type part of the event.
1413 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001414
1415 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1416 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1417 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1418 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1419 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1420 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001421 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1422 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001423 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1424 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001425 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1426 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1427 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1428 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1429 public static final int EVENT_USER_FOREGROUND_START =
1430 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1431 public static final int EVENT_USER_FOREGROUND_FINISH =
1432 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001433 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1434 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001435 public static final int EVENT_TEMP_WHITELIST_START =
1436 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1437 public static final int EVENT_TEMP_WHITELIST_FINISH =
1438 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001439 public static final int EVENT_LONG_WAKE_LOCK_START =
1440 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1441 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1442 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001443
1444 // For CMD_EVENT.
1445 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001446 public HistoryTag eventTag;
1447
Dianne Hackborn9a755432014-05-15 17:05:22 -07001448 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001449 public long currentTime;
1450
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001451 // Meta-data when reading.
1452 public int numReadInts;
1453
1454 // Pre-allocated objects.
1455 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001456 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001457 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001458
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001459 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001460 }
1461
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001462 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001463 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001464 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001465 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001466 }
1467
1468 public int describeContents() {
1469 return 0;
1470 }
1471
1472 public void writeToParcel(Parcel dest, int flags) {
1473 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001474 int bat = (((int)cmd)&0xff)
1475 | ((((int)batteryLevel)<<8)&0xff00)
1476 | ((((int)batteryStatus)<<16)&0xf0000)
1477 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001478 | ((((int)batteryPlugType)<<24)&0xf000000)
1479 | (wakelockTag != null ? 0x10000000 : 0)
1480 | (wakeReasonTag != null ? 0x20000000 : 0)
1481 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001482 dest.writeInt(bat);
1483 bat = (((int)batteryTemperature)&0xffff)
1484 | ((((int)batteryVoltage)<<16)&0xffff0000);
1485 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001486 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001487 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001488 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001489 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001490 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001491 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001492 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001493 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001494 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001495 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001496 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001497 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001498 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001499 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001500 dest.writeLong(currentTime);
1501 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001502 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001503
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001504 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001505 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001506 int bat = src.readInt();
1507 cmd = (byte)(bat&0xff);
1508 batteryLevel = (byte)((bat>>8)&0xff);
1509 batteryStatus = (byte)((bat>>16)&0xf);
1510 batteryHealth = (byte)((bat>>20)&0xf);
1511 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001512 int bat2 = src.readInt();
1513 batteryTemperature = (short)(bat2&0xffff);
1514 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001515 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001516 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001517 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001518 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001519 wakelockTag = localWakelockTag;
1520 wakelockTag.readFromParcel(src);
1521 } else {
1522 wakelockTag = null;
1523 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001524 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001525 wakeReasonTag = localWakeReasonTag;
1526 wakeReasonTag.readFromParcel(src);
1527 } else {
1528 wakeReasonTag = null;
1529 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001530 if ((bat&0x40000000) != 0) {
1531 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001532 eventTag = localEventTag;
1533 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001534 } else {
1535 eventCode = EVENT_NONE;
1536 eventTag = null;
1537 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001538 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001539 currentTime = src.readLong();
1540 } else {
1541 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001542 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001543 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001544 }
1545
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001546 public void clear() {
1547 time = 0;
1548 cmd = CMD_NULL;
1549 batteryLevel = 0;
1550 batteryStatus = 0;
1551 batteryHealth = 0;
1552 batteryPlugType = 0;
1553 batteryTemperature = 0;
1554 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001555 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001556 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001557 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001558 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001559 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001560 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001561 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001562 }
1563
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001564 public void setTo(HistoryItem o) {
1565 time = o.time;
1566 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001567 setToCommon(o);
1568 }
1569
1570 public void setTo(long time, byte cmd, HistoryItem o) {
1571 this.time = time;
1572 this.cmd = cmd;
1573 setToCommon(o);
1574 }
1575
1576 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001577 batteryLevel = o.batteryLevel;
1578 batteryStatus = o.batteryStatus;
1579 batteryHealth = o.batteryHealth;
1580 batteryPlugType = o.batteryPlugType;
1581 batteryTemperature = o.batteryTemperature;
1582 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001583 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001584 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001585 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001586 if (o.wakelockTag != null) {
1587 wakelockTag = localWakelockTag;
1588 wakelockTag.setTo(o.wakelockTag);
1589 } else {
1590 wakelockTag = null;
1591 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001592 if (o.wakeReasonTag != null) {
1593 wakeReasonTag = localWakeReasonTag;
1594 wakeReasonTag.setTo(o.wakeReasonTag);
1595 } else {
1596 wakeReasonTag = null;
1597 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001598 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001599 if (o.eventTag != null) {
1600 eventTag = localEventTag;
1601 eventTag.setTo(o.eventTag);
1602 } else {
1603 eventTag = null;
1604 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001605 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001606 }
1607
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001608 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001609 return batteryLevel == o.batteryLevel
1610 && batteryStatus == o.batteryStatus
1611 && batteryHealth == o.batteryHealth
1612 && batteryPlugType == o.batteryPlugType
1613 && batteryTemperature == o.batteryTemperature
1614 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001615 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001616 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001617 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001618 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001619 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001620
1621 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001622 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001623 return false;
1624 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001625 if (wakelockTag != o.wakelockTag) {
1626 if (wakelockTag == null || o.wakelockTag == null) {
1627 return false;
1628 }
1629 if (!wakelockTag.equals(o.wakelockTag)) {
1630 return false;
1631 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001632 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001633 if (wakeReasonTag != o.wakeReasonTag) {
1634 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1635 return false;
1636 }
1637 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1638 return false;
1639 }
1640 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001641 if (eventTag != o.eventTag) {
1642 if (eventTag == null || o.eventTag == null) {
1643 return false;
1644 }
1645 if (!eventTag.equals(o.eventTag)) {
1646 return false;
1647 }
1648 }
1649 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001650 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001651 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001652
1653 public final static class HistoryEventTracker {
1654 private final HashMap<String, SparseIntArray>[] mActiveEvents
1655 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1656
1657 public boolean updateState(int code, String name, int uid, int poolIdx) {
1658 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1659 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1660 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1661 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001662 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001663 mActiveEvents[idx] = active;
1664 }
1665 SparseIntArray uids = active.get(name);
1666 if (uids == null) {
1667 uids = new SparseIntArray();
1668 active.put(name, uids);
1669 }
1670 if (uids.indexOfKey(uid) >= 0) {
1671 // Already set, nothing to do!
1672 return false;
1673 }
1674 uids.put(uid, poolIdx);
1675 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1676 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1677 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1678 if (active == null) {
1679 // not currently active, nothing to do.
1680 return false;
1681 }
1682 SparseIntArray uids = active.get(name);
1683 if (uids == null) {
1684 // not currently active, nothing to do.
1685 return false;
1686 }
1687 idx = uids.indexOfKey(uid);
1688 if (idx < 0) {
1689 // not currently active, nothing to do.
1690 return false;
1691 }
1692 uids.removeAt(idx);
1693 if (uids.size() <= 0) {
1694 active.remove(name);
1695 }
1696 }
1697 return true;
1698 }
1699
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001700 public void removeEvents(int code) {
1701 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1702 mActiveEvents[idx] = null;
1703 }
1704
Dianne Hackborn37de0982014-05-09 09:32:18 -07001705 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1706 return mActiveEvents[code];
1707 }
1708 }
1709
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001710 public static final class BitDescription {
1711 public final int mask;
1712 public final int shift;
1713 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001714 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001715 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001716 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001717
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001718 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001719 this.mask = mask;
1720 this.shift = -1;
1721 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001722 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001723 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001724 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001725 }
1726
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001727 public BitDescription(int mask, int shift, String name, String shortName,
1728 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001729 this.mask = mask;
1730 this.shift = shift;
1731 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001732 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001733 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001734 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001735 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001736 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001737
Dianne Hackbornfc064132014-06-02 12:42:12 -07001738 /**
1739 * Don't allow any more batching in to the current history event. This
1740 * is called when printing partial histories, so to ensure that the next
1741 * history event will go in to a new batch after what was printed in the
1742 * last partial history.
1743 */
1744 public abstract void commitCurrentHistoryBatchLocked();
1745
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001746 public abstract int getHistoryTotalSize();
1747
1748 public abstract int getHistoryUsedSize();
1749
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001750 public abstract boolean startIteratingHistoryLocked();
1751
Dianne Hackborn099bc622014-01-22 13:39:16 -08001752 public abstract int getHistoryStringPoolSize();
1753
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001754 public abstract int getHistoryStringPoolBytes();
1755
1756 public abstract String getHistoryTagPoolString(int index);
1757
1758 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001759
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001760 public abstract boolean getNextHistoryLocked(HistoryItem out);
1761
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001762 public abstract void finishIteratingHistoryLocked();
1763
1764 public abstract boolean startIteratingOldHistoryLocked();
1765
1766 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1767
1768 public abstract void finishIteratingOldHistoryLocked();
1769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001771 * Return the base time offset for the battery history.
1772 */
1773 public abstract long getHistoryBaseTime();
1774
1775 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 * Returns the number of times the device has been started.
1777 */
1778 public abstract int getStartCount();
1779
1780 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001781 * 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 -08001782 * running on battery.
1783 *
1784 * {@hide}
1785 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001786 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001788 /**
1789 * Returns the number of times the screen was turned on.
1790 *
1791 * {@hide}
1792 */
1793 public abstract int getScreenOnCount(int which);
1794
Jeff Browne95c3cd2014-05-02 16:59:26 -07001795 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1796
Dianne Hackborn617f8772009-03-31 15:04:46 -07001797 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1798 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1799 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1800 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1801 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
1802
1803 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1804 "dark", "dim", "medium", "light", "bright"
1805 };
1806
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001807 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1808 "0", "1", "2", "3", "4"
1809 };
1810
Dianne Hackborn617f8772009-03-31 15:04:46 -07001811 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001812
Dianne Hackborn617f8772009-03-31 15:04:46 -07001813 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001814 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001815 * the given brightness
1816 *
1817 * {@hide}
1818 */
1819 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001820 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001823 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001824 * running on battery.
1825 *
1826 * {@hide}
1827 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001828 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001829
1830 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001831 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001832 *
1833 * {@hide}
1834 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001835 public abstract int getPowerSaveModeEnabledCount(int which);
1836
1837 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001838 * Constant for device idle mode: not active.
1839 */
1840 public static final int DEVICE_IDLE_MODE_OFF = 0;
1841
1842 /**
1843 * Constant for device idle mode: active in lightweight mode.
1844 */
1845 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1846
1847 /**
1848 * Constant for device idle mode: active in full mode.
1849 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001850 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001851
1852 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001853 * Returns the time in microseconds that device has been in idle mode while
1854 * running on battery.
1855 *
1856 * {@hide}
1857 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001858 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001859
1860 /**
1861 * Returns the number of times that the devie has gone in to idle mode.
1862 *
1863 * {@hide}
1864 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001865 public abstract int getDeviceIdleModeCount(int mode, int which);
1866
1867 /**
1868 * Return the longest duration we spent in a particular device idle mode (fully in the
1869 * mode, not in idle maintenance etc).
1870 */
1871 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001872
1873 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001874 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001875 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001876 * counts all of the time that we consider the device to be idle, whether or not
1877 * it is currently in the actual device idle mode.
1878 *
1879 * {@hide}
1880 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001881 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001882
1883 /**
1884 * Returns the number of times that the devie has started idling.
1885 *
1886 * {@hide}
1887 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001888 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001889
1890 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001891 * Returns the number of times that connectivity state changed.
1892 *
1893 * {@hide}
1894 */
1895 public abstract int getNumConnectivityChange(int which);
1896
1897 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001898 * 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 -08001899 * running on battery.
1900 *
1901 * {@hide}
1902 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001903 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001904
1905 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001906 * Returns the number of times a phone call was activated.
1907 *
1908 * {@hide}
1909 */
1910 public abstract int getPhoneOnCount(int which);
1911
1912 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001913 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001914 * the given signal strength.
1915 *
1916 * {@hide}
1917 */
1918 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001919 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001920
Dianne Hackborn617f8772009-03-31 15:04:46 -07001921 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001922 * Returns the time in microseconds that the phone has been trying to
1923 * acquire a signal.
1924 *
1925 * {@hide}
1926 */
1927 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001928 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001929
1930 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001931 * Returns the number of times the phone has entered the given signal strength.
1932 *
1933 * {@hide}
1934 */
1935 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1936
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001937 /**
1938 * Returns the time in microseconds that the mobile network has been active
1939 * (in a high power state).
1940 *
1941 * {@hide}
1942 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001943 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001944
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001945 /**
1946 * Returns the number of times that the mobile network has transitioned to the
1947 * active state.
1948 *
1949 * {@hide}
1950 */
1951 public abstract int getMobileRadioActiveCount(int which);
1952
1953 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001954 * Returns the time in microseconds that is the difference between the mobile radio
1955 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1956 * from the radio.
1957 *
1958 * {@hide}
1959 */
1960 public abstract long getMobileRadioActiveAdjustedTime(int which);
1961
1962 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001963 * Returns the time in microseconds that the mobile network has been active
1964 * (in a high power state) but not being able to blame on an app.
1965 *
1966 * {@hide}
1967 */
1968 public abstract long getMobileRadioActiveUnknownTime(int which);
1969
1970 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001971 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001972 *
1973 * {@hide}
1974 */
1975 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001976
Dianne Hackborn627bba72009-03-24 22:32:56 -07001977 public static final int DATA_CONNECTION_NONE = 0;
1978 public static final int DATA_CONNECTION_GPRS = 1;
1979 public static final int DATA_CONNECTION_EDGE = 2;
1980 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001981 public static final int DATA_CONNECTION_CDMA = 4;
1982 public static final int DATA_CONNECTION_EVDO_0 = 5;
1983 public static final int DATA_CONNECTION_EVDO_A = 6;
1984 public static final int DATA_CONNECTION_1xRTT = 7;
1985 public static final int DATA_CONNECTION_HSDPA = 8;
1986 public static final int DATA_CONNECTION_HSUPA = 9;
1987 public static final int DATA_CONNECTION_HSPA = 10;
1988 public static final int DATA_CONNECTION_IDEN = 11;
1989 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001990 public static final int DATA_CONNECTION_LTE = 13;
1991 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08001992 public static final int DATA_CONNECTION_HSPAP = 15;
1993 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07001994
Dianne Hackborn627bba72009-03-24 22:32:56 -07001995 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001996 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07001997 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08001998 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07001999 };
2000
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002001 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -07002002
2003 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002004 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002005 * the given data connection.
2006 *
2007 * {@hide}
2008 */
2009 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002010 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002013 * Returns the number of times the phone has entered the given data
2014 * connection type.
2015 *
2016 * {@hide}
2017 */
2018 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002019
Dianne Hackborn3251b902014-06-20 14:40:53 -07002020 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2021 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2022 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2023 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2024 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2025 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2026 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2027 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2028 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2029 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2030 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2031 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2032 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2033
2034 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2035
2036 static final String[] WIFI_SUPPL_STATE_NAMES = {
2037 "invalid", "disconn", "disabled", "inactive", "scanning",
2038 "authenticating", "associating", "associated", "4-way-handshake",
2039 "group-handshake", "completed", "dormant", "uninit"
2040 };
2041
2042 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2043 "inv", "dsc", "dis", "inact", "scan",
2044 "auth", "ascing", "asced", "4-way",
2045 "group", "compl", "dorm", "uninit"
2046 };
2047
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002048 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2049 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002050 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002051 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2052 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002053 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002054 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2055 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2056 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002057 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002058 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002059 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002060 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002061 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2062 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002063 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2064 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2065 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2066 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2067 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2068 new String[] {"in", "out", "emergency", "off"},
2069 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002070 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2071 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2072 SignalStrength.SIGNAL_STRENGTH_NAMES,
2073 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002074 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2075 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2076 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002077 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002078
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002079 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2080 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002081 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002082 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002083 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002084 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002085 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002086 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2087 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2088 new String[] { "off", "light", "full", "???" },
2089 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002090 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2091 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2092 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002093 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2094 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2095 new String[] { "0", "1", "2", "3", "4" },
2096 new String[] { "0", "1", "2", "3", "4" }),
2097 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2098 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2099 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002100 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002101 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002102 };
2103
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002104 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002105 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002106 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2107 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002108 };
2109
2110 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002111 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002112 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002113 "Esw", "Ewa", "Elw", "Eec"
2114 };
2115
2116 @FunctionalInterface
2117 public interface IntToString {
2118 String applyAsString(int val);
2119 }
2120
2121 private static final IntToString sUidToString = UserHandle::formatUid;
2122 private static final IntToString sIntToString = Integer::toString;
2123
2124 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2125 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2126 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2127 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2128 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002129 };
2130
Dianne Hackborn617f8772009-03-31 15:04:46 -07002131 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002132 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002133 * running on battery.
2134 *
2135 * {@hide}
2136 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002137 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002138
2139 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002140 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002141 * been in the running state while the device was running on battery.
2142 *
2143 * {@hide}
2144 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002145 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002146
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002147 public static final int WIFI_STATE_OFF = 0;
2148 public static final int WIFI_STATE_OFF_SCANNING = 1;
2149 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2150 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2151 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2152 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2153 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2154 public static final int WIFI_STATE_SOFT_AP = 7;
2155
2156 static final String[] WIFI_STATE_NAMES = {
2157 "off", "scanning", "no_net", "disconn",
2158 "sta", "p2p", "sta_p2p", "soft_ap"
2159 };
2160
2161 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2162
2163 /**
2164 * Returns the time in microseconds that WiFi has been running in the given state.
2165 *
2166 * {@hide}
2167 */
2168 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002169 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002170
2171 /**
2172 * Returns the number of times that WiFi has entered the given state.
2173 *
2174 * {@hide}
2175 */
2176 public abstract int getWifiStateCount(int wifiState, int which);
2177
The Android Open Source Project10592532009-03-18 17:39:46 -07002178 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002179 * Returns the time in microseconds that the wifi supplicant has been
2180 * in a given state.
2181 *
2182 * {@hide}
2183 */
2184 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2185
2186 /**
2187 * Returns the number of times that the wifi supplicant has transitioned
2188 * to a given state.
2189 *
2190 * {@hide}
2191 */
2192 public abstract int getWifiSupplStateCount(int state, int which);
2193
2194 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2195
2196 /**
2197 * Returns the time in microseconds that WIFI has been running with
2198 * the given signal strength.
2199 *
2200 * {@hide}
2201 */
2202 public abstract long getWifiSignalStrengthTime(int strengthBin,
2203 long elapsedRealtimeUs, int which);
2204
2205 /**
2206 * Returns the number of times WIFI has entered the given signal strength.
2207 *
2208 * {@hide}
2209 */
2210 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2211
2212 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002213 * Returns the time in microseconds that the flashlight has been on while the device was
2214 * running on battery.
2215 *
2216 * {@hide}
2217 */
2218 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2219
2220 /**
2221 * Returns the number of times that the flashlight has been turned on while the device was
2222 * running on battery.
2223 *
2224 * {@hide}
2225 */
2226 public abstract long getFlashlightOnCount(int which);
2227
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002228 /**
2229 * Returns the time in microseconds that the camera has been on while the device was
2230 * running on battery.
2231 *
2232 * {@hide}
2233 */
2234 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2235
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002236 /**
2237 * Returns the time in microseconds that bluetooth scans were running while the device was
2238 * on battery.
2239 *
2240 * {@hide}
2241 */
2242 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002243
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002244 public static final int NETWORK_MOBILE_RX_DATA = 0;
2245 public static final int NETWORK_MOBILE_TX_DATA = 1;
2246 public static final int NETWORK_WIFI_RX_DATA = 2;
2247 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002248 public static final int NETWORK_BT_RX_DATA = 4;
2249 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002250 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2251 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2252 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2253 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2254 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002255
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002256 public abstract long getNetworkActivityBytes(int type, int which);
2257 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002258
Adam Lesinskie08af192015-03-25 16:42:59 -07002259 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002260 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002261 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002262 * actual power data.
2263 */
2264 public abstract boolean hasWifiActivityReporting();
2265
2266 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002267 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2268 * in various radio controller states, such as transmit, receive, and idle.
2269 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002270 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002271 public abstract ControllerActivityCounter getWifiControllerActivity();
2272
2273 /**
2274 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2275 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2276 * actual power data.
2277 */
2278 public abstract boolean hasBluetoothActivityReporting();
2279
2280 /**
2281 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2282 * in various radio controller states, such as transmit, receive, and idle.
2283 * @return non-null {@link ControllerActivityCounter}
2284 */
2285 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2286
2287 /**
2288 * Returns true if the BatteryStats object has detailed modem power reports.
2289 * When true, calling {@link #getModemControllerActivity()} will yield the
2290 * actual power data.
2291 */
2292 public abstract boolean hasModemActivityReporting();
2293
2294 /**
2295 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2296 * in various radio controller states, such as transmit, receive, and idle.
2297 * @return non-null {@link ControllerActivityCounter}
2298 */
2299 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002300
The Android Open Source Project10592532009-03-18 17:39:46 -07002301 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002302 * Return the wall clock time when battery stats data collection started.
2303 */
2304 public abstract long getStartClockTime();
2305
2306 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002307 * Return platform version tag that we were running in when the battery stats started.
2308 */
2309 public abstract String getStartPlatformVersion();
2310
2311 /**
2312 * Return platform version tag that we were running in when the battery stats ended.
2313 */
2314 public abstract String getEndPlatformVersion();
2315
2316 /**
2317 * Return the internal version code of the parcelled format.
2318 */
2319 public abstract int getParcelVersion();
2320
2321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 * Return whether we are currently running on battery.
2323 */
2324 public abstract boolean getIsOnBattery();
2325
2326 /**
2327 * Returns a SparseArray containing the statistics for each uid.
2328 */
2329 public abstract SparseArray<? extends Uid> getUidStats();
2330
2331 /**
2332 * Returns the current battery uptime in microseconds.
2333 *
2334 * @param curTime the amount of elapsed realtime in microseconds.
2335 */
2336 public abstract long getBatteryUptime(long curTime);
2337
2338 /**
2339 * Returns the current battery realtime in microseconds.
2340 *
2341 * @param curTime the amount of elapsed realtime in microseconds.
2342 */
2343 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07002344
2345 /**
Evan Millar633a1742009-04-02 16:36:33 -07002346 * Returns the battery percentage level at the last time the device was unplugged from power, or
2347 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002348 */
Evan Millar633a1742009-04-02 16:36:33 -07002349 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07002350
2351 /**
Evan Millar633a1742009-04-02 16:36:33 -07002352 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2353 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002354 */
Evan Millar633a1742009-04-02 16:36:33 -07002355 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356
2357 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002358 * Get the amount the battery has discharged since the stats were
2359 * last reset after charging, as a lower-end approximation.
2360 */
2361 public abstract int getLowDischargeAmountSinceCharge();
2362
2363 /**
2364 * Get the amount the battery has discharged since the stats were
2365 * last reset after charging, as an upper-end approximation.
2366 */
2367 public abstract int getHighDischargeAmountSinceCharge();
2368
2369 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002370 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2371 */
2372 public abstract int getDischargeAmount(int which);
2373
2374 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002375 * Get the amount the battery has discharged while the screen was on,
2376 * since the last time power was unplugged.
2377 */
2378 public abstract int getDischargeAmountScreenOn();
2379
2380 /**
2381 * Get the amount the battery has discharged while the screen was on,
2382 * since the last time the device was charged.
2383 */
2384 public abstract int getDischargeAmountScreenOnSinceCharge();
2385
2386 /**
2387 * Get the amount the battery has discharged while the screen was off,
2388 * since the last time power was unplugged.
2389 */
2390 public abstract int getDischargeAmountScreenOff();
2391
2392 /**
2393 * Get the amount the battery has discharged while the screen was off,
2394 * since the last time the device was charged.
2395 */
2396 public abstract int getDischargeAmountScreenOffSinceCharge();
2397
2398 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 * Returns the total, last, or current battery uptime in microseconds.
2400 *
2401 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002402 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 */
2404 public abstract long computeBatteryUptime(long curTime, int which);
2405
2406 /**
2407 * Returns the total, last, or current battery realtime in microseconds.
2408 *
2409 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002410 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 */
2412 public abstract long computeBatteryRealtime(long curTime, int which);
2413
2414 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002415 * Returns the total, last, or current battery screen off uptime in microseconds.
2416 *
2417 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002418 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002419 */
2420 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2421
2422 /**
2423 * Returns the total, last, or current battery screen off realtime in microseconds.
2424 *
2425 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002426 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002427 */
2428 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2429
2430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 * Returns the total, last, or current uptime in microseconds.
2432 *
2433 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002434 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 */
2436 public abstract long computeUptime(long curTime, int which);
2437
2438 /**
2439 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002440 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002442 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 */
2444 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002445
2446 /**
2447 * Compute an approximation for how much run time (in microseconds) is remaining on
2448 * the battery. Returns -1 if no time can be computed: either there is not
2449 * enough current data to make a decision, or the battery is currently
2450 * charging.
2451 *
2452 * @param curTime The current elepsed realtime in microseconds.
2453 */
2454 public abstract long computeBatteryTimeRemaining(long curTime);
2455
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002456 // The part of a step duration that is the actual time.
2457 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2458
2459 // Bits in a step duration that are the new battery level we are at.
2460 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002461 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002462
2463 // Bits in a step duration that are the initial mode we were in at that step.
2464 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002465 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002466
2467 // Bits in a step duration that indicate which modes changed during that step.
2468 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002469 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002470
2471 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2472 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2473
Santos Cordone94f0502017-02-24 12:31:20 -08002474 // The largest value for screen state that is tracked in battery states. Any values above
2475 // this should be mapped back to one of the tracked values before being tracked here.
2476 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2477
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002478 // Step duration mode: power save is on.
2479 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2480
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002481 // Step duration mode: device is currently in idle mode.
2482 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2483
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002484 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2485 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002486 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2487 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002488 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2489 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2490 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2491 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2492 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002493 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2494 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002495 };
2496 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2497 (Display.STATE_OFF-1),
2498 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002499 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002500 (Display.STATE_ON-1),
2501 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2502 (Display.STATE_DOZE-1),
2503 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2504 (Display.STATE_DOZE_SUSPEND-1),
2505 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002506 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002507 };
2508 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2509 "screen off",
2510 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002511 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002512 "screen on",
2513 "screen on power save",
2514 "screen doze",
2515 "screen doze power save",
2516 "screen doze-suspend",
2517 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002518 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002519 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002520
2521 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002522 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2523 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2524 * a coulomb counter.
2525 */
2526 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2527
2528 /**
2529 * Return the counter keeping track of the amount of battery discharge measured in
2530 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2531 * a coulomb counter.
2532 */
2533 public abstract LongCounter getDischargeCoulombCounter();
2534
2535 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002536 * Returns the estimated real battery capacity, which may be less than the capacity
2537 * declared by the PowerProfile.
2538 * @return The estimated battery capacity in mAh.
2539 */
2540 public abstract int getEstimatedBatteryCapacity();
2541
2542 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002543 * @return The minimum learned battery capacity in uAh.
2544 */
2545 public abstract int getMinLearnedBatteryCapacity();
2546
2547 /**
2548 * @return The maximum learned battery capacity in uAh.
2549 */
2550 public abstract int getMaxLearnedBatteryCapacity() ;
2551
2552 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002553 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002554 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002555 public abstract LevelStepTracker getDischargeLevelStepTracker();
2556
2557 /**
2558 * Return the array of daily discharge step durations.
2559 */
2560 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002561
2562 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002563 * Compute an approximation for how much time (in microseconds) remains until the battery
2564 * is fully charged. Returns -1 if no time can be computed: either there is not
2565 * enough current data to make a decision, or the battery is currently
2566 * discharging.
2567 *
2568 * @param curTime The current elepsed realtime in microseconds.
2569 */
2570 public abstract long computeChargeTimeRemaining(long curTime);
2571
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002572 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002573 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002574 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002575 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002576
2577 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002578 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002579 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002580 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002581
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002582 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2583
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002584 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002585
Evan Millarc64edde2009-04-18 12:26:32 -07002586 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587
James Carr2dd7e5e2016-07-20 18:48:39 -07002588 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2589
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002590 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2591
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002592 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 long days = seconds / (60 * 60 * 24);
2594 if (days != 0) {
2595 out.append(days);
2596 out.append("d ");
2597 }
2598 long used = days * 60 * 60 * 24;
2599
2600 long hours = (seconds - used) / (60 * 60);
2601 if (hours != 0 || used != 0) {
2602 out.append(hours);
2603 out.append("h ");
2604 }
2605 used += hours * 60 * 60;
2606
2607 long mins = (seconds-used) / 60;
2608 if (mins != 0 || used != 0) {
2609 out.append(mins);
2610 out.append("m ");
2611 }
2612 used += mins * 60;
2613
2614 if (seconds != 0 || used != 0) {
2615 out.append(seconds-used);
2616 out.append("s ");
2617 }
2618 }
2619
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002620 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002622 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 sb.append(time - (sec * 1000));
2624 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 }
2626
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002627 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002628 long sec = time / 1000;
2629 formatTimeRaw(sb, sec);
2630 sb.append(time - (sec * 1000));
2631 sb.append("ms");
2632 }
2633
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002634 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002636 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 }
2638 float perc = ((float)num) / ((float)den) * 100;
2639 mFormatBuilder.setLength(0);
2640 mFormatter.format("%.1f%%", perc);
2641 return mFormatBuilder.toString();
2642 }
2643
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002644 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002645 mFormatBuilder.setLength(0);
2646
2647 if (bytes < BYTES_PER_KB) {
2648 return bytes + "B";
2649 } else if (bytes < BYTES_PER_MB) {
2650 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2651 return mFormatBuilder.toString();
2652 } else if (bytes < BYTES_PER_GB){
2653 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2654 return mFormatBuilder.toString();
2655 } else {
2656 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2657 return mFormatBuilder.toString();
2658 }
2659 }
2660
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002661 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002662 if (timer != null) {
2663 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002664 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002665 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2666 return totalTimeMillis;
2667 }
2668 return 0;
2669 }
2670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 /**
2672 *
2673 * @param sb a StringBuilder object.
2674 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002675 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002677 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 * @param linePrefix a String to be prepended to each line of output.
2679 * @return the line prefix
2680 */
2681 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002682 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002685 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002686
Evan Millarc64edde2009-04-18 12:26:32 -07002687 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 if (totalTimeMillis != 0) {
2689 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002690 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002691 if (name != null) {
2692 sb.append(name);
2693 sb.append(' ');
2694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 sb.append('(');
2696 sb.append(count);
2697 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002698 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2699 if (maxDurationMs >= 0) {
2700 sb.append(" max=");
2701 sb.append(maxDurationMs);
2702 }
Bookatz506a8182017-05-01 14:18:42 -07002703 // Put actual time if it is available and different from totalTimeMillis.
2704 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2705 if (totalDurMs > totalTimeMillis) {
2706 sb.append(" actual=");
2707 sb.append(totalDurMs);
2708 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002709 if (timer.isRunningLocked()) {
2710 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2711 if (currentMs >= 0) {
2712 sb.append(" (running for ");
2713 sb.append(currentMs);
2714 sb.append("ms)");
2715 } else {
2716 sb.append(" (running)");
2717 }
2718 }
2719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 return ", ";
2721 }
2722 }
2723 return linePrefix;
2724 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002725
2726 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002727 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002728 *
2729 * @param pw a PrintWriter object to print to.
2730 * @param sb a StringBuilder object.
2731 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002732 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002733 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2734 * @param prefix a String to be prepended to each line of output.
2735 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002736 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002737 */
2738 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002739 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002740 if (timer != null) {
2741 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002742 final long totalTimeMs = (timer.getTotalTimeLocked(
2743 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002744 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002745 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002746 sb.setLength(0);
2747 sb.append(prefix);
2748 sb.append(" ");
2749 sb.append(type);
2750 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002751 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002752 sb.append("realtime (");
2753 sb.append(count);
2754 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002755 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2756 if (maxDurationMs >= 0) {
2757 sb.append(" max=");
2758 sb.append(maxDurationMs);
2759 }
2760 if (timer.isRunningLocked()) {
2761 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2762 if (currentMs >= 0) {
2763 sb.append(" (running for ");
2764 sb.append(currentMs);
2765 sb.append("ms)");
2766 } else {
2767 sb.append(" (running)");
2768 }
2769 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002770 pw.println(sb.toString());
2771 return true;
2772 }
2773 }
2774 return false;
2775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776
2777 /**
2778 * Checkin version of wakelock printer. Prints simple comma-separated list.
2779 *
2780 * @param sb a StringBuilder object.
2781 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002782 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002784 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 * @param linePrefix a String to be prepended to each line of output.
2786 * @return the line prefix
2787 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002788 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2789 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 long totalTimeMicros = 0;
2791 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07002792 long max = 0;
2793 long current = 0;
2794 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002796 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07002797 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002798 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2799 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07002800 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 }
2802 sb.append(linePrefix);
2803 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2804 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002805 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002807 sb.append(',');
2808 sb.append(current);
2809 sb.append(',');
2810 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07002811 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
2812 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
2813 // totalDuration independent of totalTimeMicros (since they are not pooled).
2814 if (name != null) {
2815 sb.append(',');
2816 sb.append(totalDuration);
2817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 return ",";
2819 }
Bookatz506a8182017-05-01 14:18:42 -07002820
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002821 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2822 String type) {
2823 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2824 pw.print(',');
2825 pw.print(uid);
2826 pw.print(',');
2827 pw.print(category);
2828 pw.print(',');
2829 pw.print(type);
2830 }
2831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 /**
2833 * Dump a comma-separated line of values for terse checkin mode.
2834 *
2835 * @param pw the PageWriter to dump log to
2836 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2837 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2838 * @param args type-dependent data arguments
2839 */
2840 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
2841 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002842 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002843 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002844 pw.print(',');
2845 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002847 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002849
2850 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002851 * Dump a given timer stat for terse checkin mode.
2852 *
2853 * @param pw the PageWriter to dump log to
2854 * @param uid the UID to log
2855 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2856 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2857 * @param timer a {@link Timer} to dump stats for
2858 * @param rawRealtime the current elapsed realtime of the system in microseconds
2859 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2860 */
2861 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2862 Timer timer, long rawRealtime, int which) {
2863 if (timer != null) {
2864 // Convert from microseconds to milliseconds with rounding
2865 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2866 / 1000;
2867 final int count = timer.getCountLocked(which);
2868 if (totalTime != 0) {
2869 dumpLine(pw, uid, category, type, totalTime, count);
2870 }
2871 }
2872 }
2873
2874 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002875 * Checks if the ControllerActivityCounter has any data worth dumping.
2876 */
2877 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2878 if (counter == null) {
2879 return false;
2880 }
2881
2882 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2883 || counter.getRxTimeCounter().getCountLocked(which) != 0
2884 || counter.getPowerCounter().getCountLocked(which) != 0) {
2885 return true;
2886 }
2887
2888 for (LongCounter c : counter.getTxTimeCounters()) {
2889 if (c.getCountLocked(which) != 0) {
2890 return true;
2891 }
2892 }
2893 return false;
2894 }
2895
2896 /**
2897 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2898 * The order of the arguments in the final check in line is:
2899 *
2900 * idle, rx, power, tx...
2901 *
2902 * where tx... is one or more transmit level times.
2903 */
2904 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2905 String type,
2906 ControllerActivityCounter counter,
2907 int which) {
2908 if (!controllerActivityHasData(counter, which)) {
2909 return;
2910 }
2911
2912 dumpLineHeader(pw, uid, category, type);
2913 pw.print(",");
2914 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2915 pw.print(",");
2916 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2917 pw.print(",");
2918 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2919 for (LongCounter c : counter.getTxTimeCounters()) {
2920 pw.print(",");
2921 pw.print(c.getCountLocked(which));
2922 }
2923 pw.println();
2924 }
2925
2926 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2927 String prefix, String controllerName,
2928 ControllerActivityCounter counter,
2929 int which) {
2930 if (controllerActivityHasData(counter, which)) {
2931 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2932 }
2933 }
2934
2935 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2936 String controllerName,
2937 ControllerActivityCounter counter, int which) {
2938 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2939 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2940 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2941 long totalTxTimeMs = 0;
2942 for (LongCounter txState : counter.getTxTimeCounters()) {
2943 totalTxTimeMs += txState.getCountLocked(which);
2944 }
2945
2946 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2947
2948 sb.setLength(0);
2949 sb.append(prefix);
2950 sb.append(" ");
2951 sb.append(controllerName);
2952 sb.append(" Idle time: ");
2953 formatTimeMs(sb, idleTimeMs);
2954 sb.append("(");
2955 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2956 sb.append(")");
2957 pw.println(sb.toString());
2958
2959 sb.setLength(0);
2960 sb.append(prefix);
2961 sb.append(" ");
2962 sb.append(controllerName);
2963 sb.append(" Rx time: ");
2964 formatTimeMs(sb, rxTimeMs);
2965 sb.append("(");
2966 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
2967 sb.append(")");
2968 pw.println(sb.toString());
2969
2970 sb.setLength(0);
2971 sb.append(prefix);
2972 sb.append(" ");
2973 sb.append(controllerName);
2974 sb.append(" Tx time: ");
2975 formatTimeMs(sb, totalTxTimeMs);
2976 sb.append("(");
2977 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
2978 sb.append(")");
2979 pw.println(sb.toString());
2980
2981 final int numTxLvls = counter.getTxTimeCounters().length;
2982 if (numTxLvls > 1) {
2983 for (int lvl = 0; lvl < numTxLvls; lvl++) {
2984 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
2985 sb.setLength(0);
2986 sb.append(prefix);
2987 sb.append(" [");
2988 sb.append(lvl);
2989 sb.append("] ");
2990 formatTimeMs(sb, txLvlTimeMs);
2991 sb.append("(");
2992 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
2993 sb.append(")");
2994 pw.println(sb.toString());
2995 }
2996 }
2997
2998 sb.setLength(0);
2999 sb.append(prefix);
3000 sb.append(" ");
3001 sb.append(controllerName);
3002 sb.append(" Power drain: ").append(
3003 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
3004 sb.append("mAh");
3005 pw.println(sb.toString());
3006 }
3007
3008 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003009 * Temporary for settings.
3010 */
3011 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3012 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3013 }
3014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 /**
3016 * Checkin server version of dump to produce more compact, computer-readable log.
3017 *
3018 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003020 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3021 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3023 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3024 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3026 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003027 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3028 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3029 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 final long totalRealtime = computeRealtime(rawRealtime, which);
3031 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003032 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003033 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003034 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003035 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3036 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003037 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003038 rawRealtime, which);
3039 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3040 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003041 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003042 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003043 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003044 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003045 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
3046 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
3047 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003048
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003049 final StringBuilder sb = new StringBuilder(128);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003051 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003052 final int NU = uidStats.size();
3053
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003054 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003057 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003058 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003059 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003060 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003061 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003062 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003063 getEstimatedBatteryCapacity(),
3064 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07003065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003067 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003068 long fullWakeLockTimeTotal = 0;
3069 long partialWakeLockTimeTotal = 0;
3070
3071 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003072 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003073
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003074 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3075 = u.getWakelockStats();
3076 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3077 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003078
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003079 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3080 if (fullWakeTimer != null) {
3081 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3082 which);
3083 }
3084
3085 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3086 if (partialWakeTimer != null) {
3087 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3088 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003089 }
3090 }
3091 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003092
3093 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003094 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3095 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3096 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3097 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3098 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3099 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3100 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3101 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003102 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3103 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003104 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3105 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003106 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3107 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003108
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003109 // Dump Modem controller stats
3110 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3111 getModemControllerActivity(), which);
3112
Adam Lesinskie283d332015-04-16 12:29:25 -07003113 // Dump Wifi controller stats
3114 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3115 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003116 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003117 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003118
3119 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3120 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003121
3122 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003123 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3124 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 // Dump misc stats
3127 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003128 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003129 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003130 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003131 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003132 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003133 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3134 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003135 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003136 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3137 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3138 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3139 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003140 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003141
3142 // Dump screen brightness stats
3143 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3144 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003145 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003146 }
3147 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07003148
Dianne Hackborn627bba72009-03-24 22:32:56 -07003149 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003150 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3151 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003152 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003153 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003154 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003155 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003156 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003157 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003158 args[i] = getPhoneSignalStrengthCount(i, which);
3159 }
3160 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003161
Dianne Hackborn627bba72009-03-24 22:32:56 -07003162 // Dump network type stats
3163 args = new Object[NUM_DATA_CONNECTION_TYPES];
3164 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003165 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003166 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003167 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3168 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3169 args[i] = getPhoneDataConnectionCount(i, which);
3170 }
3171 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003172
3173 // Dump wifi state stats
3174 args = new Object[NUM_WIFI_STATES];
3175 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003176 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003177 }
3178 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3179 for (int i=0; i<NUM_WIFI_STATES; i++) {
3180 args[i] = getWifiStateCount(i, which);
3181 }
3182 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3183
Dianne Hackborn3251b902014-06-20 14:40:53 -07003184 // Dump wifi suppl state stats
3185 args = new Object[NUM_WIFI_SUPPL_STATES];
3186 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3187 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3188 }
3189 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3190 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3191 args[i] = getWifiSupplStateCount(i, which);
3192 }
3193 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3194
3195 // Dump wifi signal strength stats
3196 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3197 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3198 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3199 }
3200 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3201 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3202 args[i] = getWifiSignalStrengthCount(i, which);
3203 }
3204 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3205
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003206 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003207 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003208 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003211 if (which == STATS_SINCE_UNPLUGGED) {
3212 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3213 getDischargeStartLevel()-getDischargeCurrentLevel(),
3214 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003215 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3216 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003217 } else {
3218 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3219 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003220 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003221 getDischargeAmountScreenOffSinceCharge(),
3222 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003223 }
3224
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003225 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003226 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003227 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003228 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003229 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003230 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003231 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3232 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003233 }
Evan Millarc64edde2009-04-18 12:26:32 -07003234 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003235 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003236 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003237 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3238 // Not doing the regular wake lock formatting to remain compatible
3239 // with the old checkin format.
3240 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3241 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003242 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003243 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003244 }
3245 }
Evan Millarc64edde2009-04-18 12:26:32 -07003246 }
3247
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003248 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003249 helper.create(this);
3250 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003251 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003252 if (sippers != null && sippers.size() > 0) {
3253 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3254 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003255 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003256 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3257 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3258 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003259 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003260 int uid = 0;
3261 String label;
3262 switch (bs.drainType) {
3263 case IDLE:
3264 label="idle";
3265 break;
3266 case CELL:
3267 label="cell";
3268 break;
3269 case PHONE:
3270 label="phone";
3271 break;
3272 case WIFI:
3273 label="wifi";
3274 break;
3275 case BLUETOOTH:
3276 label="blue";
3277 break;
3278 case SCREEN:
3279 label="scrn";
3280 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003281 case FLASHLIGHT:
3282 label="flashlight";
3283 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003284 case APP:
3285 uid = bs.uidObj.getUid();
3286 label = "uid";
3287 break;
3288 case USER:
3289 uid = UserHandle.getUid(bs.userId, 0);
3290 label = "user";
3291 break;
3292 case UNACCOUNTED:
3293 label = "unacc";
3294 break;
3295 case OVERCOUNTED:
3296 label = "over";
3297 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003298 case CAMERA:
3299 label = "camera";
3300 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003301 default:
3302 label = "???";
3303 }
3304 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Adam Lesinskie08af192015-03-25 16:42:59 -07003305 BatteryStatsHelper.makemAh(bs.totalPowerMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003306 }
3307 }
3308
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003309 final long[] cpuFreqs = getCpuFreqs();
3310 if (cpuFreqs != null) {
3311 sb.setLength(0);
3312 for (int i = 0; i < cpuFreqs.length; ++i) {
3313 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3314 }
3315 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3316 }
3317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 for (int iu = 0; iu < NU; iu++) {
3319 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003320 if (reqUid >= 0 && uid != reqUid) {
3321 continue;
3322 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003323 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003326 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3327 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3328 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3329 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3330 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3331 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3332 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3333 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003334 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003335 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3336 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003337 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003338 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3339 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003340 // Background data transfers
3341 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3342 which);
3343 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3344 which);
3345 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3346 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3347 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3348 which);
3349 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3350 which);
3351 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3352 which);
3353 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3354 which);
3355
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003356 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3357 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003358 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003359 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3360 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3361 || wifiBytesBgTx > 0
3362 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3363 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003364 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3365 wifiBytesRx, wifiBytesTx,
3366 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003367 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003368 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003369 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3370 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3371 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3372 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003373 }
3374
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003375 // Dump modem controller data, per UID.
3376 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3377 u.getModemControllerActivity(), which);
3378
3379 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003380 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3381 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3382 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003383 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3384 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003385 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3386 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3387 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003388 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003389 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003390 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3391 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003392 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3393 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003394 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003395 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003398 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3399 u.getWifiControllerActivity(), which);
3400
Bookatz867c0d72017-03-07 18:23:42 -08003401 final Timer bleTimer = u.getBluetoothScanTimer();
3402 if (bleTimer != null) {
3403 // Convert from microseconds to milliseconds with rounding
3404 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3405 / 1000;
3406 if (totalTime != 0) {
3407 final int count = bleTimer.getCountLocked(which);
3408 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3409 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
3410 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3411 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3412 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3413 final long actualTimeBg = bleTimerBg != null ?
3414 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatz956f36bf2017-04-28 09:48:17 -07003415 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3416 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003417 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatz956f36bf2017-04-28 09:48:17 -07003418 countBg, actualTime, actualTimeBg, resultCount);
Bookatz867c0d72017-03-07 18:23:42 -08003419 }
3420 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003421
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003422 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3423 u.getBluetoothControllerActivity(), which);
3424
Dianne Hackborn617f8772009-03-31 15:04:46 -07003425 if (u.hasUserActivity()) {
3426 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3427 boolean hasData = false;
3428 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3429 int val = u.getUserActivityCount(i, which);
3430 args[i] = val;
3431 if (val != 0) hasData = true;
3432 }
3433 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003434 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003435 }
3436 }
3437
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003438 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3439 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3440 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3441 String linePrefix = "";
3442 sb.setLength(0);
3443 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3444 rawRealtime, "f", which, linePrefix);
3445 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
3446 rawRealtime, "p", which, linePrefix);
3447 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3448 rawRealtime, "w", which, linePrefix);
3449
3450 // Only log if we had at lease one wakelock...
3451 if (sb.length() > 0) {
3452 String name = wakelocks.keyAt(iw);
3453 if (name.indexOf(',') >= 0) {
3454 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003456 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 }
3458 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003459
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003460 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3461 for (int isy=syncs.size()-1; isy>=0; isy--) {
3462 final Timer timer = syncs.valueAt(isy);
3463 // Convert from microseconds to milliseconds with rounding
3464 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3465 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003466 final Timer bgTimer = timer.getSubTimer();
3467 final long bgTime = bgTimer != null ?
3468 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
3469 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003470 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003471 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003472 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003473 }
3474 }
3475
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003476 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3477 for (int ij=jobs.size()-1; ij>=0; ij--) {
3478 final Timer timer = jobs.valueAt(ij);
3479 // Convert from microseconds to milliseconds with rounding
3480 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3481 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003482 final Timer bgTimer = timer.getSubTimer();
3483 final long bgTime = bgTimer != null ?
3484 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
3485 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003486 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003487 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003488 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003489 }
3490 }
3491
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003492 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3493 rawRealtime, which);
3494 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3495 rawRealtime, which);
3496 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3497 rawRealtime, which);
3498 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3499 rawRealtime, which);
3500
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003501 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3502 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003503 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003504 final Uid.Sensor se = sensors.valueAt(ise);
3505 final int sensorNumber = sensors.keyAt(ise);
3506 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003507 if (timer != null) {
3508 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003509 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3510 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003511 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003512 final int count = timer.getCountLocked(which);
3513 final Timer bgTimer = se.getSensorBackgroundTime();
3514 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
3515 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
3516 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3517 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3518 final long bgActualTime = bgTimer != null ?
3519 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3520 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3521 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 }
3524 }
3525
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003526 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3527 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003528
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003529 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3530 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003531
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003532 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003533 long totalStateTime = 0;
3534 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003535 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3536 totalStateTime += time;
3537 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003538 }
3539 if (totalStateTime > 0) {
3540 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3541 }
3542
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003543 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3544 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003545 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003546 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003547 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003548 }
3549
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003550 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
3551 // If total cpuFreqTimes is null, then we don't need to check for screenOffCpuFreqTimes.
3552 if (cpuFreqTimeMs != null) {
3553 sb.setLength(0);
3554 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3555 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
3556 }
3557 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
3558 if (screenOffCpuFreqTimeMs != null) {
3559 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
3560 sb.append("," + screenOffCpuFreqTimeMs[i]);
3561 }
3562 } else {
3563 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3564 sb.append(",0");
3565 }
3566 }
3567 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
3568 cpuFreqTimeMs.length, sb.toString());
3569 }
3570
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003571 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3572 = u.getProcessStats();
3573 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3574 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003575
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003576 final long userMillis = ps.getUserTime(which);
3577 final long systemMillis = ps.getSystemTime(which);
3578 final long foregroundMillis = ps.getForegroundTime(which);
3579 final int starts = ps.getStarts(which);
3580 final int numCrashes = ps.getNumCrashes(which);
3581 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003582
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003583 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3584 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003585 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3586 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 }
3588 }
3589
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003590 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3591 = u.getPackageStats();
3592 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3593 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3594 int wakeups = 0;
3595 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3596 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003597 int count = alarms.valueAt(iwa).getCountLocked(which);
3598 wakeups += count;
3599 String name = alarms.keyAt(iwa).replace(',', '_');
3600 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003601 }
3602 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3603 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3604 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3605 final long startTime = ss.getStartTime(batteryUptime, which);
3606 final int starts = ss.getStarts(which);
3607 final int launches = ss.getLaunches(which);
3608 if (startTime != 0 || starts != 0 || launches != 0) {
3609 dumpLine(pw, uid, category, APK_DATA,
3610 wakeups, // wakeup alarms
3611 packageStats.keyAt(ipkg), // Apk
3612 serviceStats.keyAt(isvc), // service
3613 startTime / 1000, // time spent started, in ms
3614 starts,
3615 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 }
3617 }
3618 }
3619 }
3620 }
3621
Dianne Hackborn81038902012-11-26 17:04:09 -08003622 static final class TimerEntry {
3623 final String mName;
3624 final int mId;
3625 final BatteryStats.Timer mTimer;
3626 final long mTime;
3627 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3628 mName = name;
3629 mId = id;
3630 mTimer = timer;
3631 mTime = time;
3632 }
3633 }
3634
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003635 private void printmAh(PrintWriter printer, double power) {
3636 printer.print(BatteryStatsHelper.makemAh(power));
3637 }
3638
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003639 private void printmAh(StringBuilder sb, double power) {
3640 sb.append(BatteryStatsHelper.makemAh(power));
3641 }
3642
Dianne Hackbornd953c532014-08-16 18:17:38 -07003643 /**
3644 * Temporary for settings.
3645 */
3646 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3647 int reqUid) {
3648 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3649 }
3650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003652 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003653 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3655 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
3656 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657
3658 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3659 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3660 final long totalRealtime = computeRealtime(rawRealtime, which);
3661 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003662 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3663 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3664 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003665 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3666 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003667
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003668 final StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07003669
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003670 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003671 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003673 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3674 if (estimatedBatteryCapacity > 0) {
3675 sb.setLength(0);
3676 sb.append(prefix);
3677 sb.append(" Estimated battery capacity: ");
3678 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3679 sb.append(" mAh");
3680 pw.println(sb.toString());
3681 }
3682
Jocelyn Dangc627d102017-04-14 13:15:14 -07003683 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
3684 if (minLearnedBatteryCapacity > 0) {
3685 sb.setLength(0);
3686 sb.append(prefix);
3687 sb.append(" Min learned battery capacity: ");
3688 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
3689 sb.append(" mAh");
3690 pw.println(sb.toString());
3691 }
3692 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
3693 if (maxLearnedBatteryCapacity > 0) {
3694 sb.setLength(0);
3695 sb.append(prefix);
3696 sb.append(" Max learned battery capacity: ");
3697 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
3698 sb.append(" mAh");
3699 pw.println(sb.toString());
3700 }
3701
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003702 sb.setLength(0);
3703 sb.append(prefix);
3704 sb.append(" Time on battery: ");
3705 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3706 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3707 sb.append(") realtime, ");
3708 formatTimeMs(sb, whichBatteryUptime / 1000);
3709 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3710 sb.append(") uptime");
3711 pw.println(sb.toString());
3712 sb.setLength(0);
3713 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003714 sb.append(" Time on battery screen off: ");
3715 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3716 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3717 sb.append(") realtime, ");
3718 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3719 sb.append("(");
3720 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3721 sb.append(") uptime");
3722 pw.println(sb.toString());
3723 sb.setLength(0);
3724 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003725 sb.append(" Total run time: ");
3726 formatTimeMs(sb, totalRealtime / 1000);
3727 sb.append("realtime, ");
3728 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003729 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003730 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003731 if (batteryTimeRemaining >= 0) {
3732 sb.setLength(0);
3733 sb.append(prefix);
3734 sb.append(" Battery time remaining: ");
3735 formatTimeMs(sb, batteryTimeRemaining / 1000);
3736 pw.println(sb.toString());
3737 }
3738 if (chargeTimeRemaining >= 0) {
3739 sb.setLength(0);
3740 sb.append(prefix);
3741 sb.append(" Charge time remaining: ");
3742 formatTimeMs(sb, chargeTimeRemaining / 1000);
3743 pw.println(sb.toString());
3744 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003745
3746 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3747 final long dischargeCount = dischargeCounter.getCountLocked(which);
3748 if (dischargeCount >= 0) {
3749 sb.setLength(0);
3750 sb.append(prefix);
3751 sb.append(" Discharge: ");
3752 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3753 sb.append(" mAh");
3754 pw.println(sb.toString());
3755 }
3756
3757 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3758 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3759 if (dischargeScreenOffCount >= 0) {
3760 sb.setLength(0);
3761 sb.append(prefix);
3762 sb.append(" Screen off discharge: ");
3763 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3764 sb.append(" mAh");
3765 pw.println(sb.toString());
3766 }
3767
3768 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3769 if (dischargeScreenOnCount >= 0) {
3770 sb.setLength(0);
3771 sb.append(prefix);
3772 sb.append(" Screen on discharge: ");
3773 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3774 sb.append(" mAh");
3775 pw.println(sb.toString());
3776 }
3777
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003778 pw.print(" Start clock time: ");
3779 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3780
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003781 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003782 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003783 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003784 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3785 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003786 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003787 rawRealtime, which);
3788 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3789 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003790 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003791 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003792 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3793 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3794 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003795 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003796 sb.append(prefix);
3797 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3798 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003799 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003800 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3801 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003802 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003803 pw.println(sb.toString());
3804 sb.setLength(0);
3805 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003806 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003807 boolean didOne = false;
3808 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003809 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003810 if (time == 0) {
3811 continue;
3812 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003813 sb.append("\n ");
3814 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003815 didOne = true;
3816 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3817 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003818 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003819 sb.append("(");
3820 sb.append(formatRatioLocked(time, screenOnTime));
3821 sb.append(")");
3822 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003823 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003824 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003825 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003826 sb.setLength(0);
3827 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003828 sb.append(" Power save mode enabled: ");
3829 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003830 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003831 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003832 sb.append(")");
3833 pw.println(sb.toString());
3834 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003835 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003836 sb.setLength(0);
3837 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003838 sb.append(" Device light idling: ");
3839 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003840 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003841 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3842 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003843 sb.append("x");
3844 pw.println(sb.toString());
3845 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003846 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003847 sb.setLength(0);
3848 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003849 sb.append(" Idle mode light time: ");
3850 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003851 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003852 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3853 sb.append(") ");
3854 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003855 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003856 sb.append(" -- longest ");
3857 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3858 pw.println(sb.toString());
3859 }
3860 if (deviceIdlingTime != 0) {
3861 sb.setLength(0);
3862 sb.append(prefix);
3863 sb.append(" Device full idling: ");
3864 formatTimeMs(sb, deviceIdlingTime / 1000);
3865 sb.append("(");
3866 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003867 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003868 sb.append("x");
3869 pw.println(sb.toString());
3870 }
3871 if (deviceIdleModeFullTime != 0) {
3872 sb.setLength(0);
3873 sb.append(prefix);
3874 sb.append(" Idle mode full time: ");
3875 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3876 sb.append("(");
3877 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3878 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003879 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003880 sb.append("x");
3881 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003882 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003883 pw.println(sb.toString());
3884 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003885 if (phoneOnTime != 0) {
3886 sb.setLength(0);
3887 sb.append(prefix);
3888 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3889 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003890 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003891 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003892 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003893 if (connChanges != 0) {
3894 pw.print(prefix);
3895 pw.print(" Connectivity changes: "); pw.println(connChanges);
3896 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003897
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003898 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003899 long fullWakeLockTimeTotalMicros = 0;
3900 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003901
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003902 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003903
Evan Millar22ac0432009-03-31 11:33:18 -07003904 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003905 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003906
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003907 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3908 = u.getWakelockStats();
3909 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3910 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003911
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003912 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3913 if (fullWakeTimer != null) {
3914 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3915 rawRealtime, which);
3916 }
3917
3918 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3919 if (partialWakeTimer != null) {
3920 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3921 rawRealtime, which);
3922 if (totalTimeMicros > 0) {
3923 if (reqUid < 0) {
3924 // Only show the ordered list of all wake
3925 // locks if the caller is not asking for data
3926 // about a specific uid.
3927 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3928 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003929 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003930 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003931 }
3932 }
3933 }
3934 }
3935
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003936 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3937 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3938 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3939 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3940 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3941 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3942 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3943 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08003944 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3945 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003946
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003947 if (fullWakeLockTimeTotalMicros != 0) {
3948 sb.setLength(0);
3949 sb.append(prefix);
3950 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
3951 (fullWakeLockTimeTotalMicros + 500) / 1000);
3952 pw.println(sb.toString());
3953 }
3954
3955 if (partialWakeLockTimeTotalMicros != 0) {
3956 sb.setLength(0);
3957 sb.append(prefix);
3958 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
3959 (partialWakeLockTimeTotalMicros + 500) / 1000);
3960 pw.println(sb.toString());
3961 }
3962
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003963 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003964 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
3965 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
3966 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
3967 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003968 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003969 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07003970 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003971 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08003972 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003973 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003974 if (time == 0) {
3975 continue;
3976 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003977 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003978 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003979 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08003980 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003981 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003982 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003983 sb.append("(");
3984 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07003985 sb.append(") ");
3986 sb.append(getPhoneSignalStrengthCount(i, which));
3987 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003988 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003989 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07003990 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07003991
3992 sb.setLength(0);
3993 sb.append(prefix);
3994 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003995 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003996 pw.println(sb.toString());
3997
Dianne Hackborn627bba72009-03-24 22:32:56 -07003998 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003999 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004000 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004001 didOne = false;
4002 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004003 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004004 if (time == 0) {
4005 continue;
4006 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004007 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004008 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004009 didOne = true;
4010 sb.append(DATA_CONNECTION_NAMES[i]);
4011 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004012 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004013 sb.append("(");
4014 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004015 sb.append(") ");
4016 sb.append(getPhoneDataConnectionCount(i, which));
4017 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004018 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004019 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004020 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004021
4022 sb.setLength(0);
4023 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004024 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004025 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004026 formatTimeMs(sb, mobileActiveTime / 1000);
4027 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4028 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
4029 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004030 pw.println(sb.toString());
4031
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004032 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
4033 if (mobileActiveUnknownTime != 0) {
4034 sb.setLength(0);
4035 sb.append(prefix);
4036 sb.append(" Mobile radio active unknown time: ");
4037 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
4038 sb.append("(");
4039 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
4040 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
4041 sb.append("x");
4042 pw.println(sb.toString());
4043 }
4044
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004045 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
4046 if (mobileActiveAdjustedTime != 0) {
4047 sb.setLength(0);
4048 sb.append(prefix);
4049 sb.append(" Mobile radio active adjusted time: ");
4050 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
4051 sb.append("(");
4052 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
4053 sb.append(")");
4054 pw.println(sb.toString());
4055 }
4056
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004057 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
4058
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004059 pw.print(prefix);
4060 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
4061 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
4062 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
4063 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004064 sb.setLength(0);
4065 sb.append(prefix);
4066 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
4067 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
4068 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
4069 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004070 sb.append(")");
4071 pw.println(sb.toString());
4072
4073 sb.setLength(0);
4074 sb.append(prefix);
4075 sb.append(" Wifi states:");
4076 didOne = false;
4077 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004078 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004079 if (time == 0) {
4080 continue;
4081 }
4082 sb.append("\n ");
4083 didOne = true;
4084 sb.append(WIFI_STATE_NAMES[i]);
4085 sb.append(" ");
4086 formatTimeMs(sb, time/1000);
4087 sb.append("(");
4088 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4089 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004090 sb.append(getWifiStateCount(i, which));
4091 sb.append("x");
4092 }
4093 if (!didOne) sb.append(" (no activity)");
4094 pw.println(sb.toString());
4095
4096 sb.setLength(0);
4097 sb.append(prefix);
4098 sb.append(" Wifi supplicant states:");
4099 didOne = false;
4100 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4101 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4102 if (time == 0) {
4103 continue;
4104 }
4105 sb.append("\n ");
4106 didOne = true;
4107 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4108 sb.append(" ");
4109 formatTimeMs(sb, time/1000);
4110 sb.append("(");
4111 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4112 sb.append(") ");
4113 sb.append(getWifiSupplStateCount(i, which));
4114 sb.append("x");
4115 }
4116 if (!didOne) sb.append(" (no activity)");
4117 pw.println(sb.toString());
4118
4119 sb.setLength(0);
4120 sb.append(prefix);
4121 sb.append(" Wifi signal levels:");
4122 didOne = false;
4123 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4124 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4125 if (time == 0) {
4126 continue;
4127 }
4128 sb.append("\n ");
4129 sb.append(prefix);
4130 didOne = true;
4131 sb.append("level(");
4132 sb.append(i);
4133 sb.append(") ");
4134 formatTimeMs(sb, time/1000);
4135 sb.append("(");
4136 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4137 sb.append(") ");
4138 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004139 sb.append("x");
4140 }
4141 if (!didOne) sb.append(" (no activity)");
4142 pw.println(sb.toString());
4143
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004144 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004145
Adam Lesinski50e47602015-12-04 17:04:54 -08004146 pw.print(prefix);
4147 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4148 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4149
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004150 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4151 sb.setLength(0);
4152 sb.append(prefix);
4153 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4154 pw.println(sb.toString());
4155
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004156 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4157 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004158
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004159 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004160
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004161 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004162 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004163 pw.print(prefix); pw.println(" Device is currently unplugged");
4164 pw.print(prefix); pw.print(" Discharge cycle start level: ");
4165 pw.println(getDischargeStartLevel());
4166 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4167 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004168 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004169 pw.print(prefix); pw.println(" Device is currently plugged into power");
4170 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
4171 pw.println(getDischargeStartLevel());
4172 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
4173 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004174 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004175 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4176 pw.println(getDischargeAmountScreenOn());
4177 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4178 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004179 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004180 } else {
4181 pw.print(prefix); pw.println(" Device battery use since last full charge");
4182 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4183 pw.println(getLowDischargeAmountSinceCharge());
4184 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4185 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004186 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4187 pw.println(getDischargeAmountScreenOnSinceCharge());
4188 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4189 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004190 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004191 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004192
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004193 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004194 helper.create(this);
4195 helper.refreshStats(which, UserHandle.USER_ALL);
4196 List<BatterySipper> sippers = helper.getUsageList();
4197 if (sippers != null && sippers.size() > 0) {
4198 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4199 pw.print(prefix); pw.print(" Capacity: ");
4200 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004201 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004202 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4203 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4204 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4205 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004206 pw.println();
4207 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004208 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004209 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004210 switch (bs.drainType) {
4211 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004212 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004213 break;
4214 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004215 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004216 break;
4217 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004218 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004219 break;
4220 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004221 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004222 break;
4223 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004224 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004225 break;
4226 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004227 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004228 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004229 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004230 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004231 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004232 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004233 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004234 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004235 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004236 break;
4237 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004238 pw.print(" User "); pw.print(bs.userId);
4239 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004240 break;
4241 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004242 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004243 break;
4244 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004245 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004246 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004247 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004248 pw.print(" Camera: ");
4249 break;
4250 default:
4251 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004252 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004253 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004254 printmAh(pw, bs.totalPowerMah);
4255
Adam Lesinski57123002015-06-12 16:12:07 -07004256 if (bs.usagePowerMah != bs.totalPowerMah) {
4257 // If the usage (generic power) isn't the whole amount, we list out
4258 // what components are involved in the calculation.
4259
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004260 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004261 if (bs.usagePowerMah != 0) {
4262 pw.print(" usage=");
4263 printmAh(pw, bs.usagePowerMah);
4264 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004265 if (bs.cpuPowerMah != 0) {
4266 pw.print(" cpu=");
4267 printmAh(pw, bs.cpuPowerMah);
4268 }
4269 if (bs.wakeLockPowerMah != 0) {
4270 pw.print(" wake=");
4271 printmAh(pw, bs.wakeLockPowerMah);
4272 }
4273 if (bs.mobileRadioPowerMah != 0) {
4274 pw.print(" radio=");
4275 printmAh(pw, bs.mobileRadioPowerMah);
4276 }
4277 if (bs.wifiPowerMah != 0) {
4278 pw.print(" wifi=");
4279 printmAh(pw, bs.wifiPowerMah);
4280 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004281 if (bs.bluetoothPowerMah != 0) {
4282 pw.print(" bt=");
4283 printmAh(pw, bs.bluetoothPowerMah);
4284 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004285 if (bs.gpsPowerMah != 0) {
4286 pw.print(" gps=");
4287 printmAh(pw, bs.gpsPowerMah);
4288 }
4289 if (bs.sensorPowerMah != 0) {
4290 pw.print(" sensor=");
4291 printmAh(pw, bs.sensorPowerMah);
4292 }
4293 if (bs.cameraPowerMah != 0) {
4294 pw.print(" camera=");
4295 printmAh(pw, bs.cameraPowerMah);
4296 }
4297 if (bs.flashlightPowerMah != 0) {
4298 pw.print(" flash=");
4299 printmAh(pw, bs.flashlightPowerMah);
4300 }
4301 pw.print(" )");
4302 }
4303 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004304 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004305 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004306 }
4307
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004308 sippers = helper.getMobilemsppList();
4309 if (sippers != null && sippers.size() > 0) {
4310 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004311 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004312 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004313 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004314 sb.setLength(0);
4315 sb.append(prefix); sb.append(" Uid ");
4316 UserHandle.formatUid(sb, bs.uidObj.getUid());
4317 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4318 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4319 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004320 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004321 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004322 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004323 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004324 sb.setLength(0);
4325 sb.append(prefix);
4326 sb.append(" TOTAL TIME: ");
4327 formatTimeMs(sb, totalTime);
4328 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4329 sb.append(")");
4330 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004331 pw.println();
4332 }
4333
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004334 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4335 @Override
4336 public int compare(TimerEntry lhs, TimerEntry rhs) {
4337 long lhsTime = lhs.mTime;
4338 long rhsTime = rhs.mTime;
4339 if (lhsTime < rhsTime) {
4340 return 1;
4341 }
4342 if (lhsTime > rhsTime) {
4343 return -1;
4344 }
4345 return 0;
4346 }
4347 };
4348
4349 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004350 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4351 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004352 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004353 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4354 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4355 : kernelWakelocks.entrySet()) {
4356 final BatteryStats.Timer timer = ent.getValue();
4357 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004358 if (totalTimeMillis > 0) {
4359 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4360 }
4361 }
4362 if (ktimers.size() > 0) {
4363 Collections.sort(ktimers, timerComparator);
4364 pw.print(prefix); pw.println(" All kernel wake locks:");
4365 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004366 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004367 String linePrefix = ": ";
4368 sb.setLength(0);
4369 sb.append(prefix);
4370 sb.append(" Kernel Wake lock ");
4371 sb.append(timer.mName);
4372 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4373 which, linePrefix);
4374 if (!linePrefix.equals(": ")) {
4375 sb.append(" realtime");
4376 // Only print out wake locks that were held
4377 pw.println(sb.toString());
4378 }
4379 }
4380 pw.println();
4381 }
4382 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004383
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004384 if (timers.size() > 0) {
4385 Collections.sort(timers, timerComparator);
4386 pw.print(prefix); pw.println(" All partial wake locks:");
4387 for (int i=0; i<timers.size(); i++) {
4388 TimerEntry timer = timers.get(i);
4389 sb.setLength(0);
4390 sb.append(" Wake lock ");
4391 UserHandle.formatUid(sb, timer.mId);
4392 sb.append(" ");
4393 sb.append(timer.mName);
4394 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4395 sb.append(" realtime");
4396 pw.println(sb.toString());
4397 }
4398 timers.clear();
4399 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004400 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004401
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004402 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004403 if (wakeupReasons.size() > 0) {
4404 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004405 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004406 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004407 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004408 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4409 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004410 }
4411 Collections.sort(reasons, timerComparator);
4412 for (int i=0; i<reasons.size(); i++) {
4413 TimerEntry timer = reasons.get(i);
4414 String linePrefix = ": ";
4415 sb.setLength(0);
4416 sb.append(prefix);
4417 sb.append(" Wakeup reason ");
4418 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004419 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4420 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004421 pw.println(sb.toString());
4422 }
4423 pw.println();
4424 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004425 }
Evan Millar22ac0432009-03-31 11:33:18 -07004426
James Carr2dd7e5e2016-07-20 18:48:39 -07004427 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4428 pw.println("Memory Stats");
4429 for (int i = 0; i < mMemoryStats.size(); i++) {
4430 sb.setLength(0);
4431 sb.append("Bandwidth ");
4432 sb.append(mMemoryStats.keyAt(i));
4433 sb.append(" Time ");
4434 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4435 pw.println(sb.toString());
4436 }
4437
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004438 final long[] cpuFreqs = getCpuFreqs();
4439 if (cpuFreqs != null) {
4440 sb.setLength(0);
4441 sb.append("CPU freqs:");
4442 for (int i = 0; i < cpuFreqs.length; ++i) {
4443 sb.append(" " + cpuFreqs[i]);
4444 }
4445 pw.println(sb.toString());
4446 }
4447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 for (int iu=0; iu<NU; iu++) {
4449 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004450 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004451 continue;
4452 }
4453
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004454 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004455
4456 pw.print(prefix);
4457 pw.print(" ");
4458 UserHandle.formatUid(pw, uid);
4459 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004461
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004462 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4463 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4464 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4465 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004466 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4467 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4468
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004469 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4470 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004471 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4472 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004473
4474 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4475 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4476
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004477 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4478 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4479 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004480 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4481 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4482 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4483 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004484 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004485
Adam Lesinski5f056f62016-07-14 16:56:08 -07004486 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4487 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4488
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004489 if (mobileRxBytes > 0 || mobileTxBytes > 0
4490 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004491 pw.print(prefix); pw.print(" Mobile network: ");
4492 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004493 pw.print(formatBytesLocked(mobileTxBytes));
4494 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4495 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004496 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004497 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4498 sb.setLength(0);
4499 sb.append(prefix); sb.append(" Mobile radio active: ");
4500 formatTimeMs(sb, uidMobileActiveTime / 1000);
4501 sb.append("(");
4502 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4503 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4504 long packets = mobileRxPackets + mobileTxPackets;
4505 if (packets == 0) {
4506 packets = 1;
4507 }
4508 sb.append(" @ ");
4509 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4510 sb.append(" mspp");
4511 pw.println(sb.toString());
4512 }
4513
Adam Lesinski5f056f62016-07-14 16:56:08 -07004514 if (mobileWakeup > 0) {
4515 sb.setLength(0);
4516 sb.append(prefix);
4517 sb.append(" Mobile radio AP wakeups: ");
4518 sb.append(mobileWakeup);
4519 pw.println(sb.toString());
4520 }
4521
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004522 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4523 u.getModemControllerActivity(), which);
4524
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004525 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004526 pw.print(prefix); pw.print(" Wi-Fi network: ");
4527 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004528 pw.print(formatBytesLocked(wifiTxBytes));
4529 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4530 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004531 }
4532
Dianne Hackborn62793e42015-03-09 11:15:41 -07004533 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004534 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004535 || uidWifiRunningTime != 0) {
4536 sb.setLength(0);
4537 sb.append(prefix); sb.append(" Wifi Running: ");
4538 formatTimeMs(sb, uidWifiRunningTime / 1000);
4539 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4540 whichBatteryRealtime)); sb.append(")\n");
4541 sb.append(prefix); sb.append(" Full Wifi Lock: ");
4542 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4543 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4544 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004545 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004546 formatTimeMs(sb, wifiScanTime / 1000);
4547 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004548 whichBatteryRealtime)); sb.append(") ");
4549 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004550 sb.append("x\n");
4551 // actual and background times are unpooled and since reset (regardless of 'which')
4552 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4553 formatTimeMs(sb, wifiScanActualTime / 1000);
4554 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4555 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4556 sb.append(") ");
4557 sb.append(wifiScanCount);
4558 sb.append("x\n");
4559 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4560 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4561 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4562 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4563 sb.append(") ");
4564 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004565 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004566 pw.println(sb.toString());
4567 }
4568
Adam Lesinski5f056f62016-07-14 16:56:08 -07004569 if (wifiWakeup > 0) {
4570 sb.setLength(0);
4571 sb.append(prefix);
4572 sb.append(" WiFi AP wakeups: ");
4573 sb.append(wifiWakeup);
4574 pw.println(sb.toString());
4575 }
4576
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004577 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4578 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004579
Adam Lesinski50e47602015-12-04 17:04:54 -08004580 if (btRxBytes > 0 || btTxBytes > 0) {
4581 pw.print(prefix); pw.print(" Bluetooth network: ");
4582 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4583 pw.print(formatBytesLocked(btTxBytes));
4584 pw.println(" sent");
4585 }
4586
Bookatz867c0d72017-03-07 18:23:42 -08004587 final Timer bleTimer = u.getBluetoothScanTimer();
4588 if (bleTimer != null) {
4589 // Convert from microseconds to milliseconds with rounding
4590 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4591 / 1000;
4592 if (totalTimeMs != 0) {
4593 final int count = bleTimer.getCountLocked(which);
4594 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4595 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
4596 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4597 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4598 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4599 final long actualTimeMsBg = bleTimerBg != null ?
4600 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatz956f36bf2017-04-28 09:48:17 -07004601 final int resultCount = u.getBluetoothScanResultCounter() != null ?
4602 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004603
4604 sb.setLength(0);
4605 sb.append(prefix);
4606 sb.append(" ");
4607 sb.append("Bluetooth Scan");
4608 sb.append(": ");
4609 if (actualTimeMs != totalTimeMs) {
4610 formatTimeMs(sb, totalTimeMs);
4611 sb.append("blamed realtime, ");
4612 }
4613 formatTimeMs(sb, actualTimeMs); // since reset, regardless of 'which'
4614 sb.append("realtime (");
4615 sb.append(count);
4616 sb.append(" times)");
4617 if (bleTimer.isRunningLocked()) {
4618 sb.append(" (running)");
4619 }
4620 if (actualTimeMsBg != 0 || countBg > 0) {
4621 sb.append(", ");
4622 formatTimeMs(sb, actualTimeMsBg); // since reset, regardless of 'which'
4623 sb.append("background (");
4624 sb.append(countBg);
4625 sb.append(" times)");
4626 }
Bookatz956f36bf2017-04-28 09:48:17 -07004627 sb.append("; Results count ");
4628 sb.append(resultCount);
Bookatz867c0d72017-03-07 18:23:42 -08004629 pw.println(sb.toString());
4630 uidActivity = true;
4631 }
4632 }
4633
4634
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004635
Dianne Hackborn617f8772009-03-31 15:04:46 -07004636 if (u.hasUserActivity()) {
4637 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004638 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004639 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004640 if (val != 0) {
4641 if (!hasData) {
4642 sb.setLength(0);
4643 sb.append(" User activity: ");
4644 hasData = true;
4645 } else {
4646 sb.append(", ");
4647 }
4648 sb.append(val);
4649 sb.append(" ");
4650 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4651 }
4652 }
4653 if (hasData) {
4654 pw.println(sb.toString());
4655 }
4656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004657
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004658 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4659 = u.getWakelockStats();
4660 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004661 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004662 int countWakelock = 0;
4663 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4664 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4665 String linePrefix = ": ";
4666 sb.setLength(0);
4667 sb.append(prefix);
4668 sb.append(" Wake lock ");
4669 sb.append(wakelocks.keyAt(iw));
4670 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4671 "full", which, linePrefix);
4672 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), rawRealtime,
4673 "partial", which, linePrefix);
4674 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4675 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004676 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4677 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004678 sb.append(" realtime");
4679 pw.println(sb.toString());
4680 uidActivity = true;
4681 countWakelock++;
4682
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004683 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4684 rawRealtime, which);
4685 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4686 rawRealtime, which);
4687 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4688 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004689 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004690 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004691 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004692 if (countWakelock > 1) {
4693 if (totalFullWakelock != 0 || totalPartialWakelock != 0
4694 || totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004695 sb.setLength(0);
4696 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004697 sb.append(" TOTAL wake: ");
4698 boolean needComma = false;
4699 if (totalFullWakelock != 0) {
4700 needComma = true;
4701 formatTimeMs(sb, totalFullWakelock);
4702 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004703 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004704 if (totalPartialWakelock != 0) {
4705 if (needComma) {
4706 sb.append(", ");
4707 }
4708 needComma = true;
4709 formatTimeMs(sb, totalPartialWakelock);
4710 sb.append("partial");
4711 }
4712 if (totalWindowWakelock != 0) {
4713 if (needComma) {
4714 sb.append(", ");
4715 }
4716 needComma = true;
4717 formatTimeMs(sb, totalWindowWakelock);
4718 sb.append("window");
4719 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004720 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004721 if (needComma) {
4722 sb.append(",");
4723 }
4724 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004725 formatTimeMs(sb, totalDrawWakelock);
4726 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004727 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004728 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004729 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004730 }
4731 }
4732
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004733 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4734 for (int isy=syncs.size()-1; isy>=0; isy--) {
4735 final Timer timer = syncs.valueAt(isy);
4736 // Convert from microseconds to milliseconds with rounding
4737 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4738 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004739 final Timer bgTimer = timer.getSubTimer();
4740 final long bgTime = bgTimer != null ?
4741 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4742 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004743 sb.setLength(0);
4744 sb.append(prefix);
4745 sb.append(" Sync ");
4746 sb.append(syncs.keyAt(isy));
4747 sb.append(": ");
4748 if (totalTime != 0) {
4749 formatTimeMs(sb, totalTime);
4750 sb.append("realtime (");
4751 sb.append(count);
4752 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07004753 if (bgTime > 0) {
4754 sb.append(", ");
4755 formatTimeMs(sb, bgTime);
4756 sb.append("background (");
4757 sb.append(bgCount);
4758 sb.append(" times)");
4759 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004760 } else {
4761 sb.append("(not used)");
4762 }
4763 pw.println(sb.toString());
4764 uidActivity = true;
4765 }
4766
4767 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4768 for (int ij=jobs.size()-1; ij>=0; ij--) {
4769 final Timer timer = jobs.valueAt(ij);
4770 // Convert from microseconds to milliseconds with rounding
4771 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4772 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004773 final Timer bgTimer = timer.getSubTimer();
4774 final long bgTime = bgTimer != null ?
4775 (bgTimer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000 : -1;
4776 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004777 sb.setLength(0);
4778 sb.append(prefix);
4779 sb.append(" Job ");
4780 sb.append(jobs.keyAt(ij));
4781 sb.append(": ");
4782 if (totalTime != 0) {
4783 formatTimeMs(sb, totalTime);
4784 sb.append("realtime (");
4785 sb.append(count);
4786 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07004787 if (bgTime > 0) {
4788 sb.append(", ");
4789 formatTimeMs(sb, bgTime);
4790 sb.append("background (");
4791 sb.append(bgCount);
4792 sb.append(" times)");
4793 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004794 } else {
4795 sb.append("(not used)");
4796 }
4797 pw.println(sb.toString());
4798 uidActivity = true;
4799 }
4800
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004801 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4802 prefix, "Flashlight");
4803 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4804 prefix, "Camera");
4805 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4806 prefix, "Video");
4807 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4808 prefix, "Audio");
4809
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004810 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4811 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004812 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004813 final Uid.Sensor se = sensors.valueAt(ise);
4814 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004815 sb.setLength(0);
4816 sb.append(prefix);
4817 sb.append(" Sensor ");
4818 int handle = se.getHandle();
4819 if (handle == Uid.Sensor.GPS) {
4820 sb.append("GPS");
4821 } else {
4822 sb.append(handle);
4823 }
4824 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004825
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004826 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004827 if (timer != null) {
4828 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08004829 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
4830 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004831 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08004832 final Timer bgTimer = se.getSensorBackgroundTime();
4833 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
4834 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
4835 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4836 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
4837 final long bgActualTime = bgTimer != null ?
4838 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4839
Dianne Hackborn61659e52014-07-09 16:13:01 -07004840 //timer.logState();
4841 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004842 if (actualTime != totalTime) {
4843 formatTimeMs(sb, totalTime);
4844 sb.append("blamed realtime, ");
4845 }
4846
4847 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07004848 sb.append("realtime (");
4849 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08004850 sb.append(" times)");
4851
4852 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004853 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08004854 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
4855 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004856 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08004857 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08004858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004859 } else {
4860 sb.append("(not used)");
4861 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004862 } else {
4863 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004864 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004865
4866 pw.println(sb.toString());
4867 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004868 }
4869
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004870 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
4871 "Vibrator");
4872 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
4873 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004874
Dianne Hackborn61659e52014-07-09 16:13:01 -07004875 long totalStateTime = 0;
4876 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
4877 long time = u.getProcessStateTime(ips, rawRealtime, which);
4878 if (time > 0) {
4879 totalStateTime += time;
4880 sb.setLength(0);
4881 sb.append(prefix);
4882 sb.append(" ");
4883 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
4884 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08004885 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004886 pw.println(sb.toString());
4887 uidActivity = true;
4888 }
4889 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08004890 if (totalStateTime > 0) {
4891 sb.setLength(0);
4892 sb.append(prefix);
4893 sb.append(" Total running: ");
4894 formatTimeMs(sb, (totalStateTime + 500) / 1000);
4895 pw.println(sb.toString());
4896 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004897
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004898 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
4899 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07004900 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004901 sb.setLength(0);
4902 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07004903 sb.append(" Total cpu time: u=");
4904 formatTimeMs(sb, userCpuTimeUs / 1000);
4905 sb.append("s=");
4906 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004907 pw.println(sb.toString());
4908 }
4909
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004910 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
4911 if (cpuFreqTimes != null) {
4912 sb.setLength(0);
4913 sb.append(" Total cpu time per freq:");
4914 for (int i = 0; i < cpuFreqTimes.length; ++i) {
4915 sb.append(" " + cpuFreqTimes[i]);
4916 }
4917 pw.println(sb.toString());
4918 }
4919 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
4920 if (screenOffCpuFreqTimes != null) {
4921 sb.setLength(0);
4922 sb.append(" Total screen-off cpu time per freq:");
4923 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
4924 sb.append(" " + screenOffCpuFreqTimes[i]);
4925 }
4926 pw.println(sb.toString());
4927 }
4928
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004929 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
4930 = u.getProcessStats();
4931 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
4932 final Uid.Proc ps = processStats.valueAt(ipr);
4933 long userTime;
4934 long systemTime;
4935 long foregroundTime;
4936 int starts;
4937 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004938
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004939 userTime = ps.getUserTime(which);
4940 systemTime = ps.getSystemTime(which);
4941 foregroundTime = ps.getForegroundTime(which);
4942 starts = ps.getStarts(which);
4943 final int numCrashes = ps.getNumCrashes(which);
4944 final int numAnrs = ps.getNumAnrs(which);
4945 numExcessive = which == STATS_SINCE_CHARGED
4946 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004948 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
4949 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
4950 sb.setLength(0);
4951 sb.append(prefix); sb.append(" Proc ");
4952 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
4953 sb.append(prefix); sb.append(" CPU: ");
4954 formatTimeMs(sb, userTime); sb.append("usr + ");
4955 formatTimeMs(sb, systemTime); sb.append("krn ; ");
4956 formatTimeMs(sb, foregroundTime); sb.append("fg");
4957 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
4958 sb.append("\n"); sb.append(prefix); sb.append(" ");
4959 boolean hasOne = false;
4960 if (starts != 0) {
4961 hasOne = true;
4962 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004963 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004964 if (numCrashes != 0) {
4965 if (hasOne) {
4966 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004967 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004968 hasOne = true;
4969 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004970 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004971 if (numAnrs != 0) {
4972 if (hasOne) {
4973 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004974 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004975 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 }
4977 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004978 pw.println(sb.toString());
4979 for (int e=0; e<numExcessive; e++) {
4980 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
4981 if (ew != null) {
4982 pw.print(prefix); pw.print(" * Killed for ");
4983 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
4984 pw.print("wake lock");
4985 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
4986 pw.print("cpu");
4987 } else {
4988 pw.print("unknown");
4989 }
4990 pw.print(" use: ");
4991 TimeUtils.formatDuration(ew.usedTime, pw);
4992 pw.print(" over ");
4993 TimeUtils.formatDuration(ew.overTime, pw);
4994 if (ew.overTime != 0) {
4995 pw.print(" (");
4996 pw.print((ew.usedTime*100)/ew.overTime);
4997 pw.println("%)");
4998 }
4999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 }
5001 uidActivity = true;
5002 }
5003 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005004
5005 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5006 = u.getPackageStats();
5007 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5008 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5009 pw.println(":");
5010 boolean apkActivity = false;
5011 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5012 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5013 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5014 pw.print(prefix); pw.print(" Wakeup alarm ");
5015 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5016 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5017 pw.println(" times");
5018 apkActivity = true;
5019 }
5020 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5021 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5022 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5023 final long startTime = ss.getStartTime(batteryUptime, which);
5024 final int starts = ss.getStarts(which);
5025 final int launches = ss.getLaunches(which);
5026 if (startTime != 0 || starts != 0 || launches != 0) {
5027 sb.setLength(0);
5028 sb.append(prefix); sb.append(" Service ");
5029 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5030 sb.append(prefix); sb.append(" Created for: ");
5031 formatTimeMs(sb, startTime / 1000);
5032 sb.append("uptime\n");
5033 sb.append(prefix); sb.append(" Starts: ");
5034 sb.append(starts);
5035 sb.append(", launches: "); sb.append(launches);
5036 pw.println(sb.toString());
5037 apkActivity = true;
5038 }
5039 }
5040 if (!apkActivity) {
5041 pw.print(prefix); pw.println(" (nothing executed)");
5042 }
5043 uidActivity = true;
5044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005046 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047 }
5048 }
5049 }
5050
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005051 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005052 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005053 int diff = oldval ^ newval;
5054 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005055 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005056 for (int i=0; i<descriptions.length; i++) {
5057 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005058 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005059 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005060 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005061 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005062 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005063 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5064 didWake = true;
5065 pw.print("=");
5066 if (longNames) {
5067 UserHandle.formatUid(pw, wakelockTag.uid);
5068 pw.print(":\"");
5069 pw.print(wakelockTag.string);
5070 pw.print("\"");
5071 } else {
5072 pw.print(wakelockTag.poolIdx);
5073 }
5074 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005075 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005076 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005077 pw.print("=");
5078 int val = (newval&bd.mask)>>bd.shift;
5079 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005080 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005081 } else {
5082 pw.print(val);
5083 }
5084 }
5085 }
5086 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005087 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005088 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005089 if (longNames) {
5090 UserHandle.formatUid(pw, wakelockTag.uid);
5091 pw.print(":\"");
5092 pw.print(wakelockTag.string);
5093 pw.print("\"");
5094 } else {
5095 pw.print(wakelockTag.poolIdx);
5096 }
5097 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005098 }
5099
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005100 public void prepareForDumpLocked() {
5101 }
5102
5103 public static class HistoryPrinter {
5104 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005105 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005106 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005107 int oldStatus = -1;
5108 int oldHealth = -1;
5109 int oldPlug = -1;
5110 int oldTemp = -1;
5111 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005112 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005113 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005114
Dianne Hackborn3251b902014-06-20 14:40:53 -07005115 void reset() {
5116 oldState = oldState2 = 0;
5117 oldLevel = -1;
5118 oldStatus = -1;
5119 oldHealth = -1;
5120 oldPlug = -1;
5121 oldTemp = -1;
5122 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005123 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005124 }
5125
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005126 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005127 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005128 if (!checkin) {
5129 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005130 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005131 pw.print(" (");
5132 pw.print(rec.numReadInts);
5133 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005134 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005135 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5136 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005137 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005138 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005139 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005140 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005141 }
5142 lastTime = rec.time;
5143 }
5144 if (rec.cmd == HistoryItem.CMD_START) {
5145 if (checkin) {
5146 pw.print(":");
5147 }
5148 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005149 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005150 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5151 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005152 if (checkin) {
5153 pw.print(":");
5154 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005155 if (rec.cmd == HistoryItem.CMD_RESET) {
5156 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005157 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005158 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005159 pw.print("TIME:");
5160 if (checkin) {
5161 pw.println(rec.currentTime);
5162 } else {
5163 pw.print(" ");
5164 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5165 rec.currentTime).toString());
5166 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005167 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5168 if (checkin) {
5169 pw.print(":");
5170 }
5171 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005172 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5173 if (checkin) {
5174 pw.print(":");
5175 }
5176 pw.println("*OVERFLOW*");
5177 } else {
5178 if (!checkin) {
5179 if (rec.batteryLevel < 10) pw.print("00");
5180 else if (rec.batteryLevel < 100) pw.print("0");
5181 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005182 if (verbose) {
5183 pw.print(" ");
5184 if (rec.states < 0) ;
5185 else if (rec.states < 0x10) pw.print("0000000");
5186 else if (rec.states < 0x100) pw.print("000000");
5187 else if (rec.states < 0x1000) pw.print("00000");
5188 else if (rec.states < 0x10000) pw.print("0000");
5189 else if (rec.states < 0x100000) pw.print("000");
5190 else if (rec.states < 0x1000000) pw.print("00");
5191 else if (rec.states < 0x10000000) pw.print("0");
5192 pw.print(Integer.toHexString(rec.states));
5193 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005194 } else {
5195 if (oldLevel != rec.batteryLevel) {
5196 oldLevel = rec.batteryLevel;
5197 pw.print(",Bl="); pw.print(rec.batteryLevel);
5198 }
5199 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005200 if (oldStatus != rec.batteryStatus) {
5201 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005202 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005203 switch (oldStatus) {
5204 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005205 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005206 break;
5207 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005208 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005209 break;
5210 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005211 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005212 break;
5213 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005214 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005215 break;
5216 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005217 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005218 break;
5219 default:
5220 pw.print(oldStatus);
5221 break;
5222 }
5223 }
5224 if (oldHealth != rec.batteryHealth) {
5225 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005226 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005227 switch (oldHealth) {
5228 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005229 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005230 break;
5231 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005232 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005233 break;
5234 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005235 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005236 break;
5237 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005238 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005239 break;
5240 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005241 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005242 break;
5243 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005244 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005245 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005246 case BatteryManager.BATTERY_HEALTH_COLD:
5247 pw.print(checkin ? "c" : "cold");
5248 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005249 default:
5250 pw.print(oldHealth);
5251 break;
5252 }
5253 }
5254 if (oldPlug != rec.batteryPlugType) {
5255 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005256 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005257 switch (oldPlug) {
5258 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005259 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005260 break;
5261 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005262 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005263 break;
5264 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005265 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005266 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005267 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005268 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005269 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005270 default:
5271 pw.print(oldPlug);
5272 break;
5273 }
5274 }
5275 if (oldTemp != rec.batteryTemperature) {
5276 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005277 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005278 pw.print(oldTemp);
5279 }
5280 if (oldVolt != rec.batteryVoltage) {
5281 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005282 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005283 pw.print(oldVolt);
5284 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005285 final int chargeMAh = rec.batteryChargeUAh / 1000;
5286 if (oldChargeMAh != chargeMAh) {
5287 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005288 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005289 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005290 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005291 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005292 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005293 printBitDescriptions(pw, oldState2, rec.states2, null,
5294 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005295 if (rec.wakeReasonTag != null) {
5296 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005297 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005298 pw.print(rec.wakeReasonTag.poolIdx);
5299 } else {
5300 pw.print(" wake_reason=");
5301 pw.print(rec.wakeReasonTag.uid);
5302 pw.print(":\"");
5303 pw.print(rec.wakeReasonTag.string);
5304 pw.print("\"");
5305 }
5306 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005307 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005308 pw.print(checkin ? "," : " ");
5309 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5310 pw.print("+");
5311 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5312 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005313 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005314 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5315 : HISTORY_EVENT_NAMES;
5316 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5317 | HistoryItem.EVENT_FLAG_FINISH);
5318 if (idx >= 0 && idx < eventNames.length) {
5319 pw.print(eventNames[idx]);
5320 } else {
5321 pw.print(checkin ? "Ev" : "event");
5322 pw.print(idx);
5323 }
5324 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005325 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005326 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005327 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005328 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5329 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005330 pw.print(":\"");
5331 pw.print(rec.eventTag.string);
5332 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005333 }
5334 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005335 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005336 if (rec.stepDetails != null) {
5337 if (!checkin) {
5338 pw.print(" Details: cpu=");
5339 pw.print(rec.stepDetails.userTime);
5340 pw.print("u+");
5341 pw.print(rec.stepDetails.systemTime);
5342 pw.print("s");
5343 if (rec.stepDetails.appCpuUid1 >= 0) {
5344 pw.print(" (");
5345 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5346 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5347 if (rec.stepDetails.appCpuUid2 >= 0) {
5348 pw.print(", ");
5349 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5350 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5351 }
5352 if (rec.stepDetails.appCpuUid3 >= 0) {
5353 pw.print(", ");
5354 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5355 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5356 }
5357 pw.print(')');
5358 }
5359 pw.println();
5360 pw.print(" /proc/stat=");
5361 pw.print(rec.stepDetails.statUserTime);
5362 pw.print(" usr, ");
5363 pw.print(rec.stepDetails.statSystemTime);
5364 pw.print(" sys, ");
5365 pw.print(rec.stepDetails.statIOWaitTime);
5366 pw.print(" io, ");
5367 pw.print(rec.stepDetails.statIrqTime);
5368 pw.print(" irq, ");
5369 pw.print(rec.stepDetails.statSoftIrqTime);
5370 pw.print(" sirq, ");
5371 pw.print(rec.stepDetails.statIdlTime);
5372 pw.print(" idle");
5373 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5374 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5375 + rec.stepDetails.statSoftIrqTime;
5376 int total = totalRun + rec.stepDetails.statIdlTime;
5377 if (total > 0) {
5378 pw.print(" (");
5379 float perc = ((float)totalRun) / ((float)total) * 100;
5380 pw.print(String.format("%.1f%%", perc));
5381 pw.print(" of ");
5382 StringBuilder sb = new StringBuilder(64);
5383 formatTimeMsNoSpace(sb, total*10);
5384 pw.print(sb);
5385 pw.print(")");
5386 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005387 pw.print(", PlatformIdleStat ");
5388 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005389 pw.println();
5390 } else {
5391 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5392 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5393 pw.print(rec.stepDetails.userTime);
5394 pw.print(":");
5395 pw.print(rec.stepDetails.systemTime);
5396 if (rec.stepDetails.appCpuUid1 >= 0) {
5397 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5398 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5399 if (rec.stepDetails.appCpuUid2 >= 0) {
5400 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5401 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5402 }
5403 if (rec.stepDetails.appCpuUid3 >= 0) {
5404 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5405 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5406 }
5407 }
5408 pw.println();
5409 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5410 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5411 pw.print(rec.stepDetails.statUserTime);
5412 pw.print(',');
5413 pw.print(rec.stepDetails.statSystemTime);
5414 pw.print(',');
5415 pw.print(rec.stepDetails.statIOWaitTime);
5416 pw.print(',');
5417 pw.print(rec.stepDetails.statIrqTime);
5418 pw.print(',');
5419 pw.print(rec.stepDetails.statSoftIrqTime);
5420 pw.print(',');
5421 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005422 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005423 if (rec.stepDetails.statPlatformIdleState != null) {
5424 pw.print(rec.stepDetails.statPlatformIdleState);
5425 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005426 pw.println();
5427 }
5428 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005429 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005430 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005431 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005432 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005433
5434 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5435 UserHandle.formatUid(pw, uid);
5436 pw.print("=");
5437 pw.print(utime);
5438 pw.print("u+");
5439 pw.print(stime);
5440 pw.print("s");
5441 }
5442
5443 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5444 pw.print('/');
5445 pw.print(uid);
5446 pw.print(":");
5447 pw.print(utime);
5448 pw.print(":");
5449 pw.print(stime);
5450 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005451 }
5452
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005453 private void printSizeValue(PrintWriter pw, long size) {
5454 float result = size;
5455 String suffix = "";
5456 if (result >= 10*1024) {
5457 suffix = "KB";
5458 result = result / 1024;
5459 }
5460 if (result >= 10*1024) {
5461 suffix = "MB";
5462 result = result / 1024;
5463 }
5464 if (result >= 10*1024) {
5465 suffix = "GB";
5466 result = result / 1024;
5467 }
5468 if (result >= 10*1024) {
5469 suffix = "TB";
5470 result = result / 1024;
5471 }
5472 if (result >= 10*1024) {
5473 suffix = "PB";
5474 result = result / 1024;
5475 }
5476 pw.print((int)result);
5477 pw.print(suffix);
5478 }
5479
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005480 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5481 String label3, long estimatedTime) {
5482 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005483 return false;
5484 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005485 pw.print(label1);
5486 pw.print(label2);
5487 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005488 StringBuilder sb = new StringBuilder(64);
5489 formatTimeMs(sb, estimatedTime);
5490 pw.print(sb);
5491 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005492 return true;
5493 }
5494
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005495 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5496 LevelStepTracker steps, boolean checkin) {
5497 if (steps == null) {
5498 return false;
5499 }
5500 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005501 if (count <= 0) {
5502 return false;
5503 }
5504 if (!checkin) {
5505 pw.println(header);
5506 }
Kweku Adams030980a2015-04-01 16:07:48 -07005507 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005508 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005509 long duration = steps.getDurationAt(i);
5510 int level = steps.getLevelAt(i);
5511 long initMode = steps.getInitModeAt(i);
5512 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005513 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005514 lineArgs[0] = Long.toString(duration);
5515 lineArgs[1] = Integer.toString(level);
5516 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5517 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5518 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5519 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5520 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5521 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005522 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005523 }
5524 } else {
5525 lineArgs[2] = "";
5526 }
5527 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5528 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5529 } else {
5530 lineArgs[3] = "";
5531 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005532 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005533 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005534 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005535 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005536 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005537 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5538 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005539 pw.print(prefix);
5540 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005541 TimeUtils.formatDuration(duration, pw);
5542 pw.print(" to "); pw.print(level);
5543 boolean haveModes = false;
5544 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5545 pw.print(" (");
5546 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5547 case Display.STATE_OFF: pw.print("screen-off"); break;
5548 case Display.STATE_ON: pw.print("screen-on"); break;
5549 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5550 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005551 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005552 }
5553 haveModes = true;
5554 }
5555 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5556 pw.print(haveModes ? ", " : " (");
5557 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5558 ? "power-save-on" : "power-save-off");
5559 haveModes = true;
5560 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005561 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5562 pw.print(haveModes ? ", " : " (");
5563 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5564 ? "device-idle-on" : "device-idle-off");
5565 haveModes = true;
5566 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005567 if (haveModes) {
5568 pw.print(")");
5569 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005570 pw.println();
5571 }
5572 }
5573 return true;
5574 }
5575
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005576 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005577 public static final int DUMP_DAILY_ONLY = 1<<2;
5578 public static final int DUMP_HISTORY_ONLY = 1<<3;
5579 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5580 public static final int DUMP_VERBOSE = 1<<5;
5581 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005582
Dianne Hackborn37de0982014-05-09 09:32:18 -07005583 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5584 final HistoryPrinter hprinter = new HistoryPrinter();
5585 final HistoryItem rec = new HistoryItem();
5586 long lastTime = -1;
5587 long baseTime = -1;
5588 boolean printed = false;
5589 HistoryEventTracker tracker = null;
5590 while (getNextHistoryLocked(rec)) {
5591 lastTime = rec.time;
5592 if (baseTime < 0) {
5593 baseTime = lastTime;
5594 }
5595 if (rec.time >= histStart) {
5596 if (histStart >= 0 && !printed) {
5597 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005598 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005599 || rec.cmd == HistoryItem.CMD_START
5600 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005601 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005602 hprinter.printNextItem(pw, rec, baseTime, checkin,
5603 (flags&DUMP_VERBOSE) != 0);
5604 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005605 } else if (rec.currentTime != 0) {
5606 printed = true;
5607 byte cmd = rec.cmd;
5608 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005609 hprinter.printNextItem(pw, rec, baseTime, checkin,
5610 (flags&DUMP_VERBOSE) != 0);
5611 rec.cmd = cmd;
5612 }
5613 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005614 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5615 hprinter.printNextItem(pw, rec, baseTime, checkin,
5616 (flags&DUMP_VERBOSE) != 0);
5617 rec.cmd = HistoryItem.CMD_UPDATE;
5618 }
5619 int oldEventCode = rec.eventCode;
5620 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005621 rec.eventTag = new HistoryTag();
5622 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5623 HashMap<String, SparseIntArray> active
5624 = tracker.getStateForEvent(i);
5625 if (active == null) {
5626 continue;
5627 }
5628 for (HashMap.Entry<String, SparseIntArray> ent
5629 : active.entrySet()) {
5630 SparseIntArray uids = ent.getValue();
5631 for (int j=0; j<uids.size(); j++) {
5632 rec.eventCode = i;
5633 rec.eventTag.string = ent.getKey();
5634 rec.eventTag.uid = uids.keyAt(j);
5635 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005636 hprinter.printNextItem(pw, rec, baseTime, checkin,
5637 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005638 rec.wakeReasonTag = null;
5639 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005640 }
5641 }
5642 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005643 rec.eventCode = oldEventCode;
5644 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005645 tracker = null;
5646 }
5647 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005648 hprinter.printNextItem(pw, rec, baseTime, checkin,
5649 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005650 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5651 // This is an attempt to aggregate the previous state and generate
5652 // fake events to reflect that state at the point where we start
5653 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005654 if (tracker == null) {
5655 tracker = new HistoryEventTracker();
5656 }
5657 tracker.updateState(rec.eventCode, rec.eventTag.string,
5658 rec.eventTag.uid, rec.eventTag.poolIdx);
5659 }
5660 }
5661 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005662 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005663 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5664 }
5665 }
5666
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005667 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5668 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5669 if (steps == null) {
5670 return;
5671 }
5672 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5673 if (timeRemaining >= 0) {
5674 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5675 tmpSb.setLength(0);
5676 formatTimeMs(tmpSb, timeRemaining);
5677 pw.print(tmpSb);
5678 pw.print(" (from "); pw.print(tmpOutInt[0]);
5679 pw.println(" steps)");
5680 }
5681 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5682 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5683 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5684 if (estimatedTime > 0) {
5685 pw.print(prefix); pw.print(label); pw.print(" ");
5686 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5687 pw.print(" time: ");
5688 tmpSb.setLength(0);
5689 formatTimeMs(tmpSb, estimatedTime);
5690 pw.print(tmpSb);
5691 pw.print(" (from "); pw.print(tmpOutInt[0]);
5692 pw.println(" steps)");
5693 }
5694 }
5695 }
5696
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005697 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5698 ArrayList<PackageChange> changes) {
5699 if (changes == null) {
5700 return;
5701 }
5702 pw.print(prefix); pw.println("Package changes:");
5703 for (int i=0; i<changes.size(); i++) {
5704 PackageChange pc = changes.get(i);
5705 if (pc.mUpdate) {
5706 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5707 pw.print(" vers="); pw.println(pc.mVersionCode);
5708 } else {
5709 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5710 }
5711 }
5712 }
5713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 /**
5715 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5716 *
5717 * @param pw a Printer to receive the dump output.
5718 */
5719 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005720 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005721 prepareForDumpLocked();
5722
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005723 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005724 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005725
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005726 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005727 final long historyTotalSize = getHistoryTotalSize();
5728 final long historyUsedSize = getHistoryUsedSize();
5729 if (startIteratingHistoryLocked()) {
5730 try {
5731 pw.print("Battery History (");
5732 pw.print((100*historyUsedSize)/historyTotalSize);
5733 pw.print("% used, ");
5734 printSizeValue(pw, historyUsedSize);
5735 pw.print(" used of ");
5736 printSizeValue(pw, historyTotalSize);
5737 pw.print(", ");
5738 pw.print(getHistoryStringPoolSize());
5739 pw.print(" strings using ");
5740 printSizeValue(pw, getHistoryStringPoolBytes());
5741 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005742 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005743 pw.println();
5744 } finally {
5745 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005746 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005747 }
5748
5749 if (startIteratingOldHistoryLocked()) {
5750 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005751 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005752 pw.println("Old battery History:");
5753 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005754 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005755 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005756 if (baseTime < 0) {
5757 baseTime = rec.time;
5758 }
5759 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005760 }
5761 pw.println();
5762 } finally {
5763 finishIteratingOldHistoryLocked();
5764 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005765 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005766 }
5767
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005768 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005769 return;
5770 }
5771
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005772 if (!filtering) {
5773 SparseArray<? extends Uid> uidStats = getUidStats();
5774 final int NU = uidStats.size();
5775 boolean didPid = false;
5776 long nowRealtime = SystemClock.elapsedRealtime();
5777 for (int i=0; i<NU; i++) {
5778 Uid uid = uidStats.valueAt(i);
5779 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5780 if (pids != null) {
5781 for (int j=0; j<pids.size(); j++) {
5782 Uid.Pid pid = pids.valueAt(j);
5783 if (!didPid) {
5784 pw.println("Per-PID Stats:");
5785 didPid = true;
5786 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005787 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5788 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005789 pw.print(" PID "); pw.print(pids.keyAt(j));
5790 pw.print(" wake time: ");
5791 TimeUtils.formatDuration(time, pw);
5792 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005793 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005794 }
5795 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005796 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005797 pw.println();
5798 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005799 }
5800
5801 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005802 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5803 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005804 long timeRemaining = computeBatteryTimeRemaining(
5805 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005806 if (timeRemaining >= 0) {
5807 pw.print(" Estimated discharge time remaining: ");
5808 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5809 pw.println();
5810 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005811 final LevelStepTracker steps = getDischargeLevelStepTracker();
5812 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5813 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
5814 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5815 STEP_LEVEL_MODE_VALUES[i], null));
5816 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005817 pw.println();
5818 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005819 if (dumpDurationSteps(pw, " ", "Charge step durations:",
5820 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005821 long timeRemaining = computeChargeTimeRemaining(
5822 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005823 if (timeRemaining >= 0) {
5824 pw.print(" Estimated charge time remaining: ");
5825 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5826 pw.println();
5827 }
5828 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005829 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005830 }
5831 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
5832 pw.println("Daily stats:");
5833 pw.print(" Current start time: ");
5834 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5835 getCurrentDailyStartTime()).toString());
5836 pw.print(" Next min deadline: ");
5837 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5838 getNextMinDailyDeadline()).toString());
5839 pw.print(" Next max deadline: ");
5840 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5841 getNextMaxDailyDeadline()).toString());
5842 StringBuilder sb = new StringBuilder(64);
5843 int[] outInt = new int[1];
5844 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
5845 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005846 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
5847 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005848 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005849 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
5850 dsteps, false)) {
5851 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5852 sb, outInt);
5853 }
5854 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
5855 csteps, false)) {
5856 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5857 sb, outInt);
5858 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005859 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005860 } else {
5861 pw.println(" Current daily steps:");
5862 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
5863 sb, outInt);
5864 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
5865 sb, outInt);
5866 }
5867 }
5868 DailyItem dit;
5869 int curIndex = 0;
5870 while ((dit=getDailyItemLocked(curIndex)) != null) {
5871 curIndex++;
5872 if ((flags&DUMP_DAILY_ONLY) != 0) {
5873 pw.println();
5874 }
5875 pw.print(" Daily from ");
5876 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
5877 pw.print(" to ");
5878 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
5879 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005880 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005881 if (dumpDurationSteps(pw, " ",
5882 " Discharge step durations:", dit.mDischargeSteps, false)) {
5883 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5884 sb, outInt);
5885 }
5886 if (dumpDurationSteps(pw, " ",
5887 " Charge step durations:", dit.mChargeSteps, false)) {
5888 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5889 sb, outInt);
5890 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005891 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005892 } else {
5893 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
5894 sb, outInt);
5895 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
5896 sb, outInt);
5897 }
5898 }
5899 pw.println();
5900 }
5901 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005902 pw.println("Statistics since last charge:");
5903 pw.println(" System starts: " + getStartCount()
5904 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07005905 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
5906 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005907 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07005908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 }
5910
5911 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005912 public void dumpCheckinLocked(Context context, PrintWriter pw,
5913 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005914 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005915
5916 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005917 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
5918 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005919
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005920 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
5921
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005922 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005923 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005924
5925 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07005926 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005927 try {
5928 for (int i=0; i<getHistoryStringPoolSize(); i++) {
5929 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5930 pw.print(HISTORY_STRING_POOL); pw.print(',');
5931 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005932 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005933 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005934 pw.print(",\"");
5935 String str = getHistoryTagPoolString(i);
5936 str = str.replace("\\", "\\\\");
5937 str = str.replace("\"", "\\\"");
5938 pw.print(str);
5939 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005940 pw.println();
5941 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005942 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005943 } finally {
5944 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08005945 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005946 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07005947 }
5948
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005949 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005950 return;
5951 }
5952
Dianne Hackborne4a59512010-12-07 11:08:07 -08005953 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005954 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08005955 for (int i=0; i<apps.size(); i++) {
5956 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005957 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
5958 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08005959 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005960 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
5961 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005962 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005963 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005964 }
5965 SparseArray<? extends Uid> uidStats = getUidStats();
5966 final int NU = uidStats.size();
5967 String[] lineArgs = new String[2];
5968 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005969 int uid = UserHandle.getAppId(uidStats.keyAt(i));
5970 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
5971 if (pkgs != null && !pkgs.second.value) {
5972 pkgs.second.value = true;
5973 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08005974 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07005975 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08005976 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
5977 (Object[])lineArgs);
5978 }
5979 }
5980 }
5981 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005982 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005983 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005984 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07005985 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005986 if (timeRemaining >= 0) {
5987 lineArgs[0] = Long.toString(timeRemaining);
5988 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
5989 (Object[])lineArgs);
5990 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005991 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07005992 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005993 if (timeRemaining >= 0) {
5994 lineArgs[0] = Long.toString(timeRemaining);
5995 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
5996 (Object[])lineArgs);
5997 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005998 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
5999 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002}