blob: cf4fd99c3ec2c0417e1c95e1f83aa2bde0fdeb9e [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 /**
Bookatzc8c44962017-05-11 12:12:54 -0700160 * A constant indicating an aggregated partial wake lock timer.
161 */
162 public static final int AGGREGATED_WAKE_TYPE_PARTIAL = 20;
163
164 /**
Bookatzb1f04f32017-05-19 13:57:32 -0700165 * A constant indicating a bluetooth scan timer for unoptimized scans.
166 */
167 public static final int BLUETOOTH_UNOPTIMIZED_SCAN_ON = 21;
168
169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 * Include all of the data in the stats, including previously saved data.
171 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700172 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
174 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * Include only the current run in the stats.
176 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700177 public static final int STATS_CURRENT = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
179 /**
180 * Include only the run since the last time the device was unplugged in the stats.
181 */
Dianne Hackborn4590e522014-03-24 13:36:46 -0700182 public static final int STATS_SINCE_UNPLUGGED = 2;
Evan Millare84de8d2009-04-02 22:16:12 -0700183
184 // NOTE: Update this list if you add/change any stats above.
185 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700186 // and "unplugged". They were shortened for efficiency sake.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700187 private static final String[] STAT_NAMES = { "l", "c", "u" };
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 /**
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700190 * Current version of checkin data format.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700191 *
192 * New in version 19:
193 * - Wakelock data (wl) gets current and max times.
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800194 * New in version 20:
Bookatz2bffb5b2017-04-13 11:59:33 -0700195 * - Background timers and counters for: Sensor, BluetoothScan, WifiScan, Jobs, Syncs.
Bookatz506a8182017-05-01 14:18:42 -0700196 * New in version 21:
197 * - Actual (not just apportioned) Wakelock time is also recorded.
Bookatzc8c44962017-05-11 12:12:54 -0700198 * - Aggregated partial wakelock time (per uid, instead of per wakelock) is recorded.
Bookatzb1f04f32017-05-19 13:57:32 -0700199 * - BLE scan result count
200 * - CPU frequency time per uid
201 * New in version 22:
202 * - BLE scan result background count, BLE unoptimized scan time
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700203 */
Bookatz6d799932017-06-07 12:30:07 -0700204 static final String CHECKIN_VERSION = "23";
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700205
206 /**
207 * Old version, we hit 9 and ran out of room, need to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 */
Ashish Sharma213bb2f2014-07-07 17:14:52 -0700209 private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700210
Evan Millar22ac0432009-03-31 11:33:18 -0700211 private static final long BYTES_PER_KB = 1024;
212 private static final long BYTES_PER_MB = 1048576; // 1024^2
213 private static final long BYTES_PER_GB = 1073741824; //1024^3
Bookatz506a8182017-05-01 14:18:42 -0700214
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700215 private static final String VERSION_DATA = "vers";
Dianne Hackborne4a59512010-12-07 11:08:07 -0800216 private static final String UID_DATA = "uid";
Joe Onorato1476d322016-05-05 14:46:15 -0700217 private static final String WAKEUP_ALARM_DATA = "wua";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700219 private static final String PROCESS_DATA = "pr";
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700220 private static final String CPU_DATA = "cpu";
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700221 private static final String GLOBAL_CPU_FREQ_DATA = "gcf";
222 private static final String CPU_TIMES_AT_FREQ_DATA = "ctf";
Evan Millare84de8d2009-04-02 22:16:12 -0700223 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800224 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700225 private static final String FOREGROUND_DATA = "fg";
Dianne Hackborn61659e52014-07-09 16:13:01 -0700226 private static final String STATE_TIME_DATA = "st";
Bookatz506a8182017-05-01 14:18:42 -0700227 // wl line is:
228 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "wl", name,
Bookatz5b5ec322017-05-26 09:40:38 -0700229 // full totalTime, 'f', count, current duration, max duration, total duration,
230 // partial totalTime, 'p', count, current duration, max duration, total duration,
231 // bg partial totalTime, 'bp', count, current duration, max duration, total duration,
232 // window totalTime, 'w', count, current duration, max duration, total duration
Bookatz506a8182017-05-01 14:18:42 -0700233 // [Currently, full and window wakelocks have durations current = max = total = -1]
Evan Millare84de8d2009-04-02 22:16:12 -0700234 private static final String WAKELOCK_DATA = "wl";
Bookatzc8c44962017-05-11 12:12:54 -0700235 // awl line is:
236 // BATTERY_STATS_CHECKIN_VERSION, uid, which, "awl",
237 // cumulative partial wakelock duration, cumulative background partial wakelock duration
238 private static final String AGGREGATED_WAKELOCK_DATA = "awl";
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700239 private static final String SYNC_DATA = "sy";
240 private static final String JOB_DATA = "jb";
Evan Millarc64edde2009-04-18 12:26:32 -0700241 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700242 private static final String WAKEUP_REASON_DATA = "wr";
Evan Millare84de8d2009-04-02 22:16:12 -0700243 private static final String NETWORK_DATA = "nt";
244 private static final String USER_ACTIVITY_DATA = "ua";
245 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800246 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700247 private static final String BATTERY_LEVEL_DATA = "lv";
Adam Lesinskie283d332015-04-16 12:29:25 -0700248 private static final String GLOBAL_WIFI_DATA = "gwfl";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700249 private static final String WIFI_DATA = "wfl";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800250 private static final String GLOBAL_WIFI_CONTROLLER_DATA = "gwfcd";
251 private static final String WIFI_CONTROLLER_DATA = "wfcd";
252 private static final String GLOBAL_BLUETOOTH_CONTROLLER_DATA = "gble";
253 private static final String BLUETOOTH_CONTROLLER_DATA = "ble";
Adam Lesinskid9b99be2016-03-30 16:58:51 -0700254 private static final String BLUETOOTH_MISC_DATA = "blem";
Evan Millare84de8d2009-04-02 22:16:12 -0700255 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800256 private static final String GLOBAL_NETWORK_DATA = "gn";
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800257 private static final String GLOBAL_MODEM_CONTROLLER_DATA = "gmcd";
258 private static final String MODEM_CONTROLLER_DATA = "mcd";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800259 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700260 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700261 private static final String SCREEN_BRIGHTNESS_DATA = "br";
262 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700263 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700264 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
265 private static final String DATA_CONNECTION_TIME_DATA = "dct";
266 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800267 private static final String WIFI_STATE_TIME_DATA = "wst";
268 private static final String WIFI_STATE_COUNT_DATA = "wsc";
Dianne Hackborn3251b902014-06-20 14:40:53 -0700269 private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
270 private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
271 private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
272 private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800273 private static final String POWER_USE_SUMMARY_DATA = "pws";
274 private static final String POWER_USE_ITEM_DATA = "pwi";
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -0700275 private static final String DISCHARGE_STEP_DATA = "dsd";
276 private static final String CHARGE_STEP_DATA = "csd";
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -0700277 private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
278 private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700279 private static final String FLASHLIGHT_DATA = "fla";
280 private static final String CAMERA_DATA = "cam";
281 private static final String VIDEO_DATA = "vid";
282 private static final String AUDIO_DATA = "aud";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
Adam Lesinski010bf372016-04-11 12:18:18 -0700284 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
285
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700286 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 private final Formatter mFormatter = new Formatter(mFormatBuilder);
288
289 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700290 * Indicates times spent by the uid at each cpu frequency in all process states.
291 *
292 * Other types might include times spent in foreground, background etc.
293 */
294 private final String UID_TIMES_TYPE_ALL = "A";
295
296 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700297 * State for keeping track of counting information.
298 */
299 public static abstract class Counter {
300
301 /**
302 * Returns the count associated with this Counter for the
303 * selected type of statistics.
304 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700305 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborn617f8772009-03-31 15:04:46 -0700306 */
Evan Millarc64edde2009-04-18 12:26:32 -0700307 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700308
309 /**
310 * Temporary for debugging.
311 */
312 public abstract void logState(Printer pw, String prefix);
313 }
314
315 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700316 * State for keeping track of long counting information.
317 */
318 public static abstract class LongCounter {
319
320 /**
321 * Returns the count associated with this Counter for the
322 * selected type of statistics.
323 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700324 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700325 */
326 public abstract long getCountLocked(int which);
327
328 /**
329 * Temporary for debugging.
330 */
331 public abstract void logState(Printer pw, String prefix);
332 }
333
334 /**
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700335 * State for keeping track of array of long counting information.
336 */
337 public static abstract class LongCounterArray {
338 /**
339 * Returns the counts associated with this Counter for the
340 * selected type of statistics.
341 *
342 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
343 */
344 public abstract long[] getCountsLocked(int which);
345
346 /**
347 * Temporary for debugging.
348 */
349 public abstract void logState(Printer pw, String prefix);
350 }
351
352 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800353 * Container class that aggregates counters for transmit, receive, and idle state of a
354 * radio controller.
355 */
356 public static abstract class ControllerActivityCounter {
357 /**
358 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
359 * idle state.
360 */
361 public abstract LongCounter getIdleTimeCounter();
362
363 /**
364 * @return a non-null {@link LongCounter} representing time spent (milliseconds) in the
365 * receive state.
366 */
367 public abstract LongCounter getRxTimeCounter();
368
369 /**
370 * An array of {@link LongCounter}, representing various transmit levels, where each level
371 * may draw a different amount of power. The levels themselves are controller-specific.
372 * @return non-null array of {@link LongCounter}s representing time spent (milliseconds) in
373 * various transmit level states.
374 */
375 public abstract LongCounter[] getTxTimeCounters();
376
377 /**
378 * @return a non-null {@link LongCounter} representing the power consumed by the controller
379 * in all states, measured in milli-ampere-milliseconds (mAms). The counter may always
380 * yield a value of 0 if the device doesn't support power calculations.
381 */
382 public abstract LongCounter getPowerCounter();
383 }
384
385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 * State for keeping track of timing information.
387 */
388 public static abstract class Timer {
389
390 /**
391 * Returns the count associated with this Timer for the
392 * selected type of statistics.
393 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700394 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 */
Evan Millarc64edde2009-04-18 12:26:32 -0700396 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397
398 /**
399 * Returns the total time in microseconds associated with this Timer for the
400 * selected type of statistics.
401 *
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800402 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700403 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * @return a time in microseconds
405 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800406 public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700409 * Returns the total time in microseconds associated with this Timer since the
410 * 'mark' was last set.
411 *
412 * @param elapsedRealtimeUs current elapsed realtime of system in microseconds
413 * @return a time in microseconds
414 */
415 public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
416
417 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700418 * Returns the max duration if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800419 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700420
421 */
422 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
423 return -1;
424 }
425
426 /**
427 * Returns the current time the timer has been active, if it is being tracked.
Bookatz867c0d72017-03-07 18:23:42 -0800428 * Not all Timer subclasses track the max, total, current durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -0700429 */
430 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
431 return -1;
432 }
433
434 /**
Bookatz867c0d72017-03-07 18:23:42 -0800435 * Returns the current time the timer has been active, if it is being tracked.
436 *
437 * Returns the total cumulative duration (i.e. sum of past durations) that this timer has
438 * been on since reset.
439 * This may differ from getTotalTimeLocked(elapsedRealtimeUs, STATS_SINCE_CHARGED)/1000 since,
440 * depending on the Timer, getTotalTimeLocked may represent the total 'blamed' or 'pooled'
441 * time, rather than the actual time. By contrast, getTotalDurationMsLocked always gives
442 * the actual total time.
443 * Not all Timer subclasses track the max, total, current durations.
444 */
445 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
446 return -1;
447 }
448
449 /**
Bookatzaa4594a2017-03-24 12:39:56 -0700450 * Returns the secondary Timer held by the Timer, if one exists. This secondary timer may be
451 * used, for example, for tracking background usage. Secondary timers are never pooled.
452 *
453 * Not all Timer subclasses have a secondary timer; those that don't return null.
454 */
455 public Timer getSubTimer() {
456 return null;
457 }
458
459 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -0700460 * Returns whether the timer is currently running. Some types of timers
461 * (e.g. BatchTimers) don't know whether the event is currently active,
462 * and report false.
463 */
464 public boolean isRunningLocked() {
465 return false;
466 }
467
468 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 * Temporary for debugging.
470 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700471 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 }
473
474 /**
475 * The statistics associated with a particular uid.
476 */
477 public static abstract class Uid {
478
479 /**
480 * Returns a mapping containing wakelock statistics.
481 *
482 * @return a Map from Strings to Uid.Wakelock objects.
483 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700484 public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485
486 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700487 * Returns a mapping containing sync statistics.
488 *
489 * @return a Map from Strings to Timer objects.
490 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700491 public abstract ArrayMap<String, ? extends Timer> getSyncStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700492
493 /**
494 * Returns a mapping containing scheduled job statistics.
495 *
496 * @return a Map from Strings to Timer objects.
497 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700498 public abstract ArrayMap<String, ? extends Timer> getJobStats();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700499
500 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 * The statistics associated with a particular wake lock.
502 */
503 public static abstract class Wakelock {
504 public abstract Timer getWakeTime(int type);
505 }
506
507 /**
Bookatzc8c44962017-05-11 12:12:54 -0700508 * The cumulative time the uid spent holding any partial wakelocks. This will generally
509 * differ from summing over the Wakelocks in getWakelockStats since the latter may have
510 * wakelocks that overlap in time (and therefore over-counts).
511 */
512 public abstract Timer getAggregatedPartialWakelockTimer();
513
514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 * Returns a mapping containing sensor statistics.
516 *
517 * @return a Map from Integer sensor ids to Uid.Sensor objects.
518 */
Dianne Hackborn61659e52014-07-09 16:13:01 -0700519 public abstract SparseArray<? extends Sensor> getSensorStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520
521 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700522 * Returns a mapping containing active process data.
523 */
524 public abstract SparseArray<? extends Pid> getPidStats();
Bookatzc8c44962017-05-11 12:12:54 -0700525
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700526 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * Returns a mapping containing process statistics.
528 *
529 * @return a Map from Strings to Uid.Proc objects.
530 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700531 public abstract ArrayMap<String, ? extends Proc> getProcessStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532
533 /**
534 * Returns a mapping containing package statistics.
535 *
536 * @return a Map from Strings to Uid.Pkg objects.
537 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700538 public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
Adam Lesinskie08af192015-03-25 16:42:59 -0700539
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800540 public abstract ControllerActivityCounter getWifiControllerActivity();
541 public abstract ControllerActivityCounter getBluetoothControllerActivity();
542 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski50e47602015-12-04 17:04:54 -0800543
544 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 * {@hide}
546 */
547 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700548
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800549 public abstract void noteWifiRunningLocked(long elapsedRealtime);
550 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
551 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
552 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
553 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
554 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
555 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
556 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
557 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
558 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800559 public abstract void noteActivityResumedLocked(long elapsedRealtime);
560 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800561 public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
562 public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
563 public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700564 public abstract int getWifiScanCount(int which);
Bookatz867c0d72017-03-07 18:23:42 -0800565 public abstract int getWifiScanBackgroundCount(int which);
566 public abstract long getWifiScanActualTime(long elapsedRealtimeUs);
567 public abstract long getWifiScanBackgroundTime(long elapsedRealtimeUs);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800568 public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
Dianne Hackborn62793e42015-03-09 11:15:41 -0700569 public abstract int getWifiBatchedScanCount(int csphBin, int which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800570 public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700571 public abstract Timer getAudioTurnedOnTimer();
572 public abstract Timer getVideoTurnedOnTimer();
573 public abstract Timer getFlashlightTurnedOnTimer();
574 public abstract Timer getCameraTurnedOnTimer();
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700575 public abstract Timer getForegroundActivityTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800576 public abstract Timer getBluetoothScanTimer();
Bookatz867c0d72017-03-07 18:23:42 -0800577 public abstract Timer getBluetoothScanBackgroundTimer();
Bookatzb1f04f32017-05-19 13:57:32 -0700578 public abstract Timer getBluetoothUnoptimizedScanTimer();
579 public abstract Timer getBluetoothUnoptimizedScanBackgroundTimer();
Bookatz956f36bf2017-04-28 09:48:17 -0700580 public abstract Counter getBluetoothScanResultCounter();
Bookatzb1f04f32017-05-19 13:57:32 -0700581 public abstract Counter getBluetoothScanResultBgCounter();
Dianne Hackborn61659e52014-07-09 16:13:01 -0700582
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700583 public abstract long[] getCpuFreqTimes(int which);
584 public abstract long[] getScreenOffCpuFreqTimes(int which);
585
Dianne Hackborna0200e32016-03-30 18:01:41 -0700586 // Note: the following times are disjoint. They can be added together to find the
587 // total time a uid has had any processes running at all.
588
589 /**
590 * Time this uid has any processes in the top state (or above such as persistent).
591 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800592 public static final int PROCESS_STATE_TOP = 0;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700593 /**
594 * Time this uid has any process with a started out bound foreground service, but
595 * none in the "top" state.
596 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800597 public static final int PROCESS_STATE_FOREGROUND_SERVICE = 1;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700598 /**
599 * Time this uid has any process that is top while the device is sleeping, but none
600 * in the "foreground service" or better state.
601 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800602 public static final int PROCESS_STATE_TOP_SLEEPING = 2;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700603 /**
604 * Time this uid has any process in an active foreground state, but none in the
605 * "top sleeping" or better state.
606 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800607 public static final int PROCESS_STATE_FOREGROUND = 3;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700608 /**
609 * Time this uid has any process in an active background state, but none in the
610 * "foreground" or better state.
611 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800612 public static final int PROCESS_STATE_BACKGROUND = 4;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700613 /**
614 * Time this uid has any processes that are sitting around cached, not in one of the
615 * other active states.
616 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800617 public static final int PROCESS_STATE_CACHED = 5;
Dianne Hackborna0200e32016-03-30 18:01:41 -0700618 /**
619 * Total number of process states we track.
620 */
Dianne Hackborna8d10942015-11-19 17:55:19 -0800621 public static final int NUM_PROCESS_STATE = 6;
Dianne Hackborn61659e52014-07-09 16:13:01 -0700622
623 static final String[] PROCESS_STATE_NAMES = {
Dianne Hackborna8d10942015-11-19 17:55:19 -0800624 "Top", "Fg Service", "Top Sleeping", "Foreground", "Background", "Cached"
Dianne Hackborn61659e52014-07-09 16:13:01 -0700625 };
626
627 public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
Joe Onorato713fec82016-03-04 10:34:02 -0800628 public abstract Timer getProcessStateTimer(int state);
Dianne Hackborn61659e52014-07-09 16:13:01 -0700629
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800630 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631
Robert Greenwalta029ea12013-09-25 16:38:12 -0700632 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
633
Dianne Hackborn617f8772009-03-31 15:04:46 -0700634 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700635 * Note that these must match the constants in android.os.PowerManager.
636 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
637 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700638 */
639 static final String[] USER_ACTIVITY_TYPES = {
Phil Weaverda80d672016-03-15 16:25:46 -0700640 "other", "button", "touch", "accessibility"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700641 };
Bookatzc8c44962017-05-11 12:12:54 -0700642
Phil Weaverda80d672016-03-15 16:25:46 -0700643 public static final int NUM_USER_ACTIVITY_TYPES = 4;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700644
Dianne Hackborn617f8772009-03-31 15:04:46 -0700645 public abstract void noteUserActivityLocked(int type);
646 public abstract boolean hasUserActivity();
647 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700648
649 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800650 public abstract long getNetworkActivityBytes(int type, int which);
651 public abstract long getNetworkActivityPackets(int type, int which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800652 public abstract long getMobileRadioActiveTime(int which);
653 public abstract int getMobileRadioActiveCount(int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700654
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700655 /**
656 * Get the total cpu time (in microseconds) this UID had processes executing in userspace.
657 */
658 public abstract long getUserCpuTimeUs(int which);
659
660 /**
661 * Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
662 */
663 public abstract long getSystemCpuTimeUs(int which);
664
665 /**
Adam Lesinski6832f392015-09-05 18:05:40 -0700666 * Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
667 * given CPU cluster.
668 * @param cluster the index of the CPU cluster.
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -0700669 * @param step the index of the CPU speed. This is not the actual speed of the CPU.
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700670 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700671 * @see com.android.internal.os.PowerProfile#getNumCpuClusters()
672 * @see com.android.internal.os.PowerProfile#getNumSpeedStepsInCpuCluster(int)
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700673 */
Adam Lesinski6832f392015-09-05 18:05:40 -0700674 public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700675
Adam Lesinski5f056f62016-07-14 16:56:08 -0700676 /**
677 * Returns the number of times this UID woke up the Application Processor to
678 * process a mobile radio packet.
679 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
680 */
681 public abstract long getMobileRadioApWakeupCount(int which);
682
683 /**
684 * Returns the number of times this UID woke up the Application Processor to
685 * process a WiFi packet.
686 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
687 */
688 public abstract long getWifiRadioApWakeupCount(int which);
689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800691 /*
692 * FIXME: it's not correct to use this magic value because it
693 * could clash with a sensor handle (which are defined by
694 * the sensor HAL, and therefore out of our control
695 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 // Magic sensor number for the GPS.
697 public static final int GPS = -10000;
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 public abstract int getHandle();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 public abstract Timer getSensorTime();
Amith Yamasaniab9ad192016-12-06 12:46:59 -0800702
Bookatz867c0d72017-03-07 18:23:42 -0800703 /** Returns a Timer for sensor usage when app is in the background. */
704 public abstract Timer getSensorBackgroundTime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
706
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700707 public class Pid {
Dianne Hackborne5167ca2014-03-08 14:39:10 -0800708 public int mWakeNesting;
709 public long mWakeSumMs;
710 public long mWakeStartMs;
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700711 }
712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 /**
714 * The statistics associated with a particular process.
715 */
716 public static abstract class Proc {
717
Dianne Hackborn287952c2010-09-22 22:34:31 -0700718 public static class ExcessivePower {
719 public static final int TYPE_WAKE = 1;
720 public static final int TYPE_CPU = 2;
721
722 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700723 public long overTime;
724 public long usedTime;
725 }
726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800728 * Returns true if this process is still active in the battery stats.
729 */
730 public abstract boolean isActive();
731
732 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700733 * Returns the total time (in milliseconds) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700735 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 */
737 public abstract long getUserTime(int which);
738
739 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700740 * Returns the total time (in milliseconds) spent executing in system code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700742 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 */
744 public abstract long getSystemTime(int which);
745
746 /**
747 * Returns the number of times the process has been started.
748 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700749 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 */
751 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700752
753 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800754 * Returns the number of times the process has crashed.
755 *
756 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
757 */
758 public abstract int getNumCrashes(int which);
759
760 /**
761 * Returns the number of times the process has ANRed.
762 *
763 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
764 */
765 public abstract int getNumAnrs(int which);
766
767 /**
Adam Lesinski33dac552015-03-09 15:24:48 -0700768 * Returns the cpu time (milliseconds) spent while the process was in the foreground.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700769 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700770 * @return foreground cpu time in microseconds
771 */
772 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700773
Dianne Hackborn287952c2010-09-22 22:34:31 -0700774 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700775
Dianne Hackborn287952c2010-09-22 22:34:31 -0700776 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 }
778
779 /**
780 * The statistics associated with a particular package.
781 */
782 public static abstract class Pkg {
783
784 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700785 * Returns information about all wakeup alarms that have been triggered for this
786 * package. The mapping keys are tag names for the alarms, the counter contains
787 * the number of times the alarm was triggered while on battery.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700789 public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790
791 /**
792 * Returns a mapping containing service statistics.
793 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -0700794 public abstract ArrayMap<String, ? extends Serv> getServiceStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795
796 /**
797 * The statistics associated with a particular service.
798 */
Joe Onoratoabded112016-02-08 16:49:39 -0800799 public static abstract class Serv {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800
801 /**
802 * Returns the amount of time spent started.
803 *
804 * @param batteryUptime elapsed uptime on battery in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700805 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 * @return
807 */
808 public abstract long getStartTime(long batteryUptime, int which);
809
810 /**
811 * Returns the total number of times startService() has been called.
812 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700813 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 */
815 public abstract int getStarts(int which);
816
817 /**
818 * Returns the total number times the service has been launched.
819 *
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700820 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 */
822 public abstract int getLaunches(int which);
823 }
824 }
825 }
826
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800827 public static final class LevelStepTracker {
828 public long mLastStepTime = -1;
829 public int mNumStepDurations;
830 public final long[] mStepDurations;
831
832 public LevelStepTracker(int maxLevelSteps) {
833 mStepDurations = new long[maxLevelSteps];
834 }
835
836 public LevelStepTracker(int numSteps, long[] steps) {
837 mNumStepDurations = numSteps;
838 mStepDurations = new long[numSteps];
839 System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
840 }
841
842 public long getDurationAt(int index) {
843 return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
844 }
845
846 public int getLevelAt(int index) {
847 return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
848 >> STEP_LEVEL_LEVEL_SHIFT);
849 }
850
851 public int getInitModeAt(int index) {
852 return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
853 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
854 }
855
856 public int getModModeAt(int index) {
857 return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
858 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
859 }
860
861 private void appendHex(long val, int topOffset, StringBuilder out) {
862 boolean hasData = false;
863 while (topOffset >= 0) {
864 int digit = (int)( (val>>topOffset) & 0xf );
865 topOffset -= 4;
866 if (!hasData && digit == 0) {
867 continue;
868 }
869 hasData = true;
870 if (digit >= 0 && digit <= 9) {
871 out.append((char)('0' + digit));
872 } else {
873 out.append((char)('a' + digit - 10));
874 }
875 }
876 }
877
878 public void encodeEntryAt(int index, StringBuilder out) {
879 long item = mStepDurations[index];
880 long duration = item & STEP_LEVEL_TIME_MASK;
881 int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
882 >> STEP_LEVEL_LEVEL_SHIFT);
883 int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
884 >> STEP_LEVEL_INITIAL_MODE_SHIFT);
885 int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
886 >> STEP_LEVEL_MODIFIED_MODE_SHIFT);
887 switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
888 case Display.STATE_OFF: out.append('f'); break;
889 case Display.STATE_ON: out.append('o'); break;
890 case Display.STATE_DOZE: out.append('d'); break;
891 case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
892 }
893 if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
894 out.append('p');
895 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700896 if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
897 out.append('i');
898 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800899 switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
900 case Display.STATE_OFF: out.append('F'); break;
901 case Display.STATE_ON: out.append('O'); break;
902 case Display.STATE_DOZE: out.append('D'); break;
903 case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
904 }
905 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
906 out.append('P');
907 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700908 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
909 out.append('I');
910 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800911 out.append('-');
912 appendHex(level, 4, out);
913 out.append('-');
914 appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
915 }
916
917 public void decodeEntryAt(int index, String value) {
918 final int N = value.length();
919 int i = 0;
920 char c;
921 long out = 0;
922 while (i < N && (c=value.charAt(i)) != '-') {
923 i++;
924 switch (c) {
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800925 case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800926 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800927 case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800928 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800929 case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800930 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800931 case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
932 << STEP_LEVEL_INITIAL_MODE_SHIFT);
933 break;
934 case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
935 << STEP_LEVEL_INITIAL_MODE_SHIFT);
936 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700937 case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
938 << STEP_LEVEL_INITIAL_MODE_SHIFT);
939 break;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800940 case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
941 break;
942 case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
943 break;
944 case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
945 break;
946 case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
947 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
948 break;
949 case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
950 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800951 break;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700952 case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
953 << STEP_LEVEL_MODIFIED_MODE_SHIFT);
954 break;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800955 }
956 }
957 i++;
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800958 long level = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800959 while (i < N && (c=value.charAt(i)) != '-') {
960 i++;
961 level <<= 4;
962 if (c >= '0' && c <= '9') {
963 level += c - '0';
964 } else if (c >= 'a' && c <= 'f') {
965 level += c - 'a' + 10;
966 } else if (c >= 'A' && c <= 'F') {
967 level += c - 'A' + 10;
968 }
969 }
Dianne Hackborn8cfb58b2015-03-04 13:28:36 -0800970 i++;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800971 out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
972 long duration = 0;
973 while (i < N && (c=value.charAt(i)) != '-') {
974 i++;
975 duration <<= 4;
976 if (c >= '0' && c <= '9') {
977 duration += c - '0';
978 } else if (c >= 'a' && c <= 'f') {
979 duration += c - 'a' + 10;
980 } else if (c >= 'A' && c <= 'F') {
981 duration += c - 'A' + 10;
982 }
983 }
984 mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
985 }
986
987 public void init() {
988 mLastStepTime = -1;
989 mNumStepDurations = 0;
990 }
991
992 public void clearTime() {
993 mLastStepTime = -1;
994 }
995
996 public long computeTimePerLevel() {
997 final long[] steps = mStepDurations;
998 final int numSteps = mNumStepDurations;
999
1000 // For now we'll do a simple average across all steps.
1001 if (numSteps <= 0) {
1002 return -1;
1003 }
1004 long total = 0;
1005 for (int i=0; i<numSteps; i++) {
1006 total += steps[i] & STEP_LEVEL_TIME_MASK;
1007 }
1008 return total / numSteps;
1009 /*
1010 long[] buckets = new long[numSteps];
1011 int numBuckets = 0;
1012 int numToAverage = 4;
1013 int i = 0;
1014 while (i < numSteps) {
1015 long totalTime = 0;
1016 int num = 0;
1017 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
1018 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
1019 num++;
1020 }
1021 buckets[numBuckets] = totalTime / num;
1022 numBuckets++;
1023 numToAverage *= 2;
1024 i += num;
1025 }
1026 if (numBuckets < 1) {
1027 return -1;
1028 }
1029 long averageTime = buckets[numBuckets-1];
1030 for (i=numBuckets-2; i>=0; i--) {
1031 averageTime = (averageTime + buckets[i]) / 2;
1032 }
1033 return averageTime;
1034 */
1035 }
1036
1037 public long computeTimeEstimate(long modesOfInterest, long modeValues,
1038 int[] outNumOfInterest) {
1039 final long[] steps = mStepDurations;
1040 final int count = mNumStepDurations;
1041 if (count <= 0) {
1042 return -1;
1043 }
1044 long total = 0;
1045 int numOfInterest = 0;
1046 for (int i=0; i<count; i++) {
1047 long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
1048 >> STEP_LEVEL_INITIAL_MODE_SHIFT;
1049 long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
1050 >> STEP_LEVEL_MODIFIED_MODE_SHIFT;
1051 // If the modes of interest didn't change during this step period...
1052 if ((modMode&modesOfInterest) == 0) {
1053 // And the mode values during this period match those we are measuring...
1054 if ((initMode&modesOfInterest) == modeValues) {
1055 // Then this can be used to estimate the total time!
1056 numOfInterest++;
1057 total += steps[i] & STEP_LEVEL_TIME_MASK;
1058 }
1059 }
1060 }
1061 if (numOfInterest <= 0) {
1062 return -1;
1063 }
1064
1065 if (outNumOfInterest != null) {
1066 outNumOfInterest[0] = numOfInterest;
1067 }
1068
1069 // The estimated time is the average time we spend in each level, multipled
1070 // by 100 -- the total number of battery levels
1071 return (total / numOfInterest) * 100;
1072 }
1073
1074 public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
1075 int stepCount = mNumStepDurations;
1076 final long lastStepTime = mLastStepTime;
1077 if (lastStepTime >= 0 && numStepLevels > 0) {
1078 final long[] steps = mStepDurations;
1079 long duration = elapsedRealtime - lastStepTime;
1080 for (int i=0; i<numStepLevels; i++) {
1081 System.arraycopy(steps, 0, steps, 1, steps.length-1);
1082 long thisDuration = duration / (numStepLevels-i);
1083 duration -= thisDuration;
1084 if (thisDuration > STEP_LEVEL_TIME_MASK) {
1085 thisDuration = STEP_LEVEL_TIME_MASK;
1086 }
1087 steps[0] = thisDuration | modeBits;
1088 }
1089 stepCount += numStepLevels;
1090 if (stepCount > steps.length) {
1091 stepCount = steps.length;
1092 }
1093 }
1094 mNumStepDurations = stepCount;
1095 mLastStepTime = elapsedRealtime;
1096 }
1097
1098 public void readFromParcel(Parcel in) {
1099 final int N = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07001100 if (N > mStepDurations.length) {
1101 throw new ParcelFormatException("more step durations than available: " + N);
1102 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001103 mNumStepDurations = N;
1104 for (int i=0; i<N; i++) {
1105 mStepDurations[i] = in.readLong();
1106 }
1107 }
1108
1109 public void writeToParcel(Parcel out) {
1110 final int N = mNumStepDurations;
1111 out.writeInt(N);
1112 for (int i=0; i<N; i++) {
1113 out.writeLong(mStepDurations[i]);
1114 }
1115 }
1116 }
1117
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001118 public static final class PackageChange {
1119 public String mPackageName;
1120 public boolean mUpdate;
1121 public int mVersionCode;
1122 }
1123
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001124 public static final class DailyItem {
1125 public long mStartTime;
1126 public long mEndTime;
1127 public LevelStepTracker mDischargeSteps;
1128 public LevelStepTracker mChargeSteps;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001129 public ArrayList<PackageChange> mPackageChanges;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08001130 }
1131
1132 public abstract DailyItem getDailyItemLocked(int daysAgo);
1133
1134 public abstract long getCurrentDailyStartTime();
1135
1136 public abstract long getNextMinDailyDeadline();
1137
1138 public abstract long getNextMaxDailyDeadline();
1139
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001140 public abstract long[] getCpuFreqs();
1141
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001142 public final static class HistoryTag {
1143 public String string;
1144 public int uid;
1145
1146 public int poolIdx;
1147
1148 public void setTo(HistoryTag o) {
1149 string = o.string;
1150 uid = o.uid;
1151 poolIdx = o.poolIdx;
1152 }
1153
1154 public void setTo(String _string, int _uid) {
1155 string = _string;
1156 uid = _uid;
1157 poolIdx = -1;
1158 }
1159
1160 public void writeToParcel(Parcel dest, int flags) {
1161 dest.writeString(string);
1162 dest.writeInt(uid);
1163 }
1164
1165 public void readFromParcel(Parcel src) {
1166 string = src.readString();
1167 uid = src.readInt();
1168 poolIdx = -1;
1169 }
1170
1171 @Override
1172 public boolean equals(Object o) {
1173 if (this == o) return true;
1174 if (o == null || getClass() != o.getClass()) return false;
1175
1176 HistoryTag that = (HistoryTag) o;
1177
1178 if (uid != that.uid) return false;
1179 if (!string.equals(that.string)) return false;
1180
1181 return true;
1182 }
1183
1184 @Override
1185 public int hashCode() {
1186 int result = string.hashCode();
1187 result = 31 * result + uid;
1188 return result;
1189 }
1190 }
1191
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001192 /**
1193 * Optional detailed information that can go into a history step. This is typically
1194 * generated each time the battery level changes.
1195 */
1196 public final static class HistoryStepDetails {
1197 // Time (in 1/100 second) spent in user space and the kernel since the last step.
1198 public int userTime;
1199 public int systemTime;
1200
1201 // Top three apps using CPU in the last step, with times in 1/100 second.
1202 public int appCpuUid1;
1203 public int appCpuUTime1;
1204 public int appCpuSTime1;
1205 public int appCpuUid2;
1206 public int appCpuUTime2;
1207 public int appCpuSTime2;
1208 public int appCpuUid3;
1209 public int appCpuUTime3;
1210 public int appCpuSTime3;
1211
1212 // Information from /proc/stat
1213 public int statUserTime;
1214 public int statSystemTime;
1215 public int statIOWaitTime;
1216 public int statIrqTime;
1217 public int statSoftIrqTime;
1218 public int statIdlTime;
1219
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001220 // Platform-level low power state stats
1221 public String statPlatformIdleState;
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001222 public String statSubsystemPowerState;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001223
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001224 public HistoryStepDetails() {
1225 clear();
1226 }
1227
1228 public void clear() {
1229 userTime = systemTime = 0;
1230 appCpuUid1 = appCpuUid2 = appCpuUid3 = -1;
1231 appCpuUTime1 = appCpuSTime1 = appCpuUTime2 = appCpuSTime2
1232 = appCpuUTime3 = appCpuSTime3 = 0;
1233 }
1234
1235 public void writeToParcel(Parcel out) {
1236 out.writeInt(userTime);
1237 out.writeInt(systemTime);
1238 out.writeInt(appCpuUid1);
1239 out.writeInt(appCpuUTime1);
1240 out.writeInt(appCpuSTime1);
1241 out.writeInt(appCpuUid2);
1242 out.writeInt(appCpuUTime2);
1243 out.writeInt(appCpuSTime2);
1244 out.writeInt(appCpuUid3);
1245 out.writeInt(appCpuUTime3);
1246 out.writeInt(appCpuSTime3);
1247 out.writeInt(statUserTime);
1248 out.writeInt(statSystemTime);
1249 out.writeInt(statIOWaitTime);
1250 out.writeInt(statIrqTime);
1251 out.writeInt(statSoftIrqTime);
1252 out.writeInt(statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001253 out.writeString(statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001254 out.writeString(statSubsystemPowerState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001255 }
1256
1257 public void readFromParcel(Parcel in) {
1258 userTime = in.readInt();
1259 systemTime = in.readInt();
1260 appCpuUid1 = in.readInt();
1261 appCpuUTime1 = in.readInt();
1262 appCpuSTime1 = in.readInt();
1263 appCpuUid2 = in.readInt();
1264 appCpuUTime2 = in.readInt();
1265 appCpuSTime2 = in.readInt();
1266 appCpuUid3 = in.readInt();
1267 appCpuUTime3 = in.readInt();
1268 appCpuSTime3 = in.readInt();
1269 statUserTime = in.readInt();
1270 statSystemTime = in.readInt();
1271 statIOWaitTime = in.readInt();
1272 statIrqTime = in.readInt();
1273 statSoftIrqTime = in.readInt();
1274 statIdlTime = in.readInt();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07001275 statPlatformIdleState = in.readString();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00001276 statSubsystemPowerState = in.readString();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001277 }
1278 }
1279
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001280 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001281 public HistoryItem next;
Dianne Hackborn9a755432014-05-15 17:05:22 -07001282
1283 // The time of this event in milliseconds, as per SystemClock.elapsedRealtime().
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001284 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001285
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001286 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001287 public static final byte CMD_NULL = -1;
1288 public static final byte CMD_START = 4;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001289 public static final byte CMD_CURRENT_TIME = 5;
1290 public static final byte CMD_OVERFLOW = 6;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001291 public static final byte CMD_RESET = 7;
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08001292 public static final byte CMD_SHUTDOWN = 8;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001293
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001294 public byte cmd = CMD_NULL;
Bookatzc8c44962017-05-11 12:12:54 -07001295
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001296 /**
1297 * Return whether the command code is a delta data update.
1298 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001299 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001300 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001301 }
1302
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001303 public byte batteryLevel;
1304 public byte batteryStatus;
1305 public byte batteryHealth;
1306 public byte batteryPlugType;
Bookatzc8c44962017-05-11 12:12:54 -07001307
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09001308 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001309 public char batteryVoltage;
Adam Lesinski926969b2016-04-28 17:31:12 -07001310
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001311 // The charge of the battery in micro-Ampere-hours.
1312 public int batteryChargeUAh;
Bookatzc8c44962017-05-11 12:12:54 -07001313
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001314 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001315 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001316 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001317 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn3251b902014-06-20 14:40:53 -07001318 public static final int STATE_PHONE_SIGNAL_STRENGTH_SHIFT = 3;
1319 public static final int STATE_PHONE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001320 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001321 public static final int STATE_PHONE_STATE_SHIFT = 6;
1322 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001323 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001324 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001325 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001326
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001327 // These states always appear directly in the first int token
1328 // of a delta change; they should be ones that change relatively
1329 // frequently.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001330 public static final int STATE_CPU_RUNNING_FLAG = 1<<31;
1331 public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001332 public static final int STATE_GPS_ON_FLAG = 1<<29;
1333 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001334 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001335 public static final int STATE_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001336 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Adam Lesinski926969b2016-04-28 17:31:12 -07001337 // Do not use, this is used for coulomb delta count.
1338 private static final int STATE_RESERVED_0 = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001339 // These are on the lower bits used for the command; if they change
1340 // we need to write another int of data.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001341 public static final int STATE_SENSOR_ON_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001342 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001343 public static final int STATE_PHONE_SCANNING_FLAG = 1<<21;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001344 public static final int STATE_SCREEN_ON_FLAG = 1<<20; // consider moving to states2
1345 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
1346 // empty slot
1347 // empty slot
1348 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<16;
Dianne Hackborn40c87252014-03-19 16:55:40 -07001349
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001350 public static final int MOST_INTERESTING_STATES =
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001351 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG;
1352
1353 public static final int SETTLE_TO_ZERO_STATES = 0xffff0000 & ~MOST_INTERESTING_STATES;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07001354
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001355 public int states;
1356
Dianne Hackborn3251b902014-06-20 14:40:53 -07001357 // Constants from WIFI_SUPPL_STATE_*
1358 public static final int STATE2_WIFI_SUPPL_STATE_SHIFT = 0;
1359 public static final int STATE2_WIFI_SUPPL_STATE_MASK = 0xf;
1360 // Values for NUM_WIFI_SIGNAL_STRENGTH_BINS
1361 public static final int STATE2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
1362 public static final int STATE2_WIFI_SIGNAL_STRENGTH_MASK =
1363 0x7 << STATE2_WIFI_SIGNAL_STRENGTH_SHIFT;
1364
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001365 public static final int STATE2_POWER_SAVE_FLAG = 1<<31;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001366 public static final int STATE2_VIDEO_ON_FLAG = 1<<30;
1367 public static final int STATE2_WIFI_RUNNING_FLAG = 1<<29;
1368 public static final int STATE2_WIFI_ON_FLAG = 1<<28;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07001369 public static final int STATE2_FLASHLIGHT_FLAG = 1<<27;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001370 public static final int STATE2_DEVICE_IDLE_SHIFT = 25;
1371 public static final int STATE2_DEVICE_IDLE_MASK = 0x3 << STATE2_DEVICE_IDLE_SHIFT;
1372 public static final int STATE2_CHARGING_FLAG = 1<<24;
1373 public static final int STATE2_PHONE_IN_CALL_FLAG = 1<<23;
1374 public static final int STATE2_BLUETOOTH_ON_FLAG = 1<<22;
1375 public static final int STATE2_CAMERA_FLAG = 1<<21;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08001376 public static final int STATE2_BLUETOOTH_SCAN_FLAG = 1 << 20;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001377
1378 public static final int MOST_INTERESTING_STATES2 =
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001379 STATE2_POWER_SAVE_FLAG | STATE2_WIFI_ON_FLAG | STATE2_DEVICE_IDLE_MASK
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001380 | STATE2_CHARGING_FLAG | STATE2_PHONE_IN_CALL_FLAG | STATE2_BLUETOOTH_ON_FLAG;
1381
1382 public static final int SETTLE_TO_ZERO_STATES2 = 0xffff0000 & ~MOST_INTERESTING_STATES2;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001383
Dianne Hackborn40c87252014-03-19 16:55:40 -07001384 public int states2;
1385
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001386 // The wake lock that was acquired at this point.
1387 public HistoryTag wakelockTag;
1388
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001389 // Kernel wakeup reason at this point.
1390 public HistoryTag wakeReasonTag;
1391
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001392 // Non-null when there is more detailed information at this step.
1393 public HistoryStepDetails stepDetails;
1394
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001395 public static final int EVENT_FLAG_START = 0x8000;
1396 public static final int EVENT_FLAG_FINISH = 0x4000;
1397
1398 // No event in this item.
1399 public static final int EVENT_NONE = 0x0000;
1400 // Event is about a process that is running.
1401 public static final int EVENT_PROC = 0x0001;
1402 // Event is about an application package that is in the foreground.
1403 public static final int EVENT_FOREGROUND = 0x0002;
1404 // Event is about an application package that is at the top of the screen.
1405 public static final int EVENT_TOP = 0x0003;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001406 // Event is about active sync operations.
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001407 public static final int EVENT_SYNC = 0x0004;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001408 // Events for all additional wake locks aquired/release within a wake block.
1409 // These are not generated by default.
1410 public static final int EVENT_WAKE_LOCK = 0x0005;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001411 // Event is about an application executing a scheduled job.
1412 public static final int EVENT_JOB = 0x0006;
1413 // Events for users running.
1414 public static final int EVENT_USER_RUNNING = 0x0007;
1415 // Events for foreground user.
1416 public static final int EVENT_USER_FOREGROUND = 0x0008;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001417 // Event for connectivity changed.
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001418 public static final int EVENT_CONNECTIVITY_CHANGED = 0x0009;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001419 // Event for becoming active taking us out of idle mode.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001420 public static final int EVENT_ACTIVE = 0x000a;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001421 // Event for a package being installed.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001422 public static final int EVENT_PACKAGE_INSTALLED = 0x000b;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001423 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001424 public static final int EVENT_PACKAGE_UNINSTALLED = 0x000c;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001425 // Event for a package being uninstalled.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001426 public static final int EVENT_ALARM = 0x000d;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07001427 // Record that we have decided we need to collect new stats data.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001428 public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000e;
Amith Yamasani67768492015-06-09 12:23:58 -07001429 // Event for a package becoming inactive due to being unused for a period of time.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001430 public static final int EVENT_PACKAGE_INACTIVE = 0x000f;
Amith Yamasani67768492015-06-09 12:23:58 -07001431 // Event for a package becoming active due to an interaction.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001432 public static final int EVENT_PACKAGE_ACTIVE = 0x0010;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001433 // Event for a package being on the temporary whitelist.
1434 public static final int EVENT_TEMP_WHITELIST = 0x0011;
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001435 // Event for the screen waking up.
1436 public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
Adam Lesinski5f056f62016-07-14 16:56:08 -07001437 // Event for the UID that woke up the application processor.
1438 // Used for wakeups coming from WiFi, modem, etc.
1439 public static final int EVENT_WAKEUP_AP = 0x0013;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001440 // Event for reporting that a specific partial wake lock has been held for a long duration.
1441 public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
Amith Yamasani67768492015-06-09 12:23:58 -07001442
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001443 // Number of event types.
Adam Lesinski041d9172016-12-12 12:03:56 -08001444 public static final int EVENT_COUNT = 0x0016;
Dianne Hackborn37de0982014-05-09 09:32:18 -07001445 // Mask to extract out only the type part of the event.
1446 public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001447
1448 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
1449 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
1450 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
1451 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
1452 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
1453 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001454 public static final int EVENT_SYNC_START = EVENT_SYNC | EVENT_FLAG_START;
1455 public static final int EVENT_SYNC_FINISH = EVENT_SYNC | EVENT_FLAG_FINISH;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001456 public static final int EVENT_WAKE_LOCK_START = EVENT_WAKE_LOCK | EVENT_FLAG_START;
1457 public static final int EVENT_WAKE_LOCK_FINISH = EVENT_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001458 public static final int EVENT_JOB_START = EVENT_JOB | EVENT_FLAG_START;
1459 public static final int EVENT_JOB_FINISH = EVENT_JOB | EVENT_FLAG_FINISH;
1460 public static final int EVENT_USER_RUNNING_START = EVENT_USER_RUNNING | EVENT_FLAG_START;
1461 public static final int EVENT_USER_RUNNING_FINISH = EVENT_USER_RUNNING | EVENT_FLAG_FINISH;
1462 public static final int EVENT_USER_FOREGROUND_START =
1463 EVENT_USER_FOREGROUND | EVENT_FLAG_START;
1464 public static final int EVENT_USER_FOREGROUND_FINISH =
1465 EVENT_USER_FOREGROUND | EVENT_FLAG_FINISH;
Dianne Hackborn1e383822015-04-10 14:02:33 -07001466 public static final int EVENT_ALARM_START = EVENT_ALARM | EVENT_FLAG_START;
1467 public static final int EVENT_ALARM_FINISH = EVENT_ALARM | EVENT_FLAG_FINISH;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001468 public static final int EVENT_TEMP_WHITELIST_START =
1469 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
1470 public static final int EVENT_TEMP_WHITELIST_FINISH =
1471 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07001472 public static final int EVENT_LONG_WAKE_LOCK_START =
1473 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
1474 public static final int EVENT_LONG_WAKE_LOCK_FINISH =
1475 EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001476
1477 // For CMD_EVENT.
1478 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001479 public HistoryTag eventTag;
1480
Dianne Hackborn9a755432014-05-15 17:05:22 -07001481 // Only set for CMD_CURRENT_TIME or CMD_RESET, as per System.currentTimeMillis().
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001482 public long currentTime;
1483
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001484 // Meta-data when reading.
1485 public int numReadInts;
1486
1487 // Pre-allocated objects.
1488 public final HistoryTag localWakelockTag = new HistoryTag();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001489 public final HistoryTag localWakeReasonTag = new HistoryTag();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001490 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001491
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001492 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001493 }
Bookatzc8c44962017-05-11 12:12:54 -07001494
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001495 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001496 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001497 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001498 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001499 }
Bookatzc8c44962017-05-11 12:12:54 -07001500
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001501 public int describeContents() {
1502 return 0;
1503 }
1504
1505 public void writeToParcel(Parcel dest, int flags) {
1506 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001507 int bat = (((int)cmd)&0xff)
1508 | ((((int)batteryLevel)<<8)&0xff00)
1509 | ((((int)batteryStatus)<<16)&0xf0000)
1510 | ((((int)batteryHealth)<<20)&0xf00000)
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001511 | ((((int)batteryPlugType)<<24)&0xf000000)
1512 | (wakelockTag != null ? 0x10000000 : 0)
1513 | (wakeReasonTag != null ? 0x20000000 : 0)
1514 | (eventCode != EVENT_NONE ? 0x40000000 : 0);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001515 dest.writeInt(bat);
1516 bat = (((int)batteryTemperature)&0xffff)
1517 | ((((int)batteryVoltage)<<16)&0xffff0000);
1518 dest.writeInt(bat);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001519 dest.writeInt(batteryChargeUAh);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001520 dest.writeInt(states);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001521 dest.writeInt(states2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001522 if (wakelockTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001523 wakelockTag.writeToParcel(dest, flags);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001524 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001525 if (wakeReasonTag != null) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001526 wakeReasonTag.writeToParcel(dest, flags);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001527 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001528 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001529 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001530 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001531 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001532 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001533 dest.writeLong(currentTime);
1534 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001535 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001536
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001537 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001538 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001539 int bat = src.readInt();
1540 cmd = (byte)(bat&0xff);
1541 batteryLevel = (byte)((bat>>8)&0xff);
1542 batteryStatus = (byte)((bat>>16)&0xf);
1543 batteryHealth = (byte)((bat>>20)&0xf);
1544 batteryPlugType = (byte)((bat>>24)&0xf);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001545 int bat2 = src.readInt();
1546 batteryTemperature = (short)(bat2&0xffff);
1547 batteryVoltage = (char)((bat2>>16)&0xffff);
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001548 batteryChargeUAh = src.readInt();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001549 states = src.readInt();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001550 states2 = src.readInt();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001551 if ((bat&0x10000000) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001552 wakelockTag = localWakelockTag;
1553 wakelockTag.readFromParcel(src);
1554 } else {
1555 wakelockTag = null;
1556 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001557 if ((bat&0x20000000) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001558 wakeReasonTag = localWakeReasonTag;
1559 wakeReasonTag.readFromParcel(src);
1560 } else {
1561 wakeReasonTag = null;
1562 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001563 if ((bat&0x40000000) != 0) {
1564 eventCode = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001565 eventTag = localEventTag;
1566 eventTag.readFromParcel(src);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001567 } else {
1568 eventCode = EVENT_NONE;
1569 eventTag = null;
1570 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001571 if (cmd == CMD_CURRENT_TIME || cmd == CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001572 currentTime = src.readLong();
1573 } else {
1574 currentTime = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001575 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001576 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001577 }
1578
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001579 public void clear() {
1580 time = 0;
1581 cmd = CMD_NULL;
1582 batteryLevel = 0;
1583 batteryStatus = 0;
1584 batteryHealth = 0;
1585 batteryPlugType = 0;
1586 batteryTemperature = 0;
1587 batteryVoltage = 0;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001588 batteryChargeUAh = 0;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001589 states = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001590 states2 = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001591 wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001592 wakeReasonTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001593 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001594 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -07001595 }
Bookatzc8c44962017-05-11 12:12:54 -07001596
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001597 public void setTo(HistoryItem o) {
1598 time = o.time;
1599 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001600 setToCommon(o);
1601 }
1602
1603 public void setTo(long time, byte cmd, HistoryItem o) {
1604 this.time = time;
1605 this.cmd = cmd;
1606 setToCommon(o);
1607 }
1608
1609 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001610 batteryLevel = o.batteryLevel;
1611 batteryStatus = o.batteryStatus;
1612 batteryHealth = o.batteryHealth;
1613 batteryPlugType = o.batteryPlugType;
1614 batteryTemperature = o.batteryTemperature;
1615 batteryVoltage = o.batteryVoltage;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001616 batteryChargeUAh = o.batteryChargeUAh;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001617 states = o.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001618 states2 = o.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001619 if (o.wakelockTag != null) {
1620 wakelockTag = localWakelockTag;
1621 wakelockTag.setTo(o.wakelockTag);
1622 } else {
1623 wakelockTag = null;
1624 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001625 if (o.wakeReasonTag != null) {
1626 wakeReasonTag = localWakeReasonTag;
1627 wakeReasonTag.setTo(o.wakeReasonTag);
1628 } else {
1629 wakeReasonTag = null;
1630 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001631 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001632 if (o.eventTag != null) {
1633 eventTag = localEventTag;
1634 eventTag.setTo(o.eventTag);
1635 } else {
1636 eventTag = null;
1637 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001638 currentTime = o.currentTime;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001639 }
1640
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001641 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001642 return batteryLevel == o.batteryLevel
1643 && batteryStatus == o.batteryStatus
1644 && batteryHealth == o.batteryHealth
1645 && batteryPlugType == o.batteryPlugType
1646 && batteryTemperature == o.batteryTemperature
1647 && batteryVoltage == o.batteryVoltage
Adam Lesinskia8018ac2016-05-03 10:18:10 -07001648 && batteryChargeUAh == o.batteryChargeUAh
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001649 && states == o.states
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001650 && states2 == o.states2
Dianne Hackborne5167ca2014-03-08 14:39:10 -08001651 && currentTime == o.currentTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001652 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001653
1654 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001655 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001656 return false;
1657 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001658 if (wakelockTag != o.wakelockTag) {
1659 if (wakelockTag == null || o.wakelockTag == null) {
1660 return false;
1661 }
1662 if (!wakelockTag.equals(o.wakelockTag)) {
1663 return false;
1664 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001665 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001666 if (wakeReasonTag != o.wakeReasonTag) {
1667 if (wakeReasonTag == null || o.wakeReasonTag == null) {
1668 return false;
1669 }
1670 if (!wakeReasonTag.equals(o.wakeReasonTag)) {
1671 return false;
1672 }
1673 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001674 if (eventTag != o.eventTag) {
1675 if (eventTag == null || o.eventTag == null) {
1676 return false;
1677 }
1678 if (!eventTag.equals(o.eventTag)) {
1679 return false;
1680 }
1681 }
1682 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001683 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001684 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001685
1686 public final static class HistoryEventTracker {
1687 private final HashMap<String, SparseIntArray>[] mActiveEvents
1688 = (HashMap<String, SparseIntArray>[]) new HashMap[HistoryItem.EVENT_COUNT];
1689
1690 public boolean updateState(int code, String name, int uid, int poolIdx) {
1691 if ((code&HistoryItem.EVENT_FLAG_START) != 0) {
1692 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1693 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1694 if (active == null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07001695 active = new HashMap<>();
Dianne Hackborn37de0982014-05-09 09:32:18 -07001696 mActiveEvents[idx] = active;
1697 }
1698 SparseIntArray uids = active.get(name);
1699 if (uids == null) {
1700 uids = new SparseIntArray();
1701 active.put(name, uids);
1702 }
1703 if (uids.indexOfKey(uid) >= 0) {
1704 // Already set, nothing to do!
1705 return false;
1706 }
1707 uids.put(uid, poolIdx);
1708 } else if ((code&HistoryItem.EVENT_FLAG_FINISH) != 0) {
1709 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1710 HashMap<String, SparseIntArray> active = mActiveEvents[idx];
1711 if (active == null) {
1712 // not currently active, nothing to do.
1713 return false;
1714 }
1715 SparseIntArray uids = active.get(name);
1716 if (uids == null) {
1717 // not currently active, nothing to do.
1718 return false;
1719 }
1720 idx = uids.indexOfKey(uid);
1721 if (idx < 0) {
1722 // not currently active, nothing to do.
1723 return false;
1724 }
1725 uids.removeAt(idx);
1726 if (uids.size() <= 0) {
1727 active.remove(name);
1728 }
1729 }
1730 return true;
1731 }
1732
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001733 public void removeEvents(int code) {
1734 int idx = code&HistoryItem.EVENT_TYPE_MASK;
1735 mActiveEvents[idx] = null;
1736 }
1737
Dianne Hackborn37de0982014-05-09 09:32:18 -07001738 public HashMap<String, SparseIntArray> getStateForEvent(int code) {
1739 return mActiveEvents[code];
1740 }
1741 }
1742
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001743 public static final class BitDescription {
1744 public final int mask;
1745 public final int shift;
1746 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001747 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001748 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001749 public final String[] shortValues;
Bookatzc8c44962017-05-11 12:12:54 -07001750
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001751 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001752 this.mask = mask;
1753 this.shift = -1;
1754 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001755 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001756 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001757 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001758 }
Bookatzc8c44962017-05-11 12:12:54 -07001759
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001760 public BitDescription(int mask, int shift, String name, String shortName,
1761 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001762 this.mask = mask;
1763 this.shift = shift;
1764 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001765 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001766 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001767 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001768 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07001769 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07001770
Dianne Hackbornfc064132014-06-02 12:42:12 -07001771 /**
1772 * Don't allow any more batching in to the current history event. This
1773 * is called when printing partial histories, so to ensure that the next
1774 * history event will go in to a new batch after what was printed in the
1775 * last partial history.
1776 */
1777 public abstract void commitCurrentHistoryBatchLocked();
1778
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001779 public abstract int getHistoryTotalSize();
1780
1781 public abstract int getHistoryUsedSize();
1782
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001783 public abstract boolean startIteratingHistoryLocked();
1784
Dianne Hackborn099bc622014-01-22 13:39:16 -08001785 public abstract int getHistoryStringPoolSize();
1786
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001787 public abstract int getHistoryStringPoolBytes();
1788
1789 public abstract String getHistoryTagPoolString(int index);
1790
1791 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001792
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001793 public abstract boolean getNextHistoryLocked(HistoryItem out);
1794
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07001795 public abstract void finishIteratingHistoryLocked();
1796
1797 public abstract boolean startIteratingOldHistoryLocked();
1798
1799 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
1800
1801 public abstract void finishIteratingOldHistoryLocked();
1802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001804 * Return the base time offset for the battery history.
1805 */
1806 public abstract long getHistoryBaseTime();
Bookatzc8c44962017-05-11 12:12:54 -07001807
Dianne Hackbornb5e31652010-09-07 12:13:55 -07001808 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 * Returns the number of times the device has been started.
1810 */
1811 public abstract int getStartCount();
Bookatzc8c44962017-05-11 12:12:54 -07001812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001814 * 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 -08001815 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 * {@hide}
1818 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001819 public abstract long getScreenOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001820
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001821 /**
1822 * Returns the number of times the screen was turned on.
1823 *
1824 * {@hide}
1825 */
1826 public abstract int getScreenOnCount(int which);
1827
Jeff Browne95c3cd2014-05-02 16:59:26 -07001828 public abstract long getInteractiveTime(long elapsedRealtimeUs, int which);
1829
Dianne Hackborn617f8772009-03-31 15:04:46 -07001830 public static final int SCREEN_BRIGHTNESS_DARK = 0;
1831 public static final int SCREEN_BRIGHTNESS_DIM = 1;
1832 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
1833 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
1834 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
Bookatzc8c44962017-05-11 12:12:54 -07001835
Dianne Hackborn617f8772009-03-31 15:04:46 -07001836 static final String[] SCREEN_BRIGHTNESS_NAMES = {
1837 "dark", "dim", "medium", "light", "bright"
1838 };
Bookatzc8c44962017-05-11 12:12:54 -07001839
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001840 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
1841 "0", "1", "2", "3", "4"
1842 };
1843
Dianne Hackborn617f8772009-03-31 15:04:46 -07001844 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
Dianne Hackborn3251b902014-06-20 14:40:53 -07001845
Dianne Hackborn617f8772009-03-31 15:04:46 -07001846 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001847 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -07001848 * the given brightness
Bookatzc8c44962017-05-11 12:12:54 -07001849 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07001850 * {@hide}
1851 */
1852 public abstract long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001853 long elapsedRealtimeUs, int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001856 * Returns the time in microseconds that power save mode has been enabled while the device was
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001857 * running on battery.
1858 *
1859 * {@hide}
1860 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001861 public abstract long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001862
1863 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001864 * Returns the number of times that power save mode was enabled.
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001865 *
1866 * {@hide}
1867 */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001868 public abstract int getPowerSaveModeEnabledCount(int which);
1869
1870 /**
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001871 * Constant for device idle mode: not active.
1872 */
1873 public static final int DEVICE_IDLE_MODE_OFF = 0;
1874
1875 /**
1876 * Constant for device idle mode: active in lightweight mode.
1877 */
1878 public static final int DEVICE_IDLE_MODE_LIGHT = 1;
1879
1880 /**
1881 * Constant for device idle mode: active in full mode.
1882 */
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001883 public static final int DEVICE_IDLE_MODE_DEEP = 2;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001884
1885 /**
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001886 * Returns the time in microseconds that device has been in idle mode while
1887 * running on battery.
1888 *
1889 * {@hide}
1890 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001891 public abstract long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07001892
1893 /**
1894 * Returns the number of times that the devie has gone in to idle mode.
1895 *
1896 * {@hide}
1897 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001898 public abstract int getDeviceIdleModeCount(int mode, int which);
1899
1900 /**
1901 * Return the longest duration we spent in a particular device idle mode (fully in the
1902 * mode, not in idle maintenance etc).
1903 */
1904 public abstract long getLongestDeviceIdleModeTime(int mode);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07001905
1906 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001907 * Returns the time in microseconds that device has been in idling while on
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001908 * battery. This is broader than {@link #getDeviceIdleModeTime} -- it
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001909 * counts all of the time that we consider the device to be idle, whether or not
1910 * it is currently in the actual device idle mode.
1911 *
1912 * {@hide}
1913 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001914 public abstract long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001915
1916 /**
1917 * Returns the number of times that the devie has started idling.
1918 *
1919 * {@hide}
1920 */
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001921 public abstract int getDeviceIdlingCount(int mode, int which);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001922
1923 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08001924 * Returns the number of times that connectivity state changed.
1925 *
1926 * {@hide}
1927 */
1928 public abstract int getNumConnectivityChange(int which);
1929
1930 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001931 * 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 -08001932 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07001933 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 * {@hide}
1935 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001936 public abstract long getPhoneOnTime(long elapsedRealtimeUs, int which);
Bookatzc8c44962017-05-11 12:12:54 -07001937
Dianne Hackborn627bba72009-03-24 22:32:56 -07001938 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001939 * Returns the number of times a phone call was activated.
1940 *
1941 * {@hide}
1942 */
1943 public abstract int getPhoneOnCount(int which);
1944
1945 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001946 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07001947 * the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07001948 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07001949 * {@hide}
1950 */
1951 public abstract long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001952 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001953
Dianne Hackborn617f8772009-03-31 15:04:46 -07001954 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -07001955 * Returns the time in microseconds that the phone has been trying to
1956 * acquire a signal.
1957 *
1958 * {@hide}
1959 */
1960 public abstract long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001961 long elapsedRealtimeUs, int which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001962
1963 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07001964 * Returns the number of times the phone has entered the given signal strength.
Bookatzc8c44962017-05-11 12:12:54 -07001965 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07001966 * {@hide}
1967 */
1968 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
1969
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001970 /**
1971 * Returns the time in microseconds that the mobile network has been active
1972 * (in a high power state).
1973 *
1974 * {@hide}
1975 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001976 public abstract long getMobileRadioActiveTime(long elapsedRealtimeUs, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001977
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001978 /**
1979 * Returns the number of times that the mobile network has transitioned to the
1980 * active state.
1981 *
1982 * {@hide}
1983 */
1984 public abstract int getMobileRadioActiveCount(int which);
1985
1986 /**
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001987 * Returns the time in microseconds that is the difference between the mobile radio
1988 * time we saw based on the elapsed timestamp when going down vs. the given time stamp
1989 * from the radio.
1990 *
1991 * {@hide}
1992 */
1993 public abstract long getMobileRadioActiveAdjustedTime(int which);
1994
1995 /**
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001996 * Returns the time in microseconds that the mobile network has been active
1997 * (in a high power state) but not being able to blame on an app.
1998 *
1999 * {@hide}
2000 */
2001 public abstract long getMobileRadioActiveUnknownTime(int which);
2002
2003 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002004 * Return count of number of times radio was up that could not be blamed on apps.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002005 *
2006 * {@hide}
2007 */
2008 public abstract int getMobileRadioActiveUnknownCount(int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002009
Dianne Hackborn627bba72009-03-24 22:32:56 -07002010 public static final int DATA_CONNECTION_NONE = 0;
2011 public static final int DATA_CONNECTION_GPRS = 1;
2012 public static final int DATA_CONNECTION_EDGE = 2;
2013 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002014 public static final int DATA_CONNECTION_CDMA = 4;
2015 public static final int DATA_CONNECTION_EVDO_0 = 5;
2016 public static final int DATA_CONNECTION_EVDO_A = 6;
2017 public static final int DATA_CONNECTION_1xRTT = 7;
2018 public static final int DATA_CONNECTION_HSDPA = 8;
2019 public static final int DATA_CONNECTION_HSUPA = 9;
2020 public static final int DATA_CONNECTION_HSPA = 10;
2021 public static final int DATA_CONNECTION_IDEN = 11;
2022 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002023 public static final int DATA_CONNECTION_LTE = 13;
2024 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -08002025 public static final int DATA_CONNECTION_HSPAP = 15;
2026 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -07002027
Dianne Hackborn627bba72009-03-24 22:32:56 -07002028 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002029 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -07002030 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -08002031 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -07002032 };
Bookatzc8c44962017-05-11 12:12:54 -07002033
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002034 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Bookatzc8c44962017-05-11 12:12:54 -07002035
Dianne Hackborn627bba72009-03-24 22:32:56 -07002036 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002037 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -07002038 * the given data connection.
Bookatzc8c44962017-05-11 12:12:54 -07002039 *
Dianne Hackborn627bba72009-03-24 22:32:56 -07002040 * {@hide}
2041 */
2042 public abstract long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002043 long elapsedRealtimeUs, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07002044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -07002046 * Returns the number of times the phone has entered the given data
2047 * connection type.
Bookatzc8c44962017-05-11 12:12:54 -07002048 *
Dianne Hackborn617f8772009-03-31 15:04:46 -07002049 * {@hide}
2050 */
2051 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002052
Dianne Hackborn3251b902014-06-20 14:40:53 -07002053 public static final int WIFI_SUPPL_STATE_INVALID = 0;
2054 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1;
2055 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2;
2056 public static final int WIFI_SUPPL_STATE_INACTIVE = 3;
2057 public static final int WIFI_SUPPL_STATE_SCANNING = 4;
2058 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5;
2059 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6;
2060 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7;
2061 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8;
2062 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9;
2063 public static final int WIFI_SUPPL_STATE_COMPLETED = 10;
2064 public static final int WIFI_SUPPL_STATE_DORMANT = 11;
2065 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12;
2066
2067 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED+1;
2068
2069 static final String[] WIFI_SUPPL_STATE_NAMES = {
2070 "invalid", "disconn", "disabled", "inactive", "scanning",
2071 "authenticating", "associating", "associated", "4-way-handshake",
2072 "group-handshake", "completed", "dormant", "uninit"
2073 };
2074
2075 static final String[] WIFI_SUPPL_STATE_SHORT_NAMES = {
2076 "inv", "dsc", "dis", "inact", "scan",
2077 "auth", "ascing", "asced", "4-way",
2078 "group", "compl", "dorm", "uninit"
2079 };
2080
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002081 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
2082 = new BitDescription[] {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002083 new BitDescription(HistoryItem.STATE_CPU_RUNNING_FLAG, "running", "r"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002084 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
2085 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002086 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002087 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
2088 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
2089 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002090 new BitDescription(HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG, "wifi_radio", "Wr"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002091 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002092 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002093 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002094 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
2095 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002096 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
2097 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
2098 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
2099 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
2100 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
2101 new String[] {"in", "out", "emergency", "off"},
2102 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002103 new BitDescription(HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK,
2104 HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT, "phone_signal_strength", "Pss",
2105 SignalStrength.SIGNAL_STRENGTH_NAMES,
2106 new String[] { "0", "1", "2", "3", "4" }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002107 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
2108 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
2109 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002110 };
Dianne Hackborn617f8772009-03-31 15:04:46 -07002111
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002112 public static final BitDescription[] HISTORY_STATE2_DESCRIPTIONS
2113 = new BitDescription[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002114 new BitDescription(HistoryItem.STATE2_POWER_SAVE_FLAG, "power_save", "ps"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002115 new BitDescription(HistoryItem.STATE2_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002116 new BitDescription(HistoryItem.STATE2_WIFI_RUNNING_FLAG, "wifi_running", "Ww"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002117 new BitDescription(HistoryItem.STATE2_WIFI_ON_FLAG, "wifi", "W"),
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002118 new BitDescription(HistoryItem.STATE2_FLASHLIGHT_FLAG, "flashlight", "fl"),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002119 new BitDescription(HistoryItem.STATE2_DEVICE_IDLE_MASK,
2120 HistoryItem.STATE2_DEVICE_IDLE_SHIFT, "device_idle", "di",
2121 new String[] { "off", "light", "full", "???" },
2122 new String[] { "off", "light", "full", "???" }),
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002123 new BitDescription(HistoryItem.STATE2_CHARGING_FLAG, "charging", "ch"),
2124 new BitDescription(HistoryItem.STATE2_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
2125 new BitDescription(HistoryItem.STATE2_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
Dianne Hackborn3251b902014-06-20 14:40:53 -07002126 new BitDescription(HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK,
2127 HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT, "wifi_signal_strength", "Wss",
2128 new String[] { "0", "1", "2", "3", "4" },
2129 new String[] { "0", "1", "2", "3", "4" }),
2130 new BitDescription(HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK,
2131 HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT, "wifi_suppl", "Wsp",
2132 WIFI_SUPPL_STATE_NAMES, WIFI_SUPPL_STATE_SHORT_NAMES),
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002133 new BitDescription(HistoryItem.STATE2_CAMERA_FLAG, "camera", "ca"),
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002134 new BitDescription(HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG, "ble_scan", "bles"),
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002135 };
2136
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002137 public static final String[] HISTORY_EVENT_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002138 "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
Kweku Adams134c59b2017-03-08 16:48:01 -08002139 "active", "pkginst", "pkgunin", "alarm", "stats", "pkginactive", "pkgactive",
2140 "tmpwhitelist", "screenwake", "wakeupap", "longwake", "est_capacity"
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002141 };
2142
2143 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07002144 "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07002145 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
Adam Lesinski041d9172016-12-12 12:03:56 -08002146 "Esw", "Ewa", "Elw", "Eec"
2147 };
2148
2149 @FunctionalInterface
2150 public interface IntToString {
2151 String applyAsString(int val);
2152 }
2153
2154 private static final IntToString sUidToString = UserHandle::formatUid;
2155 private static final IntToString sIntToString = Integer::toString;
2156
2157 public static final IntToString[] HISTORY_EVENT_INT_FORMATTERS = new IntToString[] {
2158 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2159 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2160 sUidToString, sUidToString, sUidToString, sUidToString, sUidToString, sUidToString,
2161 sUidToString, sUidToString, sUidToString, sIntToString
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002162 };
2163
Dianne Hackborn617f8772009-03-31 15:04:46 -07002164 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002165 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07002166 * running on battery.
Bookatzc8c44962017-05-11 12:12:54 -07002167 *
The Android Open Source Project10592532009-03-18 17:39:46 -07002168 * {@hide}
2169 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002170 public abstract long getWifiOnTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002171
2172 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002173 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002174 * been in the running state while the device was running on battery.
2175 *
2176 * {@hide}
2177 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002178 public abstract long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07002179
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002180 public static final int WIFI_STATE_OFF = 0;
2181 public static final int WIFI_STATE_OFF_SCANNING = 1;
2182 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
2183 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
2184 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
2185 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
2186 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
2187 public static final int WIFI_STATE_SOFT_AP = 7;
2188
2189 static final String[] WIFI_STATE_NAMES = {
2190 "off", "scanning", "no_net", "disconn",
2191 "sta", "p2p", "sta_p2p", "soft_ap"
2192 };
2193
2194 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
2195
2196 /**
2197 * Returns the time in microseconds that WiFi has been running in the given state.
2198 *
2199 * {@hide}
2200 */
2201 public abstract long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002202 long elapsedRealtimeUs, int which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002203
2204 /**
2205 * Returns the number of times that WiFi has entered the given state.
2206 *
2207 * {@hide}
2208 */
2209 public abstract int getWifiStateCount(int wifiState, int which);
2210
The Android Open Source Project10592532009-03-18 17:39:46 -07002211 /**
Dianne Hackborn3251b902014-06-20 14:40:53 -07002212 * Returns the time in microseconds that the wifi supplicant has been
2213 * in a given state.
2214 *
2215 * {@hide}
2216 */
2217 public abstract long getWifiSupplStateTime(int state, long elapsedRealtimeUs, int which);
2218
2219 /**
2220 * Returns the number of times that the wifi supplicant has transitioned
2221 * to a given state.
2222 *
2223 * {@hide}
2224 */
2225 public abstract int getWifiSupplStateCount(int state, int which);
2226
2227 public static final int NUM_WIFI_SIGNAL_STRENGTH_BINS = 5;
2228
2229 /**
2230 * Returns the time in microseconds that WIFI has been running with
2231 * the given signal strength.
2232 *
2233 * {@hide}
2234 */
2235 public abstract long getWifiSignalStrengthTime(int strengthBin,
2236 long elapsedRealtimeUs, int which);
2237
2238 /**
2239 * Returns the number of times WIFI has entered the given signal strength.
2240 *
2241 * {@hide}
2242 */
2243 public abstract int getWifiSignalStrengthCount(int strengthBin, int which);
2244
2245 /**
Dianne Hackbornabc7c492014-06-30 16:57:46 -07002246 * Returns the time in microseconds that the flashlight has been on while the device was
2247 * running on battery.
2248 *
2249 * {@hide}
2250 */
2251 public abstract long getFlashlightOnTime(long elapsedRealtimeUs, int which);
2252
2253 /**
2254 * Returns the number of times that the flashlight has been turned on while the device was
2255 * running on battery.
2256 *
2257 * {@hide}
2258 */
2259 public abstract long getFlashlightOnCount(int which);
2260
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002261 /**
2262 * Returns the time in microseconds that the camera has been on while the device was
2263 * running on battery.
2264 *
2265 * {@hide}
2266 */
2267 public abstract long getCameraOnTime(long elapsedRealtimeUs, int which);
2268
Adam Lesinski9f55cc72016-01-27 20:42:14 -08002269 /**
2270 * Returns the time in microseconds that bluetooth scans were running while the device was
2271 * on battery.
2272 *
2273 * {@hide}
2274 */
2275 public abstract long getBluetoothScanTime(long elapsedRealtimeUs, int which);
Ruben Brunk5b1308f2015-06-03 18:49:27 -07002276
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002277 public static final int NETWORK_MOBILE_RX_DATA = 0;
2278 public static final int NETWORK_MOBILE_TX_DATA = 1;
2279 public static final int NETWORK_WIFI_RX_DATA = 2;
2280 public static final int NETWORK_WIFI_TX_DATA = 3;
Adam Lesinski50e47602015-12-04 17:04:54 -08002281 public static final int NETWORK_BT_RX_DATA = 4;
2282 public static final int NETWORK_BT_TX_DATA = 5;
Amith Yamasani59fe8412017-03-03 16:28:52 -08002283 public static final int NETWORK_MOBILE_BG_RX_DATA = 6;
2284 public static final int NETWORK_MOBILE_BG_TX_DATA = 7;
2285 public static final int NETWORK_WIFI_BG_RX_DATA = 8;
2286 public static final int NETWORK_WIFI_BG_TX_DATA = 9;
2287 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_BG_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002288
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002289 public abstract long getNetworkActivityBytes(int type, int which);
2290 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002291
Adam Lesinskie08af192015-03-25 16:42:59 -07002292 /**
Adam Lesinski17390762015-04-10 13:17:47 -07002293 * Returns true if the BatteryStats object has detailed WiFi power reports.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002294 * When true, calling {@link #getWifiControllerActivity()} will yield the
Adam Lesinski17390762015-04-10 13:17:47 -07002295 * actual power data.
2296 */
2297 public abstract boolean hasWifiActivityReporting();
2298
2299 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002300 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2301 * in various radio controller states, such as transmit, receive, and idle.
2302 * @return non-null {@link ControllerActivityCounter}
Adam Lesinskie08af192015-03-25 16:42:59 -07002303 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002304 public abstract ControllerActivityCounter getWifiControllerActivity();
2305
2306 /**
2307 * Returns true if the BatteryStats object has detailed bluetooth power reports.
2308 * When true, calling {@link #getBluetoothControllerActivity()} will yield the
2309 * actual power data.
2310 */
2311 public abstract boolean hasBluetoothActivityReporting();
2312
2313 /**
2314 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2315 * in various radio controller states, such as transmit, receive, and idle.
2316 * @return non-null {@link ControllerActivityCounter}
2317 */
2318 public abstract ControllerActivityCounter getBluetoothControllerActivity();
2319
2320 /**
2321 * Returns true if the BatteryStats object has detailed modem power reports.
2322 * When true, calling {@link #getModemControllerActivity()} will yield the
2323 * actual power data.
2324 */
2325 public abstract boolean hasModemActivityReporting();
2326
2327 /**
2328 * Returns a {@link ControllerActivityCounter} which is an aggregate of the times spent
2329 * in various radio controller states, such as transmit, receive, and idle.
2330 * @return non-null {@link ControllerActivityCounter}
2331 */
2332 public abstract ControllerActivityCounter getModemControllerActivity();
Adam Lesinski33dac552015-03-09 15:24:48 -07002333
The Android Open Source Project10592532009-03-18 17:39:46 -07002334 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08002335 * Return the wall clock time when battery stats data collection started.
2336 */
2337 public abstract long getStartClockTime();
2338
2339 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07002340 * Return platform version tag that we were running in when the battery stats started.
2341 */
2342 public abstract String getStartPlatformVersion();
2343
2344 /**
2345 * Return platform version tag that we were running in when the battery stats ended.
2346 */
2347 public abstract String getEndPlatformVersion();
2348
2349 /**
2350 * Return the internal version code of the parcelled format.
2351 */
2352 public abstract int getParcelVersion();
2353
2354 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 * Return whether we are currently running on battery.
2356 */
2357 public abstract boolean getIsOnBattery();
Bookatzc8c44962017-05-11 12:12:54 -07002358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 /**
2360 * Returns a SparseArray containing the statistics for each uid.
2361 */
2362 public abstract SparseArray<? extends Uid> getUidStats();
2363
2364 /**
2365 * Returns the current battery uptime in microseconds.
2366 *
2367 * @param curTime the amount of elapsed realtime in microseconds.
2368 */
2369 public abstract long getBatteryUptime(long curTime);
2370
2371 /**
2372 * Returns the current battery realtime in microseconds.
2373 *
2374 * @param curTime the amount of elapsed realtime in microseconds.
2375 */
2376 public abstract long getBatteryRealtime(long curTime);
Bookatzc8c44962017-05-11 12:12:54 -07002377
The Android Open Source Project10592532009-03-18 17:39:46 -07002378 /**
Evan Millar633a1742009-04-02 16:36:33 -07002379 * Returns the battery percentage level at the last time the device was unplugged from power, or
Bookatzc8c44962017-05-11 12:12:54 -07002380 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07002381 */
Evan Millar633a1742009-04-02 16:36:33 -07002382 public abstract int getDischargeStartLevel();
Bookatzc8c44962017-05-11 12:12:54 -07002383
The Android Open Source Project10592532009-03-18 17:39:46 -07002384 /**
Evan Millar633a1742009-04-02 16:36:33 -07002385 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
2386 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07002387 */
Evan Millar633a1742009-04-02 16:36:33 -07002388 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389
2390 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002391 * Get the amount the battery has discharged since the stats were
2392 * last reset after charging, as a lower-end approximation.
2393 */
2394 public abstract int getLowDischargeAmountSinceCharge();
2395
2396 /**
2397 * Get the amount the battery has discharged since the stats were
2398 * last reset after charging, as an upper-end approximation.
2399 */
2400 public abstract int getHighDischargeAmountSinceCharge();
2401
2402 /**
Dianne Hackborn40c87252014-03-19 16:55:40 -07002403 * Retrieve the discharge amount over the selected discharge period <var>which</var>.
2404 */
2405 public abstract int getDischargeAmount(int which);
2406
2407 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002408 * Get the amount the battery has discharged while the screen was on,
2409 * since the last time power was unplugged.
2410 */
2411 public abstract int getDischargeAmountScreenOn();
2412
2413 /**
2414 * Get the amount the battery has discharged while the screen was on,
2415 * since the last time the device was charged.
2416 */
2417 public abstract int getDischargeAmountScreenOnSinceCharge();
2418
2419 /**
2420 * Get the amount the battery has discharged while the screen was off,
2421 * since the last time power was unplugged.
2422 */
2423 public abstract int getDischargeAmountScreenOff();
2424
2425 /**
2426 * Get the amount the battery has discharged while the screen was off,
2427 * since the last time the device was charged.
2428 */
2429 public abstract int getDischargeAmountScreenOffSinceCharge();
2430
2431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 * Returns the total, last, or current battery uptime in microseconds.
2433 *
2434 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002435 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 */
2437 public abstract long computeBatteryUptime(long curTime, int which);
2438
2439 /**
2440 * Returns the total, last, or current battery realtime in microseconds.
2441 *
2442 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002443 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 */
2445 public abstract long computeBatteryRealtime(long curTime, int which);
2446
2447 /**
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002448 * Returns the total, last, or current battery screen off uptime in microseconds.
2449 *
2450 * @param curTime the elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002451 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002452 */
2453 public abstract long computeBatteryScreenOffUptime(long curTime, int which);
2454
2455 /**
2456 * Returns the total, last, or current battery screen off realtime in microseconds.
2457 *
2458 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002459 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002460 */
2461 public abstract long computeBatteryScreenOffRealtime(long curTime, int which);
2462
2463 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 * Returns the total, last, or current uptime in microseconds.
2465 *
2466 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002467 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 */
2469 public abstract long computeUptime(long curTime, int which);
2470
2471 /**
2472 * Returns the total, last, or current realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002473 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 * @param curTime the current elapsed realtime in microseconds.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002475 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 */
2477 public abstract long computeRealtime(long curTime, int which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002478
2479 /**
2480 * Compute an approximation for how much run time (in microseconds) is remaining on
2481 * the battery. Returns -1 if no time can be computed: either there is not
2482 * enough current data to make a decision, or the battery is currently
2483 * charging.
2484 *
2485 * @param curTime The current elepsed realtime in microseconds.
2486 */
2487 public abstract long computeBatteryTimeRemaining(long curTime);
2488
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002489 // The part of a step duration that is the actual time.
2490 public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;
2491
2492 // Bits in a step duration that are the new battery level we are at.
2493 public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002494 public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002495
2496 // Bits in a step duration that are the initial mode we were in at that step.
2497 public static final long STEP_LEVEL_INITIAL_MODE_MASK = 0x00ff000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002498 public static final int STEP_LEVEL_INITIAL_MODE_SHIFT = 48;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002499
2500 // Bits in a step duration that indicate which modes changed during that step.
2501 public static final long STEP_LEVEL_MODIFIED_MODE_MASK = 0xff00000000000000L;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002502 public static final int STEP_LEVEL_MODIFIED_MODE_SHIFT = 56;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002503
2504 // Step duration mode: the screen is on, off, dozed, etc; value is Display.STATE_* - 1.
2505 public static final int STEP_LEVEL_MODE_SCREEN_STATE = 0x03;
2506
Santos Cordone94f0502017-02-24 12:31:20 -08002507 // The largest value for screen state that is tracked in battery states. Any values above
2508 // this should be mapped back to one of the tracked values before being tracked here.
2509 public static final int MAX_TRACKED_SCREEN_STATE = Display.STATE_DOZE_SUSPEND;
2510
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002511 // Step duration mode: power save is on.
2512 public static final int STEP_LEVEL_MODE_POWER_SAVE = 0x04;
2513
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002514 // Step duration mode: device is currently in idle mode.
2515 public static final int STEP_LEVEL_MODE_DEVICE_IDLE = 0x08;
2516
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002517 public static final int[] STEP_LEVEL_MODES_OF_INTEREST = new int[] {
2518 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002519 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2520 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002521 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2522 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2523 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2524 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
2525 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002526 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_POWER_SAVE|STEP_LEVEL_MODE_DEVICE_IDLE,
2527 STEP_LEVEL_MODE_SCREEN_STATE|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002528 };
2529 public static final int[] STEP_LEVEL_MODE_VALUES = new int[] {
2530 (Display.STATE_OFF-1),
2531 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002532 (Display.STATE_OFF-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002533 (Display.STATE_ON-1),
2534 (Display.STATE_ON-1)|STEP_LEVEL_MODE_POWER_SAVE,
2535 (Display.STATE_DOZE-1),
2536 (Display.STATE_DOZE-1)|STEP_LEVEL_MODE_POWER_SAVE,
2537 (Display.STATE_DOZE_SUSPEND-1),
2538 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_POWER_SAVE,
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002539 (Display.STATE_DOZE_SUSPEND-1)|STEP_LEVEL_MODE_DEVICE_IDLE,
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002540 };
2541 public static final String[] STEP_LEVEL_MODE_LABELS = new String[] {
2542 "screen off",
2543 "screen off power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002544 "screen off device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002545 "screen on",
2546 "screen on power save",
2547 "screen doze",
2548 "screen doze power save",
2549 "screen doze-suspend",
2550 "screen doze-suspend power save",
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002551 "screen doze-suspend device idle",
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002552 };
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002553
2554 /**
Adam Lesinski3ee3f632016-06-08 13:55:55 -07002555 * Return the counter keeping track of the amount of battery discharge while the screen was off,
2556 * measured in micro-Ampere-hours. This will be non-zero only if the device's battery has
2557 * a coulomb counter.
2558 */
2559 public abstract LongCounter getDischargeScreenOffCoulombCounter();
2560
2561 /**
2562 * Return the counter keeping track of the amount of battery discharge measured in
2563 * micro-Ampere-hours. This will be non-zero only if the device's battery has
2564 * a coulomb counter.
2565 */
2566 public abstract LongCounter getDischargeCoulombCounter();
2567
2568 /**
Adam Lesinskif9b20a92016-06-17 17:30:01 -07002569 * Returns the estimated real battery capacity, which may be less than the capacity
2570 * declared by the PowerProfile.
2571 * @return The estimated battery capacity in mAh.
2572 */
2573 public abstract int getEstimatedBatteryCapacity();
2574
2575 /**
Jocelyn Dangc627d102017-04-14 13:15:14 -07002576 * @return The minimum learned battery capacity in uAh.
2577 */
2578 public abstract int getMinLearnedBatteryCapacity();
2579
2580 /**
2581 * @return The maximum learned battery capacity in uAh.
2582 */
2583 public abstract int getMaxLearnedBatteryCapacity() ;
2584
2585 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002586 * Return the array of discharge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002587 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002588 public abstract LevelStepTracker getDischargeLevelStepTracker();
2589
2590 /**
2591 * Return the array of daily discharge step durations.
2592 */
2593 public abstract LevelStepTracker getDailyDischargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002594
2595 /**
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002596 * Compute an approximation for how much time (in microseconds) remains until the battery
2597 * is fully charged. Returns -1 if no time can be computed: either there is not
2598 * enough current data to make a decision, or the battery is currently
2599 * discharging.
2600 *
2601 * @param curTime The current elepsed realtime in microseconds.
2602 */
2603 public abstract long computeChargeTimeRemaining(long curTime);
2604
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002605 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002606 * Return the array of charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002607 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002608 public abstract LevelStepTracker getChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002609
2610 /**
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002611 * Return the array of daily charge step durations.
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002612 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08002613 public abstract LevelStepTracker getDailyChargeLevelStepTracker();
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07002614
Dianne Hackborn88e98df2015-03-23 13:29:14 -07002615 public abstract ArrayList<PackageChange> getDailyPackageChanges();
2616
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002617 public abstract Map<String, ? extends Timer> getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002618
Evan Millarc64edde2009-04-18 12:26:32 -07002619 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620
James Carr2dd7e5e2016-07-20 18:48:39 -07002621 public abstract LongSparseArray<? extends Timer> getKernelMemoryStats();
2622
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002623 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
2624
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002625 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 long days = seconds / (60 * 60 * 24);
2627 if (days != 0) {
2628 out.append(days);
2629 out.append("d ");
2630 }
2631 long used = days * 60 * 60 * 24;
2632
2633 long hours = (seconds - used) / (60 * 60);
2634 if (hours != 0 || used != 0) {
2635 out.append(hours);
2636 out.append("h ");
2637 }
2638 used += hours * 60 * 60;
2639
2640 long mins = (seconds-used) / 60;
2641 if (mins != 0 || used != 0) {
2642 out.append(mins);
2643 out.append("m ");
2644 }
2645 used += mins * 60;
2646
2647 if (seconds != 0 || used != 0) {
2648 out.append(seconds-used);
2649 out.append("s ");
2650 }
2651 }
2652
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002653 public final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002655 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 sb.append(time - (sec * 1000));
2657 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 }
2659
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002660 public final static void formatTimeMsNoSpace(StringBuilder sb, long time) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002661 long sec = time / 1000;
2662 formatTimeRaw(sb, sec);
2663 sb.append(time - (sec * 1000));
2664 sb.append("ms");
2665 }
2666
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002667 public final String formatRatioLocked(long num, long den) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002669 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 }
2671 float perc = ((float)num) / ((float)den) * 100;
2672 mFormatBuilder.setLength(0);
2673 mFormatter.format("%.1f%%", perc);
2674 return mFormatBuilder.toString();
2675 }
2676
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002677 final String formatBytesLocked(long bytes) {
Evan Millar22ac0432009-03-31 11:33:18 -07002678 mFormatBuilder.setLength(0);
Bookatzc8c44962017-05-11 12:12:54 -07002679
Evan Millar22ac0432009-03-31 11:33:18 -07002680 if (bytes < BYTES_PER_KB) {
2681 return bytes + "B";
2682 } else if (bytes < BYTES_PER_MB) {
2683 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
2684 return mFormatBuilder.toString();
2685 } else if (bytes < BYTES_PER_GB){
2686 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
2687 return mFormatBuilder.toString();
2688 } else {
2689 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
2690 return mFormatBuilder.toString();
2691 }
2692 }
2693
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002694 private static long computeWakeLock(Timer timer, long elapsedRealtimeUs, int which) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002695 if (timer != null) {
2696 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002697 long totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002698 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
2699 return totalTimeMillis;
2700 }
2701 return 0;
2702 }
2703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 /**
2705 *
2706 * @param sb a StringBuilder object.
2707 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002708 * @param elapsedRealtimeUs the current on-battery time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002710 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 * @param linePrefix a String to be prepended to each line of output.
2712 * @return the line prefix
2713 */
2714 private static final String printWakeLock(StringBuilder sb, Timer timer,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002715 long elapsedRealtimeUs, String name, int which, String linePrefix) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002718 long totalTimeMillis = computeWakeLock(timer, elapsedRealtimeUs, which);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002719
Evan Millarc64edde2009-04-18 12:26:32 -07002720 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 if (totalTimeMillis != 0) {
2722 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002723 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08002724 if (name != null) {
2725 sb.append(name);
2726 sb.append(' ');
2727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 sb.append('(');
2729 sb.append(count);
2730 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002731 final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
2732 if (maxDurationMs >= 0) {
2733 sb.append(" max=");
2734 sb.append(maxDurationMs);
2735 }
Bookatz506a8182017-05-01 14:18:42 -07002736 // Put actual time if it is available and different from totalTimeMillis.
2737 final long totalDurMs = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
2738 if (totalDurMs > totalTimeMillis) {
2739 sb.append(" actual=");
2740 sb.append(totalDurMs);
2741 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002742 if (timer.isRunningLocked()) {
2743 final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2744 if (currentMs >= 0) {
2745 sb.append(" (running for ");
2746 sb.append(currentMs);
2747 sb.append("ms)");
2748 } else {
2749 sb.append(" (running)");
2750 }
2751 }
2752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 return ", ";
2754 }
2755 }
2756 return linePrefix;
2757 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002758
2759 /**
Joe Onorato92fd23f2016-07-25 11:18:42 -07002760 * Prints details about a timer, if its total time was greater than 0.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002761 *
2762 * @param pw a PrintWriter object to print to.
2763 * @param sb a StringBuilder object.
2764 * @param timer a Timer object contining the wakelock times.
Bookatz867c0d72017-03-07 18:23:42 -08002765 * @param rawRealtimeUs the current on-battery time in microseconds.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002766 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
2767 * @param prefix a String to be prepended to each line of output.
2768 * @param type the name of the timer.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002769 * @return true if anything was printed.
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002770 */
2771 private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
Joe Onorato92fd23f2016-07-25 11:18:42 -07002772 long rawRealtimeUs, int which, String prefix, String type) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002773 if (timer != null) {
2774 // Convert from microseconds to milliseconds with rounding
Joe Onorato92fd23f2016-07-25 11:18:42 -07002775 final long totalTimeMs = (timer.getTotalTimeLocked(
2776 rawRealtimeUs, which) + 500) / 1000;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002777 final int count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002778 if (totalTimeMs != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002779 sb.setLength(0);
2780 sb.append(prefix);
2781 sb.append(" ");
2782 sb.append(type);
2783 sb.append(": ");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002784 formatTimeMs(sb, totalTimeMs);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002785 sb.append("realtime (");
2786 sb.append(count);
2787 sb.append(" times)");
Joe Onorato92fd23f2016-07-25 11:18:42 -07002788 final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
2789 if (maxDurationMs >= 0) {
2790 sb.append(" max=");
2791 sb.append(maxDurationMs);
2792 }
2793 if (timer.isRunningLocked()) {
2794 final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
2795 if (currentMs >= 0) {
2796 sb.append(" (running for ");
2797 sb.append(currentMs);
2798 sb.append("ms)");
2799 } else {
2800 sb.append(" (running)");
2801 }
2802 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002803 pw.println(sb.toString());
2804 return true;
2805 }
2806 }
2807 return false;
2808 }
Bookatzc8c44962017-05-11 12:12:54 -07002809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 /**
2811 * Checkin version of wakelock printer. Prints simple comma-separated list.
Bookatzc8c44962017-05-11 12:12:54 -07002812 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 * @param sb a StringBuilder object.
2814 * @param timer a Timer object contining the wakelock times.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002815 * @param elapsedRealtimeUs the current time in microseconds.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 * @param name the name of the wakelock.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002817 * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 * @param linePrefix a String to be prepended to each line of output.
2819 * @return the line prefix
2820 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002821 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer,
2822 long elapsedRealtimeUs, String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 long totalTimeMicros = 0;
2824 int count = 0;
Bookatz941d98f2017-05-02 19:25:18 -07002825 long max = 0;
2826 long current = 0;
2827 long totalDuration = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 if (timer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002829 totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
Bookatz506a8182017-05-01 14:18:42 -07002830 count = timer.getCountLocked(which);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002831 current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
2832 max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
Bookatz506a8182017-05-01 14:18:42 -07002833 totalDuration = timer.getTotalDurationMsLocked(elapsedRealtimeUs/1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 }
2835 sb.append(linePrefix);
2836 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
2837 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07002838 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 sb.append(count);
Joe Onorato92fd23f2016-07-25 11:18:42 -07002840 sb.append(',');
2841 sb.append(current);
2842 sb.append(',');
2843 sb.append(max);
Bookatz506a8182017-05-01 14:18:42 -07002844 // Partial, full, and window wakelocks are pooled, so totalDuration is meaningful (albeit
2845 // not always tracked). Kernel wakelocks (which have name == null) have no notion of
2846 // totalDuration independent of totalTimeMicros (since they are not pooled).
2847 if (name != null) {
2848 sb.append(',');
2849 sb.append(totalDuration);
2850 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 return ",";
2852 }
Bookatz506a8182017-05-01 14:18:42 -07002853
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002854 private static final void dumpLineHeader(PrintWriter pw, int uid, String category,
2855 String type) {
2856 pw.print(BATTERY_STATS_CHECKIN_VERSION);
2857 pw.print(',');
2858 pw.print(uid);
2859 pw.print(',');
2860 pw.print(category);
2861 pw.print(',');
2862 pw.print(type);
2863 }
2864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 /**
2866 * Dump a comma-separated line of values for terse checkin mode.
Bookatzc8c44962017-05-11 12:12:54 -07002867 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 * @param pw the PageWriter to dump log to
2869 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2870 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2871 * @param args type-dependent data arguments
2872 */
Bookatzc8c44962017-05-11 12:12:54 -07002873 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 Object... args ) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002875 dumpLineHeader(pw, uid, category, type);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002876 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002877 pw.print(',');
2878 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002880 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002882
2883 /**
Ruben Brunk6d2c3632015-05-26 17:32:16 -07002884 * Dump a given timer stat for terse checkin mode.
2885 *
2886 * @param pw the PageWriter to dump log to
2887 * @param uid the UID to log
2888 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
2889 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
2890 * @param timer a {@link Timer} to dump stats for
2891 * @param rawRealtime the current elapsed realtime of the system in microseconds
2892 * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
2893 */
2894 private static final void dumpTimer(PrintWriter pw, int uid, String category, String type,
2895 Timer timer, long rawRealtime, int which) {
2896 if (timer != null) {
2897 // Convert from microseconds to milliseconds with rounding
2898 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
2899 / 1000;
2900 final int count = timer.getCountLocked(which);
2901 if (totalTime != 0) {
2902 dumpLine(pw, uid, category, type, totalTime, count);
2903 }
2904 }
2905 }
2906
2907 /**
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002908 * Checks if the ControllerActivityCounter has any data worth dumping.
2909 */
2910 private static boolean controllerActivityHasData(ControllerActivityCounter counter, int which) {
2911 if (counter == null) {
2912 return false;
2913 }
2914
2915 if (counter.getIdleTimeCounter().getCountLocked(which) != 0
2916 || counter.getRxTimeCounter().getCountLocked(which) != 0
2917 || counter.getPowerCounter().getCountLocked(which) != 0) {
2918 return true;
2919 }
2920
2921 for (LongCounter c : counter.getTxTimeCounters()) {
2922 if (c.getCountLocked(which) != 0) {
2923 return true;
2924 }
2925 }
2926 return false;
2927 }
2928
2929 /**
2930 * Dumps the ControllerActivityCounter if it has any data worth dumping.
2931 * The order of the arguments in the final check in line is:
2932 *
2933 * idle, rx, power, tx...
2934 *
2935 * where tx... is one or more transmit level times.
2936 */
2937 private static final void dumpControllerActivityLine(PrintWriter pw, int uid, String category,
2938 String type,
2939 ControllerActivityCounter counter,
2940 int which) {
2941 if (!controllerActivityHasData(counter, which)) {
2942 return;
2943 }
2944
2945 dumpLineHeader(pw, uid, category, type);
2946 pw.print(",");
2947 pw.print(counter.getIdleTimeCounter().getCountLocked(which));
2948 pw.print(",");
2949 pw.print(counter.getRxTimeCounter().getCountLocked(which));
2950 pw.print(",");
2951 pw.print(counter.getPowerCounter().getCountLocked(which) / (1000 * 60 * 60));
2952 for (LongCounter c : counter.getTxTimeCounters()) {
2953 pw.print(",");
2954 pw.print(c.getCountLocked(which));
2955 }
2956 pw.println();
2957 }
2958
2959 private final void printControllerActivityIfInteresting(PrintWriter pw, StringBuilder sb,
2960 String prefix, String controllerName,
2961 ControllerActivityCounter counter,
2962 int which) {
2963 if (controllerActivityHasData(counter, which)) {
2964 printControllerActivity(pw, sb, prefix, controllerName, counter, which);
2965 }
2966 }
2967
2968 private final void printControllerActivity(PrintWriter pw, StringBuilder sb, String prefix,
2969 String controllerName,
2970 ControllerActivityCounter counter, int which) {
2971 final long idleTimeMs = counter.getIdleTimeCounter().getCountLocked(which);
2972 final long rxTimeMs = counter.getRxTimeCounter().getCountLocked(which);
2973 final long powerDrainMaMs = counter.getPowerCounter().getCountLocked(which);
2974 long totalTxTimeMs = 0;
2975 for (LongCounter txState : counter.getTxTimeCounters()) {
2976 totalTxTimeMs += txState.getCountLocked(which);
2977 }
2978
2979 final long totalTimeMs = idleTimeMs + rxTimeMs + totalTxTimeMs;
2980
2981 sb.setLength(0);
2982 sb.append(prefix);
2983 sb.append(" ");
2984 sb.append(controllerName);
2985 sb.append(" Idle time: ");
2986 formatTimeMs(sb, idleTimeMs);
2987 sb.append("(");
2988 sb.append(formatRatioLocked(idleTimeMs, totalTimeMs));
2989 sb.append(")");
2990 pw.println(sb.toString());
2991
2992 sb.setLength(0);
2993 sb.append(prefix);
2994 sb.append(" ");
2995 sb.append(controllerName);
2996 sb.append(" Rx time: ");
2997 formatTimeMs(sb, rxTimeMs);
2998 sb.append("(");
2999 sb.append(formatRatioLocked(rxTimeMs, totalTimeMs));
3000 sb.append(")");
3001 pw.println(sb.toString());
3002
3003 sb.setLength(0);
3004 sb.append(prefix);
3005 sb.append(" ");
3006 sb.append(controllerName);
3007 sb.append(" Tx time: ");
3008 formatTimeMs(sb, totalTxTimeMs);
3009 sb.append("(");
3010 sb.append(formatRatioLocked(totalTxTimeMs, totalTimeMs));
3011 sb.append(")");
3012 pw.println(sb.toString());
3013
3014 final int numTxLvls = counter.getTxTimeCounters().length;
3015 if (numTxLvls > 1) {
3016 for (int lvl = 0; lvl < numTxLvls; lvl++) {
3017 final long txLvlTimeMs = counter.getTxTimeCounters()[lvl].getCountLocked(which);
3018 sb.setLength(0);
3019 sb.append(prefix);
3020 sb.append(" [");
3021 sb.append(lvl);
3022 sb.append("] ");
3023 formatTimeMs(sb, txLvlTimeMs);
3024 sb.append("(");
3025 sb.append(formatRatioLocked(txLvlTimeMs, totalTxTimeMs));
3026 sb.append(")");
3027 pw.println(sb.toString());
3028 }
3029 }
3030
3031 sb.setLength(0);
3032 sb.append(prefix);
3033 sb.append(" ");
3034 sb.append(controllerName);
3035 sb.append(" Power drain: ").append(
3036 BatteryStatsHelper.makemAh(powerDrainMaMs / (double) (1000*60*60)));
3037 sb.append("mAh");
3038 pw.println(sb.toString());
3039 }
3040
3041 /**
Dianne Hackbornd953c532014-08-16 18:17:38 -07003042 * Temporary for settings.
3043 */
3044 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
3045 dumpCheckinLocked(context, pw, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3046 }
3047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 /**
3049 * Checkin server version of dump to produce more compact, computer-readable log.
Bookatzc8c44962017-05-11 12:12:54 -07003050 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07003053 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid,
3054 boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3056 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07003057 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3060 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003061 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3062 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3063 which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 final long totalRealtime = computeRealtime(rawRealtime, which);
3065 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003066 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003067 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003068 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003069 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3070 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003071 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003072 rawRealtime, which);
3073 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3074 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003075 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003076 rawRealtime, which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003077 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003078 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003079 final long dischargeCount = getDischargeCoulombCounter().getCountLocked(which);
3080 final long dischargeScreenOffCount = getDischargeScreenOffCoulombCounter()
3081 .getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003082
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003083 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003084
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003085 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003086 final int NU = uidStats.size();
Bookatzc8c44962017-05-11 12:12:54 -07003087
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003088 final String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 // Dump "battery" stat
Jocelyn Dangc627d102017-04-14 13:15:14 -07003091 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003092 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07003093 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003094 totalRealtime / 1000, totalUptime / 1000,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003095 getStartClockTime(),
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003096 whichBatteryScreenOffRealtime / 1000, whichBatteryScreenOffUptime / 1000,
Jocelyn Dangc627d102017-04-14 13:15:14 -07003097 getEstimatedBatteryCapacity(),
3098 getMinLearnedBatteryCapacity(), getMaxLearnedBatteryCapacity());
Adam Lesinski67c134f2016-06-10 15:15:08 -07003099
Bookatzc8c44962017-05-11 12:12:54 -07003100
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003101 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003102 long fullWakeLockTimeTotal = 0;
3103 long partialWakeLockTimeTotal = 0;
Bookatzc8c44962017-05-11 12:12:54 -07003104
Evan Millar22ac0432009-03-31 11:33:18 -07003105 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003106 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003107
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003108 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3109 = u.getWakelockStats();
3110 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3111 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003112
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003113 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3114 if (fullWakeTimer != null) {
3115 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(rawRealtime,
3116 which);
3117 }
3118
3119 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3120 if (partialWakeTimer != null) {
3121 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
3122 rawRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07003123 }
3124 }
3125 }
Adam Lesinskie283d332015-04-16 12:29:25 -07003126
3127 // Dump network stats
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003128 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3129 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3130 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3131 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3132 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3133 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3134 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3135 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003136 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3137 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003138 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
3139 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003140 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets,
3141 btRxTotalBytes, btTxTotalBytes);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003142
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003143 // Dump Modem controller stats
3144 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_MODEM_CONTROLLER_DATA,
3145 getModemControllerActivity(), which);
3146
Adam Lesinskie283d332015-04-16 12:29:25 -07003147 // Dump Wifi controller stats
3148 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
3149 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003150 dumpLine(pw, 0 /* uid */, category, GLOBAL_WIFI_DATA, wifiOnTime / 1000,
Adam Lesinski2208e742016-02-19 12:53:31 -08003151 wifiRunningTime / 1000, /* legacy fields follow, keep at 0 */ 0, 0, 0);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003152
3153 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_WIFI_CONTROLLER_DATA,
3154 getWifiControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003155
3156 // Dump Bluetooth controller stats
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003157 dumpControllerActivityLine(pw, 0 /* uid */, category, GLOBAL_BLUETOOTH_CONTROLLER_DATA,
3158 getBluetoothControllerActivity(), which);
Adam Lesinskie283d332015-04-16 12:29:25 -07003159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 // Dump misc stats
3161 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Adam Lesinskie283d332015-04-16 12:29:25 -07003162 screenOnTime / 1000, phoneOnTime / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003163 fullWakeLockTimeTotal / 1000, partialWakeLockTimeTotal / 1000,
Adam Lesinskie283d332015-04-16 12:29:25 -07003164 getMobileRadioActiveTime(rawRealtime, which) / 1000,
Ashish Sharma213bb2f2014-07-07 17:14:52 -07003165 getMobileRadioActiveAdjustedTime(which) / 1000, interactiveTime / 1000,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003166 powerSaveModeEnabledTime / 1000, connChanges, deviceIdleModeFullTime / 1000,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003167 getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which), deviceIdlingTime / 1000,
3168 getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which),
Adam Lesinski782327b2015-07-30 16:36:29 -07003169 getMobileRadioActiveCount(which),
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003170 getMobileRadioActiveUnknownTime(which) / 1000, deviceIdleModeLightTime / 1000,
3171 getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which), deviceLightIdlingTime / 1000,
3172 getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which),
3173 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT),
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003174 getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Bookatzc8c44962017-05-11 12:12:54 -07003175
Dianne Hackborn617f8772009-03-31 15:04:46 -07003176 // Dump screen brightness stats
3177 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
3178 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003179 args[i] = getScreenBrightnessTime(i, rawRealtime, which) / 1000;
Dianne Hackborn617f8772009-03-31 15:04:46 -07003180 }
3181 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
Bookatzc8c44962017-05-11 12:12:54 -07003182
Dianne Hackborn627bba72009-03-24 22:32:56 -07003183 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08003184 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
3185 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003186 args[i] = getPhoneSignalStrengthTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003187 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003188 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003189 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003190 getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08003191 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003192 args[i] = getPhoneSignalStrengthCount(i, which);
3193 }
3194 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003195
Dianne Hackborn627bba72009-03-24 22:32:56 -07003196 // Dump network type stats
3197 args = new Object[NUM_DATA_CONNECTION_TYPES];
3198 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003199 args[i] = getPhoneDataConnectionTime(i, rawRealtime, which) / 1000;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003200 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003201 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
3202 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
3203 args[i] = getPhoneDataConnectionCount(i, which);
3204 }
3205 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003206
3207 // Dump wifi state stats
3208 args = new Object[NUM_WIFI_STATES];
3209 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003210 args[i] = getWifiStateTime(i, rawRealtime, which) / 1000;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003211 }
3212 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
3213 for (int i=0; i<NUM_WIFI_STATES; i++) {
3214 args[i] = getWifiStateCount(i, which);
3215 }
3216 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
3217
Dianne Hackborn3251b902014-06-20 14:40:53 -07003218 // Dump wifi suppl state stats
3219 args = new Object[NUM_WIFI_SUPPL_STATES];
3220 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3221 args[i] = getWifiSupplStateTime(i, rawRealtime, which) / 1000;
3222 }
3223 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_TIME_DATA, args);
3224 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
3225 args[i] = getWifiSupplStateCount(i, which);
3226 }
3227 dumpLine(pw, 0 /* uid */, category, WIFI_SUPPL_STATE_COUNT_DATA, args);
3228
3229 // Dump wifi signal strength stats
3230 args = new Object[NUM_WIFI_SIGNAL_STRENGTH_BINS];
3231 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3232 args[i] = getWifiSignalStrengthTime(i, rawRealtime, which) / 1000;
3233 }
3234 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_TIME_DATA, args);
3235 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3236 args[i] = getWifiSignalStrengthCount(i, which);
3237 }
3238 dumpLine(pw, 0 /* uid */, category, WIFI_SIGNAL_STRENGTH_COUNT_DATA, args);
3239
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003240 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003241 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07003242 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07003243 }
Bookatzc8c44962017-05-11 12:12:54 -07003244
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003245 if (which == STATS_SINCE_UNPLUGGED) {
3246 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3247 getDischargeStartLevel()-getDischargeCurrentLevel(),
3248 getDischargeStartLevel()-getDischargeCurrentLevel(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003249 getDischargeAmountScreenOn(), getDischargeAmountScreenOff(),
3250 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003251 } else {
3252 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
3253 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003254 getDischargeAmountScreenOnSinceCharge(),
Adam Lesinski67c134f2016-06-10 15:15:08 -07003255 getDischargeAmountScreenOffSinceCharge(),
3256 dischargeCount / 1000, dischargeScreenOffCount / 1000);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003257 }
Bookatzc8c44962017-05-11 12:12:54 -07003258
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003259 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003260 final Map<String, ? extends Timer> kernelWakelocks = getKernelWakelockStats();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003261 if (kernelWakelocks.size() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003262 for (Map.Entry<String, ? extends Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003263 sb.setLength(0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003264 printWakeLockCheckin(sb, ent.getValue(), rawRealtime, null, which, "");
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003265 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA,
3266 "\"" + ent.getKey() + "\"", sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003267 }
Evan Millarc64edde2009-04-18 12:26:32 -07003268 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003269 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003270 if (wakeupReasons.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003271 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
3272 // Not doing the regular wake lock formatting to remain compatible
3273 // with the old checkin format.
3274 long totalTimeMicros = ent.getValue().getTotalTimeLocked(rawRealtime, which);
3275 int count = ent.getValue().getCountLocked(which);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003276 dumpLine(pw, 0 /* uid */, category, WAKEUP_REASON_DATA,
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003277 "\"" + ent.getKey() + "\"", (totalTimeMicros + 500) / 1000, count);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003278 }
3279 }
Evan Millarc64edde2009-04-18 12:26:32 -07003280 }
Bookatzc8c44962017-05-11 12:12:54 -07003281
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003282 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003283 helper.create(this);
3284 helper.refreshStats(which, UserHandle.USER_ALL);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003285 final List<BatterySipper> sippers = helper.getUsageList();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003286 if (sippers != null && sippers.size() > 0) {
3287 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
3288 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08003289 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003290 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
3291 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
3292 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003293 final BatterySipper bs = sippers.get(i);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003294 int uid = 0;
3295 String label;
3296 switch (bs.drainType) {
3297 case IDLE:
3298 label="idle";
3299 break;
3300 case CELL:
3301 label="cell";
3302 break;
3303 case PHONE:
3304 label="phone";
3305 break;
3306 case WIFI:
3307 label="wifi";
3308 break;
3309 case BLUETOOTH:
3310 label="blue";
3311 break;
3312 case SCREEN:
3313 label="scrn";
3314 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003315 case FLASHLIGHT:
3316 label="flashlight";
3317 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003318 case APP:
3319 uid = bs.uidObj.getUid();
3320 label = "uid";
3321 break;
3322 case USER:
3323 uid = UserHandle.getUid(bs.userId, 0);
3324 label = "user";
3325 break;
3326 case UNACCOUNTED:
3327 label = "unacc";
3328 break;
3329 case OVERCOUNTED:
3330 label = "over";
3331 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07003332 case CAMERA:
3333 label = "camera";
3334 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003335 default:
3336 label = "???";
3337 }
3338 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
Bookatz17d7d9d2017-06-08 14:50:46 -07003339 BatteryStatsHelper.makemAh(bs.totalPowerMah),
3340 bs.shouldHide ? 1 : 0,
3341 BatteryStatsHelper.makemAh(bs.screenPowerMah),
3342 BatteryStatsHelper.makemAh(bs.proportionalSmearMah));
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003343 }
3344 }
3345
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003346 final long[] cpuFreqs = getCpuFreqs();
3347 if (cpuFreqs != null) {
3348 sb.setLength(0);
3349 for (int i = 0; i < cpuFreqs.length; ++i) {
3350 sb.append((i == 0 ? "" : ",") + cpuFreqs[i]);
3351 }
3352 dumpLine(pw, 0 /* uid */, category, GLOBAL_CPU_FREQ_DATA, sb.toString());
3353 }
3354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 for (int iu = 0; iu < NU; iu++) {
3356 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003357 if (reqUid >= 0 && uid != reqUid) {
3358 continue;
3359 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003360 final Uid u = uidStats.valueAt(iu);
Adam Lesinskie283d332015-04-16 12:29:25 -07003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 // Dump Network stats per uid, if any
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003363 final long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
3364 final long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
3365 final long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
3366 final long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
3367 final long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
3368 final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
3369 final long mobileActiveTime = u.getMobileRadioActiveTime(which);
3370 final int mobileActiveCount = u.getMobileRadioActiveCount(which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003371 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003372 final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
3373 final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski5f056f62016-07-14 16:56:08 -07003374 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003375 final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
3376 final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Amith Yamasani59fe8412017-03-03 16:28:52 -08003377 // Background data transfers
3378 final long mobileBytesBgRx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_RX_DATA,
3379 which);
3380 final long mobileBytesBgTx = u.getNetworkActivityBytes(NETWORK_MOBILE_BG_TX_DATA,
3381 which);
3382 final long wifiBytesBgRx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_RX_DATA, which);
3383 final long wifiBytesBgTx = u.getNetworkActivityBytes(NETWORK_WIFI_BG_TX_DATA, which);
3384 final long mobilePacketsBgRx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_RX_DATA,
3385 which);
3386 final long mobilePacketsBgTx = u.getNetworkActivityPackets(NETWORK_MOBILE_BG_TX_DATA,
3387 which);
3388 final long wifiPacketsBgRx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_RX_DATA,
3389 which);
3390 final long wifiPacketsBgTx = u.getNetworkActivityPackets(NETWORK_WIFI_BG_TX_DATA,
3391 which);
3392
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003393 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
3394 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003395 || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
Amith Yamasani59fe8412017-03-03 16:28:52 -08003396 || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0
3397 || mobileBytesBgRx > 0 || mobileBytesBgTx > 0 || wifiBytesBgRx > 0
3398 || wifiBytesBgTx > 0
3399 || mobilePacketsBgRx > 0 || mobilePacketsBgTx > 0 || wifiPacketsBgRx > 0
3400 || wifiPacketsBgTx > 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003401 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
3402 wifiBytesRx, wifiBytesTx,
3403 mobilePacketsRx, mobilePacketsTx,
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003404 wifiPacketsRx, wifiPacketsTx,
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003405 mobileActiveTime, mobileActiveCount,
Amith Yamasani59fe8412017-03-03 16:28:52 -08003406 btBytesRx, btBytesTx, mobileWakeup, wifiWakeup,
3407 mobileBytesBgRx, mobileBytesBgTx, wifiBytesBgRx, wifiBytesBgTx,
3408 mobilePacketsBgRx, mobilePacketsBgTx, wifiPacketsBgRx, wifiPacketsBgTx
3409 );
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003410 }
3411
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003412 // Dump modem controller data, per UID.
3413 dumpControllerActivityLine(pw, uid, category, MODEM_CONTROLLER_DATA,
3414 u.getModemControllerActivity(), which);
3415
3416 // Dump Wifi controller data, per UID.
Adam Lesinskie283d332015-04-16 12:29:25 -07003417 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
3418 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
3419 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08003420 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
3421 // Note that 'ActualTime' are unpooled and always since reset (regardless of 'which')
Bookatzce49aca2017-04-03 09:47:05 -07003422 final long wifiScanActualTimeMs = (u.getWifiScanActualTime(rawRealtime) + 500) / 1000;
3423 final long wifiScanActualTimeMsBg = (u.getWifiScanBackgroundTime(rawRealtime) + 500)
3424 / 1000;
Adam Lesinskie283d332015-04-16 12:29:25 -07003425 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Dianne Hackborn62793e42015-03-09 11:15:41 -07003426 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatzce49aca2017-04-03 09:47:05 -07003427 || wifiScanCountBg != 0 || wifiScanActualTimeMs != 0
3428 || wifiScanActualTimeMsBg != 0 || uidWifiRunningTime != 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003429 dumpLine(pw, uid, category, WIFI_DATA, fullWifiLockOnTime, wifiScanTime,
3430 uidWifiRunningTime, wifiScanCount,
Bookatz867c0d72017-03-07 18:23:42 -08003431 /* legacy fields follow, keep at 0 */ 0, 0, 0,
Bookatzce49aca2017-04-03 09:47:05 -07003432 wifiScanCountBg, wifiScanActualTimeMs, wifiScanActualTimeMsBg);
The Android Open Source Project10592532009-03-18 17:39:46 -07003433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003435 dumpControllerActivityLine(pw, uid, category, WIFI_CONTROLLER_DATA,
3436 u.getWifiControllerActivity(), which);
3437
Bookatz867c0d72017-03-07 18:23:42 -08003438 final Timer bleTimer = u.getBluetoothScanTimer();
3439 if (bleTimer != null) {
3440 // Convert from microseconds to milliseconds with rounding
3441 final long totalTime = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
3442 / 1000;
3443 if (totalTime != 0) {
3444 final int count = bleTimer.getCountLocked(which);
3445 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
3446 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003447 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3448 final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
3449 final long actualTimeBg = bleTimerBg != null ?
3450 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003451 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07003452 final int resultCount = u.getBluetoothScanResultCounter() != null ?
3453 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07003454 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
3455 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
3456 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
3457 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
3458 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
3459 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3460 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
3461 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3462 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
3463 final Timer unoptimizedScanTimerBg =
3464 u.getBluetoothUnoptimizedScanBackgroundTimer();
3465 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
3466 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3467 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
3468 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
3469
Bookatz867c0d72017-03-07 18:23:42 -08003470 dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
Bookatzb1f04f32017-05-19 13:57:32 -07003471 countBg, actualTime, actualTimeBg, resultCount, resultCountBg,
3472 unoptimizedScanTotalTime, unoptimizedScanTotalTimeBg,
3473 unoptimizedScanMaxTime, unoptimizedScanMaxTimeBg);
Bookatz867c0d72017-03-07 18:23:42 -08003474 }
3475 }
Adam Lesinskid9b99be2016-03-30 16:58:51 -07003476
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003477 dumpControllerActivityLine(pw, uid, category, BLUETOOTH_CONTROLLER_DATA,
3478 u.getBluetoothControllerActivity(), which);
3479
Dianne Hackborn617f8772009-03-31 15:04:46 -07003480 if (u.hasUserActivity()) {
3481 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
3482 boolean hasData = false;
3483 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
3484 int val = u.getUserActivityCount(i, which);
3485 args[i] = val;
3486 if (val != 0) hasData = true;
3487 }
3488 if (hasData) {
Ashish Sharmacba12152014-07-07 17:14:52 -07003489 dumpLine(pw, uid /* uid */, category, USER_ACTIVITY_DATA, args);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003490 }
3491 }
Bookatzc8c44962017-05-11 12:12:54 -07003492
3493 if (u.getAggregatedPartialWakelockTimer() != null) {
3494 final Timer timer = u.getAggregatedPartialWakelockTimer();
Bookatz6d799932017-06-07 12:30:07 -07003495 // Times are since reset (regardless of 'which')
3496 final long totTimeMs = timer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07003497 final Timer bgTimer = timer.getSubTimer();
3498 final long bgTimeMs = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003499 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07003500 dumpLine(pw, uid, category, AGGREGATED_WAKELOCK_DATA, totTimeMs, bgTimeMs);
3501 }
3502
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003503 final ArrayMap<String, ? extends Uid.Wakelock> wakelocks = u.getWakelockStats();
3504 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3505 final Uid.Wakelock wl = wakelocks.valueAt(iw);
3506 String linePrefix = "";
3507 sb.setLength(0);
3508 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
3509 rawRealtime, "f", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003510 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3511 linePrefix = printWakeLockCheckin(sb, pTimer,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003512 rawRealtime, "p", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07003513 linePrefix = printWakeLockCheckin(sb, pTimer != null ? pTimer.getSubTimer() : null,
3514 rawRealtime, "bp", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003515 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
3516 rawRealtime, "w", which, linePrefix);
3517
3518 // Only log if we had at lease one wakelock...
3519 if (sb.length() > 0) {
3520 String name = wakelocks.keyAt(iw);
3521 if (name.indexOf(',') >= 0) {
3522 name = name.replace(',', '_');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003524 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 }
3526 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07003527
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003528 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
3529 for (int isy=syncs.size()-1; isy>=0; isy--) {
3530 final Timer timer = syncs.valueAt(isy);
3531 // Convert from microseconds to milliseconds with rounding
3532 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3533 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07003534 final Timer bgTimer = timer.getSubTimer();
3535 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003536 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07003537 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003538 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003539 dumpLine(pw, uid, category, SYNC_DATA, "\"" + syncs.keyAt(isy) + "\"",
Bookatz2bffb5b2017-04-13 11:59:33 -07003540 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003541 }
3542 }
3543
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003544 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
3545 for (int ij=jobs.size()-1; ij>=0; ij--) {
3546 final Timer timer = jobs.valueAt(ij);
3547 // Convert from microseconds to milliseconds with rounding
3548 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
3549 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07003550 final Timer bgTimer = timer.getSubTimer();
3551 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07003552 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07003553 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003554 if (totalTime != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003555 dumpLine(pw, uid, category, JOB_DATA, "\"" + jobs.keyAt(ij) + "\"",
Bookatzaa4594a2017-03-24 12:39:56 -07003556 totalTime, count, bgTime, bgCount);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003557 }
3558 }
3559
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003560 dumpTimer(pw, uid, category, FLASHLIGHT_DATA, u.getFlashlightTurnedOnTimer(),
3561 rawRealtime, which);
3562 dumpTimer(pw, uid, category, CAMERA_DATA, u.getCameraTurnedOnTimer(),
3563 rawRealtime, which);
3564 dumpTimer(pw, uid, category, VIDEO_DATA, u.getVideoTurnedOnTimer(),
3565 rawRealtime, which);
3566 dumpTimer(pw, uid, category, AUDIO_DATA, u.getAudioTurnedOnTimer(),
3567 rawRealtime, which);
3568
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003569 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
3570 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003571 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003572 final Uid.Sensor se = sensors.valueAt(ise);
3573 final int sensorNumber = sensors.keyAt(ise);
3574 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003575 if (timer != null) {
3576 // Convert from microseconds to milliseconds with rounding
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003577 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
3578 / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003579 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08003580 final int count = timer.getCountLocked(which);
3581 final Timer bgTimer = se.getSensorBackgroundTime();
3582 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08003583 // 'actualTime' are unpooled and always since reset (regardless of 'which')
3584 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
3585 final long bgActualTime = bgTimer != null ?
3586 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
3587 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime,
3588 count, bgCount, actualTime, bgActualTime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 }
3591 }
3592
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003593 dumpTimer(pw, uid, category, VIBRATOR_DATA, u.getVibratorOnTimer(),
3594 rawRealtime, which);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003595
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003596 dumpTimer(pw, uid, category, FOREGROUND_DATA, u.getForegroundActivityTimer(),
3597 rawRealtime, which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003598
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003599 final Object[] stateTimes = new Object[Uid.NUM_PROCESS_STATE];
Dianne Hackborn61659e52014-07-09 16:13:01 -07003600 long totalStateTime = 0;
3601 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
Dianne Hackborna8d10942015-11-19 17:55:19 -08003602 final long time = u.getProcessStateTime(ips, rawRealtime, which);
3603 totalStateTime += time;
3604 stateTimes[ips] = (time + 500) / 1000;
Dianne Hackborn61659e52014-07-09 16:13:01 -07003605 }
3606 if (totalStateTime > 0) {
3607 dumpLine(pw, uid, category, STATE_TIME_DATA, stateTimes);
3608 }
3609
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003610 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
3611 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003612 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003613 dumpLine(pw, uid, category, CPU_DATA, userCpuTimeUs / 1000, systemCpuTimeUs / 1000,
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07003614 0 /* old cpu power, keep for compatibility */);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003615 }
3616
Sudheer Shanka9b735c52017-05-09 18:26:18 -07003617 final long[] cpuFreqTimeMs = u.getCpuFreqTimes(which);
3618 // If total cpuFreqTimes is null, then we don't need to check for screenOffCpuFreqTimes.
3619 if (cpuFreqTimeMs != null) {
3620 sb.setLength(0);
3621 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3622 sb.append((i == 0 ? "" : ",") + cpuFreqTimeMs[i]);
3623 }
3624 final long[] screenOffCpuFreqTimeMs = u.getScreenOffCpuFreqTimes(which);
3625 if (screenOffCpuFreqTimeMs != null) {
3626 for (int i = 0; i < screenOffCpuFreqTimeMs.length; ++i) {
3627 sb.append("," + screenOffCpuFreqTimeMs[i]);
3628 }
3629 } else {
3630 for (int i = 0; i < cpuFreqTimeMs.length; ++i) {
3631 sb.append(",0");
3632 }
3633 }
3634 dumpLine(pw, uid, category, CPU_TIMES_AT_FREQ_DATA, UID_TIMES_TYPE_ALL,
3635 cpuFreqTimeMs.length, sb.toString());
3636 }
3637
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003638 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
3639 = u.getProcessStats();
3640 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
3641 final Uid.Proc ps = processStats.valueAt(ipr);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003642
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003643 final long userMillis = ps.getUserTime(which);
3644 final long systemMillis = ps.getSystemTime(which);
3645 final long foregroundMillis = ps.getForegroundTime(which);
3646 final int starts = ps.getStarts(which);
3647 final int numCrashes = ps.getNumCrashes(which);
3648 final int numAnrs = ps.getNumAnrs(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003649
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003650 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
3651 || starts != 0 || numAnrs != 0 || numCrashes != 0) {
Kweku Adamse0dd9c12017-03-08 08:12:15 -08003652 dumpLine(pw, uid, category, PROCESS_DATA, "\"" + processStats.keyAt(ipr) + "\"",
3653 userMillis, systemMillis, foregroundMillis, starts, numAnrs, numCrashes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 }
3655 }
3656
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003657 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
3658 = u.getPackageStats();
3659 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
3660 final Uid.Pkg ps = packageStats.valueAt(ipkg);
3661 int wakeups = 0;
3662 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
3663 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
Joe Onorato1476d322016-05-05 14:46:15 -07003664 int count = alarms.valueAt(iwa).getCountLocked(which);
3665 wakeups += count;
3666 String name = alarms.keyAt(iwa).replace(',', '_');
3667 dumpLine(pw, uid, category, WAKEUP_ALARM_DATA, name, count);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003668 }
3669 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
3670 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
3671 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
3672 final long startTime = ss.getStartTime(batteryUptime, which);
3673 final int starts = ss.getStarts(which);
3674 final int launches = ss.getLaunches(which);
3675 if (startTime != 0 || starts != 0 || launches != 0) {
3676 dumpLine(pw, uid, category, APK_DATA,
3677 wakeups, // wakeup alarms
3678 packageStats.keyAt(ipkg), // Apk
3679 serviceStats.keyAt(isvc), // service
3680 startTime / 1000, // time spent started, in ms
3681 starts,
3682 launches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 }
3684 }
3685 }
3686 }
3687 }
3688
Dianne Hackborn81038902012-11-26 17:04:09 -08003689 static final class TimerEntry {
3690 final String mName;
3691 final int mId;
3692 final BatteryStats.Timer mTimer;
3693 final long mTime;
3694 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
3695 mName = name;
3696 mId = id;
3697 mTimer = timer;
3698 mTime = time;
3699 }
3700 }
3701
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003702 private void printmAh(PrintWriter printer, double power) {
3703 printer.print(BatteryStatsHelper.makemAh(power));
3704 }
3705
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07003706 private void printmAh(StringBuilder sb, double power) {
3707 sb.append(BatteryStatsHelper.makemAh(power));
3708 }
3709
Dianne Hackbornd953c532014-08-16 18:17:38 -07003710 /**
3711 * Temporary for settings.
3712 */
3713 public final void dumpLocked(Context context, PrintWriter pw, String prefix, int which,
3714 int reqUid) {
3715 dumpLocked(context, pw, prefix, which, reqUid, BatteryStatsHelper.checkWifiOnly(context));
3716 }
3717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08003719 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
Dianne Hackbornd953c532014-08-16 18:17:38 -07003720 int reqUid, boolean wifiOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 final long rawUptime = SystemClock.uptimeMillis() * 1000;
3722 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
Bookatz6d799932017-06-07 12:30:07 -07003723 final long rawRealtimeMs = (rawRealtime + 500) / 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 final long batteryUptime = getBatteryUptime(rawUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725
3726 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
3727 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
3728 final long totalRealtime = computeRealtime(rawRealtime, which);
3729 final long totalUptime = computeUptime(rawUptime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003730 final long whichBatteryScreenOffUptime = computeBatteryScreenOffUptime(rawUptime, which);
3731 final long whichBatteryScreenOffRealtime = computeBatteryScreenOffRealtime(rawRealtime,
3732 which);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003733 final long batteryTimeRemaining = computeBatteryTimeRemaining(rawRealtime);
3734 final long chargeTimeRemaining = computeChargeTimeRemaining(rawRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003735
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003736 final StringBuilder sb = new StringBuilder(128);
Bookatzc8c44962017-05-11 12:12:54 -07003737
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003738 final SparseArray<? extends Uid> uidStats = getUidStats();
Evan Millar22ac0432009-03-31 11:33:18 -07003739 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740
Adam Lesinskif9b20a92016-06-17 17:30:01 -07003741 final int estimatedBatteryCapacity = getEstimatedBatteryCapacity();
3742 if (estimatedBatteryCapacity > 0) {
3743 sb.setLength(0);
3744 sb.append(prefix);
3745 sb.append(" Estimated battery capacity: ");
3746 sb.append(BatteryStatsHelper.makemAh(estimatedBatteryCapacity));
3747 sb.append(" mAh");
3748 pw.println(sb.toString());
3749 }
3750
Jocelyn Dangc627d102017-04-14 13:15:14 -07003751 final int minLearnedBatteryCapacity = getMinLearnedBatteryCapacity();
3752 if (minLearnedBatteryCapacity > 0) {
3753 sb.setLength(0);
3754 sb.append(prefix);
3755 sb.append(" Min learned battery capacity: ");
3756 sb.append(BatteryStatsHelper.makemAh(minLearnedBatteryCapacity / 1000));
3757 sb.append(" mAh");
3758 pw.println(sb.toString());
3759 }
3760 final int maxLearnedBatteryCapacity = getMaxLearnedBatteryCapacity();
3761 if (maxLearnedBatteryCapacity > 0) {
3762 sb.setLength(0);
3763 sb.append(prefix);
3764 sb.append(" Max learned battery capacity: ");
3765 sb.append(BatteryStatsHelper.makemAh(maxLearnedBatteryCapacity / 1000));
3766 sb.append(" mAh");
3767 pw.println(sb.toString());
3768 }
3769
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003770 sb.setLength(0);
3771 sb.append(prefix);
3772 sb.append(" Time on battery: ");
3773 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
3774 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
3775 sb.append(") realtime, ");
3776 formatTimeMs(sb, whichBatteryUptime / 1000);
3777 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
3778 sb.append(") uptime");
3779 pw.println(sb.toString());
3780 sb.setLength(0);
3781 sb.append(prefix);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003782 sb.append(" Time on battery screen off: ");
3783 formatTimeMs(sb, whichBatteryScreenOffRealtime / 1000); sb.append("(");
3784 sb.append(formatRatioLocked(whichBatteryScreenOffRealtime, totalRealtime));
3785 sb.append(") realtime, ");
3786 formatTimeMs(sb, whichBatteryScreenOffUptime / 1000);
3787 sb.append("(");
3788 sb.append(formatRatioLocked(whichBatteryScreenOffUptime, totalRealtime));
3789 sb.append(") uptime");
3790 pw.println(sb.toString());
3791 sb.setLength(0);
3792 sb.append(prefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003793 sb.append(" Total run time: ");
3794 formatTimeMs(sb, totalRealtime / 1000);
3795 sb.append("realtime, ");
3796 formatTimeMs(sb, totalUptime / 1000);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003797 sb.append("uptime");
Jeff Browne95c3cd2014-05-02 16:59:26 -07003798 pw.println(sb.toString());
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003799 if (batteryTimeRemaining >= 0) {
3800 sb.setLength(0);
3801 sb.append(prefix);
3802 sb.append(" Battery time remaining: ");
3803 formatTimeMs(sb, batteryTimeRemaining / 1000);
3804 pw.println(sb.toString());
3805 }
3806 if (chargeTimeRemaining >= 0) {
3807 sb.setLength(0);
3808 sb.append(prefix);
3809 sb.append(" Charge time remaining: ");
3810 formatTimeMs(sb, chargeTimeRemaining / 1000);
3811 pw.println(sb.toString());
3812 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -07003813
3814 final LongCounter dischargeCounter = getDischargeCoulombCounter();
3815 final long dischargeCount = dischargeCounter.getCountLocked(which);
3816 if (dischargeCount >= 0) {
3817 sb.setLength(0);
3818 sb.append(prefix);
3819 sb.append(" Discharge: ");
3820 sb.append(BatteryStatsHelper.makemAh(dischargeCount / 1000.0));
3821 sb.append(" mAh");
3822 pw.println(sb.toString());
3823 }
3824
3825 final LongCounter dischargeScreenOffCounter = getDischargeScreenOffCoulombCounter();
3826 final long dischargeScreenOffCount = dischargeScreenOffCounter.getCountLocked(which);
3827 if (dischargeScreenOffCount >= 0) {
3828 sb.setLength(0);
3829 sb.append(prefix);
3830 sb.append(" Screen off discharge: ");
3831 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOffCount / 1000.0));
3832 sb.append(" mAh");
3833 pw.println(sb.toString());
3834 }
3835
3836 final long dischargeScreenOnCount = dischargeCount - dischargeScreenOffCount;
3837 if (dischargeScreenOnCount >= 0) {
3838 sb.setLength(0);
3839 sb.append(prefix);
3840 sb.append(" Screen on discharge: ");
3841 sb.append(BatteryStatsHelper.makemAh(dischargeScreenOnCount / 1000.0));
3842 sb.append(" mAh");
3843 pw.println(sb.toString());
3844 }
3845
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003846 pw.print(" Start clock time: ");
3847 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
3848
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003849 final long screenOnTime = getScreenOnTime(rawRealtime, which);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003850 final long interactiveTime = getInteractiveTime(rawRealtime, which);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003851 final long powerSaveModeEnabledTime = getPowerSaveModeEnabledTime(rawRealtime, which);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003852 final long deviceIdleModeLightTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT,
3853 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003854 final long deviceIdleModeFullTime = getDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003855 rawRealtime, which);
3856 final long deviceLightIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_LIGHT,
3857 rawRealtime, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003858 final long deviceIdlingTime = getDeviceIdlingTime(DEVICE_IDLE_MODE_DEEP,
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003859 rawRealtime, which);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003860 final long phoneOnTime = getPhoneOnTime(rawRealtime, which);
3861 final long wifiRunningTime = getGlobalWifiRunningTime(rawRealtime, which);
3862 final long wifiOnTime = getWifiOnTime(rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003863 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003864 sb.append(prefix);
3865 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
3866 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003867 sb.append(") "); sb.append(getScreenOnCount(which));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003868 sb.append("x, Interactive: "); formatTimeMs(sb, interactiveTime / 1000);
3869 sb.append("("); sb.append(formatRatioLocked(interactiveTime, whichBatteryRealtime));
Jeff Browne95c3cd2014-05-02 16:59:26 -07003870 sb.append(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003871 pw.println(sb.toString());
3872 sb.setLength(0);
3873 sb.append(prefix);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003874 sb.append(" Screen brightnesses:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003875 boolean didOne = false;
3876 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003877 final long time = getScreenBrightnessTime(i, rawRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003878 if (time == 0) {
3879 continue;
3880 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003881 sb.append("\n ");
3882 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003883 didOne = true;
3884 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
3885 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003886 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003887 sb.append("(");
3888 sb.append(formatRatioLocked(time, screenOnTime));
3889 sb.append(")");
3890 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003891 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn617f8772009-03-31 15:04:46 -07003892 pw.println(sb.toString());
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003893 if (powerSaveModeEnabledTime != 0) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003894 sb.setLength(0);
3895 sb.append(prefix);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003896 sb.append(" Power save mode enabled: ");
3897 formatTimeMs(sb, powerSaveModeEnabledTime / 1000);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003898 sb.append("(");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003899 sb.append(formatRatioLocked(powerSaveModeEnabledTime, whichBatteryRealtime));
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003900 sb.append(")");
3901 pw.println(sb.toString());
3902 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003903 if (deviceLightIdlingTime != 0) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003904 sb.setLength(0);
3905 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003906 sb.append(" Device light idling: ");
3907 formatTimeMs(sb, deviceLightIdlingTime / 1000);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003908 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003909 sb.append(formatRatioLocked(deviceLightIdlingTime, whichBatteryRealtime));
3910 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003911 sb.append("x");
3912 pw.println(sb.toString());
3913 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003914 if (deviceIdleModeLightTime != 0) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003915 sb.setLength(0);
3916 sb.append(prefix);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003917 sb.append(" Idle mode light time: ");
3918 formatTimeMs(sb, deviceIdleModeLightTime / 1000);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003919 sb.append("(");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003920 sb.append(formatRatioLocked(deviceIdleModeLightTime, whichBatteryRealtime));
3921 sb.append(") ");
3922 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_LIGHT, which));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003923 sb.append("x");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003924 sb.append(" -- longest ");
3925 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_LIGHT));
3926 pw.println(sb.toString());
3927 }
3928 if (deviceIdlingTime != 0) {
3929 sb.setLength(0);
3930 sb.append(prefix);
3931 sb.append(" Device full idling: ");
3932 formatTimeMs(sb, deviceIdlingTime / 1000);
3933 sb.append("(");
3934 sb.append(formatRatioLocked(deviceIdlingTime, whichBatteryRealtime));
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003935 sb.append(") "); sb.append(getDeviceIdlingCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003936 sb.append("x");
3937 pw.println(sb.toString());
3938 }
3939 if (deviceIdleModeFullTime != 0) {
3940 sb.setLength(0);
3941 sb.append(prefix);
3942 sb.append(" Idle mode full time: ");
3943 formatTimeMs(sb, deviceIdleModeFullTime / 1000);
3944 sb.append("(");
3945 sb.append(formatRatioLocked(deviceIdleModeFullTime, whichBatteryRealtime));
3946 sb.append(") ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003947 sb.append(getDeviceIdleModeCount(DEVICE_IDLE_MODE_DEEP, which));
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003948 sb.append("x");
3949 sb.append(" -- longest ");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003950 formatTimeMs(sb, getLongestDeviceIdleModeTime(DEVICE_IDLE_MODE_DEEP));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003951 pw.println(sb.toString());
3952 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003953 if (phoneOnTime != 0) {
3954 sb.setLength(0);
3955 sb.append(prefix);
3956 sb.append(" Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
3957 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003958 sb.append(") "); sb.append(getPhoneOnCount(which)); sb.append("x");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003959 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003960 final int connChanges = getNumConnectivityChange(which);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003961 if (connChanges != 0) {
3962 pw.print(prefix);
3963 pw.print(" Connectivity changes: "); pw.println(connChanges);
3964 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003965
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003966 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07003967 long fullWakeLockTimeTotalMicros = 0;
3968 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08003969
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003970 final ArrayList<TimerEntry> timers = new ArrayList<>();
Dianne Hackborn81038902012-11-26 17:04:09 -08003971
Evan Millar22ac0432009-03-31 11:33:18 -07003972 for (int iu = 0; iu < NU; iu++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003973 final Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003974
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003975 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
3976 = u.getWakelockStats();
3977 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
3978 final Uid.Wakelock wl = wakelocks.valueAt(iw);
Evan Millar22ac0432009-03-31 11:33:18 -07003979
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003980 final Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
3981 if (fullWakeTimer != null) {
3982 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
3983 rawRealtime, which);
3984 }
3985
3986 final Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
3987 if (partialWakeTimer != null) {
3988 final long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
3989 rawRealtime, which);
3990 if (totalTimeMicros > 0) {
3991 if (reqUid < 0) {
3992 // Only show the ordered list of all wake
3993 // locks if the caller is not asking for data
3994 // about a specific uid.
3995 timers.add(new TimerEntry(wakelocks.keyAt(iw), u.getUid(),
3996 partialWakeTimer, totalTimeMicros));
Dianne Hackborn81038902012-11-26 17:04:09 -08003997 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07003998 partialWakeLockTimeTotalMicros += totalTimeMicros;
Evan Millar22ac0432009-03-31 11:33:18 -07003999 }
4000 }
4001 }
4002 }
Bookatzc8c44962017-05-11 12:12:54 -07004003
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004004 final long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4005 final long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4006 final long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4007 final long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
4008 final long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4009 final long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
4010 final long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4011 final long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004012 final long btRxTotalBytes = getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4013 final long btTxTotalBytes = getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004014
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004015 if (fullWakeLockTimeTotalMicros != 0) {
4016 sb.setLength(0);
4017 sb.append(prefix);
4018 sb.append(" Total full wakelock time: "); formatTimeMsNoSpace(sb,
4019 (fullWakeLockTimeTotalMicros + 500) / 1000);
4020 pw.println(sb.toString());
4021 }
4022
4023 if (partialWakeLockTimeTotalMicros != 0) {
4024 sb.setLength(0);
4025 sb.append(prefix);
4026 sb.append(" Total partial wakelock time: "); formatTimeMsNoSpace(sb,
4027 (partialWakeLockTimeTotalMicros + 500) / 1000);
4028 pw.println(sb.toString());
4029 }
4030
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004031 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004032 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
4033 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
4034 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
4035 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004036 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004037 sb.append(prefix);
Dianne Hackborn3251b902014-06-20 14:40:53 -07004038 sb.append(" Phone signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07004039 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08004040 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004041 final long time = getPhoneSignalStrengthTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004042 if (time == 0) {
4043 continue;
4044 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004045 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004046 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004047 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08004048 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004049 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004050 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004051 sb.append("(");
4052 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004053 sb.append(") ");
4054 sb.append(getPhoneSignalStrengthCount(i, which));
4055 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004056 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004057 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004058 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07004059
4060 sb.setLength(0);
4061 sb.append(prefix);
4062 sb.append(" Signal scanning time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004063 formatTimeMsNoSpace(sb, getPhoneSignalScanningTime(rawRealtime, which) / 1000);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004064 pw.println(sb.toString());
4065
Dianne Hackborn627bba72009-03-24 22:32:56 -07004066 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004067 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004068 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004069 didOne = false;
4070 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004071 final long time = getPhoneDataConnectionTime(i, rawRealtime, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004072 if (time == 0) {
4073 continue;
4074 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004075 sb.append("\n ");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004076 sb.append(prefix);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004077 didOne = true;
4078 sb.append(DATA_CONNECTION_NAMES[i]);
4079 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004080 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004081 sb.append("(");
4082 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07004083 sb.append(") ");
4084 sb.append(getPhoneDataConnectionCount(i, which));
4085 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004086 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08004087 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07004088 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004089
4090 sb.setLength(0);
4091 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004092 sb.append(" Mobile radio active time: ");
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004093 final long mobileActiveTime = getMobileRadioActiveTime(rawRealtime, which);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004094 formatTimeMs(sb, mobileActiveTime / 1000);
4095 sb.append("("); sb.append(formatRatioLocked(mobileActiveTime, whichBatteryRealtime));
4096 sb.append(") "); sb.append(getMobileRadioActiveCount(which));
4097 sb.append("x");
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07004098 pw.println(sb.toString());
4099
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004100 final long mobileActiveUnknownTime = getMobileRadioActiveUnknownTime(which);
4101 if (mobileActiveUnknownTime != 0) {
4102 sb.setLength(0);
4103 sb.append(prefix);
4104 sb.append(" Mobile radio active unknown time: ");
4105 formatTimeMs(sb, mobileActiveUnknownTime / 1000);
4106 sb.append("(");
4107 sb.append(formatRatioLocked(mobileActiveUnknownTime, whichBatteryRealtime));
4108 sb.append(") "); sb.append(getMobileRadioActiveUnknownCount(which));
4109 sb.append("x");
4110 pw.println(sb.toString());
4111 }
4112
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004113 final long mobileActiveAdjustedTime = getMobileRadioActiveAdjustedTime(which);
4114 if (mobileActiveAdjustedTime != 0) {
4115 sb.setLength(0);
4116 sb.append(prefix);
4117 sb.append(" Mobile radio active adjusted time: ");
4118 formatTimeMs(sb, mobileActiveAdjustedTime / 1000);
4119 sb.append("(");
4120 sb.append(formatRatioLocked(mobileActiveAdjustedTime, whichBatteryRealtime));
4121 sb.append(")");
4122 pw.println(sb.toString());
4123 }
4124
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004125 printControllerActivity(pw, sb, prefix, "Radio", getModemControllerActivity(), which);
4126
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004127 pw.print(prefix);
4128 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
4129 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
4130 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
4131 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004132 sb.setLength(0);
4133 sb.append(prefix);
4134 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
4135 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
4136 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
4137 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004138 sb.append(")");
4139 pw.println(sb.toString());
4140
4141 sb.setLength(0);
4142 sb.append(prefix);
4143 sb.append(" Wifi states:");
4144 didOne = false;
4145 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004146 final long time = getWifiStateTime(i, rawRealtime, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004147 if (time == 0) {
4148 continue;
4149 }
4150 sb.append("\n ");
4151 didOne = true;
4152 sb.append(WIFI_STATE_NAMES[i]);
4153 sb.append(" ");
4154 formatTimeMs(sb, time/1000);
4155 sb.append("(");
4156 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4157 sb.append(") ");
Dianne Hackborn3251b902014-06-20 14:40:53 -07004158 sb.append(getWifiStateCount(i, which));
4159 sb.append("x");
4160 }
4161 if (!didOne) sb.append(" (no activity)");
4162 pw.println(sb.toString());
4163
4164 sb.setLength(0);
4165 sb.append(prefix);
4166 sb.append(" Wifi supplicant states:");
4167 didOne = false;
4168 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
4169 final long time = getWifiSupplStateTime(i, rawRealtime, which);
4170 if (time == 0) {
4171 continue;
4172 }
4173 sb.append("\n ");
4174 didOne = true;
4175 sb.append(WIFI_SUPPL_STATE_NAMES[i]);
4176 sb.append(" ");
4177 formatTimeMs(sb, time/1000);
4178 sb.append("(");
4179 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4180 sb.append(") ");
4181 sb.append(getWifiSupplStateCount(i, which));
4182 sb.append("x");
4183 }
4184 if (!didOne) sb.append(" (no activity)");
4185 pw.println(sb.toString());
4186
4187 sb.setLength(0);
4188 sb.append(prefix);
4189 sb.append(" Wifi signal levels:");
4190 didOne = false;
4191 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4192 final long time = getWifiSignalStrengthTime(i, rawRealtime, which);
4193 if (time == 0) {
4194 continue;
4195 }
4196 sb.append("\n ");
4197 sb.append(prefix);
4198 didOne = true;
4199 sb.append("level(");
4200 sb.append(i);
4201 sb.append(") ");
4202 formatTimeMs(sb, time/1000);
4203 sb.append("(");
4204 sb.append(formatRatioLocked(time, whichBatteryRealtime));
4205 sb.append(") ");
4206 sb.append(getWifiSignalStrengthCount(i, which));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004207 sb.append("x");
4208 }
4209 if (!didOne) sb.append(" (no activity)");
4210 pw.println(sb.toString());
4211
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004212 printControllerActivity(pw, sb, prefix, "WiFi", getWifiControllerActivity(), which);
Adam Lesinskie08af192015-03-25 16:42:59 -07004213
Adam Lesinski50e47602015-12-04 17:04:54 -08004214 pw.print(prefix);
4215 pw.print(" Bluetooth total received: "); pw.print(formatBytesLocked(btRxTotalBytes));
4216 pw.print(", sent: "); pw.println(formatBytesLocked(btTxTotalBytes));
4217
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004218 final long bluetoothScanTimeMs = getBluetoothScanTime(rawRealtime, which) / 1000;
4219 sb.setLength(0);
4220 sb.append(prefix);
4221 sb.append(" Bluetooth scan time: "); formatTimeMs(sb, bluetoothScanTimeMs);
4222 pw.println(sb.toString());
4223
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004224 printControllerActivity(pw, sb, prefix, "Bluetooth", getBluetoothControllerActivity(),
4225 which);
Adam Lesinskie283d332015-04-16 12:29:25 -07004226
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004227 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004228
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004229 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004230 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004231 pw.print(prefix); pw.println(" Device is currently unplugged");
Bookatzc8c44962017-05-11 12:12:54 -07004232 pw.print(prefix); pw.print(" Discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004233 pw.println(getDischargeStartLevel());
4234 pw.print(prefix); pw.print(" Discharge cycle current level: ");
4235 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07004236 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004237 pw.print(prefix); pw.println(" Device is currently plugged into power");
Bookatzc8c44962017-05-11 12:12:54 -07004238 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004239 pw.println(getDischargeStartLevel());
Bookatzc8c44962017-05-11 12:12:54 -07004240 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004241 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07004242 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004243 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4244 pw.println(getDischargeAmountScreenOn());
4245 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4246 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07004247 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004248 } else {
4249 pw.print(prefix); pw.println(" Device battery use since last full charge");
4250 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
4251 pw.println(getLowDischargeAmountSinceCharge());
4252 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
4253 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004254 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
4255 pw.println(getDischargeAmountScreenOnSinceCharge());
4256 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
4257 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08004258 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07004259 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004260
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004261 final BatteryStatsHelper helper = new BatteryStatsHelper(context, false, wifiOnly);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004262 helper.create(this);
4263 helper.refreshStats(which, UserHandle.USER_ALL);
4264 List<BatterySipper> sippers = helper.getUsageList();
4265 if (sippers != null && sippers.size() > 0) {
4266 pw.print(prefix); pw.println(" Estimated power use (mAh):");
4267 pw.print(prefix); pw.print(" Capacity: ");
4268 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08004269 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborn536456f2014-05-23 16:51:05 -07004270 pw.print(", actual drain: "); printmAh(pw, helper.getMinDrainedPower());
4271 if (helper.getMinDrainedPower() != helper.getMaxDrainedPower()) {
4272 pw.print("-"); printmAh(pw, helper.getMaxDrainedPower());
4273 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004274 pw.println();
4275 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004276 final BatterySipper bs = sippers.get(i);
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004277 pw.print(prefix);
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004278 switch (bs.drainType) {
4279 case IDLE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004280 pw.print(" Idle: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004281 break;
4282 case CELL:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004283 pw.print(" Cell standby: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004284 break;
4285 case PHONE:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004286 pw.print(" Phone calls: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004287 break;
4288 case WIFI:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004289 pw.print(" Wifi: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004290 break;
4291 case BLUETOOTH:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004292 pw.print(" Bluetooth: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004293 break;
4294 case SCREEN:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004295 pw.print(" Screen: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004296 break;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004297 case FLASHLIGHT:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004298 pw.print(" Flashlight: ");
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004299 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004300 case APP:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004301 pw.print(" Uid ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004302 UserHandle.formatUid(pw, bs.uidObj.getUid());
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004303 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004304 break;
4305 case USER:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004306 pw.print(" User "); pw.print(bs.userId);
4307 pw.print(": ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004308 break;
4309 case UNACCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004310 pw.print(" Unaccounted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004311 break;
4312 case OVERCOUNTED:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004313 pw.print(" Over-counted: ");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004314 break;
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004315 case CAMERA:
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004316 pw.print(" Camera: ");
4317 break;
4318 default:
4319 pw.print(" ???: ");
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004320 break;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004321 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004322 printmAh(pw, bs.totalPowerMah);
4323
Adam Lesinski57123002015-06-12 16:12:07 -07004324 if (bs.usagePowerMah != bs.totalPowerMah) {
4325 // If the usage (generic power) isn't the whole amount, we list out
4326 // what components are involved in the calculation.
4327
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004328 pw.print(" (");
Adam Lesinski57123002015-06-12 16:12:07 -07004329 if (bs.usagePowerMah != 0) {
4330 pw.print(" usage=");
4331 printmAh(pw, bs.usagePowerMah);
4332 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004333 if (bs.cpuPowerMah != 0) {
4334 pw.print(" cpu=");
4335 printmAh(pw, bs.cpuPowerMah);
4336 }
4337 if (bs.wakeLockPowerMah != 0) {
4338 pw.print(" wake=");
4339 printmAh(pw, bs.wakeLockPowerMah);
4340 }
4341 if (bs.mobileRadioPowerMah != 0) {
4342 pw.print(" radio=");
4343 printmAh(pw, bs.mobileRadioPowerMah);
4344 }
4345 if (bs.wifiPowerMah != 0) {
4346 pw.print(" wifi=");
4347 printmAh(pw, bs.wifiPowerMah);
4348 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004349 if (bs.bluetoothPowerMah != 0) {
4350 pw.print(" bt=");
4351 printmAh(pw, bs.bluetoothPowerMah);
4352 }
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004353 if (bs.gpsPowerMah != 0) {
4354 pw.print(" gps=");
4355 printmAh(pw, bs.gpsPowerMah);
4356 }
4357 if (bs.sensorPowerMah != 0) {
4358 pw.print(" sensor=");
4359 printmAh(pw, bs.sensorPowerMah);
4360 }
4361 if (bs.cameraPowerMah != 0) {
4362 pw.print(" camera=");
4363 printmAh(pw, bs.cameraPowerMah);
4364 }
4365 if (bs.flashlightPowerMah != 0) {
4366 pw.print(" flash=");
4367 printmAh(pw, bs.flashlightPowerMah);
4368 }
4369 pw.print(" )");
4370 }
Bookatz17d7d9d2017-06-08 14:50:46 -07004371
4372 // If there is additional smearing information, include it.
4373 if (bs.totalSmearedPowerMah != bs.totalPowerMah) {
4374 pw.print(" Including smearing: ");
4375 printmAh(pw, bs.totalSmearedPowerMah);
4376 pw.print(" (");
4377 if (bs.screenPowerMah != 0) {
4378 pw.print(" screen=");
4379 printmAh(pw, bs.screenPowerMah);
4380 }
4381 if (bs.proportionalSmearMah != 0) {
4382 pw.print(" proportional=");
4383 printmAh(pw, bs.proportionalSmearMah);
4384 }
4385 pw.print(" )");
4386 }
4387 if (bs.shouldHide) {
4388 pw.print(" Excluded from smearing");
4389 }
4390
Adam Lesinski628ef9c2015-06-10 13:08:57 -07004391 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004392 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08004393 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08004394 }
4395
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004396 sippers = helper.getMobilemsppList();
4397 if (sippers != null && sippers.size() > 0) {
4398 pw.print(prefix); pw.println(" Per-app mobile ms per packet:");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004399 long totalTime = 0;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004400 for (int i=0; i<sippers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004401 final BatterySipper bs = sippers.get(i);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004402 sb.setLength(0);
4403 sb.append(prefix); sb.append(" Uid ");
4404 UserHandle.formatUid(sb, bs.uidObj.getUid());
4405 sb.append(": "); sb.append(BatteryStatsHelper.makemAh(bs.mobilemspp));
4406 sb.append(" ("); sb.append(bs.mobileRxPackets+bs.mobileTxPackets);
4407 sb.append(" packets over "); formatTimeMsNoSpace(sb, bs.mobileActive);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004408 sb.append(") "); sb.append(bs.mobileActiveCount); sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004409 pw.println(sb.toString());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004410 totalTime += bs.mobileActive;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004411 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004412 sb.setLength(0);
4413 sb.append(prefix);
4414 sb.append(" TOTAL TIME: ");
4415 formatTimeMs(sb, totalTime);
4416 sb.append("("); sb.append(formatRatioLocked(totalTime, whichBatteryRealtime));
4417 sb.append(")");
4418 pw.println(sb.toString());
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004419 pw.println();
4420 }
4421
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004422 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
4423 @Override
4424 public int compare(TimerEntry lhs, TimerEntry rhs) {
4425 long lhsTime = lhs.mTime;
4426 long rhsTime = rhs.mTime;
4427 if (lhsTime < rhsTime) {
4428 return 1;
4429 }
4430 if (lhsTime > rhsTime) {
4431 return -1;
4432 }
4433 return 0;
4434 }
4435 };
4436
4437 if (reqUid < 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004438 final Map<String, ? extends BatteryStats.Timer> kernelWakelocks
4439 = getKernelWakelockStats();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004440 if (kernelWakelocks.size() > 0) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004441 final ArrayList<TimerEntry> ktimers = new ArrayList<>();
4442 for (Map.Entry<String, ? extends BatteryStats.Timer> ent
4443 : kernelWakelocks.entrySet()) {
4444 final BatteryStats.Timer timer = ent.getValue();
4445 final long totalTimeMillis = computeWakeLock(timer, rawRealtime, which);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004446 if (totalTimeMillis > 0) {
4447 ktimers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
4448 }
4449 }
4450 if (ktimers.size() > 0) {
4451 Collections.sort(ktimers, timerComparator);
4452 pw.print(prefix); pw.println(" All kernel wake locks:");
4453 for (int i=0; i<ktimers.size(); i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004454 final TimerEntry timer = ktimers.get(i);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004455 String linePrefix = ": ";
4456 sb.setLength(0);
4457 sb.append(prefix);
4458 sb.append(" Kernel Wake lock ");
4459 sb.append(timer.mName);
4460 linePrefix = printWakeLock(sb, timer.mTimer, rawRealtime, null,
4461 which, linePrefix);
4462 if (!linePrefix.equals(": ")) {
4463 sb.append(" realtime");
4464 // Only print out wake locks that were held
4465 pw.println(sb.toString());
4466 }
4467 }
4468 pw.println();
4469 }
4470 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004471
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004472 if (timers.size() > 0) {
4473 Collections.sort(timers, timerComparator);
4474 pw.print(prefix); pw.println(" All partial wake locks:");
4475 for (int i=0; i<timers.size(); i++) {
4476 TimerEntry timer = timers.get(i);
4477 sb.setLength(0);
4478 sb.append(" Wake lock ");
4479 UserHandle.formatUid(sb, timer.mId);
4480 sb.append(" ");
4481 sb.append(timer.mName);
4482 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4483 sb.append(" realtime");
4484 pw.println(sb.toString());
4485 }
4486 timers.clear();
4487 pw.println();
Dianne Hackborn81038902012-11-26 17:04:09 -08004488 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004489
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004490 final Map<String, ? extends Timer> wakeupReasons = getWakeupReasonStats();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004491 if (wakeupReasons.size() > 0) {
4492 pw.print(prefix); pw.println(" All wakeup reasons:");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004493 final ArrayList<TimerEntry> reasons = new ArrayList<>();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004494 for (Map.Entry<String, ? extends Timer> ent : wakeupReasons.entrySet()) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004495 final Timer timer = ent.getValue();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004496 reasons.add(new TimerEntry(ent.getKey(), 0, timer,
4497 timer.getCountLocked(which)));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004498 }
4499 Collections.sort(reasons, timerComparator);
4500 for (int i=0; i<reasons.size(); i++) {
4501 TimerEntry timer = reasons.get(i);
4502 String linePrefix = ": ";
4503 sb.setLength(0);
4504 sb.append(prefix);
4505 sb.append(" Wakeup reason ");
4506 sb.append(timer.mName);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004507 printWakeLock(sb, timer.mTimer, rawRealtime, null, which, ": ");
4508 sb.append(" realtime");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004509 pw.println(sb.toString());
4510 }
4511 pw.println();
4512 }
Dianne Hackborn81038902012-11-26 17:04:09 -08004513 }
Evan Millar22ac0432009-03-31 11:33:18 -07004514
James Carr2dd7e5e2016-07-20 18:48:39 -07004515 final LongSparseArray<? extends Timer> mMemoryStats = getKernelMemoryStats();
4516 pw.println("Memory Stats");
4517 for (int i = 0; i < mMemoryStats.size(); i++) {
4518 sb.setLength(0);
4519 sb.append("Bandwidth ");
4520 sb.append(mMemoryStats.keyAt(i));
4521 sb.append(" Time ");
4522 sb.append(mMemoryStats.valueAt(i).getTotalTimeLocked(rawRealtime, which));
4523 pw.println(sb.toString());
4524 }
4525
Sudheer Shanka9b735c52017-05-09 18:26:18 -07004526 final long[] cpuFreqs = getCpuFreqs();
4527 if (cpuFreqs != null) {
4528 sb.setLength(0);
4529 sb.append("CPU freqs:");
4530 for (int i = 0; i < cpuFreqs.length; ++i) {
4531 sb.append(" " + cpuFreqs[i]);
4532 }
4533 pw.println(sb.toString());
4534 }
4535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 for (int iu=0; iu<NU; iu++) {
4537 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004538 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004539 continue;
4540 }
Bookatzc8c44962017-05-11 12:12:54 -07004541
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004542 final Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07004543
4544 pw.print(prefix);
4545 pw.print(" ");
4546 UserHandle.formatUid(pw, uid);
4547 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 boolean uidActivity = false;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004549
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004550 final long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
4551 final long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
4552 final long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
4553 final long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004554 final long btRxBytes = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
4555 final long btTxBytes = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
4556
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004557 final long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
4558 final long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004559 final long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
4560 final long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
Adam Lesinski50e47602015-12-04 17:04:54 -08004561
4562 final long uidMobileActiveTime = u.getMobileRadioActiveTime(which);
4563 final int uidMobileActiveCount = u.getMobileRadioActiveCount(which);
4564
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004565 final long fullWifiLockOnTime = u.getFullWifiLockTime(rawRealtime, which);
4566 final long wifiScanTime = u.getWifiScanTime(rawRealtime, which);
4567 final int wifiScanCount = u.getWifiScanCount(which);
Bookatz867c0d72017-03-07 18:23:42 -08004568 final int wifiScanCountBg = u.getWifiScanBackgroundCount(which);
4569 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4570 final long wifiScanActualTime = u.getWifiScanActualTime(rawRealtime);
4571 final long wifiScanActualTimeBg = u.getWifiScanBackgroundTime(rawRealtime);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004572 final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004573
Adam Lesinski5f056f62016-07-14 16:56:08 -07004574 final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
4575 final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
4576
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004577 if (mobileRxBytes > 0 || mobileTxBytes > 0
4578 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004579 pw.print(prefix); pw.print(" Mobile network: ");
4580 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004581 pw.print(formatBytesLocked(mobileTxBytes));
4582 pw.print(" sent (packets "); pw.print(mobileRxPackets);
4583 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004584 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004585 if (uidMobileActiveTime > 0 || uidMobileActiveCount > 0) {
4586 sb.setLength(0);
4587 sb.append(prefix); sb.append(" Mobile radio active: ");
4588 formatTimeMs(sb, uidMobileActiveTime / 1000);
4589 sb.append("(");
4590 sb.append(formatRatioLocked(uidMobileActiveTime, mobileActiveTime));
4591 sb.append(") "); sb.append(uidMobileActiveCount); sb.append("x");
4592 long packets = mobileRxPackets + mobileTxPackets;
4593 if (packets == 0) {
4594 packets = 1;
4595 }
4596 sb.append(" @ ");
4597 sb.append(BatteryStatsHelper.makemAh(uidMobileActiveTime / 1000 / (double)packets));
4598 sb.append(" mspp");
4599 pw.println(sb.toString());
4600 }
4601
Adam Lesinski5f056f62016-07-14 16:56:08 -07004602 if (mobileWakeup > 0) {
4603 sb.setLength(0);
4604 sb.append(prefix);
4605 sb.append(" Mobile radio AP wakeups: ");
4606 sb.append(mobileWakeup);
4607 pw.println(sb.toString());
4608 }
4609
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004610 printControllerActivityIfInteresting(pw, sb, prefix + " ", "Modem",
4611 u.getModemControllerActivity(), which);
4612
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004613 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004614 pw.print(prefix); pw.print(" Wi-Fi network: ");
4615 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004616 pw.print(formatBytesLocked(wifiTxBytes));
4617 pw.print(" sent (packets "); pw.print(wifiRxPackets);
4618 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004619 }
4620
Dianne Hackborn62793e42015-03-09 11:15:41 -07004621 if (fullWifiLockOnTime != 0 || wifiScanTime != 0 || wifiScanCount != 0
Bookatz867c0d72017-03-07 18:23:42 -08004622 || wifiScanCountBg != 0 || wifiScanActualTime != 0 || wifiScanActualTimeBg != 0
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004623 || uidWifiRunningTime != 0) {
4624 sb.setLength(0);
4625 sb.append(prefix); sb.append(" Wifi Running: ");
4626 formatTimeMs(sb, uidWifiRunningTime / 1000);
4627 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
4628 whichBatteryRealtime)); sb.append(")\n");
Bookatzc8c44962017-05-11 12:12:54 -07004629 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004630 formatTimeMs(sb, fullWifiLockOnTime / 1000);
4631 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
4632 whichBatteryRealtime)); sb.append(")\n");
Bookatz867c0d72017-03-07 18:23:42 -08004633 sb.append(prefix); sb.append(" Wifi Scan (blamed): ");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004634 formatTimeMs(sb, wifiScanTime / 1000);
4635 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn62793e42015-03-09 11:15:41 -07004636 whichBatteryRealtime)); sb.append(") ");
4637 sb.append(wifiScanCount);
Bookatz867c0d72017-03-07 18:23:42 -08004638 sb.append("x\n");
4639 // actual and background times are unpooled and since reset (regardless of 'which')
4640 sb.append(prefix); sb.append(" Wifi Scan (actual): ");
4641 formatTimeMs(sb, wifiScanActualTime / 1000);
4642 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTime,
4643 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4644 sb.append(") ");
4645 sb.append(wifiScanCount);
4646 sb.append("x\n");
4647 sb.append(prefix); sb.append(" Background Wifi Scan: ");
4648 formatTimeMs(sb, wifiScanActualTimeBg / 1000);
4649 sb.append("("); sb.append(formatRatioLocked(wifiScanActualTimeBg,
4650 computeBatteryRealtime(rawRealtime, STATS_SINCE_CHARGED)));
4651 sb.append(") ");
4652 sb.append(wifiScanCountBg);
Dianne Hackborn62793e42015-03-09 11:15:41 -07004653 sb.append("x");
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004654 pw.println(sb.toString());
4655 }
4656
Adam Lesinski5f056f62016-07-14 16:56:08 -07004657 if (wifiWakeup > 0) {
4658 sb.setLength(0);
4659 sb.append(prefix);
4660 sb.append(" WiFi AP wakeups: ");
4661 sb.append(wifiWakeup);
4662 pw.println(sb.toString());
4663 }
4664
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004665 printControllerActivityIfInteresting(pw, sb, prefix + " ", "WiFi",
4666 u.getWifiControllerActivity(), which);
Adam Lesinski049c88b2015-05-28 11:38:12 -07004667
Adam Lesinski50e47602015-12-04 17:04:54 -08004668 if (btRxBytes > 0 || btTxBytes > 0) {
4669 pw.print(prefix); pw.print(" Bluetooth network: ");
4670 pw.print(formatBytesLocked(btRxBytes)); pw.print(" received, ");
4671 pw.print(formatBytesLocked(btTxBytes));
4672 pw.println(" sent");
4673 }
4674
Bookatz867c0d72017-03-07 18:23:42 -08004675 final Timer bleTimer = u.getBluetoothScanTimer();
4676 if (bleTimer != null) {
4677 // Convert from microseconds to milliseconds with rounding
4678 final long totalTimeMs = (bleTimer.getTotalTimeLocked(rawRealtime, which) + 500)
4679 / 1000;
4680 if (totalTimeMs != 0) {
4681 final int count = bleTimer.getCountLocked(which);
4682 final Timer bleTimerBg = u.getBluetoothScanBackgroundTimer();
4683 final int countBg = bleTimerBg != null ? bleTimerBg.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004684 // 'actualTime' are unpooled and always since reset (regardless of 'which')
4685 final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
4686 final long actualTimeMsBg = bleTimerBg != null ?
4687 bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004688 // Result counters
Bookatz956f36bf2017-04-28 09:48:17 -07004689 final int resultCount = u.getBluetoothScanResultCounter() != null ?
4690 u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
Bookatzb1f04f32017-05-19 13:57:32 -07004691 final int resultCountBg = u.getBluetoothScanResultBgCounter() != null ?
4692 u.getBluetoothScanResultBgCounter().getCountLocked(which) : 0;
4693 // Unoptimized scan timer. Unpooled and since reset (regardless of 'which').
4694 final Timer unoptimizedScanTimer = u.getBluetoothUnoptimizedScanTimer();
4695 final long unoptimizedScanTotalTime = unoptimizedScanTimer != null ?
4696 unoptimizedScanTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4697 final long unoptimizedScanMaxTime = unoptimizedScanTimer != null ?
4698 unoptimizedScanTimer.getMaxDurationMsLocked(rawRealtimeMs) : 0;
4699 // Unoptimized bg scan timer. Unpooled and since reset (regardless of 'which').
4700 final Timer unoptimizedScanTimerBg =
4701 u.getBluetoothUnoptimizedScanBackgroundTimer();
4702 final long unoptimizedScanTotalTimeBg = unoptimizedScanTimerBg != null ?
4703 unoptimizedScanTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
4704 final long unoptimizedScanMaxTimeBg = unoptimizedScanTimerBg != null ?
4705 unoptimizedScanTimerBg.getMaxDurationMsLocked(rawRealtimeMs) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08004706
4707 sb.setLength(0);
Bookatz867c0d72017-03-07 18:23:42 -08004708 if (actualTimeMs != totalTimeMs) {
Bookatzb1f04f32017-05-19 13:57:32 -07004709 sb.append(prefix);
4710 sb.append(" Bluetooth Scan (total blamed realtime): ");
Bookatz867c0d72017-03-07 18:23:42 -08004711 formatTimeMs(sb, totalTimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07004712 sb.append(" (");
4713 sb.append(count);
4714 sb.append(" times)");
4715 if (bleTimer.isRunningLocked()) {
4716 sb.append(" (currently running)");
4717 }
4718 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004719 }
Bookatzb1f04f32017-05-19 13:57:32 -07004720
4721 sb.append(prefix);
4722 sb.append(" Bluetooth Scan (total actual realtime): ");
4723 formatTimeMs(sb, actualTimeMs); // since reset, ignores 'which'
4724 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004725 sb.append(count);
4726 sb.append(" times)");
4727 if (bleTimer.isRunningLocked()) {
Bookatzb1f04f32017-05-19 13:57:32 -07004728 sb.append(" (currently running)");
Bookatz867c0d72017-03-07 18:23:42 -08004729 }
Bookatzb1f04f32017-05-19 13:57:32 -07004730 sb.append("\n");
4731 if (actualTimeMsBg > 0 || countBg > 0) {
4732 sb.append(prefix);
4733 sb.append(" Bluetooth Scan (background realtime): ");
4734 formatTimeMs(sb, actualTimeMsBg); // since reset, ignores 'which'
4735 sb.append(" (");
Bookatz867c0d72017-03-07 18:23:42 -08004736 sb.append(countBg);
4737 sb.append(" times)");
Bookatzb1f04f32017-05-19 13:57:32 -07004738 if (bleTimerBg != null && bleTimerBg.isRunningLocked()) {
4739 sb.append(" (currently running in background)");
4740 }
4741 sb.append("\n");
Bookatz867c0d72017-03-07 18:23:42 -08004742 }
Bookatzb1f04f32017-05-19 13:57:32 -07004743
4744 sb.append(prefix);
4745 sb.append(" Bluetooth Scan Results: ");
Bookatz956f36bf2017-04-28 09:48:17 -07004746 sb.append(resultCount);
Bookatzb1f04f32017-05-19 13:57:32 -07004747 sb.append(" (");
4748 sb.append(resultCountBg);
4749 sb.append(" in background)");
4750
4751 if (unoptimizedScanTotalTime > 0 || unoptimizedScanTotalTimeBg > 0) {
4752 sb.append("\n");
4753 sb.append(prefix);
4754 sb.append(" Unoptimized Bluetooth Scan (realtime): ");
4755 formatTimeMs(sb, unoptimizedScanTotalTime); // since reset, ignores 'which'
4756 sb.append(" (max ");
4757 formatTimeMs(sb, unoptimizedScanMaxTime); // since reset, ignores 'which'
4758 sb.append(")");
4759 if (unoptimizedScanTimer != null
4760 && unoptimizedScanTimer.isRunningLocked()) {
4761 sb.append(" (currently running unoptimized)");
4762 }
4763 if (unoptimizedScanTimerBg != null && unoptimizedScanTotalTimeBg > 0) {
4764 sb.append("\n");
4765 sb.append(prefix);
4766 sb.append(" Unoptimized Bluetooth Scan (background realtime): ");
4767 formatTimeMs(sb, unoptimizedScanTotalTimeBg); // since reset
4768 sb.append(" (max ");
4769 formatTimeMs(sb, unoptimizedScanMaxTimeBg); // since reset
4770 sb.append(")");
4771 if (unoptimizedScanTimerBg.isRunningLocked()) {
4772 sb.append(" (currently running unoptimized in background)");
4773 }
4774 }
4775 }
Bookatz867c0d72017-03-07 18:23:42 -08004776 pw.println(sb.toString());
4777 uidActivity = true;
4778 }
4779 }
4780
4781
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004782
Dianne Hackborn617f8772009-03-31 15:04:46 -07004783 if (u.hasUserActivity()) {
4784 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07004785 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004786 final int val = u.getUserActivityCount(i, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004787 if (val != 0) {
4788 if (!hasData) {
4789 sb.setLength(0);
4790 sb.append(" User activity: ");
4791 hasData = true;
4792 } else {
4793 sb.append(", ");
4794 }
4795 sb.append(val);
4796 sb.append(" ");
4797 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
4798 }
4799 }
4800 if (hasData) {
4801 pw.println(sb.toString());
4802 }
4803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004804
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004805 final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks
4806 = u.getWakelockStats();
4807 long totalFullWakelock = 0, totalPartialWakelock = 0, totalWindowWakelock = 0;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004808 long totalDrawWakelock = 0;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004809 int countWakelock = 0;
4810 for (int iw=wakelocks.size()-1; iw>=0; iw--) {
4811 final Uid.Wakelock wl = wakelocks.valueAt(iw);
4812 String linePrefix = ": ";
4813 sb.setLength(0);
4814 sb.append(prefix);
4815 sb.append(" Wake lock ");
4816 sb.append(wakelocks.keyAt(iw));
4817 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), rawRealtime,
4818 "full", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004819 final Timer pTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
4820 linePrefix = printWakeLock(sb, pTimer, rawRealtime,
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004821 "partial", which, linePrefix);
Bookatz5b5ec322017-05-26 09:40:38 -07004822 linePrefix = printWakeLock(sb, pTimer != null ? pTimer.getSubTimer() : null,
4823 rawRealtime, "background partial", which, linePrefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004824 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), rawRealtime,
4825 "window", which, linePrefix);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004826 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_DRAW), rawRealtime,
4827 "draw", which, linePrefix);
Adam Lesinski9425fe22015-06-19 12:02:13 -07004828 sb.append(" realtime");
4829 pw.println(sb.toString());
4830 uidActivity = true;
4831 countWakelock++;
4832
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004833 totalFullWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
4834 rawRealtime, which);
4835 totalPartialWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
4836 rawRealtime, which);
4837 totalWindowWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
4838 rawRealtime, which);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004839 totalDrawWakelock += computeWakeLock(wl.getWakeTime(WAKE_TYPE_DRAW),
Adam Lesinski9425fe22015-06-19 12:02:13 -07004840 rawRealtime, which);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004841 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004842 if (countWakelock > 1) {
Bookatzc8c44962017-05-11 12:12:54 -07004843 // get unpooled partial wakelock quantities (unlike totalPartialWakelock, which is
4844 // pooled and therefore just a lower bound)
4845 long actualTotalPartialWakelock = 0;
4846 long actualBgPartialWakelock = 0;
4847 if (u.getAggregatedPartialWakelockTimer() != null) {
4848 final Timer aggTimer = u.getAggregatedPartialWakelockTimer();
4849 // Convert from microseconds to milliseconds with rounding
4850 actualTotalPartialWakelock =
Bookatz6d799932017-06-07 12:30:07 -07004851 aggTimer.getTotalDurationMsLocked(rawRealtimeMs);
Bookatzc8c44962017-05-11 12:12:54 -07004852 final Timer bgAggTimer = aggTimer.getSubTimer();
4853 actualBgPartialWakelock = bgAggTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004854 bgAggTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
Bookatzc8c44962017-05-11 12:12:54 -07004855 }
4856
4857 if (actualTotalPartialWakelock != 0 || actualBgPartialWakelock != 0 ||
4858 totalFullWakelock != 0 || totalPartialWakelock != 0 ||
4859 totalWindowWakelock != 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004860 sb.setLength(0);
4861 sb.append(prefix);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004862 sb.append(" TOTAL wake: ");
4863 boolean needComma = false;
4864 if (totalFullWakelock != 0) {
4865 needComma = true;
4866 formatTimeMs(sb, totalFullWakelock);
4867 sb.append("full");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004868 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004869 if (totalPartialWakelock != 0) {
4870 if (needComma) {
4871 sb.append(", ");
4872 }
4873 needComma = true;
4874 formatTimeMs(sb, totalPartialWakelock);
Bookatzc8c44962017-05-11 12:12:54 -07004875 sb.append("blamed partial");
4876 }
4877 if (actualTotalPartialWakelock != 0) {
4878 if (needComma) {
4879 sb.append(", ");
4880 }
4881 needComma = true;
4882 formatTimeMs(sb, actualTotalPartialWakelock);
4883 sb.append("actual partial");
4884 }
4885 if (actualBgPartialWakelock != 0) {
4886 if (needComma) {
4887 sb.append(", ");
4888 }
4889 needComma = true;
4890 formatTimeMs(sb, actualBgPartialWakelock);
4891 sb.append("actual background partial");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004892 }
4893 if (totalWindowWakelock != 0) {
4894 if (needComma) {
4895 sb.append(", ");
4896 }
4897 needComma = true;
4898 formatTimeMs(sb, totalWindowWakelock);
4899 sb.append("window");
4900 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004901 if (totalDrawWakelock != 0) {
Adam Lesinski9425fe22015-06-19 12:02:13 -07004902 if (needComma) {
4903 sb.append(",");
4904 }
4905 needComma = true;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07004906 formatTimeMs(sb, totalDrawWakelock);
4907 sb.append("draw");
Adam Lesinski9425fe22015-06-19 12:02:13 -07004908 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004909 sb.append(" realtime");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004910 pw.println(sb.toString());
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004911 }
4912 }
4913
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004914 final ArrayMap<String, ? extends Timer> syncs = u.getSyncStats();
4915 for (int isy=syncs.size()-1; isy>=0; isy--) {
4916 final Timer timer = syncs.valueAt(isy);
4917 // Convert from microseconds to milliseconds with rounding
4918 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4919 final int count = timer.getCountLocked(which);
Bookatz2bffb5b2017-04-13 11:59:33 -07004920 final Timer bgTimer = timer.getSubTimer();
4921 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004922 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatz2bffb5b2017-04-13 11:59:33 -07004923 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004924 sb.setLength(0);
4925 sb.append(prefix);
4926 sb.append(" Sync ");
4927 sb.append(syncs.keyAt(isy));
4928 sb.append(": ");
4929 if (totalTime != 0) {
4930 formatTimeMs(sb, totalTime);
4931 sb.append("realtime (");
4932 sb.append(count);
4933 sb.append(" times)");
Bookatz2bffb5b2017-04-13 11:59:33 -07004934 if (bgTime > 0) {
4935 sb.append(", ");
4936 formatTimeMs(sb, bgTime);
4937 sb.append("background (");
4938 sb.append(bgCount);
4939 sb.append(" times)");
4940 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004941 } else {
4942 sb.append("(not used)");
4943 }
4944 pw.println(sb.toString());
4945 uidActivity = true;
4946 }
4947
4948 final ArrayMap<String, ? extends Timer> jobs = u.getJobStats();
4949 for (int ij=jobs.size()-1; ij>=0; ij--) {
4950 final Timer timer = jobs.valueAt(ij);
4951 // Convert from microseconds to milliseconds with rounding
4952 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500) / 1000;
4953 final int count = timer.getCountLocked(which);
Bookatzaa4594a2017-03-24 12:39:56 -07004954 final Timer bgTimer = timer.getSubTimer();
4955 final long bgTime = bgTimer != null ?
Bookatz6d799932017-06-07 12:30:07 -07004956 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : -1;
Bookatzaa4594a2017-03-24 12:39:56 -07004957 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : -1;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004958 sb.setLength(0);
4959 sb.append(prefix);
4960 sb.append(" Job ");
4961 sb.append(jobs.keyAt(ij));
4962 sb.append(": ");
4963 if (totalTime != 0) {
4964 formatTimeMs(sb, totalTime);
4965 sb.append("realtime (");
4966 sb.append(count);
4967 sb.append(" times)");
Bookatzaa4594a2017-03-24 12:39:56 -07004968 if (bgTime > 0) {
4969 sb.append(", ");
4970 formatTimeMs(sb, bgTime);
4971 sb.append("background (");
4972 sb.append(bgCount);
4973 sb.append(" times)");
4974 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004975 } else {
4976 sb.append("(not used)");
4977 }
4978 pw.println(sb.toString());
4979 uidActivity = true;
4980 }
4981
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004982 uidActivity |= printTimer(pw, sb, u.getFlashlightTurnedOnTimer(), rawRealtime, which,
4983 prefix, "Flashlight");
4984 uidActivity |= printTimer(pw, sb, u.getCameraTurnedOnTimer(), rawRealtime, which,
4985 prefix, "Camera");
4986 uidActivity |= printTimer(pw, sb, u.getVideoTurnedOnTimer(), rawRealtime, which,
4987 prefix, "Video");
4988 uidActivity |= printTimer(pw, sb, u.getAudioTurnedOnTimer(), rawRealtime, which,
4989 prefix, "Audio");
4990
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004991 final SparseArray<? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
4992 final int NSE = sensors.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004993 for (int ise=0; ise<NSE; ise++) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004994 final Uid.Sensor se = sensors.valueAt(ise);
4995 final int sensorNumber = sensors.keyAt(ise);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004996 sb.setLength(0);
4997 sb.append(prefix);
4998 sb.append(" Sensor ");
4999 int handle = se.getHandle();
5000 if (handle == Uid.Sensor.GPS) {
5001 sb.append("GPS");
5002 } else {
5003 sb.append(handle);
5004 }
5005 sb.append(": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005006
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005007 final Timer timer = se.getSensorTime();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005008 if (timer != null) {
5009 // Convert from microseconds to milliseconds with rounding
Bookatz867c0d72017-03-07 18:23:42 -08005010 final long totalTime = (timer.getTotalTimeLocked(rawRealtime, which) + 500)
5011 / 1000;
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005012 final int count = timer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005013 final Timer bgTimer = se.getSensorBackgroundTime();
5014 final int bgCount = bgTimer != null ? bgTimer.getCountLocked(which) : 0;
Bookatz867c0d72017-03-07 18:23:42 -08005015 // 'actualTime' are unpooled and always since reset (regardless of 'which')
5016 final long actualTime = timer.getTotalDurationMsLocked(rawRealtimeMs);
5017 final long bgActualTime = bgTimer != null ?
5018 bgTimer.getTotalDurationMsLocked(rawRealtimeMs) : 0;
5019
Dianne Hackborn61659e52014-07-09 16:13:01 -07005020 //timer.logState();
5021 if (totalTime != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005022 if (actualTime != totalTime) {
5023 formatTimeMs(sb, totalTime);
5024 sb.append("blamed realtime, ");
5025 }
5026
5027 formatTimeMs(sb, actualTime); // since reset, regardless of 'which'
Dianne Hackborn61659e52014-07-09 16:13:01 -07005028 sb.append("realtime (");
5029 sb.append(count);
Bookatz867c0d72017-03-07 18:23:42 -08005030 sb.append(" times)");
5031
5032 if (bgActualTime != 0 || bgCount > 0) {
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005033 sb.append(", ");
Bookatz867c0d72017-03-07 18:23:42 -08005034 formatTimeMs(sb, bgActualTime); // since reset, regardless of 'which'
5035 sb.append("background (");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005036 sb.append(bgCount);
Bookatz867c0d72017-03-07 18:23:42 -08005037 sb.append(" times)");
Amith Yamasaniab9ad192016-12-06 12:46:59 -08005038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005039 } else {
5040 sb.append("(not used)");
5041 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005042 } else {
5043 sb.append("(not used)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005045
5046 pw.println(sb.toString());
5047 uidActivity = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048 }
5049
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005050 uidActivity |= printTimer(pw, sb, u.getVibratorOnTimer(), rawRealtime, which, prefix,
5051 "Vibrator");
5052 uidActivity |= printTimer(pw, sb, u.getForegroundActivityTimer(), rawRealtime, which,
5053 prefix, "Foreground activities");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005054
Dianne Hackborn61659e52014-07-09 16:13:01 -07005055 long totalStateTime = 0;
5056 for (int ips=0; ips<Uid.NUM_PROCESS_STATE; ips++) {
5057 long time = u.getProcessStateTime(ips, rawRealtime, which);
5058 if (time > 0) {
5059 totalStateTime += time;
5060 sb.setLength(0);
5061 sb.append(prefix);
5062 sb.append(" ");
5063 sb.append(Uid.PROCESS_STATE_NAMES[ips]);
5064 sb.append(" for: ");
Dianne Hackborna8d10942015-11-19 17:55:19 -08005065 formatTimeMs(sb, (time + 500) / 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005066 pw.println(sb.toString());
5067 uidActivity = true;
5068 }
5069 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005070 if (totalStateTime > 0) {
5071 sb.setLength(0);
5072 sb.append(prefix);
5073 sb.append(" Total running: ");
5074 formatTimeMs(sb, (totalStateTime + 500) / 1000);
5075 pw.println(sb.toString());
5076 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005077
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005078 final long userCpuTimeUs = u.getUserCpuTimeUs(which);
5079 final long systemCpuTimeUs = u.getSystemCpuTimeUs(which);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07005080 if (userCpuTimeUs > 0 || systemCpuTimeUs > 0) {
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005081 sb.setLength(0);
5082 sb.append(prefix);
Adam Lesinski72478f02015-06-17 15:39:43 -07005083 sb.append(" Total cpu time: u=");
5084 formatTimeMs(sb, userCpuTimeUs / 1000);
5085 sb.append("s=");
5086 formatTimeMs(sb, systemCpuTimeUs / 1000);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005087 pw.println(sb.toString());
5088 }
5089
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005090 final long[] cpuFreqTimes = u.getCpuFreqTimes(which);
5091 if (cpuFreqTimes != null) {
5092 sb.setLength(0);
5093 sb.append(" Total cpu time per freq:");
5094 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5095 sb.append(" " + cpuFreqTimes[i]);
5096 }
5097 pw.println(sb.toString());
5098 }
5099 final long[] screenOffCpuFreqTimes = u.getScreenOffCpuFreqTimes(which);
5100 if (screenOffCpuFreqTimes != null) {
5101 sb.setLength(0);
5102 sb.append(" Total screen-off cpu time per freq:");
5103 for (int i = 0; i < screenOffCpuFreqTimes.length; ++i) {
5104 sb.append(" " + screenOffCpuFreqTimes[i]);
5105 }
5106 pw.println(sb.toString());
5107 }
5108
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005109 final ArrayMap<String, ? extends BatteryStats.Uid.Proc> processStats
5110 = u.getProcessStats();
5111 for (int ipr=processStats.size()-1; ipr>=0; ipr--) {
5112 final Uid.Proc ps = processStats.valueAt(ipr);
5113 long userTime;
5114 long systemTime;
5115 long foregroundTime;
5116 int starts;
5117 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005119 userTime = ps.getUserTime(which);
5120 systemTime = ps.getSystemTime(which);
5121 foregroundTime = ps.getForegroundTime(which);
5122 starts = ps.getStarts(which);
5123 final int numCrashes = ps.getNumCrashes(which);
5124 final int numAnrs = ps.getNumAnrs(which);
5125 numExcessive = which == STATS_SINCE_CHARGED
5126 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005128 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
5129 || numExcessive != 0 || numCrashes != 0 || numAnrs != 0) {
5130 sb.setLength(0);
5131 sb.append(prefix); sb.append(" Proc ");
5132 sb.append(processStats.keyAt(ipr)); sb.append(":\n");
5133 sb.append(prefix); sb.append(" CPU: ");
5134 formatTimeMs(sb, userTime); sb.append("usr + ");
5135 formatTimeMs(sb, systemTime); sb.append("krn ; ");
5136 formatTimeMs(sb, foregroundTime); sb.append("fg");
5137 if (starts != 0 || numCrashes != 0 || numAnrs != 0) {
5138 sb.append("\n"); sb.append(prefix); sb.append(" ");
5139 boolean hasOne = false;
5140 if (starts != 0) {
5141 hasOne = true;
5142 sb.append(starts); sb.append(" starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005143 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005144 if (numCrashes != 0) {
5145 if (hasOne) {
5146 sb.append(", ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005147 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005148 hasOne = true;
5149 sb.append(numCrashes); sb.append(" crashes");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005150 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005151 if (numAnrs != 0) {
5152 if (hasOne) {
5153 sb.append(", ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005154 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005155 sb.append(numAnrs); sb.append(" anrs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156 }
5157 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005158 pw.println(sb.toString());
5159 for (int e=0; e<numExcessive; e++) {
5160 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
5161 if (ew != null) {
5162 pw.print(prefix); pw.print(" * Killed for ");
Dianne Hackbornffca58b2017-05-24 16:15:45 -07005163 if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005164 pw.print("cpu");
5165 } else {
5166 pw.print("unknown");
5167 }
5168 pw.print(" use: ");
5169 TimeUtils.formatDuration(ew.usedTime, pw);
5170 pw.print(" over ");
5171 TimeUtils.formatDuration(ew.overTime, pw);
5172 if (ew.overTime != 0) {
5173 pw.print(" (");
5174 pw.print((ew.usedTime*100)/ew.overTime);
5175 pw.println("%)");
5176 }
5177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005178 }
5179 uidActivity = true;
5180 }
5181 }
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005182
5183 final ArrayMap<String, ? extends BatteryStats.Uid.Pkg> packageStats
5184 = u.getPackageStats();
5185 for (int ipkg=packageStats.size()-1; ipkg>=0; ipkg--) {
5186 pw.print(prefix); pw.print(" Apk "); pw.print(packageStats.keyAt(ipkg));
5187 pw.println(":");
5188 boolean apkActivity = false;
5189 final Uid.Pkg ps = packageStats.valueAt(ipkg);
5190 final ArrayMap<String, ? extends Counter> alarms = ps.getWakeupAlarmStats();
5191 for (int iwa=alarms.size()-1; iwa>=0; iwa--) {
5192 pw.print(prefix); pw.print(" Wakeup alarm ");
5193 pw.print(alarms.keyAt(iwa)); pw.print(": ");
5194 pw.print(alarms.valueAt(iwa).getCountLocked(which));
5195 pw.println(" times");
5196 apkActivity = true;
5197 }
5198 final ArrayMap<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
5199 for (int isvc=serviceStats.size()-1; isvc>=0; isvc--) {
5200 final BatteryStats.Uid.Pkg.Serv ss = serviceStats.valueAt(isvc);
5201 final long startTime = ss.getStartTime(batteryUptime, which);
5202 final int starts = ss.getStarts(which);
5203 final int launches = ss.getLaunches(which);
5204 if (startTime != 0 || starts != 0 || launches != 0) {
5205 sb.setLength(0);
5206 sb.append(prefix); sb.append(" Service ");
5207 sb.append(serviceStats.keyAt(isvc)); sb.append(":\n");
5208 sb.append(prefix); sb.append(" Created for: ");
5209 formatTimeMs(sb, startTime / 1000);
5210 sb.append("uptime\n");
5211 sb.append(prefix); sb.append(" Starts: ");
5212 sb.append(starts);
5213 sb.append(", launches: "); sb.append(launches);
5214 pw.println(sb.toString());
5215 apkActivity = true;
5216 }
5217 }
5218 if (!apkActivity) {
5219 pw.print(prefix); pw.println(" (nothing executed)");
5220 }
5221 uidActivity = true;
5222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005224 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 }
5226 }
5227 }
5228
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005229 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005230 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005231 int diff = oldval ^ newval;
5232 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005233 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005234 for (int i=0; i<descriptions.length; i++) {
5235 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005236 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005237 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005238 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005239 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005240 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005241 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
5242 didWake = true;
5243 pw.print("=");
5244 if (longNames) {
5245 UserHandle.formatUid(pw, wakelockTag.uid);
5246 pw.print(":\"");
5247 pw.print(wakelockTag.string);
5248 pw.print("\"");
5249 } else {
5250 pw.print(wakelockTag.poolIdx);
5251 }
5252 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005253 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005254 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005255 pw.print("=");
5256 int val = (newval&bd.mask)>>bd.shift;
5257 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005258 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005259 } else {
5260 pw.print(val);
5261 }
5262 }
5263 }
5264 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005265 if (!didWake && wakelockTag != null) {
Ashish Sharma81850c42014-05-05 13:57:07 -07005266 pw.print(longNames ? " wake_lock=" : ",w=");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005267 if (longNames) {
5268 UserHandle.formatUid(pw, wakelockTag.uid);
5269 pw.print(":\"");
5270 pw.print(wakelockTag.string);
5271 pw.print("\"");
5272 } else {
5273 pw.print(wakelockTag.poolIdx);
5274 }
5275 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005276 }
5277
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005278 public void prepareForDumpLocked() {
5279 }
5280
5281 public static class HistoryPrinter {
5282 int oldState = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005283 int oldState2 = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005284 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005285 int oldStatus = -1;
5286 int oldHealth = -1;
5287 int oldPlug = -1;
5288 int oldTemp = -1;
5289 int oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005290 int oldChargeMAh = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005291 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005292
Dianne Hackborn3251b902014-06-20 14:40:53 -07005293 void reset() {
5294 oldState = oldState2 = 0;
5295 oldLevel = -1;
5296 oldStatus = -1;
5297 oldHealth = -1;
5298 oldPlug = -1;
5299 oldTemp = -1;
5300 oldVolt = -1;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005301 oldChargeMAh = -1;
Dianne Hackborn3251b902014-06-20 14:40:53 -07005302 }
5303
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005304 public void printNextItem(PrintWriter pw, HistoryItem rec, long baseTime, boolean checkin,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005305 boolean verbose) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005306 if (!checkin) {
5307 pw.print(" ");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005308 TimeUtils.formatDuration(rec.time - baseTime, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005309 pw.print(" (");
5310 pw.print(rec.numReadInts);
5311 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005312 } else {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005313 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5314 pw.print(HISTORY_DATA); pw.print(',');
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005315 if (lastTime < 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005316 pw.print(rec.time - baseTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005317 } else {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005318 pw.print(rec.time - lastTime);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005319 }
5320 lastTime = rec.time;
5321 }
5322 if (rec.cmd == HistoryItem.CMD_START) {
5323 if (checkin) {
5324 pw.print(":");
5325 }
5326 pw.println("START");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005327 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005328 } else if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
5329 || rec.cmd == HistoryItem.CMD_RESET) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005330 if (checkin) {
5331 pw.print(":");
5332 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005333 if (rec.cmd == HistoryItem.CMD_RESET) {
5334 pw.print("RESET:");
Dianne Hackborn3251b902014-06-20 14:40:53 -07005335 reset();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005336 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005337 pw.print("TIME:");
5338 if (checkin) {
5339 pw.println(rec.currentTime);
5340 } else {
5341 pw.print(" ");
5342 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
5343 rec.currentTime).toString());
5344 }
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005345 } else if (rec.cmd == HistoryItem.CMD_SHUTDOWN) {
5346 if (checkin) {
5347 pw.print(":");
5348 }
5349 pw.println("SHUTDOWN");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005350 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
5351 if (checkin) {
5352 pw.print(":");
5353 }
5354 pw.println("*OVERFLOW*");
5355 } else {
5356 if (!checkin) {
5357 if (rec.batteryLevel < 10) pw.print("00");
5358 else if (rec.batteryLevel < 100) pw.print("0");
5359 pw.print(rec.batteryLevel);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005360 if (verbose) {
5361 pw.print(" ");
5362 if (rec.states < 0) ;
5363 else if (rec.states < 0x10) pw.print("0000000");
5364 else if (rec.states < 0x100) pw.print("000000");
5365 else if (rec.states < 0x1000) pw.print("00000");
5366 else if (rec.states < 0x10000) pw.print("0000");
5367 else if (rec.states < 0x100000) pw.print("000");
5368 else if (rec.states < 0x1000000) pw.print("00");
5369 else if (rec.states < 0x10000000) pw.print("0");
5370 pw.print(Integer.toHexString(rec.states));
5371 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005372 } else {
5373 if (oldLevel != rec.batteryLevel) {
5374 oldLevel = rec.batteryLevel;
5375 pw.print(",Bl="); pw.print(rec.batteryLevel);
5376 }
5377 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005378 if (oldStatus != rec.batteryStatus) {
5379 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005380 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005381 switch (oldStatus) {
5382 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005383 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005384 break;
5385 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005386 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005387 break;
5388 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005389 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005390 break;
5391 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005392 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005393 break;
5394 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005395 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005396 break;
5397 default:
5398 pw.print(oldStatus);
5399 break;
5400 }
5401 }
5402 if (oldHealth != rec.batteryHealth) {
5403 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005404 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005405 switch (oldHealth) {
5406 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005407 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005408 break;
5409 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005410 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005411 break;
5412 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005413 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005414 break;
5415 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005416 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005417 break;
5418 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005419 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005420 break;
5421 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005422 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005423 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005424 case BatteryManager.BATTERY_HEALTH_COLD:
5425 pw.print(checkin ? "c" : "cold");
5426 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005427 default:
5428 pw.print(oldHealth);
5429 break;
5430 }
5431 }
5432 if (oldPlug != rec.batteryPlugType) {
5433 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005434 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005435 switch (oldPlug) {
5436 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005437 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005438 break;
5439 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005440 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005441 break;
5442 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005443 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005444 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005445 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005446 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07005447 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005448 default:
5449 pw.print(oldPlug);
5450 break;
5451 }
5452 }
5453 if (oldTemp != rec.batteryTemperature) {
5454 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005455 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005456 pw.print(oldTemp);
5457 }
5458 if (oldVolt != rec.batteryVoltage) {
5459 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005460 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005461 pw.print(oldVolt);
5462 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005463 final int chargeMAh = rec.batteryChargeUAh / 1000;
5464 if (oldChargeMAh != chargeMAh) {
5465 oldChargeMAh = chargeMAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07005466 pw.print(checkin ? ",Bcc=" : " charge=");
Adam Lesinskia8018ac2016-05-03 10:18:10 -07005467 pw.print(oldChargeMAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07005468 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005469 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005470 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005471 printBitDescriptions(pw, oldState2, rec.states2, null,
5472 HISTORY_STATE2_DESCRIPTIONS, !checkin);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005473 if (rec.wakeReasonTag != null) {
5474 if (checkin) {
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07005475 pw.print(",wr=");
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005476 pw.print(rec.wakeReasonTag.poolIdx);
5477 } else {
5478 pw.print(" wake_reason=");
5479 pw.print(rec.wakeReasonTag.uid);
5480 pw.print(":\"");
5481 pw.print(rec.wakeReasonTag.string);
5482 pw.print("\"");
5483 }
5484 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08005485 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005486 pw.print(checkin ? "," : " ");
5487 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
5488 pw.print("+");
5489 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
5490 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005491 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08005492 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
5493 : HISTORY_EVENT_NAMES;
5494 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
5495 | HistoryItem.EVENT_FLAG_FINISH);
5496 if (idx >= 0 && idx < eventNames.length) {
5497 pw.print(eventNames[idx]);
5498 } else {
5499 pw.print(checkin ? "Ev" : "event");
5500 pw.print(idx);
5501 }
5502 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005503 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005504 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08005505 } else {
Adam Lesinski041d9172016-12-12 12:03:56 -08005506 pw.append(HISTORY_EVENT_INT_FORMATTERS[idx]
5507 .applyAsString(rec.eventTag.uid));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005508 pw.print(":\"");
5509 pw.print(rec.eventTag.string);
5510 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08005511 }
5512 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005513 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005514 if (rec.stepDetails != null) {
5515 if (!checkin) {
5516 pw.print(" Details: cpu=");
5517 pw.print(rec.stepDetails.userTime);
5518 pw.print("u+");
5519 pw.print(rec.stepDetails.systemTime);
5520 pw.print("s");
5521 if (rec.stepDetails.appCpuUid1 >= 0) {
5522 pw.print(" (");
5523 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid1,
5524 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5525 if (rec.stepDetails.appCpuUid2 >= 0) {
5526 pw.print(", ");
5527 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid2,
5528 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5529 }
5530 if (rec.stepDetails.appCpuUid3 >= 0) {
5531 pw.print(", ");
5532 printStepCpuUidDetails(pw, rec.stepDetails.appCpuUid3,
5533 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5534 }
5535 pw.print(')');
5536 }
5537 pw.println();
5538 pw.print(" /proc/stat=");
5539 pw.print(rec.stepDetails.statUserTime);
5540 pw.print(" usr, ");
5541 pw.print(rec.stepDetails.statSystemTime);
5542 pw.print(" sys, ");
5543 pw.print(rec.stepDetails.statIOWaitTime);
5544 pw.print(" io, ");
5545 pw.print(rec.stepDetails.statIrqTime);
5546 pw.print(" irq, ");
5547 pw.print(rec.stepDetails.statSoftIrqTime);
5548 pw.print(" sirq, ");
5549 pw.print(rec.stepDetails.statIdlTime);
5550 pw.print(" idle");
5551 int totalRun = rec.stepDetails.statUserTime + rec.stepDetails.statSystemTime
5552 + rec.stepDetails.statIOWaitTime + rec.stepDetails.statIrqTime
5553 + rec.stepDetails.statSoftIrqTime;
5554 int total = totalRun + rec.stepDetails.statIdlTime;
5555 if (total > 0) {
5556 pw.print(" (");
5557 float perc = ((float)totalRun) / ((float)total) * 100;
5558 pw.print(String.format("%.1f%%", perc));
5559 pw.print(" of ");
5560 StringBuilder sb = new StringBuilder(64);
5561 formatTimeMsNoSpace(sb, total*10);
5562 pw.print(sb);
5563 pw.print(")");
5564 }
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005565 pw.print(", PlatformIdleStat ");
5566 pw.print(rec.stepDetails.statPlatformIdleState);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005567 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00005568
5569 pw.print(", SubsystemPowerState ");
5570 pw.print(rec.stepDetails.statSubsystemPowerState);
5571 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005572 } else {
5573 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5574 pw.print(HISTORY_DATA); pw.print(",0,Dcpu=");
5575 pw.print(rec.stepDetails.userTime);
5576 pw.print(":");
5577 pw.print(rec.stepDetails.systemTime);
5578 if (rec.stepDetails.appCpuUid1 >= 0) {
5579 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid1,
5580 rec.stepDetails.appCpuUTime1, rec.stepDetails.appCpuSTime1);
5581 if (rec.stepDetails.appCpuUid2 >= 0) {
5582 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid2,
5583 rec.stepDetails.appCpuUTime2, rec.stepDetails.appCpuSTime2);
5584 }
5585 if (rec.stepDetails.appCpuUid3 >= 0) {
5586 printStepCpuUidCheckinDetails(pw, rec.stepDetails.appCpuUid3,
5587 rec.stepDetails.appCpuUTime3, rec.stepDetails.appCpuSTime3);
5588 }
5589 }
5590 pw.println();
5591 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
5592 pw.print(HISTORY_DATA); pw.print(",0,Dpst=");
5593 pw.print(rec.stepDetails.statUserTime);
5594 pw.print(',');
5595 pw.print(rec.stepDetails.statSystemTime);
5596 pw.print(',');
5597 pw.print(rec.stepDetails.statIOWaitTime);
5598 pw.print(',');
5599 pw.print(rec.stepDetails.statIrqTime);
5600 pw.print(',');
5601 pw.print(rec.stepDetails.statSoftIrqTime);
5602 pw.print(',');
5603 pw.print(rec.stepDetails.statIdlTime);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07005604 pw.print(',');
Adam Lesinski8568d8f2016-07-15 18:13:23 -07005605 if (rec.stepDetails.statPlatformIdleState != null) {
5606 pw.print(rec.stepDetails.statPlatformIdleState);
5607 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005608 pw.println();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00005609
5610 if (rec.stepDetails.statSubsystemPowerState != null) {
5611 pw.print(rec.stepDetails.statSubsystemPowerState);
5612 }
5613 pw.println();
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005614 }
5615 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005616 oldState = rec.states;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005617 oldState2 = rec.states2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005618 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005619 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005620
5621 private void printStepCpuUidDetails(PrintWriter pw, int uid, int utime, int stime) {
5622 UserHandle.formatUid(pw, uid);
5623 pw.print("=");
5624 pw.print(utime);
5625 pw.print("u+");
5626 pw.print(stime);
5627 pw.print("s");
5628 }
5629
5630 private void printStepCpuUidCheckinDetails(PrintWriter pw, int uid, int utime, int stime) {
5631 pw.print('/');
5632 pw.print(uid);
5633 pw.print(":");
5634 pw.print(utime);
5635 pw.print(":");
5636 pw.print(stime);
5637 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005638 }
5639
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005640 private void printSizeValue(PrintWriter pw, long size) {
5641 float result = size;
5642 String suffix = "";
5643 if (result >= 10*1024) {
5644 suffix = "KB";
5645 result = result / 1024;
5646 }
5647 if (result >= 10*1024) {
5648 suffix = "MB";
5649 result = result / 1024;
5650 }
5651 if (result >= 10*1024) {
5652 suffix = "GB";
5653 result = result / 1024;
5654 }
5655 if (result >= 10*1024) {
5656 suffix = "TB";
5657 result = result / 1024;
5658 }
5659 if (result >= 10*1024) {
5660 suffix = "PB";
5661 result = result / 1024;
5662 }
5663 pw.print((int)result);
5664 pw.print(suffix);
5665 }
5666
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005667 private static boolean dumpTimeEstimate(PrintWriter pw, String label1, String label2,
5668 String label3, long estimatedTime) {
5669 if (estimatedTime < 0) {
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005670 return false;
5671 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005672 pw.print(label1);
5673 pw.print(label2);
5674 pw.print(label3);
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005675 StringBuilder sb = new StringBuilder(64);
5676 formatTimeMs(sb, estimatedTime);
5677 pw.print(sb);
5678 pw.println();
Dianne Hackbornad6a99b2014-11-18 10:11:10 -08005679 return true;
5680 }
5681
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005682 private static boolean dumpDurationSteps(PrintWriter pw, String prefix, String header,
5683 LevelStepTracker steps, boolean checkin) {
5684 if (steps == null) {
5685 return false;
5686 }
5687 int count = steps.mNumStepDurations;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005688 if (count <= 0) {
5689 return false;
5690 }
5691 if (!checkin) {
5692 pw.println(header);
5693 }
Kweku Adams030980a2015-04-01 16:07:48 -07005694 String[] lineArgs = new String[5];
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005695 for (int i=0; i<count; i++) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005696 long duration = steps.getDurationAt(i);
5697 int level = steps.getLevelAt(i);
5698 long initMode = steps.getInitModeAt(i);
5699 long modMode = steps.getModModeAt(i);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005700 if (checkin) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005701 lineArgs[0] = Long.toString(duration);
5702 lineArgs[1] = Integer.toString(level);
5703 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5704 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5705 case Display.STATE_OFF: lineArgs[2] = "s-"; break;
5706 case Display.STATE_ON: lineArgs[2] = "s+"; break;
5707 case Display.STATE_DOZE: lineArgs[2] = "sd"; break;
5708 case Display.STATE_DOZE_SUSPEND: lineArgs[2] = "sds"; break;
Kweku Adams030980a2015-04-01 16:07:48 -07005709 default: lineArgs[2] = "?"; break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005710 }
5711 } else {
5712 lineArgs[2] = "";
5713 }
5714 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5715 lineArgs[3] = (initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0 ? "p+" : "p-";
5716 } else {
5717 lineArgs[3] = "";
5718 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005719 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
Kweku Adams030980a2015-04-01 16:07:48 -07005720 lineArgs[4] = (initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0 ? "i+" : "i-";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005721 } else {
Kweku Adams030980a2015-04-01 16:07:48 -07005722 lineArgs[4] = "";
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005723 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005724 dumpLine(pw, 0 /* uid */, "i" /* category */, header, (Object[])lineArgs);
5725 } else {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005726 pw.print(prefix);
5727 pw.print("#"); pw.print(i); pw.print(": ");
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005728 TimeUtils.formatDuration(duration, pw);
5729 pw.print(" to "); pw.print(level);
5730 boolean haveModes = false;
5731 if ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) == 0) {
5732 pw.print(" (");
5733 switch ((int)(initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
5734 case Display.STATE_OFF: pw.print("screen-off"); break;
5735 case Display.STATE_ON: pw.print("screen-on"); break;
5736 case Display.STATE_DOZE: pw.print("screen-doze"); break;
5737 case Display.STATE_DOZE_SUSPEND: pw.print("screen-doze-suspend"); break;
Kweku Adams030980a2015-04-01 16:07:48 -07005738 default: pw.print("screen-?"); break;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005739 }
5740 haveModes = true;
5741 }
5742 if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) == 0) {
5743 pw.print(haveModes ? ", " : " (");
5744 pw.print((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0
5745 ? "power-save-on" : "power-save-off");
5746 haveModes = true;
5747 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005748 if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) == 0) {
5749 pw.print(haveModes ? ", " : " (");
5750 pw.print((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0
5751 ? "device-idle-on" : "device-idle-off");
5752 haveModes = true;
5753 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07005754 if (haveModes) {
5755 pw.print(")");
5756 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005757 pw.println();
5758 }
5759 }
5760 return true;
5761 }
5762
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005763 public static final int DUMP_CHARGED_ONLY = 1<<1;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005764 public static final int DUMP_DAILY_ONLY = 1<<2;
5765 public static final int DUMP_HISTORY_ONLY = 1<<3;
5766 public static final int DUMP_INCLUDE_HISTORY = 1<<4;
5767 public static final int DUMP_VERBOSE = 1<<5;
5768 public static final int DUMP_DEVICE_WIFI_ONLY = 1<<6;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005769
Dianne Hackborn37de0982014-05-09 09:32:18 -07005770 private void dumpHistoryLocked(PrintWriter pw, int flags, long histStart, boolean checkin) {
5771 final HistoryPrinter hprinter = new HistoryPrinter();
5772 final HistoryItem rec = new HistoryItem();
5773 long lastTime = -1;
5774 long baseTime = -1;
5775 boolean printed = false;
5776 HistoryEventTracker tracker = null;
5777 while (getNextHistoryLocked(rec)) {
5778 lastTime = rec.time;
5779 if (baseTime < 0) {
5780 baseTime = lastTime;
5781 }
5782 if (rec.time >= histStart) {
5783 if (histStart >= 0 && !printed) {
5784 if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
Ashish Sharma60200712014-05-23 18:22:20 -07005785 || rec.cmd == HistoryItem.CMD_RESET
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08005786 || rec.cmd == HistoryItem.CMD_START
5787 || rec.cmd == HistoryItem.CMD_SHUTDOWN) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005788 printed = true;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005789 hprinter.printNextItem(pw, rec, baseTime, checkin,
5790 (flags&DUMP_VERBOSE) != 0);
5791 rec.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005792 } else if (rec.currentTime != 0) {
5793 printed = true;
5794 byte cmd = rec.cmd;
5795 rec.cmd = HistoryItem.CMD_CURRENT_TIME;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005796 hprinter.printNextItem(pw, rec, baseTime, checkin,
5797 (flags&DUMP_VERBOSE) != 0);
5798 rec.cmd = cmd;
5799 }
5800 if (tracker != null) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005801 if (rec.cmd != HistoryItem.CMD_UPDATE) {
5802 hprinter.printNextItem(pw, rec, baseTime, checkin,
5803 (flags&DUMP_VERBOSE) != 0);
5804 rec.cmd = HistoryItem.CMD_UPDATE;
5805 }
5806 int oldEventCode = rec.eventCode;
5807 HistoryTag oldEventTag = rec.eventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005808 rec.eventTag = new HistoryTag();
5809 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
5810 HashMap<String, SparseIntArray> active
5811 = tracker.getStateForEvent(i);
5812 if (active == null) {
5813 continue;
5814 }
5815 for (HashMap.Entry<String, SparseIntArray> ent
5816 : active.entrySet()) {
5817 SparseIntArray uids = ent.getValue();
5818 for (int j=0; j<uids.size(); j++) {
5819 rec.eventCode = i;
5820 rec.eventTag.string = ent.getKey();
5821 rec.eventTag.uid = uids.keyAt(j);
5822 rec.eventTag.poolIdx = uids.valueAt(j);
Dianne Hackborn37de0982014-05-09 09:32:18 -07005823 hprinter.printNextItem(pw, rec, baseTime, checkin,
5824 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005825 rec.wakeReasonTag = null;
5826 rec.wakelockTag = null;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005827 }
5828 }
5829 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005830 rec.eventCode = oldEventCode;
5831 rec.eventTag = oldEventTag;
Dianne Hackborn37de0982014-05-09 09:32:18 -07005832 tracker = null;
5833 }
5834 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07005835 hprinter.printNextItem(pw, rec, baseTime, checkin,
5836 (flags&DUMP_VERBOSE) != 0);
Dianne Hackborn536456f2014-05-23 16:51:05 -07005837 } else if (false && rec.eventCode != HistoryItem.EVENT_NONE) {
5838 // This is an attempt to aggregate the previous state and generate
5839 // fake events to reflect that state at the point where we start
5840 // printing real events. It doesn't really work right, so is turned off.
Dianne Hackborn37de0982014-05-09 09:32:18 -07005841 if (tracker == null) {
5842 tracker = new HistoryEventTracker();
5843 }
5844 tracker.updateState(rec.eventCode, rec.eventTag.string,
5845 rec.eventTag.uid, rec.eventTag.poolIdx);
5846 }
5847 }
5848 if (histStart >= 0) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07005849 commitCurrentHistoryBatchLocked();
Dianne Hackborn37de0982014-05-09 09:32:18 -07005850 pw.print(checkin ? "NEXT: " : " NEXT: "); pw.println(lastTime+1);
5851 }
5852 }
5853
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005854 private void dumpDailyLevelStepSummary(PrintWriter pw, String prefix, String label,
5855 LevelStepTracker steps, StringBuilder tmpSb, int[] tmpOutInt) {
5856 if (steps == null) {
5857 return;
5858 }
5859 long timeRemaining = steps.computeTimeEstimate(0, 0, tmpOutInt);
5860 if (timeRemaining >= 0) {
5861 pw.print(prefix); pw.print(label); pw.print(" total time: ");
5862 tmpSb.setLength(0);
5863 formatTimeMs(tmpSb, timeRemaining);
5864 pw.print(tmpSb);
5865 pw.print(" (from "); pw.print(tmpOutInt[0]);
5866 pw.println(" steps)");
5867 }
5868 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
5869 long estimatedTime = steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
5870 STEP_LEVEL_MODE_VALUES[i], tmpOutInt);
5871 if (estimatedTime > 0) {
5872 pw.print(prefix); pw.print(label); pw.print(" ");
5873 pw.print(STEP_LEVEL_MODE_LABELS[i]);
5874 pw.print(" time: ");
5875 tmpSb.setLength(0);
5876 formatTimeMs(tmpSb, estimatedTime);
5877 pw.print(tmpSb);
5878 pw.print(" (from "); pw.print(tmpOutInt[0]);
5879 pw.println(" steps)");
5880 }
5881 }
5882 }
5883
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005884 private void dumpDailyPackageChanges(PrintWriter pw, String prefix,
5885 ArrayList<PackageChange> changes) {
5886 if (changes == null) {
5887 return;
5888 }
5889 pw.print(prefix); pw.println("Package changes:");
5890 for (int i=0; i<changes.size(); i++) {
5891 PackageChange pc = changes.get(i);
5892 if (pc.mUpdate) {
5893 pw.print(prefix); pw.print(" Update "); pw.print(pc.mPackageName);
5894 pw.print(" vers="); pw.println(pc.mVersionCode);
5895 } else {
5896 pw.print(prefix); pw.print(" Uninstall "); pw.println(pc.mPackageName);
5897 }
5898 }
5899 }
5900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005901 /**
5902 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
5903 *
5904 * @param pw a Printer to receive the dump output.
5905 */
5906 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005907 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005908 prepareForDumpLocked();
5909
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005910 final boolean filtering = (flags
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005911 & (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005912
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005913 if ((flags&DUMP_HISTORY_ONLY) != 0 || !filtering) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005914 final long historyTotalSize = getHistoryTotalSize();
5915 final long historyUsedSize = getHistoryUsedSize();
5916 if (startIteratingHistoryLocked()) {
5917 try {
5918 pw.print("Battery History (");
5919 pw.print((100*historyUsedSize)/historyTotalSize);
5920 pw.print("% used, ");
5921 printSizeValue(pw, historyUsedSize);
5922 pw.print(" used of ");
5923 printSizeValue(pw, historyTotalSize);
5924 pw.print(", ");
5925 pw.print(getHistoryStringPoolSize());
5926 pw.print(" strings using ");
5927 printSizeValue(pw, getHistoryStringPoolBytes());
5928 pw.println("):");
Dianne Hackborn37de0982014-05-09 09:32:18 -07005929 dumpHistoryLocked(pw, flags, histStart, false);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005930 pw.println();
5931 } finally {
5932 finishIteratingHistoryLocked();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08005933 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005934 }
5935
5936 if (startIteratingOldHistoryLocked()) {
5937 try {
Dianne Hackborn37de0982014-05-09 09:32:18 -07005938 final HistoryItem rec = new HistoryItem();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005939 pw.println("Old battery History:");
5940 HistoryPrinter hprinter = new HistoryPrinter();
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005941 long baseTime = -1;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005942 while (getNextOldHistoryLocked(rec)) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07005943 if (baseTime < 0) {
5944 baseTime = rec.time;
5945 }
5946 hprinter.printNextItem(pw, rec, baseTime, false, (flags&DUMP_VERBOSE) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005947 }
5948 pw.println();
5949 } finally {
5950 finishIteratingOldHistoryLocked();
5951 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005952 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07005953 }
5954
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005955 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005956 return;
5957 }
5958
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005959 if (!filtering) {
5960 SparseArray<? extends Uid> uidStats = getUidStats();
5961 final int NU = uidStats.size();
5962 boolean didPid = false;
5963 long nowRealtime = SystemClock.elapsedRealtime();
5964 for (int i=0; i<NU; i++) {
5965 Uid uid = uidStats.valueAt(i);
5966 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
5967 if (pids != null) {
5968 for (int j=0; j<pids.size(); j++) {
5969 Uid.Pid pid = pids.valueAt(j);
5970 if (!didPid) {
5971 pw.println("Per-PID Stats:");
5972 didPid = true;
5973 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005974 long time = pid.mWakeSumMs + (pid.mWakeNesting > 0
5975 ? (nowRealtime - pid.mWakeStartMs) : 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005976 pw.print(" PID "); pw.print(pids.keyAt(j));
5977 pw.print(" wake time: ");
5978 TimeUtils.formatDuration(time, pw);
5979 pw.println("");
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005980 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005981 }
5982 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08005983 if (didPid) {
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005984 pw.println();
5985 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005986 }
5987
5988 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005989 if (dumpDurationSteps(pw, " ", "Discharge step durations:",
5990 getDischargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07005991 long timeRemaining = computeBatteryTimeRemaining(
5992 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07005993 if (timeRemaining >= 0) {
5994 pw.print(" Estimated discharge time remaining: ");
5995 TimeUtils.formatDuration(timeRemaining / 1000, pw);
5996 pw.println();
5997 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08005998 final LevelStepTracker steps = getDischargeLevelStepTracker();
5999 for (int i=0; i< STEP_LEVEL_MODES_OF_INTEREST.length; i++) {
6000 dumpTimeEstimate(pw, " Estimated ", STEP_LEVEL_MODE_LABELS[i], " time: ",
6001 steps.computeTimeEstimate(STEP_LEVEL_MODES_OF_INTEREST[i],
6002 STEP_LEVEL_MODE_VALUES[i], null));
6003 }
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006004 pw.println();
6005 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006006 if (dumpDurationSteps(pw, " ", "Charge step durations:",
6007 getChargeLevelStepTracker(), false)) {
Kweku Adamsb0449e02016-10-12 14:18:27 -07006008 long timeRemaining = computeChargeTimeRemaining(
6009 SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006010 if (timeRemaining >= 0) {
6011 pw.print(" Estimated charge time remaining: ");
6012 TimeUtils.formatDuration(timeRemaining / 1000, pw);
6013 pw.println();
6014 }
6015 pw.println();
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006016 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006017 }
6018 if (!filtering || (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0) {
6019 pw.println("Daily stats:");
6020 pw.print(" Current start time: ");
6021 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6022 getCurrentDailyStartTime()).toString());
6023 pw.print(" Next min deadline: ");
6024 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6025 getNextMinDailyDeadline()).toString());
6026 pw.print(" Next max deadline: ");
6027 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss",
6028 getNextMaxDailyDeadline()).toString());
6029 StringBuilder sb = new StringBuilder(64);
6030 int[] outInt = new int[1];
6031 LevelStepTracker dsteps = getDailyDischargeLevelStepTracker();
6032 LevelStepTracker csteps = getDailyChargeLevelStepTracker();
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006033 ArrayList<PackageChange> pkgc = getDailyPackageChanges();
6034 if (dsteps.mNumStepDurations > 0 || csteps.mNumStepDurations > 0 || pkgc != null) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006035 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006036 if (dumpDurationSteps(pw, " ", " Current daily discharge step durations:",
6037 dsteps, false)) {
6038 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6039 sb, outInt);
6040 }
6041 if (dumpDurationSteps(pw, " ", " Current daily charge step durations:",
6042 csteps, false)) {
6043 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6044 sb, outInt);
6045 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006046 dumpDailyPackageChanges(pw, " ", pkgc);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006047 } else {
6048 pw.println(" Current daily steps:");
6049 dumpDailyLevelStepSummary(pw, " ", "Discharge", dsteps,
6050 sb, outInt);
6051 dumpDailyLevelStepSummary(pw, " ", "Charge", csteps,
6052 sb, outInt);
6053 }
6054 }
6055 DailyItem dit;
6056 int curIndex = 0;
6057 while ((dit=getDailyItemLocked(curIndex)) != null) {
6058 curIndex++;
6059 if ((flags&DUMP_DAILY_ONLY) != 0) {
6060 pw.println();
6061 }
6062 pw.print(" Daily from ");
6063 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mStartTime).toString());
6064 pw.print(" to ");
6065 pw.print(DateFormat.format("yyyy-MM-dd-HH-mm-ss", dit.mEndTime).toString());
6066 pw.println(":");
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006067 if ((flags&DUMP_DAILY_ONLY) != 0 || !filtering) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006068 if (dumpDurationSteps(pw, " ",
6069 " Discharge step durations:", dit.mDischargeSteps, false)) {
6070 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6071 sb, outInt);
6072 }
6073 if (dumpDurationSteps(pw, " ",
6074 " Charge step durations:", dit.mChargeSteps, false)) {
6075 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6076 sb, outInt);
6077 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006078 dumpDailyPackageChanges(pw, " ", dit.mPackageChanges);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006079 } else {
6080 dumpDailyLevelStepSummary(pw, " ", "Discharge", dit.mDischargeSteps,
6081 sb, outInt);
6082 dumpDailyLevelStepSummary(pw, " ", "Charge", dit.mChargeSteps,
6083 sb, outInt);
6084 }
6085 }
6086 pw.println();
6087 }
6088 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006089 pw.println("Statistics since last charge:");
6090 pw.println(" System starts: " + getStartCount()
6091 + ", currently on battery: " + getIsOnBattery());
Dianne Hackbornd953c532014-08-16 18:17:38 -07006092 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid,
6093 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006094 pw.println();
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07006095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006096 }
6097
6098 @SuppressWarnings("unused")
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006099 public void dumpCheckinLocked(Context context, PrintWriter pw,
6100 List<ApplicationInfo> apps, int flags, long histStart) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006101 prepareForDumpLocked();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006102
6103 dumpLine(pw, 0 /* uid */, "i" /* category */, VERSION_DATA,
Dianne Hackborn0c820db2015-04-14 17:47:34 -07006104 CHECKIN_VERSION, getParcelVersion(), getStartPlatformVersion(),
6105 getEndPlatformVersion());
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006106
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006107 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
6108
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006109 final boolean filtering = (flags &
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006110 (DUMP_HISTORY_ONLY|DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) != 0;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006111
6112 if ((flags&DUMP_INCLUDE_HISTORY) != 0 || (flags&DUMP_HISTORY_ONLY) != 0) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07006113 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006114 try {
6115 for (int i=0; i<getHistoryStringPoolSize(); i++) {
6116 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
6117 pw.print(HISTORY_STRING_POOL); pw.print(',');
6118 pw.print(i);
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006119 pw.print(",");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006120 pw.print(getHistoryTagPoolUid(i));
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006121 pw.print(",\"");
6122 String str = getHistoryTagPoolString(i);
6123 str = str.replace("\\", "\\\\");
6124 str = str.replace("\"", "\\\"");
6125 pw.print(str);
6126 pw.print("\"");
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006127 pw.println();
6128 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006129 dumpHistoryLocked(pw, flags, histStart, true);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006130 } finally {
6131 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08006132 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006133 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07006134 }
6135
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006136 if (filtering && (flags&(DUMP_CHARGED_ONLY|DUMP_DAILY_ONLY)) == 0) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006137 return;
6138 }
6139
Dianne Hackborne4a59512010-12-07 11:08:07 -08006140 if (apps != null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006141 SparseArray<Pair<ArrayList<String>, MutableBoolean>> uids = new SparseArray<>();
Dianne Hackborne4a59512010-12-07 11:08:07 -08006142 for (int i=0; i<apps.size(); i++) {
6143 ApplicationInfo ai = apps.get(i);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006144 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(
6145 UserHandle.getAppId(ai.uid));
Dianne Hackborne4a59512010-12-07 11:08:07 -08006146 if (pkgs == null) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006147 pkgs = new Pair<>(new ArrayList<String>(), new MutableBoolean(false));
6148 uids.put(UserHandle.getAppId(ai.uid), pkgs);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006149 }
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006150 pkgs.first.add(ai.packageName);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006151 }
6152 SparseArray<? extends Uid> uidStats = getUidStats();
6153 final int NU = uidStats.size();
6154 String[] lineArgs = new String[2];
6155 for (int i=0; i<NU; i++) {
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006156 int uid = UserHandle.getAppId(uidStats.keyAt(i));
6157 Pair<ArrayList<String>, MutableBoolean> pkgs = uids.get(uid);
6158 if (pkgs != null && !pkgs.second.value) {
6159 pkgs.second.value = true;
6160 for (int j=0; j<pkgs.first.size(); j++) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08006161 lineArgs[0] = Integer.toString(uid);
Dianne Hackborn9cfba352016-03-24 17:31:28 -07006162 lineArgs[1] = pkgs.first.get(j);
Dianne Hackborne4a59512010-12-07 11:08:07 -08006163 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
6164 (Object[])lineArgs);
6165 }
6166 }
6167 }
6168 }
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006169 if (!filtering || (flags&DUMP_CHARGED_ONLY) != 0) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006170 dumpDurationSteps(pw, "", DISCHARGE_STEP_DATA, getDischargeLevelStepTracker(), true);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006171 String[] lineArgs = new String[1];
Kweku Adamsb0449e02016-10-12 14:18:27 -07006172 long timeRemaining = computeBatteryTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006173 if (timeRemaining >= 0) {
6174 lineArgs[0] = Long.toString(timeRemaining);
6175 dumpLine(pw, 0 /* uid */, "i" /* category */, DISCHARGE_TIME_REMAIN_DATA,
6176 (Object[])lineArgs);
6177 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006178 dumpDurationSteps(pw, "", CHARGE_STEP_DATA, getChargeLevelStepTracker(), true);
Kweku Adamsb0449e02016-10-12 14:18:27 -07006179 timeRemaining = computeChargeTimeRemaining(SystemClock.elapsedRealtime() * 1000);
Dianne Hackbornd06dcfd2014-05-02 13:49:30 -07006180 if (timeRemaining >= 0) {
6181 lineArgs[0] = Long.toString(timeRemaining);
6182 dumpLine(pw, 0 /* uid */, "i" /* category */, CHARGE_TIME_REMAIN_DATA,
6183 (Object[])lineArgs);
6184 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006185 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1,
6186 (flags&DUMP_DEVICE_WIFI_ONLY) != 0);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08006187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189}